Source Code Cross Referenced for XMLUtil.java in  » Content-Management-System » dspace » org » dspace » app » webui » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » dspace » org.dspace.app.webui.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.