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.util;
8   
9   import org.mule.util.IOUtils;
10  
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import javax.xml.parsers.DocumentBuilderFactory;
17  import javax.xml.parsers.ParserConfigurationException;
18  import javax.xml.stream.XMLInputFactory;
19  import javax.xml.stream.XMLStreamException;
20  import javax.xml.stream.XMLStreamReader;
21  import javax.xml.transform.Source;
22  
23  import org.dom4j.Document;
24  import org.dom4j.DocumentException;
25  import org.dom4j.DocumentHelper;
26  import org.xml.sax.InputSource;
27  import org.xml.sax.SAXException;
28  
29  public class XMLTestUtils
30  {
31  
32      public static List<?> getXmlMessageVariants(String resource) throws Exception
33      {
34          List<Object> list = new ArrayList<Object>();
35  
36          list.add(toInputStream(resource));
37          list.add(toDom4jDocument(resource));
38          list.add(toW3cDocument(resource));
39          list.add(toInputSource(resource));
40          list.add(toSource(resource));
41          list.add(toXmlStreamReader(resource));
42  
43          return list;
44      }
45  
46      public static XMLStreamReader toXmlStreamReader(String resource)
47              throws IOException, XMLStreamException
48      {
49          InputStream is = toInputStream(resource);
50  
51          return XMLUtils.toXMLStreamReader(XMLInputFactory.newInstance(), is);
52      }
53  
54      public static Source toSource(String resource) throws Exception
55      {
56          InputStream is = toInputStream(resource);
57  
58          return XMLUtils.toXmlSource(XMLInputFactory.newInstance(), false, is);
59      }
60  
61      public static org.w3c.dom.Document toW3cDocument(String resource) throws IOException, SAXException, ParserConfigurationException
62      {
63          InputStream is = toInputStream(resource);
64  
65          return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
66      }
67  
68      public static InputSource toInputSource(String resource) throws IOException
69      {
70          InputStream is = toInputStream(resource);
71  
72          return new InputSource(is);
73      }
74  
75      public static Document toDom4jDocument(String resource) throws IOException, DocumentException
76      {
77          String xml = toString(resource);
78          return DocumentHelper.parseText(xml);
79      }
80  
81      public static String toString(String resource) throws IOException
82      {
83          return IOUtils.getResourceAsString(resource, XMLTestUtils.class);
84      }
85  
86      public static InputStream toInputStream(String resource) throws IOException
87      {
88          return IOUtils.getResourceAsStream(resource, XMLTestUtils.class);
89      }
90  }