View Javadoc

1   /*
2    * Copyright 2001-2004 (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: XPathPattern.java,v 1.16 2004/06/25 08:03:42 maartenc Exp $
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  * Redistribution and use of this software and associated documentation
133  * ("Software"), with or without modification, are permitted provided
134  * that the following conditions are met:
135  *
136  * 1. Redistributions of source code must retain copyright
137  *    statements and notices.  Redistributions must also contain a
138  *    copy of this document.
139  *
140  * 2. Redistributions in binary form must reproduce the
141  *    above copyright notice, this list of conditions and the
142  *    following disclaimer in the documentation and/or other
143  *    materials provided with the distribution.
144  *
145  * 3. The name "DOM4J" must not be used to endorse or promote
146  *    products derived from this Software without prior written
147  *    permission of MetaStuff, Ltd.  For written permission,
148  *    please contact dom4j-info@metastuff.com.
149  *
150  * 4. Products derived from this Software may not be called "DOM4J"
151  *    nor may "DOM4J" appear in their names without prior written
152  *    permission of MetaStuff, Ltd. DOM4J is a registered
153  *    trademark of MetaStuff, Ltd.
154  *
155  * 5. Due credit should be given to the DOM4J Project - 
156  *    http://www.dom4j.org
157  *
158  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
159  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
160  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
161  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
162  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
163  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
164  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
165  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
166  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
167  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
168  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
169  * OF THE POSSIBILITY OF SUCH DAMAGE.
170  *
171  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
172  *
173  * $Id: XPathPattern.java,v 1.16 2004/06/25 08:03:42 maartenc Exp $
174  */