// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // 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 3 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, see . package xaal.adapters; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import xaal.objects.Xaal; /** * This Adapter can be used if you have an XSL stylesheet that converts * Xaal to some other format. By giving the stylesheet as a source the * given Xaal Objects hierarchy is first adapted to Xaal XML and then * the stylesheet is applied. * @author vkaravir */ public class XSLAdapter { /** * Applies the given stylesheet to an XML adaptation of the given Xaal * object hierarchy and writes the result to the given Writer. * * @param xaal The root of the Xaal object hierarchy. * @param style The stylesheet to be applied. * @param out The output Writer that the result is written to. */ public static void adapt(Xaal xaal, Source style, OutputStream out) { try { FileOutputStream fos = new FileOutputStream(System.getProperty("java.io.tmpdir") + File.separatorChar + "xaal.tmp"); new XaalObjects2XaalXML().adapt(xaal, fos); fos.flush(); fos.close(); Transformer trans = TransformerFactory.newInstance().newTransformer(style); StreamSource source = new StreamSource(new FileReader(System.getProperty("java.io.tmpdir") + File.separatorChar + "xaal.tmp")); StreamResult result = new StreamResult(out); trans.transform(source, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }