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
* ArrayObjectDescription.java
29
* ---------------------------
30
* (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
32
* Original Author: Thomas Morgner (taquera@sherito.org);
33
* Contributor(s): David Gilbert (for Object Refinery Limited);
35
* $Id: ArrayObjectDescription.java,v 1.3 2005/11/14 10:58:49 mungady Exp $
39
* 14-Apr-2003 : Initial version
40
* 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon
42
package org.jfree.xml.factory.objects;
44
import java.lang.reflect.Array;
45
import java.util.ArrayList;
46
import java.util.Iterator;
48
import org.jfree.util.Log;
51
* Describes an Object- or primitive value array. This object description is
52
* not intended to be created outside the ArrayClassFactory.
54
* @author Thomas Morgner
56
public class ArrayObjectDescription extends AbstractObjectDescription {
59
* Constructs a new array objet description for the given array class.
61
* Note: throws <code>IllegalArgumentException</code> if the given class is no array.
63
* @param c the array class object.
65
public ArrayObjectDescription(final Class c) {
68
throw new IllegalArgumentException("Need an array class");
73
* Creates an object based on the description.
77
public Object createObject() {
79
final Integer size = (Integer) getParameter("size");
81
final ArrayList l = new ArrayList();
83
while (getParameterDefinition(String.valueOf(counter)) != null) {
84
final Object value = getParameter(String.valueOf(counter));
93
final Object o = Array.newInstance
94
(getObjectClass().getComponentType(), l.size());
95
for (int i = 0; i < l.size(); i++) {
96
Array.set(o, i, l.get(i));
101
// a size is given, so we can assume that all values are defined.
102
final Object o = Array.newInstance
103
(getObjectClass().getComponentType(), size.intValue());
104
for (int i = 0; i < size.intValue(); i++) {
105
Array.set(o, i, getParameter(String.valueOf(i)));
110
catch (Exception ie) {
111
Log.warn("Unable to instantiate Object", ie);
117
* Sets the parameters of this description object to match the supplied object.
119
* @param o the object.
121
* @throws ObjectFactoryException if there is a
122
* problem while reading the properties of the given object.
124
public void setParameterFromObject(final Object o) throws ObjectFactoryException {
126
throw new ObjectFactoryException("Given object is null.");
129
if (!o.getClass().isArray()) {
130
throw new ObjectFactoryException("Given object is no array");
133
if (!getObjectClass().isAssignableFrom(o.getClass())) {
134
throw new ObjectFactoryException("Given object is incompatible with base class");
137
final int size = Array.getLength(o);
138
setParameter("size", new Integer(size));
139
for (int i = 0; i < size; i++) {
140
setParameter(String.valueOf(i), Array.get(o, i));
145
* Tries to parse the given parameter string into a positive integer.
146
* Returns -1 if the parsing failed for some reason.
148
* @param name the name of the parameter.
149
* @return the parsed int value or -1 on errors.
151
private int parseParameterName(final String name) {
153
return Integer.parseInt(name);
155
catch (Exception e) {
161
* Returns a parameter definition. If the parameter is invalid, this
162
* function returns null.
164
* @param name the definition name.
166
* @return The parameter class or null, if the parameter is not defined.
168
public Class getParameterDefinition(final String name) {
169
if (name.equals("size")) {
172
final int par = parseParameterName(name);
180
* Returns an iterator for the parameter names.
182
* @return The iterator.
184
public Iterator getParameterNames() {
185
final Integer size = (Integer) getParameter("size");
187
return getDefinedParameterNames();
190
final ArrayList l = new ArrayList();
192
for (int i = 0; i < size.intValue(); i++) {
193
l.add(String.valueOf(i));
200
* Returns a new instance of the object description.
202
* @return The object description.
204
public ObjectDescription getInstance() {
205
return new ArrayObjectDescription(getObjectClass());