~ubuntu-branches/ubuntu/lucid/libjcommon-java/lucid

« back to all changes in this revision

Viewing changes to source/org/jfree/xml/factory/objects/ArrayObjectDescription.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2006-02-09 15:58:13 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060209155813-fzi9zwh2rzedbllq
Tags: 1.0.0-1
* New stable upstream release (closes: #328574)
* Move to main - build with kaffe
* Use cdbs build system - added cdbs build-dependency
* Move package to pkg-java-maintainers for comaintenance, 
  added Christian Bayle and myself as uploaders
* Removed unneeded README.Debian
* Added README.Debian-source how the upstream tarball was cleaned
* Move big documentation in an own -doc package
* Register javadoc api with doc-base
* Standards-Version 3.6.2 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * ArrayObjectDescription.java
 
29
 * ---------------------------
 
30
 * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
 
31
 *
 
32
 * Original Author:  Thomas Morgner (taquera@sherito.org);
 
33
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 
34
 *
 
35
 * $Id: ArrayObjectDescription.java,v 1.3 2005/11/14 10:58:49 mungady Exp $
 
36
 *
 
37
 * Changes
 
38
 * -------
 
39
 * 14-Apr-2003 : Initial version
 
40
 * 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon
 
41
 */
 
42
package org.jfree.xml.factory.objects;
 
43
 
 
44
import java.lang.reflect.Array;
 
45
import java.util.ArrayList;
 
46
import java.util.Iterator;
 
47
 
 
48
import org.jfree.util.Log;
 
49
 
 
50
/**
 
51
 * Describes an Object- or primitive value array. This object description is
 
52
 * not intended to be created outside the ArrayClassFactory.
 
53
 *
 
54
 * @author Thomas Morgner
 
55
 */
 
56
public class ArrayObjectDescription extends AbstractObjectDescription {
 
57
 
 
58
    /**
 
59
     * Constructs a new array objet description for the given array class.
 
60
     * <P>
 
61
     * Note:  throws <code>IllegalArgumentException</code> if the given class is no array.
 
62
     * 
 
63
     * @param c the array class object.
 
64
     */
 
65
    public ArrayObjectDescription(final Class c) {
 
66
        super(c);
 
67
        if (!c.isArray()) {
 
68
            throw new IllegalArgumentException("Need an array class");
 
69
        }
 
70
    }
 
71
 
 
72
    /**
 
73
     * Creates an object based on the description.
 
74
     *
 
75
     * @return The object.
 
76
     */
 
77
    public Object createObject() {
 
78
        try {
 
79
            final Integer size = (Integer) getParameter("size");
 
80
            if (size == null) {
 
81
                final ArrayList l = new ArrayList();
 
82
                int counter = 0;
 
83
                while (getParameterDefinition(String.valueOf(counter)) != null) {
 
84
                    final Object value = getParameter(String.valueOf(counter));
 
85
                    if (value == null) {
 
86
                        break;
 
87
                    }
 
88
 
 
89
                    l.add(value);
 
90
                    counter += 1;
 
91
                }
 
92
 
 
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));
 
97
                }
 
98
                return o;
 
99
            }
 
100
            else {
 
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)));
 
106
                }
 
107
                return o;
 
108
            }
 
109
        }
 
110
        catch (Exception ie) {
 
111
            Log.warn("Unable to instantiate Object", ie);
 
112
            return null;
 
113
        }
 
114
    }
 
115
 
 
116
    /**
 
117
     * Sets the parameters of this description object to match the supplied object.
 
118
     *
 
119
     * @param o  the object.
 
120
     *
 
121
     * @throws ObjectFactoryException if there is a
 
122
     * problem while reading the properties of the given object.
 
123
     */
 
124
    public void setParameterFromObject(final Object o) throws ObjectFactoryException {
 
125
        if (o == null) {
 
126
            throw new ObjectFactoryException("Given object is null.");
 
127
        }
 
128
 
 
129
        if (!o.getClass().isArray()) {
 
130
            throw new ObjectFactoryException("Given object is no array");
 
131
        }
 
132
 
 
133
        if (!getObjectClass().isAssignableFrom(o.getClass())) {
 
134
            throw new ObjectFactoryException("Given object is incompatible with base class");
 
135
        }
 
136
 
 
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));
 
141
        }
 
142
    }
 
143
 
 
144
    /**
 
145
     * Tries to parse the given parameter string into a positive integer.
 
146
     * Returns -1 if the parsing failed for some reason.
 
147
     *
 
148
     * @param name the name of the parameter.
 
149
     * @return the parsed int value or -1 on errors.
 
150
     */
 
151
    private int parseParameterName(final String name) {
 
152
        try {
 
153
            return Integer.parseInt(name);
 
154
        }
 
155
        catch (Exception e) {
 
156
            return -1;
 
157
        }
 
158
    }
 
159
 
 
160
    /**
 
161
     * Returns a parameter definition. If the parameter is invalid, this
 
162
     * function returns null.
 
163
     *
 
164
     * @param name  the definition name.
 
165
     *
 
166
     * @return The parameter class or null, if the parameter is not defined.
 
167
     */
 
168
    public Class getParameterDefinition(final String name) {
 
169
        if (name.equals("size")) {
 
170
            return Integer.TYPE;
 
171
        }
 
172
        final int par = parseParameterName(name);
 
173
        if (par < 0) {
 
174
            return null;
 
175
        }
 
176
        return Object.class;
 
177
    }
 
178
 
 
179
    /**
 
180
     * Returns an iterator for the parameter names.
 
181
     *
 
182
     * @return The iterator.
 
183
     */
 
184
    public Iterator getParameterNames() {
 
185
        final Integer size = (Integer) getParameter("size");
 
186
        if (size == null) {
 
187
            return getDefinedParameterNames();
 
188
        }
 
189
        else {
 
190
            final ArrayList l = new ArrayList();
 
191
            l.add("size");
 
192
            for (int i = 0; i < size.intValue(); i++) {
 
193
                l.add(String.valueOf(i));
 
194
            }
 
195
            return l.iterator();
 
196
        }
 
197
    }
 
198
 
 
199
    /**
 
200
     * Returns a new instance of the object description.
 
201
     *
 
202
     * @return The object description.
 
203
     */
 
204
    public ObjectDescription getInstance() {
 
205
        return new ArrayObjectDescription(getObjectClass());
 
206
    }
 
207
}