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.1 by pjm2, Wed Nov 22 08:40:53 2000 UTC vs.
Revision 1.9 by tdb, Sat Mar 10 02:03:55 2001 UTC

# Line 1 | Line 1
1 + //---PACKAGE DECLARATION---
2 + package uk.ac.ukc.iscream.util;
3 +
4 + //---IMPORTS---
5   import java.io.*;
6   import java.util.ArrayList;
7  
8   import org.xml.sax.*;
9 <
9 > import org.xml.sax.helpers.*;
10   import javax.xml.parsers.SAXParserFactory;
11   import javax.xml.parsers.ParserConfigurationException;
12   import javax.xml.parsers.SAXParser;
13  
14 < // Paul Mutton, pjm2@ukc.ac.uk
14 > /**
15 > * XMLStringParser - Used to assist in creating XMLPacket objects.
16 > *
17 > * @author  $Author$
18 > * @version $Id$
19 > */
20 > public class XMLStringParser extends DefaultHandler {
21  
22 < // XMLStringParser - Used to assist in creating XMLPacket objects.
13 < public class XMLStringParser extends HandlerBase {
22 > //---FINAL ATTRIBUTES---
23  
24 <    // No-args constructor.  Generally not used.
24 >    /**
25 >     * The current CVS revision of this class
26 >     */
27 >    public final String REVISION = "$Revision$";
28 >    
29 > //---STATIC METHODS---
30 >
31 > //---CONSTRUCTORS---
32 >
33 >    /**
34 >     * No-args constructor.  Generally not used.
35 >     */
36      public XMLStringParser () {
37 <        this.packet = new XMLPacket();
37 >        _packet = new XMLPacket();
38      }
39  
40 <    // Constructor for accepting a reference to an XMLPacket
40 >    /**
41 >     * Constructor for accepting a reference to an XMLPacket
42 >     */
43      public XMLStringParser (XMLPacket packet) {
44 <        this.packet = packet;
44 >        _packet = packet;
45      }
46  
47 <    // Accessor to the XMLPacket.
47 > //---PUBLIC METHODS---
48 >
49 >    /**
50 >     * Accessor to the XMLPacket.
51 >     */
52      public XMLPacket getXMLPacket() {
53 <        return packet;
53 >        return _packet;
54      }
55  
30    private int indentLevel = 0;
31    private ArrayList tagList = new ArrayList();
32    private XMLPacket packet;
33
56      //===========================================================
57      // SAX DocumentHandler methods
58      //===========================================================
59  
60 <    public void startDocument () throws SAXException {
61 <        System.out.println("XMLPacketParser - Starting parse process...");
62 <    }
63 <
64 <    public void endDocument () throws SAXException {
65 <        System.out.println("XMLPacketParser - I just finished parsing an XML String.");
66 <    }
67 <
68 <    // Add each tag's attribute to the XMLPacket.
69 <    // Note that all attributes within an opening tag are
70 <    // stored as "someroot.sometag.attributes.attribute_name"
71 <    // E.g. If <packet> is the root node, then:
72 <    //     <packet machine_name="raptor">
73 <    // is stored as:
74 <    //     "packet.attributes.machine_name"
75 <    // within the XMLPacket.
54 <    public void startElement (String name, AttributeList attrs) throws SAXException {
55 <        indentLevel++;
56 <        tagList.add(name);
57 <        if (attrs != null) {
58 <            for (int i = 0; i < attrs.getLength (); i++) {
59 <                packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i));
60 >    /**
61 >     * Add each tag's attribute to the XMLPacket.
62 >     * Note that all attributes within an opening tag are
63 >     * stored as "someroot.sometag.attributes.attribute_name"
64 >     * E.g. If <packet> is the root node, then:
65 >     *     <packet machine_name="raptor">
66 >     * is stored as:
67 >     *     "packet.attributes.machine_name"
68 >     * within the XMLPacket.
69 >     */
70 >    public void startElement (String uri, String name, String qName, Attributes atts) {
71 >            _indentLevel++;
72 >        _tagList.add(name);
73 >        if (atts != null) {
74 >            for (int i = 0; i < atts.getLength (); i++) {
75 >                _packet.addParam(getPath()+".attributes."+atts.getLocalName(i), atts.getValue(i));
76              }
77          }
78      }
79  
80 <    // When an XML element is finished with, we must remove
81 <    // the tag name from the tagList and decrement the indent
82 <    // level.
83 <    public void endElement (String name) throws SAXException {
84 <        tagList.remove(tagList.size()-1);    
85 <        indentLevel--;
80 >    /**
81 >     * When an XML element is finished with, we must remove
82 >     * the tag name from the tagList and decrement the indent
83 >     * level.
84 >     */
85 >    public void endElement (String uri, String name, String qName) {
86 >            _tagList.remove(_tagList.size() - 1);    
87 >        _indentLevel--;
88      }
89  
90 <    // Any text falling within a pair of terminal tags must
91 <    // be added to the XMLPacket.  Trim leading and trailing
92 <    // spaces and do not bother to add if there is no data
93 <    // specified within the tags.
90 >
91 >    /**
92 >     * Any text falling within a pair of terminal tags must
93 >     * be added to the XMLPacket.  Trim leading and trailing
94 >     * spaces and do not bother to add if there is no data
95 >     * specified within the tags.
96 >     */
97      public void characters (char[] buf, int offset, int len) throws SAXException {
98          String s = new String(buf, offset, len);
99 <        if (!s.trim().equals("")) {
79 <            packet.addParam(getPath(), s);
80 <        }
99 >        _packet.addParam(getPath(), s);
100      }
101  
102 +    /**
103 +     * Overrides the {@link java.lang.Object#toString() Object.toString()}
104 +     * method to provide clean logging (every class should have this).
105 +     *
106 +     * This uses the uk.ac.ukc.iscream.util.NameFormat class
107 +     * to format the toString()
108 +     *
109 +     * @return the name of this class and its CVS revision
110 +     */
111 +    public String toString() {
112 +        return FormatName.getName(
113 +            _name,
114 +            getClass().getName(),
115 +            REVISION);
116 +    }
117  
118 + //---PRIVATE METHODS---
119 +
120      //===========================================================
121      // Helpers ...
122      //===========================================================
123      
124 <    
125 <    // Return the heirarchical string to be used as a key value
126 <    // in the XMLPacket.
124 >    /**
125 >     * Return the heirarchical string to be used as a key value
126 >     * in the XMLPacket.
127 >     */
128      private String getPath () {
129 <        String path = (String)tagList.get(0);
130 <        if (tagList.size() > 0) {
131 <            for (int i = 1 ; i < tagList.size() ; i++) {
132 <                path = path + "." + (String)tagList.get(i);
129 >        String path = (String) _tagList.get(0);
130 >        if (_tagList.size() > 0) {
131 >            for (int i = 1 ; i < _tagList.size() ; i++) {
132 >                path = path + "." + (String) _tagList.get(i);
133              }
134          }
135          return path;
136      }
137 +
138 + //---ACCESSOR/MUTATOR METHODS---
139 +
140 + //---ATTRIBUTES---
141 +
142 +    /**
143 +     * To keep track of our identation level
144 +     */
145 +    private int _indentLevel = 0;
146      
147 < }
147 >    /**
148 >     * An ArrayList of tags
149 >     */
150 >    private ArrayList _tagList = new ArrayList();
151 >    
152 >    /**
153 >     * A reference to the XMLPacket we are making
154 >     */
155 >    private XMLPacket _packet;
156 >    
157 >    /**
158 >     * This is the friendly identifier of the
159 >     * component this class is running in.
160 >     * eg, a Filter may be called "filter1",
161 >     * If this class does not have an owning
162 >     * component,  a name from the configuration
163 >     * can be placed here.  This name could also
164 >     * be changed to null for utility classes.
165 >     */
166 >    private String _name = null;
167 >
168 > //---STATIC ATTRIBUTES---
169 >
170 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines