|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjlibs.xml.DefaultNamespaceContext
public class DefaultNamespaceContext
This is an implementation of 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 Summary | |
|---|---|
DefaultNamespaceContext()
Creates new instance of DefaultNamespaceContext with suggested prefixes Namespaces.getSuggested() |
|
DefaultNamespaceContext(Properties suggested)
Creates new instance of DefaultNamespaceContext with specified suggested prefixes |
|
| Method Summary | |
|---|---|
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. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public DefaultNamespaceContext()
Namespaces.getSuggested()
declarePrefix(String)public DefaultNamespaceContext(Properties suggested)
suggested - suggested prefixes, where key is URI and value is suggested prefixdeclarePrefix(String)| Method Detail |
|---|
public String getNamespaceURI(String prefix)
getNamespaceURI in interface NamespaceContextpublic String getPrefix(String namespaceURI)
getPrefix in interface NamespaceContextpublic Iterator getPrefixes(String namespaceURI)
getPrefixes in interface NamespaceContext
public void declarePrefix(String prefix,
String uri)
prefix to the given uri
prefix - 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 declared
uri is boundURLUtil.suggestPrefix(java.util.Properties, String)public QName toQName(String qname)
QName for the specified qname.
qname - the qualified name
QName object constructed.
IllegalArgumentException - if the prefix in qname is undeclared.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||