    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 * FontReadHandler
    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: FontReadHandler.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
    043package org.jfree.xml.parser.coretypes;
    044
    045import java.awt.Font;
    046
    047import org.jfree.xml.parser.AbstractXmlReadHandler;
    048import org.xml.sax.Attributes;
    049import org.xml.sax.SAXException;
    050
    051/**
    052 * A SAX handler for reading a font definition.
    053 */
    054public class FontReadHandler extends AbstractXmlReadHandler  {
    055
    056    /** The font under construction. */
    057    private Font font;
    058
    059    /**
    060     * Creates a new SAX handler for reading a {@link Font} from XML.
    061     */
    062    public FontReadHandler() {
    063        super();
    064    }
    065
    066    /**
    067     * Called at the start of parsing a font element, this method reads the attributes and
    068     * constructs the font.
    069     *
    070     * @param attrs  the attributes.
    071     *
    072     * @throws SAXException to indicate a parsing error.
    073     */
    074    protected void startParsing(final Attributes attrs) throws SAXException {
    075        final String family = attrs.getValue("family");
    076        final int size = Integer.parseInt(attrs.getValue("size"));
    077        final int style = getFontStyle(attrs.getValue("style"));
    078        this.font = new Font(family, style, size);
    079    }
    080
    081    /**
    082     * Converts a string to a font style constant.
    083     *
    084     * @param style  the style as text.
    085     *
    086     * @return The font style.
    087     */
    088    private int getFontStyle (final String style) {
    089        if ("bold-italic".equals(style)) {
    090            return Font.BOLD | Font.ITALIC;
    091        }
    092        if ("bold".equals(style)) {
    093            return Font.BOLD;
    094        }
    095        if ("italic".equals(style)) {
    096            return Font.ITALIC;
    097        }
    098        return Font.PLAIN;
    099    }
    100
    101    /**
    102     * Returns the font under construction.
    103     *
    104     * @return the font.
    105     */
    106    public Object getObject() {
    107        return this.font;
    108    }
    109
    110}