1
2
3
4
5
6
7
8
9
10 package org.dom4j.xpath;
11
12 import java.util.ArrayList;
13
14 import org.dom4j.InvalidXPathException;
15 import org.dom4j.Node;
16 import org.dom4j.XPathException;
17 import org.jaxen.Context;
18 import org.jaxen.ContextSupport;
19 import org.jaxen.JaxenException;
20 import org.jaxen.SimpleNamespaceContext;
21 import org.jaxen.SimpleVariableContext;
22 import org.jaxen.VariableContext;
23 import org.jaxen.XPathFunctionContext;
24 import org.jaxen.dom4j.DocumentNavigator;
25 import org.jaxen.pattern.Pattern;
26 import org.jaxen.pattern.PatternParser;
27 import org.jaxen.saxpath.SAXPathException;
28
29 /*** <p><code>XPathPattern</code> is an implementation of Pattern
30 * which uses an XPath xpath.</p>
31 *
32 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33 * @version $Revision: 1.16 $
34 */
35 public class XPathPattern implements org.dom4j.rule.Pattern {
36
37 private String text;
38 private Pattern pattern;
39 private Context context;
40
41
42 public XPathPattern(Pattern pattern) {
43 this.pattern = pattern;
44 this.text = pattern.getText();
45 this.context = new Context( getContextSupport() );
46 }
47
48 public XPathPattern(String text) {
49 this.text = text;
50 this.context = new Context( getContextSupport() );
51 try {
52 this.pattern = PatternParser.parse( text );
53 }
54 catch (SAXPathException e) {
55 throw new InvalidXPathException( text, e.getMessage() );
56 }
57 catch (RuntimeException e) {
58 throw new InvalidXPathException( text );
59 }
60 }
61
62 public boolean matches( Node node ) {
63 try {
64 ArrayList list = new ArrayList(1);
65 list.add( node );
66 context.setNodeSet( list );
67 return pattern.matches( node, context );
68 }
69 catch (JaxenException e) {
70 handleJaxenException(e);
71 return false;
72 }
73 }
74
75 public String getText() {
76 return text;
77 }
78
79
80 public double getPriority() {
81 return pattern.getPriority();
82 }
83
84 public org.dom4j.rule.Pattern[] getUnionPatterns() {
85 Pattern[] patterns = pattern.getUnionPatterns();
86 if ( patterns != null ) {
87 int size = patterns.length;
88 XPathPattern[] answer = new XPathPattern[size];
89 for ( int i = 0; i < size; i++ ) {
90 answer[i] = new XPathPattern( patterns[i] );
91 }
92 return answer;
93 }
94 return null;
95 }
96
97 public short getMatchType() {
98 return pattern.getMatchType();
99 }
100
101 public String getMatchesNodeName() {
102 return pattern.getMatchesNodeName();
103 }
104
105 public void setVariableContext(VariableContext variableContext) {
106 context.getContextSupport().setVariableContext( variableContext );
107 }
108
109
110 public String toString() {
111 return "[XPathPattern: text: " + text + " Pattern: " + pattern + "]";
112 }
113
114 protected ContextSupport getContextSupport() {
115 return new ContextSupport(
116 new SimpleNamespaceContext(),
117 XPathFunctionContext.getInstance(),
118 new SimpleVariableContext(),
119 DocumentNavigator.getInstance()
120 );
121 }
122
123 protected void handleJaxenException(JaxenException e) throws XPathException {
124 throw new XPathException(text, e);
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174