source: documentation/trunk/shared/ApplyXSLT.java@ 22922

Last change on this file since 22922 was 19339, checked in by anna, 15 years ago

added a language parameter so that it can be set in the command line when transform xml source file. References to other language-dependent strings will be resolved to the specified language.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
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
32import java.io.*;
33import javax.xml.transform.Transformer;
34import javax.xml.transform.TransformerFactory;
35import javax.xml.transform.TransformerConfigurationException;
36import javax.xml.transform.TransformerException;
37import javax.xml.transform.stream.StreamSource;
38import 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 */
44public class ApplyXSLT
45{
46 static public void main(String[] args)
47 {
48 if (args.length < 3) {
49 System.err.println("Usage: ApplyXSLT <Target_language_code> <XSLfile> <XMLfile>");
50 System.exit(1);
51 }
52
53 System.setProperty("javax.xml.transform.TransformerFactory",
54 "org.apache.xalan.processor.TransformerFactoryImpl");
55 if (args.length == 4){
56 System.setProperty("GSDLHOME",args[3]);
57 }
58
59 TransformerFactory t_factory = null;
60 try {
61 t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
62 }
63 catch (Exception ex) {
64 System.err.println("XMLTransformer() exception " + ex.getMessage());
65 System.exit(1);
66 }
67
68 String lang = args[0];
69 String stylesheet = args[1];
70 String xml_in = args[2];
71 try {
72 // Use the TransformerFactory to process the stylesheet Source and generate a Transformer
73 Transformer transformer = t_factory.newTransformer(new StreamSource(stylesheet));
74
75 // Use the Transformer to transform an XML Source and send the output to a Result object
76 StreamSource input = (xml_in.equals("-") ? new StreamSource(System.in) : new StreamSource(xml_in));
77 OutputStreamWriter output = new OutputStreamWriter(System.out, "UTF-8");
78
79 transformer.setParameter("lang", lang);
80 transformer.transform(input, new StreamResult(output));
81 }
82 catch (TransformerConfigurationException ex) {
83 //System.err.println("XMLTransformer: couldn't create transformer object: " + ex.getMessageAndLocation());
84 //System.err.println(ex.getLocationAsString());
85 ex.printStackTrace();
86 System.exit(1);
87 }
88 catch (Exception ex) {
89 //System.err.println("XMLTransformer: couldn't transform the source: " + ex.getMessage());
90 ex.printStackTrace();
91 System.exit(1);
92 }
93 }
94
95
96 static public String replaceAll(String source_string, String match_regexp, String replace_string)
97 {
98 return source_string.replaceAll(match_regexp, replace_string);
99 }
100}
Note: See TracBrowser for help on using the repository browser.