001: /*
002: * XMLUtil.java
003: *
004: * Version: $Revision: 1414 $
005: *
006: * Date: $Date: 2006-02-08 08:41:08 -0600 (Wed, 08 Feb 2006) $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts Institute of
009: * Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are met: -
013: * Redistributions of source code must retain the above copyright notice, this
014: * list of conditions and the following disclaimer. - Redistributions in binary
015: * form must reproduce the above copyright notice, this list of conditions and
016: * the following disclaimer in the documentation and/or other materials provided
017: * with the distribution. - Neither the name of the Hewlett-Packard Company nor
018: * the name of the Massachusetts Institute of Technology nor the names of their
019: * contributors may be used to endorse or promote products derived from this
020: * software without specific prior written permission.
021: *
022: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
023: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
024: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
025: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
026: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
027: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
028: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
029: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
030: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
031: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: * POSSIBILITY OF SUCH DAMAGE.
033: */
034: package org.dspace.app.webui.util;
035:
036: import java.io.File;
037: import java.io.IOException;
038: import java.io.StringWriter;
039: import java.util.Enumeration;
040: import java.util.Hashtable;
041:
042: import javax.xml.parsers.DocumentBuilder;
043: import javax.xml.parsers.DocumentBuilderFactory;
044: import javax.xml.parsers.ParserConfigurationException;
045: import javax.xml.transform.Transformer;
046: import javax.xml.transform.TransformerFactory;
047: import javax.xml.transform.dom.DOMResult;
048: import javax.xml.transform.dom.DOMSource;
049: import javax.xml.transform.stream.StreamResult;
050: import javax.xml.transform.stream.StreamSource;
051:
052: import org.w3c.dom.Document;
053: import org.xml.sax.SAXException;
054:
055: /**
056: * This class provides a set of static methods to load and transform XML
057: * documents. It supports parameter-aware stylesheets (XSLT).
058: *
059: * @author Miguel Ferreira
060: *
061: */
062: public class XMLUtil {
063:
064: /**
065: * Loads a W3C XML document from a file.
066: *
067: * @param filename
068: * The name of the file to be loaded
069: * @return a document object model object representing the XML file
070: * @throws IOException
071: * @throws ParserConfigurationException
072: * @throws SAXException
073: */
074: public static Document loadXML(String filename) throws IOException,
075: ParserConfigurationException, SAXException {
076: DocumentBuilder builder = DocumentBuilderFactory.newInstance()
077: .newDocumentBuilder();
078: return builder.parse(new File(filename));
079: }
080:
081: /**
082: * Applies a stylesheet to a given xml document.
083: *
084: * @param xmlDocument
085: * the xml document to be transformed
086: * @param xsltFilename
087: * the filename of the stylesheet
088: * @return the transformed xml document
089: * @throws Exception
090: */
091: public static Document transformDocument(Document xmlDocument,
092: String xsltFilename) throws Exception {
093: return transformDocument(xmlDocument, new Hashtable(),
094: xsltFilename);
095: }
096:
097: /**
098: * Applies a stylesheet (that receives parameters) to a given xml document.
099: *
100: * @param xmlDocument
101: * the xml document to be transformed
102: * @param parameters
103: * the hashtable with the parameters to be passed to the
104: * stylesheet
105: * @param xsltFilename
106: * the filename of the stylesheet
107: * @return the transformed xml document
108: * @throws Exception
109: */
110: public static Document transformDocument(Document xmlDocument,
111: Hashtable parameters, String xsltFilename) throws Exception {
112:
113: // Generate a Transformer.
114: Transformer transformer = TransformerFactory.newInstance()
115: .newTransformer(new StreamSource(xsltFilename));
116:
117: // set transformation parameters
118: if (parameters != null) {
119: Enumeration keys = parameters.keys();
120: while (keys.hasMoreElements()) {
121: String key = (String) keys.nextElement();
122: String value = (String) parameters.get(key);
123: transformer.setParameter(key, value);
124: }
125:
126: }
127:
128: // Create an empy DOMResult object for the output.
129: DocumentBuilderFactory dFactory = DocumentBuilderFactory
130: .newInstance();
131: dFactory.setNamespaceAware(true);
132: DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
133: Document dstDocument = dBuilder.newDocument();
134:
135: DOMResult domResult = new DOMResult(dstDocument);
136:
137: // Perform the transformation.
138: transformer.transform(new DOMSource(xmlDocument), domResult);
139: // Now you can get the output Node from the DOMResult.
140: return dstDocument;
141: }
142:
143: /**
144: * Applies a stylesheet (that receives parameters) to a given xml document.
145: * The resulting XML document is converted to a string after transformation.
146: *
147: * @param xmlDocument
148: * the xml document to be transformed
149: * @param parameters
150: * the hashtable with the parameters to be passed to the
151: * stylesheet
152: * @param xsltFilename
153: * the filename of the stylesheet
154: * @return the transformed xml document as a string
155: * @throws Exception
156: */
157: public static String transformDocumentAsString(
158: Document xmlDocument, Hashtable parameters,
159: String xsltFilename) throws Exception {
160:
161: // Generate a Transformer.
162: Transformer transformer = TransformerFactory.newInstance()
163: .newTransformer(new StreamSource(xsltFilename));
164:
165: // set transformation parameters
166: if (parameters != null) {
167: Enumeration keys = parameters.keys();
168: while (keys.hasMoreElements()) {
169: String key = (String) keys.nextElement();
170: String value = (String) parameters.get(key);
171: transformer.setParameter(key, value);
172: }
173:
174: }
175:
176: StringWriter stringWriter = new StringWriter();
177: StreamResult streamResult = new StreamResult(stringWriter);
178:
179: // Perform the transformation.
180: transformer.transform(new DOMSource(xmlDocument), streamResult);
181: // Now you can get the output Node from the DOMResult.
182: return stringWriter.toString();
183: }
184:
185: /**
186: * Applies a stylesheet to a given xml document.
187: *
188: * @param xmlDocument
189: * the xml document to be transformed
190: * @param xsltFilename
191: * the filename of the stylesheet
192: * @return the transformed xml document
193: * @throws Exception
194: */
195: public static String transformDocumentAsString(
196: Document xmlDocument, String xsltFilename) throws Exception {
197: // Generate a Transformer.
198: Transformer transformer = TransformerFactory.newInstance()
199: .newTransformer(new StreamSource(xsltFilename));
200:
201: StringWriter stringWriter = new StringWriter();
202: StreamResult streamResult = new StreamResult(stringWriter);
203:
204: // Perform the transformation.
205: transformer.transform(new DOMSource(xmlDocument), streamResult);
206: // Now you can get the output Node from the DOMResult.
207: return stringWriter.toString();
208: }
209:
210: }
|