View Javadoc
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      private XmlToDomDocument xmlToDom = new XmlToDomDocument();
38  
39      private DocumentBuilderFactory documentBuilderFactory;
40      
41      public AbstractJaxpFilter()
42      {
43          super();
44          xmlToDom.setReturnDataType(DataTypeFactory.create(Document.class));
45      }
46      public void initialise() throws InitialisationException
47      {
48          if (getDocumentBuilderFactory() == null)
49          {
50              DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
51              builderFactory.setNamespaceAware(true);
52              setDocumentBuilderFactory(builderFactory);
53          }
54      }
55      
56      public Node toDOMNode(Object src) throws Exception
57      {
58          if (src instanceof Node)
59          {
60              return (Document) src;
61          }
62          else if (src instanceof org.dom4j.Document)
63          {
64              org.dom4j.Document dom4j = (org.dom4j.Document) src;
65              DOMDocument dom = new DOMDocument();
66              dom.setDocument(dom4j);
67              return dom;
68          }
69          else if (src instanceof OutputHandler)
70          {
71              OutputHandler handler = ((OutputHandler) src);
72              ByteArrayOutputStream output = new ByteArrayOutputStream();
73              handler.write(RequestContext.getEvent(), output);
74              InputStream stream = new ByteArrayInputStream(output.toByteArray());
75              return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
76          }
77          else if (src instanceof byte[])
78          {
79              ByteArrayInputStream stream = new ByteArrayInputStream((byte[]) src);
80              return getDocumentBuilderFactory().newDocumentBuilder().parse(stream);
81          }
82          else if (src instanceof InputStream)
83          {
84              return getDocumentBuilderFactory().newDocumentBuilder().parse((InputStream) src);
85          }
86          else if (src instanceof String)
87          {
88              return getDocumentBuilderFactory().newDocumentBuilder().parse(
89                  new InputSource(new StringReader((String) src)));
90          }
91          else if (src instanceof XMLStreamReader)
92          {
93              XMLStreamReader xsr = (XMLStreamReader) src;
94      
95              // StaxSource requires that we advance to a start element/document event
96              if (!xsr.isStartElement() && xsr.getEventType() != XMLStreamConstants.START_DOCUMENT)
97              {
98                  xsr.nextTag();
99              }
100     
101             return getDocumentBuilderFactory().newDocumentBuilder().parse(new InputSource());
102         }
103         else if (src instanceof DelayedResult)
104         {
105             DelayedResult result = ((DelayedResult) src);
106             DOMResult domResult = new DOMResult();
107             result.write(domResult);
108             return domResult.getNode();
109         }
110         else
111         {
112             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         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         this.documentBuilderFactory = documentBuilderFactory;
135     }
136 }