1 43 44 package org.jfree.xml.generator; 45 46 import java.beans.BeanInfo ; 47 import java.beans.IntrospectionException ; 48 import java.beans.Introspector ; 49 import java.beans.PropertyDescriptor ; 50 import java.io.File ; 51 import java.io.IOException ; 52 import java.net.URL ; 53 import java.util.ArrayList ; 54 55 import org.jfree.io.IOUtils; 56 import org.jfree.xml.generator.model.ClassDescription; 57 import org.jfree.xml.generator.model.Comments; 58 import org.jfree.xml.generator.model.DescriptionModel; 59 import org.jfree.xml.generator.model.IgnoredPropertyInfo; 60 import org.jfree.xml.generator.model.ManualMappingInfo; 61 import org.jfree.xml.generator.model.MultiplexMappingInfo; 62 import org.jfree.xml.generator.model.PropertyInfo; 63 import org.jfree.xml.generator.model.PropertyType; 64 import org.jfree.xml.generator.model.TypeInfo; 65 import org.jfree.xml.util.AbstractModelReader; 66 import org.jfree.xml.util.ObjectDescriptionException; 67 68 71 public class DefaultModelReader extends AbstractModelReader { 72 73 74 private DescriptionModel model; 75 76 77 private ClassDescription currentClassDescription; 78 79 80 private BeanInfo currentBeanInfo; 81 82 83 private URL baseURL; 84 85 86 private String source; 87 88 89 private MultiplexMappingInfo multiplexInfo; 90 91 92 private ArrayList multiplexTypeInfos; 93 94 95 private ArrayList propertyList; 96 97 98 private ArrayList constructorList; 99 100 103 public DefaultModelReader() { 104 super(); 105 } 106 107 117 public synchronized DescriptionModel load(final String file) throws IOException , 118 ObjectDescriptionException { 119 120 this.model = new DescriptionModel(); 121 this.baseURL = new File (file).toURL(); 122 parseXml(this.baseURL); 123 fillSuperClasses(); 124 return this.model; 125 126 } 127 128 132 protected void fillSuperClasses() { 133 for (int i = 0; i < this.model.size(); i++) { 134 final ClassDescription cd = this.model.get(i); 135 final Class parent = cd.getObjectClass().getSuperclass(); 136 if (parent == null) { 137 continue; 138 } 139 final ClassDescription superCD = this.model.get(parent); 140 if (superCD != null) { 141 cd.setSuperClass(superCD.getObjectClass()); 142 } 143 } 144 } 145 146 155 protected boolean startObjectDefinition(final String className, final String register, final boolean ignore) { 156 final Class c = loadClass(className); 157 if (c == null) { 158 return false; 159 } 160 this.currentClassDescription = new ClassDescription(c); 161 this.currentClassDescription.setPreserve(ignore); 162 this.currentClassDescription.setRegisterKey(register); 163 try { 164 this.currentBeanInfo = Introspector.getBeanInfo(c, Object .class); 165 } 166 catch (IntrospectionException ie) { 167 return false; 168 } 169 this.propertyList = new java.util.ArrayList (); 170 this.constructorList = new java.util.ArrayList (); 171 return true; 172 } 173 174 180 protected void endObjectDefinition() throws ObjectDescriptionException { 181 final PropertyInfo[] pis = (PropertyInfo[]) 182 this.propertyList.toArray(new PropertyInfo[this.propertyList.size()]); 183 this.currentClassDescription.setProperties(pis); 184 185 final TypeInfo[] tis = (TypeInfo[]) 186 this.constructorList.toArray(new TypeInfo[this.constructorList.size()]); 187 188 this.currentClassDescription.setConstructorDescription(tis); 189 this.currentClassDescription.setComments 190 (new Comments(getOpenComment(), getCloseComment())); 191 this.currentClassDescription.setSource(this.source); 192 193 this.model.addClassDescription(this.currentClassDescription); 194 195 this.propertyList = null; 196 this.currentBeanInfo = null; 197 this.currentClassDescription = null; 198 } 199 200 209 protected void handleAttributeDefinition(final String name, final String attribName, final String handlerClass) 210 throws ObjectDescriptionException { 211 212 final PropertyInfo propertyInfo = ModelBuilder.getInstance().createSimplePropertyInfo 213 (getPropertyDescriptor(name)); 214 215 if (propertyInfo == null) { 216 throw new ObjectDescriptionException("Unable to load property " + name); 217 } 218 219 propertyInfo.setComments(new Comments(getOpenComment(), getCloseComment())); 220 propertyInfo.setPropertyType(PropertyType.ATTRIBUTE); 221 propertyInfo.setXmlName(attribName); 222 propertyInfo.setXmlHandler(handlerClass); 223 this.propertyList.add(propertyInfo); 224 } 225 226 234 protected void handleConstructorDefinition(final String tagName, final String parameterClass) 235 throws ObjectDescriptionException { 236 237 final Class c = loadClass(parameterClass); 238 if (c == null) { 239 throw new ObjectDescriptionException("Failed to load class " + parameterClass); 240 } 241 final TypeInfo ti = new TypeInfo(tagName, c); 242 ti.setComments(new Comments(getOpenComment(), getCloseComment())); 243 this.constructorList.add (ti); 244 } 245 246 254 protected void handleElementDefinition(final String name, final String element) 255 throws ObjectDescriptionException { 256 257 final PropertyInfo propertyInfo = ModelBuilder.getInstance().createSimplePropertyInfo 258 (getPropertyDescriptor(name)); 259 260 if (propertyInfo == null) { 261 throw new ObjectDescriptionException("Unable to load property " + name); 262 } 263 264 propertyInfo.setComments(new Comments(getOpenComment(), getCloseComment())); 265 propertyInfo.setPropertyType(PropertyType.ELEMENT); 266 propertyInfo.setXmlName(element); 267 propertyInfo.setXmlHandler(null); 268 this.propertyList.add(propertyInfo); 269 270 } 271 272 280 protected void handleLookupDefinition(final String name, final String lookupKey) 281 throws ObjectDescriptionException { 282 final PropertyInfo propertyInfo = ModelBuilder.getInstance().createSimplePropertyInfo 283 (getPropertyDescriptor(name)); 284 285 if (propertyInfo == null) { 286 throw new ObjectDescriptionException("Unable to load property " + name); 287 } 288 289 propertyInfo.setComments(new Comments(getOpenComment(), getCloseComment())); 290 propertyInfo.setPropertyType(PropertyType.LOOKUP); 291 propertyInfo.setXmlName(lookupKey); 292 propertyInfo.setXmlHandler(null); 293 this.propertyList.add(propertyInfo); 294 } 295 296 304 protected PropertyDescriptor getPropertyDescriptor(final String propertyName) { 305 final PropertyDescriptor [] pds = this.currentBeanInfo.getPropertyDescriptors(); 306 for (int i = 0; i < pds.length; i++) { 307 if (pds[i].getName().equals(propertyName)) { 308 return pds[i]; 309 } 310 } 311 return null; 312 } 313 314 319 protected void handleIgnoredProperty(final String name) { 320 final IgnoredPropertyInfo propertyInfo = new IgnoredPropertyInfo(name); 321 propertyInfo.setComments(new Comments(getOpenComment(), getCloseComment())); 322 this.propertyList.add(propertyInfo); 323 } 324 325 336 protected boolean handleManualMapping(final String className, final String readHandler, final String writeHandler) 337 throws ObjectDescriptionException { 338 339 final ManualMappingInfo manualMappingInfo = 340 new ManualMappingInfo(loadClass(className), 341 loadClass(readHandler), loadClass(writeHandler)); 342 manualMappingInfo.setComments(new Comments(getOpenComment(), getCloseComment())); 343 manualMappingInfo.setSource(this.source); 344 this.model.getMappingModel().addManualMapping(manualMappingInfo); 345 return true; 346 } 347 348 354 protected void startMultiplexMapping(final String className, final String typeAttr) { 355 this.multiplexInfo = new MultiplexMappingInfo(loadClass(className), typeAttr); 356 this.multiplexInfo.setSource(this.source); 357 this.multiplexTypeInfos = new ArrayList (); 358 } 359 360 368 protected void handleMultiplexMapping(final String typeName, final String className) 369 throws ObjectDescriptionException { 370 final TypeInfo info = new TypeInfo(typeName, loadClass(className)); 371 info.setComments(new Comments(getOpenComment(), getCloseComment())); 372 this.multiplexTypeInfos.add (info); 373 } 374 375 380 protected void endMultiplexMapping() throws ObjectDescriptionException { 381 final TypeInfo[] typeInfos = (TypeInfo[]) this.multiplexTypeInfos.toArray( 382 new TypeInfo[this.multiplexTypeInfos.size()] 383 ); 384 this.multiplexInfo.setComments(new Comments(getOpenComment(), getCloseComment())); 385 this.multiplexInfo.setChildClasses(typeInfos); 386 this.model.getMappingModel().addMultiplexMapping(this.multiplexInfo); 387 this.multiplexInfo = null; 388 } 389 390 395 protected void startIncludeHandling(final URL resource) { 396 this.source = IOUtils.getInstance().createRelativeURL(resource, this.baseURL); 397 this.model.addSource(this.source); 398 this.model.addIncludeComment( 399 this.source, new Comments(getOpenComment(), getCloseComment()) 400 ); 401 } 402 403 406 protected void endIncludeHandling() { 407 this.source = ""; 408 } 409 410 413 protected void startRootDocument() { 414 this.source = ""; 415 } 416 417 420 protected void endRootDocument() { 421 this.model.setModelComments(new Comments(getOpenComment(), getCloseComment())); 422 } 423 } 424
| Popular Tags
|