/** * @(#)ParseXml.java 02/04/02 * * Author: Manish Dixit * * Version: 1.0 * * Copyright (c) 2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not disclose * such Confidential Information and shall use it only in accordance with * the terms of the license agreement you entered into with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE * OR ITS DERIVATIVES. * * Description: * * This class is designed to server as a channel generator for * * REVISION HISTORY: * 05/15/2003 Manish Dixit -- initial version ***********************************************************************************/ import java.io.*; import java.util.*; //Import for XML import javax.xml.transform.*; import javax.xml.transform.stream.*; public class ParseXml{ public static StringBuffer getContent(BufferedInputStream in, String xsl) throws Exception { System.out.println("Inside getContent"); try{ //Set the Transformation to use System.setProperty("javax.xml.transform.TransformerFactory","org.apache.xalan.processor.TransformerFactoryImpl"); System.setProperty("javax.xml.parsers.DocumentBuilderFactory","org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); System.setProperty("javax.xml.parsers.SAXParserFactory","org.apache.xerces.jaxp.SAXParserFactoryImpl"); TransformerFactory tFactory = null; Transformer transformer = null; tFactory = TransformerFactory.newInstance(); transformer = tFactory.newTransformer(new StreamSource(xsl)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(new StreamSource(in), new StreamResult(baos)); // Output content int len; StringBuffer buf = new StringBuffer(); buf.append(baos.toString()); baos.close(); return buf; } catch(Exception e){ System.out.println("Exception in getContent"); e.printStackTrace(); return null; } } public static void main(String args[]) { BufferedInputStream xmlStream = null; File outputFile = null; if (args.length < 3) { usage(); System.exit(-1); } /* * Open the files and initialize streams */ try { // FileOutputStream = new FileOutputStream(args[2]); FileInputStream xmlFile = new FileInputStream(args[0]); xmlStream = new BufferedInputStream(xmlFile); } catch (Exception ex) { usage(); System.exit(-1); } String xsl = args[1]; StringBuffer output = null; try { output = getContent(xmlStream, xsl); } catch (Exception ex) { System.out.println("Exception during parsing"); } try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(args[2]))); out.write(output.toString(), 0, output.toString().length()); out.flush(); out.close(); } catch (Exception ex) { System.out.println("Exception during processing output stream"); } // System.out.println("Output is " + output); } private static void usage() { System.out.println("Usage: java ParseXml "); System.out.println("Please Enter valid file names\n"); } }