1 |
87
|
jones
|
/**
|
2 |
|
|
* Name: DBTransform.java
|
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 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
package edu.ucsb.nceas.metacat;
|
13 |
|
|
|
14 |
|
|
import java.io.*;
|
15 |
|
|
import java.net.URL;
|
16 |
|
|
import java.net.MalformedURLException;
|
17 |
|
|
import java.sql.*;
|
18 |
|
|
import java.util.Stack;
|
19 |
|
|
|
20 |
|
|
import oracle.xml.parser.v2.XSLStylesheet;
|
21 |
|
|
import oracle.xml.parser.v2.XSLException;
|
22 |
|
|
import oracle.xml.parser.v2.XSLProcessor;
|
23 |
|
|
import oracle.xml.parser.v2.XMLDocument;
|
24 |
|
|
import oracle.xml.parser.v2.DOMParser;
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* A Class that transforms XML documents utitlizing XSL style sheets
|
28 |
|
|
*/
|
29 |
|
|
public class DBTransform {
|
30 |
|
|
|
31 |
99
|
jones
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
32 |
87
|
jones
|
private Connection conn = null;
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* construct a DBTransform instance.
|
36 |
|
|
*
|
37 |
|
|
* Generally, one calls transformXMLDocument() after constructing the instance
|
38 |
|
|
*
|
39 |
|
|
* @param conn the database connection from which to lookup the public ids
|
40 |
|
|
*/
|
41 |
|
|
public DBTransform( Connection conn )
|
42 |
|
|
throws IOException,
|
43 |
|
|
SQLException,
|
44 |
|
|
ClassNotFoundException
|
45 |
|
|
{
|
46 |
|
|
this.conn = conn;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Transform an XML document using the stylesheet reference from the db
|
51 |
|
|
*
|
52 |
|
|
* @param doc the document to be transformed
|
53 |
|
|
* @param sourcetype the document type of the source
|
54 |
|
|
* @param targettype the target document type
|
55 |
|
|
*/
|
56 |
|
|
public void transformXMLDocument(String doc, String sourcetype,
|
57 |
|
|
String targettype, PrintWriter pw) {
|
58 |
99
|
jones
|
|
59 |
87
|
jones
|
// Look up the stylesheet for this type combination
|
60 |
|
|
String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
|
61 |
|
|
|
62 |
|
|
if (xsl_system_id != null) {
|
63 |
|
|
// Create a stylesheet from the system id that was found
|
64 |
|
|
try {
|
65 |
|
|
XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
|
66 |
|
|
DOMParser dp = new DOMParser();
|
67 |
|
|
dp.setValidationMode(false);
|
68 |
|
|
dp.parse((Reader)(new StringReader(doc)));
|
69 |
|
|
new XSLProcessor().processXSL(style, dp.getDocument(), pw);
|
70 |
|
|
} catch (Exception e) {
|
71 |
|
|
pw.println(xsl_system_id + "Error transforming document:\n" +
|
72 |
|
|
e.getMessage());
|
73 |
|
|
}
|
74 |
|
|
} else {
|
75 |
|
|
// No stylesheet registered form this document type, so just return the
|
76 |
|
|
// XML stream we were passed
|
77 |
100
|
jones
|
pw.print(doc);
|
78 |
87
|
jones
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Lookup a stylesheet reference from the db catalog
|
83 |
|
|
*
|
84 |
|
|
* @param objecttype the type of the object we want to retrieve
|
85 |
|
|
* @param sourcetype the document type of the source
|
86 |
|
|
* @param targettype the document type of the target
|
87 |
|
|
*/
|
88 |
|
|
public String getSystemId(String objecttype, String sourcetype,
|
89 |
|
|
String targettype) {
|
90 |
|
|
|
91 |
|
|
// Look up the System ID of a particular object
|
92 |
|
|
PreparedStatement pstmt;
|
93 |
|
|
String the_system_id = null;
|
94 |
|
|
try {
|
95 |
|
|
pstmt =
|
96 |
|
|
conn.prepareStatement("SELECT system_id " +
|
97 |
94
|
jones
|
"FROM xml_catalog " +
|
98 |
|
|
"WHERE entity_type LIKE ? " +
|
99 |
|
|
"AND source_doctype LIKE ? " +
|
100 |
|
|
"AND target_doctype LIKE ? ");
|
101 |
87
|
jones
|
// Bind the values to the query
|
102 |
94
|
jones
|
pstmt.setString(1, objecttype);
|
103 |
|
|
pstmt.setString(2, sourcetype);
|
104 |
|
|
pstmt.setString(3, targettype);
|
105 |
87
|
jones
|
pstmt.execute();
|
106 |
|
|
try {
|
107 |
|
|
ResultSet rs = pstmt.getResultSet();
|
108 |
|
|
try {
|
109 |
|
|
boolean tableHasRows = rs.next();
|
110 |
|
|
if (tableHasRows) {
|
111 |
|
|
try {
|
112 |
|
|
the_system_id = rs.getString(1);
|
113 |
|
|
} catch (SQLException e) {
|
114 |
|
|
System.out.println("Error with getString: " + e.getMessage()); }
|
115 |
|
|
} else {
|
116 |
|
|
the_system_id = null;
|
117 |
|
|
}
|
118 |
|
|
} catch (SQLException e) {
|
119 |
99
|
jones
|
System.err.println("Error with next: " + e.getMessage());
|
120 |
87
|
jones
|
return ("Error with next: " + e.getMessage());
|
121 |
|
|
}
|
122 |
|
|
} catch (SQLException e) {
|
123 |
99
|
jones
|
System.err.println("Error with getrset: " + e.getMessage());
|
124 |
87
|
jones
|
return ("Error with getrset: " + e.getMessage());
|
125 |
|
|
}
|
126 |
|
|
pstmt.close();
|
127 |
|
|
} catch (SQLException e) {
|
128 |
99
|
jones
|
System.err.println("Error getting id: " + e.getMessage());
|
129 |
87
|
jones
|
return ("Error getting id: " + e.getMessage());
|
130 |
|
|
}
|
131 |
|
|
return the_system_id;
|
132 |
|
|
}
|
133 |
99
|
jones
|
|
134 |
|
|
/**
|
135 |
|
|
* the main routine used to test the transform utility.
|
136 |
|
|
*
|
137 |
|
|
* Usage: java transform <user> <password> [dbstring]
|
138 |
|
|
*
|
139 |
|
|
* @param user the username to use for the database connection
|
140 |
|
|
* @param password the password to use for the database connection
|
141 |
|
|
* @param dbstring the connection info to use for the database connection
|
142 |
|
|
*/
|
143 |
|
|
static public void main(String[] args) {
|
144 |
|
|
|
145 |
|
|
if (args.length < 2)
|
146 |
|
|
{
|
147 |
|
|
System.err.println("Wrong number of arguments!!!");
|
148 |
|
|
System.err.println("USAGE: java transform " +
|
149 |
|
|
"<user> <password> [dbstring]");
|
150 |
|
|
return;
|
151 |
|
|
} else {
|
152 |
|
|
try {
|
153 |
|
|
|
154 |
|
|
String user = args[0];
|
155 |
|
|
String password = args[1];
|
156 |
|
|
String dbstring = null;
|
157 |
|
|
|
158 |
|
|
if (args.length <= 2) {
|
159 |
|
|
dbstring = defaultDB;
|
160 |
|
|
} else {
|
161 |
|
|
dbstring = args[2];
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
// Open a connection to the database
|
165 |
|
|
Connection dbconn = MetaCatUtil.openDBConnection(
|
166 |
|
|
"oracle.jdbc.driver.OracleDriver",
|
167 |
|
|
dbstring, user, password);
|
168 |
|
|
|
169 |
|
|
// Create a test document
|
170 |
|
|
StringBuffer testdoc = new StringBuffer();
|
171 |
|
|
testdoc.append("<?xml version=\"1.0\"?>");
|
172 |
|
|
testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
|
173 |
|
|
testdoc.append("<dataset_id>DS001</dataset_id>");
|
174 |
|
|
testdoc.append("<title>My test doc</title></eml-dataset>");
|
175 |
|
|
|
176 |
|
|
// Transform the document to the new doctype
|
177 |
|
|
DBTransform dbt = new DBTransform(dbconn);
|
178 |
|
|
dbt.transformXMLDocument(testdoc.toString(),
|
179 |
|
|
"-//NCEAS//eml-dataset//EN",
|
180 |
|
|
"-//W3C//HTML//EN",
|
181 |
|
|
new PrintWriter(System.out));
|
182 |
|
|
|
183 |
|
|
} catch (Exception e) {
|
184 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
185 |
|
|
System.err.println(e.getMessage());
|
186 |
|
|
e.printStackTrace(System.err);
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
public void dbg(int position) {
|
192 |
|
|
System.err.println("Debug flag: " + position);
|
193 |
|
|
}
|
194 |
|
|
|
195 |
87
|
jones
|
}
|