KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > PropUtil


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common;
24
25 // ToolBox imports
26
import org.enhydra.tool.ToolBoxInfo;
27
28 // Kelp imports
29
import org.enhydra.kelp.common.node.OtterNode;
30 import org.enhydra.kelp.common.node.PropertyKeys;
31
32 // Standard imports
33
import java.io.File JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import java.util.Vector JavaDoc;
38
39 /**
40  * Class declaration
41  *
42  *
43  * @author Paul Mahar
44  */

45 public class PropUtil {
46
47     /**
48      * Constructor declaration
49      *
50      */

51     public PropUtil() {}
52
53     public static String JavaDoc removeQuotes(String JavaDoc in) {
54         String JavaDoc out = PropUtil.removeChar(in, '\'');
55
56         out = PropUtil.removeChar(out, '\"');
57         return out;
58     }
59
60     public static String JavaDoc removeChar(String JavaDoc in, char c) {
61         String JavaDoc out = new String JavaDoc();
62         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
63         int index = -1;
64
65         if (in == null) {
66
67             // done
68
} else {
69             out = new String JavaDoc(in);
70         }
71         index = out.indexOf(c);
72         while (index > -1) {
73             buf.setLength(0);
74             buf.append(out.substring(0, index));
75             buf.append(out.substring(index + 1, out.length()));
76             out = buf.toString();
77             index = out.indexOf(c);
78         }
79         return out;
80     }
81
82     /**
83      * Method declaration
84      *
85      *
86      * @param s
87      * @param def
88      *
89      * @return
90      */

91     public static int stringToInt(String JavaDoc s, int def) {
92         int i = def;
93
94         if (s != null) {
95             if (s.trim().length() > 0) {
96                 try {
97                     i = Integer.parseInt(s.trim());
98                 } catch (Exception JavaDoc e) {
99                     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 JavaDoc booleanToString(boolean b) {
115         String JavaDoc flag = b ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
116
117         return flag;
118     }
119
120     /**
121      * Method declaration
122      *
123      *
124      * @param s
125      * @param def
126      *
127      * @return
128      */

129     public static boolean stringToBoolean(String JavaDoc s, boolean def) {
130         boolean b = def;
131
132         if (s != null) {
133             if (s.trim().length() > 0) {
134                 b = s.trim().equalsIgnoreCase(Boolean.TRUE.toString());
135             }
136         }
137         return b;
138     }
139
140     /**
141      * Method declaration
142      *
143      *
144      * @param node
145      * @param lengthName
146      * @param columnName1
147      * @param columnName2
148      *
149      * @return
150      */

151     public static String JavaDoc[][] getArrayProperty(OtterNode node,
152                                               String JavaDoc lengthName,
153                                               String JavaDoc columnName1,
154                                               String JavaDoc columnName2) {
155         String JavaDoc[][] array = new String JavaDoc[0][0];
156         String JavaDoc columnValue1 = null;
157         String JavaDoc columnValue2 = null;
158         Vector JavaDoc valueVector = new Vector JavaDoc();
159         int length = 0;
160
161         length = stringToInt(node.getProperty(lengthName), 100);
162         for (int i = 0; i < length; i++) {
163             columnValue1 = node.getProperty(columnName1 + '.' + i);
164             columnValue2 = node.getProperty(columnName2 + '.' + i);
165             if (columnValue1 != null && columnValue2 != null) {
166                 if (columnValue1.trim().length() > 0
167                         && columnValue2.trim().length() > 0) {
168
169                     // note: new 'row' object is needed for each call to addElement
170
String JavaDoc[] row = new String JavaDoc[2];
171
172                     row[0] = columnValue1;
173                     row[1] = columnValue2;
174                     valueVector.addElement(row);
175                 }
176             }
177         }
178         length = valueVector.size();
179         node.setProperty(lengthName, length); // update to valid length
180
String JavaDoc[] row = new String JavaDoc[2];
181
182         array = new String JavaDoc[length][2];
183         for (int i = 0; i < length; i++) {
184             row = (String JavaDoc[]) valueVector.elementAt(i);
185             array[i][0] = row[0];
186             array[i][1] = row[1];
187         }
188         return array;
189     }
190
191     /**
192      * Method declaration
193      *
194      *
195      * @param map
196      */

197     public static void putArrayProperty(OtterNode node, String JavaDoc lengthName,
198                                         String JavaDoc columnName1,
199                                         String JavaDoc columnName2,
200                                         String JavaDoc[][] array) {
201         int rawLength = 0;
202         int newLength = 0;
203         String JavaDoc columnValue1 = new String JavaDoc();
204         String JavaDoc columnValue2 = new String JavaDoc();
205
206         rawLength = stringToInt(node.getProperty(lengthName), 100);
207         for (int i = 0; i < rawLength; i++) {
208             node.setProperty(columnName1 + '.' + i, null);
209             node.setProperty(columnName2 + '.' + i, null);
210         }
211         rawLength = array.length;
212         for (int i = 0; i < rawLength; i++) {
213             columnValue1 = array[i][0];
214             columnValue2 = array[i][1];
215             if (columnValue1.trim().length() > 0
216                     && columnValue2.trim().length() > 0) {
217                 node.setProperty(columnName1 + '.' + newLength, columnValue1);
218                 node.setProperty(columnName2 + '.' + newLength, columnValue2);
219                 newLength++;
220             }
221         }
222         node.setProperty(lengthName, newLength); // update to valid length
223
}
224
225     public static String JavaDoc arrayToList(String JavaDoc[] array) {
226         StringBuffer JavaDoc list = new StringBuffer JavaDoc();
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 JavaDoc[] listToArray(String JavaDoc list, String JavaDoc[] defaultArray) {
238         String JavaDoc[] array = defaultArray;
239         StringTokenizer JavaDoc tokenizer = null;
240
241         if (list == null || list.trim().length() == 0) {}
242         else {
243
244             // string not to be resourced
245
final String JavaDoc DELIM = ";"; // nores
246

247             tokenizer = new StringTokenizer JavaDoc(list, DELIM);
248             array = new String JavaDoc[tokenizer.countTokens()];
249             int i = 0;
250
251             while (tokenizer.hasMoreTokens()) {
252                 array[i] = tokenizer.nextToken();
253                 i++;
254             }
255         }
256         return array;
257     }
258
259     public static String JavaDoc[] XMLCParametersToArray(String JavaDoc in) {
260         StringTokenizer JavaDoc tokenizer = null;
261         StringBuffer JavaDoc buf = null;
262         String JavaDoc cursor = null;
263         String JavaDoc[] out = new String JavaDoc[0];
264         Vector JavaDoc paramVector = new Vector JavaDoc();
265         int count = -1;
266
267         buf = new StringBuffer JavaDoc();
268         if (in == null) {
269             in = new String JavaDoc();
270         }
271         tokenizer = new StringTokenizer JavaDoc(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 JavaDoc[paramVector.size()];
287         out = (String JavaDoc[]) paramVector.toArray(out);
288         return out;
289     }
290
291     public static String JavaDoc XMLCParametersToString(String JavaDoc[] in) {
292         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc cleanXMLCParameters(String JavaDoc in) {
302         String JavaDoc[] clean = new String JavaDoc[0];
303         String JavaDoc out = new String JavaDoc();
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 JavaDoc[] cleanXMLCParameters(String JavaDoc[] paramIn) {
312         String JavaDoc[] clean = new String JavaDoc[0];
313         String JavaDoc[] exclude = Constants.XMLC_NOOPS;
314         ArrayList JavaDoc list = null;
315
316         list = new ArrayList JavaDoc(Arrays.asList(paramIn));
317         for (int i = 0; i < exclude.length; i++) {
318             for (int j = 0; j < list.size(); j++) {
319                 String JavaDoc 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 JavaDoc[list.size()];
330         clean = (String JavaDoc[]) list.toArray(clean);
331         list.clear();
332         return clean;
333     }
334
335     public static String JavaDoc[] getDefaultContentTypes() {
336         String JavaDoc[] types = new String JavaDoc[0];
337         ArrayList JavaDoc list = null;
338
339         types = ToolBoxInfo.getSupportedDocTypes();
340         list = new ArrayList JavaDoc(Arrays.asList(PropertyKeys.DEFAULT_CONTENT));
341         for (int i = 0; i < types.length; i++) {
342             if (list.contains(types[i])) {
343
344                 // ok
345
} else {
346                 list.add(types[i]);
347             }
348         }
349         list.trimToSize();
350         types = new String JavaDoc[list.size()];
351         types = (String JavaDoc[]) list.toArray(types);
352         list.clear();
353         return types;
354     }
355
356 }
357
Popular Tags