File: NameTableImpl.java

package info (click to toggle)
libxt-java 0.19991105-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,908 kB
  • ctags: 2,762
  • sloc: java: 12,823; makefile: 52; xml: 46
file content (280 lines) | stat: -rw-r--r-- 7,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.jclark.xsl.om;

import java.util.Hashtable;

public class NameTableImpl implements NameTable {

  private final
  class NamespacePrefixMapImpl implements NamespacePrefixMap {
    final private String[] bindings;
    final private String defaultNS;

    NamespacePrefixMapImpl() {
      this.bindings = new String[0];
      this.defaultNS = null;
    }

    private
    NamespacePrefixMapImpl(String[] bindings, String defaultNS) {
      this.bindings = bindings;
      this.defaultNS = defaultNS;
    }

    public final
    int getSize() {
      return bindings.length >> 1;
    }

    public final
    String getPrefix(int i) {
      return bindings[i << 1];
    }

    public final
    String getNamespace(int i) {
      return bindings[(i << 1) | 1];
    }

    public final
    String getPrefix(String namespace) {
      for (int i = 1; i < bindings.length; i += 2) {
	if (namespace.equals(bindings[i]))
	  return bindings[i - 1];
      }
      return null;
    }

    public final
    String getNamespace(String prefix) {
      for (int i = 0; i < bindings.length; i += 2) {
	if (prefix.equals(bindings[i]))
	  return bindings[i + 1];
      }
      return null;
    }

    public final
    String getDefaultNamespace() {
      return defaultNS;
    }

    public
    Name expandAttributeName(String name, Node node) throws XSLException {
      int i = name.indexOf(':');
      if (i == -1)
	return createName(name);
      if (i == 3 && name.regionMatches(0, "xml", 0, 3))
	return createName(name, Name.XML_NAMESPACE);
      for (int j = 0; j < bindings.length; j += 2) {
	String prefix = bindings[j];
	if (prefix.length() == i && name.regionMatches(0, prefix, 0, i))
	  return createName(name, bindings[j + 1]);
      }
      throw new XSLException("no such prefix \"" + name.substring(0, i) + '"', node);
    }

    public
    Name expandElementTypeName(String name, Node node) throws XSLException {
      int i = name.indexOf(':');
      if (i == -1) {
	if (defaultNS != null)
	  return createName(name, defaultNS);
	else
	  return createName(name);
      }
      if (i == 3 && name.regionMatches(0, "xml", 0, 3))
	return createName(name, Name.XML_NAMESPACE);
      for (int j = 0; j < bindings.length; j += 2) {
	String prefix = bindings[j];
	if (prefix.length() == i && name.regionMatches(0, prefix, 0, i))
	  return createName(name, bindings[j + 1]);
      }
      throw new XSLException("no such prefix \"" + name.substring(0, i) + '"', node);
    }

    public
    NamespacePrefixMap unbind(String prefix) {
      for (int i = 0; i < bindings.length; i += 2) {
	if (prefix.equals(bindings[i])) {
	  String[] newBindings = new String[bindings.length - 2];
	  System.arraycopy(bindings, 0, newBindings, 0, i);
	  System.arraycopy(bindings, i + 2, newBindings, i, bindings.length - i - 2);
	  return intern(new NamespacePrefixMapImpl(newBindings, defaultNS));
	}
      }
      return this;
    }

    public
    NamespacePrefixMap bind(String prefix, String namespace) {
      int i;
      for (i = 0; i < bindings.length; i += 2) {
	int cmp = prefix.compareTo(bindings[i]);
	if (cmp < 0)
	  break;
	if (cmp == 0) {
	  if (namespace.equals(bindings[i + 1]))
	    return this;
	  String[] newBindings = (String[])bindings.clone();
	  newBindings[i + 1] = namespace;
	  return intern(new NamespacePrefixMapImpl(newBindings, defaultNS));
	}
      }
      String[] newBindings = new String[bindings.length + 2];
      System.arraycopy(bindings, 0, newBindings, 0, i);
      newBindings[i] = prefix;
      newBindings[i + 1] = namespace;
      System.arraycopy(bindings, i, newBindings, i + 2, bindings.length - i);
      return intern(new NamespacePrefixMapImpl(newBindings, defaultNS));
    }

