1
|
/**
|
2
|
* '$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
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: harris $'
|
11
|
* '$Date: 2006-02-20 19:41:34 -0800 (Mon, 20 Feb 2006) $'
|
12
|
* '$Revision: 2912 $'
|
13
|
*
|
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
|
*/
|
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.Enumeration;
|
36
|
import java.util.Hashtable;
|
37
|
import java.util.Stack;
|
38
|
|
39
|
import javax.xml.transform.TransformerFactory;
|
40
|
import javax.xml.transform.Transformer;
|
41
|
import javax.xml.transform.stream.StreamSource;
|
42
|
import javax.xml.transform.stream.StreamResult;
|
43
|
import javax.xml.transform.TransformerException;
|
44
|
import javax.xml.transform.TransformerConfigurationException;
|
45
|
|
46
|
import org.apache.log4j.Logger;
|
47
|
import org.apache.xerces.parsers.DOMParser;
|
48
|
import org.w3c.dom.Attr;
|
49
|
import org.w3c.dom.NamedNodeMap;
|
50
|
import org.w3c.dom.NodeList;
|
51
|
import org.w3c.dom.Document;
|
52
|
import org.w3c.dom.Node;
|
53
|
import org.w3c.dom.NodeList;
|
54
|
import org.w3c.dom.DocumentType;
|
55
|
import org.xml.sax.SAXException;
|
56
|
import org.xml.sax.InputSource;
|
57
|
import org.apache.xerces.dom.DocumentTypeImpl;
|
58
|
import org.apache.xpath.XPathAPI;
|
59
|
import org.w3c.dom.NamedNodeMap;
|
60
|
|
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
|
import java.util.Properties;
|
75
|
|
76
|
|
77
|
/**
|
78
|
* A Class that transforms XML documents utitlizing XSL style sheets
|
79
|
*/
|
80
|
public class DBTransform {
|
81
|
|
82
|
//private Connection conn = null;
|
83
|
private MetaCatUtil util = null;
|
84
|
private String configDir = null;
|
85
|
private String defaultStyle = null;
|
86
|
private Logger logMetacat = Logger.getLogger(DBTransform.class);
|
87
|
|
88
|
/**
|
89
|
* construct a DBTransform instance.
|
90
|
*
|
91
|
* Generally, one calls transformXMLDocument() after constructing the instance
|
92
|
*
|
93
|
* @param conn the database connection from which to lookup the public ids
|
94
|
*/
|
95
|
public DBTransform()
|
96
|
throws IOException,
|
97
|
SQLException,
|
98
|
ClassNotFoundException
|
99
|
{
|
100
|
//this.conn = conn;
|
101
|
util = new MetaCatUtil();
|
102
|
configDir = util.getOption("config-dir");
|
103
|
defaultStyle = util.getOption("default-style");
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* @see transformXMLDocument(String doc, String sourceType,
|
108
|
* String targetType, String qformat, PrintWriter pw,
|
109
|
* String sessionid)
|
110
|
*/
|
111
|
public void transformXMLDocument(String doc, String sourceType,
|
112
|
String targetType, String qformat, PrintWriter pw,
|
113
|
Hashtable param)
|
114
|
{
|
115
|
transformXMLDocument(doc, sourceType, targetType, qformat, pw, param, null);
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* @see transformXMLDocument(String doc, String sourceType,
|
120
|
* String targetType, String qFormat, StringWriter pw
|
121
|
* String sessionid)
|
122
|
*/
|
123
|
public void transformXMLDocument(String doc, String sourceType,
|
124
|
String targetType, String qFormat, StringWriter pw)
|
125
|
{
|
126
|
transformXMLDocument(doc, sourceType, targetType, qFormat, pw, null);
|
127
|
}
|
128
|
|
129
|
|
130
|
/**
|
131
|
* Transform an XML document using the stylesheet reference from the db
|
132
|
*
|
133
|
* @param doc the document to be transformed
|
134
|
* @param sourcetype the document type of the source
|
135
|
* @param targettype the target document type
|
136
|
* @param qformat the name of the style set to use
|
137
|
* @param pw the PrintWriter to which output is printed
|
138
|
* @param params some parameters for eml2 transformation
|
139
|
*/
|
140
|
public void transformXMLDocument(String doc, String sourceType,
|
141
|
String targetType, String qformat,
|
142
|
PrintWriter pw, Hashtable param,
|
143
|
String sessionid)
|
144
|
{
|
145
|
|
146
|
// Look up the stylesheet for this type combination
|
147
|
String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
|
148
|
if (xslSystemId != null)
|
149
|
{
|
150
|
try
|
151
|
{// Create a stylesheet from the system id that was found
|
152
|
doc = removeDOCTYPE(doc);
|
153
|
StringReader xml = new StringReader(doc);
|
154
|
StreamResult result = new StreamResult(pw);
|
155
|
doTransform(xml, result, xslSystemId, param, qformat, sessionid);
|
156
|
|
157
|
}
|
158
|
catch (Exception e)
|
159
|
{
|
160
|
pw.println(xslSystemId + "Error transforming document in " +
|
161
|
"DBTransform.transformXMLDocument: " +
|
162
|
e.getMessage());
|
163
|
|
164
|
}
|
165
|
}
|
166
|
else
|
167
|
{
|
168
|
// No stylesheet registered form this document type, so just return the
|
169
|
// XML stream we were passed
|
170
|
pw.print(doc);
|
171
|
}
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Transform an XML document to StringWriter using the stylesheet reference
|
176
|
* from the db
|
177
|
* @param doc the document to be transformed
|
178
|
* @param sourceType the document type of the source
|
179
|
* @param targetType the target document type
|
180
|
* @param qFormat the name of the style set to use
|
181
|
* @param pw the StringWriter to which output will be stored
|
182
|
*/
|
183
|
public void transformXMLDocument(String doc, String sourceType,
|
184
|
String targetType, String qFormat, StringWriter pw,
|
185
|
String sessionid)
|
186
|
{
|
187
|
|
188
|
// Look up the stylesheet for this type combination
|
189
|
String xslSystemId = getStyleSystemId(qFormat, sourceType, targetType);
|
190
|
if (xslSystemId != null)
|
191
|
{
|
192
|
// Create a stylesheet from the system id that was found
|
193
|
try
|
194
|
{
|
195
|
doc = removeDOCTYPE(doc);
|
196
|
StringReader xml = new StringReader(doc);
|
197
|
StreamResult result = new StreamResult(pw);
|
198
|
doTransform(xml, result, xslSystemId, null, qFormat, sessionid);
|
199
|
}
|
200
|
catch (Exception e)
|
201
|
{
|
202
|
logMetacat.error(xslSystemId + "Error transforming document in " +
|
203
|
"DBTransform.transformXMLDocument: " +
|
204
|
e.getMessage());
|
205
|
|
206
|
}
|
207
|
}
|
208
|
else
|
209
|
{
|
210
|
// No stylesheet registered form this document type, so just return the
|
211
|
// XML stream we were passed
|
212
|
pw.write(doc);
|
213
|
}
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Method to do transform for a string reader
|
218
|
* @param doc the document to be transformed
|
219
|
* @param sourcetype the document type of the source
|
220
|
* @param targettype the target document type
|
221
|
* @param qformat the name of the style set to use
|
222
|
* @param pw the PrintWriter to which output is printed
|
223
|
* @param params some parameters for eml2 transformation
|
224
|
*/
|
225
|
public void transformXMLDocument(StringReader docContent, String sourceType,
|
226
|
String targetType, String qformat,
|
227
|
PrintWriter pw, Hashtable param,
|
228
|
String sessionid)
|
229
|
{
|
230
|
// Look up the stylesheet for this type combination
|
231
|
String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
|
232
|
if (xslSystemId != null)
|
233
|
{
|
234
|
try
|
235
|
{// Create a stylesheet from the system id that was found
|
236
|
StreamResult result = new StreamResult(pw);
|
237
|
doTransform(docContent, result, xslSystemId, param, qformat, sessionid);
|
238
|
|
239
|
}
|
240
|
catch (Exception e)
|
241
|
{
|
242
|
pw.println(xslSystemId + "Error transforming document in " +
|
243
|
"DBTransform.transformXMLDocument: " +
|
244
|
e.getMessage());
|
245
|
|
246
|
}
|
247
|
}
|
248
|
else
|
249
|
{
|
250
|
// No stylesheet registered form this document type, so just return the
|
251
|
// XML stream we were passed
|
252
|
pw.print(docContent);
|
253
|
}
|
254
|
}
|
255
|
|
256
|
/*
|
257
|
* Method to do transfer
|
258
|
*/
|
259
|
private void doTransform(StringReader docContent, StreamResult resultOutput,
|
260
|
String xslSystemId, Hashtable param,
|
261
|
String qformat, String sessionid)
|
262
|
throws Exception
|
263
|
{
|
264
|
if (xslSystemId != null)
|
265
|
{
|
266
|
TransformerFactory tFactory = TransformerFactory.newInstance();
|
267
|
Transformer transformer = tFactory.newTransformer(
|
268
|
new StreamSource(xslSystemId));
|
269
|
transformer.setParameter("qformat", qformat);
|
270
|
logMetacat.warn("qformat: "+qformat);
|
271
|
|
272
|
if (MetaCatUtil.skinconfigs.containsKey(qformat)){
|
273
|
Properties skinOptions = (Properties)MetaCatUtil.skinconfigs.get(qformat);
|
274
|
|
275
|
if(skinOptions.getProperty("lsidauthority") != null){
|
276
|
transformer.setParameter("lsidauthority", skinOptions.getProperty("lsidauthority"));
|
277
|
}
|
278
|
|
279
|
if(skinOptions.getProperty("registryurl") != null){
|
280
|
transformer.setParameter("registryurl", skinOptions.getProperty("registryurl"));
|
281
|
}
|
282
|
|
283
|
if(skinOptions.getProperty("registryname") != null){
|
284
|
transformer.setParameter("registryname", skinOptions.getProperty("registryname"));
|
285
|
}
|
286
|
}
|
287
|
|
288
|
if(sessionid != null)
|
289
|
{
|
290
|
transformer.setParameter("sessid", sessionid);
|
291
|
}
|
292
|
// Set up parameter for transformation
|
293
|
if ( param != null)
|
294
|
{
|
295
|
Enumeration en = param.keys();
|
296
|
while (en.hasMoreElements())
|
297
|
{
|
298
|
String key =(String)en.nextElement();
|
299
|
String value = ((String[])(param.get(key)))[0];
|
300
|
logMetacat.info(key+" : "+value);
|
301
|
transformer.setParameter(key, value);
|
302
|
}
|
303
|
}
|
304
|
StreamSource xml = new StreamSource(docContent);
|
305
|
transformer.transform(xml,resultOutput);
|
306
|
}
|
307
|
}//doTransform
|
308
|
|
309
|
|
310
|
/**
|
311
|
* gets the content of a tag in a given xml file with the given path
|
312
|
* @param f the file to parse
|
313
|
* @param path the path to get the content from
|
314
|
*/
|
315
|
public static NodeList getPathContent(File f, String path)
|
316
|
{
|
317
|
if(f == null)
|
318
|
{
|
319
|
return null;
|
320
|
}
|
321
|
|
322
|
DOMParser parser = new DOMParser();
|
323
|
InputSource in;
|
324
|
FileInputStream fs;
|
325
|
|
326
|
try
|
327
|
{
|
328
|
fs = new FileInputStream(f);
|
329
|
in = new InputSource(fs);
|
330
|
}
|
331
|
catch(FileNotFoundException fnf)
|
332
|
{
|
333
|
fnf.printStackTrace();
|
334
|
return null;
|
335
|
}
|
336
|
|
337
|
try
|
338
|
{
|
339
|
parser.parse(in);
|
340
|
fs.close();
|
341
|
}
|
342
|
catch(Exception e1)
|
343
|
{
|
344
|
System.err.println("File: " + f.getPath() + " : parse threw: " +
|
345
|
e1.toString());
|
346
|
return null;
|
347
|
}
|
348
|
|
349
|
Document doc = parser.getDocument();
|
350
|
|
351
|
try
|
352
|
{
|
353
|
NodeList docNodeList = XPathAPI.selectNodeList(doc, path);
|
354
|
return docNodeList;
|
355
|
}
|
356
|
catch(Exception se)
|
357
|
{
|
358
|
System.err.println("file: " + f.getPath() + " : parse threw: " +
|
359
|
se.toString());
|
360
|
return null;
|
361
|
}
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
* Lookup a stylesheet reference from the db catalog
|
366
|
*
|
367
|
* @param qformat the named style-set format
|
368
|
* @param sourcetype the document type of the source
|
369
|
* @param targettype the document type of the target
|
370
|
*/
|
371
|
public String getStyleSystemId(String qformat, String sourcetype,
|
372
|
String targettype) {
|
373
|
String systemId = null;
|
374
|
|
375
|
if ((qformat == null) || (qformat.equals("html"))) {
|
376
|
qformat = defaultStyle;
|
377
|
}
|
378
|
|
379
|
// Load the style-set map for this qformat into a DOM
|
380
|
try {
|
381
|
boolean breakflag = false;
|
382
|
String filename = configDir + "/" + qformat + "/" + qformat + ".xml";
|
383
|
logMetacat.warn("Trying style-set file: " + filename);
|
384
|
File f = new File(filename);
|
385
|
NodeList nlDoctype = getPathContent(f, "/style-set/doctype");
|
386
|
NodeList nlDefault = getPathContent(f, "/style-set/default-style");
|
387
|
Node nDefault = nlDefault.item(0);
|
388
|
systemId = nDefault.getFirstChild().getNodeValue(); //set the default
|
389
|
|
390
|
for(int i=0; i<nlDoctype.getLength(); i++)
|
391
|
{ //look for the right sourcetype
|
392
|
Node nDoctype = nlDoctype.item(i);
|
393
|
NamedNodeMap atts = nDoctype.getAttributes();
|
394
|
Node nAtt = atts.getNamedItem("publicid");
|
395
|
String doctype = nAtt.getFirstChild().getNodeValue();
|
396
|
if(doctype.equals(sourcetype))
|
397
|
{ //found the right sourcetype now we need to get the target type
|
398
|
NodeList nlChildren = nDoctype.getChildNodes();
|
399
|
for(int j=0; j<nlChildren.getLength(); j++)
|
400
|
{
|
401
|
Node nChild = nlChildren.item(j);
|
402
|
String childName = nChild.getNodeName();
|
403
|
if(childName.equals("target"))
|
404
|
{
|
405
|
NamedNodeMap childAtts = nChild.getAttributes();
|
406
|
Node nTargetPublicId = childAtts.getNamedItem("publicid");
|
407
|
String target = nTargetPublicId.getFirstChild().getNodeValue();
|
408
|
if(target.equals(targettype))
|
409
|
{ //we found the right target type
|
410
|
NodeList nlTarget = nChild.getChildNodes();
|
411
|
for(int k=0; k<nlTarget.getLength(); k++)
|
412
|
{
|
413
|
Node nChildText = nlTarget.item(k);
|
414
|
if(nChildText.getNodeType() == Node.TEXT_NODE)
|
415
|
{ //get the text from the target node
|
416
|
systemId = nChildText.getNodeValue();
|
417
|
breakflag = true;
|
418
|
break;
|
419
|
}
|
420
|
}
|
421
|
}
|
422
|
}
|
423
|
|
424
|
if(breakflag)
|
425
|
{
|
426
|
break;
|
427
|
}
|
428
|
}
|
429
|
}
|
430
|
|
431
|
if(breakflag)
|
432
|
{
|
433
|
break;
|
434
|
}
|
435
|
}
|
436
|
}
|
437
|
catch(Exception e)
|
438
|
{
|
439
|
System.out.println("Error parsing style-set file: " + e.getMessage());
|
440
|
e.printStackTrace();
|
441
|
}
|
442
|
|
443
|
// Return the system ID for this particular source document type
|
444
|
logMetacat.info("style system id is: "+systemId);
|
445
|
return systemId;
|
446
|
}
|
447
|
|
448
|
/* Method to modified the system id of xml input -- make sure it
|
449
|
points to system id in xml_catalog table
|
450
|
*/
|
451
|
private void modifiedXmlStreamSource(StreamSource xml, String publicId)
|
452
|
throws Exception
|
453
|
{
|
454
|
// make sure the xml is not null
|
455
|
if (xml == null || publicId == null)
|
456
|
{
|
457
|
return;
|
458
|
}
|
459
|
logMetacat.info("public id of input stream is " +publicId);
|
460
|
// Get system id from xml_catalog table
|
461
|
String systemId = DBEntityResolver.getDTDSystemID(publicId);
|
462
|
logMetacat.info("system id of input stream from xml_catalog"
|
463
|
+"table is " +systemId);
|
464
|
//set system id to input stream
|
465
|
xml.setSystemId(systemId);
|
466
|
}
|
467
|
|
468
|
/*
|
469
|
* removes the DOCTYPE element and its contents from a Sting
|
470
|
* used to avoid problems with incorrect SystemIDs
|
471
|
*/
|
472
|
private String removeDOCTYPE(String in) {
|
473
|
String ret = "";
|
474
|
int startpos = in.indexOf("<!DOCTYPE");
|
475
|
if (startpos>-1) {
|
476
|
int stoppos = in.indexOf(">", startpos + 8);
|
477
|
ret = in.substring(0,startpos) + in.substring(stoppos+1,in.length());
|
478
|
} else {
|
479
|
return in;
|
480
|
}
|
481
|
return ret;
|
482
|
}
|
483
|
|
484
|
/**
|
485
|
* the main routine used to test the transform utility.
|
486
|
*
|
487
|
* Usage: java DBTransform
|
488
|
*/
|
489
|
static public void main(String[] args) {
|
490
|
|
491
|
if (args.length > 0)
|
492
|
{
|
493
|
System.err.println("Wrong number of arguments!!!");
|
494
|
System.err.println("USAGE: java DBTransform");
|
495
|
return;
|
496
|
} else {
|
497
|
try {
|
498
|
|
499
|
// Open a connection to the database
|
500
|
/*MetaCatUtil util = new MetaCatUtil();
|
501
|
Connection dbconn = util.openDBConnection();*/
|
502
|
|
503
|
// Create a test document
|
504
|
StringBuffer testdoc = new StringBuffer();
|
505
|
testdoc.append("<?xml version=\"1.0\"?>");
|
506
|
testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
|
507
|
testdoc.append("<dataset_id>DS001</dataset_id>");
|
508
|
testdoc.append("<title>My test doc</title></eml-dataset>");
|
509
|
|
510
|
// Transform the document to the new doctype
|
511
|
DBTransform dbt = new DBTransform();
|
512
|
dbt.transformXMLDocument(testdoc.toString(),
|
513
|
"-//NCEAS//eml-dataset//EN",
|
514
|
"-//W3C//HTML//EN",
|
515
|
"knb",
|
516
|
new PrintWriter(System.out), null);
|
517
|
|
518
|
} catch (Exception e) {
|
519
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
520
|
System.err.println(e.getMessage());
|
521
|
e.printStackTrace(System.err);
|
522
|
}
|
523
|
}
|
524
|
}
|
525
|
|
526
|
private void dbg(int position) {
|
527
|
System.err.println("Debug flag: " + position);
|
528
|
}
|
529
|
|
530
|
}
|