Skip to content

Commit

Permalink
Take 2. Fix failures of xpath with custom object. By this change, nam…
Browse files Browse the repository at this point in the history
…espace for xpath function has been added to NokogiriNamespaceContext.
  • Loading branch information
yokolet committed Apr 1, 2010
1 parent 96df126 commit 468c757
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
19 changes: 19 additions & 0 deletions ext/java/nokogiri/XmlXpathContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nokogiri;

import java.util.Set;

import nokogiri.internals.NokogiriNamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
Expand Down Expand Up @@ -36,6 +38,12 @@ public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject
String src = expr.convertToString().asJavaString();
try {
if(!handler.isNil()) {
if (!isContainsPrefix(src)) {
Set<String> methodNames = handler.getMetaClass().getMethods().keySet();
for (String name : methodNames) {
src = src.replaceAll(name, NokogiriNamespaceContext.NOKOGIRI_PREFIX+":"+name);

This comment has been minimized.

Copy link
@munkyboy

munkyboy Feb 25, 2013

I'm running into an intermittent failure with nokogiri >= 1.5.6 on JRuby. I get a very large java stack trace which leads me to this line of code. I'm unclear on exactly what parameters are given to this function, but it seems that the name variable here can be a ruby method name like []=. Java's String#replaceAll will treat the first parameter as a regular expression. Is that intended here?

}
}
xpath.setXPathFunctionResolver(new NokogiriXPathFunctionResolver(handler));
}
XPathExpression xpathExpression = xpath.compile(src);
Expand All @@ -44,6 +52,17 @@ public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject
throw new RaiseException(XmlSyntaxError.getXPathSyntaxError(context, xpee));
}
}

private boolean isContainsPrefix(String str) {
Set<String> prefixes = ((NokogiriNamespaceContext)xpath.getNamespaceContext()).getAllPrefixes();
for (String prefix : prefixes) {
if (str.contains(prefix + ":")) {
return true;
}
}
return false;
}


@JRubyMethod
public IRubyObject evaluate(ThreadContext context, IRubyObject expr) {
Expand Down
56 changes: 39 additions & 17 deletions ext/java/nokogiri/internals/NokogiriNamespaceContext.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
package nokogiri.internals;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

public class NokogiriNamespaceContext implements NamespaceContext{

Hashtable<String,String> register;
public class NokogiriNamespaceContext implements NamespaceContext {
public static final String NOKOGIRI_PREFIX = "nokogiri";
public static final String NOKOGIRI_URI = "http://www.nokogiri.org/default_ns/ruby/extensions_functions";

private Hashtable<String,String> register;

public NokogiriNamespaceContext(){
public NokogiriNamespaceContext() {
this.register = new Hashtable<String,String>();
register.put(NOKOGIRI_PREFIX, NOKOGIRI_URI);
}

public String getNamespaceURI(String prefix) {
// System.out.println("Asked for " + prefix);
if (prefix == null) {
throw new IllegalArgumentException();
}
String uri = this.register.get(prefix);
if(uri != null) {
// System.out.println("Returned "+uri);
if (uri != null) {
return uri;
}

// System.out.println("Returned another url");

if(prefix == null) {
throw new IllegalArgumentException();
} else if(prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
uri = this.register.get(XMLConstants.XMLNS_ATTRIBUTE);
return (uri == null) ? XMLConstants.XMLNS_ATTRIBUTE_NS_URI : uri;
}

return XMLConstants.NULL_NS_URI;
}

public String getPrefix(String uri){
public String getPrefix(String uri) {
if (uri == null) {
throw new IllegalArgumentException("uri is null");
} else if (uri.equals(NOKOGIRI_URI)) {
return NOKOGIRI_PREFIX;
}
return null;
}

public Iterator getPrefixes(String uri){
return null;
public Iterator<String> getPrefixes(String uri) {
if (register == null) return null;
Set<Entry<String, String>> entries = register.entrySet();
List<String> list = new ArrayList<String>();
for (Entry<String, String> entry : entries) {
if (uri.equals(entry.getValue())) {
list.add(entry.getKey());
}
}
return list.iterator();
}

public Set<String> getAllPrefixes() {
if (register == null) return null;
return register.keySet();
}

public void registerNamespace(String prefix, String uri){
public void registerNamespace(String prefix, String uri) {
if("xmlns".equals(prefix)) prefix = "";
// System.out.println("Registered prefix "+prefix+" with uri " + uri);
this.register.put(prefix, uri);
}
}

0 comments on commit 468c757

Please sign in to comment.