1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.property.basic;
25
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31 import net.jcip.annotations.ThreadSafe;
32 import org.modeshape.common.util.CheckArg;
33 import org.modeshape.graph.property.NamespaceRegistry;
34
35
36
37
38
39 @ThreadSafe
40 public class LocalNamespaceRegistry extends SimpleNamespaceRegistry {
41
42 private final NamespaceRegistry delegate;
43
44
45
46
47 public LocalNamespaceRegistry( NamespaceRegistry delegate ) {
48 super();
49 CheckArg.isNotNull(delegate, "delegate");
50 this.delegate = delegate;
51 unregister(DEFAULT_NAMESPACE_URI);
52 }
53
54
55
56
57
58 public LocalNamespaceRegistry( NamespaceRegistry delegate,
59 final String defaultNamespaceUri ) {
60 super();
61 CheckArg.isNotNull(delegate, "delegate");
62 this.delegate = delegate;
63 register("", defaultNamespaceUri);
64 }
65
66
67
68
69
70
71 @Override
72 public String getDefaultNamespaceUri() {
73 String result = super.getDefaultNamespaceUri();
74 if (result == null) result = this.delegate.getDefaultNamespaceUri();
75 return result;
76 }
77
78
79
80
81
82
83 @Override
84 public String getNamespaceForPrefix( String prefix ) {
85 String result = super.getNamespaceForPrefix(prefix);
86 if (result == null) {
87 result = this.delegate.getNamespaceForPrefix(prefix);
88
89 if (result != null && super.getPrefixForNamespaceUri(result, false) != null) {
90 return null;
91 }
92 }
93 return result;
94 }
95
96
97
98
99
100
101 @Override
102 public Set<Namespace> getNamespaces() {
103 Set<Namespace> delegateNamespaces = this.delegate.getNamespaces();
104 Set<Namespace> localNamespaces = super.getNamespaces();
105 if (localNamespaces.isEmpty()) return delegateNamespaces;
106
107
108 Set<Namespace> namespaces = new HashSet<Namespace>(localNamespaces);
109
110
111 Map<String, Namespace> localNamespacesByPrefix = new HashMap<String, Namespace>();
112 for (Namespace ns : localNamespaces)
113 localNamespacesByPrefix.put(ns.getPrefix(), ns);
114
115
116 for (Namespace ns : delegateNamespaces) {
117 if (localNamespacesByPrefix.get(ns.getPrefix()) != null) continue;
118
119 namespaces.add(ns);
120 }
121 return Collections.unmodifiableSet(namespaces);
122 }
123
124
125
126
127
128
129 public Set<Namespace> getLocalNamespaces() {
130 return super.getNamespaces();
131 }
132
133
134
135
136
137
138 @Override
139 public String getPrefixForNamespaceUri( String namespaceUri,
140 boolean generateIfMissing ) {
141 String result = super.getPrefixForNamespaceUri(namespaceUri, false);
142 if (result == null) {
143 result = this.delegate.getPrefixForNamespaceUri(namespaceUri, false);
144 if (result != null) {
145
146 if (!this.getNamespaceForPrefix(result).equals(namespaceUri)) {
147 result = null;
148 }
149 }
150 }
151 if (result == null && generateIfMissing) result = super.getPrefixForNamespaceUri(namespaceUri, true);
152 return result;
153 }
154
155
156
157
158
159
160 @Override
161 public Set<String> getRegisteredNamespaceUris() {
162 Set<String> uris = new HashSet<String>(this.delegate.getRegisteredNamespaceUris());
163 uris.addAll(super.getRegisteredNamespaceUris());
164 return Collections.unmodifiableSet(uris);
165 }
166
167
168
169
170
171
172 @Override
173 public boolean isRegisteredNamespaceUri( String namespaceUri ) {
174 return super.isRegisteredNamespaceUri(namespaceUri) || this.delegate.isRegisteredNamespaceUri(namespaceUri);
175 }
176
177
178
179
180
181
182 @Override
183 public String register( String prefix,
184 String namespaceUri ) {
185
186 String previous = super.register(prefix, namespaceUri);
187
188 if (previous == null && delegate != null) previous = delegate.getPrefixForNamespaceUri(namespaceUri, false);
189 return previous;
190 }
191
192
193
194
195
196
197 @Override
198 public boolean unregister( String namespaceUri ) {
199
200 return super.unregister(namespaceUri);
201 }
202
203 }