jlibs.xml
Class DefaultNamespaceContext

java.lang.Object
  extended by jlibs.xml.DefaultNamespaceContext
All Implemented Interfaces:
NamespaceContext

public class DefaultNamespaceContext
extends Object
implements NamespaceContext

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"
 

Author:
Santhosh Kumar T

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

DefaultNamespaceContext

public DefaultNamespaceContext()
Creates new instance of DefaultNamespaceContext with suggested prefixes Namespaces.getSuggested()

See Also:
declarePrefix(String)

DefaultNamespaceContext

public DefaultNamespaceContext(Properties suggested)
Creates new instance of DefaultNamespaceContext with specified suggested prefixes

Parameters:
suggested - suggested prefixes, where key is URI and value is suggested prefix
See Also:
declarePrefix(String)
Method Detail

getNamespaceURI

public String getNamespaceURI(String prefix)
Specified by:
getNamespaceURI in interface NamespaceContext

getPrefix

public String getPrefix(String namespaceURI)
Specified by:
getPrefix in interface NamespaceContext

getPrefixes

public Iterator getPrefixes(String namespaceURI)
Specified by:
getPrefixes in interface NamespaceContext

declarePrefix

public void declarePrefix(String prefix,
                          String uri)
Binds specified prefix to the given uri

Parameters:
prefix - the prefix to be bound
uri - the uri to which specified prefix to be bound

declarePrefix

public String declarePrefix(String uri)
Declared the specified 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

Parameters:
uri - uri to be declared
Returns:
the prefix to which uri is bound
See Also:
URLUtil.suggestPrefix(java.util.Properties, String)

toQName

public QName toQName(String qname)
Constructs QName for the specified qname.

Parameters:
qname - the qualified name
Returns:
QName object constructed.
Throws:
IllegalArgumentException - if the prefix in qname is undeclared.


Copyright © 2015. All rights reserved.