1 |
87
|
jones
|
/**
|
2 |
203
|
jones
|
* '$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 |
349
|
jones
|
* Release: @release@
|
9 |
87
|
jones
|
*
|
10 |
203
|
jones
|
* '$Author$'
|
11 |
|
|
* '$Date$'
|
12 |
|
|
* '$Revision$'
|
13 |
669
|
jones
|
*
|
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 |
87
|
jones
|
*/
|
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 |
99
|
jones
|
|
75 |
87
|
jones
|
// 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 |
675
|
berkley
|
pw.println(xsl_system_id + "Error transforming document in " +
|
88 |
|
|
"DBTransform.transformXMLDocument: " +
|
89 |
87
|
jones
|
e.getMessage());
|
90 |
|
|
}
|
91 |
|
|
} else {
|
92 |
|
|
// No stylesheet registered form this document type, so just return the
|
93 |
|
|
// XML stream we were passed
|
94 |
100
|
jones
|
pw.print(doc);
|
95 |
87
|
jones
|
}
|
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 |
94
|
jones
|
"FROM xml_catalog " +
|
115 |
122
|
jones
|
"WHERE entry_type LIKE ? " +
|
116 |
94
|
jones
|
"AND source_doctype LIKE ? " +
|
117 |
|
|
"AND target_doctype LIKE ? ");
|
118 |
87
|
jones
|
// Bind the values to the query
|
119 |
94
|
jones
|
pstmt.setString(1, objecttype);
|
120 |
|
|
pstmt.setString(2, sourcetype);
|
121 |
|
|
pstmt.setString(3, targettype);
|
122 |
87
|
jones
|
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 |
675
|
berkley
|
System.out.println("Error with getString in " +
|
132 |
|
|
"DBTransform.getSystemId: " + e.getMessage());
|
133 |
|
|
}
|
134 |
87
|
jones
|
} else {
|
135 |
|
|
the_system_id = null;
|
136 |
|
|
}
|
137 |
|
|
} catch (SQLException e) {
|
138 |
675
|
berkley
|
System.err.println("Error with next in DBTransform.getSystemId: " +
|
139 |
|
|
e.getMessage());
|
140 |
87
|
jones
|
return ("Error with next: " + e.getMessage());
|
141 |
|
|
}
|
142 |
|
|
} catch (SQLException e) {
|
143 |
675
|
berkley
|
System.err.println("Error with getrset in DBTransform.getSystemId: " +
|
144 |
|
|
e.getMessage());
|
145 |
87
|
jones
|
return ("Error with getrset: " + e.getMessage());
|
146 |
|
|
}
|
147 |
|
|
pstmt.close();
|
148 |
|
|
} catch (SQLException e) {
|
149 |
675
|
berkley
|
System.err.println("Error getting id in DBTransform.getSystemId: " +
|
150 |
|
|
e.getMessage());
|
151 |
|
|
return ("Error getting id in DBTransform.getSystemId:: " +
|
152 |
|
|
e.getMessage());
|
153 |
87
|
jones
|
}
|
154 |
|
|
return the_system_id;
|
155 |
|
|
}
|
156 |
99
|
jones
|
|
157 |
|
|
/**
|
158 |
|
|
* the main routine used to test the transform utility.
|
159 |
|
|
*
|
160 |
184
|
jones
|
* Usage: java DBTransform
|
161 |
99
|
jones
|
*/
|
162 |
|
|
static public void main(String[] args) {
|
163 |
|
|
|
164 |
184
|
jones
|
if (args.length > 0)
|
165 |
99
|
jones
|
{
|
166 |
|
|
System.err.println("Wrong number of arguments!!!");
|
167 |
184
|
jones
|
System.err.println("USAGE: java DBTransform");
|
168 |
99
|
jones
|
return;
|
169 |
|
|
} else {
|
170 |
|
|
try {
|
171 |
|
|
|
172 |
|
|
// Open a connection to the database
|
173 |
184
|
jones
|
MetaCatUtil util = new MetaCatUtil();
|
174 |
|
|
Connection dbconn = util.openDBConnection();
|
175 |
99
|
jones
|
|
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 |
184
|
jones
|
private void dbg(int position) {
|
199 |
99
|
jones
|
System.err.println("Debug flag: " + position);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
87
|
jones
|
}
|