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
Revision: 1.9
Committed: Sat Mar 10 02:03:55 2001 UTC (23 years, 1 month ago) by tdb
Branch: MAIN
Changes since 1.8: +26 -32 lines
Log Message:
Changed to use the newer parsing libraries.

File Contents

# User Rev Content
1 ajm 1.5 //---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 tdb 1.8 import org.xml.sax.helpers.*;
10 ajm 1.5 import javax.xml.parsers.SAXParserFactory;
11     import javax.xml.parsers.ParserConfigurationException;
12     import javax.xml.parsers.SAXParser;
13    
14     /**
15     * XMLStringParser - Used to assist in creating XMLPacket objects.
16     *
17 tdb 1.9 * @author $Author: tdb1 $
18     * @version $Id: XMLStringParser.java,v 1.8 2001/03/10 00:49:39 tdb1 Exp $
19 ajm 1.5 */
20 tdb 1.8 public class XMLStringParser extends DefaultHandler {
21 ajm 1.5
22     //---FINAL ATTRIBUTES---
23    
24     /**
25     * The current CVS revision of this class
26     */
27 tdb 1.9 public final String REVISION = "$Revision: 1.8 $";
28 ajm 1.5
29     //---STATIC METHODS---
30    
31     //---CONSTRUCTORS---
32    
33     /**
34     * No-args constructor. Generally not used.
35     */
36     public XMLStringParser () {
37 tdb 1.9 _packet = new XMLPacket();
38 ajm 1.5 }
39    
40     /**
41     * Constructor for accepting a reference to an XMLPacket
42     */
43     public XMLStringParser (XMLPacket packet) {
44 tdb 1.9 _packet = packet;
45 ajm 1.5 }
46    
47     //---PUBLIC METHODS---
48    
49     /**
50     * Accessor to the XMLPacket.
51     */
52     public XMLPacket getXMLPacket() {
53 tdb 1.9 return _packet;
54 ajm 1.5 }
55    
56     //===========================================================
57     // SAX DocumentHandler methods
58     //===========================================================
59    
60 tdb 1.9 /**
61     * Add each tag's attribute to the XMLPacket.
62 ajm 1.5 * 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 tdb 1.9 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 ajm 1.5 }
77     }
78     }
79    
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 tdb 1.9 public void endElement (String uri, String name, String qName) {
86     _tagList.remove(_tagList.size() - 1);
87     _indentLevel--;
88 ajm 1.5 }
89    
90 tdb 1.9
91 ajm 1.5 /**
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 tdb 1.9 _packet.addParam(getPath(), s);
100 ajm 1.5 }
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.
127     */
128     private String getPath () {
129 tdb 1.9 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 ajm 1.5 }
134     }
135     return path;
136     }
137    
138     //---ACCESSOR/MUTATOR METHODS---
139    
140     //---ATTRIBUTES---
141    
142 tdb 1.6 /**
143     * To keep track of our identation level
144     */
145 tdb 1.9 private int _indentLevel = 0;
146 tdb 1.6
147     /**
148     * An ArrayList of tags
149     */
150 tdb 1.9 private ArrayList _tagList = new ArrayList();
151 tdb 1.6
152     /**
153     * A reference to the XMLPacket we are making
154     */
155 tdb 1.9 private XMLPacket _packet;
156 ajm 1.5
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     }