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. import java.util.Enumeration;
  59. import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
  60. import com.sun.org.apache.xerces.internal.util.XMLSymbols;
  61. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  62. /**
  63. * This implementation of NamespaceContext has the ability to maintain multiple
  64. * scopes of namespace/prefix bindings. This is useful it situtions when it is
  65. * not always appropriate for elements to inherit the namespace bindings of their
  66. * ancestors (such as included elements in XInclude).
  67. *
  68. * When searching for a URI to match a prefix, or a prefix to match a URI, it is
  69. * searched for in the current context, then the ancestors of the current context,
  70. * up to the beginning of the current scope. Other scopes are not searched.
  71. *
  72. * @author Peter McCracken, IBM
  73. *
  74. * @version $Id: MultipleScopeNamespaceSupport.java,v 1.5 2004/01/22 16:08:58 mrglavas Exp $
  75. */
  76. public class MultipleScopeNamespaceSupport extends NamespaceSupport {
  77. protected int[] fScope = new int[8];
  78. protected int fCurrentScope;
  79. /**
  80. *
  81. */
  82. public MultipleScopeNamespaceSupport() {
  83. super();
  84. fCurrentScope = 0;
  85. fScope[0] = 0;
  86. }
  87. /**
  88. * @param context
  89. */
  90. public MultipleScopeNamespaceSupport(NamespaceContext context) {
  91. super(context);
  92. fCurrentScope = 0;
  93. fScope[0] = 0;
  94. }
  95. /* (non-Javadoc)
  96. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getAllPrefixes()
  97. */
  98. public Enumeration getAllPrefixes() {
  99. int count = 0;
  100. if (fPrefixes.length < (fNamespace.length / 2)) {
  101. // resize prefix array
  102. String[] prefixes = new String[fNamespaceSize];
  103. fPrefixes = prefixes;
  104. }
  105. String prefix = null;
  106. boolean unique = true;
  107. for (int i = fContext[fScope[fCurrentScope]];
  108. i <= (fNamespaceSize - 2);
  109. i += 2) {
  110. prefix = fNamespace[i];
  111. for (int k = 0; k < count; k++) {
  112. if (fPrefixes[k] == prefix) {
  113. unique = false;
  114. break;
  115. }
  116. }
  117. if (unique) {
  118. fPrefixes[count++] = prefix;
  119. }
  120. unique = true;
  121. }
  122. return new Prefixes(fPrefixes, count);
  123. }
  124. public int getScopeForContext(int context) {
  125. int scope = fCurrentScope;
  126. while (context < fScope[scope]) {
  127. scope--;
  128. }
  129. return scope;
  130. }
  131. /* (non-Javadoc)
  132. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getPrefix(java.lang.String)
  133. */
  134. public String getPrefix(String uri) {
  135. return getPrefix(uri, fNamespaceSize, fScope[fCurrentScope]);
  136. }
  137. /* (non-Javadoc)
  138. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getURI(java.lang.String)
  139. */
  140. public String getURI(String prefix) {
  141. return getURI(prefix, fNamespaceSize, fScope[fCurrentScope]);
  142. }
  143. public String getPrefix(String uri, int context) {
  144. return getPrefix(uri, fContext[context+1], fScope[getScopeForContext(context)]);
  145. }
  146. public String getURI(String prefix, int context) {
  147. return getURI(prefix, fContext[context+1], fScope[getScopeForContext(context)]);
  148. }
  149. public String getPrefix(String uri, int start, int end) {
  150. // this saves us from having a copy of each of these in fNamespace for each scope
  151. if (uri == NamespaceContext.XML_URI) {
  152. return XMLSymbols.PREFIX_XML;
  153. }
  154. if (uri == NamespaceContext.XMLNS_URI) {
  155. return XMLSymbols.PREFIX_XMLNS;
  156. }
  157. // find uri in current context
  158. for (int i = start; i > end; i -= 2) {
  159. if (fNamespace[i - 1] == uri) {
  160. if (getURI(fNamespace[i - 2]) == uri)
  161. return fNamespace[i - 2];
  162. }
  163. }
  164. // uri not found
  165. return null;
  166. }
  167. public String getURI(String prefix, int start, int end) {
  168. // this saves us from having a copy of each of these in fNamespace for each scope
  169. if (prefix == XMLSymbols.PREFIX_XML) {
  170. return NamespaceContext.XML_URI;
  171. }
  172. if (prefix == XMLSymbols.PREFIX_XMLNS) {
  173. return NamespaceContext.XMLNS_URI;
  174. }
  175. // find prefix in current context
  176. for (int i = start; i > end; i -= 2) {
  177. if (fNamespace[i - 2] == prefix) {
  178. return fNamespace[i - 1];
  179. }
  180. }
  181. // prefix not found
  182. return null;
  183. }
  184. /**
  185. * Onlys resets the current scope -- all namespaces defined in lower scopes
  186. * remain valid after a call to reset.
  187. */
  188. public void reset() {
  189. fCurrentContext = fScope[fCurrentScope];
  190. fNamespaceSize = fContext[fCurrentContext];
  191. }
  192. /**
  193. * Begins a new scope. None of the previous namespace bindings will be used,
  194. * until the new scope is popped with popScope()
  195. */
  196. public void pushScope() {
  197. if (fCurrentScope + 1 == fScope.length) {
  198. int[] contextarray = new int[fScope.length * 2];
  199. System.arraycopy(fScope, 0, contextarray, 0, fScope.length);
  200. fScope = contextarray;
  201. }
  202. pushContext();
  203. fScope[++fCurrentScope] = fCurrentContext;
  204. }
  205. /**
  206. * Pops the current scope. The namespace bindings from the new current scope
  207. * are then used for searching for namespaces and prefixes.
  208. */
  209. public void popScope() {
  210. fCurrentContext = fScope[fCurrentScope--];
  211. popContext();
  212. }
  213. }