Source Code Cross Referenced for XMLPrettyPrinter.java in  » Web-Services-AXIS2 » kernal » org » apache » axis2 » 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 » Web Services AXIS2 » kernal » org.apache.axis2.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */
019:        package org.apache.axis2.util;
020:
021:        import org.apache.commons.logging.Log;
022:        import org.apache.commons.logging.LogFactory;
023:        import org.apache.axiom.om.OMElement;
024:        import org.apache.axiom.attachments.utils.IOUtils;
025:
026:        import javax.xml.transform.Source;
027:        import javax.xml.transform.TransformerFactory;
028:        import javax.xml.transform.Templates;
029:        import javax.xml.transform.Transformer;
030:        import javax.xml.transform.ErrorListener;
031:        import javax.xml.transform.TransformerException;
032:        import javax.xml.transform.stream.StreamSource;
033:        import javax.xml.transform.stream.StreamResult;
034:        import java.io.File;
035:        import java.io.FileInputStream;
036:        import java.io.FileOutputStream;
037:        import java.io.InputStream;
038:        import java.io.OutputStream;
039:        import java.io.ByteArrayOutputStream;
040:        import java.io.ByteArrayInputStream;
041:        import java.io.IOException;
042:
043:        /**
044:         * An XML pretty printer based on xsl stylesheets
045:         */
046:        public class XMLPrettyPrinter {
047:
048:            private static final Log log = LogFactory
049:                    .getLog(XMLPrettyPrinter.class);
050:
051:            /**
052:             * Pretty prints contents of the xml file.
053:             *
054:             * @param file
055:             */
056:            public static void prettify(final File file) {
057:                InputStream inputStream = null;
058:                FileOutputStream outputStream = null;
059:                byte[] byteArray = null;
060:                try {
061:                    byteArray = IOUtils
062:                            .getStreamAsByteArray(new FileInputStream(file));
063:                    inputStream = new ByteArrayInputStream(byteArray);
064:                    outputStream = new FileOutputStream(file);
065:
066:                    Source stylesheetSource = new StreamSource(
067:                            new ByteArrayInputStream(prettyPrintStylesheet
068:                                    .getBytes()));
069:                    Source xmlSource = new StreamSource(inputStream);
070:
071:                    TransformerFactory tf = TransformerFactory.newInstance();
072:                    Templates templates = tf.newTemplates(stylesheetSource);
073:                    Transformer transformer = templates.newTransformer();
074:                    transformer.setErrorListener(new ErrorListener() {
075:                        public void warning(TransformerException exception)
076:                                throws TransformerException {
077:                            log.warn(
078:                                    "Exception occurred while trying to pretty print file "
079:                                            + file, exception);
080:                        }
081:
082:                        public void error(TransformerException exception)
083:                                throws TransformerException {
084:                            log.error(
085:                                    "Exception occurred while trying to pretty print file "
086:                                            + file, exception);
087:                        }
088:
089:                        public void fatalError(TransformerException exception)
090:                                throws TransformerException {
091:                            log.error(
092:                                    "Exception occurred while trying to pretty print file "
093:                                            + file, exception);
094:                        }
095:                    });
096:                    transformer.transform(xmlSource, new StreamResult(
097:                            outputStream));
098:
099:                    inputStream.close();
100:                    outputStream.close();
101:                    log.debug("Pretty printed file : " + file);
102:                } catch (Throwable t) {
103:                    log.debug(
104:                            "Exception occurred while trying to pretty print file "
105:                                    + file, t);
106:                    try {
107:                        if (byteArray != null) {
108:                            outputStream = new FileOutputStream(file);
109:                            outputStream.write(byteArray);
110:                        }
111:                    } catch (IOException e) {
112:                        log.debug(e.getMessage(), e);
113:                    }
114:                } finally {
115:                    if (inputStream != null) {
116:                        try {
117:                            inputStream.close();
118:                        } catch (IOException e) {
119:                            log.debug(e.getMessage(), e);
120:                        }
121:                    }
122:                    if (outputStream != null) {
123:                        try {
124:                            outputStream.close();
125:                        } catch (IOException e) {
126:                            log.debug(e.getMessage(), e);
127:                        }
128:                    }
129:                }
130:            }
131:
132:            private static final String prettyPrintStylesheet = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0' "
133:                    + " xmlns:xalan='http://xml.apache.org/xslt' "
134:                    + " exclude-result-prefixes='xalan'>"
135:                    + "  <xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>"
136:                    + "  <xsl:strip-space elements='*'/>"
137:                    + "  <xsl:template match='/'>"
138:                    + "    <xsl:apply-templates/>"
139:                    + "  </xsl:template>"
140:                    + "  <xsl:template match='node() | @*'>"
141:                    + "        <xsl:copy>"
142:                    + "          <xsl:apply-templates select='node() | @*'/>"
143:                    + "        </xsl:copy>"
144:                    + "  </xsl:template>"
145:                    + "</xsl:stylesheet>";
146:
147:            public static void prettify(OMElement wsdlElement, OutputStream out)
148:                    throws Exception {
149:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
150:                wsdlElement.serialize(baos);
151:
152:                Source stylesheetSource = new StreamSource(
153:                        new ByteArrayInputStream(prettyPrintStylesheet
154:                                .getBytes()));
155:                Source xmlSource = new StreamSource(new ByteArrayInputStream(
156:                        baos.toByteArray()));
157:
158:                TransformerFactory tf = TransformerFactory.newInstance();
159:                Templates templates = tf.newTemplates(stylesheetSource);
160:                Transformer transformer = templates.newTransformer();
161:                transformer.transform(xmlSource, new StreamResult(out));
162:            }
163:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.