KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > NamespaceSupport


1 /*
2  * Copyright 2000-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.util;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21
22 import org.apache.xerces.xni.NamespaceContext;
23
24 /**
25  * Namespace support for XML document handlers. This class doesn't
26  * perform any error checking and assumes that all strings passed
27  * as arguments to methods are unique symbols. The SymbolTable class
28  * can be used for this purpose.
29  *
30  * @author Andy Clark, IBM
31  *
32  * @version $Id: NamespaceSupport.java,v 1.20 2005/06/23 05:39:17 mrglavas Exp $
33  */

34 public class NamespaceSupport implements NamespaceContext {
35
36     //
37
// Data
38
//
39

40     /**
41      * Namespace binding information. This array is composed of a
42      * series of tuples containing the namespace binding information:
43      * <prefix, uri>. The default size can be set to anything
44      * as long as it is a power of 2 greater than 1.
45      *
46      * @see #fNamespaceSize
47      * @see #fContext
48      */

49     protected String JavaDoc[] fNamespace = new String JavaDoc[16 * 2];
50
51     /** The top of the namespace information array. */
52     protected int fNamespaceSize;
53
54     // NOTE: The constructor depends on the initial context size
55
// being at least 1. -Ac
56

57     /**
58      * Context indexes. This array contains indexes into the namespace
59      * information array. The index at the current context is the start
60      * index of declared namespace bindings and runs to the size of the
61      * namespace information array.
62      *
63      * @see #fNamespaceSize
64      */

65     protected int[] fContext = new int[8];
66
67     /** The current context. */
68     protected int fCurrentContext;
69     
70     protected String JavaDoc[] fPrefixes = new String JavaDoc[16];
71     
72     //
73
// Constructors
74
//
75

76     /** Default constructor. */
77     public NamespaceSupport() {
78     } // <init>()
79

80     /**
81      * Constructs a namespace context object and initializes it with
82      * the prefixes declared in the specified context.
83      */

84     public NamespaceSupport(NamespaceContext context) {
85         pushContext();
86         // copy declaration in the context
87
Enumeration JavaDoc prefixes = context.getAllPrefixes();
88         while (prefixes.hasMoreElements()){
89             String JavaDoc prefix = (String JavaDoc)prefixes.nextElement();
90             String JavaDoc uri = context.getURI(prefix);
91             declarePrefix(prefix, uri);
92         }
93       } // <init>(NamespaceContext)
94

95
96     //
97
// Public methods
98
//
99

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     /**
122      * @see org.apache.xerces.xni.NamespaceContext#pushContext()
123      */

124     public void pushContext() {
125
126         // extend the array, if necessary
127
if (fCurrentContext + 1 == fContext.length) {
128             int[] contextarray = new int[fContext.length * 2];
129             System.arraycopy(fContext, 0, contextarray, 0, fContext.length);
130             fContext = contextarray;
131         }
132
133         // push context
134
fContext[++fCurrentContext] = fNamespaceSize;
135
136     } // pushContext()
137

138
139     /**
140      * @see org.apache.xerces.xni.NamespaceContext#popContext()
141      */

142     public void popContext() {
143         fNamespaceSize = fContext[fCurrentContext--];
144     } // popContext()
145

146     /**
147      * @see org.apache.xerces.xni.NamespaceContext#declarePrefix(String, String)
148      */

149     public boolean declarePrefix(String JavaDoc prefix, String JavaDoc uri) {
150         // ignore "xml" and "xmlns" prefixes
151
if (prefix == XMLSymbols.PREFIX_XML || 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 JavaDoc[] namespacearray = new String JavaDoc[fNamespaceSize * 2];
172             System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize);
173             fNamespace = namespacearray;
174         }
175
176         // bind prefix to uri in current context
177
fNamespace[fNamespaceSize++] = prefix;
178         fNamespace[fNamespaceSize++] = uri;
179
180         return true;
181
182     } // declarePrefix(String,String):boolean
183

184     /**
185      * @see org.apache.xerces.xni.NamespaceContext#getURI(String)
186      */

187     public String JavaDoc getURI(String JavaDoc prefix) {
188         
189         // find prefix in current context
190
for (int i = fNamespaceSize; i > 0; i -= 2) {
191             if (fNamespace[i - 2] == prefix) {
192                 return fNamespace[i - 1];
193             }
194         }
195
196         // prefix not found
197
return null;
198
199     } // getURI(String):String
200

201
202     /**
203      * @see org.apache.xerces.xni.NamespaceContext#getPrefix(String)
204      */

205     public String JavaDoc getPrefix(String JavaDoc 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     /**
222      * @see org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixCount()
223      */

224     public int getDeclaredPrefixCount() {
225         return (fNamespaceSize - fContext[fCurrentContext]) / 2;
226     } // getDeclaredPrefixCount():int
227

228     /**
229      * @see org.apache.xerces.xni.NamespaceContext#getDeclaredPrefixAt(int)
230      */

231     public String JavaDoc getDeclaredPrefixAt(int index) {
232         return fNamespace[fContext[fCurrentContext] + index * 2];
233     } // getDeclaredPrefixAt(int):String
234

235     /**
236      * @see org.apache.xerces.xni.NamespaceContext#getAllPrefixes()
237      */

238     public Enumeration JavaDoc getAllPrefixes() {
239         int count = 0;
240         if (fPrefixes.length < (fNamespace.length/2)) {
241             // resize prefix array
242
String JavaDoc[] prefixes = new String JavaDoc[fNamespaceSize];
243             fPrefixes = prefixes;
244         }
245         String JavaDoc prefix = null;
246         boolean unique = true;
247         for (int i = 2; i < (fNamespaceSize-2); i += 2) {
248             prefix = fNamespace[i + 2];
249             for (int k=0;k<count;k++){
250                 if (fPrefixes[k]==prefix){
251                     unique = false;
252                     break;
253                 }
254             }
255             if (unique){
256                 fPrefixes[count++] = prefix;
257             }
258             unique = true;
259         }
260         return new Prefixes(fPrefixes, count);
261     }
262     
263     /*
264      * non-NamespaceContext methods
265      */

266     
267     /**
268      * Checks whether a binding or unbinding for
269      * the given prefix exists in the context.
270      *
271      * @param prefix The prefix to look up.
272      *
273      * @return true if the given prefix exists in the context
274      */

275     public boolean containsPrefix(String JavaDoc prefix) {
276
277         // find prefix in current context
278
for (int i = fNamespaceSize; i > 0; i -= 2) {
279             if (fNamespace[i - 2] == prefix) {
280                 return true;
281             }
282         }
283         
284         // prefix not found
285
return false;
286     }
287     
288     protected final class Prefixes implements Enumeration JavaDoc {
289         private String JavaDoc[] prefixes;
290         private int counter = 0;
291         private int size = 0;
292                
293         /**
294          * Constructor for Prefixes.
295          */

296         public Prefixes(String JavaDoc [] prefixes, int size) {
297             this.prefixes = prefixes;
298             this.size = size;
299         }
300
301        /**
302          * @see java.util.Enumeration#hasMoreElements()
303          */

304         public boolean hasMoreElements() {
305             return (counter< size);
306         }
307
308         /**
309          * @see java.util.Enumeration#nextElement()
310          */

311         public Object JavaDoc nextElement() {
312             if (counter< size){
313                 return fPrefixes[counter++];
314             }
315             throw new NoSuchElementException JavaDoc("Illegal access to Namespace prefixes enumeration.");
316         }
317         
318         public String JavaDoc toString(){
319             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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
331
Popular Tags