1 43 44 package org.jfree.xml.writer.coretypes; 45 46 import java.io.IOException ; 47 import java.util.ArrayList ; 48 49 import org.jfree.util.Log; 50 import org.jfree.xml.util.AttributeDefinition; 51 import org.jfree.xml.util.GenericObjectFactory; 52 import org.jfree.xml.util.ObjectDescriptionException; 53 import org.jfree.xml.util.PropertyDefinition; 54 import org.jfree.xml.writer.AbstractXmlWriteHandler; 55 import org.jfree.xml.writer.AttributeList; 56 import org.jfree.xml.writer.RootXmlWriteHandler; 57 import org.jfree.xml.writer.XMLWriter; 58 import org.jfree.xml.writer.XMLWriterException; 59 60 63 public class GenericWriteHandler extends AbstractXmlWriteHandler { 64 65 private GenericObjectFactory factory; 66 67 72 public GenericWriteHandler(final GenericObjectFactory factory) { 73 this.factory = factory; 74 } 75 76 88 public void write(final String tagName, final Object object, final XMLWriter writer, 89 final String mPlexAttribute, final String mPlexValue) 90 throws IOException , XMLWriterException { 91 92 try { 93 this.factory.readProperties(object); 94 95 final AttributeList attributes = new AttributeList(); 96 if (mPlexAttribute != null) { 97 attributes.setAttribute(mPlexAttribute, mPlexValue); 98 } 99 final AttributeDefinition[] attribDefs = this.factory.getAttributeDefinitions(); 100 final ArrayList properties = new ArrayList (); 101 for (int i = 0; i < attribDefs.length; i++) { 102 final AttributeDefinition adef = attribDefs[i]; 103 final String pName = adef.getAttributeName(); 104 final Object propValue = this.factory.getProperty(adef.getPropertyName()); 105 if (propValue != null) { 106 Log.debug( 107 "Here: " + this.factory.getBaseClass() + " -> " + adef.getPropertyName() 108 ); 109 final String value = adef.getHandler().toAttributeValue(propValue); 110 if (value != null) { 111 attributes.setAttribute(pName, value); 112 } 113 } 114 properties.add(adef.getPropertyName()); 115 } 116 writer.writeTag(tagName, attributes, false); 117 writer.startBlock(); 118 119 final PropertyDefinition[] propertyDefs = this.factory.getPropertyDefinitions(); 120 final RootXmlWriteHandler rootHandler = getRootHandler(); 121 for (int i = 0; i < propertyDefs.length; i++) { 122 final PropertyDefinition pDef = propertyDefs[i]; 123 final String elementName = pDef.getElementName(); 124 rootHandler.write 125 (elementName, this.factory.getProperty(pDef.getPropertyName()), 126 this.factory.getTypeForTagName(elementName), writer); 127 } 128 writer.endBlock(); 129 writer.writeCloseTag(tagName); 130 } 131 catch (ObjectDescriptionException ode) { 132 Log.warn ("Unable to write element", ode); 133 throw new IOException (ode.getMessage()); 134 } 135 } 136 137 } 138 | Popular Tags |