--- projects/cms/source/util/uk/org/iscream/cms/util/XMLPacketMaker.java 2000/12/12 20:44:30 1.7 +++ projects/cms/source/util/uk/org/iscream/cms/util/XMLPacketMaker.java 2001/03/10 04:03:52 1.13 @@ -3,62 +3,54 @@ package uk.ac.ukc.iscream.util; //---IMPORTS--- import java.io.*; -import java.util.ArrayList; - import org.xml.sax.*; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; +import javax.xml.parsers.*; -import uk.ac.ukc.iscream.core.*; - /** * XMLPacketMaker - Creates an XMLPacket object. * - * @author $Author: ajm $ - * @version $Id: XMLPacketMaker.java,v 1.7 2000/12/12 20:44:30 ajm Exp $ + * @author $Author: tdb $ + * @version $Id: XMLPacketMaker.java,v 1.13 2001/03/10 04:03:52 tdb Exp $ */ -public class XMLPacketMaker extends HandlerBase { +public class XMLPacketMaker { //---FINAL ATTRIBUTES--- /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.7 $"; - + public final String REVISION = "$Revision: 1.13 $"; + //---STATIC METHODS--- //---CONSTRUCTORS--- - // Constructor for accepting XML input. - public XMLPacketMaker (String xml) { - _xml = xml; - } - //---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)); - + InputSource inputSource = new InputSource(new StringReader(xml)); + _factory.newSAXParser().parse(inputSource, new XMLStringParser(packet)); } catch (Exception e) { - _logger.write(this.toString(), Logger.WARNING, e.toString()); - _logger.write(this.toString(), Logger.WARNING, "An invalid XML UDP packet has been detected: "+packet.printAll()); - 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; } @@ -83,19 +75,8 @@ public class XMLPacketMaker extends HandlerBase { //---ACCESSOR/MUTATOR METHODS--- //---ATTRIBUTES--- - - /** - * Holds the xml - */ - String _xml; /** - * This holds a reference to the - * system logger that is being used. - */ - private Logger _logger = ReferenceManager.getInstance().getLogger(); - - /** * This is the friendly identifier of the * component this class is running in. * eg, a Filter may be called "filter1", @@ -107,5 +88,10 @@ public class XMLPacketMaker extends HandlerBase { private String _name = null; //---STATIC ATTRIBUTES--- + + /** + * A static reference to the system saxParser factory + */ + private static SAXParserFactory _factory = SAXParserFactory.newInstance(); }