Source for org.jfree.xml.writer.coretypes.GenericWriteHandler

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ------------------------
  28:  * GenericWriteHandler.java
  29:  * ------------------------
  30:  * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: GenericWriteHandler.java,v 1.6 2005/11/16 15:58:41 taqua Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 23-Sep-2003 : Initial version (TM);
  40:  * 23-Dec-2003 : Added missing Javadocs (DG);
  41:  * 
  42:  */
  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: /**
  61:  * A handler for writing generic objects.
  62:  */
  63: public class GenericWriteHandler extends AbstractXmlWriteHandler {
  64: 
  65:     private GenericObjectFactory factory;
  66: 
  67:     /**
  68:      * Creates a new handler.
  69:      * 
  70:      * @param factory  the object factory.
  71:      */
  72:     public GenericWriteHandler(final GenericObjectFactory factory) {
  73:         this.factory = factory;
  74:     }
  75: 
  76:     /**
  77:      * Performs the writing of a generic object.
  78:      *
  79:      * @param tagName  the tag name.
  80:      * @param object  the generic object.
  81:      * @param writer  the writer.
  82:      * @param mPlexAttribute  ??.
  83:      * @param mPlexValue  ??.
  84:      * 
  85:      * @throws IOException if there is an I/O error.
  86:      * @throws XMLWriterException if there is a writer error.
  87:      */
  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: }