KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > NamespaceSupport


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2002 The Apache Software Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.util;
59
60 import java.util.Enumeration JavaDoc;
61 import java.util.NoSuchElementException JavaDoc;
62
63 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
64
65 /**
66  * Namespace support for XML document handlers. This class doesn't
67  * perform any error checking and assumes that all strings passed
68  * as arguments to methods are unique symbols. The SymbolTable class
69  * can be used for this purpose.
70  *
71  * @author Andy Clark, IBM
72  *
73  * @version $Id: NamespaceSupport.java,v 1.17 2003/09/23 21:42:31 mrglavas Exp $
74  */

75 public class NamespaceSupport implements NamespaceContext {
76
77     //
78
// Data
79
//
80

81     /**
82      * Namespace binding information. This array is composed of a
83      * series of tuples containing the namespace binding information:
84      * &lt;prefix, uri&gt;. The default size can be set to anything
85      * as long as it is a power of 2 greater than 1.
86      *
87      * @see #fNamespaceSize
88      * @see #fContext
89      */

90     protected String JavaDoc[] fNamespace = new String JavaDoc[16 * 2];
91
92     /** The top of the namespace information array. */
93     protected int fNamespaceSize;
94
95     // NOTE: The constructor depends on the initial context size
96
// being at least 1. -Ac
97

98     /**
99      * Context indexes. This array contains indexes into the namespace
100      * information array. The index at the current context is the start
101      * index of declared namespace bindings and runs to the size of the
102      * namespace information array.
103      *
104      * @see #fNamespaceSize
105      */

106     protected int[] fContext = new int[8];
107
108     /** The current context. */
109     protected int fCurrentContext;
110     
111     protected String JavaDoc[] fPrefixes = new String JavaDoc[16];
112     
113     //
114
// Constructors
115
//
116

117     /** Default constructor. */
118     public NamespaceSupport() {
119     } // <init>()
120

121     /**
122      * Constructs a namespace context object and initializes it with
123      * the prefixes declared in the specified context.
124      */

125     public NamespaceSupport(NamespaceContext context) {
126         pushContext();
127         // copy declaration in the context
128
Enumeration JavaDoc prefixes = context.getAllPrefixes();
129         while (prefixes.hasMoreElements()){
130             String JavaDoc prefix = (String JavaDoc)prefixes.nextElement();
131             String JavaDoc uri = context.getURI(prefix);
132             declarePrefix(prefix, uri);
133         }
134       } // <init>(NamespaceContext)
135

136
137     //
138
// Public methods
139
//
140

141     /**
142      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#reset()
143      */

144     public void reset() {
145
146         // reset namespace and context info
147
fNamespaceSize = 0;
148         fCurrentContext = 0;
149         fContext[fCurrentContext] = fNamespaceSize;
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         ++fCurrentContext;
158
159     } // reset(SymbolTable)
160

161
162     /**
163      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#pushContext()
164      */

165     public void pushContext() {
166
167         // extend the array, if necessary
168
if (fCurrentContext + 1 == fContext.length) {
169             int[] contextarray = new int[fContext.length * 2];
170             System.arraycopy(fContext, 0, contextarray, 0, fContext.length);
171             fContext = contextarray;
172         }
173
174         // push context
175
fContext[++fCurrentContext] = fNamespaceSize;
176
177     } // pushContext()
178

179
180     /**
181      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#popContext()
182      */

183     public void popContext() {
184         fNamespaceSize = fContext[fCurrentContext--];
185     } // popContext()
186

187     /**
188      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#declarePrefix(String, String)
189      */

190     public boolean declarePrefix(String JavaDoc prefix, String JavaDoc uri) {
191         // ignore "xml" and "xmlns" prefixes
192
if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) {
193             return false;
194         }
195
196         // see if prefix already exists in current context
197
for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
198             if (fNamespace[i - 2] == prefix) {
199                 // REVISIT: [Q] Should the new binding override the
200
// previously declared binding or should it
201
// it be ignored? -Ac
202
// NOTE: The SAX2 "NamespaceSupport" helper allows
203
// re-bindings with the new binding overwriting
204
// the previous binding. -Ac
205
fNamespace[i - 1] = uri;
206                 return true;
207             }
208         }
209
210         // resize array, if needed
211
if (fNamespaceSize == fNamespace.length) {
212             String JavaDoc[] namespacearray = new String JavaDoc[fNamespaceSize * 2];
213             System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize);
214             fNamespace = namespacearray;
215         }
216
217         // bind prefix to uri in current context
218
fNamespace[fNamespaceSize++] = prefix;
219         fNamespace[fNamespaceSize++] = uri;
220
221         return true;
222
223     } // declarePrefix(String,String):boolean
224

