Debian Codegraph @

net/sf/saxon/om/PrefixPool.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
9 package net.sf.saxon.om;
10
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.Map;
14
15 /**
16 * A prefix pool maintains a two-way mapping from namespace prefixes (as strings) to
17 * integer prefix codes. Prefix codes always fit in 10 bits, but are handled as ints.
18 *
19 * Until 9.8, prefixes were managed by the NamePool. The NamePool now only handles
20 * fingerprints, which are integer representations of the URI and local name parts of
21 * a QName. Prefix codes are now used only in the tinytree, and the table of codes
22 * is local to a document. For this reason, access is not synchronised.
23 */
24 public class PrefixPool {
25
26 String[] prefixes = new String[8];
27 int used = 0;
28 Map<String, Integer> index = null;
29
30 public PrefixPool() {
31 prefixes[0] = "";
32 used = 1;
33 }
34
35 /**
36 * Get the prefix code corresponding to a given prefix, allocating a new code if necessary
37 * @param prefix the namespace prefix. If empty, the prefix code is always zero.
38 * @return the integer prefix code (always fits in 10 bits)
39 */
40
41 public int obtainPrefixCode(String prefix) {
42 if (prefix.isEmpty()) {
43 return 0;
44 }
45 // Create an index if it's going to be useful
46 if (index == null && used > 8) {
47 makeIndex();
48 }
49 // See if the prefix is already known
50 if (index != null) {
51 Integer existing = index.get(prefix);
52 if (existing != null) {
53 return existing;
54 }
55 } else {
56 for (int i=0; i<used; i++) {
57 if (prefixes[i].equals(prefix)) {
58 return i;
59 }
60 }
61 }
62 // Allocate a new code
63 int code = used++;
64 if (used >= prefixes.length) {
65 prefixes = Arrays.copyOf(prefixes, used * 2);
66 }
67 prefixes[code] = prefix;
68 if (index != null) {
69 index.put(prefix, code);
70 }
71 return code;
72 }
73
74 private void makeIndex() {
75 index = new HashMap<>(used);
76 for (int i=0; i<used; i++) {
77 index.put(prefixes[i], i);
78 }
79 }
80
81 /**
82 * Get the prefix corresponding to a given code
83 * @param code the prefix code (which must have been allocated)
84 * @return the corresponding prefix
85 * @throws IllegalArgumentException if the code has not been allocated
86 */
87
88 public String getPrefix(int code) {
89 if (code < used) {
90 return prefixes[code];
91 }
92 throw new IllegalArgumentException("Unknown prefix code " + code);
93 }
94
95 /**
96 * Eliminate unused space, on the assumption that no further prefixes will be added to the pool,
97 * and that subsequent access will be to get the prefix for a code, and not vice versa.
98 */
99
100 public void condense() {
101 prefixes = Arrays.copyOf(prefixes, used);
102 index = null;
103 }
104 }
105