1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.xml; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import java.io.*; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import org.xml.sax.*; |
9 |
import javax.xml.parsers.SAXParserFactory; |
10 |
import javax.xml.parsers.ParserConfigurationException; |
11 |
import javax.xml.parsers.SAXParser; |
12 |
|
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 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: 1.1 $"; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
// No-args constructor. Generally not used. |
33 |
public XMLStringParser () { |
34 |
this.packet = new XMLPacket(); |
35 |
} |
36 |
|
37 |
// Constructor for accepting a reference to an XMLPacket |
38 |
public XMLStringParser (XMLPacket packet) { |
39 |
this.packet = packet; |
40 |
} |
41 |
|
42 |
//---PUBLIC METHODS--- |
43 |
|
44 |
// Accessor to the XMLPacket. |
45 |
public XMLPacket getXMLPacket() { |
46 |
return packet; |
47 |
} |
48 |
|
49 |
//=========================================================== |
50 |
// SAX DocumentHandler methods |
51 |
//=========================================================== |
52 |
|
53 |
public void startDocument () throws SAXException { |
54 |
//System.out.println("XMLPacketParser - Starting parse process..."); |
55 |
} |
56 |
|
57 |
public void endDocument () throws SAXException { |
58 |
//System.out.println("XMLPacketParser - I just finished parsing an XML String."); |
59 |
} |
60 |
|
61 |
// Add each tag's attribute to the XMLPacket. |
62 |
// Note that all attributes within an opening tag are |
63 |
// stored as "someroot.sometag.attributes.attribute_name" |
64 |
// E.g. If <packet> is the root node, then: |
65 |
// <packet machine_name="raptor"> |
66 |
// is stored as: |
67 |
// "packet.attributes.machine_name" |
68 |
// within the XMLPacket. |
69 |
public void startElement (String name, AttributeList attrs) throws SAXException { |
70 |
indentLevel++; |
71 |
tagList.add(name); |
72 |
if (attrs != null) { |
73 |
for (int i = 0; i < attrs.getLength (); i++) { |
74 |
packet.addParam(getPath()+".attributes."+attrs.getName(i), attrs.getValue(i)); |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
// When an XML element is finished with, we must remove |
80 |
// the tag name from the tagList and decrement the indent |
81 |
// level. |
82 |
public void endElement (String name) throws SAXException { |
83 |
tagList.remove(tagList.size()-1); |
84 |
indentLevel--; |
85 |
} |
86 |
|
87 |
// Any text falling within a pair of terminal tags must |
88 |
// be added to the XMLPacket. Trim leading and trailing |
89 |
// spaces and do not bother to add if there is no data |
90 |
// specified within the tags. |
91 |
public void characters (char[] buf, int offset, int len) throws SAXException { |
92 |
String s = new String(buf, offset, len); |
93 |
if (!s.trim().equals("")) { |
94 |
packet.addParam(getPath(), s); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
100 |
* method to provide clean logging (every class should have this). |
101 |
* |
102 |
* @return the name of this class and its CVS revision |
103 |
*/ |
104 |
public String toString() { |
105 |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
106 |
} |
107 |
|
108 |
//---PRIVATE METHODS--- |
109 |
|
110 |
//=========================================================== |
111 |
// Helpers ... |
112 |
//=========================================================== |
113 |
|
114 |
|
115 |
// Return the heirarchical string to be used as a key value |
116 |
// in the XMLPacket. |
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 |
//---ATTRIBUTES--- |
130 |
|
131 |
private int indentLevel = 0; |
132 |
private ArrayList tagList = new ArrayList(); |
133 |
private XMLPacket packet; |
134 |
|
135 |
//---STATIC ATTRIBUTES--- |
136 |
|
137 |
} |