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  
  * $Id: AbstractJaxpFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
 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.filters;
 12  
 
 13  
 import org.mule.RequestContext;
 14  
 import org.mule.api.lifecycle.InitialisationException;
 15  
 import org.mule.api.transport.OutputHandler;
 16  
 import org.mule.module.xml.transformer.DelayedResult;
 17  
 import org.mule.module.xml.transformer.XmlToDomDocument;
 18  
 import org.mule.transformer.types.DataTypeFactory;
 19  
 
 20  
 import java.io.ByteArrayInputStream;
 21  
 import java.io.InputStream;
 22  
 import java.io.StringReader;
 23  
 
 24  
 import javax.xml.parsers.DocumentBuilderFactory;
 25  
 import javax.xml.stream.XMLStreamConstants;
 26  
 import javax.xml.stream.XMLStreamReader;
 27  
 import javax.xml.transform.dom.DOMResult;
 28  
 
 29  
 import org.apache.commons.io.output.ByteArrayOutputStream;
 30  
 import org.dom4j.dom.DOMDocument;
 31  
 import org.w3c.dom.Document;
 32  
 import org.w3c.dom.Node;
 33  
 import org.xml.sax.InputSource;
 34  
 
 35  
 /**
 36  
  * Common filter functionality for filters which need to convert payloads to {@link Document}s.
 37  
  */
 38  
 public abstract class AbstractJaxpFilter
 39  
 {
 40  
 
 41  0
     private XmlToDomDocument xmlToDom = new XmlToDomDocument();
 42  
 
 43  
     private DocumentBuilderFactory documentBuilderFactory;
 44  
     
 45  
     public AbstractJaxpFilter()
 46  
     {
 47  0
         super();
 48  0
         xmlToDom.setReturnDataType(DataTypeFactory.create(Document.class));
 49  0
     }
 50  
     public void initialise() throws InitialisationException
 51  
     {
 52  0
         if (getDocumentBuilderFactory() == null)
 53  
         {
 54  0
             DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
 55  0
             builderFactory.setNamespaceAware(true);
 56  0
             setDocumentBuilderFactory(builderFactory);
 57  
         }
 58  0
     }
 59  
     
 60  
     public Node toDOMNode(Object src) throws Exception
 61  
     {
 62  0
         if (src instanceof Node)
 63  
         {
 64  0
             return (Document) src;
 65  
         }
 66  0
         else if (src instanceof org.dom4j.Document)
 67  
         {
 68  0
             org.dom4j.Document dom4j = (org.dom4j.Document) src;
 69  0
             DOMDocument dom = new DOMDocument();
 70  0
             dom.setDocument(dom4j);
 71  0
             return dom;
 72  
         }
 73  0
         else if (src instanceof OutputHandler)
 74  
         {
 75  0
             OutputHandler handler = ((OutputHandler) src);
 76  0
             ByteArrayOutputStream output = new ByteArrayOutputStream();
 77  0
             handler.write(RequestContext.getEvent(), output);
 78  0
             InputStream stream = new ByteArrayInputStream(output.toByteArray());
 79  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
 80  
         }
 81  0
         else if (src instanceof byte[])
 82  
         {
 83  0
             ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src);
 84  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
 85  
         }
 86  0
         else if (src instanceof InputStream)
 87  
         {
 88  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse((InputStream) src);
 89  
         }
 90  0
         else if (src instanceof String)
 91  
         {
 92  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(
 93  
                 new InputSource(new StringReader((String) src)));
 94  
         }
 95  0
         else if (src instanceof XMLStreamReader)
 96  
         {
 97  0
             XMLStreamReader xsr = (XMLStreamReader) src;
 98  
     
 99  
             // StaxSource requires that we advance to a start element/document event
 100  0
             if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT)
 101  
             {
 102  0
                 xsr.nextTag();
 103  
             }
 104  
     
 105  0
             return getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource());
 106  
         }
 107  0
         else if (src instanceof DelayedResult)
 108  
         {
 109  0
             DelayedResult result = ((DelayedResult) src);
 110  0
             DOMResult domResult = new DOMResult();
 111  0
             result.write(domResult);
 112  0
             return domResult.getNode();
 113  
         }
 114  
         else
 115  
         {
 116  0
             return (Node) xmlToDom.transform(src);
 117  
         }
 118  
     }
 119  
 
 120  
     /**
 121  
      * The document builder factory to use in case XML needs to be parsed.
 122  
      * 
 123  
      * @return The document builder factory to use in case XML needs to be parsed.
 124  
      */
 125  
     public DocumentBuilderFactory getDocumentBuilderFactory()
 126  
     {
 127  0
         return documentBuilderFactory;
 128  
     }
 129  
 
 130  
     /**
 131  
      * The document builder factory to use in case XML needs to be parsed.
 132  
      * 
 133  
      * @param documentBuilderFactory The document builder factory to use in case XML
 134  
      *            needs to be parsed.
 135  
      */
 136  
     public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory)
 137  
     {
 138  0
         this.documentBuilderFactory = documentBuilderFactory;
 139  0
     }
 140  
 }