1 |
ajm |
1.7 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.14 |
package uk.org.iscream.util; |
3 |
ajm |
1.7 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import java.io.*; |
6 |
|
|
import org.xml.sax.*; |
7 |
tdb |
1.13 |
import javax.xml.parsers.*; |
8 |
ajm |
1.7 |
|
9 |
|
|
/** |
10 |
|
|
* XMLPacketMaker - Creates an XMLPacket object. |
11 |
|
|
* |
12 |
tdb |
1.9 |
* @author $Author: tdb1 $ |
13 |
tdb |
1.15 |
* @version $Id: XMLPacketMaker.java,v 1.14 2001/03/14 23:25:30 tdb1 Exp $ |
14 |
ajm |
1.7 |
*/ |
15 |
tdb |
1.13 |
public class XMLPacketMaker { |
16 |
ajm |
1.7 |
|
17 |
|
|
//---FINAL ATTRIBUTES--- |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* The current CVS revision of this class |
21 |
|
|
*/ |
22 |
tdb |
1.15 |
public final String REVISION = "$Revision: 1.14 $"; |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* A reference to the system saxParser factory |
26 |
|
|
*/ |
27 |
|
|
private final SAXParserFactory _factory = SAXParserFactory.newInstance(); |
28 |
tdb |
1.11 |
|
29 |
ajm |
1.7 |
//---STATIC METHODS--- |
30 |
|
|
|
31 |
|
|
//---CONSTRUCTORS--- |
32 |
|
|
|
33 |
|
|
//---PUBLIC METHODS--- |
34 |
|
|
|
35 |
tdb |
1.9 |
/** |
36 |
|
|
* Method to create an XML packet from the data this |
37 |
|
|
* class was constructed with. |
38 |
|
|
* |
39 |
tdb |
1.13 |
* @param xml the XML String to parse |
40 |
tdb |
1.9 |
* @return an XMLPacket representing the XML String given |
41 |
tdb |
1.10 |
* @throws InvalidXMLException if the XML cannot be parsed |
42 |
tdb |
1.9 |
*/ |
43 |
tdb |
1.13 |
public XMLPacket createXMLPacket(String xml) throws InvalidXMLException { |
44 |
ajm |
1.7 |
|
45 |
|
|
// Create the XMLPacket to store values in. |
46 |
|
|
XMLPacket packet = new XMLPacket(); |
47 |
tdb |
1.11 |
|
48 |
ajm |
1.7 |
try { |
49 |
|
|
// Parse the input |
50 |
tdb |
1.13 |
InputSource inputSource = new InputSource(new StringReader(xml)); |
51 |
|
|
_factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); |
52 |
ajm |
1.7 |
} |
53 |
|
|
catch (Exception e) { |
54 |
tdb |
1.10 |
// couldn't parse the XML for some reason |
55 |
tdb |
1.13 |
throw new InvalidXMLException("Could not parse the XML: "+xml); |
56 |
ajm |
1.7 |
} |
57 |
tdb |
1.13 |
|
58 |
tdb |
1.10 |
// parsed successfully, return the packet |
59 |
ajm |
1.7 |
return packet; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
64 |
|
|
* method to provide clean logging (every class should have this). |
65 |
|
|
* |
66 |
tdb |
1.14 |
* This uses the uk.org.iscream.util.NameFormat class |
67 |
ajm |
1.7 |
* to format the toString() |
68 |
|
|
* |
69 |
|
|
* @return the name of this class and its CVS revision |
70 |
|
|
*/ |
71 |
|
|
public String toString() { |
72 |
|
|
return FormatName.getName( |
73 |
|
|
_name, |
74 |
|
|
getClass().getName(), |
75 |
|
|
REVISION); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
//---PRIVATE METHODS--- |
79 |
|
|
|
80 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
81 |
|
|
|
82 |
|
|
//---ATTRIBUTES--- |
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 |
|
|
|
97 |
|
|
} |