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