001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.graph.properties;
023
024 import java.util.Set;
025 import net.jcip.annotations.Immutable;
026 import net.jcip.annotations.ThreadSafe;
027
028 /**
029 * Registry of namespaces, which are used to provide isolated and independent domains for {@link Name names}.
030 *
031 * @author Randall Hauch
032 */
033 @ThreadSafe
034 public interface NamespaceRegistry {
035
036 /**
037 * Return the namespace URI that is currently mapped to the empty prefix, or null if there is no current default namespace.
038 *
039 * @return the namespace URI that represents the default namespace, or null if there is no default namespace
040 */
041 String getDefaultNamespaceUri();
042
043 /**
044 * Get the namespace URI for the supplied prefix.
045 *
046 * @param prefix the namespace prefix
047 * @return the namespace URI for the supplied prefix, or null if there is no namespace currently registered to use that prefix
048 * @throws IllegalArgumentException if the prefix is null
049 */
050 String getNamespaceForPrefix( String prefix );
051
052 /**
053 * Return the prefix used for the supplied namespace URI.
054 *
055 * @param namespaceUri the namespace URI
056 * @param generateIfMissing true if the namespace URI has not already been registered and the method should auto-register the
057 * namespace with a generated prefix, or false if the method should never auto-register the namespace
058 * @return the prefix currently being used for the namespace, or <code>null</code> if the namespace has not been registered
059 * and <code>generateIfMissing</code> is <code>false</code>
060 * @throws IllegalArgumentException if the namespace URI is null
061 * @see #isRegisteredNamespaceUri(String)
062 */
063 String getPrefixForNamespaceUri( String namespaceUri,
064 boolean generateIfMissing );
065
066 /**
067 * Return whether there is a registered prefix for the supplied namespace URI.
068 *
069 * @param namespaceUri the namespace URI
070 * @return true if the supplied namespace has been registered with a prefix, or false otherwise
071 * @throws IllegalArgumentException if the namespace URI is null
072 */
073 boolean isRegisteredNamespaceUri( String namespaceUri );
074
075 /**
076 * Register a new namespace using the supplied prefix, returning the namespace URI previously registered under that prefix.
077 *
078 * @param prefix the prefix for the namespace, or null if a namesapce prefix should be generated automatically
079 * @param namespaceUri the namespace URI
080 * @return the namespace URI that was previously registered with the supplied prefix, or null if the prefix was not previously
081 * bound to a namespace URI
082 * @throws IllegalArgumentException if the namespace URI is null
083 */
084 String register( String prefix,
085 String namespaceUri );
086
087 /**
088 * Unregister the namespace with the supplied URI.
089 *
090 * @param namespaceUri the namespace URI
091 * @return true if the namespace was removed, or false if the namespace was not registered
092 * @throws IllegalArgumentException if the namespace URI is null
093 * @throws NamespaceException if there is a problem unregistering the namespace
094 */
095 boolean unregister( String namespaceUri );
096
097 /**
098 * Obtain the set of namespaces that are registered.
099 *
100 * @return the set of
101 */
102 Set<String> getRegisteredNamespaceUris();
103
104 /**
105 * Obtain a snapshot of all of the {@link Namespace namespaces} registered at the time this method is called. The resulting
106 * set is immutable, and will <i>not</i> reflect changes made to the registry.
107 *
108 * @return an immutable set of {@link Namespace} objects reflecting a snapshot of the registry; never null
109 */
110 Set<Namespace> getNamespaces();
111
112 /**
113 * Representation of a single namespace at a single point in time. This object does not change to reflect changes made to the
114 * {@link NamespaceRegistry registry}.
115 *
116 * @see NamespaceRegistry#getNamespaces()
117 * @author Randall Hauch
118 */
119 @Immutable
120 interface Namespace {
121 /**
122 * Get the prefix for the namespace
123 *
124 * @return the prefix; never null but possibly the empty string
125 */
126 String getPrefix();
127
128 /**
129 * Get the URI for the namespace
130 *
131 * @return the namespace URI; never null but possibly the empty string
132 */
133 String getNamespaceUri();
134 }
135
136 }