1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
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.
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.
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,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
27
* ----------------------
28
* ObjectDescription.java
29
* ----------------------
30
* (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner;
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: CollectionObjectDescription.java,v 1.2 2005/10/18 13:31:58 mungady Exp $
38
* -------------------------
39
* 06-May-2003 : Initial version
41
package org.jfree.xml.factory.objects;
43
import java.util.ArrayList;
44
import java.util.Collection;
45
import java.util.Iterator;
47
import org.jfree.util.Log;
50
* An object description for simple collection objects, like java.util.List
53
* @author Thomas Morgner
55
public class CollectionObjectDescription extends AbstractObjectDescription {
58
* Creates a list object description for the given collection class.
60
* Throws <code>ClassCastException</code> if the given class is no collection instance.
62
* @param c the class of the collection implementation.
64
public CollectionObjectDescription(final Class c) {
66
if (!Collection.class.isAssignableFrom(c)) {
67
throw new ClassCastException("The given class is no Collection instance");
72
* Tries to parse the given parameter string into a positive integer.
73
* Returns -1 if the parsing failed for some reason.
75
* @param name the name of the parameter.
76
* @return the parsed int value or -1 on errors.
78
private int parseParameterName(final String name) {
80
return Integer.parseInt(name);
88
* Returns a parameter definition. If the parameter is invalid, this
89
* function returns null.
91
* @param name the definition name.
93
* @return The parameter class or null, if the parameter is not defined.
95
public Class getParameterDefinition(final String name) {
96
if (name.equals("size")) {
99
final int par = parseParameterName(name);
107
* Returns an iterator for the parameter names.
109
* @return The iterator.
111
public Iterator getParameterNames() {
112
final Integer size = (Integer) getParameter("size");
114
return getDefinedParameterNames();
117
final ArrayList l = new ArrayList();
119
for (int i = 0; i < size.intValue(); i++) {
120
l.add(String.valueOf(i));
127
* Creates an object based on the description.
129
* @return The object.
131
public Object createObject() {
133
final Collection l = (Collection) getObjectClass().newInstance();
135
while (getParameterDefinition(String.valueOf(counter)) != null) {
136
final Object value = getParameter(String.valueOf(counter));
146
catch (Exception ie) {
147
Log.warn("Unable to instantiate Object", ie);
153
* Sets the parameters of this description object to match the supplied object.
155
* @param o the object.
157
* @throws ObjectFactoryException if there is a problem while reading the
158
* properties of the given object.
160
public void setParameterFromObject(final Object o) throws ObjectFactoryException {
162
throw new NullPointerException("Given object is null");
164
final Class c = getObjectClass();
165
if (!c.isInstance(o)) {
166
throw new ObjectFactoryException("Object is no instance of " + c + "(is "
167
+ o.getClass() + ")");
170
final Collection l = (Collection) o;
171
final Iterator it = l.iterator();
173
while (it.hasNext()) {
174
final Object ob = it.next();
175
setParameter(String.valueOf(counter), ob);