--- projects/cms/source/util/uk/org/iscream/cms/util/XMLStringParser.java 2000/11/30 02:38:17 1.4 +++ projects/cms/source/util/uk/org/iscream/cms/util/XMLStringParser.java 2001/03/16 01:33:46 1.12 @@ -1,108 +1,106 @@ //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.util; //---IMPORTS--- import java.io.*; -import java.util.ArrayList; - +import java.util.LinkedList; +import java.util.Iterator; import org.xml.sax.*; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; +import org.xml.sax.helpers.*; +import javax.xml.parsers.*; /** * XMLStringParser - Used to assist in creating XMLPacket objects. * - * @author $Author: ajm $ - * @version $Id: XMLStringParser.java,v 1.4 2000/11/30 02:38:17 ajm Exp $ + * @author $Author: tdb $ + * @version $Id: XMLStringParser.java,v 1.12 2001/03/16 01:33:46 tdb Exp $ */ -public class XMLStringParser extends HandlerBase { +public class XMLStringParser extends DefaultHandler { //---FINAL ATTRIBUTES--- /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.4 $"; + public final String REVISION = "$Revision: 1.12 $"; //---STATIC METHODS--- //---CONSTRUCTORS--- - // No-args constructor. Generally not used. + /** + * No-args constructor. Generally not used. + */ public XMLStringParser () { - this.packet = new XMLPacket(); + _packet = new XMLPacket(); } - // Constructor for accepting a reference to an XMLPacket + /** + * Constructor for accepting a reference to an XMLPacket + */ public XMLStringParser (XMLPacket packet) { - this.packet = packet; + _packet = packet; } //---PUBLIC METHODS--- - // Accessor to the XMLPacket. - public XMLPacket getXMLPacket() { - return packet; - } - //=========================================================== // SAX DocumentHandler methods //=========================================================== - public void startDocument () throws SAXException { - //System.out.println("XMLPacketParser - Starting parse process..."); - } - - public void endDocument () throws SAXException { - //System.out.println("XMLPacketParser - I just finished parsing an XML String."); - } - - // Add each tag's attribute to the XMLPacket. - // Note that all attributes within an opening tag are - // stored as "someroot.sometag.attributes.attribute_name" - // E.g. If is the root node, then: - // - // is stored as: - // "packet.attributes.machine_name" - // within the XMLPacket. - public void startElement (String name, AttributeList attrs) throws SAXException { - indentLevel++; - tagList.add(name); - if (attrs != null) { - for (int i = 0; i < attrs.getLength (); i++) { - packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); + /** + * Add each tag's attribute to the XMLPacket. + * Note that all attributes within an opening tag are + * stored as "someroot.sometag.attributes.attribute_name" + * E.g. If is the root node, then: + * + * is stored as: + * "packet.attributes.machine_name" + * within the XMLPacket. + */ + public void startElement (String uri, String name, String qName, Attributes atts) { + _tagList.addLast(name); + if (atts != null) { + for (int i = 0; i < atts.getLength (); i++) { + _packet.addParam(getPath()+".attributes."+atts.getLocalName(i), atts.getValue(i)); } } } - - // When an XML element is finished with, we must remove - // the tag name from the tagList and decrement the indent - // level. - public void endElement (String name) throws SAXException { - tagList.remove(tagList.size()-1); - indentLevel--; + + /** + * When an XML element is finished with, we must remove + * the tag name from the tagList and decrement the indent + * level. + */ + public void endElement (String uri, String name, String qName) { + _tagList.removeLast(); } - - // Any text falling within a pair of terminal tags must - // be added to the XMLPacket. Trim leading and trailing - // spaces and do not bother to add if there is no data - // specified within the tags. + + /** + * Any text falling within a pair of terminal tags must + * be added to the XMLPacket. Trim leading and trailing + * spaces and do not bother to add if there is no data + * specified within the tags. + */ public void characters (char[] buf, int offset, int len) throws SAXException { String s = new String(buf, offset, len); - if (!s.trim().equals("")) { - packet.addParam(getPath(), s); - } + _packet.addParam(getPath(), s); } /** * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * + * This uses the uk.org.iscream.util.NameFormat class + * to format the toString() + * * @return the name of this class and its CVS revision */ public String toString() { - return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; + return FormatName.getName( + _name, + getClass().getName(), + REVISION); } //---PRIVATE METHODS--- @@ -111,14 +109,17 @@ public class XMLStringParser extends HandlerBase { // Helpers ... //=========================================================== - - // Return the heirarchical string to be used as a key value - // in the XMLPacket. + /** + * Return the heirarchical string to be used as a key value + * in the XMLPacket. + */ private String getPath () { - String path = (String)tagList.get(0); - if (tagList.size() > 0) { - for (int i = 1 ; i < tagList.size() ; i++) { - path = path + "." + (String)tagList.get(i); + String path = ""; + if (_tagList.size() > 0) { + Iterator i = _tagList.iterator(); + path = (String) i.next(); + while(i.hasNext()) { + path = path + "." + (String) i.next(); } } return path; @@ -126,11 +127,35 @@ public class XMLStringParser extends HandlerBase { //---ACCESSOR/MUTATOR METHODS--- -//---ATTRIBUTES--- + /** + * Accessor to the XMLPacket. + */ + public XMLPacket getXMLPacket() { + return _packet; + } - private int indentLevel = 0; - private ArrayList tagList = new ArrayList(); - private XMLPacket packet; +//---ATTRIBUTES--- + + /** + * A LinkedList of tags + */ + private LinkedList _tagList = new LinkedList(); + + /** + * A reference to the XMLPacket we are making + */ + private XMLPacket _packet; + + /** + * This is the friendly identifier of the + * component this class is running in. + * eg, a Filter may be called "filter1", + * If this class does not have an owning + * component, a name from the configuration + * can be placed here. This name could also + * be changed to null for utility classes. + */ + private String _name = null; //---STATIC ATTRIBUTES---