[13632] | 1 | /**
|
---|
| 2 | *#########################################################################
|
---|
| 3 | *
|
---|
| 4 | * A component of the Greenstone Librarian Interface (GLI) application,
|
---|
| 5 | * part of the Greenstone digital library software suite from the New
|
---|
| 6 | * Zealand Digital Library Project at the University of Waikato,
|
---|
| 7 | * New Zealand.
|
---|
| 8 | *
|
---|
| 9 | * Author: Michael Dewsnip
|
---|
| 10 | * Greenstone Project, New Zealand Digital Library
|
---|
| 11 | * University of Waikato
|
---|
| 12 | * http://www.nzdl.org
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 2004 New Zealand Digital Library, University of Waikato
|
---|
| 15 | *
|
---|
| 16 | * This program is free software; you can redistribute it and/or modify
|
---|
| 17 | * it under the terms of the GNU General Public License as published by
|
---|
| 18 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 19 | * (at your option) any later version.
|
---|
| 20 | *
|
---|
| 21 | * This program is distributed in the hope that it will be useful,
|
---|
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 24 | * GNU General Public License for more details.
|
---|
| 25 | *
|
---|
| 26 | * You should have received a copy of the GNU General Public License
|
---|
| 27 | * along with this program; if not, write to the Free Software
|
---|
| 28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 29 | *########################################################################
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | import java.io.*;
|
---|
| 33 | import javax.xml.transform.Transformer;
|
---|
| 34 | import javax.xml.transform.TransformerFactory;
|
---|
| 35 | import javax.xml.transform.TransformerConfigurationException;
|
---|
| 36 | import javax.xml.transform.TransformerException;
|
---|
| 37 | import javax.xml.transform.stream.StreamSource;
|
---|
| 38 | import javax.xml.transform.stream.StreamResult;
|
---|
| 39 |
|
---|
| 40 | /** Any class that wants to act as the owner of an AppendLineOnlyFileDocument has to implement this.
|
---|
| 41 | * @author Michael Dewsnip, Greenstone Project, New Zealand Digital Library, University of Waikato
|
---|
| 42 | * @version 2.41 final
|
---|
| 43 | */
|
---|
| 44 | public class ApplyXSLT
|
---|
| 45 | {
|
---|
| 46 | static public void main(String[] args)
|
---|
| 47 | {
|
---|
[19339] | 48 | if (args.length < 3) {
|
---|
[25471] | 49 | System.err.println("Usage: ApplyXSLT <Target_language_code> <XSLfile> <XMLfile> [greenstone-major-version-number]");
|
---|
[13632] | 50 | System.exit(1);
|
---|
[19339] | 51 | }
|
---|
[13632] | 52 |
|
---|
[25471] | 53 | String major_version = "2"; // defaults to Greenstone 2
|
---|
| 54 |
|
---|
[13632] | 55 | System.setProperty("javax.xml.transform.TransformerFactory",
|
---|
| 56 | "org.apache.xalan.processor.TransformerFactoryImpl");
|
---|
[25471] | 57 | if (args.length >= 4){
|
---|
| 58 | for(int i = 3; i < args.length; i++) { // 4 to 5 args
|
---|
| 59 | if(args[i].length() > 1) {
|
---|
| 60 | System.setProperty("GSDLHOME",args[i]);
|
---|
| 61 | } else { // number of chars is 1, assume it's the Greenstone version (2 or 3)
|
---|
| 62 | major_version = args[i];
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
[13885] | 65 | }
|
---|
| 66 |
|
---|
[13632] | 67 | TransformerFactory t_factory = null;
|
---|
| 68 | try {
|
---|
| 69 | t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
| 70 | }
|
---|
[19339] | 71 | catch (Exception ex) {
|
---|
[13632] | 72 | System.err.println("XMLTransformer() exception " + ex.getMessage());
|
---|
| 73 | System.exit(1);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[19339] | 76 | String lang = args[0];
|
---|
| 77 | String stylesheet = args[1];
|
---|
| 78 | String xml_in = args[2];
|
---|
[25471] | 79 | //String major_version = (args.length >= 5) ? args[4] : "2";
|
---|
| 80 | //System.err.println("**** Greenstone version requested: " + major_version);
|
---|
| 81 |
|
---|
[13632] | 82 | try {
|
---|
| 83 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer
|
---|
| 84 | Transformer transformer = t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
| 85 |
|
---|
| 86 | // Use the Transformer to transform an XML Source and send the output to a Result object
|
---|
| 87 | StreamSource input = (xml_in.equals("-") ? new StreamSource(System.in) : new StreamSource(xml_in));
|
---|
| 88 | OutputStreamWriter output = new OutputStreamWriter(System.out, "UTF-8");
|
---|
[19339] | 89 |
|
---|
| 90 | transformer.setParameter("lang", lang);
|
---|
[25471] | 91 | transformer.setParameter("gs-major-version", major_version);
|
---|
[13632] | 92 | transformer.transform(input, new StreamResult(output));
|
---|
| 93 | }
|
---|
| 94 | catch (TransformerConfigurationException ex) {
|
---|
| 95 | //System.err.println("XMLTransformer: couldn't create transformer object: " + ex.getMessageAndLocation());
|
---|
| 96 | //System.err.println(ex.getLocationAsString());
|
---|
| 97 | ex.printStackTrace();
|
---|
| 98 | System.exit(1);
|
---|
| 99 | }
|
---|
| 100 | catch (Exception ex) {
|
---|
| 101 | //System.err.println("XMLTransformer: couldn't transform the source: " + ex.getMessage());
|
---|
| 102 | ex.printStackTrace();
|
---|
| 103 | System.exit(1);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | static public String replaceAll(String source_string, String match_regexp, String replace_string)
|
---|
| 109 | {
|
---|
| 110 | return source_string.replaceAll(match_regexp, replace_string);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|