|
1 /* |
|
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 * |
|
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 */ |
|
27 |
|
28 package com.sun.xml.internal.fastinfoset.util; |
|
29 |
|
30 import java.util.ArrayList; |
|
31 import java.util.Iterator; |
|
32 import java.util.List; |
|
33 import javax.xml.namespace.NamespaceContext; |
|
34 |
|
35 /** |
|
36 * |
|
37 * @author Paul.Sandoz@Sun.Com |
|
38 */ |
|
39 final public class NamespaceContextImplementation implements NamespaceContext { |
|
40 private static int DEFAULT_SIZE = 8; |
|
41 |
|
42 private String[] prefixes = new String[DEFAULT_SIZE]; |
|
43 private String[] namespaceURIs = new String[DEFAULT_SIZE]; |
|
44 private int namespacePosition; |
|
45 |
|
46 private int[] contexts = new int[DEFAULT_SIZE]; |
|
47 private int contextPosition; |
|
48 |
|
49 private int currentContext; |
|
50 |
|
51 public NamespaceContextImplementation() { |
|
52 prefixes[0] = "xml"; |
|
53 namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace"; |
|
54 prefixes[1] = "xmlns"; |
|
55 namespaceURIs[1] = "http://www.w3.org/2000/xmlns/"; |
|
56 |
|
57 currentContext = namespacePosition = 2; |
|
58 } |
|
59 |
|
60 |
|
61 public String getNamespaceURI(String prefix) { |
|
62 if (prefix == null) throw new IllegalArgumentException(); |
|
63 |
|
64 // prefix = prefix.intern(); |
|
65 |
|
66 for (int i = namespacePosition - 1; i >= 0; i--) { |
|
67 final String declaredPrefix = prefixes[i]; |
|
68 if (declaredPrefix.equals(prefix)) { |
|
69 return namespaceURIs[i]; |
|
70 } |
|
71 } |
|
72 |
|
73 return ""; |
|
74 } |
|
75 |
|
76 public String getPrefix(String namespaceURI) { |
|
77 if (namespaceURI == null) throw new IllegalArgumentException(); |
|
78 |
|
79 // namespaceURI = namespaceURI.intern(); |
|
80 |
|
81 for (int i = namespacePosition - 1; i >= 0; i--) { |
|
82 final String declaredNamespaceURI = namespaceURIs[i]; |
|
83 if (declaredNamespaceURI.equals(namespaceURI)) { |
|
84 final String declaredPrefix = prefixes[i]; |
|
85 |
|
86 // Check if prefix is out of scope |
|
87 boolean isOutOfScope = false; |
|
88 for (int j = i + 1; j < namespacePosition; j++) |
|
89 if (declaredPrefix.equals(prefixes[j])) { |
|
90 isOutOfScope = true; |
|
91 break; |
|
92 } |
|
93 |
|
94 if (!isOutOfScope) { |
|
95 return declaredPrefix; |
|
96 } |
|
97 } |
|
98 } |
|
99 |
|
100 return null; |
|
101 } |
|
102 |
|
103 public String getNonDefaultPrefix(String namespaceURI) { |
|
104 if (namespaceURI == null) throw new IllegalArgumentException(); |
|
105 |
|
106 // namespaceURI = namespaceURI.intern(); |
|
107 |
|
108 for (int i = namespacePosition - 1; i >= 0; i--) { |
|
109 final String declaredNamespaceURI = namespaceURIs[i]; |
|
110 if (declaredNamespaceURI.equals(namespaceURI) && |
|
111 prefixes[i].length() > 0){ |
|
112 final String declaredPrefix = prefixes[i]; |
|
113 |
|
114 // Check if prefix is out of scope |
|
115 for (++i; i < namespacePosition; i++) |
|
116 if (declaredPrefix.equals(prefixes[i])) |
|
117 return null; |
|
118 |
|
119 return declaredPrefix; |
|
120 } |
|
121 } |
|
122 |
|
123 return null; |
|
124 } |
|
125 |
|
126 public Iterator getPrefixes(String namespaceURI) { |
|
127 if (namespaceURI == null) throw new IllegalArgumentException(); |
|
128 |
|
129 // namespaceURI = namespaceURI.intern(); |
|
130 |
|
131 List l = new ArrayList(); |
|
132 |
|
133 NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) { |
|
134 final String declaredNamespaceURI = namespaceURIs[i]; |
|
135 if (declaredNamespaceURI.equals(namespaceURI)) { |
|
136 final String declaredPrefix = prefixes[i]; |
|
137 |
|
138 // Check if prefix is out of scope |
|
139 for (int j = i + 1; j < namespacePosition; j++) |
|
140 if (declaredPrefix.equals(prefixes[j])) |
|
141 continue NAMESPACE_LOOP; |
|
142 |
|
143 l.add(declaredPrefix); |
|
144 } |
|
145 } |
|
146 |
|
147 return l.iterator(); |
|
148 } |
|
149 |
|
150 |
|
151 public String getPrefix(int index) { |
|
152 return prefixes[index]; |
|
153 } |
|
154 |
|
155 public String getNamespaceURI(int index) { |
|
156 return namespaceURIs[index]; |
|
157 } |
|
158 |
|
159 public int getCurrentContextStartIndex() { |
|
160 return currentContext; |
|
161 } |
|
162 |
|
163 public int getCurrentContextEndIndex() { |
|
164 return namespacePosition; |
|
165 } |
|
166 |
|
167 public boolean isCurrentContextEmpty() { |
|
168 return currentContext == namespacePosition; |
|
169 } |
|
170 |
|
171 public void declarePrefix(String prefix, String namespaceURI) { |
|
172 prefix = prefix.intern(); |
|
173 namespaceURI = namespaceURI.intern(); |
|
174 |
|
175 // Ignore the "xml" or "xmlns" declarations |
|
176 if (prefix == "xml" || prefix == "xmlns") |
|
177 return; |
|
178 |
|
179 // Replace any previous declaration |
|
180 for (int i = currentContext; i < namespacePosition; i++) { |
|
181 final String declaredPrefix = prefixes[i]; |
|
182 if (declaredPrefix == prefix) { |
|
183 prefixes[i] = prefix; |
|
184 namespaceURIs[i] = namespaceURI; |
|
185 return; |
|
186 } |
|
187 } |
|
188 |
|
189 if (namespacePosition == namespaceURIs.length) |
|
190 resizeNamespaces(); |
|
191 |
|
192 // Add new declaration |
|
193 prefixes[namespacePosition] = prefix; |
|
194 namespaceURIs[namespacePosition++] = namespaceURI; |
|
195 } |
|
196 |
|
197 private void resizeNamespaces() { |
|
198 final int newLength = namespaceURIs.length * 3 / 2 + 1; |
|
199 |
|
200 String[] newPrefixes = new String[newLength]; |
|
201 System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length); |
|
202 prefixes = newPrefixes; |
|
203 |
|
204 String[] newNamespaceURIs = new String[newLength]; |
|
205 System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0, namespaceURIs.length); |
|
206 namespaceURIs = newNamespaceURIs; |
|
207 } |
|
208 |
|
209 public void pushContext() { |
|
210 if (contextPosition == contexts.length) |
|
211 resizeContexts(); |
|
212 |
|
213 contexts[contextPosition++] = currentContext = namespacePosition; |
|
214 } |
|
215 |
|
216 private void resizeContexts() { |
|
217 int[] newContexts = new int[contexts.length * 3 / 2 + 1]; |
|
218 System.arraycopy(contexts, 0, newContexts, 0, contexts.length); |
|
219 contexts = newContexts; |
|
220 } |
|
221 |
|
222 public void popContext() { |
|
223 if (contextPosition > 0) { |
|
224 namespacePosition = currentContext = contexts[--contextPosition]; |
|
225 } |
|
226 } |
|
227 |
|
228 public void reset() { |
|
229 currentContext = namespacePosition = 2; |
|
230 } |
|
231 } |