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 * Rectangle2DReadHandler
029 * ----------------------
030 * (C) Copyright 2003, by Thomas Morgner and Contributors.
031 *
032 * Original Author:  Thomas Morgner;
033 * Contributor(s):   -;
034 *
035 * $Id: Rectangle2DReadHandler.java,v 1.2 2005/10/18 13:33:32 mungady Exp $
036 *
037 * Changes
038 * -------
039 *
040 */
041
042package org.jfree.xml.parser.coretypes;
043
044import java.awt.geom.Rectangle2D;
045
046import org.jfree.xml.parser.AbstractXmlReadHandler;
047import org.xml.sax.Attributes;
048import org.xml.sax.SAXException;
049
050/**
051 * A handler for reading a {@link Rectangle2D} object.
052 */
053public class Rectangle2DReadHandler extends AbstractXmlReadHandler  {
054    
055    /** The rectangle being constructed. */
056    private Rectangle2D rectangle;
057
058    /**
059     * Default constructor.
060     */
061    public Rectangle2DReadHandler() {
062        super();
063    }
064
065    /**
066     * Begins parsing.
067     * 
068     * @param attrs  the attributes.
069     * 
070     * @throws SAXException if there is a parsing error.
071     */
072    protected void startParsing(final Attributes attrs) throws SAXException {
073        final String type = attrs.getValue("type");
074        this.rectangle = createRect(type);
075        final String x = attrs.getValue("x");
076        final String y = attrs.getValue("y");
077        final String w = attrs.getValue("width");
078        final String h = attrs.getValue("height");
079
080        this.rectangle.setRect(
081            Double.parseDouble(x), Double.parseDouble(y),
082            Double.parseDouble(w), Double.parseDouble(h)
083        );
084    }
085
086    /**
087     * Creates a rectangle.
088     * 
089     * @param type  the type ('float' or 'double').
090     * 
091     * @return The rectangle.
092     */
093    private Rectangle2D createRect(final String type) {
094        if ("float".equals(type)) {
095            return new Rectangle2D.Float();
096        }
097        return new Rectangle2D.Double();
098    }
099
100    /**
101     * Returns the object under construction.
102     * 
103     * @return The object.
104     */
105    public Object getObject() {
106        return this.rectangle;
107    }
108}