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 * BasicStrokeWriteHandler.java
029 * ----------------------------
030 * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
031 *
032 * Original Author:  Thomas Morgner;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * $Id: BasicStrokeWriteHandler.java,v 1.3 2005/10/18 13:35:21 mungady Exp $
036 *
037 * Changes
038 * -------
039 * 12-Nov-2003 : Initial version (TM);
040 * 25-Nov-2003 : Updated header (DG);
041 * 
042 */
043
044package org.jfree.xml.writer.coretypes;
045
046import java.awt.BasicStroke;
047import java.io.IOException;
048
049import org.jfree.xml.writer.AbstractXmlWriteHandler;
050import org.jfree.xml.writer.AttributeList;
051import org.jfree.xml.writer.XMLWriter;
052import org.jfree.xml.writer.XMLWriterException;
053
054/**
055 * A handler that can write the XML description for a {@link BasicStroke} object.
056 */
057public class BasicStrokeWriteHandler extends AbstractXmlWriteHandler  {
058
059    /**
060     * Creates a new handler.
061     */
062    public BasicStrokeWriteHandler() {
063        super();
064    }
065
066    /**
067     * Performs the writing of a single object.
068     *
069     * @param tagName  the tag name.
070     * @param object  the object ({@link BasicStroke} expected).
071     * @param writer  the writer.
072     * @param mPlexAttribute  ??
073     * @param mPlexValue  ??
074     * 
075     * @throws IOException if there is an I/O problem.
076     * @throws XMLWriterException if there is a problem with the writer.
077     */
078    public void write(final String tagName, final Object object, final XMLWriter writer,
079                      final String mPlexAttribute, final String mPlexValue)
080        throws IOException, XMLWriterException {
081        final BasicStroke stroke = (BasicStroke) object;
082        final float[] dashArray = stroke.getDashArray();
083        final float dashPhase = stroke.getDashPhase();
084        final int endCap = stroke.getEndCap();
085        final int lineJoin = stroke.getLineJoin();
086        final float lineWidth = stroke.getLineWidth();
087        final float miterLimit = stroke.getMiterLimit();
088        final AttributeList attribs = new AttributeList();
089        if (mPlexAttribute != null) {
090            attribs.setAttribute(mPlexAttribute, mPlexValue);
091        }
092        attribs.setAttribute("type", "basic");
093        attribs.setAttribute("endCap", String.valueOf(endCap));
094        attribs.setAttribute("lineJoin", String.valueOf(lineJoin));
095        attribs.setAttribute("lineWidth", String.valueOf(lineWidth));
096        attribs.setAttribute("miterLimit", String.valueOf(miterLimit));
097        if (dashArray != null) {
098            attribs.setAttribute("dashArray", toString(dashArray));
099            attribs.setAttribute("dashPhase", String.valueOf(dashPhase));
100        }
101        writer.writeTag(tagName, attribs, true);
102    }
103
104    /**
105     * A utility method that converts a dash array (from a {@link BasicStroke} object) to 
106     * a {@link String}.
107     * 
108     * @param dashArray  the dash array.
109     * 
110     * @return a {@link String} representing the dash array.
111     */
112    private String toString(final float[] dashArray) {
113        final StringBuffer buffer = new StringBuffer();
114        for (int i = 0; i < dashArray.length; i++) {
115            final float f = dashArray[i];
116            if (i != 0) {
117                buffer.append(',');
118            }
119            buffer.append(f);
120        }
121        return buffer.toString();
122    }
123    
124}