| 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.*; |
| 14 |
|
|
| 15 |
< |
// Paul Mutton, pjm2@ukc.ac.uk |
| 16 |
< |
|
| 17 |
< |
// XMLPacketMaker - Creates an XMLPacket object. |
| 15 |
> |
/** |
| 16 |
> |
* XMLPacketMaker - Creates an XMLPacket object. |
| 17 |
> |
* |
| 18 |
> |
* @author $Author$ |
| 19 |
> |
* @version $Id$ |
| 20 |
> |
*/ |
| 21 |
|
public class XMLPacketMaker extends HandlerBase { |
| 22 |
|
|
| 23 |
+ |
//---FINAL ATTRIBUTES--- |
| 24 |
+ |
|
| 25 |
+ |
/** |
| 26 |
+ |
* The current CVS revision of this class |
| 27 |
+ |
*/ |
| 28 |
+ |
public final String REVISION = "$Revision$"; |
| 29 |
+ |
|
| 30 |
+ |
//---STATIC METHODS--- |
| 31 |
+ |
|
| 32 |
+ |
//---CONSTRUCTORS--- |
| 33 |
+ |
|
| 34 |
|
// Constructor for accepting XML input. |
| 35 |
< |
public XMLPacketMaker (String xml, Logger logger) { |
| 36 |
< |
this.xml = xml; |
| 20 |
< |
this.logger = logger; |
| 35 |
> |
public XMLPacketMaker (String xml) { |
| 36 |
> |
_xml = xml; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
+ |
//---PUBLIC METHODS--- |
| 40 |
+ |
|
| 41 |
|
public XMLPacket createXMLPacket() { |
| 42 |
|
|
| 43 |
|
// Create the XMLPacket to store values in. |
| 48 |
|
try { |
| 49 |
|
|
| 50 |
|
// Parse the input |
| 51 |
< |
InputSource inputSource = new InputSource(new StringReader(xml)); |
| 51 |
> |
InputSource inputSource = new InputSource(new StringReader(_xml)); |
| 52 |
|
SAXParser saxParser = factory.newSAXParser(); |
| 53 |
|
saxParser.parse(inputSource, new XMLStringParser(packet)); |
| 54 |
|
|
| 55 |
|
} |
| 56 |
|
catch (Exception e) { |
| 57 |
< |
logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); |
| 58 |
< |
//t.printStackTrace(); |
| 57 |
> |
_logger.write(this.toString(), Logger.WARNING, e.toString()); |
| 58 |
> |
_logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); |
| 59 |
|
return null; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
return packet; |
| 63 |
|
} |
| 64 |
+ |
|
| 65 |
+ |
/** |
| 66 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 67 |
+ |
* method to provide clean logging (every class should have this). |
| 68 |
+ |
* |
| 69 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 70 |
+ |
* to format the toString() |
| 71 |
+ |
* |
| 72 |
+ |
* @return the name of this class and its CVS revision |
| 73 |
+ |
*/ |
| 74 |
+ |
public String toString() { |
| 75 |
+ |
return FormatName.getName( |
| 76 |
+ |
_name, |
| 77 |
+ |
getClass().getName(), |
| 78 |
+ |
REVISION); |
| 79 |
+ |
} |
| 80 |
+ |
|
| 81 |
+ |
//---PRIVATE METHODS--- |
| 82 |
+ |
|
| 83 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
| 84 |
+ |
|
| 85 |
+ |
//---ATTRIBUTES--- |
| 86 |
+ |
|
| 87 |
+ |
/** |
| 88 |
+ |
* Holds the xml |
| 89 |
+ |
*/ |
| 90 |
+ |
String _xml; |
| 91 |
|
|
| 92 |
< |
String xml; |
| 93 |
< |
Logger logger; |
| 94 |
< |
} |
| 92 |
> |
/** |
| 93 |
> |
* This holds a reference to the |
| 94 |
> |
* system logger that is being used. |
| 95 |
> |
*/ |
| 96 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 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 |
> |
} |