1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that transforms an XML text document
|
4
|
* into a another type using XSL
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Matt Jones
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: berkley $'
|
11
|
* '$Date: 2001-01-18 15:15:21 -0800 (Thu, 18 Jan 2001) $'
|
12
|
* '$Revision: 675 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import java.io.*;
|
32
|
import java.net.URL;
|
33
|
import java.net.MalformedURLException;
|
34
|
import java.sql.*;
|
35
|
import java.util.Stack;
|
36
|
|
37
|
import oracle.xml.parser.v2.XSLStylesheet;
|
38
|
import oracle.xml.parser.v2.XSLException;
|
39
|
import oracle.xml.parser.v2.XSLProcessor;
|
40
|
import oracle.xml.parser.v2.XMLDocument;
|
41
|
import oracle.xml.parser.v2.DOMParser;
|
42
|
|
43
|
/**
|
44
|
* A Class that transforms XML documents utitlizing XSL style sheets
|
45
|
*/
|
46
|
public class DBTransform {
|
47
|
|
48
|
private Connection conn = null;
|
49
|
|
50
|
/**
|
51
|
* construct a DBTransform instance.
|
52
|
*
|
53
|
* Generally, one calls transformXMLDocument() after constructing the instance
|
54
|
*
|
55
|
* @param conn the database connection from which to lookup the public ids
|
56
|
*/
|
57
|
public DBTransform( Connection conn )
|
58
|
throws IOException,
|
59
|
SQLException,
|
60
|
ClassNotFoundException
|
61
|
{
|
62
|
this.conn = conn;
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Transform an XML document using the stylesheet reference from the db
|
67
|
*
|
68
|
* @param doc the document to be transformed
|
69
|
* @param sourcetype the document type of the source
|
70
|
* @param targettype the target document type
|
71
|
*/
|
72
|
public void transformXMLDocument(String doc, String sourcetype,
|
73
|
String targettype, PrintWriter pw) {
|
74
|
|
75
|
// Look up the stylesheet for this type combination
|
76
|
String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
|
77
|
|
78
|
if (xsl_system_id != null) {
|
79
|
// Create a stylesheet from the system id that was found
|
80
|
try {
|
81
|
XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
|
82
|
DOMParser dp = new DOMParser();
|
83
|
dp.setValidationMode(false);
|
84
|
dp.parse((Reader)(new StringReader(doc)));
|
85
|
new XSLProcessor().processXSL(style, dp.getDocument(), pw);
|
86
|
} catch (Exception e) {
|
87
|
pw.println(xsl_system_id + "Error transforming document in " +
|
88
|
"DBTransform.transformXMLDocument: " +
|
89
|
e.getMessage());
|
90
|
}
|
91
|
} else {
|
92
|
// No stylesheet registered form this document type, so just return the
|
93
|
// XML stream we were passed
|
94
|
pw.print(doc);
|
95
|
}
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Lookup a stylesheet reference from the db catalog
|
100
|
*
|
101
|
* @param objecttype the type of the object we want to retrieve
|
102
|
* @param sourcetype the document type of the source
|
103
|
* @param targettype the document type of the target
|
104
|
*/
|
105
|
public String getSystemId(String objecttype, String sourcetype,
|
106
|
String targettype) {
|
107
|
|
108
|
// Look up the System ID of a particular object
|
109
|
PreparedStatement pstmt;
|
110
|
String the_system_id = null;
|
111
|
try {
|
112
|
pstmt =
|
113
|
conn.prepareStatement("SELECT system_id " +
|
114
|
"FROM xml_catalog " +
|
115
|
"WHERE entry_type LIKE ? " +
|
116
|
"AND source_doctype LIKE ? " +
|
117
|
"AND target_doctype LIKE ? ");
|
118
|
// Bind the values to the query
|
119
|
pstmt.setString(1, objecttype);
|
120
|
pstmt.setString(2, sourcetype);
|
121
|
pstmt.setString(3, targettype);
|
122
|
pstmt.execute();
|
123
|
try {
|
124
|
ResultSet rs = pstmt.getResultSet();
|
125
|
try {
|
126
|
boolean tableHasRows = rs.next();
|
127
|
if (tableHasRows) {
|
128
|
try {
|
129
|
the_system_id = rs.getString(1);
|
130
|
} catch (SQLException e) {
|
131
|
System.out.println("Error with getString in " +
|
132
|
"DBTransform.getSystemId: " + e.getMessage());
|
133
|
}
|
134
|
} else {
|
135
|
the_system_id = null;
|
136
|
}
|
137
|
} catch (SQLException e) {
|
138
|
System.err.println("Error with next in DBTransform.getSystemId: " +
|
139
|
e.getMessage());
|
140
|
return ("Error with next: " + e.getMessage());
|
141
|
}
|
142
|
} catch (SQLException e) {
|
143
|
System.err.println("Error with getrset in DBTransform.getSystemId: " +
|
144
|
e.getMessage());
|
145
|
return ("Error with getrset: " + e.getMessage());
|
146
|
}
|
147
|
pstmt.close();
|
148
|
} catch (SQLException e) {
|
149
|
System.err.println("Error getting id in DBTransform.getSystemId: " +
|
150
|
e.getMessage());
|
151
|
return ("Error getting id in DBTransform.getSystemId:: " +
|
152
|
e.getMessage());
|
153
|
}
|
154
|
return the_system_id;
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* the main routine used to test the transform utility.
|
159
|
*
|
160
|
* Usage: java DBTransform
|
161
|
*/
|
162
|
static public void main(String[] args) {
|
163
|
|
164
|
if (args.length > 0)
|
165
|
{
|
166
|
System.err.println("Wrong number of arguments!!!");
|
167
|
System.err.println("USAGE: java DBTransform");
|
168
|
return;
|
169
|
} else {
|
170
|
try {
|
171
|
|
172
|
// Open a connection to the database
|
173
|
MetaCatUtil util = new MetaCatUtil();
|
174
|
Connection dbconn = util.openDBConnection();
|
175
|
|
176
|
// Create a test document
|
177
|
StringBuffer testdoc = new StringBuffer();
|
178
|
testdoc.append("<?xml version=\"1.0\"?>");
|
179
|
testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
|
180
|
testdoc.append("<dataset_id>DS001</dataset_id>");
|
181
|
testdoc.append("<title>My test doc</title></eml-dataset>");
|
182
|
|
183
|
// Transform the document to the new doctype
|
184
|
DBTransform dbt = new DBTransform(dbconn);
|
185
|
dbt.transformXMLDocument(testdoc.toString(),
|
186
|
"-//NCEAS//eml-dataset//EN",
|
187
|
"-//W3C//HTML//EN",
|
188
|
new PrintWriter(System.out));
|
189
|
|
190
|
} catch (Exception e) {
|
191
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
192
|
System.err.println(e.getMessage());
|
193
|
e.printStackTrace(System.err);
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
|
198
|
private void dbg(int position) {
|
199
|
System.err.println("Debug flag: " + position);
|
200
|
}
|
201
|
|
202
|
}
|