Coverage Report - org.mule.module.xml.transformer.XmlPrettyPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlPrettyPrinter
32%
18/56
25%
1/4
1.172
 
 1  
 /*
 2  
  * $Id: XmlPrettyPrinter.java 12273 2008-07-10 13:25:32Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.module.xml.transformer;
 12  
 
 13  
 import org.mule.api.transformer.TransformerException;
 14  
 import org.mule.module.xml.util.XMLUtils;
 15  
 import org.mule.transformer.AbstractTransformer;
 16  
 import org.mule.util.StringUtils;
 17  
 
 18  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 19  
 import org.dom4j.Document;
 20  
 import org.dom4j.DocumentException;
 21  
 import org.dom4j.io.OutputFormat;
 22  
 import org.dom4j.io.XMLWriter;
 23  
 
 24  
 public class XmlPrettyPrinter extends AbstractTransformer
 25  
 {
 26  6
     protected OutputFormat outputFormat = OutputFormat.createPrettyPrint();
 27  
 
 28  
     public XmlPrettyPrinter()
 29  
     {
 30  6
         super();
 31  6
         this.registerSourceType(String.class);
 32  6
         this.registerSourceType(org.dom4j.Document.class);
 33  6
         this.setReturnClass(String.class);
 34  6
     }
 35  
 
 36  
     public synchronized OutputFormat getOutputFormat()
 37  
     {
 38  6
         return outputFormat;
 39  
     }
 40  
 
 41  
     // @Override
 42  
     protected Object doTransform(Object src, String encoding) throws TransformerException
 43  
     {
 44  
         try
 45  
         {
 46  4
             Document document = XMLUtils.toDocument(src);
 47  4
             if (document != null)
 48  
             {
 49  4
                 ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
 50  4
                 XMLWriter writer = new XMLWriter(resultStream, this.getOutputFormat());
 51  4
                 writer.write(document);
 52  4
                 writer.close();
 53  4
                 return resultStream.toString(encoding);
 54  
             }
 55  
             else 
 56  
             {
 57  0
                 throw new DocumentException("Payload is not valid XML");
 58  
             }
 59  
         }
 60  0
         catch (Exception e)
 61  
         {
 62  0
             throw new TransformerException(this, e);
 63  
         }
 64  
     }
 65  
 
 66  
     /**
 67  
      * @see OutputFormat#getEncoding()
 68  
      */
 69  
     public synchronized String getEncoding()
 70  
     {
 71  0
         return outputFormat.getEncoding();
 72  
     }
 73  
 
 74  
     /**
 75  
      * @see OutputFormat#setEncoding(String)
 76  
      */
 77  
     public synchronized void setEncoding(String encoding)
 78  
     {
 79  0
         outputFormat.setEncoding(encoding);
 80  0
     }
 81  
 
 82  
     /**
 83  
      * @see OutputFormat#getIndent()
 84  
      */
 85  
     public synchronized boolean getIndentEnabled()
 86  
     {
 87  0
         return outputFormat.getIndent() != null;
 88  
     }
 89  
 
 90  
     /**
 91  
      * @see OutputFormat#setIndent(boolean)
 92  
      */
 93  
     public synchronized void setIndentEnabled(boolean doIndent)
 94  
     {
 95  0
         outputFormat.setIndent(doIndent);
 96  0
     }
 97  
 
 98  
     /**
 99  
      * @see OutputFormat#getIndent()
 100  
      */
 101  
     public synchronized String getIndentString()
 102  
     {
 103  0
         return outputFormat.getIndent();
 104  
     }
 105  
 
 106  
     /**
 107  
      * @see OutputFormat#setIndent(boolean)
 108  
      */
 109  
     public synchronized void setIndentString(String indentString)
 110  
     {
 111  0
         outputFormat.setIndent(indentString);
 112  0
     }
 113  
 
 114  
     /**
 115  
      * @see OutputFormat#setIndentSize(int)
 116  
      */
 117  
     public synchronized int getIndentSize()
 118  
     {
 119  0
         return StringUtils.defaultIfEmpty(outputFormat.getIndent(), "").length();
 120  
     }
 121  
 
 122  
     /**
 123  
      * @see OutputFormat#setIndentSize(int)
 124  
      */
 125  
     public synchronized void setIndentSize(int indentSize)
 126  
     {
 127  2
         outputFormat.setIndentSize(indentSize);
 128  2
     }
 129  
 
 130  
     /**
 131  
      * @see OutputFormat#getLineSeparator()
 132  
      */
 133  
     public synchronized String getLineSeparator()
 134  
     {
 135  0
         return outputFormat.getLineSeparator();
 136  
     }
 137  
 
 138  
     /**
 139  
      * @see OutputFormat#setLineSeparator(String)
 140  
      */
 141  
     public synchronized void setLineSeparator(String separator)
 142  
     {
 143  0
         outputFormat.setLineSeparator(separator);
 144  0
     }
 145  
 
 146  
     /**
 147  
      * @see OutputFormat#getNewLineAfterNTags()
 148  
      */
 149  
     public synchronized int getNewLineAfterNTags()
 150  
     {
 151  0
         return outputFormat.getNewLineAfterNTags();
 152  
     }
 153  
 
 154  
     /**
 155  
      * @see OutputFormat#setNewLineAfterNTags(int)
 156  
      */
 157  
     public synchronized void setNewLineAfterNTags(int tagCount)
 158  
     {
 159  0
         outputFormat.setNewLineAfterNTags(tagCount);
 160  0
     }
 161  
 
 162  
     /**
 163  
      * @see OutputFormat#isExpandEmptyElements()
 164  
      */
 165  
     public synchronized boolean isExpandEmptyElements()
 166  
     {
 167  0
         return outputFormat.isExpandEmptyElements();
 168  
     }
 169  
 
 170  
     /**
 171  
      * @see OutputFormat#setExpandEmptyElements(boolean)
 172  
      */
 173  
     public synchronized void setExpandEmptyElements(boolean expandEmptyElements)
 174  
     {
 175  0
         outputFormat.setExpandEmptyElements(expandEmptyElements);
 176  0
     }
 177  
 
 178  
     /**
 179  
      * @see OutputFormat#isNewlines()
 180  
      */
 181  
     public synchronized boolean isNewlines()
 182  
     {
 183  0
         return outputFormat.isNewlines();
 184  
     }
 185  
 
 186  
     /**
 187  
      * @see OutputFormat#setNewlines(boolean)
 188  
      */
 189  
     public synchronized void setNewlines(boolean newlines)
 190  
     {
 191  0
         outputFormat.setNewlines(newlines);
 192  0
     }
 193  
 
 194  
     /**
 195  
      * @see OutputFormat#isOmitEncoding()
 196  
      */
 197  
     public synchronized boolean isOmitEncoding()
 198  
     {
 199  0
         return outputFormat.isOmitEncoding();
 200  
     }
 201  
 
 202  
     /**
 203  
      * @see OutputFormat#setOmitEncoding(boolean)
 204  
      */
 205  
     public synchronized void setOmitEncoding(boolean omitEncoding)
 206  
     {
 207  0
         outputFormat.setOmitEncoding(omitEncoding);
 208  0
     }
 209  
 
 210  
     /**
 211  
      * @see OutputFormat#getEncoding()
 212  
      */
 213  
     public synchronized boolean isPadText()
 214  
     {
 215  0
         return outputFormat.isPadText();
 216  
     }
 217  
 
 218  
     /**
 219  
      * @see OutputFormat#getEncoding()
 220  
      */
 221  
     public synchronized void setPadText(boolean padText)
 222  
     {
 223  2
         outputFormat.setPadText(padText);
 224  2
     }
 225  
 
 226  
     /**
 227  
      * @see OutputFormat#getEncoding()
 228  
      */
 229  
     public synchronized boolean isSuppressDeclaration()
 230  
     {
 231  0
         return outputFormat.isSuppressDeclaration();
 232  
     }
 233  
 
 234  
     /**
 235  
      * @see OutputFormat#getEncoding()
 236  
      */
 237  
     public synchronized void setSuppressDeclaration(boolean suppressDeclaration)
 238  
     {
 239  0
         outputFormat.setSuppressDeclaration(suppressDeclaration);
 240  0
     }
 241  
 
 242  
     /**
 243  
      * @see OutputFormat#isTrimText()
 244  
      */
 245  
     public synchronized boolean isTrimText()
 246  
     {
 247  0
         return outputFormat.isTrimText();
 248  
     }
 249  
 
 250  
     /**
 251  
      * @see OutputFormat#setTrimText(boolean)
 252  
      */
 253  
     public synchronized void setTrimText(boolean trimText)
 254  
     {
 255  0
         outputFormat.setTrimText(trimText);
 256  0
     }
 257  
 
 258  
     /**
 259  
      * @see OutputFormat#isXHTML()
 260  
      */
 261  
     public synchronized boolean isXHTML()
 262  
     {
 263  0
         return outputFormat.isXHTML();
 264  
     }
 265  
 
 266  
     /**
 267  
      * @see OutputFormat#setXHTML(boolean)
 268  
      */
 269  
     public synchronized void setXHTML(boolean xhtml)
 270  
     {
 271  0
         outputFormat.setXHTML(xhtml);
 272  0
     }
 273  
 
 274  
 }