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