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) throws SAXException { 086 this.listType = attrs.getValue("type"); 087 if (this.listType == null) { 088 this.listType = "array-list"; 089 } 090 this.handlers = new ArrayList(); 091 } 092 093 /** 094 * Gets a handler for a child. 095 * 096 * @param tagName the tag name. 097 * @param atts the attributes. 098 * 099 * @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 tagName, final Attributes atts) 105 throws XmlReaderException, SAXException { 106 final XmlReadHandler handler = getRootHandler().createHandler(Object.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, 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 createList(final int initialSize) { 136 if (this.listType.equals("stack")) { 137 return new Stack(); 138 } 139 if (this.listType.equals("linked-list")) { 140 return new LinkedList(); 141 } 142 if (this.listType.equals("vector")) { 143 return new Vector(initialSize); 144 } 145 return new ArrayList(initialSize); 146 } 147 148 /** 149 * Returns the object under construction. 150 * 151 * @return The list. 152 */ 153 public Object getObject() { 154 return this.retval; 155 } 156 157 }