001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.portletcontainercommon;
015:
016: import java.util.List;
017: import java.util.ArrayList;
018:
019: public class PortletPreferencesUtility {
020:
021: private static String ARRAY_DELIMITER = "|";
022: private static String SPACE = " ";
023: private static char ARRAY_DELIMITER_CHAR = ARRAY_DELIMITER
024: .charAt(0);
025: private static String ESCAPED_ARRAY_DELIMITER = ARRAY_DELIMITER
026: + ARRAY_DELIMITER;
027: private static int ARRAY_DELIMITER_LENGTH = ARRAY_DELIMITER
028: .length();
029: private static int ESCAPED_ARRAY_DELIMITER_LENGTH = ESCAPED_ARRAY_DELIMITER
030: .length();
031: public static final String NULL_STRING = "@@$$NULL_STRING$$@@";
032: private static final String EMPTY_STRING = "@@$$EMPTY_STRING$$@@";
033:
034: /**
035: * Converts the List of preference values by using | character as the
036: * delimiter.
037: * @param prefs list of preference values
038: * @return String that has preference values delimited by |
039: */
040: public static String getPreferenceString(List prefs) {
041: if (prefs != null) {
042: StringBuffer sb = new StringBuffer("");
043: for (int i = 0; i < prefs.size(); i++) {
044: sb.append(ARRAY_DELIMITER);
045: sb.append(getPreferenceString((String) prefs.get(i)));
046: }
047: return sb.toString();
048: } else {
049: return null;
050: }
051: }
052:
053: /**
054: * Converts the List of preference values by using | character as the
055: * delimiter.
056: * @param prefs array of preference values
057: * @return String that has preference values delimited by |
058: */
059: public static String getPreferenceString(String[] prefs) {
060: if (prefs != null) {
061: StringBuffer sb = new StringBuffer("");
062: for (int i = 0; i < prefs.length; i++) {
063: sb.append(ARRAY_DELIMITER);
064: sb.append(getPreferenceString(prefs[i]));
065: }
066: return sb.toString();
067: } else {
068: return null;
069: }
070: }
071:
072: // If the value includes |, then its escaped by adding one more | character.
073: // Eg: if pref="v1|v2", then the returned value will be "v1||v2"
074: private static String getPreferenceString(String pref) {
075: if (pref == null) {
076: pref = NULL_STRING;
077: } else if (pref.length() == 0) {
078: pref = EMPTY_STRING;
079: }
080: StringBuffer prefBuffer = new StringBuffer(pref);
081: // pref should never start with '|' and never end with '|'. In case
082: // such a thing happens, its abnormal, so in order for the
083: // parsing algorithm to work properly, if pref starts with '|', insert a space
084: // and if pref endswith '|' append a space.
085: if (pref.startsWith(ARRAY_DELIMITER))
086: prefBuffer.insert(0, SPACE);
087: if (pref.endsWith(ARRAY_DELIMITER))
088: prefBuffer.insert(prefBuffer.length(), SPACE);
089: // If the preference value contains |, then add one more |
090: int arrayDelimiterIndex = prefBuffer.indexOf(ARRAY_DELIMITER);
091: if (arrayDelimiterIndex != -1) {
092: while (arrayDelimiterIndex != -1) {
093: prefBuffer.replace(arrayDelimiterIndex,
094: arrayDelimiterIndex + ARRAY_DELIMITER_LENGTH,
095: ESCAPED_ARRAY_DELIMITER);
096: arrayDelimiterIndex = prefBuffer.indexOf(
097: ARRAY_DELIMITER, arrayDelimiterIndex
098: + ESCAPED_ARRAY_DELIMITER_LENGTH);
099: }
100: }
101: return prefBuffer.toString();
102: }
103:
104: /**
105: * Converts the preference value string into a list. Multiple preference
106: * values are delimited by '|'.
107: * @param pref multiple preference value delimited by '|'.
108: * @return List of preference values.
109: */
110: public static List getPreferenceValues(String pref) {
111:
112: ArrayList prefArray = new ArrayList();
113: if (pref == null || pref.length() == 0) {
114: return prefArray;
115: }
116:
117: StringBuffer valueBuffer = new StringBuffer();
118: StringBuffer prefBuffer = new StringBuffer(pref);
119: boolean start = false;
120:
121: // pref from DP should always start with '|' and never end with '|'. In case
122: // such a thing happens, its abnormal, however just to be nice and not
123: // blow things,
124: // (a)if pref does not start with '|', insert '|'
125: // (b)if pref starts with '||' , insert '| '
126: // (c)if pref ends with '|', insert ' |'.
127:
128: if (pref.startsWith(ARRAY_DELIMITER)) {
129: if (pref.startsWith(ESCAPED_ARRAY_DELIMITER)) {
130: prefBuffer.insert(0, SPACE);
131: prefBuffer.insert(0, ARRAY_DELIMITER);
132: }
133: } else {
134: prefBuffer.insert(0, ARRAY_DELIMITER);
135: }
136: if (pref.endsWith(ARRAY_DELIMITER)) {
137: prefBuffer.insert(prefBuffer.length(), SPACE);
138: prefBuffer.insert(prefBuffer.length(), ARRAY_DELIMITER);
139: } else {
140: prefBuffer.insert(prefBuffer.length(), ARRAY_DELIMITER);
141: }
142:
143: /*
144: In order to convert the preferences value back into array
145: check for the array delimiter '|'. If if the value contains '||'
146: include it. Before adding into the array, replace '||' by '|'
147: */
148: String modifiedPref = prefBuffer.toString();
149: char[] prefCharArray = modifiedPref.toCharArray();
150: int modifiedPrefLength = prefCharArray.length;
151: for (int i = 0; i < prefCharArray.length; i++) {
152: if (!start && (prefCharArray[i] == '|')) {
153: start = true;
154: valueBuffer = new StringBuffer();
155: } else if (start
156: && (prefCharArray[i] == ARRAY_DELIMITER_CHAR
157: && i + 1 != modifiedPrefLength && prefCharArray[i + 1] == ARRAY_DELIMITER_CHAR)) {
158: valueBuffer.append(prefCharArray[i]);
159: valueBuffer.append(prefCharArray[i + 1]);
160: i = i + 1;
161: } else if (start
162: && (prefCharArray[i] == ARRAY_DELIMITER_CHAR && (i + 1 == modifiedPrefLength || prefCharArray[i + 1] != ARRAY_DELIMITER_CHAR))) {
163: if (valueBuffer.equals(NULL_STRING)) {
164: valueBuffer = null;
165: } else if (valueBuffer.indexOf(EMPTY_STRING) != -1) {
166: valueBuffer = new StringBuffer("");
167: } else {
168: replaceEscapedArrayDelimiter(valueBuffer);
169: }
170: prefArray.add(valueBuffer.toString());
171: valueBuffer = new StringBuffer();
172: } else if (start) {
173: valueBuffer.append(prefCharArray[i]);
174: }
175: }
176: return prefArray;
177: }
178:
179: private static void replaceEscapedArrayDelimiter(
180: StringBuffer valueBuffer) {
181: int index = valueBuffer.indexOf(ESCAPED_ARRAY_DELIMITER);
182: while (index != -1) {
183: valueBuffer.replace(index, index
184: + ESCAPED_ARRAY_DELIMITER_LENGTH, ARRAY_DELIMITER);
185: index = valueBuffer.indexOf(ESCAPED_ARRAY_DELIMITER, index
186: + ARRAY_DELIMITER_LENGTH);
187: }
188: }
189: }
|