1 57 58 package org.enhydra.apache.xerces.validators.datatype; 59 60 import java.util.Enumeration ; 61 import java.util.Hashtable ; 62 import java.util.Vector ; 63 64 import org.enhydra.apache.xerces.utils.regex.RegularExpression; 65 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols; 66 67 68 69 75 public class UnionDatatypeValidator extends AbstractDatatypeValidator { 76 77 private Vector fBaseValidators = null; private int fValidatorsSize = 0; 79 private Vector fEnumeration = null; 80 private StringBuffer errorMsg = null; 81 82 83 public UnionDatatypeValidator () throws InvalidDatatypeFacetException{ 84 this( null, null, false ); 86 } 87 88 89 public UnionDatatypeValidator ( DatatypeValidator base, Hashtable facets, boolean derivedBy ) throws InvalidDatatypeFacetException { 90 fBaseValidator = base; 91 if ( facets != null ) { 93 for ( Enumeration e = facets.keys(); e.hasMoreElements(); ) { 94 String key = (String ) e.nextElement(); 95 if ( key.equals(SchemaSymbols.ELT_ENUMERATION) ) { 96 fFacetsDefined |= DatatypeValidator.FACET_ENUMERATION; 97 fEnumeration = (Vector )facets.get(key); 98 } 99 else if ( key.equals(SchemaSymbols.ELT_PATTERN) ) { 100 fFacetsDefined |= DatatypeValidator.FACET_PATTERN; 101 fPattern = (String )facets.get(key); 102 fRegex = new RegularExpression(fPattern, "X"); 103 104 105 } 106 else { 107 throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_UNION_FACET, 108 DatatypeMessageProvider.MSG_NONE, new Object [] { key })); 109 } 110 } 112 if ( base != null && 114 (fFacetsDefined & DatatypeValidator.FACET_ENUMERATION) != 0 && 115 (fEnumeration != null) ) { 116 int i = 0; 117 try { 118 for (; i < fEnumeration.size(); i++) { 119 base.validate ((String )fEnumeration.elementAt(i), null); 120 } 121 } catch ( Exception idve ){ 122 throw new InvalidDatatypeFacetException( "Value of enumeration = '" + fEnumeration.elementAt(i) + 123 "' must be from the value space of base."); 124 } 125 } 126 } 128 } 129 130 public UnionDatatypeValidator ( Vector base) { 131 132 if ( base !=null ) { 133 fValidatorsSize = base.size(); 134 fBaseValidators = new Vector (fValidatorsSize); 135 fBaseValidators = base; 136 137 } 138 139 } 140 141 142 143 152 public Object validate(String content, Object state) throws InvalidDatatypeValueException 153 { 154 if ( content == null && state != null ) { 155 this.fBaseValidator.validate( content, state ); } 158 else { 159 checkContentEnum( content, state, false , null ); 160 } 161 return(null); 162 } 163 164 165 170 public Hashtable getFacets(){ 171 return(null); 172 } 173 174 public int compare( String value1, String value2 ){ 175 if (fBaseValidator != null) { 176 return this.fBaseValidator.compare(value1, value2); 177 } 178 int index=-1; 180 DatatypeValidator currentDV; 181 while ( ++index < fValidatorsSize ) { 182 currentDV = (DatatypeValidator)this.fBaseValidators.elementAt(index); 183 if (currentDV.compare(value1, value2) == 0) { 184 return 0; 185 } 186 } 187 return -1; 189 } 190 191 194 public Object clone() throws CloneNotSupportedException { 195 UnionDatatypeValidator newObj = null; 196 try { 197 newObj = new UnionDatatypeValidator(); 198 newObj.fLocale = this.fLocale; 199 newObj.fBaseValidator = this.fBaseValidator; 200 newObj.fBaseValidators = (Vector )this.fBaseValidators.clone(); 201 newObj.fPattern = this.fPattern; 202 newObj.fEnumeration = this.fEnumeration; 203 newObj.fFacetsDefined = this.fFacetsDefined; 204 } 205 catch ( InvalidDatatypeFacetException ex ) { 206 ex.printStackTrace(); 207 } 208 return(newObj); 209 210 } 211 212 public Vector getBaseValidators() { 215 return fBaseValidators; 216 } 217 218 227 private boolean verifyEnum (Vector enumer){ 228 229 if ((fFacetsDefined & DatatypeValidator.FACET_ENUMERATION ) != 0) { 230 for (Enumeration e = enumer.elements() ; e.hasMoreElements() ;) { 231 if (fEnumeration.contains(e.nextElement()) == false) { 232 return false; 233 } 234 } 235 } 236 return true; 237 } 238 239 247 private void checkContentEnum( String content, Object state, boolean pattern, Vector enumeration ) throws InvalidDatatypeValueException 248 { 249 int index = -1; boolean valid=false; 255 DatatypeValidator currentDV = null; 256 if (fBaseValidator !=null) { if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) { 258 if ( fRegex == null || fRegex.matches( content) == false ) 259 throw new InvalidDatatypeValueException("Value '"+content+ 260 "' does not match regular expression facet '" + fPattern + "'." ); 261 pattern = true; 262 } 263 264 if (enumeration!=null) { 265 if (!verifyEnum(enumeration)) { 266 throw new InvalidDatatypeValueException("Enumeration '" +enumeration+"' for value '" +content+ 267 "' is based on enumeration '"+fEnumeration+"'"); 268 } 269 } 270 else { 271 enumeration = (fEnumeration!=null) ? fEnumeration : null; 272 } 273 ((UnionDatatypeValidator)this.fBaseValidator).checkContentEnum( content, state, pattern, enumeration ); 274 return; 275 } 276 while ( ++index < fValidatorsSize) { 278 currentDV = (DatatypeValidator)this.fBaseValidators.elementAt(index); 281 if ( valid ) break; 282 try { 283 if ( currentDV instanceof ListDatatypeValidator ) { 284 if ( pattern ) { 285 throw new InvalidDatatypeValueException("Facet \"Pattern\" can not be applied to a list datatype" ); 286 } 287 ((ListDatatypeValidator)currentDV).checkContentEnum( content, state, enumeration ); 288 } 289 else if ( currentDV instanceof UnionDatatypeValidator ) { 290 ((UnionDatatypeValidator)currentDV).checkContentEnum( content, state, pattern, enumeration ); 291 } 292 else { 293 if (enumeration!=null) { 294 if (currentDV instanceof AbstractNumericValidator) { 296 ((AbstractNumericValidator)currentDV).checkContentEnum(content, state, enumeration); 297 } 298 else { 299 if (enumeration.contains( content ) == false) { 300 throw new InvalidDatatypeValueException("Value '"+content+ "' must be one of "+ enumeration); 301 } 302 ((DatatypeValidator)currentDV).validate( content, state ); 303 } 304 } 305 else { 306 ((DatatypeValidator)currentDV).validate( content, state ); 307 } 308 } 309 valid=true; 310 311 } 312 catch ( InvalidDatatypeValueException e ) { 313 } 314 } 315 if ( !valid ) { 316 throw new InvalidDatatypeValueException( "Content '"+content+"' does not match any union types" ); 317 } 318 } 319 320 } 321 322 323 | Popular Tags |