001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2000-2002 The Apache Software Foundation.
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package com.sun.xml.stream.xerces.util;
059:
060: import java.util.Enumeration;
061: import java.util.Iterator;
062: import java.util.NoSuchElementException;
063: import java.util.Vector;
064:
065: import com.sun.xml.stream.xerces.xni.NamespaceContext;
066:
067: /**
068: * Namespace support for XML document handlers. This class doesn't
069: * perform any error checking and assumes that all strings passed
070: * as arguments to methods are unique symbols. The SymbolTable class
071: * can be used for this purpose.
072: *
073: * @author Andy Clark, IBM
074: *
075: * @version $Id: NamespaceSupport.java,v 1.2 2006/04/01 06:01:41 jeffsuttor Exp $
076: */
077: public class NamespaceSupport implements NamespaceContext {
078:
079: //
080: // Data
081: //
082:
083: /**
084: * Namespace binding information. This array is composed of a
085: * series of tuples containing the namespace binding information:
086: * <prefix, uri>. The default size can be set to anything
087: * as long as it is a power of 2 greater than 1.
088: *
089: * @see #fNamespaceSize
090: * @see #fContext
091: */
092: protected String[] fNamespace = new String[16 * 2];
093:
094: /** The top of the namespace information array. */
095: protected int fNamespaceSize;
096:
097: // NOTE: The constructor depends on the initial context size
098: // being at least 1. -Ac
099:
100: /**
101: * Context indexes. This array contains indexes into the namespace
102: * information array. The index at the current context is the start
103: * index of declared namespace bindings and runs to the size of the
104: * namespace information array.
105: *
106: * @see #fNamespaceSize
107: */
108: protected int[] fContext = new int[8];
109:
110: /** The current context. */
111: protected int fCurrentContext;
112:
113: protected String[] fPrefixes = new String[16];
114:
115: //
116: // Constructors
117: //
118:
119: /** Default constructor. */
120: public NamespaceSupport() {
121: } // <init>()
122:
123: /**
124: * Constructs a namespace context object and initializes it with
125: * the prefixes declared in the specified context.
126: */
127: public NamespaceSupport(NamespaceContext context) {
128: pushContext();
129: // copy declaration in the context
130: Enumeration prefixes = context.getAllPrefixes();
131: while (prefixes.hasMoreElements()) {
132: String prefix = (String) prefixes.nextElement();
133: String uri = context.getURI(prefix);
134: declarePrefix(prefix, uri);
135: }
136: } // <init>(NamespaceContext)
137:
138: //
139: // Public methods
140: //
141:
142: /**
143: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#reset()
144: */
145: public void reset() {
146:
147: // reset namespace and context info
148: fNamespaceSize = 0;
149: fCurrentContext = 0;
150:
151: // bind "xml" prefix to the XML uri
152: fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML;
153: fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI;
154: // bind "xmlns" prefix to the XMLNS uri
155: fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS;
156: fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI;
157:
158: fContext[fCurrentContext] = fNamespaceSize;
159: //++fCurrentContext;
160:
161: } // reset(SymbolTable)
162:
163: /**
164: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#pushContext()
165: */
166: public void pushContext() {
167:
168: // extend the array, if necessary
169: if (fCurrentContext + 1 == fContext.length) {
170: int[] contextarray = new int[fContext.length * 2];
171: System.arraycopy(fContext, 0, contextarray, 0,
172: fContext.length);
173: fContext = contextarray;
174: }
175:
176: // push context
177: fContext[++fCurrentContext] = fNamespaceSize;
178: //System.out.println("calling push context, current context = " + fCurrentContext);
179: } // pushContext()
180:
181: /**
182: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#popContext()
183: */
184: public void popContext() {
185: fNamespaceSize = fContext[fCurrentContext--];
186: //System.out.println("Calling popContext, fCurrentContext = " + fCurrentContext);
187: } // popContext()
188:
189: /**
190: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#declarePrefix(String, String)
191: */
192: public boolean declarePrefix(String prefix, String uri) {
193: // ignore "xml" and "xmlns" prefixes
194: if (prefix == XMLSymbols.PREFIX_XML
195: || prefix == XMLSymbols.PREFIX_XMLNS) {
196: return false;
197: }
198:
199: // see if prefix already exists in current context
200: for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
201: if (fNamespace[i - 2] == prefix) {
202: // REVISIT: [Q] Should the new binding override the
203: // previously declared binding or should it
204: // it be ignored? -Ac
205: // NOTE: The SAX2 "NamespaceSupport" helper allows
206: // re-bindings with the new binding overwriting
207: // the previous binding. -Ac
208: fNamespace[i - 1] = uri;
209: return true;
210: }
211: }
212:
213: // resize array, if needed
214: if (fNamespaceSize == fNamespace.length) {
215: String[] namespacearray = new String[fNamespaceSize * 2];
216: System.arraycopy(fNamespace, 0, namespacearray, 0,
217: fNamespaceSize);
218: fNamespace = namespacearray;
219: }
220:
221: // bind prefix to uri in current context
222: fNamespace[fNamespaceSize++] = prefix;
223: fNamespace[fNamespaceSize++] = uri;
224:
225: return true;
226:
227: } // declarePrefix(String,String):boolean
228:
229: /**
230: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#getURI(String)
231: */
232: public String getURI(String prefix) {
233:
234: // find prefix in current context
235: for (int i = fNamespaceSize; i > 0; i -= 2) {
236: if (fNamespace[i - 2] == prefix) {
237: return fNamespace[i - 1];
238: }
239: }
240:
241: // prefix not found
242: return null;
243:
244: } // getURI(String):String
245:
246: /**
247: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#getPrefix(String)
248: */
249: public String getPrefix(String uri) {
250:
251: // find uri in current context
252: for (int i = fNamespaceSize; i > 0; i -= 2) {
253: if (fNamespace[i - 1] == uri) {
254: if (getURI(fNamespace[i - 2]) == uri)
255: return fNamespace[i - 2];
256: }
257: }
258:
259: // uri not found
260: return null;
261:
262: } // getPrefix(String):String
263:
264: /**
265: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#getDeclaredPrefixCount()
266: */
267: public int getDeclaredPrefixCount() {
268: return (fNamespaceSize - fContext[fCurrentContext]) / 2;
269: } // getDeclaredPrefixCount():int
270:
271: /**
272: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#getDeclaredPrefixAt(int)
273: */
274: public String getDeclaredPrefixAt(int index) {
275: return fNamespace[fContext[fCurrentContext] + index * 2];
276: } // getDeclaredPrefixAt(int):String
277:
278: public Iterator getPrefixes() {
279: int count = 0;
280: if (fPrefixes.length < (fNamespace.length / 2)) {
281: // resize prefix array
282: String[] prefixes = new String[fNamespaceSize];
283: fPrefixes = prefixes;
284: }
285: String prefix = null;
286: boolean unique = true;
287: for (int i = 2; i < (fNamespaceSize - 2); i += 2) {
288: prefix = fNamespace[i + 2];
289: for (int k = 0; k < count; k++) {
290: if (fPrefixes[k] == prefix) {
291: unique = false;
292: break;
293: }
294: }
295: if (unique) {
296: fPrefixes[count++] = prefix;
297: }
298: unique = true;
299: }
300: return new IteratorPrefixes(fPrefixes, count);
301: }//getPrefixes
302:
303: /**
304: * @see com.sun.xml.stream.xerces.xni.NamespaceContext#getAllPrefixes()
305: */
306: public Enumeration getAllPrefixes() {
307: int count = 0;
308: if (fPrefixes.length < (fNamespace.length / 2)) {
309: // resize prefix array
310: String[] prefixes = new String[fNamespaceSize];
311: fPrefixes = prefixes;
312: }
313: String prefix = null;
314: boolean unique = true;
315: for (int i = 2; i < (fNamespaceSize - 2); i += 2) {
316: prefix = fNamespace[i + 2];
317: for (int k = 0; k < count; k++) {
318: if (fPrefixes[k] == prefix) {
319: unique = false;
320: break;
321: }
322: }
323: if (unique) {
324: fPrefixes[count++] = prefix;
325: }
326: unique = true;
327: }
328: return new Prefixes(fPrefixes, count);
329: }
330:
331: public Vector getPrefixes(String uri) {
332: int count = 0;
333: String prefix = null;
334: boolean unique = true;
335: Vector prefixList = new Vector();
336: for (int i = fNamespaceSize; i > 0; i -= 2) {
337: if (fNamespace[i - 1] == uri) {
338: if (!prefixList.contains(fNamespace[i - 2]))
339: prefixList.add(fNamespace[i - 2]);
340: }
341: }
342: return prefixList;
343: }
344:
345: /*
346: * non-NamespaceContext methods
347: */
348:
349: /**
350: * Checks whether a binding or unbinding for
351: * the given prefix exists in the context.
352: *
353: * @param prefix The prefix to look up.
354: *
355: * @return true if the given prefix exists in the context
356: */
357: public boolean containsPrefix(String prefix) {
358:
359: // find prefix in context
360: for (int i = fNamespaceSize; i > 0; i -= 2) {
361: if (fNamespace[i - 2] == prefix) {
362: return true;
363: }
364: }
365:
366: // prefix not found
367: return false;
368: }
369:
370: /**
371: * Checks whether a binding or unbinding for
372: * the given prefix exists in the current context.
373: *
374: * @param prefix The prefix to look up.
375: *
376: * @return true if the given prefix exists in the current context
377: */
378: public boolean containsPrefixInCurrentContext(String prefix) {
379:
380: // find prefix in current context
381: for (int i = fContext[fCurrentContext]; i < fNamespaceSize; i += 2) {
382: if (fNamespace[i] == prefix) {
383: return true;
384: }
385: }
386:
387: // prefix not found
388: return false;
389: }
390:
391: protected final class IteratorPrefixes implements Iterator {
392: private String[] prefixes;
393: private int counter = 0;
394: private int size = 0;
395:
396: /**
397: * Constructor for Prefixes.
398: */
399: public IteratorPrefixes(String[] prefixes, int size) {
400: this .prefixes = prefixes;
401: this .size = size;
402: }
403:
404: /**
405: * @see java.util.Enumeration#hasMoreElements()
406: */
407: public boolean hasNext() {
408: return (counter < size);
409: }
410:
411: /**
412: * @see java.util.Enumeration#nextElement()
413: */
414: public Object next() {
415: if (counter < size) {
416: return fPrefixes[counter++];
417: }
418: throw new NoSuchElementException(
419: "Illegal access to Namespace prefixes enumeration.");
420: }
421:
422: public String toString() {
423: StringBuffer buf = new StringBuffer();
424: for (int i = 0; i < size; i++) {
425: buf.append(prefixes[i]);
426: buf.append(" ");
427: }
428:
429: return buf.toString();
430: }
431:
432: public void remove() {
433: throw new UnsupportedOperationException();
434: }
435: }
436:
437: protected final class Prefixes implements Enumeration {
438: private String[] prefixes;
439: private int counter = 0;
440: private int size = 0;
441:
442: /**
443: * Constructor for Prefixes.
444: */
445: public Prefixes(String[] prefixes, int size) {
446: this .prefixes = prefixes;
447: this .size = size;
448: }
449:
450: /**
451: * @see java.util.Enumeration#hasMoreElements()
452: */
453: public boolean hasMoreElements() {
454: return (counter < size);
455: }
456:
457: /**
458: * @see java.util.Enumeration#nextElement()
459: */
460: public Object nextElement() {
461: if (counter < size) {
462: return fPrefixes[counter++];
463: }
464: throw new NoSuchElementException(
465: "Illegal access to Namespace prefixes enumeration.");
466: }
467:
468: public String toString() {
469: StringBuffer buf = new StringBuffer();
470: for (int i = 0; i < size; i++) {
471: buf.append(prefixes[i]);
472: buf.append(" ");
473: }
474:
475: return buf.toString();
476: }
477:
478: }
479:
480: } // class NamespaceSupport
|