Source code: com/sonoma/TransformUtility.java
1 package com.sonoma;
2 import javax.xml.transform.TransformerFactory;
3 import javax.xml.transform.Transformer;
4 import javax.xml.transform.stream.StreamSource;
5 import javax.xml.transform.stream.StreamResult;
6 import javax.xml.transform.TransformerException;
7 import javax.xml.transform.TransformerConfigurationException;
8
9 import java.io.PrintWriter;
10 import java.io.FileOutputStream;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13
14 /**
15 * Utitlity that transforms xml files with XSL.
16 * Other classes and JSP pages use this object instead of Xerces or XML.
17 * dbRDF.java
18 * @author Roy Hoobler
19 * @version 1.1 04/7/2002
20 */
21 public class TransformUtility extends Object {
22
23 private String xmlDoc = new String("/home/java/jswdk/jswdk-1.0.1/examples/xml/supremevideo2.xml");
24 private String xslDoc = new String("/home/java/jswdk/jswdk-1.0.1/examples/xml/supremevid2.xsl");
25 private String sContentType = new String("text/HTML");
26 private XSLParamVector vParameters = new XSLParamVector();
27
28 /** Creates new myxml2 */
29 public TransformUtility() {
30 }
31 /** Sets which XML file is to transformed
32 *@param FileName full path to the xml file
33 */
34 public void setXMLDoc(String FileName){
35 //Complete Path of the XML Document
36 xmlDoc = FileName;
37 return;
38 }
39 /** Sets which XSL file is to be used perform the transformation
40 *@param FileName full path to the XSL file
41 */
42 public void setXSLDoc(String FileName){
43 //Complete Path of the XSL Document
44 xslDoc = FileName;
45 return;
46 }
47 /** XSL files take parameters. addParameter encapsulates this functionality
48 *@param ParamName the string value of the XSL parameter
49 *@param ParamValue the value to be set
50 */
51 public void addParameter(String ParamName, String ParamValue){
52 //Add a Parameter if needed
53 XSLParam objParam = new XSLParam(ParamName, ParamValue);
54 vParameters.add(objParam);
55 return;
56 }
57 public void setContentType(String ContentType){
58 sContentType = ContentType;
59 }
60 /** Performs the transformation.
61 *@param OutPut is usually the printwriter from the response object on a JSP page
62 */
63 public void Transform(PrintWriter OutPut)throws TransformerException, TransformerConfigurationException,
64 FileNotFoundException, IOException
65 {
66 // Use the static TransformerFactory.newInstance() method to instantiate
67 // a TransformerFactory. The javax.xml.transform.TransformerFactory
68 // system property setting determines the actual class to instantiate --
69 // org.apache.xalan.transformer.TransformerImpl.
70 TransformerFactory tFactory = TransformerFactory.newInstance();
71
72 // Use the TransformerFactory to instantiate a Transformer that will work with
73 // the stylesheet you specify. This method call also processes the stylesheet
74 // into a compiled Templates object.
75 System.out.println(xslDoc);
76 Transformer transformer = tFactory.newTransformer(new StreamSource(xslDoc));
77 XSLParam objParam = new XSLParam();
78 for (int i=0;i < vParameters.size();i++)
79 {
80 objParam = (XSLParam) vParameters.elementAt(i);
81 transformer.setParameter(objParam.ParamName(), objParam.ParamValue());
82 }
83
84 // Use the Transformer to apply the associated Templates object to an XML document
85 // (foo.xml) and write the output to a file (foo.out).
86 //transformer.transform(new StreamSource("birds.xml"), new StreamResult(new FileOutputStream("birds.out")));
87 transformer.transform(new StreamSource(xmlDoc), new StreamResult(OutPut));
88
89
90 }
91
92
93 }