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