1 package org.sapia.util.xml.confix; 2 3 import java.lang.reflect.InvocationTargetException ; 4 5 import java.lang.reflect.Method ; 8 9 import java.util.ArrayList ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 14 26 public class ReflectionFactory implements ObjectFactoryIF { 27 31 32 private List _thePackages; 33 34 38 43 public ReflectionFactory(String [] somePackages) { 44 _thePackages = new ArrayList (); 45 46 for (int i = 0; i < somePackages.length; i++) { 47 _thePackages.add(somePackages[i]); 48 } 49 } 50 51 55 60 public static String firstToUpper(String aString) { 61 StringBuffer aBuffer = new StringBuffer (); 62 63 for (int i = 0; i < aString.length(); i++) { 64 if (i == 0) { 65 aBuffer.append(Character.toUpperCase(aString.charAt(i))); 66 } else { 67 aBuffer.append(aString.charAt(i)); 68 } 69 } 70 71 return aBuffer.toString(); 72 } 73 74 83 protected static Object invokeMethod(String aMethodPrefix, 84 String anElementName, Object aTarget) { 85 Object anObject = null; 86 87 try { 88 Method aMethod = aTarget.getClass().getMethod(aMethodPrefix + 90 anElementName, new Class [0]); 91 92 anObject = aMethod.invoke(aTarget, new Object [0]); 93 } catch (NoSuchMethodException nsme) { 94 } catch (IllegalAccessException iae) { 95 } catch (InvocationTargetException ite) { 96 } 97 98 return anObject; 99 } 100 101 protected static boolean invokeVoidMethod(Object aTarget, String elementName){ 102 if(aTarget == null){ 103 return false; 104 } 105 106 Method toInvoke = null; 107 toInvoke = findVoidMethod(aTarget.getClass(), elementName); 108 109 if(toInvoke == null){ 110 toInvoke = findVoidMethod(aTarget.getClass(), "set" + AbstractXMLProcessor.toMethodName(elementName)); 111 } 112 if(toInvoke == null){ 113 toInvoke = findVoidMethod(aTarget.getClass(), "add" + AbstractXMLProcessor.toMethodName(elementName)); 114 } 115 if(toInvoke == null){ 116 return false; 117 } 118 try{ 119 toInvoke.invoke(aTarget, new Object [0]); 120 return true; 121 } catch (IllegalAccessException iae) { 122 return false; 123 } catch (InvocationTargetException ite) { 124 return false; 125 } 126 } 127 128 protected static Method findVoidMethod(Class owner, String name){ 129 try{ 130 Method m = owner.getMethod(name, new Class [0]); 131 if(m.getReturnType().equals(void.class)){ 132 return m; 133 } 134 return null; 135 }catch(NoSuchMethodException e){ 136 return null; 137 } 138 } 139 140 144 149 public void addPackage(String aPackage) { 150 _thePackages.add(aPackage); 151 } 152 153 157 166 public CreationStatus newObjectFor(String aPrefix, String aNamespaceURI, 167 String anElementName, Object aParent) throws ObjectCreationException { 168 Object anObject = null; 169 170 if (aParent != null) { 171 173 anObject = invokeMethod("create", firstToUpper(anElementName), aParent); 174 175 if (anObject != null) { 176 return CreationStatus.create(anObject).assigned(true); 177 } else { 178 anObject = invokeMethod("add", firstToUpper(anElementName), aParent); 180 181 if (anObject != null) { 182 return CreationStatus.create(anObject).assigned(true); 183 } else { 184 anObject = invokeMethod("set", firstToUpper(anElementName), aParent); 185 186 if (anObject != null) { 187 return CreationStatus.create(anObject).assigned(true); 188 } 189 } 190 } 191 } 192 193 String aLocalClassName = firstToUpper(AbstractXMLProcessor.formatElementName( 194 anElementName)); 195 Class clazz = null; 196 boolean isFound = false; 197 198 for (Iterator it = _thePackages.iterator(); !isFound && it.hasNext();) { 199 String aPackageName = (String ) it.next(); 200 201 try { 202 String aQualifiedClassName = new StringBuffer ().append(aPackageName) 203 .append(".") 204 .append(aLocalClassName) 205 .toString(); 206 207 clazz = Class.forName(aQualifiedClassName); 208 isFound = true; 209 } catch (ClassNotFoundException cnfe) { 210 } 211 } 212 213 try { 214 if (clazz != null) { 215 return CreationStatus.create(clazz.newInstance()); 216 } else { 217 if(!invokeVoidMethod(aParent, anElementName)){ 218 throw new ObjectCreationException("Element not recognized: " + 219 anElementName); 220 } 221 else{ 222 return CreationStatus.create(new NullObjectImpl()).assigned(true); 223 } 224 } 225 } catch (IllegalAccessException iae) { 226 String aMessage = "Security error creating an object for the element " + 227 anElementName; 228 229 throw new ObjectCreationException(aMessage, iae); 230 } catch (InstantiationException ie) { 231 String aMessage = "Instantiation error creating an object for the element " + 232 anElementName; 233 234 throw new ObjectCreationException(aMessage, ie); 235 } catch (RuntimeException re) { 236 String aMessage = "System error creating an object for the element " + 237 anElementName; 238 239 throw new ObjectCreationException(aMessage, re); 240 } 241 } 242 243 public static class NullObjectImpl implements NullObject{} 244 } 245 | Popular Tags |