Coverage Report - org.mule.module.xml.filters.AbstractJaxpFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJaxpFilter
0%
0/43
0%
0/22
4.6
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.xml.filters;
 8  
 
 9  
 import org.mule.RequestContext;
 10  
 import org.mule.api.lifecycle.InitialisationException;
 11  
 import org.mule.api.transport.OutputHandler;
 12  
 import org.mule.module.xml.transformer.DelayedResult;
 13  
 import org.mule.module.xml.transformer.XmlToDomDocument;
 14  
 import org.mule.transformer.types.DataTypeFactory;
 15  
 
 16  
 import java.io.ByteArrayInputStream;
 17  
 import java.io.InputStream;
 18  
 import java.io.StringReader;
 19  
 
 20  
 import javax.xml.parsers.DocumentBuilderFactory;
 21  
 import javax.xml.stream.XMLStreamConstants;
 22  
 import javax.xml.stream.XMLStreamReader;
 23  
 import javax.xml.transform.dom.DOMResult;
 24  
 
 25  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 26  
 import org.dom4j.dom.DOMDocument;
 27  
 import org.w3c.dom.Document;
 28  
 import org.w3c.dom.Node;
 29  
 import org.xml.sax.InputSource;
 30  
 
 31  
 /**
 32  
  * Common filter functionality for filters which need to convert payloads to {@link Document}s.
 33  
  */
 34  
 public abstract class AbstractJaxpFilter
 35  
 {
 36  
 
 37  0
     private XmlToDomDocument xmlToDom = new XmlToDomDocument();
 38  
 
 39  
     private DocumentBuilderFactory documentBuilderFactory;
 40  
     
 41  
     public AbstractJaxpFilter()
 42  
     {
 43  0
         super();
 44  0
         xmlToDom.setReturnDataType(DataTypeFactory.create(Document.class));
 45  0
     }
 46  
     public void initialise() throws InitialisationException
 47  
     {
 48  0
         if (getDocumentBuilderFactory() == null)
 49  
         {
 50  0
             DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 51  0
             builderFactory.setNamespaceAware(true);
 52  0
             setDocumentBuilderFactory(builderFactory);
 53  
         }
 54  0
     }
 55  
     
 56  
     public Node toDOMNode(Object src) throws Exception
 57  
     {
 58  0
         if (src instanceof Node)
 59  
         {
 60  0
             return (Document) src;
 61  
         }
 62  0
         else if (src instanceof org.dom4j.Document)
 63  
         {
 64  0
             org.dom4j.Document dom4j = (org.dom4j.Document) src;
 65  0
             DOMDocument dom = new DOMDocument();
 66  0
             dom.setDocument(dom4j);
 67  0
             return dom;
 68  
         }
 69  0
         else if (src instanceof OutputHandler)
 70  
         {
 71  0
             OutputHandler handler = ((OutputHandler) src);
 72  0
             ByteArrayOutputStream output = new ByteArrayOutputStream();
 73  0
             handler.write(RequestContext.getEvent(), output);
 74  0
             InputStream stream = new ByteArrayInputStream(output.toByteArray());
 75  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
 76  
         }
 77  0
         else if (src instanceof byte[])
 78  
         {
 79  0
             ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src);
 80  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
 81  
         }
 82  0
         else if (src instanceof InputStream)
 83  
         {
 84  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse((InputStream) src);
 85  
         }
 86  0
         else if (src instanceof String)
 87  
         {
 88  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(
 89  
                 new InputSource(new StringReader((String) src)));
 90  
         }
 91  0
         else if (src instanceof XMLStreamReader)
 92  
         {
 93  0
             XMLStreamReader xsr = (XMLStreamReader) src;
 94  
     
 95  
             // StaxSource requires that we advance to a start element/document event
 96  0
             if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT)
 97  
             {
 98  0
                 xsr.nextTag();
 99  
             }
 100  
     
 101  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource());
 102  
         }
 103  0
         else if (src instanceof DelayedResult)
 104  
         {
 105  0
             DelayedResult result = ((DelayedResult) src);
 106  0
             DOMResult domResult = new DOMResult();
 107  0
             result.write(domResult);
 108  0
             return domResult.getNode();
 109  
         }
 110  
         else
 111  
         {
 112  0
             return (Node) xmlToDom.transform(src);
 113  
         }
 114  
     }
 115  
 
 116  
     /**
 117  
      * The document builder factory to use in case XML needs to be parsed.
 118  
      * 
 119  
      * @return The document builder factory to use in case XML needs to be parsed.
 120  
      */
 121  
     public DocumentBuilderFactory getDocumentBuilderFactory()
 122  
     {
 123  0
         return documentBuilderFactory;
 124  
     }
 125  
 
 126  
     /**
 127  
      * The document builder factory to use in case XML needs to be parsed.
 128  
      * 
 129  
      * @param documentBuilderFactory The document builder factory to use in case XML
 130  
      *            needs to be parsed.
 131  
      */
 132  
     public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory)
 133  
     {
 134  0
         this.documentBuilderFactory = documentBuilderFactory;
 135  0
     }
 136  
 }