01: /*
02: * This file is part of the WfMOpen project.
03: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
04: * All rights reserved.
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * $Id: XMLSerializer.java,v 1.1 2007/04/22 16:15:13 mlipp Exp $
21: *
22: * $Log: XMLSerializer.java,v $
23: * Revision 1.1 2007/04/22 16:15:13 mlipp
24: * New generic XMLRPC tool.
25: *
26: */
27: package de.danet.an.workflow.tools.xmlrpc;
28:
29: import javax.xml.transform.Templates;
30: import javax.xml.transform.TransformerConfigurationException;
31: import javax.xml.transform.sax.SAXResult;
32: import javax.xml.transform.sax.SAXTransformerFactory;
33: import javax.xml.transform.sax.TransformerHandler;
34:
35: import org.apache.xmlrpc.serializer.TypeSerializer;
36: import org.xml.sax.ContentHandler;
37: import org.xml.sax.SAXException;
38:
39: import de.danet.an.util.sax.BodyFilter;
40: import de.danet.an.workflow.api.SAXEventBuffer;
41:
42: /**
43: * This class provides a serializer for XML passes in a SAX event buffer.
44: *
45: * @author mnl
46: *
47: */
48: public class XMLSerializer implements TypeSerializer {
49:
50: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
51: .getLog(XmlRpcTool.class);
52:
53: private SAXTransformerFactory transformerFactory = null;
54: private Templates templates = null;
55:
56: /**
57: * Create a new instance with all attributes initialized
58: * to defaults or the given values.
59: *
60: * @param transformerFactory the transformer factory to use
61: * @param templates the templates to use
62: */
63: public XMLSerializer(SAXTransformerFactory transformerFactory,
64: Templates templates) {
65: this .transformerFactory = transformerFactory;
66: this .templates = templates;
67: }
68:
69: /* (non-Javadoc)
70: * Comment copied from interface or superclass.
71: */
72: public void write(ContentHandler handler, Object object)
73: throws SAXException {
74: try {
75: TransformerHandler th = transformerFactory
76: .newTransformerHandler(templates);
77: // XML header is output despite xsl:output settings
78: th.setResult(new SAXResult(new BodyFilter(handler)));
79: ((SAXEventBuffer) object).emit(th);
80: } catch (TransformerConfigurationException e) {
81: logger.error(
82: "Cannot create transformer: " + e.getMessage(), e);
83: throw (IllegalStateException) new IllegalStateException(e
84: .getMessage()).initCause(e);
85: }
86: }
87:
88: }
|