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: * BasicStrokeReadHandler
029: * ----------------------
030: * (C) Copyright 2003, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: BasicStrokeReadHandler.java,v 1.2 2005/10/18 13:33:32 mungady Exp $
036: *
037: * Changes (from 25-Nov-2003)
038: * --------------------------
039: * 25-Nov-2003 : Added standard header and Javadocs (DG);
040: *
041: */
042:
043: package org.jfree.xml.parser.coretypes;
044:
045: import java.awt.BasicStroke;
046: import java.util.StringTokenizer;
047:
048: import org.jfree.xml.parser.AbstractXmlReadHandler;
049: import org.xml.sax.Attributes;
050: import org.xml.sax.SAXException;
051:
052: /**
053: * A SAX handler for reading a {@link BasicStroke} object from XML.
054: * <p>
055: * To do: this should have a multiplexer in front like the PaintReadHandler
056: */
057: public class BasicStrokeReadHandler extends AbstractXmlReadHandler {
058:
059: /** The stroke under construction. */
060: private BasicStroke stroke;
061:
062: /**
063: * Creates a new handler.
064: */
065: public BasicStrokeReadHandler() {
066: super ();
067: }
068:
069: /**
070: * Called at the start of parsing a {@link BasicStroke} element, this method reads the
071: * attributes and constructs the stroke.
072: *
073: * @param attrs the attributes.
074: *
075: * @throws SAXException to indicate a parsing error.
076: */
077: protected void startParsing(final Attributes attrs)
078: throws SAXException {
079: final int endCap = Integer.parseInt(attrs.getValue("endCap"));
080: final int lineJoin = Integer.parseInt(attrs
081: .getValue("lineJoin"));
082: final float lineWidth = Float.parseFloat(attrs
083: .getValue("lineWidth"));
084: final float miterLimit = Float.parseFloat(attrs
085: .getValue("miterLimit"));
086: final String dashArrayAttrib = attrs.getValue("dashArray");
087: if (dashArrayAttrib != null) {
088: final float[] dashArray = parseDashArray(dashArrayAttrib);
089: final float dashPhase = Float.parseFloat(attrs
090: .getValue("dashPhase"));
091: this .stroke = new BasicStroke(lineWidth, endCap, lineJoin,
092: miterLimit, dashArray, dashPhase);
093: } else {
094: this .stroke = new BasicStroke(lineWidth, endCap, lineJoin,
095: miterLimit);
096: }
097: }
098:
099: /**
100: * Parses the dash array.
101: *
102: * @param string the dash array string representation.
103: *
104: * @return a dash array.
105: */
106: private float[] parseDashArray(final String string) {
107: final StringTokenizer tokenizer = new StringTokenizer(string,
108: ",");
109: final float[] retVal = new float[tokenizer.countTokens()];
110: for (int i = 0; i < retVal.length; i++) {
111: retVal[i] = Float.parseFloat(tokenizer.nextToken());
112: }
113: return retVal;
114: }
115:
116: /**
117: * Returns the stroke under construction.
118: *
119: * @return the stroke.
120: */
121: public Object getObject() {
122: return this.stroke;
123: }
124:
125: }
|