Revision 5025
Added by daigle over 14 years ago
DBTransform.java | ||
---|---|---|
28 | 28 |
package edu.ucsb.nceas.metacat; |
29 | 29 |
|
30 | 30 |
import java.io.*; |
31 |
import java.net.URL; |
|
32 |
import java.net.MalformedURLException; |
|
33 | 31 |
import java.sql.*; |
34 | 32 |
import java.util.Enumeration; |
35 | 33 |
import java.util.Hashtable; |
36 | 34 |
import java.util.Iterator; |
37 | 35 |
import java.util.Map; |
38 |
import java.util.Map.Entry; |
|
39 |
import java.util.Stack; |
|
40 | 36 |
|
41 | 37 |
import javax.xml.transform.TransformerFactory; |
42 | 38 |
import javax.xml.transform.Transformer; |
43 | 39 |
import javax.xml.transform.stream.StreamSource; |
44 | 40 |
import javax.xml.transform.stream.StreamResult; |
45 |
import javax.xml.transform.TransformerException; |
|
46 |
import javax.xml.transform.TransformerConfigurationException; |
|
47 | 41 |
|
48 | 42 |
import org.apache.log4j.Logger; |
49 | 43 |
import org.apache.xerces.parsers.DOMParser; |
50 |
import org.w3c.dom.Attr; |
|
51 | 44 |
import org.w3c.dom.NamedNodeMap; |
52 | 45 |
import org.w3c.dom.NodeList; |
53 | 46 |
import org.w3c.dom.Document; |
54 | 47 |
import org.w3c.dom.Node; |
55 |
import org.w3c.dom.DocumentType; |
|
56 |
import org.xml.sax.SAXException; |
|
57 | 48 |
import org.xml.sax.InputSource; |
58 |
import org.apache.xerces.dom.DocumentTypeImpl; |
|
59 | 49 |
import org.apache.xpath.XPathAPI; |
60 | 50 |
|
61 |
/* |
|
62 |
import oracle.xml.parser.v2.XSLStylesheet; |
|
63 |
import oracle.xml.parser.v2.XSLException; |
|
64 |
import oracle.xml.parser.v2.XMLParseException; |
|
65 |
import oracle.xml.parser.v2.XSLProcessor; |
|
66 |
import oracle.xml.parser.v2.XMLDocument; |
|
67 |
import oracle.xml.parser.v2.DOMParser; |
|
68 |
*/ |
|
69 |
import org.w3c.dom.Document; |
|
70 |
import org.w3c.dom.Node; |
|
71 |
import org.w3c.dom.Element; |
|
72 |
import org.xml.sax.SAXException; |
|
73 |
|
|
74 | 51 |
import edu.ucsb.nceas.metacat.service.PropertyService; |
75 | 52 |
import edu.ucsb.nceas.metacat.service.SkinPropertyService; |
76 |
import edu.ucsb.nceas.metacat.util.MetacatUtil; |
|
77 | 53 |
import edu.ucsb.nceas.metacat.util.SystemUtil; |
78 | 54 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
79 | 55 |
import edu.ucsb.nceas.utilities.SortedProperties; |
80 | 56 |
|
81 |
import java.util.Properties; |
|
82 |
|
|
83 |
|
|
84 | 57 |
/** |
85 | 58 |
* A Class that transforms XML documents utitlizing XSL style sheets |
86 | 59 |
*/ |
87 | 60 |
public class DBTransform { |
88 | 61 |
|
89 |
//private Connection conn = null; |
|
90 |
private MetacatUtil util = null; |
|
62 |
// private Connection conn = null;
|
|
63 |
// private MetacatUtil util = null;
|
|
91 | 64 |
private String configDir = null; |
92 | 65 |
private String defaultStyle = null; |
93 | 66 |
private Logger logMetacat = Logger.getLogger(DBTransform.class); |
94 | 67 |
private String httpServer = null; |
95 | 68 |
private String contextURL = null; |
96 | 69 |
private String servletURL = null; |
70 |
private String webServletURL = null; |
|
97 | 71 |
|
98 | 72 |
/** |
99 | 73 |
* construct a DBTransform instance. |
... | ... | |
113 | 87 |
httpServer = SystemUtil.getServerURL(); |
114 | 88 |
contextURL = SystemUtil.getContextURL(); |
115 | 89 |
servletURL = SystemUtil.getServletURL(); |
90 |
webServletURL = SystemUtil.getWebServletURL(); |
|
116 | 91 |
} |
117 | 92 |
|
118 | 93 |
/** |
119 |
* @see transformXMLDocument(String doc, String sourceType, |
|
120 |
* String targetType, String qformat, PrintWriter pw, |
|
121 |
* String sessionid) |
|
122 |
*/ |
|
123 |
public void transformXMLDocument(String doc, String sourceType, |
|
124 |
String targetType, String qformat, PrintWriter pw, |
|
125 |
Hashtable param) |
|
126 |
{ |
|
127 |
transformXMLDocument(doc, sourceType, targetType, qformat, pw, param, null); |
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* @see transformXMLDocument(String doc, String sourceType, |
|
132 |
* String targetType, String qFormat, StringWriter pw |
|
133 |
* String sessionid) |
|
134 |
*/ |
|
135 |
public void transformXMLDocument(String doc, String sourceType, |
|
136 |
String targetType, String qFormat, StringWriter pw) |
|
137 |
{ |
|
138 |
transformXMLDocument(doc, sourceType, targetType, qFormat, pw, null); |
|
139 |
} |
|
140 |
|
|
141 |
|
|
142 |
/** |
|
143 | 94 |
* Transform an XML document using the stylesheet reference from the db |
144 | 95 |
* |
145 | 96 |
* @param doc the document to be transformed |
... | ... | |
151 | 102 |
*/ |
152 | 103 |
public void transformXMLDocument(String doc, String sourceType, |
153 | 104 |
String targetType, String qformat, |
154 |
PrintWriter pw, Hashtable param, |
|
105 |
PrintWriter pw, Hashtable<String, String[]> param,
|
|
155 | 106 |
String sessionid) |
156 | 107 |
{ |
157 | 108 |
|
... | ... | |
182 | 133 |
pw.print(doc); |
183 | 134 |
} |
184 | 135 |
} |
185 |
|
|
136 |
|
|
186 | 137 |
/** |
187 | 138 |
* Transform an XML document to StringWriter using the stylesheet reference |
188 | 139 |
* from the db |
... | ... | |
194 | 145 |
*/ |
195 | 146 |
public void transformXMLDocument(String doc, String sourceType, |
196 | 147 |
String targetType, String qFormat, StringWriter pw, |
197 |
String sessionid) |
|
148 |
Hashtable<String, String[]> param, String sessionid)
|
|
198 | 149 |
{ |
199 | 150 |
|
200 | 151 |
// Look up the stylesheet for this type combination |
... | ... | |
207 | 158 |
doc = removeDOCTYPE(doc); |
208 | 159 |
StringReader xml = new StringReader(doc); |
209 | 160 |
StreamResult result = new StreamResult(pw); |
210 |
doTransform(xml, result, xslSystemId, null, qFormat, sessionid);
|
|
161 |
doTransform(xml, result, xslSystemId, param, qFormat, sessionid);
|
|
211 | 162 |
} |
212 | 163 |
catch (Exception e) |
213 | 164 |
{ |
... | ... | |
236 | 187 |
*/ |
237 | 188 |
public void transformXMLDocument(StringReader docContent, String sourceType, |
238 | 189 |
String targetType, String qformat, |
239 |
PrintWriter pw, Hashtable param, |
|
190 |
PrintWriter pw, Hashtable<String, String[]> param,
|
|
240 | 191 |
String sessionid) |
241 | 192 |
{ |
242 | 193 |
// Look up the stylesheet for this type combination |
... | ... | |
274 | 225 |
private void doTransform(StringReader docContent, |
275 | 226 |
StreamResult resultOutput, |
276 | 227 |
String xslSystemId, |
277 |
Hashtable param, |
|
228 |
Hashtable<String, String[]> param,
|
|
278 | 229 |
String qformat, |
279 | 230 |
String sessionid) |
280 | 231 |
throws Exception { |
... | ... | |
284 | 235 |
Transformer transformer; |
285 | 236 |
String key, value; |
286 | 237 |
StreamSource xml; |
287 |
Enumeration en; |
|
288 |
Iterator iterIt; |
|
289 |
Map.Entry entry; |
|
238 |
Enumeration<String> en;
|
|
239 |
Iterator<Map.Entry<String, String>> iterIt;
|
|
240 |
Map.Entry<String, String> entry;
|
|
290 | 241 |
|
291 | 242 |
if (xslSystemId != null) { |
292 | 243 |
tFactory = TransformerFactory.newInstance(); |
... | ... | |
299 | 250 |
if (skinOptions != null) { |
300 | 251 |
iterIt = skinOptions.getProperties().entrySet().iterator(); |
301 | 252 |
while (iterIt.hasNext()) { |
302 |
entry = (Entry) iterIt.next();
|
|
303 |
key = (String) entry.getKey();
|
|
304 |
value = (String) entry.getValue();
|
|
253 |
entry = iterIt.next(); |
|
254 |
key = entry.getKey(); |
|
255 |
value = entry.getValue(); |
|
305 | 256 |
//only include the plain properties |
306 | 257 |
if (key.indexOf('.') == -1) { |
307 | 258 |
transformer.setParameter(key, value); |
... | ... | |
318 | 269 |
logMetacat.debug("cgi-prefix=" + cgiPrefix); |
319 | 270 |
logMetacat.debug("httpServer=" + httpServer); |
320 | 271 |
logMetacat.debug("contextURL=" + contextURL); |
321 |
logMetacat.debug("serletPath=" + servletURL); |
|
272 |
logMetacat.debug("serletURL=" + servletURL); |
|
273 |
logMetacat.debug("webServletURL=" + webServletURL); |
|
322 | 274 |
transformer.setParameter("cgi-prefix", cgiPrefix); |
323 | 275 |
transformer.setParameter("httpServer", httpServer); |
324 | 276 |
transformer.setParameter("contextURL", contextURL); |
325 | 277 |
transformer.setParameter("servletURL", servletURL); |
278 |
transformer.setParameter("webServletURL", webServletURL); |
|
326 | 279 |
|
327 | 280 |
// Set up parameter for transformation |
328 | 281 |
if ( param != null) { |
329 | 282 |
en = param.keys(); |
330 | 283 |
while (en.hasMoreElements()) { |
331 |
key = (String) en.nextElement();
|
|
332 |
value = ((String[]) (param.get(key)))[0];
|
|
284 |
key = en.nextElement(); |
|
285 |
value = (param.get(key))[0];
|
|
333 | 286 |
logMetacat.info(key+" : "+value); |
334 | 287 |
transformer.setParameter(key, value); |
335 | 288 |
} |
... | ... | |
483 | 436 |
return systemId; |
484 | 437 |
} |
485 | 438 |
|
486 |
/* Method to modified the system id of xml input -- make sure it |
|
487 |
points to system id in xml_catalog table |
|
488 |
*/ |
|
489 |
private void modifiedXmlStreamSource(StreamSource xml, String publicId) |
|
490 |
throws Exception |
|
491 |
{ |
|
492 |
// make sure the xml is not null |
|
493 |
if (xml == null || publicId == null) |
|
494 |
{ |
|
495 |
return; |
|
496 |
} |
|
497 |
logMetacat.info("public id of input stream is " +publicId); |
|
498 |
// Get system id from xml_catalog table |
|
499 |
String systemId = DBEntityResolver.getDTDSystemID(publicId); |
|
500 |
logMetacat.info("system id of input stream from xml_catalog" |
|
501 |
+"table is " +systemId); |
|
502 |
//set system id to input stream |
|
503 |
xml.setSystemId(systemId); |
|
504 |
} |
|
439 |
// /* Method to modified the system id of xml input -- make sure it
|
|
440 |
// points to system id in xml_catalog table
|
|
441 |
// */
|
|
442 |
// private void modifiedXmlStreamSource(StreamSource xml, String publicId)
|
|
443 |
// throws Exception
|
|
444 |
// {
|
|
445 |
// // make sure the xml is not null
|
|
446 |
// if (xml == null || publicId == null)
|
|
447 |
// {
|
|
448 |
// return;
|
|
449 |
// }
|
|
450 |
// logMetacat.info("public id of input stream is " +publicId);
|
|
451 |
// // Get system id from xml_catalog table
|
|
452 |
// String systemId = DBEntityResolver.getDTDSystemID(publicId);
|
|
453 |
// logMetacat.info("system id of input stream from xml_catalog"
|
|
454 |
// +"table is " +systemId);
|
|
455 |
// //set system id to input stream
|
|
456 |
// xml.setSystemId(systemId);
|
|
457 |
// }
|
|
505 | 458 |
|
506 | 459 |
/* |
507 | 460 |
* removes the DOCTYPE element and its contents from a Sting |
... | ... | |
547 | 500 |
"-//NCEAS//eml-dataset//EN", |
548 | 501 |
"-//W3C//HTML//EN", |
549 | 502 |
"knb", |
550 |
new PrintWriter(System.out), null); |
|
503 |
new PrintWriter(System.out), null, null);
|
|
551 | 504 |
|
552 | 505 |
} catch (Exception e) { |
553 | 506 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
... | ... | |
557 | 510 |
} |
558 | 511 |
} |
559 | 512 |
|
560 |
private void dbg(int position) { |
|
561 |
System.err.println("Debug flag: " + position); |
|
562 |
} |
|
513 |
// private void dbg(int position) {
|
|
514 |
// System.err.println("Debug flag: " + position);
|
|
515 |
// }
|
|
563 | 516 |
|
564 | 517 |
} |
Also available in: Unified diff
Move document specific utilities to DocumentUtil from MetacatUtil. This makes it easier to define a layer between the core metacat services and the rest of the code.