01: package org.sakaibrary.xserver;
02:
03: import javax.xml.parsers.DocumentBuilder;
04: import javax.xml.parsers.DocumentBuilderFactory;
05: import javax.xml.parsers.ParserConfigurationException;
06: import org.xml.sax.SAXException;
07: import org.w3c.dom.Document;
08:
09: // For write operation
10: import javax.xml.transform.Transformer;
11: import javax.xml.transform.TransformerException;
12: import javax.xml.transform.TransformerFactory;
13: import javax.xml.transform.TransformerConfigurationException;
14: import javax.xml.transform.dom.DOMSource;
15: import javax.xml.transform.stream.StreamSource;
16: import javax.xml.transform.stream.StreamResult;
17: import java.io.*;
18:
19: public class XMLTransform {
20: private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
21: .getLog("org.sakaibrary.xserver.XMLTransform");
22:
23: private Document document;
24: private String xslFileName;
25: private ByteArrayOutputStream xml;
26: private ByteArrayOutputStream transformedXml;
27:
28: public XMLTransform(String xslFile, ByteArrayOutputStream xml) {
29: xslFileName = xslFile;
30: this .xml = xml;
31: }
32:
33: public ByteArrayOutputStream transform() {
34: DocumentBuilderFactory factory = DocumentBuilderFactory
35: .newInstance();
36:
37: try {
38: InputStream stylesheet = this .getClass()
39: .getResourceAsStream(xslFileName);
40:
41: DocumentBuilder builder = factory.newDocumentBuilder();
42: document = builder.parse(new ByteArrayInputStream(xml
43: .toByteArray()));
44:
45: // Use a Transformer for output
46: TransformerFactory tFactory = TransformerFactory
47: .newInstance();
48: StreamSource stylesource = new StreamSource(stylesheet);
49: Transformer transformer = tFactory
50: .newTransformer(stylesource);
51:
52: transformedXml = new ByteArrayOutputStream();
53: StreamResult result = new StreamResult(transformedXml);
54:
55: DOMSource source = new DOMSource(document);
56:
57: transformer.transform(source, result);
58: } catch (TransformerConfigurationException tce) {
59: // Error generated by the parser
60: LOG
61: .warn("XMLTransform.transform() - TransformerFactory error: "
62: + tce.getMessage());
63: } catch (TransformerException te) {
64: // Error generated by the parser
65: LOG
66: .warn("XMLTransform.transform() - Transformation error: "
67: + te.getMessage());
68: } catch (SAXException sxe) {
69: // Error generated by this application
70: // (or a parser-initialization error)
71: Exception x = sxe;
72:
73: if (sxe.getException() != null) {
74: x = sxe.getException();
75: }
76:
77: LOG.warn("XMLTransform.transform() SAX exception: "
78: + sxe.getMessage(), x);
79: } catch (ParserConfigurationException pce) {
80: // Parser with specified options can't be built
81: LOG
82: .warn("XMLTransform.transform() SAX parser cannot be built with "
83: + "specified options");
84: } catch (IOException ioe) {
85: // I/O error
86: LOG.warn("XMLCleanup.cleanup() IO exception", ioe);
87: }
88:
89: return transformedXml;
90: } // main
91: }
|