Revision 5752
Added by ben leinfelder over 13 years ago
DBTransform.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package edu.ucsb.nceas.metacat; |
29 | 29 |
|
30 |
import java.io.*; |
|
31 |
import java.sql.*; |
|
30 |
import java.io.File; |
|
31 |
import java.io.FileInputStream; |
|
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.OutputStreamWriter; |
|
35 |
import java.io.StringReader; |
|
36 |
import java.io.StringWriter; |
|
37 |
import java.io.Writer; |
|
38 |
import java.sql.SQLException; |
|
32 | 39 |
import java.util.Enumeration; |
33 | 40 |
import java.util.Hashtable; |
34 | 41 |
import java.util.Iterator; |
... | ... | |
100 | 107 |
*/ |
101 | 108 |
public void transformXMLDocument(String doc, String sourceType, |
102 | 109 |
String targetType, String qformat, |
103 |
PrintWriter pw, Hashtable<String, String[]> param,
|
|
110 |
Writer w, Hashtable<String, String[]> param,
|
|
104 | 111 |
String sessionid) |
105 | 112 |
{ |
106 | 113 |
|
107 |
// Look up the stylesheet for this type combination |
|
108 |
String xslSystemId = getStyleSystemId(qformat, sourceType, targetType); |
|
109 |
if (xslSystemId != null) |
|
110 |
{ |
|
111 |
try |
|
112 |
{// Create a stylesheet from the system id that was found |
|
113 |
doc = removeDOCTYPE(doc); |
|
114 |
StringReader xml = new StringReader(doc); |
|
115 |
StreamResult result = new StreamResult(pw); |
|
116 |
doTransform(xml, result, xslSystemId, param, qformat, sessionid); |
|
117 |
|
|
114 |
String xslSystemId = getStyleSystemId(qformat, sourceType, targetType); |
|
115 |
try { |
|
116 |
// Look up the stylesheet for this type combination |
|
117 |
if (xslSystemId != null) { |
|
118 |
// Create a stylesheet from the system id that was found |
|
119 |
doc = removeDOCTYPE(doc); |
|
120 |
StringReader xml = new StringReader(doc); |
|
121 |
StreamResult result = new StreamResult(w); |
|
122 |
doTransform(xml, result, xslSystemId, param, qformat, sessionid); |
|
123 |
} |
|
124 |
else { |
|
125 |
// No stylesheet registered form this document type, so just return the |
|
126 |
// XML stream we were passed |
|
127 |
w.write(doc); |
|
128 |
} |
|
118 | 129 |
} |
119 | 130 |
catch (Exception e) |
120 | 131 |
{ |
121 |
pw.println(xslSystemId + ": Error transforming document in " + |
|
122 |
"DBTransform.transformXMLDocument: " + |
|
123 |
e.getMessage()); |
|
132 |
try { |
|
133 |
w.write(xslSystemId + ": Error transforming document in " + |
|
134 |
"DBTransform.transformXMLDocument: " + |
|
135 |
e.getMessage()); |
|
136 |
} catch (IOException e1) { |
|
137 |
logMetacat.error(e1.getMessage(), e1); |
|
138 |
} |
|
124 | 139 |
|
125 | 140 |
} |
126 |
} |
|
127 |
else |
|
128 |
{ |
|
129 |
// No stylesheet registered form this document type, so just return the |
|
130 |
// XML stream we were passed |
|
131 |
pw.print(doc); |
|
132 |
} |
|
141 |
|
|
133 | 142 |
} |
134 | 143 |
|
135 | 144 |
/** |
... | ... | |
174 | 183 |
} |
175 | 184 |
} |
176 | 185 |
|
177 |
/** |
|
178 |
* Method to do transform for a string reader |
|
179 |
* @param doc the document to be transformed |
|
180 |
* @param sourcetype the document type of the source |
|
181 |
* @param targettype the target document type |
|
182 |
* @param qformat the name of the style set to use |
|
183 |
* @param pw the PrintWriter to which output is printed |
|
184 |
* @param params some parameters for eml2 transformation |
|
185 |
*/ |
|
186 |
public void transformXMLDocument(StringReader docContent, String sourceType, |
|
187 |
String targetType, String qformat, |
|
188 |
PrintWriter pw, Hashtable<String, String[]> param, |
|
189 |
String sessionid) |
|
190 |
{ |
|
191 |
// Look up the stylesheet for this type combination |
|
192 |
String xslSystemId = getStyleSystemId(qformat, sourceType, targetType); |
|
193 |
if (xslSystemId != null) |
|
194 |
{ |
|
195 |
try |
|
196 |
{// Create a stylesheet from the system id that was found |
|
197 |
StreamResult result = new StreamResult(pw); |
|
198 |
doTransform(docContent, result, xslSystemId, param, qformat, sessionid); |
|
199 | 186 |
|
200 |
} |
|
201 |
catch (Exception e) |
|
202 |
{ |
|
203 |
pw.println(xslSystemId + ": Error transforming document in " + |
|
204 |
"DBTransform.transformXMLDocument: " + |
|
205 |
e.getMessage()); |
|
206 |
|
|
207 |
} |
|
208 |
} |
|
209 |
else |
|
210 |
{ |
|
211 |
// No stylesheet registered form this document type, so just return the |
|
212 |
// XML stream we were passed |
|
213 |
pw.print(docContent); |
|
214 |
} |
|
215 |
} |
|
216 |
|
|
217 | 187 |
/** |
218 | 188 |
* Reads skin's config file if it exists, and populates Transformer paramaters |
219 | 189 |
* with its contents. |
... | ... | |
485 | 455 |
|
486 | 456 |
// Create a test document |
487 | 457 |
StringBuffer testdoc = new StringBuffer(); |
488 |
testdoc.append("<?xml version=\"1.0\"?>"); |
|
458 |
String encoding = "UTF-8"; |
|
459 |
testdoc.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>"); |
|
489 | 460 |
testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>"); |
490 | 461 |
testdoc.append("<dataset_id>DS001</dataset_id>"); |
491 | 462 |
testdoc.append("<title>My test doc</title></eml-dataset>"); |
492 | 463 |
|
493 | 464 |
// Transform the document to the new doctype |
465 |
Writer w = new OutputStreamWriter(System.out, encoding); |
|
494 | 466 |
DBTransform dbt = new DBTransform(); |
495 | 467 |
dbt.transformXMLDocument(testdoc.toString(), |
496 | 468 |
"-//NCEAS//eml-dataset//EN", |
497 | 469 |
"-//W3C//HTML//EN", |
498 | 470 |
"knb", |
499 |
new PrintWriter(System.out), null, null);
|
|
471 |
w, null, null);
|
|
500 | 472 |
|
501 | 473 |
} catch (Exception e) { |
502 | 474 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
Also available in: Unified diff
use detected XML encoding when reading/writing files
use UTF-8 as default when performing queries in the DB (assume DB is using UTF-8)
remove as many PrintWriters (uses system default character encoding only) as possible and construct OutputStreamWriters where explicit encoding can be given.
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=2495