    001/* ========================================================================
    002 * JCommon : a free general purpose class library for the Java(tm) platform
    003 * ========================================================================
    004 *
    005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006 *
    007 * Project Info:  http://www.jfree.org/jcommon/index.html
    008 *
    009 * This library is free software; you can redistribute it and/or modify it
    010 * under the terms of the GNU Lesser General Public License as published by
    011 * the Free Software Foundation; either version 2.1 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This library is distributed in the hope that it will be useful, but
    015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017 * License for more details.
    018 *
    019 * You should have received a copy of the GNU Lesser General Public
    020 * License along with this library; if not, write to the Free Software
    021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022 * USA.
    023 *
    024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025 * in the United States and other countries.]
    026 *
    027 * ------------------------
    028 * GenericWriteHandler.java
    029 * ------------------------
    030 * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
    031 *
    032 * Original Author:  Thomas Morgner;
    033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034 *
    035 * $Id: GenericWriteHandler.java,v 1.6 2005/11/16 15:58:41 taqua Exp $
    036 *
    037 * Changes
    038 * -------
    039 * 23-Sep-2003 : Initial version (TM);
    040 * 23-Dec-2003 : Added missing Javadocs (DG);
    041 *
    042 */
    043
    044package org.jfree.xml.writer.coretypes;
    045
    046import java.io.IOException;
    047import java.util.ArrayList;
    048
    049import org.jfree.util.Log;
    050import org.jfree.xml.util.AttributeDefinition;
    051import org.jfree.xml.util.GenericObjectFactory;
    052import org.jfree.xml.util.ObjectDescriptionException;
    053import org.jfree.xml.util.PropertyDefinition;
    054import org.jfree.xml.writer.AbstractXmlWriteHandler;
    055import org.jfree.xml.writer.AttributeList;
    056import org.jfree.xml.writer.RootXmlWriteHandler;
    057import org.jfree.xml.writer.XMLWriter;
    058import org.jfree.xml.writer.XMLWriterException;
    059
    060/**
    061 * A handler for writing generic objects.
    062 */
    063public class GenericWriteHandler extends AbstractXmlWriteHandler {
    064
    065    private GenericObjectFactory factory;
    066
    067    /**
    068     * Creates a new handler.
    069     *
    070     * @param factory  the object factory.
    071     */
    072    public GenericWriteHandler(final GenericObjectFactory factory) {
    073        this.factory = factory;
    074    }
    075
    076    /**
    077     * Performs the writing of a generic object.
    078     *
    079     * @param tagName  the tag name.
    080     * @param object  the generic object.
    081     * @param writer  the writer.
    082     * @param mPlexAttribute  ??.
    083     * @param mPlexValue  ??.
    084     *
    085     * @throws IOException if there is an I/O error.
    086     * @throws XMLWriterException if there is a writer error.
    087     */
    088    public void write(final String tagName, final Object object, final XMLWriter writer,
    089                      final String mPlexAttribute, final String mPlexValue)
    090        throws IOException, XMLWriterException {
    091
    092        try {
    093            this.factory.readProperties(object);
    094
    095            final AttributeList attributes = new AttributeList();
    096            if (mPlexAttribute != null) {
    097                attributes.setAttribute(mPlexAttribute, mPlexValue);
    098            }
    099            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}