1 | /*
|
---|
2 | * XMLTransformer.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 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | // XML classes
|
---|
22 | import javax.xml.transform.Transformer;
|
---|
23 | import javax.xml.transform.TransformerFactory;
|
---|
24 | import javax.xml.transform.TransformerConfigurationException;
|
---|
25 | import javax.xml.transform.TransformerException;
|
---|
26 |
|
---|
27 | import javax.xml.transform.stream.StreamSource;
|
---|
28 | import javax.xml.transform.dom.DOMSource;
|
---|
29 | import javax.xml.transform.stream.StreamResult;
|
---|
30 |
|
---|
31 | import org.w3c.dom.Document;
|
---|
32 | import org.w3c.dom.Node;
|
---|
33 | import org.w3c.dom.NodeList;
|
---|
34 |
|
---|
35 | // other java classes
|
---|
36 | import java.io.StringReader;
|
---|
37 | import java.io.StringWriter;
|
---|
38 | import java.io.File;
|
---|
39 |
|
---|
40 | /** XMLTransformer - utility class for greenstone
|
---|
41 | *
|
---|
42 | * transforms xml using xslt
|
---|
43 | *
|
---|
44 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
45 | * @version $Revision: 3451 $
|
---|
46 | */
|
---|
47 | public class XMLTransformer {
|
---|
48 |
|
---|
49 | /** The transformer factory we're using */
|
---|
50 | TransformerFactory t_factory_=null;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The no-arguments constructor.
|
---|
54 | *
|
---|
55 | * Any exceptions thrown are caught internally
|
---|
56 | *
|
---|
57 | * @return a new transformer
|
---|
58 | * @see javax.xml.transform.TransformerFactory
|
---|
59 | */
|
---|
60 | public XMLTransformer() {
|
---|
61 |
|
---|
62 | // make sure we are using the xalan transformer
|
---|
63 | System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
|
---|
64 | try {
|
---|
65 | t_factory_ = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
66 |
|
---|
67 | } catch (Exception e) {
|
---|
68 | System.out.println("XMLTransformer() exception "+e.getMessage());
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Transform an XML document using a XSLT stylesheet
|
---|
76 | *
|
---|
77 | * @param stylesheet a filename for an XSLT stylesheet
|
---|
78 | * @param xml_in the XML to be transformed
|
---|
79 | * @return the transformed XML
|
---|
80 | */
|
---|
81 | public String transform(String stylesheet, String xml_in) {
|
---|
82 |
|
---|
83 | try {
|
---|
84 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
85 | Transformer transformer = t_factory_.newTransformer(new StreamSource(stylesheet));
|
---|
86 |
|
---|
87 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
88 | StringWriter output = new StringWriter();
|
---|
89 |
|
---|
90 | transformer.transform(new StreamSource(new StringReader(xml_in)), new StreamResult(output));
|
---|
91 | return output.toString();
|
---|
92 | } catch (TransformerConfigurationException e) {
|
---|
93 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessage());
|
---|
94 | return "";
|
---|
95 | } catch (TransformerException e) {
|
---|
96 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
97 | return "";
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public String transform(String stylesheet, Node xml_in) {
|
---|
102 |
|
---|
103 | try {
|
---|
104 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
105 | Transformer transformer = t_factory_.newTransformer(new StreamSource(stylesheet));
|
---|
106 |
|
---|
107 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
108 | StringWriter output = new StringWriter();
|
---|
109 |
|
---|
110 | transformer.transform(new DOMSource(xml_in), new StreamResult(output));
|
---|
111 | return output.toString();
|
---|
112 | } catch (TransformerConfigurationException e) {
|
---|
113 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessageAndLocation());
|
---|
114 | return "";
|
---|
115 | } catch (TransformerException e) {
|
---|
116 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
117 | return "";
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | public String transform(Document stylesheet, Node xml_in) {
|
---|
123 |
|
---|
124 | try {
|
---|
125 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
126 | Transformer transformer = t_factory_.newTransformer(new DOMSource(stylesheet));
|
---|
127 |
|
---|
128 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
129 | StringWriter output = new StringWriter();
|
---|
130 |
|
---|
131 | transformer.transform(new DOMSource(xml_in), new StreamResult(output));
|
---|
132 | return output.toString();
|
---|
133 | } catch (TransformerConfigurationException e) {
|
---|
134 | System.err.println("XMLTransformer: couldn't create transformer object: "+e.getMessage());
|
---|
135 | return "";
|
---|
136 | } catch (TransformerException e) {
|
---|
137 | System.err.println("XMLTransformer: couldn't transform the source: " + e.getMessage());
|
---|
138 | return "";
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|