KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > xml > parser > coretypes > ListReadHandler


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  * ListReadHandler.java
29  * --------------------
30  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: ListReadHandler.java,v 1.3 2005/10/18 13:33:32 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 12-Nov-2003 : Initial version (TM);
40  *
41  */

42
43 package org.jfree.xml.parser.coretypes;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.LinkedList JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Stack JavaDoc;
49 import java.util.Vector JavaDoc;
50
51 import org.jfree.xml.parser.AbstractXmlReadHandler;
52 import org.jfree.xml.parser.XmlReadHandler;
53 import org.jfree.xml.parser.XmlReaderException;
54 import org.xml.sax.Attributes JavaDoc;
55 import org.xml.sax.SAXException JavaDoc;
56
57 /**
58  * A SAX handler for reading a list from an XML element.
59  */

60 public class ListReadHandler extends AbstractXmlReadHandler {
61
62     /** The list under construction. */
63     private List JavaDoc retval;
64     
65     /** The handlers. */
66     private ArrayList JavaDoc handlers;
67     
68     /** The type of list ('array-list', 'linked-list', 'stack', 'vector'). */
69     private String JavaDoc listType;
70
71     /**
72      * Default constructor.
73      */

74     public ListReadHandler() {
75         super();
76     }
77
78     /**
79      * Start parsing.
80      *
81      * @param attrs the attributes.
82      *
83      * @throws SAXException if there is a parsing error.
84      */

85     protected void startParsing(final Attributes JavaDoc attrs) throws SAXException JavaDoc {
86         this.listType = attrs.getValue("type");
87         if (this.listType == null) {
88             this.listType = "array-list";
89         }
90         this.handlers = new ArrayList JavaDoc();
91     }
92
93     /**
94      * Gets a handler for a child.
95      *
96      * @param tagName the tag name.
97      * @param atts the attributes.
98      *
99      * @return A handler.
100      *
101      * @throws XmlReaderException if there is a problem with the reader.
102      * @throws SAXException if there is a parsing error.
103      */

104     protected XmlReadHandler getHandlerForChild(final String JavaDoc tagName, final Attributes JavaDoc atts)
105         throws XmlReaderException, SAXException JavaDoc {
106         final XmlReadHandler handler = getRootHandler().createHandler(Object JavaDoc.class, tagName, atts);
107         this.handlers.add(handler);
108         return handler;
109     }
110
111     /**
112      * Parsing is finished.
113      *
114      * @throws SAXException if there is a parsing error.
115      * @throws XmlReaderException if there is a problem with the reader.
116      *
117      */

118     protected void doneParsing() throws SAXException JavaDoc, XmlReaderException {
119         final XmlReadHandler[] handler = (XmlReadHandler[])
120         this.handlers.toArray(new XmlReadHandler[this.handlers.size()]);
121         this.retval = createList(handler.length);
122         for (int i = 0; i < handler.length; i++) {
123             this.retval.add(handler[i].getObject());
124         }
125         this.handlers.clear();
126     }
127
128     /**
129      * Creates a list.
130      *
131      * @param initialSize the initial size.
132      *
133      * @return A new list.
134      */

135     private List JavaDoc createList(final int initialSize) {
136         if (this.listType.equals("stack")) {
137             return new Stack JavaDoc();
138         }
139         if (this.listType.equals("linked-list")) {
140             return new LinkedList JavaDoc();
141         }
142         if (this.listType.equals("vector")) {
143             return new Vector JavaDoc(initialSize);
144         }
145         return new ArrayList JavaDoc(initialSize);
146     }
147
148     /**
149      * Returns the object under construction.
150      *
151      * @return The list.
152      */

153     public Object JavaDoc getObject() {
154         return this.retval;
155     }
156     
157 }
158
Popular Tags