/* * Xcm2Gxl.java * * Copyright (C) 2003 Miika Nurminen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ import java.io.*; import org.w3c.dom.*; import org.w3c.dom.html.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** * Representation of a xml transform filter. * @author Miika Nurminen * @version 0.1 - 2003/10/14 */ public class XmlTransformFramework { InputStream in; PrintStream out; protected Document doc; String docSystem = null; String docPublic = null; /** Creates a new instance of XmlTransformFramework */ public XmlTransformFramework(InputStream in, PrintStream out) { this.in = in; this.out = out; } /** If left null, no changes are made. */ public void setDoctype(String system,String publ) { docSystem = system; docPublic = publ; } protected Document newDocument(InputStream inp) throws SAXException,ParserConfigurationException,IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); if (inp!=null) return builder.parse( in ); else return builder.newDocument(); } protected void inputData() throws SAXException,ParserConfigurationException,IOException { doc = newDocument(in); } protected void doTransform() { } protected void outputData() throws TransformerException { doc.normalize(); DOMSource domSource = new DOMSource(doc); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); // XSL transformation is required to set correct doctype. Transformer serializer = tf.newTransformer(new StreamSource( this.getClass().getResourceAsStream("simpleoutput.xsl") /*"transformoutput.xsl"*/)); serializer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); if (docSystem!=null) serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,docSystem); if (docPublic!=null) serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,docPublic); serializer.setOutputProperty(OutputKeys.INDENT,"yes"); serializer.transform(domSource, streamResult); } public void performTransform() { try { inputData(); doTransform(); outputData(); } catch (SAXException sxe) { // Error generated during parsing) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); ErrorLib.errorReport(x); } catch (ParserConfigurationException pce) { ErrorLib.errorReport(pce); } catch (TransformerException e) { ErrorLib.errorReport(e); } catch (IOException e) { ErrorLib.errorReport(e); } } }