commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml XPathExpression.java
Date Wed, 13 Nov 2002 09:03:32 GMT
jstrachan    2002/11/13 01:03:31

  Modified:    jelly/src/test/org/apache/commons/jelly/xml suite.jelly
               jelly/src/test/org/apache/commons/jelly/junit suite.jelly
               jelly/src/java/org/apache/commons/jelly/tags/xml
                        XPathExpression.java
  Log:
  Added a JellyUnit test case to demonstrate the bug that J. Matthew Pryor recently found
in the XPath feature in Jelly.
  
  Plus I've now fixed the bug. When using the namespace context for the XPath expression,
the default no-prefix namespace should not be used, as this breaks XPath and hides the default
XPath function libraries.
  Inside XPath, the default namespace (i.e. when no prefix is used) should always be "".
  
  Revision  Changes    Path
  1.2       +56 -0     jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/xml/suite.jelly,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- suite.jelly	12 Nov 2002 08:39:50 -0000	1.1
  +++ suite.jelly	13 Nov 2002 09:03:31 -0000	1.2
  @@ -18,4 +18,60 @@
   		<test:assert xpath="count($doc/bar) = 0"/>
   		
     </test:case>
  +  
  +  <test:case name="assertXPathTests">
  +  
  +    <x:parse var="doc">
  +      <foo>
  +      	<bar>cheese</bar>
  +      </foo>
  +    </x:parse>  
  +     
  +  
  +    <test:assert xpath="$doc/foo/bar">This should never fail</test:assert>
  +    <test:assert xpath="$doc/foo/bar = 'cheese'">This should never fail</test:assert>
  +    
  +    <j:catch var="ex">
  +		  <test:assert xpath="$doc/foo/xyz">This should always fail</test:assert>
  +		</j:catch>    	
  +		
  +		<test:assert test="${ex != null}">We should have created an exception</test:assert>
  +		
  +		The exception was: ${ex.message}
  +  </test:case>
  +
  +	<!-- test the use of namespaces with XPath -->
  +  <test:case name="assertXPathWithNamespaceTests" xmlns:a="fooURI" xmlns:z="doesNotMatch"
xmlns="different">
  +  
  +    <x:parse var="doc">
  +      <foo xmlns="fooURI">
  +      	<bar>cheese</bar>
  +      </foo>
  +    </x:parse>  
  +     
  +    <test:assert xpath="$doc/a:foo/a:bar">This should never fail</test:assert>
  +    <test:assert xpath="$doc/b:foo/b:bar" xmlns:b="fooURI">This should never fail</test:assert>
  +    
  +    <j:catch var="ex">
  +		  <test:assert xpath="$doc/z:foo/z:bar">This should always fail</test:assert>
  +		</j:catch>    	
  +		
  +		<test:assert test="${ex != null}">We should have created an exception</test:assert>
  +		
  +    <j:catch var="ex">
  +		  <test:assert xpath="$doc/foo/bar">This should always fail, since foo is not in
the empty namespace</test:assert>
  +		</j:catch>    	
  +		
  +		<test:assert test="${ex != null}">We should have created an exception</test:assert>
  +		
  +		<!-- 
  +			|| now lets test that the default namespace 'different' is ignored by the XPath expressions
  +			|| since XPath should ignore the default namespace - you must use a prefix in XPath
