Hi all,
Currently I cannot see in Xerces-J codebase, a Xerces
implementation of the JAXP XPath interface,
javax.xml.namespace.NamespaceContext.
It seems user's have to write their own implementation of this
interface, when they need this functionality.
I propose an implementation of this interface in Xerces-J, along the
following lines:
import javax.xml.namespace.NamespaceContext;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
public class NamespaceContextImpl implements NamespaceContext {
private Map namespaceStore;
public NamespaceContextImpl() {
namespaceStore = new HashMap();
}
public void setNamespace(String prefix, String namespaceURI) {
namespaceStore.put(prefix, namespaceURI);
}
public String getNamespaceURI(String prefix) {
return (String) namespaceStore.get(prefix);
}
public String getPrefix(java.lang.String namespaceURI) {
Set keys = namespaceStore.keySet();
for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
String prefix = (String) iterator.next();
String uri = (String) namespaceStore.get(prefix);
if (uri.equals(namespaceURI)) {
return prefix;
}
}
return null;
}
public Iterator getPrefixes(String namespaceURI) {
List prefixes = new ArrayList();
Set keys = namespaceStore.keySet();
for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
String prefix = (String) iterator.next();
String uri = (String) namespaceStore.get(prefix);
if (uri.equals(namespaceURI)) {
prefixes.add(prefix);
}
}
return prefixes.iterator();
}
}
An implementation like above is needed, when we have to do
namespace-aware XPath evaluations with JAXP XPath API. for e.g,
something like following needs to be done by the user:
XPathFactory xFactory = XPathFactory.newInstance();
XPath xPath = xFactory.newXPath();
NamespaceContextImpl namespaceContext = new NamespaceContextImpl();
namespaceContext.setNamespace(prefix,uri);
xPath.setNamespaceContext(namespaceContext);
XPathExpression xpression = xPath.compile("xpath-string using namespaces");
The class, NamespaceContextImpl might be stored in Xerces-J package,
org.apache.xerces.jaxp
Any thoughts about this, please?
I have an implementation for this ready with me (which is just only
the above Java code). This could be committed to Xerces-J code-base,
if we agree on this.
--
Regards,
Mukul Gandhi
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]