    public
    NamespacePrefixMap bindDefault(String namespace) {
      if (namespace.equals(defaultNS))
	return this;
      return intern(new NamespacePrefixMapImpl(bindings, namespace));
    }

    public
    NamespacePrefixMap unbindDefault() {
      if (defaultNS == null)
	return this;
      return intern(new NamespacePrefixMapImpl(bindings, null));
    }

    public NameTable getNameTable() {
      return NameTableImpl.this;
    }

    public
    int hashCode() {
      int h = defaultNS != null ? defaultNS.hashCode() : 0;
      for (int i = 0; i < bindings.length; i++)
	h ^= bindings[i].hashCode();
      return h;
    }

    public
    boolean equals(Object obj) {
      if (obj == null || !(obj instanceof NamespacePrefixMapImpl))
	return false;
      NamespacePrefixMapImpl map = (NamespacePrefixMapImpl)obj;
      if (defaultNS == null) {
	if (map.defaultNS != null)
	  return false;
      }
      else if (!defaultNS.equals(map.defaultNS))
	return false;
      if (bindings.length != map.bindings.length)
	return false;
      for (int i = 0; i < bindings.length; i++) {
	if (!bindings[i].equals(map.bindings[i]))
	  return false;
      }
      return true;
    }
  }

  private
  class NameImpl implements Name {
    private String qName;
    private String namespace;
    private NameImpl canon;

    NameImpl(String qName, String namespace) {
      this.qName = qName;
      this.namespace = namespace;
      this.canon = this;
    }

    NameImpl(String qName, String namespace, NameImpl canon) {
      this.qName = qName;
      this.namespace = namespace;
      this.canon = canon;
    }

    public String getNamespace() {
      return namespace;
    }
    public String getLocalPart() {
      return canon.qName;
    }
    public String getPrefix() {
      int i = qName.indexOf(':');
      if (i < 0)
	return null;
      return qName.substring(0, i);
    }
    public boolean equals(Object obj) {
      if (obj != null && (obj instanceof NameImpl))
	return ((NameImpl)obj).canon == canon;
      return false;
    }
    public String toString() {
      return qName;
    }
    public int hashCode() {
      return System.identityHashCode(canon);
    }

    public Object getCreator() {
      return NameTableImpl.this;
    }
  }

  public Name createName(String qName, String namespace) {
    Hashtable ns;
    synchronized (namespaces) {
      ns = (Hashtable)namespaces.get(namespace);
      if (ns == null) {
	ns = new Hashtable();
	namespaces.put(namespace, ns);
      }
    }
    return createName(ns, qName, namespace);
  }

  public Name createName(String nonQName) {
    return createName(docNamespace, nonQName, null);
  }

  NameImpl createName(Hashtable ns, String qName, String namespace) {
    synchronized (ns) {
      NameImpl nm = (NameImpl)ns.get(qName);
      if (nm == null) {
	int i = qName.indexOf(':');
	if (i == -1)
	  nm = new NameImpl(qName, namespace);
	else
	  nm = new NameImpl(qName,
			    namespace,
			    createName(ns, qName.substring(i + 1), namespace));
	ns.put(qName, nm);
      }
      return nm;
    }
  }

  public NamespacePrefixMap getEmptyNamespacePrefixMap() {
    return emptyMap;
  }

  NamespacePrefixMap intern(NamespacePrefixMap prefixMap) {
    synchronized (prefixMaps) {
      Object obj = prefixMaps.get(prefixMap);
      if (obj != null)
	return (NamespacePrefixMap)obj;
      prefixMaps.put(prefixMap, prefixMap);
      return prefixMap;
    }
  }

  public NameTableImpl() {
    prefixMaps.put(emptyMap, emptyMap);
  }

  private NamespacePrefixMap emptyMap = new NamespacePrefixMapImpl();
  private Hashtable prefixMaps = new Hashtable();
  private Hashtable namespaces = new Hashtable();
  private Hashtable docNamespace = new Hashtable();
}