1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.validation;
  22 
  23 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  24 import com.sun.org.apache.xerces.internal.util.SymbolTable;
  25 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
  26 import java.util.ArrayList;
  27 import java.util.HashSet;
  28 import java.util.Iterator;
  29 import java.util.List;
  30 import java.util.Locale;
  31 
  32 /**
  33  * Implementation of ValidationContext inteface. Used to establish an
  34  * environment for simple type validation.
  35  *
  36  * @xerces.internal
  37  *
  38  * @author Elena Litani, IBM
  39  * @LastModified: Oct 2017
  40  */
  41 public class ValidationState implements ValidationContext {
  42 
  43     //
  44     // private data
  45     //
  46     private boolean fExtraChecking              = true;
  47     private boolean fFacetChecking              = true;
  48     private boolean fNormalize                  = true;
  49     private boolean fNamespaces                 = true;
  50 
  51     private EntityState fEntityState            = null;
  52     private NamespaceContext fNamespaceContext  = null;
  53     private SymbolTable fSymbolTable            = null;
  54     private Locale fLocale                      = null;
  55 
  56     private HashSet<String> fIds;
  57     private List<String> fIdRefList;
  58 
  59     //
  60     // public methods
  61     //
  62     public void setExtraChecking(boolean newValue) {
  63         fExtraChecking = newValue;
  64     }
  65 
  66     public void setFacetChecking(boolean newValue) {
  67         fFacetChecking = newValue;
  68     }
  69 
  70     public void setNormalizationRequired (boolean newValue) {
  71           fNormalize = newValue;
  72     }
  73 
  74     public void setUsingNamespaces (boolean newValue) {
  75           fNamespaces = newValue;
  76     }
  77 
  78     public void setEntityState(EntityState state) {
  79         fEntityState = state;
  80     }
  81 
  82     public void setNamespaceSupport(NamespaceContext namespace) {
  83         fNamespaceContext = namespace;
  84     }
  85 
  86     public void setSymbolTable(SymbolTable sTable) {
  87         fSymbolTable = sTable;
  88     }
  89 
  90     /**
  91      * return null if all IDREF values have a corresponding ID value;
  92      * otherwise return an iterator for all the IDREF values without
  93      * a matching ID value.
  94      */
  95     public Iterator<String> checkIDRefID () {
  96         HashSet<String> missingIDs = null;
  97         if (fIdRefList != null) {
  98             String key;
  99             for (int i = 0; i < fIdRefList.size(); i++) {
 100                 key = fIdRefList.get(i);
 101                 if (fIds == null || !fIds.contains(key)) {
 102                     if (missingIDs == null) {
 103                         missingIDs = new HashSet<>();
 104                     }
 105                     missingIDs.add(key);
 106                 }
 107             }
 108         }
 109         return (missingIDs != null) ? missingIDs.iterator() : null;
 110     }
 111 
 112     public void reset () {
 113         fExtraChecking = true;
 114         fFacetChecking = true;
 115         fNamespaces = true;
 116         fIds = null;
 117         fIdRefList = null;
 118         fEntityState = null;
 119         fNamespaceContext = null;
 120         fSymbolTable = null;
 121     }
 122 
 123     /**
 124      * The same validation state can be used to validate more than one (schema)
 125      * validation roots. Entity/Namespace/Symbol are shared, but each validation
 126      * root needs its own id/idref tables. So we need this method to reset only
 127      * the two tables.
 128      */
 129     public void resetIDTables() {
 130         fIds = null;
 131         fIdRefList = null;
 132     }
 133 
 134     //
 135     // implementation of ValidationContext methods
 136     //
 137 
 138     // whether to do extra id/idref/entity checking
 139     public boolean needExtraChecking() {
 140         return fExtraChecking;
 141     }
 142 
 143     // whether to validate against facets
 144     public boolean needFacetChecking() {
 145         return fFacetChecking;
 146     }
 147 
 148     public boolean needToNormalize (){
 149         return fNormalize;
 150     }
 151 
 152     public boolean useNamespaces() {
 153         return fNamespaces;
 154     }
 155 
 156     // entity
 157     public boolean isEntityDeclared (String name) {
 158         if (fEntityState !=null) {
 159             return fEntityState.isEntityDeclared(getSymbol(name));
 160         }
 161         return false;
 162     }
 163     public boolean isEntityUnparsed (String name) {
 164         if (fEntityState !=null) {
 165             return fEntityState.isEntityUnparsed(getSymbol(name));
 166         }
 167         return false;
 168     }
 169 
 170     // id
 171     public boolean isIdDeclared(String name) {
 172         return fIds != null && fIds.contains(name);
 173     }
 174     public void addId(String name) {
 175         if (fIds == null) fIds = new HashSet<>();
 176         fIds.add(name);
 177     }
 178 
 179     // idref
 180     public void addIdRef(String name) {
 181         if (fIdRefList == null) fIdRefList = new ArrayList<>();
 182         fIdRefList.add(name);
 183     }
 184     // get symbols
 185 
 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 
 203     // Locale
 204 
 205     public void setLocale(Locale locale) {
 206         fLocale = locale;
 207     }
 208 
 209     public Locale getLocale() {
 210         return fLocale;
 211     }
 212 }