Source code: org/enhydra/kelp/common/PropUtil.java
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 *
21 */
22 package org.enhydra.kelp.common;
23
24 // ToolBox imports
25 import org.enhydra.tool.ToolBoxInfo;
26
27 // Kelp imports
28 import org.enhydra.kelp.common.node.OtterNode;
29 import org.enhydra.kelp.common.node.PropertyKeys;
30
31 // Standard imports
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.StringTokenizer;
36 import java.util.Vector;
37
38 /**
39 * Class declaration
40 *
41 *
42 * @author Paul Mahar
43 */
44 public class PropUtil {
45
46 /**
47 * Constructor declaration
48 *
49 */
50 public PropUtil() {}
51
52 public static String removeQuotes(String in) {
53 String out = PropUtil.removeChar(in, '\'');
54
55 out = PropUtil.removeChar(out, '\"');
56 return out;
57 }
58
59 public static String removeChar(String in, char c) {
60 String out = new String();
61 StringBuffer buf = new StringBuffer();
62 int index = -1;
63
64 if (in == null) {
65
66 // done
67 } else {
68 out = new String(in);
69 }
70 index = out.indexOf(c);
71 while (index > -1) {
72 buf.setLength(0);
73 buf.append(out.substring(0, index));
74 buf.append(out.substring(index + 1, out.length()));
75 out = buf.toString();
76 index = out.indexOf(c);
77 }
78 return out;
79 }
80
81 /**
82 * Method declaration
83 *
84 *
85 * @param s
86 * @param def
87 *
88 * @return
89 */
90 public static int stringToInt(String s, int def) {
91 int i = def;
92
93 if (s != null) {
94 if (s.trim().length() > 0) {
95 try {
96 i = Integer.parseInt(s.trim());
97 } catch (Exception e) {
98 i = def;
99 }
100 }
101 }
102 return i;
103 }
104
105 /**
106 * Method declaration
107 *
108 *
109 * @param b
110 *
111 * @return
112 */
113 public static String booleanToString(boolean b) {
114 String flag = b ? Boolean.TRUE.toString() : Boolean.FALSE.toString();
115
116 return flag;
117 }
118
119 /**
120 * Method declaration
121 *
122 *
123 * @param s
124 * @param def
125 *
126 * @return
127 */
128 public static boolean stringToBoolean(String s, boolean def) {
129 boolean b = def;
130
131 if (s != null) {
132 if (s.trim().length() > 0) {
133 b = s.trim().equalsIgnoreCase(Boolean.TRUE.toString());
134 }
135 }
136 return b;
137 }
138
139 /**
140 * Method declaration
141 *
142 *
143 * @param node
144 * @param lengthName
145 * @param columnName1
146 * @param columnName2
147 *
148 * @return
149 */
150 public static String[][] getArrayProperty(OtterNode node,
151 String lengthName,
152 String columnName1,
153 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, String lengthName,
197 String columnName1,
198 String columnName2,
199 String[][] array) {
200 int rawLength = 0;
201 int newLength = 0;
202 String columnValue1 = new String();
203 String columnValue2 = new String();
204
205 rawLength = stringToInt(node.getProperty(lengthName), 100);
206 for (int i = 0; i < rawLength; i++) {
207 node.setProperty(columnName1 + '.' + i, null);
208 node.setProperty(columnName2 + '.' + i, null);
209 }
210 rawLength = array.length;
211 for (int i = 0; i < rawLength; i++) {
212 columnValue1 = array[i][0];
213 columnValue2 = array[i][1];
214 if (columnValue1.trim().length() > 0
215 && columnValue2.trim().length() > 0) {
216 node.setProperty(columnName1 + '.' + newLength, columnValue1);
217 node.setProperty(columnName2 + '.' + newLength, columnValue2);
218 newLength++;
219 }
220 }
221 node.setProperty(lengthName, newLength); // update to valid length
222 }
223
224 public static String arrayToList(String[] array) {
225 StringBuffer list = new StringBuffer();
226
227 for (int i = 0; i < array.length; i++) {
228 if (i > 0) {
229 list.append(';');
230 }
231 list.append(array[i]);
232 }
233 return list.toString();
234 }
235
236 public static String[] listToArray(String list, String[] defaultArray) {
237 String[] array = defaultArray;
238 StringTokenizer tokenizer = null;
239
240 if (list == null || list.trim().length() == 0) {}
241 else {
242
243 // string not to be resourced
244 final String DELIM = ";"; // nores
245
246 tokenizer = new StringTokenizer(list, DELIM);
247 array = new String[tokenizer.countTokens()];
248 int i = 0;
249
250 while (tokenizer.hasMoreTokens()) {
251 array[i] = tokenizer.nextToken();
252 i++;
253 }
254 }
255 return array;
256 }
257
258 public static String[] XMLCParametersToArray(String in) {
259 StringTokenizer tokenizer = null;
260 StringBuffer buf = null;
261 String cursor = null;
262 String[] out = new String[0];
263 Vector paramVector = new Vector();
264 int count = -1;
265
266 buf = new StringBuffer();
267 if (in == null) {
268 in = new String();
269 }
270 tokenizer = new StringTokenizer(in);
271 while (tokenizer.hasMoreTokens()) {
272 cursor = tokenizer.nextToken().trim();
273 if ((cursor.length() > 0) && (cursor.charAt(0) == '-')
274 && (buf.length() > 0)) {
275 paramVector.addElement(buf.toString().trim());
276 buf.setLength(0);
277 }
278 buf.append(' ');
279 buf.append(cursor);
280 }
281 if (buf.length() > 0) {
282 paramVector.addElement(buf.toString().trim());
283 }
284 paramVector.trimToSize();
285 out = new String[paramVector.size()];
286 out = (String[]) paramVector.toArray(out);
287 return out;
288 }
289
290 public static String XMLCParametersToString(String[] in) {
291 StringBuffer buf = new StringBuffer();
292
293 for (int i = 0; i < in.length; i++) {
294 buf.append(in[i]);
295 buf.append(Constants.TAB1);
296 }
297 return buf.toString().trim();
298 }
299
300 public static String cleanXMLCParameters(String in) {
301 String[] clean = new String[0];
302 String out = new String();
303
304 clean = PropUtil.XMLCParametersToArray(in);
305 clean = PropUtil.cleanXMLCParameters(clean);
306 out = PropUtil.XMLCParametersToString(clean);
307 return out;
308 }
309
310 public static String[] cleanXMLCParameters(String[] paramIn) {
311 String[] clean = new String[0];
312 String[] exclude = Constants.XMLC_NOOPS;
313 ArrayList list = null;
314
315 list = new ArrayList(Arrays.asList(paramIn));
316 for (int i = 0; i < exclude.length; i++) {
317 for (int j = 0; j < list.size(); j++) {
318 String cursor = null;
319
320 cursor = list.get(j).toString().toLowerCase();
321 if (cursor.startsWith(exclude[i])) {
322 list.remove(j);
323 list.trimToSize();
324 j--;
325 }
326 }
327 }
328 clean = new String[list.size()];
329 clean = (String[]) list.toArray(clean);
330 list.clear();
331 return clean;
332 }
333
334 public static String[] getDefaultContentTypes() {
335 String[] types = new String[0];
336 ArrayList list = null;
337
338 types = ToolBoxInfo.getSupportedDocTypes();
339 list = new ArrayList(Arrays.asList(PropertyKeys.DEFAULT_CONTENT));
340 for (int i = 0; i < types.length; i++) {
341 if (list.contains(types[i])) {
342
343 // ok
344 } else {
345 list.add(types[i]);
346 }
347 }
348 list.trimToSize();
349 types = new String[list.size()];
350 types = (String[]) list.toArray(types);
351 list.clear();
352 return types;
353 }
354
355 }