1
|
/**
|
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: DBTransform.java 87 2000-05-09 01:51:20Z jones $'
|
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
|
private Connection conn = null;
|
32
|
|
33
|
/**
|
34
|
* construct a DBTransform instance.
|
35
|
*
|
36
|
* Generally, one calls transformXMLDocument() after constructing the instance
|
37
|
*
|
38
|
* @param conn the database connection from which to lookup the public ids
|
39
|
*/
|
40
|
public DBTransform( Connection conn )
|
41
|
throws IOException,
|
42
|
SQLException,
|
43
|
ClassNotFoundException
|
44
|
{
|
45
|
this.conn = conn;
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* Transform an XML document using the stylesheet reference from the db
|
50
|
*
|
51
|
* @param doc the document to be transformed
|
52
|
* @param sourcetype the document type of the source
|
53
|
* @param targettype the target document type
|
54
|
*/
|
55
|
public void transformXMLDocument(String doc, String sourcetype,
|
56
|
String targettype, PrintWriter pw) {
|
57
|
|
58
|
// Look up the stylesheet for this type combination
|
59
|
String xsl_system_id = getSystemId("XSL", sourcetype, targettype);
|
60
|
|
61
|
if (xsl_system_id != null) {
|
62
|
// Create a stylesheet from the system id that was found
|
63
|
try {
|
64
|
XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
|
65
|
DOMParser dp = new DOMParser();
|
66
|
dp.setValidationMode(false);
|
67
|
dp.parse((Reader)(new StringReader(doc)));
|
68
|
new XSLProcessor().processXSL(style, dp.getDocument(), pw);
|
69
|
} catch (Exception e) {
|
70
|
pw.println(xsl_system_id + "Error transforming document:\n" +
|
71
|
e.getMessage());
|
72
|
}
|
73
|
} else {
|
74
|
// No stylesheet registered form this document type, so just return the
|
75
|
// XML stream we were passed
|
76
|
pw.print(doc);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Lookup a stylesheet reference from the db catalog
|
82
|
*
|
83
|
* @param objecttype the type of the object we want to retrieve
|
84
|
* @param sourcetype the document type of the source
|
85
|
* @param targettype the document type of the target
|
86
|
*/
|
87
|
public String getSystemId(String objecttype, String sourcetype,
|
88
|
String targettype) {
|
89
|
|
90
|
// Look up the System ID of a particular object
|
91
|
PreparedStatement pstmt;
|
92
|
String the_system_id = null;
|
93
|
|
94
|
try {
|
95
|
pstmt =
|
96
|
conn.prepareStatement("SELECT system_id " +
|
97
|
"FROM xml_catalog_entities " +
|
98
|
"WHERE source_doctype LIKE ? " +
|
99
|
"AND target_doctype LIKE ? " +
|
100
|
"AND entity_type LIKE ?");
|
101
|
// Bind the values to the query
|
102
|
pstmt.setString(1, sourcetype);
|
103
|
pstmt.setString(2, targettype);
|
104
|
pstmt.setString(3, objecttype);
|
105
|
|
106
|
pstmt.execute();
|
107
|
try {
|
108
|
ResultSet rs = pstmt.getResultSet();
|
109
|
try {
|
110
|
boolean tableHasRows = rs.next();
|
111
|
if (tableHasRows) {
|
112
|
try {
|
113
|
the_system_id = rs.getString(1);
|
114
|
} catch (SQLException e) {
|
115
|
System.out.println("Error with getString: " + e.getMessage()); }
|
116
|
} else {
|
117
|
the_system_id = null;
|
118
|
}
|
119
|
} catch (SQLException e) {
|
120
|
//System.out.println("Error with next: " + e.getMessage());
|
121
|
return ("Error with next: " + e.getMessage());
|
122
|
}
|
123
|
} catch (SQLException e) {
|
124
|
//System.out.println("Error with getrset: " + e.getMessage());
|
125
|
return ("Error with getrset: " + e.getMessage());
|
126
|
}
|
127
|
pstmt.close();
|
128
|
} catch (SQLException e) {
|
129
|
//System.out.println("Error getting id: " + e.getMessage());
|
130
|
return ("Error getting id: " + e.getMessage());
|
131
|
}
|
132
|
return the_system_id;
|
133
|
}
|
134
|
}
|