View Javadoc

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