Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 174   Methods: 12
NCLOC: 92   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XPathPattern.java 25% 53,1% 66,7% 54,2%
coverage coverage
 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  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    * 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    */