2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
8
package org.dom4j.xpath;
10
import java.util.ArrayList;
12
import org.dom4j.InvalidXPathException;
13
import org.dom4j.Node;
14
import org.dom4j.XPathException;
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;
30
* <code>XPathPattern</code> is an implementation of Pattern which uses an
34
* @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
35
* @version $Revision: 1.18.2.1 $
37
public class XPathPattern implements org.dom4j.rule.Pattern {
40
private Pattern pattern;
42
private Context context;
44
public XPathPattern(Pattern pattern) {
45
this.pattern = pattern;
46
this.text = pattern.getText();
47
this.context = new Context(getContextSupport());
50
public XPathPattern(String text) {
52
this.context = new Context(getContextSupport());
55
this.pattern = PatternParser.parse(text);
56
} catch (SAXPathException e) {
57
throw new InvalidXPathException(text, e.getMessage());
58
} catch (Throwable t) {
59
throw new InvalidXPathException(text, t);
63
public boolean matches(Node node) {
65
ArrayList list = new ArrayList(1);
67
context.setNodeSet(list);
69
return pattern.matches(node, context);
70
} catch (JaxenException e) {
71
handleJaxenException(e);
77
public String getText() {
81
public double getPriority() {
82
return pattern.getPriority();
85
public org.dom4j.rule.Pattern[] getUnionPatterns() {
86
Pattern[] patterns = pattern.getUnionPatterns();
88
if (patterns != null) {
89
int size = patterns.length;
90
XPathPattern[] answer = new XPathPattern[size];
92
for (int i = 0; i < size; i++) {
93
answer[i] = new XPathPattern(patterns[i]);
102
public short getMatchType() {
103
return pattern.getMatchType();
106
public String getMatchesNodeName() {
107
return pattern.getMatchesNodeName();
110
public void setVariableContext(VariableContext variableContext) {
111
context.getContextSupport().setVariableContext(variableContext);
114
public String toString() {
115
return "[XPathPattern: text: " + text + " Pattern: " + pattern + "]";
118
protected ContextSupport getContextSupport() {
119
return new ContextSupport(new SimpleNamespaceContext(),
120
XPathFunctionContext.getInstance(),
121
new SimpleVariableContext(), DocumentNavigator.getInstance());
124
protected void handleJaxenException(JaxenException exception)
125
throws XPathException {
126
throw new XPathException(text, exception);
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:
135
* 1. Redistributions of source code must retain copyright statements and
136
* notices. Redistributions must also contain a copy of this document.
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.
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.
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.
150
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
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.
164
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.