001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: XPathPattern.java,v 1.11 2001/08/31 08:58:53 jstrachan Exp $ 008 */ 009 010package org.dom4j.xpath; 011 012import org.dom4j.Document; 013import org.dom4j.Element; 014import org.dom4j.Node; 015import org.dom4j.InvalidXPathException; 016import org.dom4j.XPathException; 017 018import org.jaxen.Context; 019import org.jaxen.ContextSupport; 020import org.jaxen.JaxenException; 021import org.jaxen.SimpleNamespaceContext; 022import org.jaxen.SimpleVariableContext; 023import org.jaxen.VariableContext; 024import org.jaxen.XPathFunctionContext; 025import org.jaxen.dom4j.DocumentNavigator; 026import org.jaxen.pattern.Pattern; 027import org.jaxen.pattern.PatternParser; 028 029import org.saxpath.SAXPathException; 030 031import java.io.StringReader; 032 033import java.util.ArrayList; 034import java.util.Comparator; 035import java.util.HashMap; 036import java.util.HashSet; 037import java.util.Iterator; 038import java.util.List; 039import java.util.Map; 040 041/** <p><code>XPathPattern</code> is an implementation of Pattern 042 * which uses an XPath xpath.</p> 043 * 044 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 045 * @version $Revision: 1.11 $ 046 */ 047public class XPathPattern implements org.dom4j.rule.Pattern { 048 049 private String text; 050 private Pattern pattern; 051 private Context context; 052 053 054 public XPathPattern(Pattern pattern) { 055 this.pattern = pattern; 056 this.text = pattern.getText(); 057 this.context = new Context( getContextSupport() ); 058 } 059 060 public XPathPattern(String text) { 061 this.text = text; 062 this.context = new Context( getContextSupport() ); 063 try { 064 this.pattern = PatternParser.parse( text ); 065 } 066 catch (SAXPathException e) { 067 throw new InvalidXPathException( text, e.getMessage() ); 068 } 069 catch (RuntimeException e) { 070 throw new InvalidXPathException( text ); 071 } 072 } 073 074 public boolean matches( Node node ) { 075 try { 076 ArrayList list = new ArrayList(1); 077 list.add( node ); 078 context.setNodeSet( list ); 079 return pattern.matches( node, context ); 080 } 081 catch (JaxenException e) { 082 handleJaxenException(e); 083 return false; 084 } 085 } 086 087 public String getText() { 088 return text; 089 } 090 091 092 public double getPriority() { 093 return pattern.getPriority(); 094 } 095 096 public org.dom4j.rule.Pattern[] getUnionPatterns() { 097 Pattern[] patterns = pattern.getUnionPatterns(); 098 if ( patterns != null ) { 099 int size = patterns.length; 100 XPathPattern[] answer = new XPathPattern[size]; 101 for ( int i = 0; i < size; i++ ) { 102 answer[i] = new XPathPattern( patterns[i] ); 103 } 104 return answer; 105 } 106 return null; 107 } 108 109 public short getMatchType() { 110 return pattern.getMatchType(); 111 } 112 113 public String getMatchesNodeName() { 114 return pattern.getMatchesNodeName(); 115 } 116 117 public void setVariableContext(VariableContext variableContext) { 118 context.getContextSupport().setVariableContext( variableContext ); 119 } 120 121 122 public String toString() { 123 return "[XPathPattern: text: " + text + " Pattern: " + pattern + "]"; 124 } 125 126 protected ContextSupport getContextSupport() { 127 return new ContextSupport( 128 new SimpleNamespaceContext(), 129 XPathFunctionContext.getInstance(), 130 new SimpleVariableContext(), 131 DocumentNavigator.getInstance() 132 ); 133 } 134 135 protected void handleJaxenException(JaxenException e) throws XPathException { 136 throw new XPathException(text, e); 137 } 138} 139 140 141 142 143/* 144 * Redistribution and use of this software and associated documentation 145 * ("Software"), with or without modification, are permitted provided 146 * that the following conditions are met: 147 * 148 * 1. Redistributions of source code must retain copyright 149 * statements and notices. Redistributions must also contain a 150 * copy of this document. 151 * 152 * 2. Redistributions in binary form must reproduce the 153 * above copyright notice, this list of conditions and the 154 * following disclaimer in the documentation and/or other 155 * materials provided with the distribution. 156 * 157 * 3. The name "DOM4J" must not be used to endorse or promote 158 * products derived from this Software without prior written 159 * permission of MetaStuff, Ltd. For written permission, 160 * please contact dom4j-info@metastuff.com. 161 * 162 * 4. Products derived from this Software may not be called "DOM4J" 163 * nor may "DOM4J" appear in their names without prior written 164 * permission of MetaStuff, Ltd. DOM4J is a registered 165 * trademark of MetaStuff, Ltd. 166 * 167 * 5. Due credit should be given to the DOM4J Project 168 * (http://dom4j.org/). 169 * 170 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 171 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 172 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 173 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 174 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 175 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 176 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 177 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 178 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 179 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 180 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 181 * OF THE POSSIBILITY OF SUCH DAMAGE. 182 * 183 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 184 * 185 * $Id: XPathPattern.java,v 1.11 2001/08/31 08:58:53 jstrachan Exp $ 186 */