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.util.Collections; 027 import java.util.HashMap; 028 import java.util.HashSet; 029 import java.util.Map; 030 import java.util.Set; 031 import net.jcip.annotations.ThreadSafe; 032 import org.jboss.dna.common.util.CheckArg; 033 import org.jboss.dna.graph.property.NamespaceRegistry; 034 035 /** 036 * @author Randall Hauch 037 */ 038 @ThreadSafe 039 public class LocalNamespaceRegistry extends SimpleNamespaceRegistry { 040 041 private final NamespaceRegistry delegate; 042 043 /** 044 * @param delegate the namespace registry that this registry should delegate to if not found locally 045 */ 046 public LocalNamespaceRegistry( NamespaceRegistry delegate ) { 047 super(); 048 CheckArg.isNotNull(delegate, "delegate"); 049 this.delegate = delegate; 050 unregister(DEFAULT_NAMESPACE_URI); 051 } 052 053 /** 054 * @param delegate the namespace registry that this registry should delegate to if not found locally 055 * @param defaultNamespaceUri the namespace URI to use for the default prefix 056 */ 057 public LocalNamespaceRegistry( NamespaceRegistry delegate, 058 final String defaultNamespaceUri ) { 059 super(); 060 CheckArg.isNotNull(delegate, "delegate"); 061 this.delegate = delegate; 062 register("", defaultNamespaceUri); 063 } 064 065 /** 066 * {@inheritDoc} 067 * 068 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#getDefaultNamespaceUri() 069 */ 070 @Override 071 public String getDefaultNamespaceUri() { 072 String result = super.getDefaultNamespaceUri(); 073 if (result == null) result = this.delegate.getDefaultNamespaceUri(); 074 return result; 075 } 076 077 /** 078 * {@inheritDoc} 079 * 080 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#getNamespaceForPrefix(java.lang.String) 081 */ 082 @Override 083 public String getNamespaceForPrefix( String prefix ) { 084 String result = super.getNamespaceForPrefix(prefix); 085 if (result == null) result = this.delegate.getNamespaceForPrefix(prefix); 086 return result; 087 } 088 089 /** 090 * {@inheritDoc} 091 * 092 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#getNamespaces() 093 */ 094 @Override 095 public Set<Namespace> getNamespaces() { 096 Set<Namespace> delegateNamespaces = this.delegate.getNamespaces(); 097 Set<Namespace> localNamespaces = super.getNamespaces(); 098 099 // Load the local namespaces first ... 100 Set<Namespace> namespaces = new HashSet<Namespace>(localNamespaces); 101 102 // Now build a map of the local prefixes so we can check for prefixes 103 Map<String, Namespace> localNamespacesByPrefix = new HashMap<String, Namespace>(); 104 for (Namespace ns : localNamespaces) 105 localNamespacesByPrefix.put(ns.getPrefix(), ns); 106 107 // Now iterate over the local namespaces, removing any existing namespace with the same prefix 108 for (Namespace ns : delegateNamespaces) { 109 if (localNamespacesByPrefix.get(ns.getPrefix()) != null) continue; 110 // Try to add the delegate namespace, which won't work if a local with the same URI was already added... 111 namespaces.add(ns); 112 } 113 return Collections.unmodifiableSet(namespaces); 114 } 115 116 /** 117 * {@inheritDoc} 118 * 119 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#getPrefixForNamespaceUri(java.lang.String, boolean) 120 */ 121 @Override 122 public String getPrefixForNamespaceUri( String namespaceUri, 123 boolean generateIfMissing ) { 124 String result = super.getPrefixForNamespaceUri(namespaceUri, false); 125 if (result == null) result = this.delegate.getPrefixForNamespaceUri(namespaceUri, false); 126 if (result == null && generateIfMissing) result = super.getPrefixForNamespaceUri(namespaceUri, true); 127 return result; 128 } 129 130 /** 131 * {@inheritDoc} 132 * 133 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#getRegisteredNamespaceUris() 134 */ 135 @Override 136 public Set<String> getRegisteredNamespaceUris() { 137 Set<String> uris = new HashSet<String>(this.delegate.getRegisteredNamespaceUris()); 138 uris.addAll(super.getRegisteredNamespaceUris()); 139 return Collections.unmodifiableSet(uris); 140 } 141 142 /** 143 * {@inheritDoc} 144 * 145 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#isRegisteredNamespaceUri(java.lang.String) 146 */ 147 @Override 148 public boolean isRegisteredNamespaceUri( String namespaceUri ) { 149 return super.isRegisteredNamespaceUri(namespaceUri) || this.delegate.isRegisteredNamespaceUri(namespaceUri); 150 } 151 152 /** 153 * {@inheritDoc} 154 * 155 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#register(java.lang.String, java.lang.String) 156 */ 157 @Override 158 public String register( String prefix, 159 String namespaceUri ) { 160 // Just register the namespace locally ... 161 String previous = super.register(prefix, namespaceUri); 162 // But check whether there is a "previous" from the delegate ... 163 if (previous == null && delegate != null) previous = delegate.getPrefixForNamespaceUri(namespaceUri, false); 164 return previous; 165 } 166 167 /** 168 * {@inheritDoc} 169 * 170 * @see org.jboss.dna.graph.property.basic.SimpleNamespaceRegistry#unregister(java.lang.String) 171 */ 172 @Override 173 public boolean unregister( String namespaceUri ) { 174 // Unregister locally ... 175 return super.unregister(namespaceUri); 176 } 177 178 }