/Users/lyon/j4p/src/xml/musicCatalog/XmlTransformer.java
|
1 /**
2 * XmlTransformer.java
3 * @author Thomas Rowland
4 * @version 02-08-03
5 */
6 package xml.musicCatalog;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Node;
10
11 import javax.xml.transform.*;
12 import javax.xml.transform.dom.DOMSource;
13 import javax.xml.transform.stream.StreamResult;
14 import javax.xml.transform.stream.StreamSource;
15 import java.io.BufferedWriter;
16 import java.io.File;
17
18
19 /*
20 * This class demonstrates how to use XSLT to perform transformations,
21 * for example XML to HTML, or writing an XML document back out to file.
22 * This program uses the DOM Level 2 api and JAXP 1.2.
23 * You need to have jaxp-api.jar, xalan.jar in your classpath.
24 */
25
26 public class XmlTransformer {
27
28 /*
29 * Private constructor - no instances of this class should be created.
30 * All public methods are decared as static.
31 */
32 private XmlTransformer() {
33 }
34
35
36 /*
37 * Writes a DOM Document to an OutputStream. Demonstrates how easy it
38 * is to update an XML file or create a new XML file after modification.
39 * @args document a DOM Document
40 * @args uri output to write the XML to
41 */
42 public static void writeXml(Document document, String uri)
43 throws Exception {
44 try {
45 File out = new File(uri);
46
47 // Obtain a Transformer
48 TransformerFactory tf = TransformerFactory.newInstance();
49 Transformer t = tf.newTransformer();
50
51 // Set some properties if desired
52 t.setOutputProperty(OutputKeys.INDENT, "yes");
53
54 // Create a source object
55 DOMSource source = new DOMSource(document.getDocumentElement());
56
57 // Create a result object and perform the transform
58 StreamResult result = new StreamResult(out);
59 t.transform(source, result);
60 } catch (TransformerConfigurationException e) {
61 // wrapped exception generated by the TransformerFactory
62 Throwable ex = e.getException();
63 String msg = null;
64 if (ex != null) {
65 ex.printStackTrace();
66 msg = ex.getMessage();
67 }
68 throw new Exception(
69 "** TransformerConfigurationException:\n"
70 + e.getMessage());
71 } catch (TransformerException e) {
72 // wrapped exception generated by the Transformer
73 Throwable ex = e.getException();
74 String msg = null;
75 if (ex != null) {
76 ex.printStackTrace();
77 msg = ex.getMessage();
78 }
79 throw new Exception(
80 "** TransformerException:\n"
81 + e.getMessage());
82 }
83 }
84
85
86 /*
87 * Transforms a DOM Document into another form, specified by its stylesheet.
88 * @args node the top level DOM node reference
89 * @args stylesheet the file containing the transform definitions
90 * @args out the output destination
91 */
92 public static void transform(Node node, File stylesheet, BufferedWriter out) {
93 try {
94 // Instantiate the TransformerFactory
95 TransformerFactory tf = TransformerFactory.newInstance();
96
97 // Create a source object from the stylesheet
98 StreamSource ss = new StreamSource(stylesheet);
99
100 // Obtain a Transformer
101 Transformer t = tf.newTransformer(ss);
102
103 // Create a source object from the DOM document
104 DOMSource ds = new DOMSource(node);
105
106 // Create a result object from the BufferedWriter
107 StreamResult result = new StreamResult(out);
108
109 // Perform the transform
110 t.transform(ds, result);
111 } catch (TransformerConfigurationException e) {
112 // Exception generated by the TransformerFactory
113 System.out.println("\n** Transformer Factory error: "
114 + e.getMessage());
115 //get the wrapped exception
116 Throwable th = e;
117 if (e.getException() != null)
118 th = e.getException();
119 th.printStackTrace();
120 } catch (TransformerException e) {
121 // Exception generated by the transformer
122 System.out.println("\n** Transformation error: "
123 + e.getMessage());
124 //get the wrapped exception
125 Throwable th = e;
126 if (e.getException() != null)
127 th = e.getException();
128 th.printStackTrace();
129 }
130 }
131
132 }//