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. package com.sun.org.apache.xerces.internal.util;
  58. import java.util.Enumeration;
  59. import java.util.NoSuchElementException;
  60. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  61. /**
  62. * Namespace support for XML document handlers. This class doesn't
  63. * perform any error checking and assumes that all strings passed
  64. * as arguments to methods are unique symbols. The SymbolTable class
  65. * can be used for this purpose.
  66. *
  67. * @author Andy Clark, IBM
  68. *
  69. * @version $Id: NamespaceSupport.java,v 1.17 2003/09/23 21:42:31 mrglavas Exp $
  70. */
  71. public class NamespaceSupport implements NamespaceContext {
  72. //
  73. // Data
  74. //
  75. /**
  76. * Namespace binding information. This array is composed of a
  77. * series of tuples containing the namespace binding information:
  78. * <prefix, uri>. The default size can be set to anything
  79. * as long as it is a power of 2 greater than 1.
  80. *
  81. * @see #fNamespaceSize
  82. * @see #fContext
  83. */
  84. protected String[] fNamespace = new String[16 * 2];
  85. /** The top of the namespace information array. */
  86. protected int fNamespaceSize;
  87. // NOTE: The constructor depends on the initial context size
  88. // being at least 1. -Ac
  89. /**
  90. * Context indexes. This array contains indexes into the namespace
  91. * information array. The index at the current context is the start
  92. * index of declared namespace bindings and runs to the size of the
  93. * namespace information array.
  94. *
  95. * @see #fNamespaceSize
  96. */
  97. protected int[] fContext = new int[8];
  98. /** The current context. */
  99. protected int fCurrentContext;
  100. protected String[] fPrefixes = new String[16];
  101. //
  102. // Constructors
  103. //
  104. /** Default constructor. */
  105. public NamespaceSupport() {
  106. } // <init>()
  107. /**
  108. * Constructs a namespace context object and initializes it with
  109. * the prefixes declared in the specified context.
  110. */
  111. public NamespaceSupport(NamespaceContext context) {
  112. pushContext();
  113. // copy declaration in the context
  114. Enumeration prefixes = context.getAllPrefixes();
  115. while (prefixes.hasMoreElements()){
  116. String prefix = (String)prefixes.nextElement();
  117. String uri = context.getURI(prefix);
  118. declarePrefix(prefix, uri);
  119. }
  120. } // <init>(NamespaceContext)
  121. //
  122. // Public methods
  123. //
  124. /**
  125. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#reset()
  126. */
  127. public void reset() {
  128. // reset namespace and context info
  129. fNamespaceSize = 0;
  130. fCurrentContext = 0;
  131. fContext[fCurrentContext] = fNamespaceSize;
  132. // bind "xml" prefix to the XML uri
  133. fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XML;
  134. fNamespace[fNamespaceSize++] = NamespaceContext.XML_URI;
  135. // bind "xmlns" prefix to the XMLNS uri
  136. fNamespace[fNamespaceSize++] = XMLSymbols.PREFIX_XMLNS;
  137. fNamespace[fNamespaceSize++] = NamespaceContext.XMLNS_URI;
  138. ++fCurrentContext;
  139. } // reset(SymbolTable)
  140. /**
  141. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#pushContext()
  142. */
  143. public void pushContext() {
  144. // extend the array, if necessary
  145. if (fCurrentContext + 1 == fContext.length) {
  146. int[] contextarray = new int[fContext.length * 2];
  147. System.arraycopy(fContext, 0, contextarray, 0, fContext.length);
  148. fContext = contextarray;
  149. }
  150. // push context
  151. fContext[++fCurrentContext] = fNamespaceSize;
  152. } // pushContext()
  153. /**
  154. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#popContext()
  155. */
  156. public void popContext() {
  157. fNamespaceSize = fContext[fCurrentContext--];
  158. } // popContext()
  159. /**
  160. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#declarePrefix(String, String)
  161. */
  162. public boolean declarePrefix(String prefix, String uri) {
  163. // ignore "xml" and "xmlns" prefixes
  164. if (prefix == XMLSymbols.PREFIX_XML || prefix == XMLSymbols.PREFIX_XMLNS) {
  165. return false;
  166. }
  167. // see if prefix already exists in current context
  168. for (int i = fNamespaceSize; i > fContext[fCurrentContext]; i -= 2) {
  169. if (fNamespace[i - 2] == prefix) {
  170. // REVISIT: [Q] Should the new binding override the
  171. // previously declared binding or should it
  172. // it be ignored? -Ac
  173. // NOTE: The SAX2 "NamespaceSupport" helper allows
  174. // re-bindings with the new binding overwriting
  175. // the previous binding. -Ac
  176. fNamespace[i - 1] = uri;
  177. return true;
  178. }
  179. }
  180. // resize array, if needed
  181. if (fNamespaceSize == fNamespace.length) {
  182. String[] namespacearray = new String[fNamespaceSize * 2];
  183. System.arraycopy(fNamespace, 0, namespacearray, 0, fNamespaceSize);
  184. fNamespace = namespacearray;
  185. }
  186. // bind prefix to uri in current context
  187. fNamespace[fNamespaceSize++] = prefix;
  188. fNamespace[fNamespaceSize++] = uri;
  189. return true;
  190. } // declarePrefix(String,String):boolean
  191. /**
  192. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getURI(String)
  193. */
  194. public String getURI(String prefix) {
  195. // find prefix in current context
  196. for (int i = fNamespaceSize; i > 0; i -= 2) {
  197. if (fNamespace[i - 2] == prefix) {
  198. return fNamespace[i - 1];
  199. }
  200. }
  201. // prefix not found
  202. return null;
  203. } // getURI(String):String
  204. /**
  205. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getPrefix(String)
  206. */
  207. public String getPrefix(String uri) {
  208. // find uri in current context
  209. for (int i = fNamespaceSize; i > 0; i -= 2) {
  210. if (fNamespace[i - 1] == uri) {
  211. if (getURI(fNamespace[i - 2]) == uri)
  212. return fNamespace[i - 2];
  213. }
  214. }
  215. // uri not found
  216. return null;
  217. } // getPrefix(String):String
  218. /**
  219. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getDeclaredPrefixCount()
  220. */
  221. public int getDeclaredPrefixCount() {
  222. return (fNamespaceSize - fContext[fCurrentContext]) / 2;
  223. } // getDeclaredPrefixCount():int
  224. /**
  225. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getDeclaredPrefixAt(int)
  226. */
  227. public String getDeclaredPrefixAt(int index) {
  228. return fNamespace[fContext[fCurrentContext] + index * 2];
  229. } // getDeclaredPrefixAt(int):String
  230. /**
  231. * @see com.sun.org.apache.xerces.internal.xni.NamespaceContext#getAllPrefixes()
  232. */
  233. public Enumeration getAllPrefixes() {
  234. int count = 0;
  235. if (fPrefixes.length < (fNamespace.length2)) {
  236. // resize prefix array
  237. String[] prefixes = new String[fNamespaceSize];
  238. fPrefixes = prefixes;
  239. }
  240. String prefix = null;
  241. boolean unique = true;
  242. for (int i = 2; i < (fNamespaceSize-2); i += 2) {
  243. prefix = fNamespace[i + 2];
  244. for (int k=0;k<count;k++){
  245. if (fPrefixes[k]==prefix){
  246. unique = false;
  247. break;
  248. }
  249. }
  250. if (unique){
  251. fPrefixes[count++] = prefix;
  252. }
  253. unique = true;
  254. }
  255. return new Prefixes(fPrefixes, count);
  256. }
  257. protected final class Prefixes implements Enumeration {
  258. private String[] prefixes;
  259. private int counter = 0;
  260. private int size = 0;
  261. /**
  262. * Constructor for Prefixes.
  263. */
  264. public Prefixes(String [] prefixes, int size) {
  265. this.prefixes = prefixes;
  266. this.size = size;
  267. }
  268. /**
  269. * @see java.util.Enumeration#hasMoreElements()
  270. */
  271. public boolean hasMoreElements() {
  272. return (counter< size);
  273. }
  274. /**
  275. * @see java.util.Enumeration#nextElement()
  276. */
  277. public Object nextElement() {
  278. if (counter< size){
  279. return fPrefixes[counter++];
  280. }
  281. throw new NoSuchElementException("Illegal access to Namespace prefixes enumeration.");
  282. }
  283. public String toString(){
  284. StringBuffer buf = new StringBuffer();
  285. for (int i=0;i<size;i++){
  286. buf.append(prefixes[i]);
  287. buf.append(" ");
  288. }
  289. return buf.toString();
  290. }
  291. }
  292. } // class NamespaceSupport