org.apache.jackrabbit.commons
Class NamespaceHelper

java.lang.Object
  extended by org.apache.jackrabbit.commons.NamespaceHelper

public class NamespaceHelper
extends Object

Helper class for working with JCR namespaces.

Since:
Jackrabbit JCR Commons 1.5

Field Summary
static String JCR
          The jcr namespace URI.
static String MIX
          The mix namespace URI.
static String NT
          The nt namespace URI.
 
Constructor Summary
NamespaceHelper(Session session)
          Creates a namespace helper for the given session.
 
Method Summary
 String getJcrName(String name)
          Replaces the standard jcr, nt, or mix prefix in the given name with the prefix mapped to that namespace in the current session.
 String getJcrName(String uri, String name)
          Returns the prefixed JCR name for the given namespace URI and local name in the current session.
 Map getNamespaces()
          Returns a map containing all prefix to namespace URI mappings of the current session.
 String getPrefix(String uri)
          Returns the prefix mapped to the given namespace URI in the current session, or null if the namespace does not exist.
 String getURI(String prefix)
          Returns the namespace URI mapped to the given prefix in the current session, or null if the namespace does not exist.
 String registerNamespace(String prefix, String uri)
          Safely registers the given namespace.
 void registerNamespaces(Map namespaces)
          Safely registers all namespaces in the given map from prefixes to namespace URIs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JCR

public static final String JCR
The jcr namespace URI.

See Also:
Constant Field Values

NT

public static final String NT
The nt namespace URI.

See Also:
Constant Field Values

MIX

public static final String MIX
The mix namespace URI.

See Also:
Constant Field Values
Constructor Detail

NamespaceHelper

public NamespaceHelper(Session session)
Creates a namespace helper for the given session.

Parameters:
session - current session
Method Detail

getNamespaces

public Map getNamespaces()
                  throws RepositoryException
Returns a map containing all prefix to namespace URI mappings of the current session. The returned map is newly allocated and can can be freely modified by the caller.

Returns:
namespace mappings
Throws:
RepositoryException - if the namespaces could not be retrieved
See Also:
Session.getNamespacePrefixes()

getPrefix

public String getPrefix(String uri)
                 throws RepositoryException
Returns the prefix mapped to the given namespace URI in the current session, or null if the namespace does not exist.

Parameters:
uri - namespace URI
Returns:
namespace prefix, or null
Throws:
RepositoryException - if the namespace could not be retrieved
See Also:
Session.getNamespacePrefix(String)

getURI

public String getURI(String prefix)
              throws RepositoryException
Returns the namespace URI mapped to the given prefix in the current session, or null if the namespace does not exist.

Parameters:
uri - namespace URI
Returns:
namespace prefix, or null
Throws:
RepositoryException - if the namespace could not be retrieved
See Also:
Session.getNamespaceURI(String)

getJcrName

public String getJcrName(String uri,
                         String name)
                  throws NamespaceException,
                         RepositoryException
Returns the prefixed JCR name for the given namespace URI and local name in the current session.

Parameters:
uri - namespace URI
name - local name
Returns:
prefixed JCR name
Throws:
NamespaceException - if the namespace does not exist
RepositoryException - if the namespace could not be retrieved

getJcrName

public String getJcrName(String name)
                  throws IllegalArgumentException,
                         RepositoryException
Replaces the standard jcr, nt, or mix prefix in the given name with the prefix mapped to that namespace in the current session.

The purpose of this method is to make it easier to write namespace-aware code that uses names in the standard JCR namespaces. For example:

     node.getProperty(helper.getName("jcr:data"));
 

Parameters:
name - prefixed name using the standard JCR prefixes
Returns:
prefixed name using the current session namespace mappings
Throws:
IllegalArgumentException - if the prefix is unknown
RepositoryException - if the namespace could not be retrieved

registerNamespace

public String registerNamespace(String prefix,
                                String uri)
                         throws RepositoryException
Safely registers the given namespace. If the namespace already exists, then the prefix mapped to the namespace in the current session is returned. Otherwise the namespace is registered to the namespace registry. If the given prefix is already registered for some other namespace or otherwise invalid, then another prefix is automatically generated. After the namespace has been registered, the prefix mapped to it in the current session is returned.

Parameters:
prefix - namespace prefix
uri - namespace URI
Returns:
namespace prefix in the current session
Throws:
RepositoryException - if the namespace could not be registered
See Also:
NamespaceRegistry.registerNamespace(String, String)

registerNamespaces

public void registerNamespaces(Map namespaces)
                        throws RepositoryException
Safely registers all namespaces in the given map from prefixes to namespace URIs.

Parameters:
namespaces - namespace mappings
Throws:
RepositoryException - if the namespaces could not be registered


Copyright © 2004-2010 The Apache Software Foundation. All Rights Reserved.