--- projects/cms/source/util/uk/org/iscream/cms/util/XMLPacketMaker.java 2000/11/23 09:36:07 1.2 +++ projects/cms/source/util/uk/org/iscream/cms/util/XMLPacketMaker.java 2001/03/10 04:03:52 1.13 @@ -1,49 +1,97 @@ -import java.io.*; -import java.util.ArrayList; +//---PACKAGE DECLARATION--- +package uk.ac.ukc.iscream.util; +//---IMPORTS--- +import java.io.*; import org.xml.sax.*; +import javax.xml.parsers.*; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; +/** + * XMLPacketMaker - Creates an XMLPacket object. + * + * @author $Author: tdb $ + * @version $Id: XMLPacketMaker.java,v 1.13 2001/03/10 04:03:52 tdb Exp $ + */ +public class XMLPacketMaker { -import uk.ac.ukc.iscream.core.*; +//---FINAL ATTRIBUTES--- -// Paul Mutton, pjm2@ukc.ac.uk + /** + * The current CVS revision of this class + */ + public final String REVISION = "$Revision: 1.13 $"; + +//---STATIC METHODS--- -// XMLPacketMaker - Creates an XMLPacket object. -public class XMLPacketMaker extends HandlerBase { +//---CONSTRUCTORS--- - // Constructor for accepting XML input. - public XMLPacketMaker (String xml, Logger logger) { - this.xml = xml; - this.logger = logger; - } +//---PUBLIC METHODS--- - public XMLPacket createXMLPacket() { + /** + * Method to create an XML packet from the data this + * class was constructed with. + * + * @param xml the XML String to parse + * @return an XMLPacket representing the XML String given + * @throws InvalidXMLException if the XML cannot be parsed + */ + public XMLPacket createXMLPacket(String xml) throws InvalidXMLException { // Create the XMLPacket to store values in. XMLPacket packet = new XMLPacket(); - - // Use the default (non-validating) parser - SAXParserFactory factory = SAXParserFactory.newInstance(); + try { - // Parse the input InputSource inputSource = new InputSource(new StringReader(xml)); - SAXParser saxParser = factory.newSAXParser(); - saxParser.parse(inputSource, new XMLStringParser(packet)); - + _factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); } catch (Exception e) { - logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); - //t.printStackTrace(); - return null; + // couldn't parse the XML for some reason + throw new InvalidXMLException("Could not parse the XML: "+xml); } + // parsed successfully, return the packet return packet; } + + /** + * Overrides the {@link java.lang.Object#toString() Object.toString()} + * method to provide clean logging (every class should have this). + * + * This uses the uk.ac.ukc.iscream.util.NameFormat class + * to format the toString() + * + * @return the name of this class and its CVS revision + */ + public String toString() { + return FormatName.getName( + _name, + getClass().getName(), + REVISION); + } + +//---PRIVATE METHODS--- + +//---ACCESSOR/MUTATOR METHODS--- + +//---ATTRIBUTES--- - String xml; - Logger logger; -} \ No newline at end of file + /** + * This is the friendly identifier of the + * component this class is running in. + * eg, a Filter may be called "filter1", + * If this class does not have an owning + * component, a name from the configuration + * can be placed here. This name could also + * be changed to null for utility classes. + */ + private String _name = null; + +//---STATIC ATTRIBUTES--- + + /** + * A static reference to the system saxParser factory + */ + private static SAXParserFactory _factory = SAXParserFactory.newInstance(); + +}