source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLTransformer.java@ 3769

Last change on this file since 3769 was 3572, checked in by kjdon, 22 years ago

transform method now returns an Element instead of a string

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