001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.util;
040:
041: import java.util.ArrayList;
042: import java.util.Iterator;
043: import java.util.List;
044: import javax.xml.namespace.NamespaceContext;
045:
046: /**
047: *
048: * @author Paul.Sandoz@Sun.Com
049: */
050: final public class NamespaceContextImplementation implements
051: NamespaceContext {
052: private static int DEFAULT_SIZE = 8;
053:
054: private String[] prefixes = new String[DEFAULT_SIZE];
055: private String[] namespaceURIs = new String[DEFAULT_SIZE];
056: private int namespacePosition;
057:
058: private int[] contexts = new int[DEFAULT_SIZE];
059: private int contextPosition;
060:
061: private int currentContext;
062:
063: public NamespaceContextImplementation() {
064: prefixes[0] = "xml";
065: namespaceURIs[0] = "http://www.w3.org/XML/1998/namespace";
066: prefixes[1] = "xmlns";
067: namespaceURIs[1] = "http://www.w3.org/2000/xmlns/";
068:
069: currentContext = namespacePosition = 2;
070: }
071:
072: public String getNamespaceURI(String prefix) {
073: if (prefix == null)
074: throw new IllegalArgumentException();
075:
076: prefix = prefix.intern();
077:
078: for (int i = namespacePosition - 1; i >= 0; i--) {
079: final String declaredPrefix = prefixes[i];
080: if (declaredPrefix == prefix) {
081: return namespaceURIs[i];
082: }
083: }
084:
085: return "";
086: }
087:
088: public String getPrefix(String namespaceURI) {
089: if (namespaceURI == null)
090: throw new IllegalArgumentException();
091:
092: // namespaceURI = namespaceURI.intern();
093:
094: for (int i = namespacePosition - 1; i >= 0; i--) {
095: final String declaredNamespaceURI = namespaceURIs[i];
096: if (declaredNamespaceURI == namespaceURI
097: || declaredNamespaceURI.equals(namespaceURI)) {
098: final String declaredPrefix = prefixes[i];
099:
100: // Check if prefix is out of scope
101: for (++i; i < namespacePosition; i++)
102: if (declaredPrefix == prefixes[i])
103: return null;
104:
105: return declaredPrefix;
106: }
107: }
108:
109: return null;
110: }
111:
112: public String getNonDefaultPrefix(String namespaceURI) {
113: if (namespaceURI == null)
114: throw new IllegalArgumentException();
115:
116: // namespaceURI = namespaceURI.intern();
117:
118: for (int i = namespacePosition - 1; i >= 0; i--) {
119: final String declaredNamespaceURI = namespaceURIs[i];
120: if ((declaredNamespaceURI == namespaceURI || declaredNamespaceURI
121: .equals(namespaceURI))
122: && prefixes[i].length() > 0) {
123: final String declaredPrefix = prefixes[i];
124:
125: // Check if prefix is out of scope
126: for (++i; i < namespacePosition; i++)
127: if (declaredPrefix == prefixes[i])
128: return null;
129:
130: return declaredPrefix;
131: }
132: }
133:
134: return null;
135: }
136:
137: public Iterator getPrefixes(String namespaceURI) {
138: if (namespaceURI == null)
139: throw new IllegalArgumentException();
140:
141: // namespaceURI = namespaceURI.intern();
142:
143: List l = new ArrayList();
144:
145: NAMESPACE_LOOP: for (int i = namespacePosition - 1; i >= 0; i--) {
146: final String declaredNamespaceURI = namespaceURIs[i];
147: if (declaredNamespaceURI == namespaceURI
148: || declaredNamespaceURI.equals(namespaceURI)) {
149: final String declaredPrefix = prefixes[i];
150:
151: // Check if prefix is out of scope
152: for (int j = i + 1; j < namespacePosition; j++)
153: if (declaredPrefix == prefixes[j])
154: continue NAMESPACE_LOOP;
155:
156: l.add(declaredPrefix);
157: }
158: }
159:
160: return l.iterator();
161: }
162:
163: public String getPrefix(int index) {
164: return prefixes[index];
165: }
166:
167: public String getNamespaceURI(int index) {
168: return namespaceURIs[index];
169: }
170:
171: public int getCurrentContextStartIndex() {
172: return currentContext;
173: }
174:
175: public int getCurrentContextEndIndex() {
176: return namespacePosition;
177: }
178:
179: public boolean isCurrentContextEmpty() {
180: return currentContext == namespacePosition;
181: }
182:
183: public void declarePrefix(String prefix, String namespaceURI) {
184: prefix = prefix.intern();
185: namespaceURI = namespaceURI.intern();
186:
187: // Ignore the "xml" or "xmlns" declarations
188: if (prefix == "xml" || prefix == "xmlns")
189: return;
190:
191: // Replace any previous declaration
192: for (int i = currentContext; i < namespacePosition; i++) {
193: final String declaredPrefix = prefixes[i];
194: if (declaredPrefix == prefix) {
195: prefixes[i] = prefix;
196: namespaceURIs[i] = namespaceURI;
197: return;
198: }
199: }
200:
201: if (namespacePosition == namespaceURIs.length)
202: resizeNamespaces();
203:
204: // Add new declaration
205: prefixes[namespacePosition] = prefix;
206: namespaceURIs[namespacePosition++] = namespaceURI;
207: }
208:
209: private void resizeNamespaces() {
210: final int newLength = namespaceURIs.length * 3 / 2 + 1;
211:
212: String[] newPrefixes = new String[newLength];
213: System.arraycopy(prefixes, 0, newPrefixes, 0, prefixes.length);
214: prefixes = newPrefixes;
215:
216: String[] newNamespaceURIs = new String[newLength];
217: System.arraycopy(namespaceURIs, 0, newNamespaceURIs, 0,
218: namespaceURIs.length);
219: namespaceURIs = newNamespaceURIs;
220: }
221:
222: public void pushContext() {
223: if (contextPosition == contexts.length)
224: resizeContexts();
225:
226: contexts[contextPosition++] = currentContext = namespacePosition;
227: }
228:
229: private void resizeContexts() {
230: int[] newContexts = new int[contexts.length * 3 / 2 + 1];
231: System.arraycopy(contexts, 0, newContexts, 0, contexts.length);
232: contexts = newContexts;
233: }
234:
235: public void popContext() {
236: if (contextPosition > 0) {
237: namespacePosition = currentContext = contexts[--contextPosition];
238: }
239: }
240:
241: public void reset() {
242: currentContext = namespacePosition = 2;
243: }
244: }
|