-
-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Take 2. Fix failures of xpath with custom object. By this change, nam…
…espace for xpath function has been added to NokogiriNamespaceContext.
- Loading branch information
Showing
2 changed files
with
58 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 39 additions & 17 deletions
56
ext/java/nokogiri/internals/NokogiriNamespaceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
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?