001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.validation;
019:
020: import org.apache.xerces.util.SymbolTable;
021: import org.apache.xerces.impl.dv.ValidationContext;
022:
023: import org.apache.xerces.xni.NamespaceContext;
024: import java.util.Hashtable;
025: import java.util.Enumeration;
026:
027: /**
028: * Implementation of ValidationContext inteface. Used to establish an
029: * environment for simple type validation.
030: *
031: * @xerces.internal
032: *
033: * @author Elena Litani, IBM
034: * @version $Id: ValidationState.java 446719 2006-09-15 20:32:39Z mrglavas $
035: */
036: public class ValidationState implements ValidationContext {
037:
038: //
039: // private data
040: //
041: private boolean fExtraChecking = true;
042: private boolean fFacetChecking = true;
043: private boolean fNormalize = true;
044: private boolean fNamespaces = true;
045:
046: private EntityState fEntityState = null;
047: private NamespaceContext fNamespaceContext = null;
048: private SymbolTable fSymbolTable = null;
049:
050: //REVISIT: Should replace with a lighter structure.
051: private final Hashtable fIdTable = new Hashtable();
052: private final Hashtable fIdRefTable = new Hashtable();
053: private final static Object fNullValue = new Object();
054:
055: //
056: // public methods
057: //
058: public void setExtraChecking(boolean newValue) {
059: fExtraChecking = newValue;
060: }
061:
062: public void setFacetChecking(boolean newValue) {
063: fFacetChecking = newValue;
064: }
065:
066: public void setNormalizationRequired(boolean newValue) {
067: fNormalize = newValue;
068: }
069:
070: public void setUsingNamespaces(boolean newValue) {
071: fNamespaces = newValue;
072: }
073:
074: public void setEntityState(EntityState state) {
075: fEntityState = state;
076: }
077:
078: public void setNamespaceSupport(NamespaceContext namespace) {
079: fNamespaceContext = namespace;
080: }
081:
082: public void setSymbolTable(SymbolTable sTable) {
083: fSymbolTable = sTable;
084: }
085:
086: /**
087: * return null if all IDREF values have a corresponding ID value;
088: * otherwise return the first IDREF value without a matching ID value.
089: */
090: public String checkIDRefID() {
091: Enumeration en = fIdRefTable.keys();
092:
093: String key;
094: while (en.hasMoreElements()) {
095: key = (String) en.nextElement();
096: if (!fIdTable.containsKey(key)) {
097: return key;
098: }
099: }
100: return null;
101: }
102:
103: public void reset() {
104: fExtraChecking = true;
105: fFacetChecking = true;
106: fNamespaces = true;
107: fIdTable.clear();
108: fIdRefTable.clear();
109: fEntityState = null;
110: fNamespaceContext = null;
111: fSymbolTable = null;
112: }
113:
114: /**
115: * The same validation state can be used to validate more than one (schema)
116: * validation roots. Entity/Namespace/Symbol are shared, but each validation
117: * root needs its own id/idref tables. So we need this method to reset only
118: * the two tables.
119: */
120: public void resetIDTables() {
121: fIdTable.clear();
122: fIdRefTable.clear();
123: }
124:
125: //
126: // implementation of ValidationContext methods
127: //
128:
129: // whether to do extra id/idref/entity checking
130: public boolean needExtraChecking() {
131: return fExtraChecking;
132: }
133:
134: // whether to validate against facets
135: public boolean needFacetChecking() {
136: return fFacetChecking;
137: }
138:
139: public boolean needToNormalize() {
140: return fNormalize;
141: }
142:
143: public boolean useNamespaces() {
144: return fNamespaces;
145: }
146:
147: // entity
148: public boolean isEntityDeclared(String name) {
149: if (fEntityState != null) {
150: return fEntityState.isEntityDeclared(getSymbol(name));
151: }
152: return false;
153: }
154:
155: public boolean isEntityUnparsed(String name) {
156: if (fEntityState != null) {
157: return fEntityState.isEntityUnparsed(getSymbol(name));
158: }
159: return false;
160: }
161:
162: // id
163: public boolean isIdDeclared(String name) {
164: return fIdTable.containsKey(name);
165: }
166:
167: public void addId(String name) {
168: fIdTable.put(name, fNullValue);
169: }
170:
171: // idref
172: public void addIdRef(String name) {
173: fIdRefTable.put(name, fNullValue);
174: }
175:
176: // get symbols
177:
178: public String getSymbol(String symbol) {
179: if (fSymbolTable != null)
180: return fSymbolTable.addSymbol(symbol);
181: // if there is no symbol table, we return java-internalized string,
182: // because symbol table strings are also java-internalzied.
183: // this guarantees that the returned string from this method can be
184: // compared by reference with other symbol table string. -SG
185: return symbol.intern();
186: }
187:
188: // qname, notation
189: public String getURI(String prefix) {
190: if (fNamespaceContext != null) {
191: return fNamespaceContext.getURI(prefix);
192: }
193: return null;
194: }
195:
196: }
|