1 |
+ |
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.org.iscream.util; |
3 |
+ |
|
4 |
+ |
//---IMPORTS--- |
5 |
|
import java.io.*; |
6 |
|
import java.util.ArrayList; |
3 |
– |
|
7 |
|
import org.xml.sax.*; |
8 |
+ |
import org.xml.sax.helpers.*; |
9 |
+ |
import javax.xml.parsers.*; |
10 |
|
|
11 |
< |
import javax.xml.parsers.SAXParserFactory; |
12 |
< |
import javax.xml.parsers.ParserConfigurationException; |
13 |
< |
import javax.xml.parsers.SAXParser; |
11 |
> |
/** |
12 |
> |
* XMLStringParser - Used to assist in creating XMLPacket objects. |
13 |
> |
* |
14 |
> |
* @author $Author$ |
15 |
> |
* @version $Id$ |
16 |
> |
*/ |
17 |
> |
public class XMLStringParser extends DefaultHandler { |
18 |
|
|
19 |
< |
// Paul Mutton, pjm2@ukc.ac.uk |
19 |
> |
//---FINAL ATTRIBUTES--- |
20 |
|
|
21 |
< |
// XMLStringParser - Used to assist in creating XMLPacket objects. |
22 |
< |
public class XMLStringParser extends HandlerBase { |
21 |
> |
/** |
22 |
> |
* The current CVS revision of this class |
23 |
> |
*/ |
24 |
> |
public final String REVISION = "$Revision$"; |
25 |
> |
|
26 |
> |
//---STATIC METHODS--- |
27 |
|
|
28 |
< |
// No-args constructor. Generally not used. |
28 |
> |
//---CONSTRUCTORS--- |
29 |
> |
|
30 |
> |
/** |
31 |
> |
* No-args constructor. Generally not used. |
32 |
> |
*/ |
33 |
|
public XMLStringParser () { |
34 |
< |
this.packet = new XMLPacket(); |
34 |
> |
_packet = new XMLPacket(); |
35 |
|
} |
36 |
|
|
37 |
< |
// Constructor for accepting a reference to an XMLPacket |
37 |
> |
/** |
38 |
> |
* Constructor for accepting a reference to an XMLPacket |
39 |
> |
*/ |
40 |
|
public XMLStringParser (XMLPacket packet) { |
41 |
< |
this.packet = packet; |
41 |
> |
_packet = packet; |
42 |
|
} |
43 |
|
|
44 |
< |
// Accessor to the XMLPacket. |
26 |
< |
public XMLPacket getXMLPacket() { |
27 |
< |
return packet; |
28 |
< |
} |
44 |
> |
//---PUBLIC METHODS--- |
45 |
|
|
30 |
– |
private int indentLevel = 0; |
31 |
– |
private ArrayList tagList = new ArrayList(); |
32 |
– |
private XMLPacket packet; |
33 |
– |
|
46 |
|
//=========================================================== |
47 |
|
// SAX DocumentHandler methods |
48 |
|
//=========================================================== |
49 |
|
|
50 |
< |
public void startDocument () throws SAXException { |
51 |
< |
System.out.println("XMLPacketParser - Starting parse process..."); |
52 |
< |
} |
53 |
< |
|
54 |
< |
public void endDocument () throws SAXException { |
55 |
< |
System.out.println("XMLPacketParser - I just finished parsing an XML String."); |
56 |
< |
} |
57 |
< |
|
58 |
< |
// Add each tag's attribute to the XMLPacket. |
59 |
< |
// Note that all attributes within an opening tag are |
60 |
< |
// stored as "someroot.sometag.attributes.attribute_name" |
61 |
< |
// E.g. If <packet> is the root node, then: |
62 |
< |
// <packet machine_name="raptor"> |
63 |
< |
// is stored as: |
64 |
< |
// "packet.attributes.machine_name" |
65 |
< |
// 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)); |
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 |
< |
// When an XML element is finished with, we must remove |
71 |
< |
// the tag name from the tagList and decrement the indent |
72 |
< |
// level. |
73 |
< |
public void endElement (String name) throws SAXException { |
74 |
< |
tagList.remove(tagList.size()-1); |
75 |
< |
indentLevel--; |
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 |
< |
// 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. |
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 |
< |
if (!s.trim().equals("")) { |
79 |
< |
packet.addParam(getPath(), s); |
80 |
< |
} |
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. |
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); |
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 |
< |
} |
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 |
> |
} |