1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
< |
package uk.ac.ukc.iscream.util; |
21 |
> |
package uk.org.iscream.cms.server.util; |
22 |
|
|
23 |
|
//---IMPORTS--- |
24 |
|
import java.io.*; |
6 |
– |
import java.util.ArrayList; |
7 |
– |
|
25 |
|
import org.xml.sax.*; |
26 |
< |
import javax.xml.parsers.SAXParserFactory; |
10 |
< |
import javax.xml.parsers.ParserConfigurationException; |
11 |
< |
import javax.xml.parsers.SAXParser; |
26 |
> |
import javax.xml.parsers.*; |
27 |
|
|
13 |
– |
import uk.ac.ukc.iscream.core.*; |
14 |
– |
|
28 |
|
/** |
29 |
|
* XMLPacketMaker - Creates an XMLPacket object. |
30 |
|
* |
31 |
|
* @author $Author$ |
32 |
|
* @version $Id$ |
33 |
|
*/ |
34 |
< |
public class XMLPacketMaker extends HandlerBase { |
34 |
> |
public class XMLPacketMaker { |
35 |
|
|
36 |
|
//---FINAL ATTRIBUTES--- |
37 |
|
|
39 |
|
* The current CVS revision of this class |
40 |
|
*/ |
41 |
|
public final String REVISION = "$Revision$"; |
42 |
< |
|
42 |
> |
|
43 |
> |
/** |
44 |
> |
* A reference to the system saxParser factory |
45 |
> |
*/ |
46 |
> |
private final SAXParserFactory _factory = SAXParserFactory.newInstance(); |
47 |
> |
|
48 |
|
//---STATIC METHODS--- |
49 |
|
|
50 |
|
//---CONSTRUCTORS--- |
51 |
|
|
34 |
– |
// Constructor for accepting XML input. |
35 |
– |
public XMLPacketMaker (String xml) { |
36 |
– |
_xml = xml; |
37 |
– |
} |
38 |
– |
|
52 |
|
//---PUBLIC METHODS--- |
53 |
|
|
54 |
< |
public XMLPacket createXMLPacket() { |
54 |
> |
/** |
55 |
> |
* Method to create an XML packet from the data this |
56 |
> |
* class was constructed with. |
57 |
> |
* |
58 |
> |
* @param xml the XML String to parse |
59 |
> |
* @return an XMLPacket representing the XML String given |
60 |
> |
* @throws InvalidXMLException if the XML cannot be parsed |
61 |
> |
*/ |
62 |
> |
public XMLPacket createXMLPacket(String xml) throws InvalidXMLException { |
63 |
|
|
64 |
|
// Create the XMLPacket to store values in. |
65 |
|
XMLPacket packet = new XMLPacket(); |
66 |
< |
|
46 |
< |
// Use the default (non-validating) parser |
47 |
< |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
66 |
> |
|
67 |
|
try { |
49 |
– |
|
68 |
|
// Parse the input |
69 |
< |
InputSource inputSource = new InputSource(new StringReader(_xml)); |
70 |
< |
SAXParser saxParser = factory.newSAXParser(); |
53 |
< |
saxParser.parse(inputSource, new XMLStringParser(packet)); |
54 |
< |
|
69 |
> |
InputSource inputSource = new InputSource(new StringReader(xml)); |
70 |
> |
_factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); |
71 |
|
} |
72 |
|
catch (Exception e) { |
73 |
< |
_logger.write(this.toString(), Logger.WARNING, e.toString()); |
74 |
< |
_logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); |
59 |
< |
return null; |
73 |
> |
// couldn't parse the XML for some reason |
74 |
> |
throw new InvalidXMLException("Could not parse the XML: "+xml); |
75 |
|
} |
76 |
|
|
77 |
+ |
// parsed successfully, return the packet |
78 |
|
return packet; |
79 |
|
} |
80 |
|
|
82 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
83 |
|
* method to provide clean logging (every class should have this). |
84 |
|
* |
85 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
85 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
86 |
|
* to format the toString() |
87 |
|
* |
88 |
|
* @return the name of this class and its CVS revision |
99 |
|
//---ACCESSOR/MUTATOR METHODS--- |
100 |
|
|
101 |
|
//---ATTRIBUTES--- |
86 |
– |
|
87 |
– |
/** |
88 |
– |
* Holds the xml |
89 |
– |
*/ |
90 |
– |
String _xml; |
91 |
– |
|
92 |
– |
/** |
93 |
– |
* This holds a reference to the |
94 |
– |
* system logger that is being used. |
95 |
– |
*/ |
96 |
– |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
102 |
|
|
103 |
|
/** |
104 |
|
* This is the friendly identifier of the |