--- projects/cms/source/util/uk/org/iscream/cms/util/XMLPacketMaker.java 2000/12/07 23:26:16 1.6 +++ projects/cms/source/util/uk/org/iscream/cms/util/XMLPacketMaker.java 2002/05/21 16:47:20 1.18 @@ -1,64 +1,81 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.server.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: tdb $ - * @version $Id: XMLPacketMaker.java,v 1.6 2000/12/07 23:26:16 tdb Exp $ + * @version $Id: XMLPacketMaker.java,v 1.18 2002/05/21 16:47:20 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.6 $"; - + public final String REVISION = "$Revision: 1.18 $"; + + /** + * A reference to the system saxParser factory + */ + private final SAXParserFactory _factory = SAXParserFactory.newInstance(); + //---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; } @@ -66,10 +83,16 @@ public class XMLPacketMaker extends HandlerBase { * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * + * This uses the uk.org.iscream.cms.server.util.NameFormat class + * to format the toString() + * * @return the name of this class and its CVS revision */ public String toString() { - return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; + return FormatName.getName( + _name, + getClass().getName(), + REVISION); } //---PRIVATE METHODS--- @@ -77,9 +100,17 @@ public class XMLPacketMaker extends HandlerBase { //---ACCESSOR/MUTATOR METHODS--- //---ATTRIBUTES--- - - String _xml; - 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", + * 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---