KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > xinclude > MultipleScopeNamespaceSupport


1 /*
2  * Copyright 2003-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 package org.apache.xerces.xinclude;
17
18 import java.util.Enumeration JavaDoc;
19
20 import org.apache.xerces.util.NamespaceSupport;
21 import org.apache.xerces.util.XMLSymbols;
22 import org.apache.xerces.xni.NamespaceContext;
23
24 /**
25  * This implementation of NamespaceContext has the ability to maintain multiple
26  * scopes of namespace/prefix bindings. This is useful in situations when it is
27  * not always appropriate for elements to inherit the namespace bindings of their
28  * ancestors (such as included elements in XInclude).
29  *
30  * When searching for a URI to match a prefix, or a prefix to match a URI, it is
31  * searched for in the current context, then the ancestors of the current context,
32  * up to the beginning of the current scope. Other scopes are not searched.
33  *
34  * @author Peter McCracken, IBM
35  *
36  * @version $Id: MultipleScopeNamespaceSupport.java,v 1.8 2005/02/17 06:08:16 mrglavas Exp $
37  */

38 public class MultipleScopeNamespaceSupport extends NamespaceSupport {
39
40     protected int[] fScope = new int[8];
41     protected int fCurrentScope;
42
43     /**
44      *
45      */

46     public MultipleScopeNamespaceSupport() {
47         super();
48         fCurrentScope = 0;
49         fScope[0] = 0;
50     }
51
52     /**
53      * @param context
54      */

55     public MultipleScopeNamespaceSupport(NamespaceContext context) {
56         super(context);
57         fCurrentScope = 0;
58         fScope[0] = 0;
59     }
60
61     /* (non-Javadoc)
62      * @see org.apache.xerces.xni.NamespaceContext#getAllPrefixes()
63      */

64     public Enumeration JavaDoc getAllPrefixes() {
65         int count = 0;
66         if (fPrefixes.length < (fNamespace.length / 2)) {
67             // resize prefix array
68
String JavaDoc[] prefixes = new String JavaDoc[fNamespaceSize];
69             fPrefixes = prefixes;
70         }
71         String JavaDoc prefix = null;
72         boolean unique = true;
73         for (int i = fContext[fScope[fCurrentScope]];
74             i <= (fNamespaceSize - 2);
75             i += 2) {
76             prefix = fNamespace[i];
77             for (int k = 0; k < count; k++) {
78                 if (fPrefixes[k] == prefix) {
79                     unique = false;
80                     break;
81                 }
82             }
83             if (unique) {
84                 fPrefixes[count++] = prefix;
85             }
86             unique = true;
87         }
88         return new Prefixes(fPrefixes, count);
89     }
90
91     public int getScopeForContext(int context) {
92         int scope = fCurrentScope;
93         while (context < fScope[scope]) {
94             scope--;
95         }
96         return scope;
97     }
98
99     /* (non-Javadoc)
100      * @see org.apache.xerces.xni.NamespaceContext#getPrefix(java.lang.String)
101      */

102     public String JavaDoc getPrefix(String JavaDoc uri) {
103         return getPrefix(uri, fNamespaceSize, fContext[fScope[fCurrentScope]]);
104     }
105
106     /* (non-Javadoc)
107      * @see org.apache.xerces.xni.NamespaceContext#getURI(java.lang.String)
108      */

109     public String JavaDoc getURI(String JavaDoc prefix) {
110         return getURI(prefix, fNamespaceSize, fContext[fScope[fCurrentScope]]);
111     }
112
113     public String JavaDoc getPrefix(String JavaDoc uri, int context) {
114         return getPrefix(uri, fContext[context+1], fContext[fScope[getScopeForContext(context)]]);
115     }
116
117     public String JavaDoc getURI(String JavaDoc prefix, int context) {
118         return getURI(prefix, fContext[context+1], fContext[fScope[getScopeForContext(context)]]);
119     }
120
121     public String JavaDoc getPrefix(String JavaDoc uri, int start, int end) {
122         // this saves us from having a copy of each of these in fNamespace for each scope
123
if (uri == NamespaceContext.XML_URI) {
124             return XMLSymbols.PREFIX_XML;
125         }
126         if (uri == NamespaceContext.XMLNS_URI) {
127             return XMLSymbols.PREFIX_XMLNS;
128         }
129
130         // find uri in current context
131
for (int i = start; i > end; i -= 2) {
132             if (fNamespace[i - 1] == uri) {
133                 if (getURI(fNamespace[i - 2]) == uri)
134                     return fNamespace[i - 2];
135             }
136         }
137
138         // uri not found
139
return null;
140     }
141
142     public String JavaDoc getURI(String JavaDoc prefix, int start, int end) {
143         // this saves us from having a copy of each of these in fNamespace for each scope
144
if (prefix == XMLSymbols.PREFIX_XML) {
145             return NamespaceContext.XML_URI;
146         }
147         if (prefix == XMLSymbols.PREFIX_XMLNS) {
148             return NamespaceContext.XMLNS_URI;
149         }
150
151         // find prefix in current context
152
for (int i = start; i > end; i -= 2) {
153             if (fNamespace[i - 2] == prefix) {
154                 return fNamespace[i - 1];
155             }
156         }
157
158         // prefix not found
159
return null;
160     }
161
162     /**
163      * Only resets the current scope -- all namespaces defined in lower scopes
164      * remain valid after a call to reset.
165      */

166     public void reset() {
167         fCurrentContext = fScope[fCurrentScope];
168         fNamespaceSize = fContext[fCurrentContext];
169     }
170
171     /**
172      * Begins a new scope. None of the previous namespace bindings will be used,
173      * until the new scope is popped with popScope()
174      */

175     public void pushScope() {
176         if (fCurrentScope + 1 == fScope.length) {
177             int[] contextarray = new int[fScope.length * 2];
178             System.arraycopy(fScope, 0, contextarray, 0, fScope.length);
179             fScope = contextarray;
180         }
181         pushContext();
182         fScope[++fCurrentScope] = fCurrentContext;
183     }
184
185     /**
186      * Pops the current scope. The namespace bindings from the new current scope
187      * are then used for searching for namespaces and prefixes.
188      */

189     public void popScope() {
190         fCurrentContext = fScope[fCurrentScope--];
191         popContext();
192     }
193 }
194
Popular Tags