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