Debian Codegraph @

net/sf/saxon/om/NamePool.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.om;
9
10 import net.sf.saxon.lib.NamespaceConstant;
11
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.atomic.AtomicInteger;
14
15
16 /**
17 * A NamePool holds a collection of expanded names, each containing a namespace URI,
18 * and a local name.
19 * <p>Each expanded name is allocated a unique 20-bit fingerprint. The fingerprint enables
20 * the URI and the local name to be determined. Some subsystems (notably the Tinytree) use
21 * the top 10 bits to represent the prefix, but the NamePool is no longer concerned with
22 * managing prefixes, and prefixes do not have global codes.</p>
23 * <p>The NamePool has been redesigned in Saxon 9.7 to make use of two Java
24 * ConcurrentHashMaps, one from QNames to integers, one from integers to QNames.
25 * This gives better scaleability in terms of multithreaded concurrency and in terms
26 * of the capacity of the NamePool and retention of performance as the size of
27 * the vocabulary increases.</p>
28 * <p>Fingerprints in the range 0 to 1023 are reserved for system use, and are allocated as constants
29 * mainly to names in the XSLT and XML Schema namespaces: constants representing these names
30 * are found in {@link StandardNames}.</p>
31
32 * <p>The fingerprint -1 is reserved to mean "not known" or inapplicable.</p>
33 * <p>Modified in 9.4 to remove namespace codes.</p>
34 * <p>Modified in 9.7 to remove URI codes.</p>
35 * <p>Modified in 9.8 to remove namecodes and all handling of prefixes.</p>
36 */
37
38 public final class NamePool {
39
40 /**
41 * FP_MASK is a mask used to obtain a fingerprint from a nameCode. Given a
42 * nameCode nc, the fingerprint is <code>nc &amp; NamePool.FP_MASK</code>.
43 * (In practice, Saxon code often uses the literal constant 0xfffff,
44 * to extract the bottom 20 bits).
45 * <p>The difference between a fingerprint and a nameCode is that
46 * a nameCode contains information
47 * about the prefix of a name, the fingerprint depends only on the namespace
48 * URI and local name. Note that the "null" nameCode (-1) does not produce
49 * the "null" fingerprint (also -1) when this mask is applied.</p>
50 */
51
52 public static final int FP_MASK = 0xfffff;
53
54 // Since fingerprints in the range 0-1023 belong to predefined names, user-defined names
55 // will always have a fingerprint above this range, which can be tested by a mask.
56
57 public static final int USER_DEFINED_MASK = 0xffc00;
58
59 // Limit: maximum number of fingerprints
60
61 private static final int MAX_FINGERPRINT = FP_MASK;
62
63 // A map from QNames to fingerprints
64
65 private final ConcurrentHashMap<StructuredQName, Integer> qNameToInteger = new ConcurrentHashMap<StructuredQName, Integer>(1000);
66
67 // A map from fingerprints to QNames
68
69 private final ConcurrentHashMap<Integer, StructuredQName> integerToQName = new ConcurrentHashMap<Integer, StructuredQName>(1000);
70
71 // Next fingerprint available to be allocated. Starts at 1024 as low-end fingerprints are statically allocated to system-defined
72 // names
73
74 private AtomicInteger unique = new AtomicInteger(1024);
75
76 // A map containing suggested prefixes for particular URIs
77
78 private ConcurrentHashMap<String, String> suggestedPrefixes = new ConcurrentHashMap<String, String>();
79
80
81
82 /**
83 * Create a NamePool
84 */
85
86 public NamePool() {
87
88 }
89
90 /**
91 * Suggest a preferred prefix to be used with a given URI
92 * @param uri the URI
93 * @param prefix the suggested prefix
94 */
95
96 public void suggestPrefix(String prefix, String uri) {
97 suggestedPrefixes.put(uri, prefix);
98 }
99
100 /**
101 * Get a QName for a given namecode.
102 *
103 * @param nameCode a code identifying an expanded QName, e.g. of an element or attribute
104 * @return a qName containing the URI and local name corresponding to the supplied name code.
105 * The prefix will be set to an empty string.
106 */
107
108 public StructuredQName getUnprefixedQName(int nameCode) {
109 int fp = nameCode & FP_MASK;
110 if ((fp & USER_DEFINED_MASK) == 0) {
111 return StandardNames.getUnprefixedQName(fp);
112 }
113 return integerToQName.get(fp);
114 }
115
116 /**
117 * Get a QName for a given fingerprint.
118 *
119 * @param fingerprint a code identifying an expanded QName, e.g. of an element or attribute
120 * @return a qName containing the URI and local name corresponding to the supplied fingerprint.
121 * There will be no prefix
122 */
123
124 public StructuredQName getStructuredQName(int fingerprint) {
125 return getUnprefixedQName(fingerprint);
126 }
127
128 /**
129 * Determine whether a given namecode has a non-empty prefix (and therefore, in the case of attributes,
130 * whether the name is in a non-null namespace
131 *
132 * @param nameCode the name code to be tested
133 * @return true if the name has a non-empty prefix
134 */
135
136 public static boolean isPrefixed(int nameCode) {
137 return (nameCode & 0x3ff00000) != 0;
138 }
139
140 /**
141 * Suggest a prefix for a given URI. If there are several, it's undefined which one is returned.
142 * If there are no prefixes registered for this URI, return null.
143 *
144 * @param uri the namespace URI
145 * @return a prefix that has previously been associated with this URI, if available; otherwise null
146 */
147
148 public String suggestPrefixForURI(String uri) {
149 if (uri.equals(NamespaceConstant.XML)) {
150 return "xml";
151 }
152 return suggestedPrefixes.get(uri);
153 }
154
155 /**
156 * Allocate a fingerprint from the pool, or a new Name if there is not a matching one there
157 *
158 * @param uri the namespace URI. Use "" or null for the non-namespace.
159 * @param local the local part of the name
160 * @return an integer (the "fingerprint") identifying the name within the namepool.
161 * The fingerprint omits information about the prefix, and is the same as the nameCode
162 * for the same name with a prefix equal to "".
163 */
164
165 public synchronized int allocateFingerprint(String uri, String local) {
166 if (NamespaceConstant.isReserved(uri) || NamespaceConstant.SAXON.equals(uri)) {
167 int fp = StandardNames.getFingerprint(uri, local);
168 if (fp != -1) {
169 return fp;
170 }
171 }
172 StructuredQName qName = new StructuredQName("", uri, local);
173 Integer existing = qNameToInteger.get(qName);
174 if (existing != null) {
175 return existing;
176 }
177 Integer next = unique.getAndIncrement();
178 if (next > MAX_FINGERPRINT) {
179 throw new NamePoolLimitException("Too many distinct names in NamePool");
180 }
181 existing = qNameToInteger.putIfAbsent(qName, next);
182 if (existing == null) {
183 integerToQName.put(next, qName);
184 return next;
185 } else {
186 return existing;
187 }
188 }
189
190 /**
191 * Get the namespace-URI of a name, given its name code or fingerprint
192 *
193 * @param nameCode the name code or fingerprint of a name
194 * @return the namespace URI corresponding to this name code. Returns "" for the
195 * no-namespace.
196 * @throws IllegalArgumentException if the nameCode is not known to the NamePool.
197 */
198
199 /*@NotNull*/
200 public String getURI(int nameCode) {
201 int fp = nameCode & FP_MASK;
202 if ((fp & USER_DEFINED_MASK) == 0) {
203 return StandardNames.getURI(fp);
204 }
205 return getUnprefixedQName(fp).getURI();
206 }
207
208 /**
209 * Get the local part of a name, given its name code or fingerprint
210 *
211 * @param nameCode the integer name code or fingerprint of the name
212 * @return the local part of the name represented by this name code or fingerprint
213 */
214
215 public String getLocalName(int nameCode) {
216 return getUnprefixedQName(nameCode).getLocalPart();
217 }
218
219 /**
220 * Get the display form of a name (the QName), given its name code or fingerprint
221 *
222 * @param nameCode the integer name code or fingerprint of a name in the name pool
223 * @return the corresponding lexical QName (if a fingerprint was supplied, this will
224 * simply be the local name)
225 */
226
227 public String getDisplayName(int nameCode) {
228 return getStructuredQName(nameCode).getDisplayName();
229 }
230
231 /**
232 * Get the Clark form of a name, given its name code or fingerprint
233 *
234 * @param nameCode the integer name code or fingerprint of a name in the name pool
235 * @return the local name if the name is in the null namespace, or "{uri}local"
236 * otherwise. The name is always interned.
237 */
238
239 public String getClarkName(int nameCode) {
240 return getUnprefixedQName(nameCode).getClarkName();
241 }
242
243 /**
244 * Get the EQName form of a name, given its name code or fingerprint
245 *
246 * @param nameCode the integer name code or fingerprint of a name in the name pool
247 * @return the name in the form Q{}local for a name in no namespace, or Q{uri}local for
248 * a name in a namespace
249 */
250
251 public String getEQName(int nameCode) {
252 return getUnprefixedQName(nameCode).getEQName();
253 }
254
255 /**
256 * Allocate a fingerprint given a Clark Name
257 *
258 * @param expandedName the name in Clark notation, that is "localname" or "{uri}localName"
259 * @return the fingerprint of the name, which need not previously exist in the name pool
260 */
261
262 public int allocateClarkName(String expandedName) {
263 String namespace;
264 String localName;
265 if (expandedName.charAt(0) == '{') {
266 int closeBrace = expandedName.indexOf('}');
267 if (closeBrace < 0) {
268 throw new IllegalArgumentException("No closing '}' in Clark name");
269 }
270 namespace = expandedName.substring(1, closeBrace);
271 if (closeBrace == expandedName.length()) {
272 throw new IllegalArgumentException("Missing local part in Clark name");
273 }
274 localName = expandedName.substring(closeBrace + 1);
275 } else {
276 namespace = "";
277 localName = expandedName;
278 }
279
280 return allocateFingerprint(namespace, localName);
281 }
282
283 /**
284 * Get a fingerprint for the name with a given uri and local name.
285 * These must be present in the NamePool.
286 * The fingerprint has the property that if two fingerprint are the same, the names
287 * are the same (ie. same local name and same URI).
288 *
289 * @param uri the namespace URI of the required QName
290 * @param localName the local part of the required QName
291 * @return the integer fingerprint, or -1 if this is not found in the name pool
292 */
293
294 public int getFingerprint(String uri, String localName) {
295 // A read-only version of allocate()
296
297 if (NamespaceConstant.isReserved(uri) || uri.equals(NamespaceConstant.SAXON)) {
298 int fp = StandardNames.getFingerprint(uri, localName);
299 if (fp != -1) {
300 return fp;
301 // otherwise, look for the name in this namepool
302 }
303 }
304 Integer fp = qNameToInteger.get(new StructuredQName("", uri, localName));
305 return fp == null ? -1 : fp;
306
307 }
308
309 /**
310 * Unchecked Exception raised when some limit in the design of the name pool is exceeded
311 */
312 public static class NamePoolLimitException extends RuntimeException {
313
314 /**
315 * Create the exception
316 *
317 * @param message the error message associated with the error
318 */
319
320 public NamePoolLimitException(String message) {
321 super(message);
322 }
323 }
324
325 }
326