001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024 package org.jboss.dna.graph.property.basic;
025
026 import java.text.DecimalFormat;
027 import java.util.ArrayList;
028 import java.util.Collections;
029 import java.util.HashMap;
030 import java.util.HashSet;
031 import java.util.List;
032 import java.util.Map;
033 import java.util.Set;
034 import net.jcip.annotations.NotThreadSafe;
035 import org.jboss.dna.common.util.CheckArg;
036 import org.jboss.dna.graph.property.NamespaceRegistry;
037
038 /**
039 * A simple {@link NamespaceRegistry} implementation that is not thread-safe, but that provides all the basic functionality.
040 *
041 * @author Randall Hauch
042 */
043 @NotThreadSafe
044 public class SimpleNamespaceRegistry implements NamespaceRegistry {
045
046 public static final String DEFAULT_NAMESPACE_URI = "";
047 public static final String DEFAULT_PREFIX_TEMPLATE = "ns##000";
048 public static final String DEFAULT_PREFIX_NUMBER_FORMAT = "##000";
049
050 private final Map<String, String> namespacesByPrefix = new HashMap<String, String>();
051 private final Map<String, String> prefixesByNamespace = new HashMap<String, String>();
052 private String generatedPrefixTemplate = DEFAULT_PREFIX_TEMPLATE;
053 private int nextGeneratedPrefixNumber = 1;
054
055 /**
056 *
057 */
058 public SimpleNamespaceRegistry() {
059 this(DEFAULT_NAMESPACE_URI);
060 }
061
062 /**
063 * @param defaultNamespaceUri the namespace URI to use for the default prefix
064 */
065 public SimpleNamespaceRegistry( final String defaultNamespaceUri ) {
066 register("", defaultNamespaceUri);
067 }
068
069 /**
070 * @return prefixTemplate
071 */
072 public String getGeneratedPrefixTemplate() {
073 return this.generatedPrefixTemplate;
074 }
075
076 /**
077 * @param prefixTemplate Sets prefixTemplate to the specified value.
078 */
079 public void setGeneratedPrefixTemplate( String prefixTemplate ) {
080 if (prefixTemplate == null) prefixTemplate = DEFAULT_PREFIX_TEMPLATE;
081 this.generatedPrefixTemplate = prefixTemplate;
082 }
083
084 /**
085 * {@inheritDoc}
086 */
087 public String getNamespaceForPrefix( String prefix ) {
088 CheckArg.isNotNull(prefix, "prefix");
089 return this.namespacesByPrefix.get(prefix);
090 }
091
092 /**
093 * {@inheritDoc}
094 */
095 public String getPrefixForNamespaceUri( String namespaceUri,
096 boolean generateIfMissing ) {
097 CheckArg.isNotNull(namespaceUri, "namespaceUri");
098 String prefix = null;
099 prefix = this.prefixesByNamespace.get(namespaceUri);
100 if (prefix == null && generateIfMissing) {
101 // Now we can genereate a prefix and register it ...
102 prefix = this.generatePrefix();
103 this.register(prefix, namespaceUri);
104 return prefix;
105 }
106 return prefix;
107 }
108
109 /**
110 * {@inheritDoc}
111 */
112 public boolean isRegisteredNamespaceUri( String namespaceUri ) {
113 CheckArg.isNotNull(namespaceUri, "namespaceUri");
114 return this.prefixesByNamespace.containsKey(namespaceUri);
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 public String getDefaultNamespaceUri() {
121 return this.namespacesByPrefix.get("");
122 }
123
124 /**
125 * {@inheritDoc}
126 */
127 public String register( String prefix,
128 String namespaceUri ) {
129 CheckArg.isNotNull(namespaceUri, "namespaceUri");
130 String previousNamespaceForPrefix = null;
131 namespaceUri = namespaceUri.trim();
132 if (prefix == null) prefix = generatePrefix();
133 prefix = prefix.trim();
134 prefix = prefix.replaceFirst("^:+", "");
135 prefix = prefix.replaceFirst(":+$", "");
136 previousNamespaceForPrefix = this.namespacesByPrefix.put(prefix, namespaceUri);
137 String previousPrefix = this.prefixesByNamespace.put(namespaceUri, prefix);
138 if (previousPrefix != null && !previousPrefix.equals(prefix)) {
139 this.namespacesByPrefix.remove(previousPrefix);
140 }
141 if (previousNamespaceForPrefix != null && !previousNamespaceForPrefix.equals(namespaceUri)) {
142 this.prefixesByNamespace.remove(previousNamespaceForPrefix);
143 }
144 return previousNamespaceForPrefix;
145 }
146
147 /**
148 * {@inheritDoc}
149 *
150 * @see org.jboss.dna.graph.property.NamespaceRegistry#unregister(java.lang.String)
151 */
152 public boolean unregister( String namespaceUri ) {
153 CheckArg.isNotNull(namespaceUri, "namespaceUri");
154 namespaceUri = namespaceUri.trim();
155 String prefix = this.prefixesByNamespace.remove(namespaceUri);
156 if (prefix == null) return false;
157 this.namespacesByPrefix.remove(prefix);
158 return true;
159 }
160
161 /**
162 * {@inheritDoc}
163 */
164 public Set<String> getRegisteredNamespaceUris() {
165 Set<String> result = new HashSet<String>();
166 result.addAll(this.prefixesByNamespace.keySet());
167 return Collections.unmodifiableSet(result);
168 }
169
170 /**
171 * {@inheritDoc}
172 *
173 * @see org.jboss.dna.graph.property.NamespaceRegistry#getNamespaces()
174 */
175 public Set<Namespace> getNamespaces() {
176 Set<Namespace> result = new HashSet<Namespace>();
177 for (Map.Entry<String, String> entry : this.namespacesByPrefix.entrySet()) {
178 result.add(new BasicNamespace(entry.getKey(), entry.getValue()));
179 }
180 return Collections.unmodifiableSet(result);
181 }
182
183 /**
184 * {@inheritDoc}
185 *
186 * @see java.lang.Object#toString()
187 */
188 @Override
189 public String toString() {
190 List<Namespace> namespaces = new ArrayList<Namespace>(getNamespaces());
191 Collections.sort(namespaces);
192 return namespaces.toString();
193 }
194
195 protected String generatePrefix() {
196 DecimalFormat formatter = new DecimalFormat(this.generatedPrefixTemplate);
197 return formatter.format(nextGeneratedPrefixNumber++);
198 }
199
200 }