    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 * RenderingHintsReadHandler.java
    029 * ------------------------------
    030 * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
    031 *
    032 * Original Author:  Thomas Morgner;
    033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034 *
    035 * $Id: RenderingHintsReadHandler.java,v 1.3 2005/10/18 13:33:32 mungady Exp $
    036 *
    037 * Changes
    038 * -------
    039 * 03-Dec-2003 : Initial version
    040 * 11-Feb-2004 : Added missing Javadocs (DG);
    041 *
    042 */
    043
    044package org.jfree.xml.parser.coretypes;
    045
    046import java.awt.RenderingHints;
    047import java.util.ArrayList;
    048
    049import org.jfree.xml.parser.AbstractXmlReadHandler;
    050import org.jfree.xml.parser.XmlReadHandler;
    051import org.jfree.xml.parser.XmlReaderException;
    052import org.xml.sax.Attributes;
    053import org.xml.sax.SAXException;
    054
    055/**
    056 * A read handler that can parse the XML element for a {@link RenderingHints} collection.
    057 */
    058public class RenderingHintsReadHandler extends AbstractXmlReadHandler {
    059
    060    /** The subhandlers. */
    061    private ArrayList handlers;
    062
    063    /** The rendering hints under construction. */
    064    private RenderingHints renderingHints;
    065
    066    /**
    067     * Creates a new read handler.
    068     */
    069    public RenderingHintsReadHandler() {
    070        super();
    071    }
    072
    073    /**
    074     * Starts parsing.
    075     *
    076     * @param attrs  the attributes.
    077     *
    078     * @throws SAXException never.
    079     */
    080    protected void startParsing(final Attributes attrs) throws SAXException {
    081        this.handlers = new ArrayList();
    082    }
    083
    084    /**
    085     * Returns the handler for a child element.
    086     *
    087     * @param tagName  the tag name.
    088     * @param atts  the attributes.
    089     *
    090     * @return the handler.
    091     *
    092     * @throws SAXException  if there is a parsing error.
    093     * @throws XmlReaderException if there is a reader error.
    094     */
    095    protected XmlReadHandler getHandlerForChild(final String tagName, final Attributes atts)
    096        throws XmlReaderException, SAXException {
    097
    098        if (!tagName.equals("entry")) {
    099            throw new SAXException("Expected 'entry' tag.");
    100        }
    101
    102        final XmlReadHandler handler = new RenderingHintValueReadHandler();
    103        this.handlers.add(handler);
    104        return handler;
    105    }
    106
    107    /**
    108     * Done parsing.
    109     *
    110     * @throws SAXException if there is a parsing error.
    111     * @throws XmlReaderException if there is a reader error.
    112     */
    113    protected void doneParsing() throws SAXException, XmlReaderException {
    114        this.renderingHints = new RenderingHints(null);
    115
    116        for (int i = 0; i < this.handlers.size(); i++) {
    117            final RenderingHintValueReadHandler rh =
    118                (RenderingHintValueReadHandler) this.handlers.get(i);
    119            this.renderingHints.put(rh.getKey(), rh.getValue());
    120        }
    121    }
    122
    123    /**
    124     * Returns the object for this element.
    125     *
    126     * @return the object.
    127     *
    128     * @throws XmlReaderException if there is a parsing error.
    129     */
    130    public Object getObject() throws XmlReaderException {
    131        return this.renderingHints;
    132    }
    133}