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

   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:  * FontReadHandler
  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: FontReadHandler.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.Font;
  46: 
  47: import org.jfree.xml.parser.AbstractXmlReadHandler;
  48: import org.xml.sax.Attributes;
  49: import org.xml.sax.SAXException;
  50: 
  51: /**
  52:  * A SAX handler for reading a font definition.
  53:  */
  54: public class FontReadHandler extends AbstractXmlReadHandler  {
  55:     
  56:     /** The font under construction. */
  57:     private Font font;
  58: 
  59:     /**
  60:      * Creates a new SAX handler for reading a {@link Font} from XML.
  61:      */
  62:     public FontReadHandler() {
  63:         super();
  64:     }
  65: 
  66:     /**
  67:      * Called at the start of parsing a font element, this method reads the attributes and
  68:      * constructs the font. 
  69:      * 
  70:      * @param attrs  the attributes.
  71:      * 
  72:      * @throws SAXException to indicate a parsing error.
  73:      */
  74:     protected void startParsing(final Attributes attrs) throws SAXException {
  75:         final String family = attrs.getValue("family");
  76:         final int size = Integer.parseInt(attrs.getValue("size"));
  77:         final int style = getFontStyle(attrs.getValue("style"));
  78:         this.font = new Font(family, style, size);
  79:     }
  80: 
  81:     /**
  82:      * Converts a string to a font style constant.
  83:      * 
  84:      * @param style  the style as text.
  85:      * 
  86:      * @return The font style.
  87:      */
  88:     private int getFontStyle (final String style) {
  89:         if ("bold-italic".equals(style)) {
  90:             return Font.BOLD | Font.ITALIC;
  91:         }
  92:         if ("bold".equals(style)) {
  93:             return Font.BOLD;
  94:         }
  95:         if ("italic".equals(style)) {
  96:             return Font.ITALIC;
  97:         }
  98:         return Font.PLAIN;
  99:     }
 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: }