Source for org.jfree.xml.parser.coretypes.Rectangle2DReadHandler

   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:  * Rectangle2DReadHandler
  29:  * ----------------------
  30:  * (C) Copyright 2003, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: Rectangle2DReadHandler.java,v 1.2 2005/10/18 13:33:32 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  *
  40:  */
  41: 
  42: package org.jfree.xml.parser.coretypes;
  43: 
  44: import java.awt.geom.Rectangle2D;
  45: 
  46: import org.jfree.xml.parser.AbstractXmlReadHandler;
  47: import org.xml.sax.Attributes;
  48: import org.xml.sax.SAXException;
  49: 
  50: /**
  51:  * A handler for reading a {@link Rectangle2D} object.
  52:  */
  53: public class Rectangle2DReadHandler extends AbstractXmlReadHandler  {
  54:     
  55:     /** The rectangle being constructed. */
  56:     private Rectangle2D rectangle;
  57: 
  58:     /**
  59:      * Default constructor.
  60:      */
  61:     public Rectangle2DReadHandler() {
  62:         super();
  63:     }
  64: 
  65:     /**
  66:      * Begins parsing.
  67:      * 
  68:      * @param attrs  the attributes.
  69:      * 
  70:      * @throws SAXException if there is a parsing error.
  71:      */
  72:     protected void startParsing(final Attributes attrs) throws SAXException {
  73:         final String type = attrs.getValue("type");
  74:         this.rectangle = createRect(type);
  75:         final String x = attrs.getValue("x");
  76:         final String y = attrs.getValue("y");
  77:         final String w = attrs.getValue("width");
  78:         final String h = attrs.getValue("height");
  79: 
  80:         this.rectangle.setRect(
  81:             Double.parseDouble(x), Double.parseDouble(y),
  82:             Double.parseDouble(w), Double.parseDouble(h)
  83:         );
  84:     }
  85: 
  86:     /**
  87:      * Creates a rectangle.
  88:      * 
  89:      * @param type  the type ('float' or 'double').
  90:      * 
  91:      * @return The rectangle.
  92:      */
  93:     private Rectangle2D createRect(final String type) {
  94:         if ("float".equals(type)) {
  95:             return new Rectangle2D.Float();
  96:         }
  97:         return new Rectangle2D.Double();
  98:     }
  99: 
 100:     /**
 101:      * Returns the object under construction.
 102:      * 
 103:      * @return The object.
 104:      */
 105:     public Object getObject() {
 106:         return this.rectangle;
 107:     }
 108: }