/** *######################################################################### * * A component of the Greenstone Librarian Interface (GLI) application, * part of the Greenstone digital library software suite from the New * Zealand Digital Library Project at the University of Waikato, * New Zealand. * * Author: Michael Dewsnip * Greenstone Project, New Zealand Digital Library * University of Waikato * http://www.nzdl.org * * Copyright (C) 2004 New Zealand Digital Library, University of Waikato * * 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 2 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ import java.io.*; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; /** Any class that wants to act as the owner of an AppendLineOnlyFileDocument has to implement this. * @author Michael Dewsnip, Greenstone Project, New Zealand Digital Library, University of Waikato * @version 2.41 final */ public class ApplyXSLT { static public void main(String[] args) { if (args.length < 2) { System.err.println("Usage: ApplyXSLT "); System.exit(1); } System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl"); if (args.length == 3){ System.setProperty("GSDLHOME",args[2]); } TransformerFactory t_factory = null; try { t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance(); } catch (Exception ex) { System.err.println("XMLTransformer() exception " + ex.getMessage()); System.exit(1); } String stylesheet = args[0]; String xml_in = args[1]; try { // Use the TransformerFactory to process the stylesheet Source and generate a Transformer Transformer transformer = t_factory.newTransformer(new StreamSource(stylesheet)); // Use the Transformer to transform an XML Source and send the output to a Result object StreamSource input = (xml_in.equals("-") ? new StreamSource(System.in) : new StreamSource(xml_in)); OutputStreamWriter output = new OutputStreamWriter(System.out, "UTF-8"); transformer.transform(input, new StreamResult(output)); } catch (TransformerConfigurationException ex) { //System.err.println("XMLTransformer: couldn't create transformer object: " + ex.getMessageAndLocation()); //System.err.println(ex.getLocationAsString()); ex.printStackTrace(); System.exit(1); } catch (Exception ex) { //System.err.println("XMLTransformer: couldn't transform the source: " + ex.getMessage()); ex.printStackTrace(); System.exit(1); } } static public String replaceAll(String source_string, String match_regexp, String replace_string) { return source_string.replaceAll(match_regexp, replace_string); } }