1 package org.sapia.util.xml.confix; 2 3 4 import java.lang.reflect.InvocationTargetException ; 7 import java.lang.reflect.Method ; 8 9 10 23 public abstract class AbstractXMLProcessor implements ConfixProcessorIF { 24 28 29 private ObjectFactoryIF _theObjectFactory; 30 31 35 40 public AbstractXMLProcessor(ObjectFactoryIF anObjectFactory) { 41 _theObjectFactory = anObjectFactory; 42 } 43 44 48 62 protected static void assignToParent(Object aParent, Object aChild, 63 String anElementName) throws ConfigurationException { 64 try { 65 String aMethodName = "set" + 66 toMethodName(formatElementName(anElementName)); 67 invokeMethod(aMethodName, aParent, aChild); 68 } catch (ConfigurationException ce) { 69 String aMessage = "Error assigning the child object " + aChild + 70 " with the setXXX method on the parent object " + aParent; 71 throw new ConfigurationException(aMessage, ce); 72 } catch (NoSuchMethodException nsme) { 73 try { 74 String aMethodName = "add" + 75 toMethodName(formatElementName(anElementName)); 76 try{ 77 invokeMethod(aMethodName, aParent, aChild); 78 }catch(NoSuchMethodException e){ 79 try{ 80 invokeSetterOrAdder(aParent, aChild); 81 }catch(NoSuchMethodException e2){ 82 throw e; 83 } 84 } 85 } catch (ConfigurationException ce) { 86 String aMessage = "Error assigning the child object " + aChild + 87 " with the addXXX method on the parent object " + aParent; 88 throw new ConfigurationException(aMessage, ce); 89 } catch (NoSuchMethodException nsme2) { 90 if (aParent instanceof ObjectWrapperIF) { 92 aParent = ((ObjectWrapperIF) aParent).getWrappedObject(); 93 assignToParent(aParent, aChild, anElementName); 94 } 95 else if (aParent instanceof ObjectHandlerIF) { 97 ((ObjectHandlerIF) aParent).handleObject(anElementName, aChild); 98 } else { 99 String aMessage = 100 "There's no setXXX or addXXX method to assign the object " + 101 aChild + " on the parent object " + aParent; 102 throw new ConfigurationException(aMessage); 103 } 104 } 105 } 106 } 107 108 120 protected static void invokeSetter(String anElementName, Object aTarget, 121 String anAttributeName, String aValue) throws ConfigurationException { 122 try { 123 String aMethodName = "set" + 124 toMethodName(formatElementName(anAttributeName)); 125 invokeMethod(aMethodName, aTarget, aValue); 126 } catch (ConfigurationException ce) { 127 String aMessage = 128 "Application error invoking the setXXX method to assign the value " + 129 aValue + " for the attribute " + anAttributeName + " on the object " + 130 aTarget; 131 throw new ConfigurationException(aMessage, ce); 132 } catch (NoSuchMethodException nsme) { 133 try{ 134 String aMethodName = "add" + toMethodName(formatElementName(anAttributeName)); 135 invokeMethod(aMethodName, aTarget, aValue); 136 }catch(NoSuchMethodException nsme2){ 138 if (aTarget instanceof ObjectWrapperIF) { 139 aTarget = ((ObjectWrapperIF) aTarget).getWrappedObject(); 140 invokeSetter(anElementName, aTarget, anAttributeName, aValue); 141 } 142 else if (aTarget instanceof ObjectHandlerIF) { 144 ((ObjectHandlerIF) aTarget).handleObject(anAttributeName, aValue); 145 } 146 else { 148 String aMessage = "There's no setXXX method to assign the value " + 149 aValue + " for the attribute/element " + anAttributeName + 150 " on the object " + aTarget; 151 throw new ConfigurationException(aMessage, nsme); 152 } 153 } 154 } 155 } 156 157 164 protected static Object invokeSetterOrAdder(Object aTarget, Object aValue) 165 throws ConfigurationException, NoSuchMethodException { 166 String methodName = "set"; 167 try{ 168 return invokeMethod(methodName, aTarget, aValue); 169 }catch(NoSuchMethodException e){ 170 methodName = "add"; 171 return invokeMethod(methodName, aTarget, aValue); 172 } 173 } 174 175 183 protected static Object invokeMethod(String aMethodName, Object aTarget, 184 Object aValue) throws ConfigurationException, NoSuchMethodException { 185 try { 186 Method [] someMethods = aTarget.getClass().getMethods(); 187 188 for (int i = 0; i < someMethods.length; i++) { 189 if (someMethods[i].getName().equals(aMethodName) && 190 (someMethods[i].getParameterTypes().length == 1)) { 191 Method aMethod = someMethods[i]; 192 193 Class aParamType = aMethod.getParameterTypes()[0]; 194 195 if (aParamType.isAssignableFrom(aValue.getClass())) { 196 return aMethod.invoke(aTarget, new Object [] { aValue }); 197 } 198 else if ((aParamType.equals(boolean.class) || 200 aParamType.equals(Boolean .class)) && aValue instanceof String ) { 201 String aStringValue = (String ) aValue; 202 Boolean aBoolean = new Boolean (aStringValue.equalsIgnoreCase("true") || 203 aStringValue.equalsIgnoreCase("yes")); 204 205 return aMethod.invoke(aTarget, new Object [] { aBoolean }); 206 } else if (aParamType.equals(boolean.class) && 207 aValue instanceof Boolean ) { 208 return aMethod.invoke(aTarget, new Object [] { aValue }); 209 } 210 else if ((aParamType.equals(byte.class) || 212 aParamType.equals(Byte .class)) && aValue instanceof String ) { 213 String aStringValue = (String ) aValue; 214 Byte aByte = new Byte (Byte.parseByte(aStringValue)); 215 216 return aMethod.invoke(aTarget, new Object [] { aByte }); 217 } else if (aParamType.equals(byte.class) && aValue instanceof Byte ) { 218 return aMethod.invoke(aTarget, new Object [] { aValue }); 219 } 220 else if ((aParamType.equals(short.class) || 222 aParamType.equals(Short .class)) && aValue instanceof String ) { 223 String aStringValue = (String ) aValue; 224 Short aShort = new Short (Short.parseShort(aStringValue)); 225 226 return aMethod.invoke(aTarget, new Object [] { aShort }); 227 } else if (aParamType.equals(short.class) && aValue instanceof Short ) { 228 return aMethod.invoke(aTarget, new Object [] { aValue }); 229 } 230 else if ((aParamType.equals(char.class) || 232 aParamType.equals(Character .class)) && 233 aValue instanceof String ) { 234 String aStringValue = (String ) aValue; 235 236 if (aStringValue.length() != 1) { 237 throw new ConfigurationException("'" + aStringValue + 238 "' must be a single character string"); 239 } 240 241 Character aChar = new Character (aStringValue.charAt(0)); 242 243 return aMethod.invoke(aTarget, new Object [] { aChar }); 244 } else if (aParamType.equals(char.class) && 245 aValue instanceof Character ) { 246 return aMethod.invoke(aTarget, new Object [] { aValue }); 247 } 248 else if ((aParamType.equals(int.class) || 250 aParamType.equals(Integer .class)) && aValue instanceof String ) { 251 String aStringValue = (String ) aValue; 252 Integer aInt = new Integer (Integer.parseInt(aStringValue)); 253 254 return aMethod.invoke(aTarget, new Object [] { aInt }); 255 } else if (aParamType.equals(int.class) && aValue instanceof Integer ) { 256 return aMethod.invoke(aTarget, new Object [] { aValue }); 257 } 258 else if ((aParamType.equals(long.class) || 260 aParamType.equals(Long .class)) && aValue instanceof String ) { 261 String aStringValue = (String ) aValue; 262 Long aLong = new Long (Long.parseLong(aStringValue)); 263 264 return aMethod.invoke(aTarget, new Object [] { aLong }); 265 } else if (aParamType.equals(long.class) && aValue instanceof Long ) { 266 return aMethod.invoke(aTarget, new Object [] { aValue }); 267 } 268 else if ((aParamType.equals(float.class) || 270 aParamType.equals(Float .class)) && aValue instanceof String ) { 271 String aStringValue = (String ) aValue; 272 Float aFloat = new Float (Float.parseFloat(aStringValue)); 273 274 return aMethod.invoke(aTarget, new Object [] { aFloat }); 275 } else if (aParamType.equals(float.class) && aValue instanceof Float ) { 276 return aMethod.invoke(aTarget, new Object [] { aValue }); 277 } 278 else if ((aParamType.equals(double.class) || 280 aParamType.equals(Double .class)) && aValue instanceof String ) { 281 String aStringValue = (String ) aValue; 282 Double aDouble = new Double (Double.parseDouble(aStringValue)); 283 284 return aMethod.invoke(aTarget, new Object [] { aDouble }); 285 } else if (aParamType.equals(double.class) && 286 aValue instanceof Double ) { 287 return aMethod.invoke(aTarget, new Object [] { aValue }); 288 } 289 } 290 } 291 } catch (NumberFormatException nfe) { 292 String aMessage = "'" + aValue + 293 "' is not a valid numeric value for method '" + aMethodName + "'"; 294 throw new ConfigurationException(aMessage, nfe); 295 } catch (IllegalAccessException iae) { 296 String aMessage = "Security error invoking the method '" + aMethodName + 297 "'to assign the value " + aValue + " on the object " + aTarget; 298 throw new ConfigurationException(aMessage, iae); 299 } catch (InvocationTargetException ite) { 300 String aMessage = "Application error invoking the method '" + 301 aMethodName + "'to assign the value " + aValue + " on the object " + 302 aTarget; 303 304 if (ite.getTargetException() == null) { 305 throw new ConfigurationException(aMessage, ite); 306 } else { 307 throw new ConfigurationException(aMessage, ite.getTargetException()); 308 } 309 } 310 311 String aMessage = "No method found for the name '" + aMethodName + 312 "' on the object " + aTarget + " - value: " + aValue + " (" + 313 aValue.getClass() + ")"; 314 throw new NoSuchMethodException (aMessage); 315 } 316 317 334 protected static String toMethodName(String attrName) { 335 return convertChars(new char[] { '-', '.' }, attrName); 336 } 337 338 protected static boolean containsMethod(String prefix, Object target, 339 String elemName) { 340 Method [] methods = target.getClass().getMethods(); 341 String name = prefix + toMethodName(elemName); 342 343 for (int i = 0; i < methods.length; i++) { 344 if (methods[i].getName().equals(name)) { 345 return true; 346 } 347 } 348 if(target instanceof ObjectWrapperIF){ 349 try{ 350 Object wrapped = ((ObjectWrapperIF)target).getWrappedObject(); 351 if(wrapped != null) 352 return containsMethod(prefix, wrapped, elemName); 353 }catch(RuntimeException e){ 354 return false; 355 } 356 } 357 358 return false; 359 } 360 361 365 protected static String formatElementName(String elemName) { 366 return convertChars(new char[] { '-', '.' }, elemName); 367 } 368 369 protected static String convertChars(char[] toConvert, String aSource) { 370 StringBuffer aBuffer = new StringBuffer (aSource.length()); 371 char current; 372 373 for (int i = 0; i < aSource.length(); i++) { 374 current = aSource.charAt(i); 375 376 if (isToConvert(toConvert, current)) { 377 if ((i + 1) < (aSource.length() - 1)) { 378 i++; 379 aBuffer.append(Character.toUpperCase(aSource.charAt(i))); 380 381 continue; 382 } 383 } 384 385 if (i == 0) { 386 current = Character.toUpperCase(current); 387 } 388 389 aBuffer.append(current); 390 } 391 392 return aBuffer.toString(); 393 } 394 395 private static boolean isToConvert(char[] toConvert, char current) { 396 for (int i = 0; i < toConvert.length; i++) { 397 if (toConvert[i] == current) { 398 return true; 399 } 400 } 401 402 return false; 403 } 404 405 409 413 protected ObjectFactoryIF getObjectFactory() { 414 return _theObjectFactory; 415 } 416 } 417 | Popular Tags |