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