    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 */
    041package org.jfree.xml.factory.objects;
    042
    043import java.util.ArrayList;
    044import java.util.Collection;
    045import java.util.Iterator;
    046
    047import 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 */
    055public class CollectionObjectDescription extends AbstractObjectDescription {
    056
    057    /**
    058     * Creates a list object description for the given collection class.
    059     * <P>
    060     * Throws <code>ClassCastException</code> if the given class is no collection instance.
    061     *
    062     * @param c the class of the collection implementation.
    063     */
    064    public CollectionObjectDescription(final Class c) {
    065        super(c);
    066        if (!Collection.class.isAssignableFrom(c)) {
    067            throw new ClassCastException("The given class is no Collection instance");
    068        }
    069    }
    070
    071    /**
    072     * Tries to parse the given parameter string into a positive integer.
    073     * Returns -1 if the parsing failed for some reason.
    074     *
    075     * @param name the name of the parameter.
    076     * @return the parsed int value or -1 on errors.
    077     */
    078    private int parseParameterName(final String name) {
    079        try {
    080            return Integer.parseInt(name);
    081        }
    082        catch (Exception e) {
    083            return -1;
    084        }
    085    }
    086
    087    /**
    088     * Returns a parameter definition. If the parameter is invalid, this
    089     * function returns null.
    090     *
    091     * @param name  the definition name.
    092     *
    093     * @return The parameter class or null, if the parameter is not defined.
    094     */
    095    public Class getParameterDefinition(final String name) {
    096        if (name.equals("size")) {
    097            return Integer.TYPE;
    098        }
    099        final int par = parseParameterName(name);
    100        if (par < 0) {
    101            return null;
    102        }
    103        return Object.class;
    104    }
    105
    106    /**
    107     * Returns an iterator for the parameter names.
    108     *
    109     * @return The iterator.
    110     */
    111    public Iterator getParameterNames() {
    112        final Integer size = (Integer) getParameter("size");
    113        if (size == null) {
    114            return getDefinedParameterNames();
    115        }
    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().newInstance();
    134            int counter = 0;
    135            while (getParameterDefinition(String.valueOf(counter)) != null) {
    136                final Object value = getParameter(String.valueOf(counter));
    137                if (value == null) {
    138                    break;
    139                }
    140
    141                l.add(value);
    142                counter += 1;
    143            }
    144            return l;
    145        }
    146        catch (Exception ie) {
    147            Log.warn("Unable to instantiate Object", ie);
    148            return null;
    149        }
    150    }
    151
    152    /**
    153     * Sets the parameters of this description object to match the supplied object.
    154     *
    155     * @param o  the object.
    156     *
    157     * @throws ObjectFactoryException if there is a problem while reading the
    158     * properties of the given object.
    159     */
    160    public void setParameterFromObject(final Object o) throws ObjectFactoryException {
    161        if (o == null) {
    162            throw new NullPointerException("Given object is null");
    163        }
    164        final Class c = getObjectClass();
    165        if (!c.isInstance(o)) {
    166            throw new ObjectFactoryException("Object is no instance of " + c + "(is "
    167                + o.getClass() + ")");
    168        }
    169
    170        final Collection l = (Collection) o;
    171        final Iterator it = l.iterator();
    172        int counter = 0;
    173        while (it.hasNext()) {
    174            final Object ob = it.next();
    175            setParameter(String.valueOf(counter), ob);
    176            counter++;
    177        }
    178    }
    179}