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: * ObjectDescription.java
029: * ----------------------
030: * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: CollectionObjectDescription.java,v 1.2 2005/10/18 13:31:58 mungady Exp $
036: *
037: * Changes
038: * -------------------------
039: * 06-May-2003 : Initial version
040: */
041: package org.jfree.xml.factory.objects;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046:
047: import org.jfree.util.Log;
048:
049: /**
050: * An object description for simple collection objects, like java.util.List
051: * or java.util.Set.
052: *
053: * @author Thomas Morgner
054: */
055: public class CollectionObjectDescription extends
056: AbstractObjectDescription {
057:
058: /**
059: * Creates a list object description for the given collection class.
060: * <P>
061: * Throws <code>ClassCastException</code> if the given class is no collection instance.
062: *
063: * @param c the class of the collection implementation.
064: */
065: public CollectionObjectDescription(final Class c) {
066: super (c);
067: if (!Collection.class.isAssignableFrom(c)) {
068: throw new ClassCastException(
069: "The given class is no Collection instance");
070: }
071: }
072:
073: /**
074: * Tries to parse the given parameter string into a positive integer.
075: * Returns -1 if the parsing failed for some reason.
076: *
077: * @param name the name of the parameter.
078: * @return the parsed int value or -1 on errors.
079: */
080: private int parseParameterName(final String name) {
081: try {
082: return Integer.parseInt(name);
083: } catch (Exception e) {
084: return -1;
085: }
086: }
087:
088: /**
089: * Returns a parameter definition. If the parameter is invalid, this
090: * function returns null.
091: *
092: * @param name the definition name.
093: *
094: * @return The parameter class or null, if the parameter is not defined.
095: */
096: public Class getParameterDefinition(final String name) {
097: if (name.equals("size")) {
098: return Integer.TYPE;
099: }
100: final int par = parseParameterName(name);
101: if (par < 0) {
102: return null;
103: }
104: return Object.class;
105: }
106:
107: /**
108: * Returns an iterator for the parameter names.
109: *
110: * @return The iterator.
111: */
112: public Iterator getParameterNames() {
113: final Integer size = (Integer) getParameter("size");
114: if (size == null) {
115: return getDefinedParameterNames();
116: } else {
117: final ArrayList l = new ArrayList();
118: l.add("size");
119: for (int i = 0; i < size.intValue(); i++) {
120: l.add(String.valueOf(i));
121: }
122: return l.iterator();
123: }
124: }
125:
126: /**
127: * Creates an object based on the description.
128: *
129: * @return The object.
130: */
131: public Object createObject() {
132: try {
133: final Collection l = (Collection) getObjectClass()
134: .newInstance();
135: int counter = 0;
136: while (getParameterDefinition(String.valueOf(counter)) != null) {
137: final Object value = getParameter(String
138: .valueOf(counter));
139: if (value == null) {
140: break;
141: }
142:
143: l.add(value);
144: counter += 1;
145: }
146: return l;
147: } catch (Exception ie) {
148: Log.warn("Unable to instantiate Object", ie);
149: return null;
150: }
151: }
152:
153: /**
154: * Sets the parameters of this description object to match the supplied object.
155: *
156: * @param o the object.
157: *
158: * @throws ObjectFactoryException if there is a problem while reading the
159: * properties of the given object.
160: */
161: public void setParameterFromObject(final Object o)
162: throws ObjectFactoryException {
163: if (o == null) {
164: throw new NullPointerException("Given object is null");
165: }
166: final Class c = getObjectClass();
167: if (!c.isInstance(o)) {
168: throw new ObjectFactoryException(
169: "Object is no instance of " + c + "(is "
170: + o.getClass() + ")");
171: }
172:
173: final Collection l = (Collection) o;
174: final Iterator it = l.iterator();
175: int counter = 0;
176: while (it.hasNext()) {
177: final Object ob = it.next();
178: setParameter(String.valueOf(counter), ob);
179: counter++;
180: }
181: }
182: }
|