1 23 package org.enhydra.kelp.common; 24 25 import org.enhydra.tool.ToolBoxInfo; 27 28 import org.enhydra.kelp.common.node.OtterNode; 30 import org.enhydra.kelp.common.node.PropertyKeys; 31 32 import java.io.File ; 34 import java.util.ArrayList ; 35 import java.util.Arrays ; 36 import java.util.StringTokenizer ; 37 import java.util.Vector ; 38 39 45 public class PropUtil { 46 47 51 public PropUtil() {} 52 53 public static String removeQuotes(String in) { 54 String out = PropUtil.removeChar(in, '\''); 55 56 out = PropUtil.removeChar(out, '\"'); 57 return out; 58 } 59 60 public static String removeChar(String in, char c) { 61 String out = new String (); 62 StringBuffer buf = new StringBuffer (); 63 int index = -1; 64 65 if (in == null) { 66 67 } else { 69 out = new String (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 91 public static int stringToInt(String 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 e) { 99 i = def; 100 } 101 } 102 } 103 return i; 104 } 105 106 114 public static String booleanToString(boolean b) { 115 String flag = b ? Boolean.TRUE.toString() : Boolean.FALSE.toString(); 116 117 return flag; 118 } 119 120 129 public static boolean stringToBoolean(String 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 151 public static String [][] getArrayProperty(OtterNode node, 152 String lengthName, 153 String columnName1, 154 String columnName2) { 155 String [][] array = new String [0][0]; 156 String columnValue1 = null; 157 String columnValue2 = null; 158 Vector valueVector = new Vector (); 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 String [] row = new String [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); String [] row = new String [2]; 181 182 array = new String [length][2]; 183 for (int i = 0; i < length; i++) { 184 row = (String []) valueVector.elementAt(i); 185 array[i][0] = row[0]; 186 array[i][1] = row[1]; 187 } 188 return array; 189 } 190 191 197 public static void putArrayProperty(OtterNode node, String lengthName, 198 String columnName1, 199 String columnName2, 200 String [][] array) { 201 int rawLength = 0; 202 int newLength = 0; 203 String columnValue1 = new String (); 204 String columnValue2 = new String (); 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); } 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, String [] defaultArray) { 238 String [] array = defaultArray; 239 StringTokenizer tokenizer = null; 240 241 if (list == null || list.trim().length() == 0) {} 242 else { 243 244 final String DELIM = ";"; 247 tokenizer = new StringTokenizer (list, DELIM); 248 array = new String [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 [] XMLCParametersToArray(String in) { 260 StringTokenizer tokenizer = null; 261 StringBuffer buf = null; 262 String cursor = null; 263 String [] out = new String [0]; 264 Vector paramVector = new Vector (); 265 int count = -1; 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.asList(PropertyKeys.DEFAULT_CONTENT)); 341 for (int i = 0; i < types.length; i++) { 342 if (list.contains(types[i])) { 343 344 } else { 346 list.add(types[i]); 347 } 348 } 349 list.trimToSize(); 350 types = new String [list.size()]; 351 types = (String []) list.toArray(types); 352 list.clear(); 353 return types; 354 } 355 356 } 357 | Popular Tags |