1 |
+ |
//---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 |
< |
|
9 |
> |
import org.xml.sax.helpers.*; |
10 |
|
import javax.xml.parsers.SAXParserFactory; |
11 |
|
import javax.xml.parsers.ParserConfigurationException; |
12 |
|
import javax.xml.parsers.SAXParser; |
13 |
|
|
14 |
< |
// Paul Mutton, pjm2@ukc.ac.uk |
14 |
> |
/** |
15 |
> |
* XMLPacketMaker - Creates an XMLPacket object. |
16 |
> |
* |
17 |
> |
* @author $Author$ |
18 |
> |
* @version $Id$ |
19 |
> |
*/ |
20 |
> |
public class XMLPacketMaker extends DefaultHandler { |
21 |
|
|
22 |
< |
// XMLPacketMaker - Creates an XMLPacket object. |
13 |
< |
public class XMLPacketMaker extends HandlerBase { |
22 |
> |
//---FINAL ATTRIBUTES--- |
23 |
|
|
24 |
< |
// No-args constructor. Generally not used. |
25 |
< |
public XMLPacketMaker () { |
26 |
< |
this.xml = "<packet></packet>"; |
27 |
< |
} |
24 |
> |
/** |
25 |
> |
* The current CVS revision of this class |
26 |
> |
*/ |
27 |
> |
public final String REVISION = "$Revision$"; |
28 |
> |
|
29 |
> |
//---STATIC METHODS--- |
30 |
|
|
31 |
< |
// Constructor for accepting XML input. |
31 |
> |
//---CONSTRUCTORS--- |
32 |
> |
|
33 |
> |
/** |
34 |
> |
* Constructor for accepting XML input. |
35 |
> |
* |
36 |
> |
* @param xml A String of XML to process. |
37 |
> |
*/ |
38 |
|
public XMLPacketMaker (String xml) { |
39 |
< |
this.xml = xml; |
39 |
> |
_xml = xml; |
40 |
|
} |
41 |
|
|
42 |
< |
public XMLPacket createXMLPacket() { |
42 |
> |
/** |
43 |
> |
* A static reference to the system saxParser factory |
44 |
> |
*/ |
45 |
> |
private static SAXParserFactory factory = SAXParserFactory.newInstance(); |
46 |
|
|
47 |
+ |
|
48 |
+ |
//---PUBLIC METHODS--- |
49 |
+ |
|
50 |
+ |
/** |
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 |
+ |
* @throws InvalidXMLException if the XML cannot be parsed |
56 |
+ |
*/ |
57 |
+ |
public XMLPacket createXMLPacket() throws InvalidXMLException { |
58 |
+ |
|
59 |
|
// Create the XMLPacket to store values in. |
60 |
|
XMLPacket packet = new XMLPacket(); |
61 |
< |
|
30 |
< |
// Use the default (non-validating) parser |
31 |
< |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
61 |
> |
|
62 |
|
try { |
33 |
– |
|
63 |
|
// Parse the input |
64 |
< |
InputSource inputSource = new InputSource(new StringReader(xml)); |
64 |
> |
InputSource inputSource = new InputSource(new StringReader(_xml)); |
65 |
|
SAXParser saxParser = factory.newSAXParser(); |
66 |
+ |
|
67 |
|
saxParser.parse(inputSource, new XMLStringParser(packet)); |
38 |
– |
|
68 |
|
} |
69 |
|
catch (Exception e) { |
70 |
< |
System.out.println("XMLPacketMaker - I just received an XML packet that did not contain valid XML."); |
71 |
< |
//t.printStackTrace(); |
43 |
< |
return null; |
70 |
> |
// couldn't parse the XML for some reason |
71 |
> |
throw new InvalidXMLException("Could not parse the XML: "+_xml); |
72 |
|
} |
73 |
< |
|
73 |
> |
// parsed successfully, return the packet |
74 |
|
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 |
< |
String xml; |
105 |
< |
} |
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 |
> |
} |