001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import java.util.Enumeration;
021: import java.util.NoSuchElementException;
022:
023: import org.apache.xerces.xni.NamespaceContext;
024:
025: /**
026: * Namespace support for XML document handlers. This class doesn't
027: * perform any error checking and assumes that all strings passed
028: * as arguments to methods are unique symbols. The SymbolTable class
029: * can be used for this purpose.
030: *
031: * @author Andy Clark, IBM
032: *
033: * @version $Id: NamespaceSupport.java 447241 2006-09-18 05:12:57Z mrglavas $
034: */
035: public class NamespaceSupport implements NamespaceContext {
036:
037: //
038: // Data
039: //
040:
041: /**
042: * Namespace binding information. This array is composed of a
043: * series of tuples containing the namespace binding information:
044: * <prefix, uri>. The default size can be set to anything
045: * as long as it is a power of 2 greater than 1.
046: *
047: * @see #fNamespaceSize
048: * @see #fContext
049: */
050: protected String[] fNamespace = new String[16 * 2];
051:
052: /** The top of the namespace information array. */
053: protected int fNamespaceSize;
054:
055: // NOTE: The constructor depends on the initial context size
056: // being at least 1. -Ac
057:
058: /**
059: * Context indexes. This array contains indexes into the namespace
060: * information array. The index at the current context is the start
061: * index of declared namespace bindings and runs to the size of the
062: * namespace information array.
063: *
064: * @see #fNamespaceSize
065: */
066: protected int[] fContext = new int[8];
067:
068: /** The current context. */
069: protected int fCurrentContext;
070:
071: protected String[] fPrefixes = new String[16];
072:
073: //
074: // Constructors
075: //
076:
077: /** Default constructor. */
078: public NamespaceSupport() {
079: } // <init>()
080:
081: /**
082: * Constructs a namespace context object and initializes it with
083: * the prefixes declared in the specified context.
084: */
085: public NamespaceSupport(NamespaceContext context) {
086: pushContext();
087: // copy declaration in the context
088: Enumeration prefixes = context.getAllPrefixes();
089: while (prefixes.hasMoreElements()) {
090: String prefix = (String) prefixes.nextElement();
091: String uri = context.getURI(prefix);
092: declarePrefix(prefix, uri);
093: }
094: } // <init>(NamespaceContext)
095:
096: //
097: // Public methods
098: //
099:
100: /**
101: * @see org.apache.xerces.xni.NamespaceContext#reset()
102: */
103: public void reset() {
104:
105: // reset namespace and context info
106: fNamespaceSize = 0;
107: fCurrentContext = 0;
108: fContext[fCurrentContext] = fNamespaceSize;
109:
110: // bind "xml" prefix to the XML uri
111: fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML;
112: fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI;
113: // bind "xmlns" prefix to the XMLNS uri
114: fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS;
115: fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI;
116: ++fCurrentContext;
117:
118: } // reset(SymbolTable)
119:
120: /**
121: * @see org.apache.xerces.xni.NamespaceContext#pushContext()
122: */
123: public void pushContext() {
124:
125: // extend the array, if necessary
126: if (fCurrentContext + 1 == fContext.length) {
127: int[] contextarray = new int[fContext.length * 2];
128: System.arraycopy(fContext, 0, contextarray, 0,
129: fContext.length);
130: fContext = contextarray;
131: }
132:
133: // push context
134: fContext[++fCurrentContext] = fNamespaceSize;
135:
136: } // pushContext()
137:
138: /**
139: * @see org.apache.xerces.xni.NamespaceContext#popContext()
140: */
141: public void popContext() {
142: fNamespaceSize = fContext[fCurrentContext--];
143: } // popContext()
144:
145: /**
146: * @see org.apache.xerces.xni.NamespaceContext#declarePrefix(String, String)
147: */
148: public boolean declarePrefix(String prefix, String uri) {
149: // ignore "xml" and "xmlns" prefixes
150: if (prefix == XMLSymbols.PREFIX_XML
151: || prefix == XMLSymbols.PREFIX_XMLNS) {
152: return false;
153: }
154:
155: // see if prefix already exists in current context
156: for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
157: if (fNamespace[i - 2] == prefix) {
158: // REVISIT: [Q] Should the new binding override the
159: // previously declared binding or should it
160: // it be ignored? -Ac
161: // NOTE: The SAX2 "NamespaceSupport" helper allows
162: // re-bindings with the new binding overwriting
163: // the previous binding. -Ac
164: fNamespace[i - 1] = uri;
165: return true;
166: }
167: }
168:
169: // resize array, if needed
170: if (fNamespaceSize == fNamespace.length) {
171: String[] namespacearray = new String[fNamespaceSize * 2];
172: System.arraycopy(fNamespace, 0, namespacearray, 0,
173: fNamespaceSize);
174: fNamespace = namespacearray;
175: }
176:
177: // bind prefix to uri in current context
178: fNamespace[fNamespaceSize++] = prefix;
179: fNamespace[fNamespaceSize++] = uri;
180:
181: return true;
182:
183: } // declarePrefix(String,String):boolean
184:
185: /**
186: * @see org.apache.xerces.xni.NamespaceContext#getURI(String)
187: */
188: public String getURI(String prefix) {
189:
190: // find prefix in current context
191: for (int i = fNamespaceSize; i > 0; i -= 2) {
192: if (fNamespace[i - 2] == prefix) {
193: return fNamespace[i - 1];
194: }
195: }
196:
197: // prefix not found
198: return null;
199:
200: } // getURI(String):String
201:
202: /**
203: * @see org.apache.xerces.xni.NamespaceContext#getPrefix(String)
204: */
205: public String getPrefix(String uri) {
206:
207: // find uri in current context
208: for (int i = fNamespaceSize; i > 0; i -= 2) {
209: if (fNamespace[i - 1] == uri) {
210: if (getURI(fNamespace[i - 2]) == uri)
211: return fNamespace[i - 2];
212: }
213: }
214:
215: // uri not found
216: return null;
217:
218: } // getPrefix(String):String
219:
220: /**
221: * @see org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixCount()
222: */
223: public int getDeclaredPrefixCount() {
224: return (fNamespaceSize - fContext[fCurrentContext]) / 2;
225: } // getDeclaredPrefixCount():int
226:
227: /**
228: * @see org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixAt(int)
229: */
230: public String getDeclaredPrefixAt(int index) {
231: return fNamespace[fContext[fCurrentContext] + index * 2];
232: } // getDeclaredPrefixAt(int):String
233:
234: /**
235: * @see org.apache.xerces.xni.NamespaceContext#getAllPrefixes()
236: */
237: public Enumeration getAllPrefixes() {
238: int count = 0;
239: if (fPrefixes.length < (fNamespace.length / 2)) {
240: // resize prefix array
241: String[] prefixes = new String[fNamespaceSize];
242: fPrefixes = prefixes;
243: }
244: String prefix = null;
245: boolean unique = true;
246: for (int i = 2; i < (fNamespaceSize - 2); i += 2) {
247: prefix = fNamespace[i + 2];
248: for (int k = 0; k < count; k++) {
249: if (fPrefixes[k] == prefix) {
250: unique = false;
251: break;
252: }
253: }
254: if (unique) {
255: fPrefixes[count++] = prefix;
256: }
257: unique = true;
258: }
259: return new Prefixes(fPrefixes, count);
260: }
261:
262: /*
263: * non-NamespaceContext methods
264: */
265:
266: /**
267: * Checks whether a binding or unbinding for
268: * the given prefix exists in the context.
269: *
270: * @param prefix The prefix to look up.
271: *
272: * @return true if the given prefix exists in the context
273: */
274: public boolean containsPrefix(String prefix) {
275:
276: // find prefix in current context
277: for (int i = fNamespaceSize; i > 0; i -= 2) {
278: if (fNamespace[i - 2] == prefix) {
279: return true;
280: }
281: }
282:
283: // prefix not found
284: return false;
285: }
286:
287: protected final class Prefixes implements Enumeration {
288: private String[] prefixes;
289: private int counter = 0;
290: private int size = 0;
291:
292: /**
293: * Constructor for Prefixes.
294: */
295: public Prefixes(String[] prefixes, int size) {
296: this .prefixes = prefixes;
297: this .size = size;
298: }
299:
300: /**
301: * @see java.util.Enumeration#hasMoreElements()
302: */
303: public boolean hasMoreElements() {
304: return (counter < size);
305: }
306:
307: /**
308: * @see java.util.Enumeration#nextElement()
309: */
310: public Object nextElement() {
311: if (counter < size) {
312: return fPrefixes[counter++];
313: }
314: throw new NoSuchElementException(
315: "Illegal access to Namespace prefixes enumeration.");
316: }
317:
318: public String toString() {
319: StringBuffer buf = new StringBuffer();
320: for (int i = 0; i < size; i++) {
321: buf.append(prefixes[i]);
322: buf.append(" ");
323: }
324:
325: return buf.toString();
326: }
327:
328: }
329:
330: } // class NamespaceSupport
|