Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00

file stats: | LOC: | 165 |  | Methods: | 12
| NCLOC: | 87 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

XPathPattern.java

25%

53,1%

66,7%

54,2%

1 |  | /*
2 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3 |  | *
4 |  | * This software is open source.
5 |  | * See the bottom of this file for the licence.
6 |  | */
7 |  |
8 |  | package org.dom4j.xpath;
9 |  |
10 |  | import java.util.ArrayList;
11 |  |
12 |  | import org.dom4j.InvalidXPathException;
13 |  | import org.dom4j.Node;
14 |  | import org.dom4j.XPathException;
15 |  |
16 |  | import org.jaxen.Context;
17 |  | import org.jaxen.ContextSupport;
18 |  | import org.jaxen.JaxenException;
19 |  | import org.jaxen.SimpleNamespaceContext;
20 |  | import org.jaxen.SimpleVariableContext;
21 |  | import org.jaxen.VariableContext;
22 |  | import org.jaxen.XPathFunctionContext;
23 |  | import org.jaxen.dom4j.DocumentNavigator;
24 |  | import org.jaxen.pattern.Pattern;
25 |  | import org.jaxen.pattern.PatternParser;
26 |  | import org.jaxen.saxpath.SAXPathException;
27 |  |
28 |  | /**
29 |  | * <p>
30 |  | * <code>XPathPattern</code> is an implementation of Pattern which uses an
31 |  | * XPath xpath.
32 |  | * </p>
33 |  | *
34 |  | * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
35 |  | * @version $Revision: 1.18.2.1 $
36 |  | */
37 |  | public class XPathPattern implements org.dom4j.rule.Pattern {
38 |  | private String text;
39 |  |
40 |  | private Pattern pattern;
41 |  |
42 |  | private Context context;
43 |  |
44 | 0 | public XPathPattern(Pattern pattern) {
45 | 0 | this.pattern = pattern;
46 | 0 | this.text = pattern.getText();
47 | 0 | this.context = new Context(getContextSupport());
48 |  | }
49 |  |
50 | 37 | public XPathPattern(String text) {
51 | 37 | this.text = text;
52 | 37 | this.context = new Context(getContextSupport());
53 |  |
54 | 37 | try {
55 | 37 | this.pattern = PatternParser.parse(text);
56 |  | } catch (SAXPathException e) {
57 | 0 | throw new InvalidXPathException(text, e.getMessage());
58 |  | } catch (Throwable t) {
59 | 0 | throw new InvalidXPathException(text, t);
60 |  | }
61 |  | }
62 |  |
63 | 47 | public boolean matches(Node node) {
64 | 47 | try {
65 | 47 | ArrayList list = new ArrayList(1);
66 | 47 | list.add(node);
67 | 47 | context.setNodeSet(list);
68 |  |
69 | 47 | return pattern.matches(node, context);
70 |  | } catch (JaxenException e) {
71 | 0 | handleJaxenException(e);
72 |  |
73 | 0 | return false;
74 |  | }
75 |  | }
76 |  |
77 | 0 | public String getText() {
78 | 0 | return text;
79 |  | }
80 |  |
81 | 22 | public double getPriority() {
82 | 22 | return pattern.getPriority();
83 |  | }
84 |  |
85 | 14 | public org.dom4j.rule.Pattern[] getUnionPatterns() {
86 | 14 | Pattern[] patterns = pattern.getUnionPatterns();
87 |  |
88 | 14 | if (patterns != null) {
89 | 0 | int size = patterns.length;
90 | 0 | XPathPattern[] answer = new XPathPattern[size];
91 |  |
92 | 0 | for (int i = 0; i < size; i++) {
93 | 0 | answer[i] = new XPathPattern(patterns[i]);
94 |  | }
95 |  |
96 | 0 | return answer;
97 |  | }
98 |  |
99 | 14 | return null;
100 |  | }
101 |  |
102 | 14 | public short getMatchType() {
103 | 14 | return pattern.getMatchType();
104 |  | }
105 |  |
106 | 14 | public String getMatchesNodeName() {
107 | 14 | return pattern.getMatchesNodeName();
108 |  | }
109 |  |
110 | 0 | public void setVariableContext(VariableContext variableContext) {
111 | 0 | context.getContextSupport().setVariableContext(variableContext);
112 |  | }
113 |  |
114 | 19 | public String toString() {
115 | 19 | return "[XPathPattern: text: " + text + " Pattern: " + pattern + "]";
116 |  | }
117 |  |
118 | 37 | protected ContextSupport getContextSupport() {
119 | 37 | return new ContextSupport(new SimpleNamespaceContext(),
120 |  | XPathFunctionContext.getInstance(),
121 |  | new SimpleVariableContext(), DocumentNavigator.getInstance());
122 |  | }
123 |  |
124 | 0 | protected void handleJaxenException(JaxenException exception)
125 |  | throws XPathException {
126 | 0 | throw new XPathException(text, exception);
127 |  | }
128 |  | }
129 |  |
130 |  | /*
131 |  | * Redistribution and use of this software and associated documentation
132 |  | * ("Software"), with or without modification, are permitted provided that the
133 |  | * following conditions are met:
134 |  | *
135 |  | * 1. Redistributions of source code must retain copyright statements and
136 |  | * notices. Redistributions must also contain a copy of this document.
137 |  | *
138 |  | * 2. Redistributions in binary form must reproduce the above copyright notice,
139 |  | * this list of conditions and the following disclaimer in the documentation
140 |  | * and/or other materials provided with the distribution.
141 |  | *
142 |  | * 3. The name "DOM4J" must not be used to endorse or promote products derived
143 |  | * from this Software without prior written permission of MetaStuff, Ltd. For
144 |  | * written permission, please contact dom4j-info@metastuff.com.
145 |  | *
146 |  | * 4. Products derived from this Software may not be called "DOM4J" nor may
147 |  | * "DOM4J" appear in their names without prior written permission of MetaStuff,
148 |  | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
149 |  | *
150 |  | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
151 |  | *
152 |  | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
153 |  | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
154 |  | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
155 |  | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
156 |  | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
157 |  | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
158 |  | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
159 |  | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
160 |  | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
161 |  | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
162 |  | * POSSIBILITY OF SUCH DAMAGE.
163 |  | *
164 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
165 |  | */