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

   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:  * BasicStrokeReadHandler
  29:  * ----------------------
  30:  * (C) Copyright 2003, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: BasicStrokeReadHandler.java,v 1.2 2005/10/18 13:33:32 mungady Exp $
  36:  *
  37:  * Changes (from 25-Nov-2003)
  38:  * --------------------------
  39:  * 25-Nov-2003 : Added standard header and Javadocs (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.xml.parser.coretypes;
  44: 
  45: import java.awt.BasicStroke;
  46: import java.util.StringTokenizer;
  47: 
  48: import org.jfree.xml.parser.AbstractXmlReadHandler;
  49: import org.xml.sax.Attributes;
  50: import org.xml.sax.SAXException;
  51: 
  52: /**
  53:  * A SAX handler for reading a {@link BasicStroke} object from XML.
  54:  * <p>
  55:  * To do: this should have a multiplexer in front like the PaintReadHandler
  56:  */
  57: public class BasicStrokeReadHandler extends AbstractXmlReadHandler  {
  58:     
  59:     /** The stroke under construction. */
  60:     private BasicStroke stroke;
  61: 
  62:     /**
  63:      * Creates a new handler.
  64:      */
  65:     public BasicStrokeReadHandler() {
  66:         super();
  67:     }
  68: 
  69:     /**
  70:      * Called at the start of parsing a {@link BasicStroke} element, this method reads the 
  71:      * attributes and constructs the stroke. 
  72:      * 
  73:      * @param attrs  the attributes.
  74:      * 
  75:      * @throws SAXException to indicate a parsing error.
  76:      */
  77:     protected void startParsing(final Attributes attrs) throws SAXException {
  78:         final int endCap = Integer.parseInt(attrs.getValue("endCap"));
  79:         final int lineJoin = Integer.parseInt(attrs.getValue("lineJoin"));
  80:         final float lineWidth = Float.parseFloat(attrs.getValue("lineWidth"));
  81:         final float miterLimit = Float.parseFloat(attrs.getValue("miterLimit"));
  82:         final String dashArrayAttrib = attrs.getValue("dashArray");
  83:         if (dashArrayAttrib != null) {
  84:             final float[] dashArray = parseDashArray(dashArrayAttrib);
  85:             final float dashPhase = Float.parseFloat(attrs.getValue("dashPhase"));
  86:             this.stroke = new BasicStroke(
  87:                 lineWidth, endCap, lineJoin, miterLimit, dashArray, dashPhase
  88:             );
  89:         } 
  90:         else {
  91:             this.stroke = new BasicStroke(lineWidth, endCap, lineJoin, miterLimit);
  92:         }
  93:     }
  94:     
  95:     /**
  96:      * Parses the dash array.
  97:      * 
  98:      * @param string  the dash array string representation.
  99:      * 
 100:      * @return a dash array.
 101:      */
 102:     private float[] parseDashArray(final String string) {
 103:         final StringTokenizer tokenizer = new StringTokenizer(string, ",");
 104:         final float[] retVal = new float[tokenizer.countTokens()];
 105:         for (int i = 0; i < retVal.length; i++) {
 106:             retVal[i] = Float.parseFloat(tokenizer.nextToken());
 107:         }
 108:         return retVal;
 109:     }
 110: 
 111:     /**
 112:      * Returns the stroke under construction.
 113:      * 
 114:      * @return the stroke.
 115:      */
 116:     public Object getObject() {
 117:         return this.stroke;
 118:     }
 119:     
 120: }