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