View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001, 2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * 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.util.SymbolTable;
24  import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
25  
26  import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
27  import java.util.ArrayList;
28  import java.util.Locale;
29  
30  /**
31   * Implementation of ValidationContext inteface. Used to establish an
32   * environment for simple type validation.
33   *
34   * @xerces.internal
35   *
36   * @author Elena Litani, IBM
37   * @version $Id: ValidationState.java,v 1.7 2010-11-01 04:39:53 joehw Exp $
38   */
39  public class ValidationState implements ValidationContext {
40  
41      //
42      // private data
43      //
44      private boolean fExtraChecking              = true;
45      private boolean fFacetChecking              = true;
46      private boolean fNormalize                  = true;
47      private boolean fNamespaces                 = true;
48  
49      private EntityState fEntityState            = null;
50      private NamespaceContext fNamespaceContext  = null;
51      private SymbolTable fSymbolTable            = null;
52      private Locale fLocale                      = null;
53  
54      private ArrayList<String> fIdList;
55      private ArrayList<String> fIdRefList;
56  
57      //
58      // public methods
59      //
60      public void setExtraChecking(boolean newValue) {
61          fExtraChecking = newValue;
62      }
63  
64      public void setFacetChecking(boolean newValue) {
65          fFacetChecking = newValue;
66      }
67  
68      public void setNormalizationRequired (boolean newValue) {
69            fNormalize = newValue;
70      }
71  
72      public void setUsingNamespaces (boolean newValue) {
73            fNamespaces = newValue;
74      }
75  
76      public void setEntityState(EntityState state) {
77          fEntityState = state;
78      }
79  
80      public void setNamespaceSupport(NamespaceContext namespace) {
81          fNamespaceContext = namespace;
82      }
83  
84      public void setSymbolTable(SymbolTable sTable) {
85          fSymbolTable = sTable;
86      }
87  
88      /**
89       * return null if all IDREF values have a corresponding ID value;
90       * otherwise return the first IDREF value without a matching ID value.
91       */
92      public String checkIDRefID () {
93          if (fIdList == null) {
94              if (fIdRefList != null) {
95                  return fIdRefList.get(0);
96              }
97          }
98  
99          if (fIdRefList != null) {
100             String key;
101             for (int i = 0; i < fIdRefList.size(); i++) {
102                 key = fIdRefList.get(i);
103                 if (!fIdList.contains(key)) {
104                       return key;
105                 }
106             }
107         }
108         return null;
109     }
110 
111     public void reset () {
112         fExtraChecking = true;
113         fFacetChecking = true;
114         fNamespaces = true;
115         fIdList = null;
116         fIdRefList = null;
117         fEntityState = null;
118         fNamespaceContext = null;
119         fSymbolTable = null;
120     }
121 
122     /**
123      * The same validation state can be used to validate more than one (schema)
124      * validation roots. Entity/Namespace/Symbol are shared, but each validation
125      * root needs its own id/idref tables. So we need this method to reset only
126      * the two tables.
127      */
128     public void resetIDTables() {
129         fIdList = null;
130         fIdRefList = null;
131     }
132 
133     //
134     // implementation of ValidationContext methods
135     //
136 
137     // whether to do extra id/idref/entity checking
138     public boolean needExtraChecking() {
139         return fExtraChecking;
140     }
141 
142     // whether to validate against facets
143     public boolean needFacetChecking() {
144         return fFacetChecking;
145     }
146 
147     public boolean needToNormalize (){
148         return fNormalize;
149     }
150 
151     public boolean useNamespaces() {
152         return fNamespaces;
153     }
154 
155     // entity
156     public boolean isEntityDeclared (String name) {
157         if (fEntityState !=null) {
158             return fEntityState.isEntityDeclared(getSymbol(name));
159         }
160         return false;
161     }
162     public boolean isEntityUnparsed (String name) {
163         if (fEntityState !=null) {
164             return fEntityState.isEntityUnparsed(getSymbol(name));
165         }
166         return false;
167     }
168 
169     // id
170     public boolean isIdDeclared(String name) {
171         if (fIdList == null) return false;
172         return fIdList.contains(name);
173     }
174     public void addId(String name) {
175         if (fIdList == null) fIdList = new ArrayList();
176         fIdList.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 }