1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 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) 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.validation;
  58. import com.sun.org.apache.xerces.internal.util.SymbolTable;
  59. import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  60. import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  61. import java.util.Hashtable;
  62. import java.util.Enumeration;
  63. /**
  64. * Implementation of ValidationContext inteface. Used to establish an
  65. * environment for simple type validation.
  66. *
  67. * @author Elena Litani, IBM
  68. * @version $Id: ValidationState.java,v 1.13 2003/06/05 21:50:30 neilg Exp $
  69. */
  70. public class ValidationState implements ValidationContext {
  71. //
  72. // private data
  73. //
  74. private boolean fExtraChecking = true;
  75. private boolean fFacetChecking = true;
  76. private boolean fNormalize = true;
  77. private boolean fNamespaces = true;
  78. private EntityState fEntityState = null;
  79. private NamespaceContext fNamespaceContext = null;
  80. private SymbolTable fSymbolTable = null;
  81. //REVISIT: Should replace with a lighter structure.
  82. private final Hashtable fIdTable = new Hashtable();
  83. private final Hashtable fIdRefTable = new Hashtable();
  84. private final static Object fNullValue = new Object();
  85. //
  86. // public methods
  87. //
  88. public void setExtraChecking(boolean newValue) {
  89. fExtraChecking = newValue;
  90. }
  91. public void setFacetChecking(boolean newValue) {
  92. fFacetChecking = newValue;
  93. }
  94. public void setNormalizationRequired (boolean newValue) {
  95. fNormalize = newValue;
  96. }
  97. public void setUsingNamespaces (boolean newValue) {
  98. fNamespaces = newValue;
  99. }
  100. public void setEntityState(EntityState state) {
  101. fEntityState = state;
  102. }
  103. public void setNamespaceSupport(NamespaceContext namespace) {
  104. fNamespaceContext = namespace;
  105. }
  106. public void setSymbolTable(SymbolTable sTable) {
  107. fSymbolTable = sTable;
  108. }
  109. /**
  110. * return null if all IDREF values have a corresponding ID value;
  111. * otherwise return the first IDREF value without a matching ID value.
  112. */
  113. public String checkIDRefID () {
  114. Enumeration en = fIdRefTable.keys();
  115. String key;
  116. while (en.hasMoreElements()) {
  117. key = (String)en.nextElement();
  118. if (!fIdTable.containsKey(key)) {
  119. return key;
  120. }
  121. }
  122. return null;
  123. }
  124. public void reset () {
  125. fExtraChecking = true;
  126. fFacetChecking = true;
  127. fNamespaces = true;
  128. fIdTable.clear();
  129. fIdRefTable.clear();
  130. fEntityState = null;
  131. fNamespaceContext = null;
  132. fSymbolTable = null;
  133. }
  134. /**
  135. * The same validation state can be used to validate more than one (schema)
  136. * validation roots. Entity/Namespace/Symbol are shared, but each validation
  137. * root needs its own id/idref tables. So we need this method to reset only
  138. * the two tables.
  139. */
  140. public void resetIDTables() {
  141. fIdTable.clear();
  142. fIdRefTable.clear();
  143. }
  144. //
  145. // implementation of ValidationContext methods
  146. //
  147. // whether to do extra id/idref/entity checking
  148. public boolean needExtraChecking() {
  149. return fExtraChecking;
  150. }
  151. // whether to validate against facets
  152. public boolean needFacetChecking() {
  153. return fFacetChecking;
  154. }
  155. public boolean needToNormalize (){
  156. return fNormalize;
  157. }
  158. public boolean useNamespaces() {
  159. return fNamespaces;
  160. }
  161. // entity
  162. public boolean isEntityDeclared (String name) {
  163. if (fEntityState !=null) {
  164. return fEntityState.isEntityDeclared(getSymbol(name));
  165. }
  166. return false;
  167. }
  168. public boolean isEntityUnparsed (String name) {
  169. if (fEntityState !=null) {
  170. return fEntityState.isEntityUnparsed(getSymbol(name));
  171. }
  172. return false;
  173. }
  174. // id
  175. public boolean isIdDeclared(String name) {
  176. return fIdTable.containsKey(name);
  177. }
  178. public void addId(String name) {
  179. fIdTable.put(name, fNullValue);
  180. }
  181. // idref
  182. public void addIdRef(String name) {
  183. fIdRefTable.put(name, fNullValue);
  184. }
  185. // get symbols
  186. public String getSymbol (String symbol) {
  187. if (fSymbolTable != null)
  188. return fSymbolTable.addSymbol(symbol);
  189. // if there is no symbol table, we return java-internalized string,
  190. // because symbol table strings are also java-internalzied.
  191. // this guarantees that the returned string from this method can be
  192. // compared by reference with other symbol table string. -SG
  193. return symbol.intern();
  194. }
  195. // qname, notation
  196. public String getURI(String prefix) {
  197. if (fNamespaceContext !=null) {
  198. return fNamespaceContext.getURI(prefix);
  199. }
  200. return null;
  201. }
  202. }