to denote
  +			|| a namespace
  +			-->
  +		<test:assert xpath="$doc/*[local-name()='foo']"/>
  +  </test:case>
  +
  +
  +  
   </test:suite>
  
  
  
  1.6       +0 -47     jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/junit/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/junit/suite.jelly,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- suite.jelly	8 Nov 2002 18:35:06 -0000	1.5
  +++ suite.jelly	13 Nov 2002 09:03:31 -0000	1.6
  @@ -15,53 +15,6 @@
     </test:case>
   
   
  -  <test:case name="assertXPathTests">
  -  
  -    <x:parse var="doc">
  -      <foo>
  -      	<bar>cheese</bar>
  -      </foo>
  -    </x:parse>  
  -     
  -  
  -    <test:assert xpath="$doc/foo/bar">This should never fail</test:assert>
  -    <test:assert xpath="$doc/foo/bar = 'cheese'">This should never fail</test:assert>
  -    
  -    <j:catch var="ex">
  -		  <test:assert xpath="$doc/foo/xyz">This should always fail</test:assert>
  -		</j:catch>    	
  -		
  -		<test:assert test="${ex != null}">We should have created an exception</test:assert>
  -		
  -		The exception was: ${ex.message}
  -  </test:case>
  -
  -	<!-- test the use of namespaces with XPath -->
  -  <test:case name="assertXPathWithNamespaceTests" xmlns:a="fooURI" xmlns:z="doesNotMatch">
  -  
  -    <x:parse var="doc">
  -      <foo xmlns="fooURI">
  -      	<bar>cheese</bar>
  -      </foo>
  -    </x:parse>  
  -     
  -    <test:assert xpath="$doc/a:foo/a:bar">This should never fail</test:assert>
  -    <test:assert xpath="$doc/b:foo/b:bar" xmlns:b="fooURI">This should never fail</test:assert>
  -    
  -    <j:catch var="ex">
  -		  <test:assert xpath="$doc/z:foo/z:bar">This should always fail</test:assert>
  -		</j:catch>    	
  -		
  -		<test:assert test="${ex != null}">We should have created an exception</test:assert>
  -		
  -    <j:catch var="ex">
  -		  <test:assert xpath="$doc/foo/bar">This should always fail, since foo is not in
the empty namespace</test:assert>
  -		</j:catch>    	
  -		
  -		<test:assert test="${ex != null}">We should have created an exception</test:assert>
  -  </test:case>
  -
  -
     <test:case name="failTests">
     
       <j:catch var="ex">
  
  
  
  1.9       +29 -6     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/XPathExpression.java
  
  Index: XPathExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/XPathExpression.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- XPathExpression.java	8 Nov 2002 18:27:51 -0000	1.8
  +++ XPathExpression.java	13 Nov 2002 09:03:31 -0000	1.9
  @@ -63,6 +63,8 @@
   
   import java.io.IOException;
   import java.io.Writer;
  +import java.util.Hashtable;
  +import java.util.Iterator;
   import java.util.Map;
   
   import org.apache.commons.jelly.JellyContext;
  @@ -98,7 +100,9 @@
       public XPathExpression(XPath xpath, TagScript tagScript) {
           this.xpath = xpath;
           
  -        Map uris = tagScript.getNamespaceContext();
  +        Map namespaceContext = tagScript.getNamespaceContext();
  +        
  +        Map uris = createUriMap(namespaceContext);
           
           if (log.isDebugEnabled()) {
           	log.debug( "Setting the namespace context to be: " + uris );
  @@ -131,5 +135,24 @@
           //log.debug( "Looking up XPath variable of name: " + localName + " value is: "
+ value );            
           
           return value;
  +    }
  +    
  +    // Implementation methods
  +    //-------------------------------------------------------------------------
  +    
  +    /**
  +     * Factory method to create a synchronized Map of non-null and non-blank
  +     * namespace prefixes to namespace URIs
     */ 
  +    protected Map createUriMap(Map namespaceContext) {
  +        // now lets clone the Map but ignoring default or null prefixes
  +        Map uris = new Hashtable(namespaceContext.size());
  +        for (Iterator iter = namespaceContext.entrySet().iterator(); iter.hasNext(); )
{
  +        	Map.Entry entry = (Map.Entry) iter.next();
  +        	String prefix = (String) entry.getKey();
  +        	if (prefix != null && prefix.length() != 0) {
  +        		uris.put(prefix, entry.getValue());
  +        	}
  +        }
  +        return uris;
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message