1 |
ajm |
1.7 |
//---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 |
tdb |
1.11 |
import org.xml.sax.helpers.*; |
10 |
ajm |
1.7 |
import javax.xml.parsers.SAXParserFactory; |
11 |
|
|
import javax.xml.parsers.ParserConfigurationException; |
12 |
|
|
import javax.xml.parsers.SAXParser; |
13 |
|
|
|
14 |
|
|
/** |
15 |
|
|
* XMLPacketMaker - Creates an XMLPacket object. |
16 |
|
|
* |
17 |
tdb |
1.9 |
* @author $Author: tdb1 $ |
18 |
tdb |
1.11 |
* @version $Id: XMLPacketMaker.java,v 1.14 2001/03/09 19:14:01 tdb1 Exp $ |
19 |
ajm |
1.7 |
*/ |
20 |
tdb |
1.11 |
public class XMLPacketMaker { |
21 |
ajm |
1.7 |
|
22 |
|
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* The current CVS revision of this class |
26 |
|
|
*/ |
27 |
tdb |
1.11 |
public final String REVISION = "$Revision: 1.14 $"; |
28 |
ajm |
1.7 |
|
29 |
tdb |
1.11 |
/** |
30 |
|
|
* A static reference to the system saxParser |
31 |
|
|
* Use the default (non-validating) parser |
32 |
|
|
*/ |
33 |
|
|
private static SAXParser saxParser = null; |
34 |
|
|
private static SAXParserFactory factory = SAXParserFactory.newInstance(); |
35 |
|
|
|
36 |
ajm |
1.7 |
//---STATIC METHODS--- |
37 |
|
|
|
38 |
|
|
//---CONSTRUCTORS--- |
39 |
|
|
|
40 |
tdb |
1.9 |
/** |
41 |
|
|
* Constructor for accepting XML input. |
42 |
|
|
* |
43 |
|
|
* @param xml A String of XML to process. |
44 |
|
|
*/ |
45 |
ajm |
1.7 |
public XMLPacketMaker (String xml) { |
46 |
|
|
_xml = xml; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
//---PUBLIC METHODS--- |
50 |
|
|
|
51 |
tdb |
1.9 |
/** |
52 |
|
|
* Method to create an XML packet from the data this |
53 |
|
|
* class was constructed with. |
54 |
|
|
* |
55 |
|
|
* @return an XMLPacket representing the XML String given |
56 |
tdb |
1.10 |
* @throws InvalidXMLException if the XML cannot be parsed |
57 |
tdb |
1.9 |
*/ |
58 |
tdb |
1.10 |
public XMLPacket createXMLPacket() throws InvalidXMLException { |
59 |
ajm |
1.7 |
|
60 |
|
|
// Create the XMLPacket to store values in. |
61 |
|
|
XMLPacket packet = new XMLPacket(); |
62 |
tdb |
1.11 |
|
63 |
ajm |
1.7 |
try { |
64 |
|
|
// Parse the input |
65 |
|
|
InputSource inputSource = new InputSource(new StringReader(_xml)); |
66 |
tdb |
1.11 |
|
67 |
|
|
if (saxParser == null) { |
68 |
|
|
saxParser = factory.newSAXParser(); |
69 |
|
|
} |
70 |
|
|
|
71 |
ajm |
1.7 |
saxParser.parse(inputSource, new XMLStringParser(packet)); |
72 |
|
|
} |
73 |
|
|
catch (Exception e) { |
74 |
tdb |
1.10 |
// couldn't parse the XML for some reason |
75 |
|
|
throw new InvalidXMLException("Could not parse the XML: "+_xml); |
76 |
ajm |
1.7 |
} |
77 |
tdb |
1.10 |
// parsed successfully, return the packet |
78 |
ajm |
1.7 |
return packet; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
83 |
|
|
* method to provide clean logging (every class should have this). |
84 |
|
|
* |
85 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
86 |
|
|
* to format the toString() |
87 |
|
|
* |
88 |
|
|
* @return the name of this class and its CVS revision |
89 |
|
|
*/ |
90 |
|
|
public String toString() { |
91 |
|
|
return FormatName.getName( |
92 |
|
|
_name, |
93 |
|
|
getClass().getName(), |
94 |
|
|
REVISION); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
//---PRIVATE METHODS--- |
98 |
|
|
|
99 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
100 |
|
|
|
101 |
|
|
//---ATTRIBUTES--- |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Holds the xml |
105 |
|
|
*/ |
106 |
|
|
String _xml; |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
* This is the friendly identifier of the |
110 |
|
|
* component this class is running in. |
111 |
|
|
* eg, a Filter may be called "filter1", |
112 |
|
|
* If this class does not have an owning |
113 |
|
|
* component, a name from the configuration |
114 |
|
|
* can be placed here. This name could also |
115 |
|
|
* be changed to null for utility classes. |
116 |
|
|
*/ |
117 |
|
|
private String _name = null; |
118 |
|
|
|
119 |
|
|
//---STATIC ATTRIBUTES--- |
120 |
|
|
|
121 |
|
|
} |