Revision 1716
Added by berkley almost 21 years ago
DBTransform.java | ||
---|---|---|
70 | 70 |
import org.w3c.dom.Element; |
71 | 71 |
import org.xml.sax.SAXException; |
72 | 72 |
|
73 |
/**
|
|
73 |
/** |
|
74 | 74 |
* A Class that transforms XML documents utitlizing XSL style sheets |
75 | 75 |
*/ |
76 | 76 |
public class DBTransform { |
... | ... | |
87 | 87 |
* |
88 | 88 |
* @param conn the database connection from which to lookup the public ids |
89 | 89 |
*/ |
90 |
public DBTransform()
|
|
91 |
throws IOException,
|
|
92 |
SQLException,
|
|
90 |
public DBTransform() |
|
91 |
throws IOException, |
|
92 |
SQLException, |
|
93 | 93 |
ClassNotFoundException |
94 | 94 |
{ |
95 | 95 |
//this.conn = conn; |
... | ... | |
97 | 97 |
configDir = util.getOption("config-dir"); |
98 | 98 |
defaultStyle = util.getOption("default-style"); |
99 | 99 |
} |
100 |
|
|
100 |
|
|
101 | 101 |
/** |
102 |
* @see transformXMLDocument(String doc, String sourceType, |
|
103 |
* String targetType, String qformat, PrintWriter pw, |
|
104 |
* String sessionid) |
|
105 |
*/ |
|
106 |
public void transformXMLDocument(String doc, String sourceType, |
|
107 |
String targetType, String qformat, PrintWriter pw) |
|
108 |
{ |
|
109 |
transformXMLDocument(doc, sourceType, targetType, qformat, pw, null); |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
102 | 113 |
* Transform an XML document using the stylesheet reference from the db |
103 | 114 |
* |
104 | 115 |
* @param doc the document to be transformed |
... | ... | |
108 | 119 |
* @param pw the PrintWriter to which output is printed |
109 | 120 |
* @param params some parameters for eml2 transformation |
110 | 121 |
*/ |
111 |
public void transformXMLDocument(String doc, String sourceType, |
|
112 |
String targetType, String qformat, |
|
113 |
PrintWriter pw, Hashtable param) |
|
122 |
public void transformXMLDocument(String doc, String sourceType, |
|
123 |
String targetType, String qformat, |
|
124 |
PrintWriter pw, Hashtable param, |
|
125 |
String sessionid) |
|
114 | 126 |
{ |
115 |
|
|
127 |
|
|
116 | 128 |
// Look up the stylesheet for this type combination |
117 | 129 |
String xslSystemId = getStyleSystemId(qformat, sourceType, targetType); |
118 | 130 |
|
... | ... | |
124 | 136 |
new StreamSource(xslSystemId)); |
125 | 137 |
//transformer.setParameter("qformat", qformat); |
126 | 138 |
//MetaCatUtil.debugMessage("qformat: "+qformat, 30); |
127 |
|
|
139 |
|
|
140 |
if(sessionid != null) |
|
141 |
{ |
|
142 |
transformer.setParameter("sessid", sessionid); |
|
143 |
} |
|
144 |
|
|
128 | 145 |
// Set up parameter for transformation |
129 | 146 |
if ( param != null) |
130 | 147 |
{ |
... | ... | |
137 | 154 |
transformer.setParameter(key, value); |
138 | 155 |
} |
139 | 156 |
} |
140 |
|
|
141 |
transformer.transform(new StreamSource(new StringReader(doc)),
|
|
157 |
|
|
158 |
transformer.transform(new StreamSource(new StringReader(doc)), |
|
142 | 159 |
new StreamResult(pw)); |
143 | 160 |
} catch (Exception e) { |
144 | 161 |
pw.println(xslSystemId + "Error transforming document in " + |
145 | 162 |
"DBTransform.transformXMLDocument: " + |
146 | 163 |
e.getMessage()); |
147 |
|
|
164 |
|
|
148 | 165 |
} |
149 | 166 |
} else { |
150 |
// No stylesheet registered form this document type, so just return the
|
|
167 |
// No stylesheet registered form this document type, so just return the |
|
151 | 168 |
// XML stream we were passed |
152 | 169 |
pw.print(doc); |
153 | 170 |
} |
154 | 171 |
} |
155 |
|
|
172 |
|
|
156 | 173 |
/** |
157 |
* Transform an XML document to StringWriter using the stylesheet reference
|
|
174 |
* Transform an XML document to StringWriter using the stylesheet reference |
|
158 | 175 |
* from the db |
159 | 176 |
* @param doc the document to be transformed |
160 | 177 |
* @param sourceType the document type of the source |
... | ... | |
163 | 180 |
* @param pw the StringWriter to which output will be stored |
164 | 181 |
*/ |
165 | 182 |
public void transformXMLDocument(String doc, String sourceType, |
166 |
String targetType, String qFormat, StringWriter pw) { |
|
183 |
String targetType, String qFormat, StringWriter pw, |
|
184 |
String sessionid) { |
|
167 | 185 |
|
168 | 186 |
// Look up the stylesheet for this type combination |
169 | 187 |
String xslSystemId = getStyleSystemId(qFormat, sourceType, targetType); |
... | ... | |
174 | 192 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
175 | 193 |
Transformer transformer = tFactory.newTransformer( |
176 | 194 |
new StreamSource(xslSystemId)); |
195 |
if(sessionid != null) |
|
196 |
{ |
|
197 |
transformer.setParameter("sessid", sessionid); |
|
198 |
} |
|
177 | 199 |
transformer.setParameter("qFormat", qFormat); |
178 | 200 |
transformer.transform(new StreamSource(new StringReader(doc)), |
179 | 201 |
new StreamResult(pw)); |
... | ... | |
181 | 203 |
util.debugMessage(xslSystemId + "Error transforming document in " + |
182 | 204 |
"DBTransform.transformXMLDocument: " + |
183 | 205 |
e.getMessage(), 30); |
184 |
|
|
206 |
|
|
185 | 207 |
} |
186 | 208 |
} else { |
187 |
// No stylesheet registered form this document type, so just return the
|
|
209 |
// No stylesheet registered form this document type, so just return the |
|
188 | 210 |
// XML stream we were passed |
189 | 211 |
pw.write(doc); |
190 | 212 |
} |
191 | 213 |
} |
192 |
|
|
214 |
|
|
193 | 215 |
/** |
216 |
* @see transformXMLDocument(String doc, String sourceType, |
|
217 |
* String targetType, String qFormat, StringWriter pw |
|
218 |
* String sessionid) |
|
219 |
*/ |
|
220 |
public void transformXMLDocument(String doc, String sourceType, |
|
221 |
String targetType, String qFormat, StringWriter pw) |
|
222 |
{ |
|
223 |
transformXMLDocument(doc, sourceType, targetType, qFormat, pw, null); |
|
224 |
} |
|
225 |
|
|
226 |
/** |
|
194 | 227 |
* gets the content of a tag in a given xml file with the given path |
195 | 228 |
* @param f the file to parse |
196 | 229 |
* @param path the path to get the content from |
197 | 230 |
*/ |
198 |
public static NodeList getPathContent(File f, String path)
|
|
231 |
public static NodeList getPathContent(File f, String path) |
|
199 | 232 |
{ |
200 | 233 |
if(f == null) |
201 | 234 |
{ |
202 | 235 |
return null; |
203 | 236 |
} |
204 |
|
|
237 |
|
|
205 | 238 |
DOMParser parser = new DOMParser(); |
206 | 239 |
InputSource in; |
207 | 240 |
FileInputStream fs; |
208 |
|
|
241 |
|
|
209 | 242 |
try |
210 |
{
|
|
243 |
{ |
|
211 | 244 |
fs = new FileInputStream(f); |
212 | 245 |
in = new InputSource(fs); |
213 | 246 |
} |
... | ... | |
216 | 249 |
fnf.printStackTrace(); |
217 | 250 |
return null; |
218 | 251 |
} |
219 |
|
|
252 |
|
|
220 | 253 |
try |
221 | 254 |
{ |
222 | 255 |
parser.parse(in); |
... | ... | |
224 | 257 |
} |
225 | 258 |
catch(Exception e1) |
226 | 259 |
{ |
227 |
System.err.println("File: " + f.getPath() + " : parse threw: " +
|
|
260 |
System.err.println("File: " + f.getPath() + " : parse threw: " + |
|
228 | 261 |
e1.toString()); |
229 | 262 |
return null; |
230 | 263 |
} |
231 |
|
|
264 |
|
|
232 | 265 |
Document doc = parser.getDocument(); |
233 |
|
|
266 |
|
|
234 | 267 |
try |
235 | 268 |
{ |
236 | 269 |
NodeList docNodeList = XPathAPI.selectNodeList(doc, path); |
... | ... | |
238 | 271 |
} |
239 | 272 |
catch(Exception se) |
240 | 273 |
{ |
241 |
System.err.println("file: " + f.getPath() + " : parse threw: " +
|
|
274 |
System.err.println("file: " + f.getPath() + " : parse threw: " + |
|
242 | 275 |
se.toString()); |
243 | 276 |
return null; |
244 | 277 |
} |
... | ... | |
251 | 284 |
* @param sourcetype the document type of the source |
252 | 285 |
* @param targettype the document type of the target |
253 | 286 |
*/ |
254 |
public String getStyleSystemId(String qformat, String sourcetype,
|
|
287 |
public String getStyleSystemId(String qformat, String sourcetype, |
|
255 | 288 |
String targettype) { |
256 | 289 |
String systemId = null; |
257 | 290 |
|
... | ... | |
262 | 295 |
// Load the style-set map for this qformat into a DOM |
263 | 296 |
try { |
264 | 297 |
boolean breakflag = false; |
265 |
String filename = configDir + "/" + qformat + ".xml";
|
|
298 |
String filename = configDir + "/" + qformat + ".xml"; |
|
266 | 299 |
util.debugMessage("Trying style-set file: " + filename, 30); |
267 | 300 |
File f = new File(filename); |
268 | 301 |
NodeList nlDoctype = getPathContent(f, "/style-set/doctype"); |
269 | 302 |
NodeList nlDefault = getPathContent(f, "/style-set/default-style"); |
270 | 303 |
Node nDefault = nlDefault.item(0); |
271 | 304 |
systemId = nDefault.getFirstChild().getNodeValue(); //set the default |
272 |
|
|
305 |
|
|
273 | 306 |
for(int i=0; i<nlDoctype.getLength(); i++) |
274 | 307 |
{ //look for the right sourcetype |
275 | 308 |
Node nDoctype = nlDoctype.item(i); |
... | ... | |
303 | 336 |
} |
304 | 337 |
} |
305 | 338 |
} |
306 |
|
|
339 |
|
|
307 | 340 |
if(breakflag) |
308 | 341 |
{ |
309 | 342 |
break; |
310 | 343 |
} |
311 | 344 |
} |
312 | 345 |
} |
313 |
|
|
346 |
|
|
314 | 347 |
if(breakflag) |
315 | 348 |
{ |
316 | 349 |
break; |
... | ... | |
335 | 368 |
* @param sourcetype the document type of the source |
336 | 369 |
* @param targettype the document type of the target |
337 | 370 |
*/ |
338 |
public String getSystemId(String objecttype, String sourcetype,
|
|
371 |
public String getSystemId(String objecttype, String sourcetype, |
|
339 | 372 |
String targettype) { |
340 | 373 |
|
341 | 374 |
// Look up the System ID of a particular object |
... | ... | |
366 | 399 |
try { |
367 | 400 |
the_system_id = rs.getString(1); |
368 | 401 |
} catch (SQLException e) { |
369 |
System.out.println("Error with getString in " +
|
|
370 |
"DBTransform.getSystemId: " + e.getMessage());
|
|
402 |
System.out.println("Error with getString in " + |
|
403 |
"DBTransform.getSystemId: " + e.getMessage()); |
|
371 | 404 |
} |
372 | 405 |
} else { |
373 |
the_system_id = null;
|
|
406 |
the_system_id = null; |
|
374 | 407 |
} |
375 | 408 |
} catch (SQLException e) { |
376 |
System.err.println("Error with next in DBTransform.getSystemId: " +
|
|
409 |
System.err.println("Error with next in DBTransform.getSystemId: " + |
|
377 | 410 |
e.getMessage()); |
378 | 411 |
return ("Error with next: " + e.getMessage()); |
379 | 412 |
} |
380 | 413 |
} catch (SQLException e) { |
381 |
System.err.println("Error with getrset in DBTransform.getSystemId: " +
|
|
414 |
System.err.println("Error with getrset in DBTransform.getSystemId: " + |
|
382 | 415 |
e.getMessage()); |
383 | 416 |
return ("Error with getrset: " + e.getMessage()); |
384 | 417 |
} |
385 | 418 |
pstmt.close(); |
386 | 419 |
} catch (SQLException e) { |
387 |
System.err.println("Error getting id in DBTransform.getSystemId: " +
|
|
420 |
System.err.println("Error getting id in DBTransform.getSystemId: " + |
|
388 | 421 |
e.getMessage()); |
389 |
return ("Error getting id in DBTransform.getSystemId:: " +
|
|
422 |
return ("Error getting id in DBTransform.getSystemId:: " + |
|
390 | 423 |
e.getMessage()); |
391 | 424 |
} |
392 | 425 |
finally |
... | ... | |
414 | 447 |
* Usage: java DBTransform |
415 | 448 |
*/ |
416 | 449 |
static public void main(String[] args) { |
417 |
|
|
450 |
|
|
418 | 451 |
if (args.length > 0) |
419 | 452 |
{ |
420 | 453 |
System.err.println("Wrong number of arguments!!!"); |
... | ... | |
422 | 455 |
return; |
423 | 456 |
} else { |
424 | 457 |
try { |
425 |
|
|
458 |
|
|
426 | 459 |
// Open a connection to the database |
427 | 460 |
/*MetaCatUtil util = new MetaCatUtil(); |
428 | 461 |
Connection dbconn = util.openDBConnection();*/ |
... | ... | |
436 | 469 |
|
437 | 470 |
// Transform the document to the new doctype |
438 | 471 |
DBTransform dbt = new DBTransform(); |
439 |
dbt.transformXMLDocument(testdoc.toString(),
|
|
440 |
"-//NCEAS//eml-dataset//EN",
|
|
441 |
"-//W3C//HTML//EN",
|
|
472 |
dbt.transformXMLDocument(testdoc.toString(), |
|
473 |
"-//NCEAS//eml-dataset//EN", |
|
474 |
"-//W3C//HTML//EN", |
|
442 | 475 |
"knb", |
443 | 476 |
new PrintWriter(System.out), null); |
444 | 477 |
|
... | ... | |
449 | 482 |
} |
450 | 483 |
} |
451 | 484 |
} |
452 |
|
|
485 |
|
|
453 | 486 |
private void dbg(int position) { |
454 | 487 |
System.err.println("Debug flag: " + position); |
455 | 488 |
} |
Also available in: Unified diff
changes to make session management between monarch and metacat work.