View Javadoc

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