1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 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) 2001, 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.impl.xs;
  58. import java.util.Hashtable;
  59. import java.util.Enumeration;
  60. import java.util.Vector;
  61. /**
  62. * A class used to hold the internal schema grammar set for the current instance
  63. * @author Sandy Gao, IBM
  64. * @version $Id: XSGrammarBucket.java,v 1.8 2003/09/23 21:42:31 mrglavas Exp $
  65. */
  66. public class XSGrammarBucket {
  67. // Data
  68. /**
  69. * Hashtable that maps between Namespace and a Grammar
  70. */
  71. Hashtable fGrammarRegistry = new Hashtable();
  72. SchemaGrammar fNoNSGrammar = null;
  73. /**
  74. * Get the schema grammar for the specified namespace
  75. *
  76. * @param namespace
  77. * @return SchemaGrammar associated with the namespace
  78. */
  79. public SchemaGrammar getGrammar(String namespace) {
  80. if (namespace == null)
  81. return fNoNSGrammar;
  82. return (SchemaGrammar)fGrammarRegistry.get(namespace);
  83. }
  84. /**
  85. * Put a schema grammar into the registry
  86. * This method is for internal use only: it assumes that a grammar with
  87. * the same target namespace is not already in the bucket.
  88. *
  89. * @param grammar the grammar to put in the registry
  90. */
  91. public void putGrammar(SchemaGrammar grammar) {
  92. if (grammar.getTargetNamespace() == null)
  93. fNoNSGrammar = grammar;
  94. else
  95. fGrammarRegistry.put(grammar.getTargetNamespace(), grammar);
  96. }
  97. /**
  98. * put a schema grammar and any grammars imported by it (directly or
  99. * inderectly) into the registry. when a grammar with the same target
  100. * namespace is already in the bucket, and different from the one being
  101. * added, it's an error, and no grammar will be added into the bucket.
  102. *
  103. * @param grammar the grammar to put in the registry
  104. * @param deep whether to add imported grammars
  105. * @return whether the process succeeded
  106. */
  107. public boolean putGrammar(SchemaGrammar grammar, boolean deep) {
  108. // whether there is one with the same tns
  109. SchemaGrammar sg = getGrammar(grammar.fTargetNamespace);
  110. if (sg != null) {
  111. // if the one we have is different from the one passed, it's an error
  112. return sg == grammar;
  113. }
  114. // not deep import, then just add this one grammar
  115. if (!deep) {
  116. putGrammar(grammar);
  117. return true;
  118. }
  119. // get all imported grammars, and make a copy of the Vector, so that
  120. // we can recursively process the grammars, and add distinct ones
  121. // to the same vector
  122. Vector currGrammars = (Vector)grammar.getImportedGrammars();
  123. if (currGrammars == null) {
  124. putGrammar(grammar);
  125. return true;
  126. }
  127. Vector grammars = ((Vector)currGrammars.clone());
  128. SchemaGrammar sg1, sg2;
  129. Vector gs;
  130. // for all (recursively) imported grammars
  131. for (int i = 0; i < grammars.size(); i++) {
  132. // get the grammar
  133. sg1 = (SchemaGrammar)grammars.elementAt(i);
  134. // check whether the bucket has one with the same tns
  135. sg2 = getGrammar(sg1.fTargetNamespace);
  136. if (sg2 == null) {
  137. // we need to add grammars imported by sg1 too
  138. gs = sg1.getImportedGrammars();
  139. // for all grammars imported by sg2, but not in the vector
  140. // we add them to the vector
  141. if(gs == null) continue;
  142. for (int j = gs.size() - 1; j >= 0; j--) {
  143. sg2 = (SchemaGrammar)gs.elementAt(j);
  144. if (!grammars.contains(sg2))
  145. grammars.addElement(sg2);
  146. }
  147. }
  148. // we found one with the same target namespace
  149. // if the two grammars are not the same object, then it's an error
  150. else if (sg2 != sg1) {
  151. return false;
  152. }
  153. }
  154. // now we have all imported grammars stored in the vector. add them
  155. putGrammar(grammar);
  156. for (int i = grammars.size() - 1; i >= 0; i--)
  157. putGrammar((SchemaGrammar)grammars.elementAt(i));
  158. return true;
  159. }
  160. /**
  161. * get all grammars in the registry
  162. *
  163. * @return an array of SchemaGrammars.
  164. */
  165. public SchemaGrammar[] getGrammars() {
  166. // get the number of grammars
  167. int count = fGrammarRegistry.size() + (fNoNSGrammar==null ? 0 : 1);
  168. SchemaGrammar[] grammars = new SchemaGrammar[count];
  169. // get grammars with target namespace
  170. Enumeration schemas = fGrammarRegistry.elements();
  171. int i = 0;
  172. while (schemas.hasMoreElements())
  173. grammars[i++] = (SchemaGrammar)schemas.nextElement();
  174. // add the grammar without target namespace, if any
  175. if (fNoNSGrammar != null)
  176. grammars[count-1] = fNoNSGrammar;
  177. return grammars;
  178. }
  179. /**
  180. * Clear the registry.
  181. * REVISIT: update to use another XSGrammarBucket
  182. */
  183. public void reset() {
  184. fNoNSGrammar = null;
  185. fGrammarRegistry.clear();
  186. }
  187. } // class XSGrammarBucket