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