KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > util > XMLUtil


1 /*
2  * XMLUtil.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/02/08 14:34:03 $
7  *
8  * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts Institute of
9  * Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met: -
13  * Redistributions of source code must retain the above copyright notice, this
14  * list of conditions and the following disclaimer. - Redistributions in binary
15  * form must reproduce the above copyright notice, this list of conditions and
16  * the following disclaimer in the documentation and/or other materials provided
17  * with the distribution. - Neither the name of the Hewlett-Packard Company nor
18  * the name of the Massachusetts Institute of Technology nor the names of their
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */

34 package org.dspace.app.webui.util;
35
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.StringWriter JavaDoc;
39 import java.util.Enumeration JavaDoc;
40 import java.util.Hashtable JavaDoc;
41
42 import javax.xml.parsers.DocumentBuilder JavaDoc;
43 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
44 import javax.xml.parsers.ParserConfigurationException JavaDoc;
45 import javax.xml.transform.Transformer JavaDoc;
46 import javax.xml.transform.TransformerFactory JavaDoc;
47 import javax.xml.transform.dom.DOMResult JavaDoc;
48 import javax.xml.transform.dom.DOMSource JavaDoc;
49 import javax.xml.transform.stream.StreamResult JavaDoc;
50 import javax.xml.transform.stream.StreamSource JavaDoc;
51
52 import org.w3c.dom.Document JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54
55 /**
56  * This class provides a set of static methods to load and transform XML
57  * documents. It supports parameter-aware stylesheets (XSLT).
58  *
59  * @author Miguel Ferreira
60  *
61  */

62 public class XMLUtil
63 {
64
65     /**
66      * Loads a W3C XML document from a file.
67      *
68      * @param filename
69      * The name of the file to be loaded
70      * @return a document object model object representing the XML file
71      * @throws IOException
72      * @throws ParserConfigurationException
73      * @throws SAXException
74      */

75     public static Document JavaDoc loadXML(String JavaDoc filename) throws IOException JavaDoc,
76             ParserConfigurationException JavaDoc, SAXException JavaDoc
77     {
78         DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance()
79                 .newDocumentBuilder();
80         return builder.parse(new File JavaDoc(filename));
81     }
82
83     /**
84      * Applies a stylesheet to a given xml document.
85      *
86      * @param xmlDocument
87      * the xml document to be transformed
88      * @param xsltFilename
89      * the filename of the stylesheet
90      * @return the transformed xml document
91      * @throws Exception
92      */

93     public static Document JavaDoc transformDocument(Document JavaDoc xmlDocument,
94             String JavaDoc xsltFilename) throws Exception JavaDoc
95     {
96         return transformDocument(xmlDocument, new Hashtable JavaDoc(), xsltFilename);
97     }
98
99     /**
100      * Applies a stylesheet (that receives parameters) to a given xml document.
101      *
102      * @param xmlDocument
103      * the xml document to be transformed
104      * @param parameters
105      * the hashtable with the parameters to be passed to the
106      * stylesheet
107      * @param xsltFilename
108      * the filename of the stylesheet
109      * @return the transformed xml document
110      * @throws Exception
111      */

112     public static Document JavaDoc transformDocument(Document JavaDoc xmlDocument,
113             Hashtable JavaDoc parameters, String JavaDoc xsltFilename) throws Exception JavaDoc
114     {
115
116         // Generate a Transformer.
117
Transformer JavaDoc transformer = TransformerFactory.newInstance()
118                 .newTransformer(new StreamSource JavaDoc(xsltFilename));
119
120         // set transformation parameters
121
if (parameters != null)
122         {
123             Enumeration JavaDoc keys = parameters.keys();
124             while (keys.hasMoreElements())
125             {
126                 String JavaDoc key = (String JavaDoc) keys.nextElement();
127                 String JavaDoc value = (String JavaDoc) parameters.get(key);
128                 transformer.setParameter(key, value);
129             }
130
131         }
132
133         // Create an empy DOMResult object for the output.
134
DocumentBuilderFactory JavaDoc dFactory = DocumentBuilderFactory.newInstance();
135         dFactory.setNamespaceAware(true);
136         DocumentBuilder JavaDoc dBuilder = dFactory.newDocumentBuilder();
137         Document JavaDoc dstDocument = dBuilder.newDocument();
138
139         DOMResult JavaDoc domResult = new DOMResult JavaDoc(dstDocument);
140
141         // Perform the transformation.
142
transformer.transform(new DOMSource JavaDoc(xmlDocument), domResult);
143         // Now you can get the output Node from the DOMResult.
144
return dstDocument;
145     }
146
147     /**
148      * Applies a stylesheet (that receives parameters) to a given xml document.
149      * The resulting XML document is converted to a string after transformation.
150      *
151      * @param xmlDocument
152      * the xml document to be transformed
153      * @param parameters
154      * the hashtable with the parameters to be passed to the
155      * stylesheet
156      * @param xsltFilename
157      * the filename of the stylesheet
158      * @return the transformed xml document as a string
159      * @throws Exception
160      */

161     public static String JavaDoc transformDocumentAsString(Document JavaDoc xmlDocument,
162             Hashtable JavaDoc parameters, String JavaDoc xsltFilename) throws Exception JavaDoc
163     {
164
165         // Generate a Transformer.
166
Transformer JavaDoc transformer = TransformerFactory.newInstance()
167                 .newTransformer(new StreamSource JavaDoc(xsltFilename));
168
169         // set transformation parameters
170
if (parameters != null)
171         {
172             Enumeration JavaDoc keys = parameters.keys();
173             while (keys.hasMoreElements())
174             {
175                 String JavaDoc key = (String JavaDoc) keys.nextElement();
176                 String JavaDoc value = (String JavaDoc) parameters.get(key);
177                 transformer.setParameter(key, value);
178             }
179
180         }
181
182         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
183         StreamResult JavaDoc streamResult = new StreamResult JavaDoc(stringWriter);
184
185         // Perform the transformation.
186
transformer.transform(new DOMSource JavaDoc(xmlDocument), streamResult);
187         // Now you can get the output Node from the DOMResult.
188
return stringWriter.toString();
189     }
190
191     /**
192      * Applies a stylesheet to a given xml document.
193      *
194      * @param xmlDocument
195      * the xml document to be transformed
196      * @param xsltFilename
197      * the filename of the stylesheet
198      * @return the transformed xml document
199      * @throws Exception
200      */

201     public static String JavaDoc transformDocumentAsString(Document JavaDoc xmlDocument,
202             String JavaDoc xsltFilename) throws Exception JavaDoc
203     {
204         // Generate a Transformer.
205
Transformer JavaDoc transformer = TransformerFactory.newInstance()
206                 .newTransformer(new StreamSource JavaDoc(xsltFilename));
207
208         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
209         StreamResult JavaDoc streamResult = new StreamResult JavaDoc(stringWriter);
210
211         // Perform the transformation.
212
transformer.transform(new DOMSource JavaDoc(xmlDocument), streamResult);
213         // Now you can get the output Node from the DOMResult.
214
return stringWriter.toString();
215     }
216
217 }
218
Popular Tags