225     /**
226      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getURI(String)
227      */

228     public String JavaDoc getURI(String JavaDoc prefix) {
229         
230         // find prefix in current context
231
for (int i = fNamespaceSize; i > 0; i -= 2) {
232             if (fNamespace[i - 2] == prefix) {
233                 return fNamespace[i - 1];
234             }
235         }
236
237         // prefix not found
238
return null;
239
240     } // getURI(String):String
241

242
243     /**
244      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getPrefix(String)
245      */

246     public String JavaDoc getPrefix(String JavaDoc uri) {
247
248         // find uri in current context
249
for (int i = fNamespaceSize; i > 0; i -= 2) {
250             if (fNamespace[i - 1] == uri) {
251                 if (getURI(fNamespace[i - 2]) == uri)
252                     return fNamespace[i - 2];
253             }
254         }
255
256         // uri not found
257
return null;
258
259     } // getPrefix(String):String
260

261
262     /**
263      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getDeclaredPrefixCount()
264      */

265     public int getDeclaredPrefixCount() {
266         return (fNamespaceSize - fContext[fCurrentContext]) / 2;
267     } // getDeclaredPrefixCount():int
268

269     /**
270      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getDeclaredPrefixAt(int)
271      */

272     public String JavaDoc getDeclaredPrefixAt(int index) {
273         return fNamespace[fContext[fCurrentContext] + index * 2];
274     } // getDeclaredPrefixAt(int):String
275

276     /**
277      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getAllPrefixes()
278      */

279     public Enumeration JavaDoc getAllPrefixes() {
280         int count = 0;
281         if (fPrefixes.length < (fNamespace.length/2)) {
282             // resize prefix array
283
String JavaDoc[] prefixes = new String JavaDoc[fNamespaceSize];
284             fPrefixes = prefixes;
285         }
286         String JavaDoc prefix = null;
287         boolean unique = true;
288         for (int i = 2; i < (fNamespaceSize-2); i += 2) {
289             prefix = fNamespace[i + 2];
290             for (int k=0;k<count;k++){
291                 if (fPrefixes[k]==prefix){
292                     unique = false;
293                     break;
294                 }
295             }
296             if (unique){
297                 fPrefixes[count++] = prefix;
298             }
299             unique = true;
300         }
301         return new Prefixes(fPrefixes, count);
302     }
303     
304     protected final class Prefixes implements Enumeration JavaDoc {
305         private String JavaDoc[] prefixes;
306         private int counter = 0;
307         private int size = 0;
308                
309         /**
310          * Constructor for Prefixes.
311          */

312         public Prefixes(String JavaDoc [] prefixes, int size) {
313             this.prefixes = prefixes;
314             this.size = size;
315         }
316
317        /**
318          * @see java.util.Enumeration#hasMoreElements()
319          */

320         public boolean hasMoreElements() {
321             return (counter< size);
322         }
323
324         /**
325          * @see java.util.Enumeration#nextElement()
326          */

327         public Object JavaDoc nextElement() {
328             if (counter< size){
329                 return fPrefixes[counter++];
330             }
331             throw new NoSuchElementException JavaDoc("Illegal access to Namespace prefixes enumeration.");
332         }
333         
334         public String JavaDoc toString(){
335             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
336             for (int i=0;i<size;i++){
337                 buf.append(prefixes[i]);
338                 buf.append(" ");
339             }
340                 
341             return buf.toString();
342         }
343
344 }
345
346 } // class NamespaceSupport
347
Popular Tags