1 |
ajm |
1.7 |
//---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 |
|
|
import javax.xml.parsers.SAXParserFactory; |
10 |
|
|
import javax.xml.parsers.ParserConfigurationException; |
11 |
|
|
import javax.xml.parsers.SAXParser; |
12 |
|
|
|
13 |
|
|
/** |
14 |
|
|
* XMLPacketMaker - Creates an XMLPacket object. |
15 |
|
|
* |
16 |
tdb |
1.9 |
* @author $Author: tdb1 $ |
17 |
|
|
* @version $Id: XMLPacketMaker.java,v 1.8 2001/01/18 01:55:57 tdb1 Exp $ |
18 |
ajm |
1.7 |
*/ |
19 |
|
|
public class XMLPacketMaker extends HandlerBase { |
20 |
|
|
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
tdb |
1.9 |
public final String REVISION = "$Revision: 1.8 $"; |
27 |
ajm |
1.7 |
|
28 |
|
|
//---STATIC METHODS--- |
29 |
|
|
|
30 |
|
|
//---CONSTRUCTORS--- |
31 |
|
|
|
32 |
tdb |
1.9 |
/** |
33 |
|
|
* Constructor for accepting XML input. |
34 |
|
|
* |
35 |
|
|
* @param xml A String of XML to process. |
36 |
|
|
*/ |
37 |
ajm |
1.7 |
public XMLPacketMaker (String xml) { |
38 |
|
|
_xml = xml; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
//---PUBLIC METHODS--- |
42 |
|
|
|
43 |
tdb |
1.9 |
/** |
44 |
|
|
* Method to create an XML packet from the data this |
45 |
|
|
* class was constructed with. |
46 |
|
|
* |
47 |
|
|
* @return an XMLPacket representing the XML String given |
48 |
|
|
*/ |
49 |
ajm |
1.7 |
public XMLPacket createXMLPacket() { |
50 |
|
|
|
51 |
|
|
// Create the XMLPacket to store values in. |
52 |
|
|
XMLPacket packet = new XMLPacket(); |
53 |
|
|
|
54 |
|
|
// Use the default (non-validating) parser |
55 |
|
|
SAXParserFactory factory = SAXParserFactory.newInstance(); |
56 |
|
|
try { |
57 |
|
|
|
58 |
|
|
// Parse the input |
59 |
|
|
InputSource inputSource = new InputSource(new StringReader(_xml)); |
60 |
|
|
SAXParser saxParser = factory.newSAXParser(); |
61 |
|
|
saxParser.parse(inputSource, new XMLStringParser(packet)); |
62 |
|
|
|
63 |
|
|
} |
64 |
|
|
catch (Exception e) { |
65 |
|
|
return null; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
return packet; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
73 |
|
|
* method to provide clean logging (every class should have this). |
74 |
|
|
* |
75 |
|
|
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
76 |
|
|
* to format the toString() |
77 |
|
|
* |
78 |
|
|
* @return the name of this class and its CVS revision |
79 |
|
|
*/ |
80 |
|
|
public String toString() { |
81 |
|
|
return FormatName.getName( |
82 |
|
|
_name, |
83 |
|
|
getClass().getName(), |
84 |
|
|
REVISION); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
//---PRIVATE METHODS--- |
88 |
|
|
|
89 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
90 |
|
|
|
91 |
|
|
//---ATTRIBUTES--- |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* Holds the xml |
95 |
|
|
*/ |
96 |
|
|
String _xml; |
97 |
|
|
|
98 |
|
|
/** |
99 |
|
|
* This is the friendly identifier of the |
100 |
|
|
* component this class is running in. |
101 |
|
|
* eg, a Filter may be called "filter1", |
102 |
|
|
* If this class does not have an owning |
103 |
|
|
* component, a name from the configuration |
104 |
|
|
* can be placed here. This name could also |
105 |
|
|
* be changed to null for utility classes. |
106 |
|
|
*/ |
107 |
|
|
private String _name = null; |
108 |
|
|
|
109 |
|
|
//---STATIC ATTRIBUTES--- |
110 |
|
|
|
111 |
|
|
} |