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