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

   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:  * RenderingHintValueReadHandler.java
  29:  * ----------------------------------
  30:  * (C)opyright 2003, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: RenderingHintValueReadHandler.java,v 1.3 2005/10/18 13:33:32 mungady Exp $
  36:  *
  37:  * Changes 
  38:  * -------
  39:  * 03-Dec-2003 : Initial version
  40:  * 11-Feb-2004 : Added missing Javadocs (DG);
  41:  * 
  42:  */
  43: 
  44: package org.jfree.xml.parser.coretypes;
  45: 
  46: import java.awt.RenderingHints;
  47: import java.lang.reflect.Field;
  48: import java.lang.reflect.Modifier;
  49: 
  50: import org.jfree.util.Log;
  51: import org.jfree.xml.parser.AbstractXmlReadHandler;
  52: import org.jfree.xml.parser.XmlReaderException;
  53: import org.xml.sax.Attributes;
  54: import org.xml.sax.SAXException;
  55: 
  56: /**
  57:  * A read handler for a rendering hint value.
  58:  */
  59: public class RenderingHintValueReadHandler extends AbstractXmlReadHandler {
  60: 
  61:     /** The key under construction. */
  62:     private Object key;
  63:     
  64:     /** The value under construction. */
  65:     private Object value;
  66: 
  67:     /**
  68:      * Creates a new read handler.
  69:      */
  70:     public RenderingHintValueReadHandler() {
  71:         super();
  72:     }
  73: 
  74:     /**
  75:      * Starts parsing.
  76:      *
  77:      * @param attrs  the attributes.
  78:      *
  79:      * @throws SAXException if there is a parsing error.
  80:      */
  81:     protected void startParsing(final Attributes attrs) throws SAXException {
  82:         final String keyText = attrs.getValue("key");
  83:         final String valueText = attrs.getValue("value");
  84:         this.key = stringToHintField(keyText);
  85:         this.value = stringToHintField(valueText);
  86:     }
  87: 
  88:     private Object stringToHintField (final String name) {
  89:         final Field[] fields = RenderingHints.class.getFields();
  90:         for (int i = 0; i < fields.length; i++) {
  91:             final Field f = fields[i];
  92:             if (Modifier.isFinal(f.getModifiers()) 
  93:                 && Modifier.isPublic(f.getModifiers()) 
  94:                 && Modifier.isStatic(f.getModifiers())) {
  95:                 try {
  96:                     final String fieldName = f.getName();
  97:                     if (fieldName.equals(name)) {
  98:                         return f.get(null);
  99:                     }
 100:                 }
 101:                 catch (Exception e) {
 102:                     Log.info ("Unable to write RenderingHint", e);
 103:                 }
 104:             }
 105:         }
 106:         throw new IllegalArgumentException("Invalid value given");
 107:     }
 108: 
 109:     /**
 110:      * Returns the object for this element.
 111:      *
 112:      * @return the object.
 113:      *
 114:      * @throws XmlReaderException if there is a parsing error.
 115:      */
 116:     public Object getObject() throws XmlReaderException {
 117:         return new Object[] {this.key, this.value};
 118:     }
 119: 
 120:     /**
 121:      * Returns the key.
 122:      * 
 123:      * @return the key.
 124:      */
 125:     public Object getKey() {
 126:         return this.key;
 127:     }
 128: 
 129:     /**
 130:      * Returns the value.
 131:      * 
 132:      * @return the value.
 133:      */
 134:     public Object getValue() {
 135:         return this.value;
 136:     }
 137:     
 138: }