// 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; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.xml.sax.SAXException; import xaal.adapters.XaalObjects2JAWAA; import xaal.adapters.XaalObjects2ODFDom; import xaal.adapters.XaalObjects2XaalXML; import xaal.adapters.XaalObjectsAdapter; import xaal.objects.Xaal; import xaal.parser.ParserModule; import xaal.parser.ParserModuleException; import xaal.parser.XmlParser; import xaal.parser.modules.XaalAnimParserModule; import xaal.parser.modules.XaalDSAnimParserModule; import xaal.parser.modules.XaalDSParserModule; import xaal.parser.modules.XaalGPAnimParserModule; import xaal.parser.modules.XaalGPParserModule; import xaal.parser.modules.XaalMainParserModule; import xaal.parser.modules.XaalMetadataParserModule; public class XaalTransformer { private static String fileName; /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { if (args.length < 4) { System.out.println("Arguments needed: "); System.exit(0); } fileName = args[2]; File outFile = new File(args[3]); if (!outFile.exists()) { outFile.createNewFile(); } new XaalTransformer().transform(args[0], args[1], new FileInputStream(new File(args[2])), new FileOutputStream(outFile)); } private Xaal xaal; public void transform(String fromFormat, String toFormat, InputStream in, OutputStream out) throws Exception { if (!fromFormat.equalsIgnoreCase("xaal")) { System.out.println("Invalid input format: " + fromFormat + ".\nValid formats are xaal."); return; } XaalObjectsAdapter xoa = null; if (toFormat.equalsIgnoreCase("xaal")) { xoa = new XaalObjects2XaalXML(); } else if (toFormat.equalsIgnoreCase("jawaa")) { //xoa = new XaalObjects2JAWAA(); xslTransform(in, out, "resources/stylesheets/xaal2jawaa.xsl"); return; } else if (toFormat.equalsIgnoreCase("odf")) { xoa = new XaalObjects2ODFDom(); } else { System.out.println("Invalid output format: " + fromFormat + ".\nValid formats are xaal, jawaa, odf."); System.exit(0); } XmlParser xp = new XmlParser(); xp = new XmlParser(); xp.registerParserModule(XaalDSAnimParserModule.class.getName()); xp.registerParserModule(XaalDSParserModule.class.getName()); xp.registerParserModule(XaalGPParserModule.class.getName()); xp.registerParserModule(XaalMainParserModule.class.getName()); xp.registerParserModule(XaalAnimParserModule.class.getName()); xp.registerParserModule(XaalMetadataParserModule.class.getName()); xp.registerParserModule(XaalGPAnimParserModule.class.getName()); xp.parse(new FileInputStream(new File(fileName))); xaal = (Xaal) ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); xoa.adapt(xaal, out); } private static void xslTransform(InputStream in, OutputStream out, String xslFile) throws Exception { InputStream inStyle = XaalTransformer.class.getClassLoader().getResourceAsStream(xslFile); StreamSource style = new StreamSource(inStyle); Transformer trans = TransformerFactory.newInstance().newTransformer(style); StreamSource source = new StreamSource(in); StreamResult result = new StreamResult(out); trans.transform(source, result); } }