ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/XMLStringParser.java
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/XMLStringParser.java (file contents):
Revision 1.7 by pjm2, Mon Jan 29 15:36:33 2001 UTC vs.
Revision 1.14 by tdb, Wed Mar 13 12:44:55 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.util;
2 > package uk.org.iscream.cms.server.util;
3  
4   //---IMPORTS---
5   import java.io.*;
6 < import java.util.ArrayList;
7 <
6 > import java.util.LinkedList;
7 > import java.util.Iterator;
8   import org.xml.sax.*;
9 < import javax.xml.parsers.SAXParserFactory;
10 < import javax.xml.parsers.ParserConfigurationException;
11 < import javax.xml.parsers.SAXParser;
9 > import org.xml.sax.helpers.*;
10 > import javax.xml.parsers.*;
11  
12   /**
13   * XMLStringParser - Used to assist in creating XMLPacket objects.
# Line 16 | Line 15 | import javax.xml.parsers.SAXParser;
15   * @author  $Author$
16   * @version $Id$
17   */
18 < public class XMLStringParser extends HandlerBase {
18 > public class XMLStringParser extends DefaultHandler {
19  
20   //---FINAL ATTRIBUTES---
21  
# Line 33 | Line 32 | public class XMLStringParser extends HandlerBase {
32       * No-args constructor.  Generally not used.
33       */
34      public XMLStringParser () {
35 <        this.packet = new XMLPacket();
35 >        _packet = new XMLPacket();
36      }
37  
38      /**
39       * Constructor for accepting a reference to an XMLPacket
40       */
41      public XMLStringParser (XMLPacket packet) {
42 <        this.packet = packet;
42 >        _packet = packet;
43      }
44  
45   //---PUBLIC METHODS---
46  
48    /**
49     * Accessor to the XMLPacket.
50     */
51    public XMLPacket getXMLPacket() {
52        return packet;
53    }
54
47      //===========================================================
48      // SAX DocumentHandler methods
49      //===========================================================
50  
51 <    public void startDocument () throws SAXException {
52 <        //System.out.println("XMLPacketParser - Starting parse process...");
61 <    }
62 <
63 <    public void endDocument () throws SAXException {
64 <        //System.out.println("XMLPacketParser - I just finished parsing an XML String.");
65 <    }
66 <
67 <    /** Add each tag's attribute to the XMLPacket.
51 >    /**
52 >     * Add each tag's attribute to the XMLPacket.
53       * Note that all attributes within an opening tag are
54       * stored as "someroot.sometag.attributes.attribute_name"
55       * E.g. If <packet> is the root node, then:
# Line 73 | Line 58 | public class XMLStringParser extends HandlerBase {
58       *     "packet.attributes.machine_name"
59       * within the XMLPacket.
60       */
61 <    public void startElement (String name, AttributeList attrs) throws SAXException {
62 <        indentLevel++;
63 <        tagList.add(name);
64 <        if (attrs != null) {
65 <            for (int i = 0; i < attrs.getLength (); i++) {
81 <                packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
61 >    public void startElement (String uri, String name, String qName, Attributes atts) {
62 >        _tagList.addLast(qName);
63 >        if (atts != null) {
64 >            for (int i = 0; i < atts.getLength (); i++) {
65 >                _packet.addParam(getPath()+".attributes."+atts.getQName(i), atts.getValue(i));
66              }
67          }
68      }
69 <
69 >    
70      /**
71       * When an XML element is finished with, we must remove
72       * the tag name from the tagList and decrement the indent
73       * level.
74       */
75 <    public void endElement (String name) throws SAXException {
76 <        tagList.remove(tagList.size()-1);    
93 <        indentLevel--;
75 >    public void endElement (String uri, String name, String qName) {
76 >            _tagList.removeLast();    
77      }
78 <
78 >    
79      /**
80       * Any text falling within a pair of terminal tags must
81       * be added to the XMLPacket.  Trim leading and trailing
# Line 101 | Line 84 | public class XMLStringParser extends HandlerBase {
84       */
85      public void characters (char[] buf, int offset, int len) throws SAXException {
86          String s = new String(buf, offset, len);
87 <        packet.addParam(getPath(), s);
87 >        _packet.addParam(getPath(), s);
88      }
89  
90      /**
91       * Overrides the {@link java.lang.Object#toString() Object.toString()}
92       * method to provide clean logging (every class should have this).
93       *
94 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
94 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
95       * to format the toString()
96       *
97       * @return the name of this class and its CVS revision
# Line 131 | Line 114 | public class XMLStringParser extends HandlerBase {
114       * in the XMLPacket.
115       */
116      private String getPath () {
117 <        String path = (String)tagList.get(0);
118 <        if (tagList.size() > 0) {
119 <            for (int i = 1 ; i < tagList.size() ; i++) {
120 <                path = path + "." + (String)tagList.get(i);
117 >        String path = "";
118 >        if (_tagList.size() > 0) {
119 >            Iterator i = _tagList.iterator();
120 >            path = (String) i.next();
121 >            while(i.hasNext()) {
122 >                path = path + "." + (String) i.next();
123              }
124          }
125          return path;
# Line 142 | Line 127 | public class XMLStringParser extends HandlerBase {
127  
128   //---ACCESSOR/MUTATOR METHODS---
129  
145 //---ATTRIBUTES---
146
130      /**
131 <     * To keep track of our identation level
132 <     */
133 <    private int indentLevel = 0;
131 >     * Accessor to the XMLPacket.
132 >     */
133 >    public XMLPacket getXMLPacket() {
134 >        return _packet;
135 >    }
136 >
137 > //---ATTRIBUTES---
138      
139      /**
140 <     * An ArrayList of tags
140 >     * A LinkedList of tags
141       */
142 <    private ArrayList tagList = new ArrayList();
142 >    private LinkedList _tagList = new LinkedList();
143      
144      /**
145       * A reference to the XMLPacket we are making
146       */
147 <    private XMLPacket packet;
147 >    private XMLPacket _packet;
148      
149      /**
150       * This is the friendly identifier of the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines