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 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
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 |
0
| public XPathPattern(Pattern pattern) {
|
43 |
0
| this.pattern = pattern;
|
44 |
0
| this.text = pattern.getText();
|
45 |
0
| this.context = new Context( getContextSupport() );
|
46 |
| } |
47 |
| |
48 |
72
| public XPathPattern(String text) {
|
49 |
72
| this.text = text;
|
50 |
72
| this.context = new Context( getContextSupport() );
|
51 |
72
| try {
|
52 |
72
| this.pattern = PatternParser.parse( text );
|
53 |
| } |
54 |
| catch (SAXPathException e) { |
55 |
0
| throw new InvalidXPathException( text, e.getMessage() );
|
56 |
| } |
57 |
| catch (RuntimeException e) { |
58 |
0
| throw new InvalidXPathException( text );
|
59 |
| } |
60 |
| } |
61 |
| |
62 |
120
| public boolean matches( Node node ) {
|
63 |
120
| try {
|
64 |
120
| ArrayList list = new ArrayList(1);
|
65 |
120
| list.add( node );
|
66 |
120
| context.setNodeSet( list );
|
67 |
120
| return pattern.matches( node, context );
|
68 |
| } |
69 |
| catch (JaxenException e) { |
70 |
0
| handleJaxenException(e);
|
71 |
0
| return false;
|
72 |
| } |
73 |
| } |
74 |
| |
75 |
0
| public String getText() {
|
76 |
0
| return text;
|
77 |
| } |
78 |
| |
79 |
| |
80 |
42
| public double getPriority() {
|
81 |
42
| return pattern.getPriority();
|
82 |
| } |
83 |
| |
84 |
26
| public org.dom4j.rule.Pattern[] getUnionPatterns() {
|
85 |
26
| Pattern[] patterns = pattern.getUnionPatterns();
|
86 |
26
| if ( patterns != null ) {
|
87 |
0
| int size = patterns.length;
|
88 |
0
| XPathPattern[] answer = new XPathPattern[size];
|
89 |
0
| for ( int i = 0; i < size; i++ ) {
|
90 |
0
| answer[i] = new XPathPattern( patterns[i] );
|
91 |
| } |
92 |
0
| return answer;
|
93 |
| } |
94 |
26
| return null;
|
95 |
| } |
96 |
| |
97 |
26
| public short getMatchType() {
|
98 |
26
| return pattern.getMatchType();
|
99 |
| } |
100 |
| |
101 |
26
| public String getMatchesNodeName() {
|
102 |
26
| return pattern.getMatchesNodeName();
|
103 |
| } |
104 |
| |
105 |
0
| public void setVariableContext(VariableContext variableContext) {
|
106 |
0
| context.getContextSupport().setVariableContext( variableContext );
|
107 |
| } |
108 |
| |
109 |
| |
110 |
68
| public String toString() {
|
111 |
68
| return "[XPathPattern: text: " + text + " Pattern: " + pattern + "]";
|
112 |
| } |
113 |
| |
114 |
72
| protected ContextSupport getContextSupport() {
|
115 |
72
| return new ContextSupport(
|
116 |
| new SimpleNamespaceContext(), |
117 |
| XPathFunctionContext.getInstance(), |
118 |
| new SimpleVariableContext(), |
119 |
| DocumentNavigator.getInstance() |
120 |
| ); |
121 |
| } |
122 |
| |
123 |
0
| protected void handleJaxenException(JaxenException e) throws XPathException {
|
124 |
0
| 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 |
| |