KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > xpath > XPathPatternImpl


1 /*
2  * Copyright 2001 (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  * $Id: XPathPatternImpl.java,v 1.2 2003/11/02 18:10:04 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.xpath;
11
12 import org.dom4j.InvalidXPathException;
13 import org.dom4j.Node;
14 import org.dom4j.XPathException;
15 import org.jaxen.*;
16 import org.jaxen.dom4j.DocumentNavigator;
17 import org.jaxen.pattern.Pattern;
18 import org.jaxen.pattern.PatternParser;
19 import org.ozoneDB.OzoneInterface;
20 import org.ozoneDB.OzoneObject;
21
22 import java.util.ArrayList JavaDoc;
23
24 /** <p><code>XPathPatternImpl</code> is an implementation of Pattern
25  * which uses an XPath xpath.</p>
26  *
27  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
28  * @author Per Nyfelt
29  * @version $Revision: 1.2 $
30  */

31 public class XPathPatternImpl extends OzoneObject implements XPathPattern {
32
33     final static long serialVersionUID = 1L;
34
35     private String JavaDoc text;
36     private Pattern pattern;
37     private Context context;
38
39     public static XPathPattern create(OzoneInterface db, Pattern pattern) {
40         final Class JavaDoc[] signature = new Class JavaDoc[] {Pattern.class};
41         return (XPathPattern) db.createObject(XPathPatternImpl.class,
42                 signature,
43                 new Object JavaDoc[]{pattern}
44         );
45
46     }
47
48     public XPathPatternImpl(Pattern pattern) {
49         this.pattern = pattern;
50         this.text = pattern.getText();
51         this.context = new Context(getContextSupport());
52     }
53
54     public static XPathPattern create(OzoneInterface db, String JavaDoc text) {
55         final Class JavaDoc[] signature = new Class JavaDoc[] {String JavaDoc.class};
56         return (XPathPattern) db.createObject(XPathPatternImpl.class,
57                 signature,
58                 new Object JavaDoc[]{text}
59         );
60
61     }
62
63     public XPathPatternImpl(String JavaDoc text) {
64         this.text = text;
65         this.context = new Context(getContextSupport());
66         try {
67             this.pattern = PatternParser.parse(text);
68         } catch (Exception JavaDoc e) {
69             throw new InvalidXPathException(text, e.getMessage());
70         }
71     }
72
73     public boolean matches(Node node) {
74         try {
75             ArrayList JavaDoc list = new ArrayList JavaDoc(1);
76             list.add(node);
77             context.setNodeSet(list);
78             return pattern.matches(node, context);
79         } catch (JaxenException e) {
80             handleJaxenException(e);
81             return false;
82         }
83     }
84
85     public String JavaDoc getText() {
86         return text;
87     }
88
89
90     public double getPriority() {
91         return pattern.getPriority();
92     }
93
94     public org.dom4j.rule.Pattern[] getUnionPatterns() {
95         Pattern[] patterns = pattern.getUnionPatterns();
96         if (patterns != null) {
97             int size = patterns.length;
98             XPathPattern[] answer = new XPathPattern[size];
99             for (int i = 0; i < size; i++) {
100                 answer[i] = create(database(), patterns[i]);
101             }
102             return answer;
103         }
104         return null;
105     }
106
107     public short getMatchType() {
108         return pattern.getMatchType();
109     }
110
111     public String JavaDoc getMatchesNodeName() {
112         return pattern.getMatchesNodeName();
113     }
114
115     public void setVariableContext(VariableContext variableContext) {
116         context.getContextSupport().setVariableContext(variableContext);
117     }
118
119
120     public String JavaDoc toString() {
121         return "[XPathPatternImpl: text: " + text + " Pattern: " + pattern + "]";
122     }
123
124     protected ContextSupport getContextSupport() {
125         return new ContextSupport(
126                 new SimpleNamespaceContext(),
127                 XPathFunctionContext.getInstance(),
128                 new SimpleVariableContext(),
129                 DocumentNavigator.getInstance()
130         );
131     }
132
133     protected void handleJaxenException(JaxenException e) throws XPathException {
134         throw new XPathException(text, e);
135     }
136 }
137
138
139 /*
140  * Redistribution and use of this software and associated documentation
141  * ("Software"), with or without modification, are permitted provided
142  * that the following conditions are met:
143  *
144  * 1. Redistributions of source code must retain copyright
145  * statements and notices. Redistributions must also contain a
146  * copy of this document.
147  *
148  * 2. Redistributions in binary form must reproduce the
149  * above copyright notice, this list of conditions and the
150  * following disclaimer in the documentation and/or other
151  * materials provided with the distribution.
152  *
153  * 3. The name "DOM4J" must not be used to endorse or promote
154  * products derived from this Software without prior written
155  * permission of MetaStuff, Ltd. For written permission,
156  * please contact dom4j-info@metastuff.com.
157  *
158  * 4. Products derived from this Software may not be called "DOM4J"
159  * nor may "DOM4J" appear in their names without prior written
160  * permission of MetaStuff, Ltd. DOM4J is a registered
161  * trademark of MetaStuff, Ltd.
162  *
163  * 5. Due credit should be given to the DOM4J Project
164  * (http://dom4j.org/).
165  *
166  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
167  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
168  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
169  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
170  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
171  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
172  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
173  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
174  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
175  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
176  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
177  * OF THE POSSIBILITY OF SUCH DAMAGE.
178  *
179  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
180  *
181  * $Id: XPathPatternImpl.java,v 1.2 2003/11/02 18:10:04 per_nyfelt Exp $
182  */

183
Popular Tags