1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2003 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* Author: John Harris
|
7
|
* '$Date: 2006-02-16 13:41:30 -0800 (Thu, 16 Feb 2006) $'
|
8
|
* '$Revision: 2909 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
package edu.ucsb.nceas.metacat.spatial;
|
25
|
|
26
|
import java.io.*;
|
27
|
import java.net.URL;
|
28
|
import java.net.MalformedURLException;
|
29
|
import java.sql.*;
|
30
|
import java.util.Enumeration;
|
31
|
import java.util.Hashtable;
|
32
|
import java.util.Stack;
|
33
|
|
34
|
import javax.xml.transform.TransformerFactory;
|
35
|
import javax.xml.transform.Transformer;
|
36
|
import javax.xml.transform.stream.StreamSource;
|
37
|
import javax.xml.transform.stream.StreamResult;
|
38
|
import javax.xml.transform.TransformerException;
|
39
|
import javax.xml.transform.TransformerConfigurationException;
|
40
|
import javax.xml.transform.URIResolver;
|
41
|
|
42
|
import org.apache.xerces.parsers.DOMParser;
|
43
|
import org.w3c.dom.Attr;
|
44
|
import org.w3c.dom.NamedNodeMap;
|
45
|
import org.w3c.dom.NodeList;
|
46
|
import org.w3c.dom.Document;
|
47
|
import org.w3c.dom.Node;
|
48
|
import org.w3c.dom.NodeList;
|
49
|
import org.w3c.dom.DocumentType;
|
50
|
import org.xml.sax.SAXException;
|
51
|
import org.xml.sax.InputSource;
|
52
|
import org.apache.xerces.dom.DocumentTypeImpl;
|
53
|
import org.apache.xpath.XPathAPI;
|
54
|
import org.w3c.dom.NamedNodeMap;
|
55
|
|
56
|
import org.w3c.dom.Document;
|
57
|
import org.w3c.dom.Node;
|
58
|
import org.w3c.dom.Element;
|
59
|
import org.xml.sax.SAXException;
|
60
|
|
61
|
//import edu.ucsb.nceas.utilities.IOUtil;
|
62
|
|
63
|
/**
|
64
|
* A Class that transforms XML documents utitlizing XSL style sheets. This is
|
65
|
* a convenience class that makes it easier to handle the location of
|
66
|
* stylesheets and the mapping between a request and a particular stylesheet.
|
67
|
*/
|
68
|
public class XSLTransform {
|
69
|
|
70
|
/**
|
71
|
* Private constructor because all methids are static and do not need
|
72
|
* an instance.
|
73
|
*/
|
74
|
public XSLTransform()
|
75
|
{
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Transform an XML document using an XSLT stylesheet to another format,
|
80
|
* probably HTML or another XML document format.
|
81
|
*
|
82
|
* @param docString the document to be transformed
|
83
|
* @param xslSystemId the system location of the stylesheet
|
84
|
* @param pw the PrintWriter to which output is printed
|
85
|
* @param params some parameters for inclusion to the transformation
|
86
|
*/
|
87
|
public static void transform(String docString, String xslSystemId,
|
88
|
PrintWriter pw, Hashtable param)
|
89
|
{
|
90
|
transform(new StringReader(docString), xslSystemId, pw, param);
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Transform an XML document using an XSLT stylesheet to another format,
|
95
|
* probably HTML or another XML document format.
|
96
|
*
|
97
|
* @param doc the document to be transformed
|
98
|
* @param xslSystemId the system location of the stylesheet
|
99
|
* @param pw the PrintWriter to which output is printed
|
100
|
* @param params some parameters for inclusion to the transformation
|
101
|
*/
|
102
|
public static void transform(Reader doc, String xslSystemId,
|
103
|
PrintWriter pw, Hashtable param)
|
104
|
{
|
105
|
try {
|
106
|
|
107
|
StreamSource xslSource =
|
108
|
new StreamSource(xslSystemId);
|
109
|
xslSource.setSystemId(xslSystemId);
|
110
|
// Create a stylesheet from the system id that was found
|
111
|
TransformerFactory tFactory = TransformerFactory.newInstance();
|
112
|
Transformer transformer = tFactory.newTransformer(xslSource);
|
113
|
|
114
|
// Set up parameters for transformation
|
115
|
if ( param != null) {
|
116
|
Enumeration en = param.keys();
|
117
|
while (en.hasMoreElements()) {
|
118
|
String key =(String)en.nextElement();
|
119
|
String value = ((String)(param.get(key)));
|
120
|
transformer.setParameter(key, value);
|
121
|
}
|
122
|
}
|
123
|
|
124
|
// Run the transform engine
|
125
|
StreamSource ss = new StreamSource(doc);
|
126
|
StreamResult sr = new StreamResult(pw);
|
127
|
transformer.transform(ss, sr);
|
128
|
} catch (Exception e) {
|
129
|
pw.println("Error transforming document in " +
|
130
|
"XSLTransform.transform:\n" + e.getMessage());
|
131
|
e.printStackTrace(pw);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* the main routine used to test the transform utility.
|
137
|
*
|
138
|
* Usage: java DBTransform
|
139
|
*/
|
140
|
static public void main(String[] args) {
|
141
|
|
142
|
if (args.length != 2)
|
143
|
{
|
144
|
System.err.println("Wrong number of arguments!!!");
|
145
|
System.err.println("USAGE: java XSLTransform xml style");
|
146
|
return;
|
147
|
} else {
|
148
|
String xmlfile = args[0];
|
149
|
String xslfile = args[1];
|
150
|
try {
|
151
|
Reader r = new FileReader(xmlfile);
|
152
|
XSLTransform.transform( r, xslfile,
|
153
|
new PrintWriter(System.out), null);
|
154
|
} catch (Exception e) {
|
155
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
156
|
System.err.println(e.getMessage());
|
157
|
e.printStackTrace(System.err);
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
}
|