1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.util; |
2 |
> |
package uk.org.iscream.util; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import java.io.*; |
6 |
– |
import java.util.ArrayList; |
7 |
– |
|
6 |
|
import org.xml.sax.*; |
7 |
< |
import org.xml.sax.helpers.*; |
10 |
< |
import javax.xml.parsers.SAXParserFactory; |
11 |
< |
import javax.xml.parsers.ParserConfigurationException; |
12 |
< |
import javax.xml.parsers.SAXParser; |
7 |
> |
import javax.xml.parsers.*; |
8 |
|
|
9 |
|
/** |
10 |
|
* XMLPacketMaker - Creates an XMLPacket object. |
20 |
|
* The current CVS revision of this class |
21 |
|
*/ |
22 |
|
public final String REVISION = "$Revision$"; |
23 |
< |
|
24 |
< |
/** |
25 |
< |
* A static reference to the system saxParser |
26 |
< |
* Use the default (non-validating) parser |
27 |
< |
*/ |
33 |
< |
private static SAXParser saxParser = null; |
34 |
< |
private static SAXParserFactory factory = SAXParserFactory.newInstance(); |
23 |
> |
|
24 |
> |
/** |
25 |
> |
* A reference to the system saxParser factory |
26 |
> |
*/ |
27 |
> |
private final SAXParserFactory _factory = SAXParserFactory.newInstance(); |
28 |
|
|
29 |
|
//---STATIC METHODS--- |
30 |
|
|
31 |
|
//---CONSTRUCTORS--- |
32 |
|
|
40 |
– |
/** |
41 |
– |
* Constructor for accepting XML input. |
42 |
– |
* |
43 |
– |
* @param xml A String of XML to process. |
44 |
– |
*/ |
45 |
– |
public XMLPacketMaker (String xml) { |
46 |
– |
_xml = xml; |
47 |
– |
} |
48 |
– |
|
33 |
|
//---PUBLIC METHODS--- |
34 |
|
|
35 |
|
/** |
36 |
|
* Method to create an XML packet from the data this |
37 |
|
* class was constructed with. |
38 |
|
* |
39 |
+ |
* @param xml the XML String to parse |
40 |
|
* @return an XMLPacket representing the XML String given |
41 |
|
* @throws InvalidXMLException if the XML cannot be parsed |
42 |
|
*/ |
43 |
< |
public XMLPacket createXMLPacket() throws InvalidXMLException { |
43 |
> |
public XMLPacket createXMLPacket(String xml) throws InvalidXMLException { |
44 |
|
|
45 |
|
// Create the XMLPacket to store values in. |
46 |
|
XMLPacket packet = new XMLPacket(); |
47 |
|
|
48 |
|
try { |
49 |
|
// Parse the input |
50 |
< |
InputSource inputSource = new InputSource(new StringReader(_xml)); |
51 |
< |
|
67 |
< |
if (saxParser == null) { |
68 |
< |
saxParser = factory.newSAXParser(); |
69 |
< |
} |
70 |
< |
|
71 |
< |
saxParser.parse(inputSource, new XMLStringParser(packet)); |
50 |
> |
InputSource inputSource = new InputSource(new StringReader(xml)); |
51 |
> |
_factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); |
52 |
|
} |
53 |
|
catch (Exception e) { |
54 |
|
// couldn't parse the XML for some reason |
55 |
< |
throw new InvalidXMLException("Could not parse the XML: "+_xml); |
55 |
> |
throw new InvalidXMLException("Could not parse the XML: "+xml); |
56 |
|
} |
57 |
+ |
|
58 |
|
// parsed successfully, return the packet |
59 |
|
return packet; |
60 |
|
} |
63 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
64 |
|
* method to provide clean logging (every class should have this). |
65 |
|
* |
66 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
66 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
67 |
|
* to format the toString() |
68 |
|
* |
69 |
|
* @return the name of this class and its CVS revision |
80 |
|
//---ACCESSOR/MUTATOR METHODS--- |
81 |
|
|
82 |
|
//---ATTRIBUTES--- |
102 |
– |
|
103 |
– |
/** |
104 |
– |
* Holds the xml |
105 |
– |
*/ |
106 |
– |
String _xml; |
83 |
|
|
84 |
|
/** |
85 |
|
* This is the friendly identifier of the |