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: }
|