public class DefaultNamespaceContext extends Object implements NamespaceContext
NamespaceContext.
Example Usage:
import jlibs.core.lang.StringUtil;
import jlibs.xml.DefaultNamespaceContext;
import jlibs.xml.Namespaces;
DefaultNamespaceContext nsContext = new DefaultNamespaceContext();
nsContext.declarePrefix("xsd", Namespaces.URI_XSD)
nsContext.declarePrefix("jlibs", "http://code.google.com/p/jlibs");
nsContext.declarePrefix("tns", "http://code.google.com/p/jlibs");
System.out.println(nsContext.getPrefix("http://code.google.com/p/jlibs")); // prints "jlibs"
System.out.println(nsContext.getNamespaceURI("jlibs"); // prints "http://code.google.com/p/jlibs"
System.out.println(StringUtil.join(nsContext.getPrefixes("http://code.google.com/p/jlibs"))); // prints "jlibs, tns"
Prefix Suggestions:
You can speficy the suggested prefix for a uri by passing Properties to the constructor
If no-arg constructor is used, then it uses Namespaces.getSuggested() as suggestions.
Thus you can declare a uri without specified prefix manually. It finds the prefix automatically and returns the prefix it used.
System.out.println(nsContext.declarePrefix("http://www.google.com")); // returns "google"
This class also provides a handy method to compute javax.xml.namespace.QName:
javax.xml.namespace.QName qname = nsContext.toQName("tns:myElement");
System.out.println(qname); // prints "{http://code.google.com/p/jlibs}myElement"
| Constructor and Description |
|---|
DefaultNamespaceContext()
Creates new instance of DefaultNamespaceContext with suggested prefixes
Namespaces.getSuggested() |
DefaultNamespaceContext(Properties suggested)
Creates new instance of DefaultNamespaceContext with specified suggested prefixes
|
| Modifier and Type | Method and Description |
|---|---|
String |
declarePrefix(String uri)
Declared the specified
uri in this namespaceContext and returns
the prefix to which it is bound. |
void |
declarePrefix(String prefix,
String uri)
Binds specified
prefix to the given uri |
String |
getNamespaceURI(String prefix) |
String |
getPrefix(String namespaceURI) |
Iterator |
getPrefixes(String namespaceURI) |
QName |
toQName(String qname)
Constructs
QName for the specified qname. |
public DefaultNamespaceContext()
Namespaces.getSuggested()declarePrefix(String)public DefaultNamespaceContext(Properties suggested)
suggested - suggested prefixes, where key is URI and value is suggested prefixdeclarePrefix(String)public String getNamespaceURI(String prefix)
getNamespaceURI in interface NamespaceContextpublic String getPrefix(String namespaceURI)
getPrefix in interface NamespaceContextpublic Iterator getPrefixes(String namespaceURI)
getPrefixes in interface NamespaceContextpublic void declarePrefix(String prefix, String uri)
prefix to the given uriprefix - the prefix to be bounduri - the uri to which specified prefix to be boundpublic String declarePrefix(String uri)
uri in this namespaceContext and returns
the prefix to which it is bound.
the prefix is guessed from the suggested namespaces specified at construction
or derived from the specified uri
uri - uri to be declareduri is boundURLUtil.suggestPrefix(java.util.Properties, String)public QName toQName(String qname)
QName for the specified qname.qname - the qualified nameQName object constructed.IllegalArgumentException - if the prefix in qname is undeclared.Copyright © 2019. All rights reserved.