-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: support prefixed names in patterns #115
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* $Header$ | ||
* $Revision$ | ||
* $Date$ | ||
* | ||
* ==================================================================== | ||
* | ||
* Copyright 2000-2002 bob mcwhirter & James Strachan. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2021 The Jaxen Project |
||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the Jaxen Project nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* ==================================================================== | ||
* This software consists of voluntary contributions made by many | ||
* individuals on behalf of the Jaxen Project and was originally | ||
* created by bob mcwhirter <[email protected]> and | ||
* James Strachan <[email protected]>. For more information on the | ||
* Jaxen Project, please see <http://www.jaxen.org/>. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
* $Id$ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
*/ | ||
|
||
package org.jaxen.pattern; | ||
|
||
import org.jaxen.Context; | ||
import org.jaxen.JaxenException; | ||
|
||
/** <p><code>NameNamespaceCompositeTest</code> tests for a node name and a given namespace URI.</p> | ||
* | ||
* @author Claude Mamo | ||
*/ | ||
public class NameNamespaceCompositeTest extends NodeTest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. QualifiedNameTest or perhaps PrefixedNameTest |
||
{ | ||
private final NameTest nameTest; | ||
private final NamespaceTest namespaceTest; | ||
|
||
public NameNamespaceCompositeTest(NameTest nameTest, NamespaceTest namespaceTest) | ||
{ | ||
this.nameTest = nameTest; | ||
this.namespaceTest = namespaceTest; | ||
} | ||
|
||
public boolean matches(Object node, Context context) throws JaxenException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
return nameTest.matches(node, context) && namespaceTest.matches(node, context); | ||
} | ||
|
||
public String getText() | ||
{ | ||
return namespaceTest.getText() + nameTest.getText(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should there be a separator here? |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,8 +248,17 @@ else if ( step instanceof DefaultNameStep ) | |
} | ||
else | ||
{ | ||
path.setNodeTest( new NameTest( localName, nodeType ) ); | ||
// XXXX: should support namespace in the test too | ||
NameTest nameTest = new NameTest( localName, nodeType ); | ||
NodeTest nodeTest; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be a little clearer if you pushed this into the if-block and called setNodeTest from within each branch. |
||
if (prefix.length() > 0) | ||
{ | ||
nodeTest = new NameNamespaceCompositeTest(nameTest, new NamespaceTest( prefix, nodeType )); | ||
} | ||
else | ||
{ | ||
nodeTest = nameTest; | ||
} | ||
path.setNodeTest( nodeTest ); | ||
} | ||
return convertDefaultStep(path, nameStep); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* $Header$ | ||
* $Revision$ | ||
* $Date$ | ||
* | ||
* ==================================================================== | ||
* | ||
* Copyright 2000-2002 bob mcwhirter & James Strachan. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the Jaxen Project nor the names of its | ||
* contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* ==================================================================== | ||
* This software consists of voluntary contributions made by many | ||
* individuals on behalf of the Jaxen Project and was originally | ||
* created by bob mcwhirter <[email protected]> and | ||
* James Strachan <[email protected]>. For more information on the | ||
* Jaxen Project, please see <http://www.jaxen.org/>. | ||
* | ||
* $Id$ | ||
*/ | ||
|
||
|
||
|
||
package org.jaxen.test; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import org.jaxen.Context; | ||
import org.jaxen.ContextSupport; | ||
import org.jaxen.SimpleNamespaceContext; | ||
import org.jaxen.SimpleVariableContext; | ||
import org.jaxen.XPathFunctionContext; | ||
import org.jaxen.dom.DocumentNavigator; | ||
import org.jaxen.pattern.Pattern; | ||
import org.jaxen.pattern.PatternParser; | ||
import org.jaxen.saxpath.SAXPathException; | ||
import org.jaxen.saxpath.XPathSyntaxException; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
public class PatternParserTest extends TestCase | ||
{ | ||
|
||
String[] paths = { | ||
"foo", | ||
"*", | ||
"/", | ||
"foo/bar", | ||
"foo//bar", | ||
"/*/foo", | ||
"*[@name]", | ||
"foo/bar[1]", | ||
"foo[bar=\"contents\"]", | ||
"foo[bar='contents']", | ||
"foo|bar", | ||
"foo/title | bar/title | xyz/title", | ||
"/foo//*", | ||
"foo/text()", | ||
"foo/@*", | ||
}; | ||
|
||
String[] bogusPaths = { }; | ||
|
||
String[] ignore_bogusPaths = { | ||
// this path is bogus because of a trailing / | ||
"/foo/bar/", | ||
|
||
// This path is bogus because '/' is not division, but | ||
// rather just the step separator. | ||
"12 + sum(count(//author),count(//author/attribute::*)) / 2", | ||
"id()/2", | ||
"+" | ||
}; | ||
|
||
public PatternParserTest(String name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed |
||
{ | ||
super( name ); | ||
} | ||
|
||
public void testValidPaths() throws SAXPathException | ||
{ | ||
for ( int i = 0; i < paths.length; i++ ) { | ||
String path = paths[i]; | ||
PatternParser.parse( path ); | ||
} | ||
} | ||
|
||
public void testMatchesIsTrueGivenNameWithPrefixPath() throws SAXPathException, ParserConfigurationException | ||
{ | ||
Pattern pattern = PatternParser.parse("bar:foo"); | ||
SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); | ||
simpleNamespaceContext.addNamespace("bar", "http://www.bar.org/"); | ||
ContextSupport contextSupport = new ContextSupport(simpleNamespaceContext, | ||
XPathFunctionContext.getInstance(), | ||
new SimpleVariableContext(), | ||
new DocumentNavigator()); | ||
assertTrue(pattern.matches(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. break this statement up with some local variables for clarity |
||
createElementNS("http://www.bar.org/", "foo"), new Context(contextSupport))); | ||
} | ||
|
||
public void testMatchesIsFalseGivenNameWithPrefixPath() throws SAXPathException, ParserConfigurationException | ||
{ | ||
Pattern pattern = PatternParser.parse("bar:foo"); | ||
SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); | ||
simpleNamespaceContext.addNamespace("bar", "http://www.bar.org/"); | ||
ContextSupport contextSupport = new ContextSupport(simpleNamespaceContext, | ||
XPathFunctionContext.getInstance(), | ||
new SimpleVariableContext(), | ||
new DocumentNavigator()); | ||
assertFalse(pattern.matches(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(). | ||
createElementNS("http://www.example.org/", "foo"), new Context(contextSupport))); | ||
} | ||
|
||
public void testBogusPaths() throws SAXPathException | ||
{ | ||
for ( int i = 0; i < bogusPaths.length; i++ ) { | ||
String path = bogusPaths[i]; | ||
try | ||
{ | ||
Pattern pattern = PatternParser.parse( path ); | ||
fail( "Parsed bogus path as: " + pattern ); | ||
} | ||
catch (XPathSyntaxException e) | ||
{ | ||
} | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed