source: trunk/gli/src/org/greenstone/gatherer/util/ApplyXSLT.java@ 5807

Last change on this file since 5807 was 5581, checked in by mdewsnip, 21 years ago

Many formatting, structural and code improvements.

  • Property svn:keywords set to Author Date Id Revision
File size: 2.5 KB
Line 
1/*
2 * ApplyXSLT.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.gatherer.util;
21
22import java.io.*;
23import javax.xml.transform.Transformer;
24import javax.xml.transform.TransformerFactory;
25import javax.xml.transform.TransformerConfigurationException;
26import javax.xml.transform.TransformerException;
27import javax.xml.transform.stream.StreamSource;
28import javax.xml.transform.stream.StreamResult;
29
30
31public class ApplyXSLT
32{
33 static public void main(String[] args)
34 {
35 if (args.length < 2) {
36 System.err.println("Usage: ApplyXSLT <XSLfile> <XMLfile>");
37 }
38
39 System.setProperty("javax.xml.transform.TransformerFactory",
40 "org.apache.xalan.processor.TransformerFactoryImpl");
41 TransformerFactory t_factory = null;
42 try {
43 t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
44 }
45 catch (Exception ex) {
46 System.err.println("XMLTransformer() exception " + ex.getMessage());
47 }
48
49 String stylesheet = args[0];
50 String xml_in = args[1];
51 try {
52 // Use the TransformerFactory to process the stylesheet Source and generate a Transformer
53 Transformer transformer = t_factory.newTransformer(new StreamSource(stylesheet));
54
55 // Use the Transformer to transform an XML Source and send the output to a Result object
56 StringWriter output = new StringWriter();
57 transformer.transform(new StreamSource(xml_in), new StreamResult(output));
58 System.out.println(output.toString());
59 }
60 catch (TransformerConfigurationException ex) {
61 System.err.println("XMLTransformer: couldn't create transformer object: " + ex.getMessageAndLocation());
62 System.err.println(ex.getLocationAsString());
63 }
64 catch (TransformerException ex) {
65 System.err.println("XMLTransformer: couldn't transform the source: " + ex.getMessage());
66 }
67 }
68}
Note: See TracBrowser for help on using the repository browser.