View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.xmlunit;
18  
19  import java.io.InputStream;
20  import java.io.Reader;
21  import java.net.URL;
22  
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.XMLOutput;
25  import org.apache.commons.jelly.tags.junit.AssertTagSupport;
26  import org.dom4j.Document;
27  import org.dom4j.DocumentException;
28  import org.dom4j.io.SAXContentHandler;
29  import org.dom4j.io.SAXReader;
30  import org.xml.sax.SAXException;
31  
32  public abstract class XMLUnitTagSupport extends AssertTagSupport {
33  
34      /*** The SAXReader used to parser the document */
35      private SAXReader saxReader;
36  
37      /*** @return the SAXReader used for parsing, creating one lazily if need be  */
38      public SAXReader getSAXReader() {
39          if (saxReader == null) {
40              saxReader = createSAXReader();
41          }
42          return saxReader;
43      }
44  
45      /*** Sets the SAXReader used for parsing */
46      public void setSAXReader(SAXReader saxReader) {
47          this.saxReader = saxReader;
48      }
49  
50      /***
51       * Factory method to create a new SAXReader
52       */
53      protected abstract SAXReader createSAXReader();
54  
55      /***
56       * Parses the body of this tag and returns the parsed document
57       */
58      protected Document parseBody() throws JellyTagException {
59          SAXContentHandler handler = new SAXContentHandler();
60          XMLOutput newOutput = new XMLOutput(handler);
61          try {
62              handler.startDocument();
63              invokeBody(newOutput);
64              handler.endDocument();
65          }
66          catch (SAXException e) {
67              throw new JellyTagException(e);
68          }
69          return handler.getDocument();
70      }
71  
72      /***
73       * Parses the given source
74       */
75      protected Document parse(Object source) throws JellyTagException {
76          try {
77              if (source instanceof Document) {
78                  return (Document) source;
79              } else if (source instanceof String) {
80                  String uri = (String) source;
81                  InputStream in = context.getResourceAsStream(uri);
82                  return getSAXReader().read(in, uri);
83              } else if (source instanceof Reader) {
84                  return getSAXReader().read((Reader) source);
85              } else if (source instanceof InputStream) {
86                  return getSAXReader().read((InputStream) source);
87              } else if (source instanceof URL) {
88                  return getSAXReader().read((URL) source);
89              } else {
90                  throw new IllegalArgumentException(
91                      "Invalid source argument. Must be a Document, String, Reader, InputStream or URL."
92                          + " Was type: "
93                          + source.getClass().getName()
94                          + " with value: "
95                          + source);
96              }
97          }
98          catch (DocumentException e) {
99              throw new JellyTagException(e);
100         }
101     }
102 }