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: * ListReadHandler.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: ListReadHandler.java,v 1.3 2005/10/18 13:33:32 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 12-Nov-2003 : Initial version (TM);
040: *
041: */
042:
043: package org.jfree.xml.parser.coretypes;
044:
045: import java.util.ArrayList;
046: import java.util.LinkedList;
047: import java.util.List;
048: import java.util.Stack;
049: import java.util.Vector;
050:
051: import org.jfree.xml.parser.AbstractXmlReadHandler;
052: import org.jfree.xml.parser.XmlReadHandler;
053: import org.jfree.xml.parser.XmlReaderException;
054: import org.xml.sax.Attributes;
055: import org.xml.sax.SAXException;
056:
057: /**
058: * A SAX handler for reading a list from an XML element.
059: */
060: public class ListReadHandler extends AbstractXmlReadHandler {
061:
062: /** The list under construction. */
063: private List retval;
064:
065: /** The handlers. */
066: private ArrayList handlers;
067:
068: /** The type of list ('array-list', 'linked-list', 'stack', 'vector'). */
069: private String listType;
070:
071: /**
072: * Default constructor.
073: */
074: public ListReadHandler() {
075: super ();
076: }
077:
078: /**
079: * Start parsing.
080: *
081: * @param attrs the attributes.
082: *
083: * @throws SAXException if there is a parsing error.
084: */
085: protected void startParsing(final Attributes attrs)
086: throws SAXException {
087: this .listType = attrs.getValue("type");
088: if (this .listType == null) {
089: this .listType = "array-list";
090: }
091: this .handlers = new ArrayList();
092: }
093:
094: /**
095: * Gets a handler for a child.
096: *
097: * @param tagName the tag name.
098: * @param atts the attributes.
099: *
100: * @return A handler.
101: *
102: * @throws XmlReaderException if there is a problem with the reader.
103: * @throws SAXException if there is a parsing error.
104: */
105: protected XmlReadHandler getHandlerForChild(final String tagName,
106: final Attributes atts) throws XmlReaderException,
107: SAXException {
108: final XmlReadHandler handler = getRootHandler().createHandler(
109: Object.class, tagName, atts);
110: this .handlers.add(handler);
111: return handler;
112: }
113:
114: /**
115: * Parsing is finished.
116: *
117: * @throws SAXException if there is a parsing error.
118: * @throws XmlReaderException if there is a problem with the reader.
119: *
120: */
121: protected void doneParsing() throws SAXException,
122: XmlReaderException {
123: final XmlReadHandler[] handler = (XmlReadHandler[]) this .handlers
124: .toArray(new XmlReadHandler[this .handlers.size()]);
125: this .retval = createList(handler.length);
126: for (int i = 0; i < handler.length; i++) {
127: this .retval.add(handler[i].getObject());
128: }
129: this .handlers.clear();
130: }
131:
132: /**
133: * Creates a list.
134: *
135: * @param initialSize the initial size.
136: *
137: * @return A new list.
138: */
139: private List createList(final int initialSize) {
140: if (this .listType.equals("stack")) {
141: return new Stack();
142: }
143: if (this .listType.equals("linked-list")) {
144: return new LinkedList();
145: }
146: if (this .listType.equals("vector")) {
147: return new Vector(initialSize);
148: }
149: return new ArrayList(initialSize);
150: }
151:
152: /**
153: * Returns the object under construction.
154: *
155: * @return The list.
156: */
157: public Object getObject() {
158: return this.retval;
159: }
160:
161: }
|