001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: XMLParser.java,v 1.1 2007/04/22 16:15:13 mlipp Exp $
021: *
022: * $Log: XMLParser.java,v $
023: * Revision 1.1 2007/04/22 16:15:13 mlipp
024: * New generic XMLRPC tool.
025: *
026: */
027: package de.danet.an.workflow.tools.xmlrpc;
028:
029: import javax.xml.transform.Templates;
030: import javax.xml.transform.TransformerConfigurationException;
031: import javax.xml.transform.sax.SAXResult;
032: import javax.xml.transform.sax.SAXTransformerFactory;
033: import javax.xml.transform.sax.TransformerHandler;
034:
035: import org.apache.xmlrpc.XmlRpcException;
036: import org.apache.xmlrpc.parser.TypeParser;
037: import org.apache.xmlrpc.serializer.TypeSerializer;
038: import org.xml.sax.ContentHandler;
039: import org.xml.sax.SAXException;
040: import org.xml.sax.helpers.XMLFilterImpl;
041:
042: import de.danet.an.util.sax.BodyFilter;
043: import de.danet.an.workflow.api.SAXEventBuffer;
044: import de.danet.an.workflow.util.SAXEventBufferImpl;
045:
046: /**
047: * This class provides a serializer for XML passes in a SAX event buffer.
048: *
049: * @author mnl
050: *
051: */
052: public class XMLParser extends XMLFilterImpl implements TypeParser {
053:
054: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
055: .getLog(XmlRpcTool.class);
056:
057: private SAXTransformerFactory transformerFactory = null;
058: private Templates templates = null;
059: private ContentHandler handler = null;
060: private SAXEventBufferImpl result = null;
061:
062: /**
063: * Create a new instance with all attributes initialized
064: * to defaults or the given values.
065: *
066: * @param transformerFactory the transformer factory to use
067: * @param templates the templates to use
068: */
069: public XMLParser(SAXTransformerFactory transformerFactory,
070: Templates templates) {
071: this .transformerFactory = transformerFactory;
072: this .templates = templates;
073:
074: // Prepare result chain
075: result = new SAXEventBufferImpl();
076: if (templates == null) {
077: setContentHandler(result);
078: } else {
079: try {
080: TransformerHandler th = transformerFactory
081: .newTransformerHandler(templates);
082: // XML header is output despite xsl:output settings
083: th.setResult(new SAXResult(new BodyFilter(result)));
084: setContentHandler(th);
085: } catch (TransformerConfigurationException e) {
086: logger.error("Cannot create transformer: "
087: + e.getMessage(), e);
088: throw (IllegalStateException) new IllegalStateException(
089: e.getMessage()).initCause(e);
090: }
091: }
092: }
093:
094: /* (non-Javadoc)
095: * Comment copied from interface or superclass.
096: */
097: public Object getResult() throws XmlRpcException {
098: return result;
099: }
100:
101: }
|