KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > validation > ValidationState


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.validation;
18
19 import org.apache.xerces.util.SymbolTable;
20 import org.apache.xerces.impl.dv.ValidationContext;
21
22 import org.apache.xerces.xni.NamespaceContext;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 /**
27  * Implementation of ValidationContext inteface. Used to establish an
28  * environment for simple type validation.
29  *
30  * @xerces.internal
31  *
32  * @author Elena Litani, IBM
33  * @version $Id: ValidationState.java,v 1.15 2004/10/04 22:07:41 mrglavas Exp $
34  */

35 public class ValidationState implements ValidationContext {
36
37     //
38
// private data
39
//
40
private boolean fExtraChecking = true;
41     private boolean fFacetChecking = true;
42     private boolean fNormalize = true;
43     private boolean fNamespaces = true;
44
45     private EntityState fEntityState = null;
46     private NamespaceContext fNamespaceContext = null;
47     private SymbolTable fSymbolTable = null;
48
49     //REVISIT: Should replace with a lighter structure.
50
private final Hashtable JavaDoc fIdTable = new Hashtable JavaDoc();
51     private final Hashtable JavaDoc fIdRefTable = new Hashtable JavaDoc();
52     private final static Object JavaDoc fNullValue = new Object JavaDoc();
53
54     //
55
// public methods
56
//
57
public void setExtraChecking(boolean newValue) {
58         fExtraChecking = newValue;
59     }
60
61     public void setFacetChecking(boolean newValue) {
62         fFacetChecking = newValue;
63     }
64
65     public void setNormalizationRequired (boolean newValue) {
66           fNormalize = newValue;
67     }
68
69     public void setUsingNamespaces (boolean newValue) {
70           fNamespaces = newValue;
71     }
72
73     public void setEntityState(EntityState state) {
74         fEntityState = state;
75     }
76
77     public void setNamespaceSupport(NamespaceContext namespace) {
78         fNamespaceContext = namespace;
79     }
80
81     public void setSymbolTable(SymbolTable sTable) {
82         fSymbolTable = sTable;
83     }
84
85     /**
86      * return null if all IDREF values have a corresponding ID value;
87      * otherwise return the first IDREF value without a matching ID value.
88      */

89     public String JavaDoc checkIDRefID () {
90         Enumeration JavaDoc en = fIdRefTable.keys();
91
92         String JavaDoc key;
93         while (en.hasMoreElements()) {
94             key = (String JavaDoc)en.nextElement();
95             if (!fIdTable.containsKey(key)) {
96                   return key;
97             }
98         }
99         return null;
100     }
101
102     public void reset () {
103         fExtraChecking = true;
104         fFacetChecking = true;
105         fNamespaces = true;
106         fIdTable.clear();
107         fIdRefTable.clear();
108         fEntityState = null;
109         fNamespaceContext = null;
110         fSymbolTable = null;
111     }
112
113     /**
114      * The same validation state can be used to validate more than one (schema)
115      * validation roots. Entity/Namespace/Symbol are shared, but each validation
116      * root needs its own id/idref tables. So we need this method to reset only
117      * the two tables.
118      */

119     public void resetIDTables() {
120         fIdTable.clear();
121         fIdRefTable.clear();
122     }
123
124     //
125
// implementation of ValidationContext methods
126
//
127

128     // whether to do extra id/idref/entity checking
129
public boolean needExtraChecking() {
130         return fExtraChecking;
131     }
132
133     // whether to validate against facets
134
public boolean needFacetChecking() {
135         return fFacetChecking;
136     }
137
138     public boolean needToNormalize (){
139         return fNormalize;
140     }
141
142     public boolean useNamespaces() {
143         return fNamespaces;
144     }
145
146     // entity
147
public boolean isEntityDeclared (String JavaDoc name) {
148         if (fEntityState !=null) {
149             return fEntityState.isEntityDeclared(getSymbol(name));
150         }
151         return false;
152     }
153     public boolean isEntityUnparsed (String JavaDoc name) {
154         if (fEntityState !=null) {
155             return fEntityState.isEntityUnparsed(getSymbol(name));
156         }
157         return false;
158     }
159
160     // id
161
public boolean isIdDeclared(String JavaDoc name) {
162         return fIdTable.containsKey(name);
163     }
164     public void addId(String JavaDoc name) {
165         fIdTable.put(name, fNullValue);
166     }
167
168     // idref
169
public void addIdRef(String JavaDoc name) {
170         fIdRefTable.put(name, fNullValue);
171     }
172     // get symbols
173

174     public String JavaDoc getSymbol (String JavaDoc symbol) {
175         if (fSymbolTable != null)
176             return fSymbolTable.addSymbol(symbol);
177         // if there is no symbol table, we return java-internalized string,
178
// because symbol table strings are also java-internalzied.
179
// this guarantees that the returned string from this method can be
180
// compared by reference with other symbol table string. -SG
181
return symbol.intern();
182     }
183     // qname, notation
184
public String JavaDoc getURI(String JavaDoc prefix) {
185         if (fNamespaceContext !=null) {
186             return fNamespaceContext.getURI(prefix);
187         }
188         return null;
189     }
190
191 }
192
Popular Tags