/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* 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
*
* $Id: XMLSerializer.java 2364 2007-04-22 16:15:13Z mlipp $
*
* $Log$
*/
package de.danet.an.workflow.tools.xmlrpc;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import org.apache.xmlrpc.serializer.TypeSerializer;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import de.danet.an.util.sax.BodyFilter;
import de.danet.an.workflow.api.SAXEventBuffer;
/**
* This class provides a serializer for XML passes in a SAX event buffer.
*
* @author mnl
*
*/
public class XMLSerializer implements TypeSerializer {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog(XmlRpcTool.class);
private SAXTransformerFactory transformerFactory = null;
private Templates templates = null;
/**
* Create a new instance with all attributes initialized
* to defaults or the given values.
*
* @param transformerFactory the transformer factory to use
* @param templates the templates to use
*/
public XMLSerializer
(SAXTransformerFactory transformerFactory, Templates templates) {
this.transformerFactory = transformerFactory;
this.templates = templates;
}
/* (non-Javadoc)
* Comment copied from interface or superclass.
*/
public void write(ContentHandler handler, Object object)
throws SAXException {
try {
TransformerHandler th
= transformerFactory.newTransformerHandler(templates);
// XML header is output despite xsl:output settings
th.setResult(new SAXResult(new BodyFilter(handler)));
((SAXEventBuffer)object).emit (th);
} catch (TransformerConfigurationException e) {
logger.error ("Cannot create transformer: " + e.getMessage(), e);
throw (IllegalStateException)
new IllegalStateException(e.getMessage()).initCause(e);
}
}
}