001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend.editor;
017:
018: import org.outerj.daisy.repository.ValueType;
019: import org.outerj.daisy.repository.Repository;
020: import org.outerj.daisy.repository.HierarchyPath;
021: import org.outerj.daisy.repository.VariantKey;
022:
023: import java.util.List;
024: import java.util.ArrayList;
025:
026: /**
027: * Helper methods related to editing hierarchical values as
028: * one string, whereby the individual elements of the hierarchical
029: * value are separated by slashes.
030: *
031: * <p>Double slash is used to escape slash when slash is used in a value.
032: * Whitespace around the separator slashes is not considered to be significant
033: * (so it is not possible to input values which have whitespace at the start
034: * or end).
035: */
036: public class HierarchicalFieldHelper {
037:
038: /**
039: * Parses a hierarchical value inputted using the slash-separator syntax.
040: */
041: public static String[] parseHierarchicalInput(String input) {
042: List<String> parts = new ArrayList<String>();
043: StringBuilder currentPartBuffer = new StringBuilder();
044:
045: for (int i = 0; i < input.length(); i++) {
046: char c = input.charAt(i);
047: switch (c) {
048: case '/':
049: if (i + 1 < input.length()
050: && input.charAt(i + 1) == '/') {
051: // it is an escaped slash
052: currentPartBuffer.append(c);
053: i++; // skip next char
054: } else {
055: handlePartInput(currentPartBuffer, parts);
056: }
057: break;
058: default:
059: currentPartBuffer.append(c);
060: }
061: }
062:
063: handlePartInput(currentPartBuffer, parts);
064:
065: return parts.toArray(new String[0]);
066: }
067:
068: private static void handlePartInput(StringBuilder partInputBuffer,
069: List<String> parts) {
070: if (partInputBuffer.length() > 0) { // can be the case at the start of the string
071: String part = partInputBuffer.toString().trim();
072: if (part.length() > 0) // slashes separated by only whitespace are skipped
073: parts.add(part);
074: partInputBuffer.setLength(0);
075: }
076: }
077:
078: public static String[] convertHierarchyPathsToString(
079: Object[] hierarchyPaths, ValueType valueType,
080: Repository repository) {
081: String[] strings = new String[hierarchyPaths.length];
082: for (int i = 0; i < hierarchyPaths.length; i++) {
083: strings[i] = convertHierarchyPathToString(
084: (HierarchyPath) hierarchyPaths[i], valueType,
085: repository);
086: }
087: return strings;
088: }
089:
090: public static String convertHierarchyPathToString(
091: HierarchyPath hierarchyPath, ValueType valueType,
092: Repository repository) {
093: Object[] elements = hierarchyPath.getElements();
094: StringBuilder builder = new StringBuilder();
095: for (Object element : elements) {
096: if (builder.length() > 0)
097: builder.append(" / ");
098: String value;
099: if (valueType == ValueType.STRING) {
100: value = (String) element;
101: } else if (valueType == ValueType.LINK) {
102: value = LinkFieldHelper.variantKeyToString(
103: (VariantKey) element, repository
104: .getVariantManager());
105: } else {
106: throw new RuntimeException(
107: "Unexpected valuetype for hierarhical field: "
108: + valueType);
109: }
110: // escape slashes with double slashes
111: value = value.replaceAll("/", "//");
112: builder.append(value);
113: }
114: return builder.toString();
115: }
116:
117: }
|