KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mc > formgenerator > ggf > xslprocessing > XalanExecutor


1 /*
2  * Created on 15 avr. 2004 by the Message Center Team
3  *
4  */

5  
6 package mc.formgenerator.ggf.xslprocessing;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10
11 import javax.xml.transform.Transformer JavaDoc;
12 import javax.xml.transform.TransformerConfigurationException JavaDoc;
13 import javax.xml.transform.TransformerException JavaDoc;
14 import javax.xml.transform.TransformerFactory JavaDoc;
15 import javax.xml.transform.dom.DOMResult JavaDoc;
16 import javax.xml.transform.dom.DOMSource JavaDoc;
17 import javax.xml.transform.stream.StreamResult JavaDoc;
18 import javax.xml.transform.stream.StreamSource JavaDoc;
19
20 import org.apache.xerces.dom.DOMImplementationImpl;
21 import org.w3c.dom.DOMImplementation JavaDoc;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.NamedNodeMap JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28
29 /**
30  * Execution of Xalan engine
31  * @author sempereb
32  */

33 public class XalanExecutor {
34
35     //Path of the xml file
36
public String JavaDoc xmlFile;
37     
38     //Path of the xsl file
39
public String JavaDoc xslFile;
40     
41     //Path of the output file
42
public String JavaDoc outputFile;
43     
44     public Transformer JavaDoc transformer = null;
45     
46     /**
47      * Default constructor of our class
48      */

49     public XalanExecutor(){};
50
51
52
53
54     /**
55      * Other constructor of our class
56      * It will create a XalanExecutor with a defined xsl file
57      * @param theXslFile: path of the xsl file
58      */

59     public XalanExecutor(String JavaDoc theXslFile, String JavaDoc userLanguage, String JavaDoc fileXformsBonita)
60         throws TransformerConfigurationException JavaDoc
61     {
62         //Set path of the xsl file
63
xslFile = theXslFile;
64         
65         //Creation via the factory of an instance of Xalan transfomer
66
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
67         
68         //We read the xsl file
69
transformer = tFactory.newTransformer(new StreamSource JavaDoc( xslFile ));
70         // we set the language from the user request http
71
transformer.setParameter("language", userLanguage);
72         // we set the file path
73
transformer.setParameter("fileXformsBonita", fileXformsBonita);
74     }
75
76     public static XalanExecutor xalanExecutorFormURL(String JavaDoc theXslFile, String JavaDoc userLanguage, String JavaDoc fileXformsBonita)
77         throws TransformerConfigurationException JavaDoc
78     {
79         XalanExecutor n = new XalanExecutor();
80         
81         //Set path of the xsl file
82
n.xslFile = theXslFile;
83     
84         //Creation via the factory of an instance of Xalan transfomer
85
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
86         
87         //We read the xsl file
88
n.transformer = tFactory.newTransformer(new StreamSource JavaDoc( theXslFile ));
89         // we set the language from the user request http
90

91         n.transformer.setParameter("language", userLanguage);
92         n.transformer.setParameter("fileXformsBonita", fileXformsBonita);
93         return n;
94     };
95
96     
97
98
99     /**
100      * Other execution with parameters
101      * @param theXmlFile: the source file
102      * @param theXslFile: the file that provides transformation
103      * @param theOutputFile: the file generated
104      * @throws TransformerException
105      * @throws TransformerConfigurationException
106      * @throws FileNotFoundException
107      * @throws IOException
108      */

109     public void XalanExecution(String JavaDoc theXmlFile, String JavaDoc theXslFile, String JavaDoc theOutputFile)
110             throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
111             FileNotFoundException JavaDoc, IOException JavaDoc{
112         
113         //Creation via the factory of an instance of Xalan transfomer
114
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
115     
116         //We read the xsl file
117
Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc( theXslFile ));
118         
119         // we set the language from the user request http
120
transformer.setParameter("language","en");
121
122         //Transformation thanks to xalan for the xml file
123
transformer.transform(new StreamSource JavaDoc( theXmlFile ), new StreamResult JavaDoc(new FileOutputStream JavaDoc( theOutputFile )));
124     }
125
126
127
128
129     /**
130      * Execution of Xalan on files in order to make dynamic XHTML
131      * Here the output file is a Document instance
132      * @param document: the document to transform
133      * @param document: base uri needed by Chiba
134      * @return a Document that represents the document generated by the xsl engine
135      * @throws TransformerException
136      * @throws TransformerConfigurationException
137      * @throws FileNotFoundException
138      * @throws IOException
139      */

140     public Document JavaDoc XalanExecutionDOM(Document JavaDoc document, String JavaDoc xmlBase)
141             throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
142             FileNotFoundException JavaDoc, IOException JavaDoc{
143         
144         //The DOM result
145
DOMResult JavaDoc outputDOM = new DOMResult JavaDoc();
146         
147         //Transformation thanks to xalan for the xml file
148
transformer.transform( new DOMSource JavaDoc(document), outputDOM);
149         
150         Document JavaDoc documentOutput = (Document JavaDoc)outputDOM.getNode();
151         
152         //Set xml:base to avoid Chiba Exception...
153
Element JavaDoc root = documentOutput.getDocumentElement();
154         root.setAttribute("xml:base",xmlBase);
155         
156         //Update of documentOutput
157
return documentOutput;
158     }
159     
160 }
161
Popular Tags