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$"; |
28 |
– |
|
29 |
– |
/** |
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(); |
23 |
|
|
24 |
|
//---STATIC METHODS--- |
25 |
|
|
26 |
|
//---CONSTRUCTORS--- |
27 |
|
|
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 |
– |
|
28 |
|
//---PUBLIC METHODS--- |
29 |
|
|
30 |
|
/** |
31 |
|
* Method to create an XML packet from the data this |
32 |
|
* class was constructed with. |
33 |
|
* |
34 |
+ |
* @param xml the XML String to parse |
35 |
|
* @return an XMLPacket representing the XML String given |
36 |
|
* @throws InvalidXMLException if the XML cannot be parsed |
37 |
|
*/ |
38 |
< |
public XMLPacket createXMLPacket() throws InvalidXMLException { |
38 |
> |
public XMLPacket createXMLPacket(String xml) throws InvalidXMLException { |
39 |
|
|
40 |
|
// Create the XMLPacket to store values in. |
41 |
|
XMLPacket packet = new XMLPacket(); |
42 |
|
|
43 |
|
try { |
44 |
|
// Parse the input |
45 |
< |
InputSource inputSource = new InputSource(new StringReader(_xml)); |
46 |
< |
|
67 |
< |
if (saxParser == null) { |
68 |
< |
saxParser = factory.newSAXParser(); |
69 |
< |
} |
70 |
< |
|
71 |
< |
saxParser.parse(inputSource, new XMLStringParser(packet)); |
45 |
> |
InputSource inputSource = new InputSource(new StringReader(xml)); |
46 |
> |
_factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); |
47 |
|
} |
48 |
|
catch (Exception e) { |
49 |
|
// couldn't parse the XML for some reason |
50 |
< |
throw new InvalidXMLException("Could not parse the XML: "+_xml); |
50 |
> |
throw new InvalidXMLException("Could not parse the XML: "+xml); |
51 |
|
} |
52 |
+ |
|
53 |
|
// parsed successfully, return the packet |
54 |
|
return packet; |
55 |
|
} |
75 |
|
//---ACCESSOR/MUTATOR METHODS--- |
76 |
|
|
77 |
|
//---ATTRIBUTES--- |
102 |
– |
|
103 |
– |
/** |
104 |
– |
* Holds the xml |
105 |
– |
*/ |
106 |
– |
String _xml; |
78 |
|
|
79 |
|
/** |
80 |
|
* This is the friendly identifier of the |
88 |
|
private String _name = null; |
89 |
|
|
90 |
|
//---STATIC ATTRIBUTES--- |
91 |
+ |
|
92 |
+ |
/** |
93 |
+ |
* A static reference to the system saxParser factory |
94 |
+ |
*/ |
95 |
+ |
private static SAXParserFactory _factory = SAXParserFactory.newInstance(); |
96 |
|
|
97 |
|
} |