1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.util; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.io.*; |
6 |
import java.util.ArrayList; |
7 |
import org.xml.sax.*; |
8 |
import org.xml.sax.helpers.*; |
9 |
import javax.xml.parsers.*; |
10 |
|
11 |
/** |
12 |
* XMLStringParser - Used to assist in creating XMLPacket objects. |
13 |
* |
14 |
* @author $Author: tdb1 $ |
15 |
* @version $Id: XMLStringParser.java,v 1.10 2001/03/10 04:03:52 tdb1 Exp $ |
16 |
*/ |
17 |
public class XMLStringParser extends DefaultHandler { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision: 1.10 $"; |
25 |
|
26 |
//---STATIC METHODS--- |
27 |
|
28 |
//---CONSTRUCTORS--- |
29 |
|
30 |
/** |
31 |
* No-args constructor. Generally not used. |
32 |
*/ |
33 |
public XMLStringParser () { |
34 |
_packet = new XMLPacket(); |
35 |
} |
36 |
|
37 |
/** |
38 |
* Constructor for accepting a reference to an XMLPacket |
39 |
*/ |
40 |
public XMLStringParser (XMLPacket packet) { |
41 |
_packet = packet; |
42 |
} |
43 |
|
44 |
//---PUBLIC METHODS--- |
45 |
|
46 |
//=========================================================== |
47 |
// SAX DocumentHandler methods |
48 |
//=========================================================== |
49 |
|
50 |
/** |
51 |
* Add each tag's attribute to the XMLPacket. |
52 |
* 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 |
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 |
} |
67 |
} |
68 |
} |
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 uri, String name, String qName) { |
76 |
_tagList.remove(_tagList.size() - 1); |
77 |
_indentLevel--; |
78 |
} |
79 |
|
80 |
/** |
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 |
_packet.addParam(getPath(), s); |
89 |
} |
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.org.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 |
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 |
} |
123 |
} |
124 |
return path; |
125 |
} |
126 |
|
127 |
//---ACCESSOR/MUTATOR METHODS--- |
128 |
|
129 |
/** |
130 |
* Accessor to the XMLPacket. |
131 |
*/ |
132 |
public XMLPacket getXMLPacket() { |
133 |
return _packet; |
134 |
} |
135 |
|
136 |
//---ATTRIBUTES--- |
137 |
|
138 |
/** |
139 |
* To keep track of our identation level |
140 |
*/ |
141 |
private int _indentLevel = 0; |
142 |
|
143 |
/** |
144 |
* An ArrayList of tags |
145 |
*/ |
146 |
private ArrayList _tagList = new ArrayList(); |
147 |
|
148 |
/** |
149 |
* A reference to the XMLPacket we are making |
150 |
*/ |
151 |
private XMLPacket _packet; |
152 |
|
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 |
} |