%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.xmlunit.XMLUnitTagSupport |
|
|
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 | 15 | 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 | 0 | if (saxReader == null) { |
40 | 0 | saxReader = createSAXReader(); |
41 | } |
|
42 | 0 | return saxReader; |
43 | } |
|
44 | ||
45 | /** Sets the SAXReader used for parsing */ |
|
46 | public void setSAXReader(SAXReader saxReader) { |
|
47 | 0 | this.saxReader = saxReader; |
48 | 0 | } |
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 | 8 | SAXContentHandler handler = new SAXContentHandler(); |
60 | 8 | XMLOutput newOutput = new XMLOutput(handler); |
61 | try { |
|
62 | 8 | handler.startDocument(); |
63 | 8 | invokeBody(newOutput); |
64 | 8 | handler.endDocument(); |
65 | 8 | } |
66 | catch (SAXException e) { |
|
67 | 0 | throw new JellyTagException(e); |
68 | } |
|
69 | 8 | return handler.getDocument(); |
70 | } |
|
71 | ||
72 | /** |
|
73 | * Parses the given source |
|
74 | */ |
|
75 | protected Document parse(Object source) throws JellyTagException { |
|
76 | try { |
|
77 | 13 | if (source instanceof Document) { |
78 | 12 | return (Document) source; |
79 | 1 | } else if (source instanceof String) { |
80 | 0 | String uri = (String) source; |
81 | 0 | InputStream in = context.getResourceAsStream(uri); |
82 | 0 | return getSAXReader().read(in, uri); |
83 | 1 | } else if (source instanceof Reader) { |
84 | 0 | return getSAXReader().read((Reader) source); |
85 | 1 | } else if (source instanceof InputStream) { |
86 | 0 | return getSAXReader().read((InputStream) source); |
87 | 1 | } else if (source instanceof URL) { |
88 | 0 | return getSAXReader().read((URL) source); |
89 | } else { |
|
90 | 1 | 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 | 0 | throw new JellyTagException(e); |
100 | } |
|
101 | } |
|
102 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |