1 |
+ |
//---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.*; |
5 |
– |
|
9 |
|
import javax.xml.parsers.SAXParserFactory; |
10 |
|
import javax.xml.parsers.ParserConfigurationException; |
11 |
|
import javax.xml.parsers.SAXParser; |
12 |
|
|
13 |
< |
// Paul Mutton, pjm2@ukc.ac.uk |
14 |
< |
|
15 |
< |
// XMLStringParser - Used to assist in creating XMLPacket objects. |
13 |
> |
/** |
14 |
> |
* XMLStringParser - Used to assist in creating XMLPacket objects. |
15 |
> |
* |
16 |
> |
* @author $Author$ |
17 |
> |
* @version $Id$ |
18 |
> |
*/ |
19 |
|
public class XMLStringParser extends HandlerBase { |
20 |
|
|
21 |
< |
// No-args constructor. Generally not used. |
21 |
> |
//---FINAL ATTRIBUTES--- |
22 |
> |
|
23 |
> |
/** |
24 |
> |
* The current CVS revision of this class |
25 |
> |
*/ |
26 |
> |
public final String REVISION = "$Revision$"; |
27 |
> |
|
28 |
> |
//---STATIC METHODS--- |
29 |
> |
|
30 |
> |
//---CONSTRUCTORS--- |
31 |
> |
|
32 |
> |
/** |
33 |
> |
* No-args constructor. Generally not used. |
34 |
> |
*/ |
35 |
|
public XMLStringParser () { |
36 |
|
this.packet = new XMLPacket(); |
37 |
|
} |
38 |
|
|
39 |
< |
// Constructor for accepting a reference to an XMLPacket |
39 |
> |
/** |
40 |
> |
* Constructor for accepting a reference to an XMLPacket |
41 |
> |
*/ |
42 |
|
public XMLStringParser (XMLPacket packet) { |
43 |
|
this.packet = packet; |
44 |
|
} |
45 |
|
|
46 |
< |
// Accessor to the XMLPacket. |
46 |
> |
//---PUBLIC METHODS--- |
47 |
> |
|
48 |
> |
/** |
49 |
> |
* Accessor to the XMLPacket. |
50 |
> |
*/ |
51 |
|
public XMLPacket getXMLPacket() { |
52 |
|
return packet; |
53 |
|
} |
54 |
|
|
30 |
– |
private int indentLevel = 0; |
31 |
– |
private ArrayList tagList = new ArrayList(); |
32 |
– |
private XMLPacket packet; |
33 |
– |
|
55 |
|
//=========================================================== |
56 |
|
// SAX DocumentHandler methods |
57 |
|
//=========================================================== |
64 |
|
//System.out.println("XMLPacketParser - I just finished parsing an XML String."); |
65 |
|
} |
66 |
|
|
67 |
< |
// Add each tag's attribute to the XMLPacket. |
68 |
< |
// Note that all attributes within an opening tag are |
69 |
< |
// stored as "someroot.sometag.attributes.attribute_name" |
70 |
< |
// E.g. If <packet> is the root node, then: |
71 |
< |
// <packet machine_name="raptor"> |
72 |
< |
// is stored as: |
73 |
< |
// "packet.attributes.machine_name" |
74 |
< |
// within the XMLPacket. |
67 |
> |
/** Add each tag's attribute to the XMLPacket. |
68 |
> |
* Note that all attributes within an opening tag are |
69 |
> |
* stored as "someroot.sometag.attributes.attribute_name" |
70 |
> |
* E.g. If <packet> is the root node, then: |
71 |
> |
* <packet machine_name="raptor"> |
72 |
> |
* is stored as: |
73 |
> |
* "packet.attributes.machine_name" |
74 |
> |
* within the XMLPacket. |
75 |
> |
*/ |
76 |
|
public void startElement (String name, AttributeList attrs) throws SAXException { |
77 |
|
indentLevel++; |
78 |
|
tagList.add(name); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
< |
// When an XML element is finished with, we must remove |
87 |
< |
// the tag name from the tagList and decrement the indent |
88 |
< |
// level. |
86 |
> |
/** |
87 |
> |
* When an XML element is finished with, we must remove |
88 |
> |
* the tag name from the tagList and decrement the indent |
89 |
> |
* level. |
90 |
> |
*/ |
91 |
|
public void endElement (String name) throws SAXException { |
92 |
|
tagList.remove(tagList.size()-1); |
93 |
|
indentLevel--; |
94 |
|
} |
95 |
|
|
96 |
< |
// Any text falling within a pair of terminal tags must |
97 |
< |
// be added to the XMLPacket. Trim leading and trailing |
98 |
< |
// spaces and do not bother to add if there is no data |
99 |
< |
// specified within the tags. |
96 |
> |
/** |
97 |
> |
* Any text falling within a pair of terminal tags must |
98 |
> |
* be added to the XMLPacket. Trim leading and trailing |
99 |
> |
* spaces and do not bother to add if there is no data |
100 |
> |
* specified within the tags. |
101 |
> |
*/ |
102 |
|
public void characters (char[] buf, int offset, int len) throws SAXException { |
103 |
|
String s = new String(buf, offset, len); |
104 |
|
if (!s.trim().equals("")) { |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
+ |
/** |
110 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
111 |
+ |
* method to provide clean logging (every class should have this). |
112 |
+ |
* |
113 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
114 |
+ |
* to format the toString() |
115 |
+ |
* |
116 |
+ |
* @return the name of this class and its CVS revision |
117 |
+ |
*/ |
118 |
+ |
public String toString() { |
119 |
+ |
return FormatName.getName( |
120 |
+ |
_name, |
121 |
+ |
getClass().getName(), |
122 |
+ |
REVISION); |
123 |
+ |
} |
124 |
|
|
125 |
+ |
//---PRIVATE METHODS--- |
126 |
+ |
|
127 |
|
//=========================================================== |
128 |
|
// Helpers ... |
129 |
|
//=========================================================== |
130 |
|
|
131 |
< |
|
132 |
< |
// Return the heirarchical string to be used as a key value |
133 |
< |
// in the XMLPacket. |
131 |
> |
/** |
132 |
> |
* Return the heirarchical string to be used as a key value |
133 |
> |
* in the XMLPacket. |
134 |
> |
*/ |
135 |
|
private String getPath () { |
136 |
|
String path = (String)tagList.get(0); |
137 |
|
if (tagList.size() > 0) { |
141 |
|
} |
142 |
|
return path; |
143 |
|
} |
144 |
+ |
|
145 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
146 |
+ |
|
147 |
+ |
//---ATTRIBUTES--- |
148 |
+ |
|
149 |
+ |
private int indentLevel = 0; |
150 |
+ |
private ArrayList tagList = new ArrayList(); |
151 |
+ |
private XMLPacket packet; |
152 |
|
|
153 |
< |
} |
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 |
> |
} |