/** *######################################################################### * * 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 < 3) { System.err.println("Usage: ApplyXSLT [greenstone-major-version-number]"); System.exit(1); } String major_version = "2"; // defaults to Greenstone 2 System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl"); if (args.length >= 4){ for(int i = 3; i < args.length; i++) { // 4 to 5 args if(args[i].length() > 1) { System.setProperty("GSDLHOME",args[i]); } else { // number of chars is 1, assume it's the Greenstone version (2 or 3) major_version = args[i]; } } } 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 lang = args[0]; String stylesheet = args[1]; String xml_in = args[2]; //String major_version = (args.length >= 5) ? args[4] : "2"; //System.err.println("**** Greenstone version requested: " + major_version); 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.setParameter("lang", lang); transformer.setParameter("gs-major-version", major_version); 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); } }