1 42 43 package org.jfree.xml.util; 44 45 import java.net.URL ; 46 import java.util.ArrayList ; 47 import java.util.Arrays ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 51 import org.jfree.util.Log; 52 import org.jfree.xml.attributehandlers.AttributeHandler; 53 54 63 public class ObjectFactoryLoader extends AbstractModelReader implements ObjectFactory { 64 65 66 private HashMap objectMappings; 67 68 69 private HashMap manualMappings; 70 71 72 private HashMap multiplexMappings; 73 74 75 private Class target; 76 77 78 private String registerName; 79 80 81 private ArrayList propertyDefinition; 82 83 84 private ArrayList attributeDefinition; 85 86 87 private ArrayList constructorDefinition; 88 89 90 private ArrayList lookupDefinitions; 91 92 93 private ArrayList orderedNames; 94 95 96 private String baseClass; 97 98 99 private String attributeName; 100 101 102 private ArrayList multiplexEntries; 103 104 111 public ObjectFactoryLoader(final URL resourceName) throws ObjectDescriptionException { 112 this.objectMappings = new HashMap (); 113 this.manualMappings = new HashMap (); 114 this.multiplexMappings = new HashMap (); 115 parseXml(resourceName); 116 rebuildSuperClasses(); 117 } 118 119 private void rebuildSuperClasses() throws ObjectDescriptionException { 120 this.propertyDefinition = new ArrayList (); 121 this.attributeDefinition = new ArrayList (); 122 this.constructorDefinition = new ArrayList (); 123 this.lookupDefinitions = new ArrayList (); 124 this.orderedNames = new ArrayList (); 125 126 final HashMap newObjectDescriptions = new HashMap (); 127 final Iterator it = this.objectMappings.keySet().iterator(); 128 while (it.hasNext()) { 129 final Object key = it.next(); 130 final GenericObjectFactory gef = (GenericObjectFactory) this.objectMappings.get(key); 131 performSuperClassUpdate(gef); 132 133 final PropertyDefinition[] propertyDefs = (PropertyDefinition[]) 134 this.propertyDefinition.toArray(new PropertyDefinition[0]); 135 final LookupDefinition[] lookupDefs = (LookupDefinition[]) 136 this.lookupDefinitions.toArray(new LookupDefinition[0]); 137 final AttributeDefinition[] attribDefs = (AttributeDefinition[]) 138 this.attributeDefinition.toArray(new AttributeDefinition[0]); 139 final ConstructorDefinition[] constructorDefs = (ConstructorDefinition[]) 140 this.constructorDefinition.toArray(new ConstructorDefinition[0]); 141 final String [] orderedNamesDefs = (String []) 142 this.orderedNames.toArray(new String [0]); 143 144 final GenericObjectFactory objectFactory = new GenericObjectFactory 145 (gef.getBaseClass(), gef.getRegisterName(), constructorDefs, 146 propertyDefs, lookupDefs, attribDefs, orderedNamesDefs); 147 newObjectDescriptions.put(key, objectFactory); 148 149 this.propertyDefinition.clear(); 150 this.attributeDefinition.clear(); 151 this.constructorDefinition.clear(); 152 this.lookupDefinitions.clear(); 153 this.orderedNames.clear(); 154 } 155 156 this.objectMappings.clear(); 157 this.objectMappings = newObjectDescriptions; 158 159 this.propertyDefinition = null; 160 this.attributeDefinition = null; 161 this.constructorDefinition = null; 162 this.lookupDefinitions = null; 163 this.orderedNames = null; 164 } 165 166 private void performSuperClassUpdate(final GenericObjectFactory gef) { 167 final Class superClass = gef.getBaseClass().getSuperclass(); 169 if (superClass != null && !superClass.equals(Object .class)) { 170 final GenericObjectFactory superGef = (GenericObjectFactory) this.objectMappings.get( 171 superClass 172 ); 173 if (superGef != null) { 174 performSuperClassUpdate(superGef); 175 } 176 } 177 178 this.propertyDefinition.addAll(Arrays.asList(gef.getPropertyDefinitions())); 180 this.attributeDefinition.addAll(Arrays.asList(gef.getAttributeDefinitions())); 181 this.constructorDefinition.addAll(Arrays.asList(gef.getConstructorDefinitions())); 182 this.lookupDefinitions.addAll(Arrays.asList(gef.getLookupDefinitions())); 183 this.orderedNames.addAll(Arrays.asList(gef.getOrderedPropertyNames())); 184 } 185 186 198 protected boolean startObjectDefinition(final String className, final String register, final boolean ignore) 199 throws ObjectDescriptionException { 200 201 if (ignore) { 202 return false; 203 } 204 this.target = loadClass(className); 205 if (this.target == null) { 206 Log.warn(new Log.SimpleMessage("Failed to load class ", className)); 207 return false; 208 } 209 this.registerName = register; 210 this.propertyDefinition = new ArrayList (); 211 this.attributeDefinition = new ArrayList (); 212 this.constructorDefinition = new ArrayList (); 213 this.lookupDefinitions = new ArrayList (); 214 this.orderedNames = new ArrayList (); 215 return true; 216 } 217 218 227 protected void handleAttributeDefinition(final String name, final String attribName, final String handlerClass) 228 throws ObjectDescriptionException { 229 final AttributeHandler handler = loadAttributeHandler(handlerClass); 230 this.orderedNames.add(name); 231 this.attributeDefinition.add(new AttributeDefinition(name, attribName, handler)); 232 } 233 234 243 protected void handleElementDefinition(final String name, final String element) 244 throws ObjectDescriptionException { 245 this.orderedNames.add(name); 246 this.propertyDefinition.add(new PropertyDefinition(name, element)); 247 } 248 249 258 protected void handleLookupDefinition(final String name, final String lookupKey) 259 throws ObjectDescriptionException { 260 final LookupDefinition ldef = new LookupDefinition(name, lookupKey); 261 this.orderedNames.add(name); 262 this.lookupDefinitions.add(ldef); 263 } 264 265 270 protected void endObjectDefinition() 271 throws ObjectDescriptionException { 272 273 final PropertyDefinition[] propertyDefs = (PropertyDefinition[]) 274 this.propertyDefinition.toArray(new PropertyDefinition[0]); 275 final LookupDefinition[] lookupDefs = (LookupDefinition[]) 276 this.lookupDefinitions.toArray(new LookupDefinition[0]); 277 final AttributeDefinition[] attribDefs = (AttributeDefinition[]) 278 this.attributeDefinition.toArray(new AttributeDefinition[0]); 279 final ConstructorDefinition[] constructorDefs = (ConstructorDefinition[]) 280 this.constructorDefinition.toArray(new ConstructorDefinition[0]); 281 final String [] orderedNamesDefs = (String []) 282 this.orderedNames.toArray(new String [0]); 283 284 final GenericObjectFactory objectFactory = new GenericObjectFactory 285 (this.target, this.registerName, constructorDefs, 286 propertyDefs, lookupDefs, attribDefs, orderedNamesDefs); 287 this.objectMappings.put(this.target, objectFactory); 288 } 289 290 297 protected void handleConstructorDefinition(final String propertyName, final String parameterClass) { 298 final Class c = loadClass(parameterClass); 299 this.orderedNames.add(propertyName); 300 this.constructorDefinition.add(new ConstructorDefinition(propertyName, c)); 301 } 302 303 314 protected boolean handleManualMapping(final String className, final String readHandler, final String writeHandler) 315 throws ObjectDescriptionException { 316 317 if (!this.manualMappings.containsKey(className)) { 318 final Class loadedClass = loadClass(className); 319 this.manualMappings.put(loadedClass, new ManualMappingDefinition 320 (loadedClass, readHandler, writeHandler)); 321 return true; 322 } 323 return false; 324 } 325 326 335 protected void startMultiplexMapping(final String className, final String typeAttr) { 336 this.baseClass = className; 337 this.attributeName = typeAttr; 338 this.multiplexEntries = new ArrayList (); 339 } 340 341 350 protected void handleMultiplexMapping(final String typeName, final String className) 351 throws ObjectDescriptionException { 352 this.multiplexEntries.add 353 (new MultiplexMappingEntry(typeName, className)); 354 } 355 356 361 protected void endMultiplexMapping() throws ObjectDescriptionException { 362 final MultiplexMappingEntry[] mappings = (MultiplexMappingEntry[]) 363 this.multiplexEntries.toArray(new MultiplexMappingEntry[0]); 364 final Class c = loadClass(this.baseClass); 365 this.multiplexMappings.put(c, 366 new MultiplexMappingDefinition(c, this.attributeName, mappings)); 367 this.multiplexEntries = null; 368 } 369 370 378 private AttributeHandler loadAttributeHandler(final String attribute) 379 throws ObjectDescriptionException { 380 381 final Class c = loadClass(attribute); 382 try { 383 return (AttributeHandler) c.newInstance(); 384 } 385 catch (Exception e) { 386 throw new ObjectDescriptionException 387 ("Invalid attribute handler specified: " + attribute); 388 } 389 } 390 391 397 public boolean isGenericHandler(final Class c) { 398 return this.objectMappings.containsKey(c); 399 } 400 401 408 public GenericObjectFactory getFactoryForClass(final Class c) { 409 final GenericObjectFactory factory = (GenericObjectFactory) this.objectMappings.get(c); 410 if (factory == null) { 411 return null; 412 } 413 return factory.getInstance(); 414 } 415 416 423 public ManualMappingDefinition getManualMappingDefinition(final Class c) { 424 return (ManualMappingDefinition) this.manualMappings.get(c); 425 } 426 427 434 public MultiplexMappingDefinition getMultiplexDefinition(final Class c) { 435 final MultiplexMappingDefinition definition = (MultiplexMappingDefinition) 436 this.multiplexMappings.get(c); 437 return definition; 438 } 439 440 } 441 | Popular Tags |