/Users/lyon/j4p/src/xml/classInfo/XsltXml2Html.java

1    /** 
2     * XsltXml2Html.java 
3     * @author Thomas Rowland 
4     * @version 02-08-03 
5     */ 
6    package xml.classInfo; 
7     
8    import futils.Futil; 
9    import org.w3c.dom.Document; 
10   import org.w3c.dom.Node; 
11   import org.xml.sax.SAXException; 
12    
13   import javax.xml.parsers.DocumentBuilder; 
14   import javax.xml.parsers.DocumentBuilderFactory; 
15   import javax.xml.parsers.ParserConfigurationException; 
16   import javax.xml.transform.Transformer; 
17   import javax.xml.transform.TransformerConfigurationException; 
18   import javax.xml.transform.TransformerException; 
19   import javax.xml.transform.TransformerFactory; 
20   import javax.xml.transform.dom.DOMSource; 
21   import javax.xml.transform.stream.StreamResult; 
22   import javax.xml.transform.stream.StreamSource; 
23   import java.io.BufferedWriter; 
24   import java.io.File; 
25   import java.io.FileWriter; 
26   import java.io.IOException; 
27    
28   /* 
29    * This program demonstrates how to use XSLT Transforms 
30    * to transform an XML document into HTML (or other format). 
31    */ 
32    
33   public class XsltXml2Html { 
34    
35       public static void main(String[] args) { 
36           try { 
37               // Select an XML file and associated Stylesheet as inputs 
38               File xmlFile = Futil.getReadFile("select an XML file"); 
39               File stylesheet = Futil.getReadFile("select a stylesheet"); 
40    
41               // Define the output to write to 
42               String fn = xmlFile.getName(); 
43               fn = fn.substring(0, fn.lastIndexOf(".")); 
44               File htmlFile = new File( 
45                       xmlFile.getParent() + "\\" + fn + ".html"); 
46               BufferedWriter out = new BufferedWriter( 
47                       new FileWriter(htmlFile)); 
48    
49               // Parse the XML and pass the DOM Document for transformation 
50               Document document = parse(xmlFile); 
51               transform(document, stylesheet, out); 
52               out.flush(); 
53               out.close(); 
54               System.out.println("HTML output written to:\n" + htmlFile.toString()); 
55           } catch (IOException ioe) { 
56               ioe.printStackTrace(); 
57           } 
58       } 
59    
60    
61       /* 
62        * Parses an XML file and returns the DOM Document 
63        */ 
64       private static Document parse(File xmlFile) throws IOException { 
65           try { 
66               DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
67               //factory.setNamespaceAware(true); 
68               //factory.setValidating(true); 
69    
70               DocumentBuilder db = factory.newDocumentBuilder(); 
71               Document document = db.parse(xmlFile); 
72               return document; 
73           } catch (SAXException e) { 
74               // Parsing error. Get the wrapped exception 
75               Exception x = e; 
76               if (e.getException() != null) 
77                   x = e.getException(); 
78               x.printStackTrace(); 
79           } catch (ParserConfigurationException e) { 
80               // Factory unable to create parser 
81               e.printStackTrace(); 
82           } 
83           return null; 
84       } 
85    
86    
87       /* 
88        * Transforms a DOM Document into the form specified by the stylesheet, 
89        * and writes the result to the specified output. 
90        */ 
91       public static void transform(Node node, File stylesheet, BufferedWriter out) { 
92           try { 
93               // Instantiate the TransformerFactory 
94               TransformerFactory tf = TransformerFactory.newInstance(); 
95    
96               // Create a source object from the stylesheet 
97               StreamSource ss = new StreamSource(stylesheet); 
98    
99               // Obtain a Transformer 
100              Transformer t = tf.newTransformer(ss); 
101   
102              // Create a source object from the DOM document 
103              DOMSource ds = new DOMSource(node); 
104   
105              // Create a result object from the BufferedWriter 
106              StreamResult result = new StreamResult(out); 
107   
108              // Perform the transform 
109              t.transform(ds, result); 
110          } catch (TransformerConfigurationException tce) { 
111              // Exception generated by the TransformerFactory 
112              System.out.println("\n** Transformer Factory error"); 
113              System.out.println(" " + tce.getMessage()); 
114              // Get the wrapped Throwable exception 
115              Throwable x = tce; 
116              if (tce.getException() != null) 
117                  x = tce.getException(); 
118              x.printStackTrace(); 
119          } catch (TransformerException te) { 
120              // Exception generated by the transformer 
121              System.out.println("\n** Transformation error"); 
122              System.out.println(" " + te.getMessage()); 
123              // Get the wrapped Throwable exception 
124              Throwable x = te; 
125              if (te.getException() != null) 
126                  x = te.getException(); 
127              x.printStackTrace(); 
128          } 
129      } 
130   
131  }//