1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.xml; |
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 javax.xml.parsers.SAXParserFactory; |
10 |
< |
import javax.xml.parsers.ParserConfigurationException; |
11 |
< |
import javax.xml.parsers.SAXParser; |
7 |
> |
import javax.xml.parsers.*; |
8 |
|
|
13 |
– |
import uk.ac.ukc.iscream.core.*; |
14 |
– |
|
9 |
|
/** |
10 |
|
* XMLPacketMaker - Creates an XMLPacket object. |
11 |
|
* |
12 |
|
* @author $Author$ |
13 |
|
* @version $Id$ |
14 |
|
*/ |
15 |
< |
public class XMLPacketMaker extends HandlerBase { |
15 |
> |
public class XMLPacketMaker { |
16 |
|
|
17 |
|
//---FINAL ATTRIBUTES--- |
18 |
|
|
20 |
|
* The current CVS revision of this class |
21 |
|
*/ |
22 |
|
public final String REVISION = "$Revision$"; |
23 |
< |
|
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 |
|
|
34 |
– |
// Constructor for accepting XML input. |
35 |
– |
public XMLPacketMaker (String xml, Logger logger) { |
36 |
– |
this.xml = xml; |
37 |
– |
this.logger = logger; |
38 |
– |
} |
39 |
– |
|
33 |
|
//---PUBLIC METHODS--- |
34 |
|
|
35 |
< |
public XMLPacket createXMLPacket() { |
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(String xml) throws InvalidXMLException { |
44 |
|
|
45 |
|
// Create the XMLPacket to store values in. |
46 |
|
XMLPacket packet = new XMLPacket(); |
47 |
< |
|
47 |
< |
// Use the default (non-validating) parser |
48 |
< |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
47 |
> |
|
48 |
|
try { |
50 |
– |
|
49 |
|
// Parse the input |
50 |
|
InputSource inputSource = new InputSource(new StringReader(xml)); |
51 |
< |
SAXParser saxParser = factory.newSAXParser(); |
54 |
< |
saxParser.parse(inputSource, new XMLStringParser(packet)); |
55 |
< |
|
51 |
> |
_factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); |
52 |
|
} |
53 |
|
catch (Exception e) { |
54 |
< |
logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); |
55 |
< |
//t.printStackTrace(); |
60 |
< |
return null; |
54 |
> |
// couldn't parse the XML for some reason |
55 |
> |
throw new InvalidXMLException("Could not parse the XML: "+xml); |
56 |
|
} |
57 |
|
|
58 |
+ |
// parsed successfully, return the packet |
59 |
|
return packet; |
60 |
|
} |
61 |
|
|
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.org.iscream.util.NameFormat class |
67 |
+ |
* to format the toString() |
68 |
+ |
* |
69 |
|
* @return the name of this class and its CVS revision |
70 |
|
*/ |
71 |
|
public String toString() { |
72 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
72 |
> |
return FormatName.getName( |
73 |
> |
_name, |
74 |
> |
getClass().getName(), |
75 |
> |
REVISION); |
76 |
|
} |
77 |
|
|
78 |
|
//---PRIVATE METHODS--- |
80 |
|
//---ACCESSOR/MUTATOR METHODS--- |
81 |
|
|
82 |
|
//---ATTRIBUTES--- |
83 |
< |
|
84 |
< |
String xml; |
85 |
< |
Logger logger; |
83 |
> |
|
84 |
> |
/** |
85 |
> |
* This is the friendly identifier of the |
86 |
> |
* component this class is running in. |
87 |
> |
* eg, a Filter may be called "filter1", |
88 |
> |
* If this class does not have an owning |
89 |
> |
* component, a name from the configuration |
90 |
> |
* can be placed here. This name could also |
91 |
> |
* be changed to null for utility classes. |
92 |
> |
*/ |
93 |
> |
private String _name = null; |
94 |
|
|
95 |
|
//---STATIC ATTRIBUTES--- |
96 |
|
|