KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > xinclude > MultipleScopeNamespaceSupport


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 The Apache Software Foundation. All rights
6  * 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) 2003, 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 package com.sun.org.apache.xerces.internal.xinclude;
58
59 import java.util.Enumeration JavaDoc;
60
61 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
62 import com.sun.org.apache.xerces.internal.util.XMLSymbols;
63 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
64
65 /**
66  * This implementation of NamespaceContext has the ability to maintain multiple
67  * scopes of namespace/prefix bindings. This is useful it situtions when it is
68  * not always appropriate for elements to inherit the namespace bindings of their
69  * ancestors (such as included elements in XInclude).
70  *
71  * When searching for a URI to match a prefix, or a prefix to match a URI, it is
72  * searched for in the current context, then the ancestors of the current context,
73  * up to the beginning of the current scope. Other scopes are not searched.
74  *
75  * @author Peter McCracken, IBM
76  *
77  * @version $Id: MultipleScopeNamespaceSupport.java,v 1.5 2004/01/22 16:08:58 mrglavas Exp $
78  */

79 public class MultipleScopeNamespaceSupport extends NamespaceSupport {
80
81     protected int[] fScope = new int[8];
82     protected int fCurrentScope;
83
84     /**
85      *
86      */

87     public MultipleScopeNamespaceSupport() {
88         super();
89         fCurrentScope = 0;
90         fScope[0] = 0;
91     }
92
93     /**
94      * @param context
95      */

96     public MultipleScopeNamespaceSupport(NamespaceContext context) {
97         super(context);
98         fCurrentScope = 0;
99         fScope[0] = 0;
100     }
101
102     /* (non-Javadoc)
103      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getAllPrefixes()
104      */

105     public Enumeration JavaDoc getAllPrefixes() {
106         int count = 0;
107         if (fPrefixes.length < (fNamespace.length / 2)) {
108             // resize prefix array
109
String JavaDoc[] prefixes = new String JavaDoc[fNamespaceSize];
110             fPrefixes = prefixes;
111         }
112         String JavaDoc prefix = null;
113         boolean unique = true;
114         for (int i = fContext[fScope[fCurrentScope]];
115             i <= (fNamespaceSize - 2);
116             i += 2) {
117             prefix = fNamespace[i];
118             for (int k = 0; k < count; k++) {
119                 if (fPrefixes[k] == prefix) {
120                     unique = false;
121                     break;
122                 }
123             }
124             if (unique) {
125                 fPrefixes[count++] = prefix;
126             }
127             unique = true;
128         }
129         return new Prefixes(fPrefixes, count);
130     }
131
132     public int getScopeForContext(int context) {
133         int scope = fCurrentScope;
134                 while (context < fScope[scope]) {
135                     scope--;
136                 }
137         return scope;
138     }
139
140     /* (non-Javadoc)
141      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getPrefix(java.lang.String)
142      */

143     public String JavaDoc getPrefix(String JavaDoc uri) {
144         return getPrefix(uri, fNamespaceSize, fScope[fCurrentScope]);
145     }
146
147     /* (non-Javadoc)
148      * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getURI(java.lang.String)
149      */

150     public String JavaDoc getURI(String JavaDoc prefix) {
151         return getURI(prefix, fNamespaceSize, fScope[fCurrentScope]);
152     }
153
154     public String JavaDoc getPrefix(String JavaDoc uri, int context) {
155         return getPrefix(uri, fContext[context+1], fScope[getScopeForContext(context)]);
156     }
157
158     public String JavaDoc getURI(String JavaDoc prefix, int context) {
159         return getURI(prefix, fContext[context+1], fScope[getScopeForContext(context)]);
160     }
161
162     public String JavaDoc getPrefix(String JavaDoc uri, int start, int end) {
163         // this saves us from having a copy of each of these in fNamespace for each scope
164
if (uri == NamespaceContext.XML_URI) {
165             return XMLSymbols.PREFIX_XML;
166         }
167         if (uri == NamespaceContext.XMLNS_URI) {
168             return XMLSymbols.PREFIX_XMLNS;
169         }
170
171         // find uri in current context
172
for (int i = start; i > end; i -= 2) {
173             if (fNamespace[i - 1] == uri) {
174                 if (getURI(fNamespace[i - 2]) == uri)
175                     return fNamespace[i - 2];
176             }
177         }
178
179         // uri not found
180
return null;
181     }
182
183     public String JavaDoc getURI(String JavaDoc prefix, int start, int end) {
184         // this saves us from having a copy of each of these in fNamespace for each scope
185
if (prefix == XMLSymbols.PREFIX_XML) {
186             return NamespaceContext.XML_URI;
187         }
188         if (prefix == XMLSymbols.PREFIX_XMLNS) {
189             return NamespaceContext.XMLNS_URI;
190         }
191
192         // find prefix in current context
193
for (int i = start; i > end; i -= 2) {
194             if (fNamespace[i - 2] == prefix) {
195                 return fNamespace[i - 1];
196             }
197         }
198
199         // prefix not found
200
return null;
201     }
202
203     /**
204      * Onlys resets the current scope -- all namespaces defined in lower scopes
205      * remain valid after a call to reset.
206      */

207     public void reset() {
208         fCurrentContext = fScope[fCurrentScope];
209         fNamespaceSize = fContext[fCurrentContext];
210     }
211
212     /**
213      * Begins a new scope. None of the previous namespace bindings will be used,
214      * until the new scope is popped with popScope()
215      */

216     public void pushScope() {
217         if (fCurrentScope + 1 == fScope.length) {
218             int[] contextarray = new int[fScope.length * 2];
219             System.arraycopy(fScope, 0, contextarray, 0, fScope.length);
220             fScope = contextarray;
221         }
222         pushContext();
223         fScope[++fCurrentScope] = fCurrentContext;
224     }
225
226     /**
227      * Pops the current scope. The namespace bindings from the new current scope
228      * are then used for searching for namespaces and prefixes.
229      */

230     public void popScope() {
231         fCurrentContext = fScope[fCurrentScope--];
232         popContext();
233     }
234 }
235
Popular Tags