001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common;
024:
025: // ToolBox imports
026: import org.enhydra.tool.ToolBoxInfo;
027:
028: // Kelp imports
029: import org.enhydra.kelp.common.node.OtterNode;
030: import org.enhydra.kelp.common.node.PropertyKeys;
031:
032: // Standard imports
033: import java.util.ArrayList;
034: import java.util.Arrays;
035: import java.util.StringTokenizer;
036: import java.util.Vector;
037:
038: /**
039: * Class declaration
040: *
041: *
042: * @author Paul Mahar
043: */
044: public class PropUtil {
045:
046: /**
047: * Constructor declaration
048: *
049: */
050: public PropUtil() {
051: }
052:
053: public static String removeQuotes(String in) {
054: String out = PropUtil.removeChar(in, '\'');
055:
056: out = PropUtil.removeChar(out, '\"');
057: return out;
058: }
059:
060: public static String removeChar(String in, char c) {
061: String out = new String();
062: StringBuffer buf = new StringBuffer();
063: int index = -1;
064:
065: if (in == null) {
066:
067: // done
068: } else {
069: out = new String(in);
070: }
071: index = out.indexOf(c);
072: while (index > -1) {
073: buf.setLength(0);
074: buf.append(out.substring(0, index));
075: buf.append(out.substring(index + 1, out.length()));
076: out = buf.toString();
077: index = out.indexOf(c);
078: }
079: return out;
080: }
081:
082: /**
083: * Method declaration
084: *
085: *
086: * @param s
087: * @param def
088: *
089: * @return
090: */
091: public static int stringToInt(String s, int def) {
092: int i = def;
093:
094: if (s != null) {
095: if (s.trim().length() > 0) {
096: try {
097: i = Integer.parseInt(s.trim());
098: } catch (Exception e) {
099: i = def;
100: }
101: }
102: }
103: return i;
104: }
105:
106: /**
107: * Method declaration
108: *
109: *
110: * @param b
111: *
112: * @return
113: */
114: public static String booleanToString(boolean b) {
115: String flag = b ? Boolean.TRUE.toString() : Boolean.FALSE
116: .toString();
117:
118: return flag;
119: }
120:
121: /**
122: * Method declaration
123: *
124: *
125: * @param s
126: * @param def
127: *
128: * @return
129: */
130: public static boolean stringToBoolean(String s, boolean def) {
131: boolean b = def;
132:
133: if (s != null) {
134: if (s.trim().length() > 0) {
135: b = s.trim().equalsIgnoreCase(Boolean.TRUE.toString());
136: }
137: }
138: return b;
139: }
140:
141: /**
142: * Method declaration
143: *
144: *
145: * @param node
146: * @param lengthName
147: * @param columnName1
148: * @param columnName2
149: *
150: * @return
151: */
152: public static String[][] getArrayProperty(OtterNode node,
153: String lengthName, String columnName1, String columnName2) {
154: String[][] array = new String[0][0];
155: String columnValue1 = null;
156: String columnValue2 = null;
157: Vector valueVector = new Vector();
158: int length = 0;
159:
160: length = stringToInt(node.getProperty(lengthName), 100);
161: for (int i = 0; i < length; i++) {
162: columnValue1 = node.getProperty(columnName1 + '.' + i);
163: columnValue2 = node.getProperty(columnName2 + '.' + i);
164: if (columnValue1 != null && columnValue2 != null) {
165: if (columnValue1.trim().length() > 0
166: && columnValue2.trim().length() > 0) {
167:
168: // note: new 'row' object is needed for each call to addElement
169: String[] row = new String[2];
170:
171: row[0] = columnValue1;
172: row[1] = columnValue2;
173: valueVector.addElement(row);
174: }
175: }
176: }
177: length = valueVector.size();
178: node.setProperty(lengthName, length); // update to valid length
179: String[] row = new String[2];
180:
181: array = new String[length][2];
182: for (int i = 0; i < length; i++) {
183: row = (String[]) valueVector.elementAt(i);
184: array[i][0] = row[0];
185: array[i][1] = row[1];
186: }
187: return array;
188: }
189:
190: /**
191: * Method declaration
192: *
193: *
194: * @param map
195: */
196: public static void putArrayProperty(OtterNode node,
197: String lengthName, String columnName1, String columnName2,
198: String[][] array) {
199: int rawLength = 0;
200: int newLength = 0;
201: String columnValue1 = new String();
202: String columnValue2 = new String();
203:
204: rawLength = stringToInt(node.getProperty(lengthName), 100);
205: for (int i = 0; i < rawLength; i++) {
206: node.setProperty(columnName1 + '.' + i, null);
207: node.setProperty(columnName2 + '.' + i, null);
208: }
209: rawLength = array.length;
210: for (int i = 0; i < rawLength; i++) {
211: columnValue1 = array[i][0];
212: columnValue2 = array[i][1];
213: if (columnValue1.trim().length() > 0
214: && columnValue2.trim().length() > 0) {
215: node.setProperty(columnName1 + '.' + newLength,
216: columnValue1);
217: node.setProperty(columnName2 + '.' + newLength,
218: columnValue2);
219: newLength++;
220: }
221: }
222: node.setProperty(lengthName, newLength); // update to valid length
223: }
224:
225: public static String arrayToList(String[] array) {
226: StringBuffer list = new StringBuffer();
227:
228: for (int i = 0; i < array.length; i++) {
229: if (i > 0) {
230: list.append(';');
231: }
232: list.append(array[i]);
233: }
234: return list.toString();
235: }
236:
237: public static String[] listToArray(String list,
238: String[] defaultArray) {
239: String[] array = defaultArray;
240: StringTokenizer tokenizer = null;
241:
242: if (list == null || list.trim().length() == 0) {
243: } else {
244:
245: // string not to be resourced
246: final String DELIM = ";"; // nores
247:
248: tokenizer = new StringTokenizer(list, DELIM);
249: array = new String[tokenizer.countTokens()];
250: int i = 0;
251:
252: while (tokenizer.hasMoreTokens()) {
253: array[i] = tokenizer.nextToken();
254: i++;
255: }
256: }
257: return array;
258: }
259:
260: public static String[] XMLCParametersToArray(String in) {
261: StringTokenizer tokenizer = null;
262: StringBuffer buf = null;
263: String cursor = null;
264: String[] out = new String[0];
265: Vector paramVector = new Vector();
266:
267: buf = new StringBuffer();
268: if (in == null) {
269: in = new String();
270: }
271: tokenizer = new StringTokenizer(in);
272: while (tokenizer.hasMoreTokens()) {
273: cursor = tokenizer.nextToken().trim();
274: if ((cursor.length() > 0) && (cursor.charAt(0) == '-')
275: && (buf.length() > 0)) {
276: paramVector.addElement(buf.toString().trim());
277: buf.setLength(0);
278: }
279: buf.append(' ');
280: buf.append(cursor);
281: }
282: if (buf.length() > 0) {
283: paramVector.addElement(buf.toString().trim());
284: }
285: paramVector.trimToSize();
286: out = new String[paramVector.size()];
287: out = (String[]) paramVector.toArray(out);
288: return out;
289: }
290:
291: public static String XMLCParametersToString(String[] in) {
292: StringBuffer buf = new StringBuffer();
293:
294: for (int i = 0; i < in.length; i++) {
295: buf.append(in[i]);
296: buf.append(Constants.TAB1);
297: }
298: return buf.toString().trim();
299: }
300:
301: public static String cleanXMLCParameters(String in) {
302: String[] clean = new String[0];
303: String out = new String();
304:
305: clean = PropUtil.XMLCParametersToArray(in);
306: clean = PropUtil.cleanXMLCParameters(clean);
307: out = PropUtil.XMLCParametersToString(clean);
308: return out;
309: }
310:
311: public static String[] cleanXMLCParameters(String[] paramIn) {
312: String[] clean = new String[0];
313: String[] exclude = Constants.XMLC_NOOPS;
314: ArrayList list = null;
315:
316: list = new ArrayList(Arrays.asList(paramIn));
317: for (int i = 0; i < exclude.length; i++) {
318: for (int j = 0; j < list.size(); j++) {
319: String cursor = null;
320:
321: cursor = list.get(j).toString().toLowerCase();
322: if (cursor.startsWith(exclude[i])) {
323: list.remove(j);
324: list.trimToSize();
325: j--;
326: }
327: }
328: }
329: clean = new String[list.size()];
330: clean = (String[]) list.toArray(clean);
331: list.clear();
332: return clean;
333: }
334:
335: public static String[] getDefaultContentTypes() {
336: String[] types = new String[0];
337: ArrayList list = null;
338:
339: types = ToolBoxInfo.getSupportedDocTypes();
340: list = new ArrayList(Arrays
341: .asList(PropertyKeys.DEFAULT_CONTENT));
342: for (int i = 0; i < types.length; i++) {
343: if (list.contains(types[i])) {
344:
345: // ok
346: } else {
347: list.add(types[i]);
348: }
349: }
350: list.trimToSize();
351: types = new String[list.size()];
352: types = (String[]) list.toArray(types);
353: list.clear();
354: return types;
355: }
356:
357: }
|