1 |
< |
import java.io.*; |
2 |
< |
import java.util.ArrayList; |
1 |
> |
//---PACKAGE DECLARATION--- |
2 |
> |
package uk.ac.ukc.iscream.util; |
3 |
|
|
4 |
+ |
//---IMPORTS--- |
5 |
+ |
import java.io.*; |
6 |
|
import org.xml.sax.*; |
7 |
+ |
import javax.xml.parsers.*; |
8 |
|
|
9 |
< |
import javax.xml.parsers.SAXParserFactory; |
10 |
< |
import javax.xml.parsers.ParserConfigurationException; |
11 |
< |
import javax.xml.parsers.SAXParser; |
9 |
> |
/** |
10 |
> |
* XMLPacketMaker - Creates an XMLPacket object. |
11 |
> |
* |
12 |
> |
* @author $Author$ |
13 |
> |
* @version $Id$ |
14 |
> |
*/ |
15 |
> |
public class XMLPacketMaker { |
16 |
|
|
17 |
< |
import uk.ac.ukc.iscream.core.*; |
17 |
> |
//---FINAL ATTRIBUTES--- |
18 |
|
|
19 |
< |
// Paul Mutton, pjm2@ukc.ac.uk |
19 |
> |
/** |
20 |
> |
* The current CVS revision of this class |
21 |
> |
*/ |
22 |
> |
public final String REVISION = "$Revision$"; |
23 |
> |
|
24 |
> |
//---STATIC METHODS--- |
25 |
|
|
26 |
< |
// XMLPacketMaker - Creates an XMLPacket object. |
15 |
< |
public class XMLPacketMaker extends HandlerBase { |
26 |
> |
//---CONSTRUCTORS--- |
27 |
|
|
28 |
< |
// Constructor for accepting XML input. |
18 |
< |
public XMLPacketMaker (String xml, Logger logger) { |
19 |
< |
this.xml = xml; |
20 |
< |
this.logger = logger; |
21 |
< |
} |
28 |
> |
//---PUBLIC METHODS--- |
29 |
|
|
30 |
< |
public XMLPacket createXMLPacket() { |
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(String xml) throws InvalidXMLException { |
39 |
|
|
40 |
|
// Create the XMLPacket to store values in. |
41 |
|
XMLPacket packet = new XMLPacket(); |
42 |
< |
|
28 |
< |
// Use the default (non-validating) parser |
29 |
< |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
42 |
> |
|
43 |
|
try { |
31 |
– |
|
44 |
|
// Parse the input |
45 |
|
InputSource inputSource = new InputSource(new StringReader(xml)); |
46 |
< |
SAXParser saxParser = factory.newSAXParser(); |
35 |
< |
saxParser.parse(inputSource, new XMLStringParser(packet)); |
36 |
< |
|
46 |
> |
_factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); |
47 |
|
} |
48 |
|
catch (Exception e) { |
49 |
< |
logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); |
50 |
< |
//t.printStackTrace(); |
41 |
< |
return null; |
49 |
> |
// couldn't parse the XML for some reason |
50 |
> |
throw new InvalidXMLException("Could not parse the XML: "+xml); |
51 |
|
} |
52 |
|
|
53 |
+ |
// parsed successfully, return the packet |
54 |
|
return packet; |
55 |
|
} |
56 |
+ |
|
57 |
+ |
/** |
58 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
59 |
+ |
* method to provide clean logging (every class should have this). |
60 |
+ |
* |
61 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
62 |
+ |
* to format the toString() |
63 |
+ |
* |
64 |
+ |
* @return the name of this class and its CVS revision |
65 |
+ |
*/ |
66 |
+ |
public String toString() { |
67 |
+ |
return FormatName.getName( |
68 |
+ |
_name, |
69 |
+ |
getClass().getName(), |
70 |
+ |
REVISION); |
71 |
+ |
} |
72 |
+ |
|
73 |
+ |
//---PRIVATE METHODS--- |
74 |
+ |
|
75 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
76 |
+ |
|
77 |
+ |
//---ATTRIBUTES--- |
78 |
|
|
79 |
< |
String xml; |
80 |
< |
Logger logger; |
81 |
< |
} |
79 |
> |
/** |
80 |
> |
* This is the friendly identifier of the |
81 |
> |
* component this class is running in. |
82 |
> |
* eg, a Filter may be called "filter1", |
83 |
> |
* If this class does not have an owning |
84 |
> |
* component, a name from the configuration |
85 |
> |
* can be placed here. This name could also |
86 |
> |
* be changed to null for utility classes. |
87 |
> |
*/ |
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 |
> |
} |