001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * AbstractStyleKeyFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.parser.ext.factory.stylekey;
030:
031: import java.lang.reflect.Field;
032: import java.lang.reflect.Modifier;
033: import java.util.HashMap;
034: import java.util.Iterator;
035:
036: import org.jfree.report.modules.parser.ext.factory.base.ClassFactory;
037: import org.jfree.report.modules.parser.ext.factory.base.ObjectDescription;
038: import org.jfree.report.style.StyleKey;
039: import org.jfree.util.Log;
040:
041: /**
042: * An abstract class for implementing the {@link StyleKeyFactory} interface.
043: *
044: * @author Thomas Morgner.
045: */
046: public abstract class AbstractStyleKeyFactory implements
047: StyleKeyFactory {
048: /**
049: * the parser configuration property name for this factory.
050: */
051: public static final String OBJECT_FACTORY_TAG = "object-factory";
052:
053: /**
054: * Storage for the keys.
055: */
056: private final HashMap knownKeys;
057:
058: /**
059: * Creates a new factory.
060: */
061: protected AbstractStyleKeyFactory() {
062: knownKeys = new HashMap();
063: }
064:
065: /**
066: * Registers a key.
067: *
068: * @param key the key.
069: */
070: public void addKey(final StyleKey key) {
071: knownKeys.put(key.getName(), key);
072: }
073:
074: /**
075: * Returns the key with the given name.
076: *
077: * @param name the name.
078: * @return The key.
079: */
080: public StyleKey getStyleKey(final String name) {
081: return (StyleKey) knownKeys.get(name);
082: }
083:
084: /**
085: * Creates an object.
086: *
087: * @param k the style key.
088: * @param value the value.
089: * @param c the class.
090: * @param fc the class factory used to create the basic object.
091: * @return The object.
092: */
093: public Object createBasicObject(final StyleKey k,
094: final String value, final Class c, final ClassFactory fc) {
095: if (k == null) {
096: // no such key registered ...
097: return null;
098: }
099:
100: if (c == null) {
101: throw new NullPointerException();
102: }
103:
104: if (fc == null) {
105: throw new NullPointerException("Class " + getClass());
106: }
107:
108: ObjectDescription od = fc.getDescriptionForClass(c);
109: if (od == null) {
110: od = fc.getSuperClassObjectDescription(c, null);
111: if (od == null) {
112: return null;
113: }
114: }
115: od.setParameter("value", value);
116: return od.createObject();
117: }
118:
119: /**
120: * Loads all public static stylekeys which are declared in the given class.
121: *
122: * @param c the class from where to load the stylekeys.
123: * @throws SecurityException if the current security settings deny class access.
124: */
125: protected void loadFromClass(final Class c) {
126: final Field[] fields = c.getFields();
127: for (int i = 0; i < fields.length; i++) {
128: final Field f = fields[i];
129: if (StyleKey.class.isAssignableFrom(f.getType()) == false) {
130: // is no instance of stylekey...
131: continue;
132: }
133:
134: if (Modifier.isPublic(f.getModifiers())
135: && Modifier.isStatic(f.getModifiers())) {
136: try {
137: addKey((StyleKey) f.get(null));
138: } catch (IllegalAccessException ex) {
139: Log
140: .warn(
141: "Unexpected Exception while loading stylekeys",
142: ex);
143: }
144: }
145: }
146: }
147:
148: /**
149: * Returns an iterator that provides access to the registered keys.
150: *
151: * @return The iterator.
152: */
153: public Iterator getRegisteredKeys() {
154: return knownKeys.keySet().iterator();
155: }
156:
157: /**
158: * Indicated whether an other object is equal to this one.
159: *
160: * @param o the other object.
161: * @return true, if the object is equal, false otherwise.
162: *
163: * @see java.lang.Object#equals(java.lang.Object)
164: */
165: public boolean equals(final Object o) {
166: if (this == o) {
167: return true;
168: }
169: if (!(o instanceof AbstractStyleKeyFactory)) {
170: return false;
171: }
172:
173: final AbstractStyleKeyFactory abstractStyleKeyFactory = (AbstractStyleKeyFactory) o;
174:
175: if (!knownKeys.equals(abstractStyleKeyFactory.knownKeys)) {
176: return false;
177: }
178:
179: return true;
180: }
181:
182: /**
183: * Computes an hashcode for this factory.
184: *
185: * @return the hashcode.
186: *
187: * @see java.lang.Object#hashCode()
188: */
189: public int hashCode() {
190: return knownKeys.hashCode();
191: }
192: }
|