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.XMLCharacterProperties; 65 import org.enhydra.apache.xerces.utils.regex.RegularExpression; 66 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols; 67 68 75 public abstract class AbstractStringValidator extends AbstractDatatypeValidator { 76 77 protected int fLength = 0; 78 protected int fMaxLength = Integer.MAX_VALUE; 79 protected int fMinLength = 0; 80 protected Vector fEnumeration = null; 81 82 public static final String FACET_SPECIAL_TOKEN = "specialToken"; 84 public static final String SPECIAL_TOKEN_NONE = "NONE"; 85 public static final String SPECIAL_TOKEN_NMTOKEN = "NMTOKEN"; 86 public static final String SPECIAL_TOKEN_NAME = "Name"; 87 public static final String SPECIAL_TOKEN_IDNAME = "ID(Name)"; 88 public static final String SPECIAL_TOKEN_IDREFNAME = "IDREF(Name)"; 89 public static final String SPECIAL_TOKEN_NCNAME = "NCName"; 90 public static final String SPECIAL_TOKEN_IDNCNAME = "ID(NCName)"; 91 public static final String SPECIAL_TOKEN_IDREFNCNAME = "IDREF(NCName)"; 92 public static final String SPECIAL_TOKEN_ENTITY = "ENTITY(NCName)"; 93 94 protected String fTokenType = SPECIAL_TOKEN_NONE; 96 public AbstractStringValidator () throws InvalidDatatypeFacetException{ 97 this( null, null, false ); 99 } 100 101 public AbstractStringValidator ( DatatypeValidator base, Hashtable facets, 102 boolean derivedByList ) throws InvalidDatatypeFacetException { 103 104 fBaseValidator = base; 106 107 if ( derivationList(derivedByList) ) 108 return; 109 if ( facets != null ) { 111 for ( Enumeration e = facets.keys(); e.hasMoreElements(); ) { 112 String key = (String ) e.nextElement(); 113 114 if ( key.equals(SchemaSymbols.ELT_LENGTH) ) { 115 fFacetsDefined |= DatatypeValidator.FACET_LENGTH; 116 String lengthValue = (String )facets.get(key); 117 try { 118 fLength = Integer.parseInt( lengthValue ); 119 } 120 catch ( NumberFormatException nfe ) { 121 throw new InvalidDatatypeFacetException("Length value '"+lengthValue+"' is invalid."); 122 } 123 if ( fLength < 0 ) 125 throw new InvalidDatatypeFacetException("Length value '"+lengthValue+"' must be a nonNegativeInteger."); 126 127 } 128 else if ( key.equals(SchemaSymbols.ELT_MINLENGTH) ) { 129 fFacetsDefined |= DatatypeValidator.FACET_MINLENGTH; 130 String minLengthValue = (String )facets.get(key); 131 try { 132 fMinLength = Integer.parseInt( minLengthValue ); 133 } 134 catch ( NumberFormatException nfe ) { 135 throw new InvalidDatatypeFacetException("minLength value '"+minLengthValue+"' is invalid."); 136 } 137 if ( fMinLength < 0 ) 139 throw new InvalidDatatypeFacetException("minLength value '"+minLengthValue+"' must be a nonNegativeInteger."); 140 141 } 142 else if ( key.equals(SchemaSymbols.ELT_MAXLENGTH) ) { 143 fFacetsDefined |= DatatypeValidator.FACET_MAXLENGTH; 144 String maxLengthValue = (String )facets.get(key); 145 try { 146 fMaxLength = Integer.parseInt( maxLengthValue ); 147 } 148 catch ( NumberFormatException nfe ) { 149 throw new InvalidDatatypeFacetException("maxLength value '"+maxLengthValue+"' is invalid."); 150 } 151 if ( fMaxLength < 0 ) 153 throw new InvalidDatatypeFacetException("maxLength value '"+maxLengthValue+"' must be a nonNegativeInteger."); 154 } 155 else if ( key.equals(SchemaSymbols.ELT_PATTERN) ) { 156 fFacetsDefined |= DatatypeValidator.FACET_PATTERN; 157 fPattern = (String )facets.get(key); 158 if ( fPattern != null ) 159 fRegex = new RegularExpression(fPattern, "X"); 160 } 161 else if ( key.equals(SchemaSymbols.ELT_ENUMERATION) ) { 162 fEnumeration = (Vector )facets.get(key); 163 fFacetsDefined |= DatatypeValidator.FACET_ENUMERATION; 164 } 165 else if ( key.equals(DatatypeValidator.FACET_FIXED) ) { fFlags = ((Short )facets.get(key)).shortValue(); 167 } 168 else if ( key == FACET_SPECIAL_TOKEN ) { setTokenType((String )facets.get(key)); 170 } 171 else { 172 assignAdditionalFacets(key, facets); 173 } 174 } 175 176 if ( base != null ) { 177 if ( (fFacetsDefined & DatatypeValidator.FACET_ENUMERATION) != 0 && 182 (fEnumeration != null) ) { 183 int i = 0; 184 try { 185 for ( ; i < fEnumeration.size(); i++ ) { 186 ((AbstractStringValidator)base).checkContent ((String )fEnumeration.elementAt(i), null, false); 187 } 188 } 189 catch ( Exception idve ) { 190 throw new InvalidDatatypeFacetException( "Value of enumeration = '" + fEnumeration.elementAt(i) + 191 "' must be from the value space of base."); 192 } 193 } 194 } 195 196 if ( ((fFacetsDefined & DatatypeValidator.FACET_LENGTH ) != 0 ) ) { 198 if ( ((fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH ) != 0 ) ) { 199 throw new InvalidDatatypeFacetException("It is an error for both length and maxLength to be members of facets." ); 200 } 201 else if ( ((fFacetsDefined & DatatypeValidator.FACET_MINLENGTH ) != 0 ) ) { 202 throw new InvalidDatatypeFacetException("It is an error for both length and minLength to be members of facets." ); 203 } 204 } 205 206 if ( ( (fFacetsDefined & ( DatatypeValidator.FACET_MINLENGTH | 208 DatatypeValidator.FACET_MAXLENGTH) ) != 0 ) ) { 209 if ( fMinLength > fMaxLength ) { 210 throw new InvalidDatatypeFacetException( "Value of minLength = '" + fMinLength + 211 "'must be <= the value of maxLength = '" + fMaxLength + "'."); 212 } 213 } 214 215 if ( base != null ) { 217 AbstractStringValidator strBase = (AbstractStringValidator)base; 218 219 if ( ((fFacetsDefined & DatatypeValidator.FACET_LENGTH ) != 0 ) ) { 221 if ( ((strBase.fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH ) != 0 ) ) { 222 throw new InvalidDatatypeFacetException("It is an error for both length and maxLength to be members of facets." ); 223 } 224 else if ( ((strBase.fFacetsDefined & DatatypeValidator.FACET_MINLENGTH ) != 0 ) ) { 225 throw new InvalidDatatypeFacetException("It is an error for both length and minLength to be members of facets." ); 226 } 227 else if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_LENGTH) != 0 ) { 228 if ( fLength != strBase.fLength ) 230 throw new InvalidDatatypeFacetException( "Value of length = '" + fLength + 231 "' must be = the value of base.length = '" + strBase.fLength + "'."); 232 } 233 } 234 235 if ( ((strBase.fFacetsDefined & DatatypeValidator.FACET_LENGTH ) != 0 ) ) { 237 if ( ((fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH ) != 0 ) ) { 238 throw new InvalidDatatypeFacetException("It is an error for both length and maxLength to be members of facets." ); 239 } 240 else if ( ((fFacetsDefined & DatatypeValidator.FACET_MINLENGTH ) != 0 ) ) { 241 throw new InvalidDatatypeFacetException("It is an error for both length and minLength to be members of facets." ); 242 } 243 } 244 245 if ( ((fFacetsDefined & DatatypeValidator.FACET_MINLENGTH ) != 0 ) ) { 247 if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH ) != 0 ) { 248 if ( fMinLength > strBase.fMaxLength ) { 249 throw new InvalidDatatypeFacetException( "Value of minLength = '" + fMinLength + 250 "'must be <= the value of maxLength = '" + fMaxLength + "'."); 251 } 252 } 253 else if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_MINLENGTH) != 0 ) { 254 if ( (strBase.fFlags & DatatypeValidator.FACET_MINLENGTH) != 0 && 255 fMinLength != strBase.fMinLength ) { 256 throw new InvalidDatatypeFacetException( "minLength value = '" + fMinLength + 257 "' must be equal to base.minLength value = '" + 258 strBase.fMinLength + "' with attribute {fixed} = true" ); 259 } 260 if ( fMinLength < strBase.fMinLength ) { 262 throw new InvalidDatatypeFacetException( "Value of minLength = '" + fMinLength + 263 "' must be >= the value of base.minLength = '" + strBase.fMinLength + "'."); 264 } 265 } 266 } 267 268 269 if ( ((strBase.fFacetsDefined & DatatypeValidator.FACET_MINLENGTH ) != 0 ) && 271 ((fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH ) != 0 ) ) { 272 if ( strBase.fMinLength > fMaxLength ) { 273 throw new InvalidDatatypeFacetException( "Value of minLength = '" + fMinLength + 274 "'must be <= the value of maxLength = '" + fMaxLength + "'."); 275 } 276 } 277 278 if ( (fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH) != 0 ) { 280 if ( (strBase.fFlags & DatatypeValidator.FACET_MAXLENGTH) != 0 && 281 fMaxLength != strBase.fMaxLength ) { 282 throw new InvalidDatatypeFacetException( "maxLength value = '" + fMaxLength + 283 "' must be equal to base.maxLength value = '" + 284 strBase.fMaxLength + "' with attribute {fixed} = true" ); 285 } 286 if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH) != 0 ) { 287 if ( fMaxLength > strBase.fMaxLength ) { 288 289 throw new InvalidDatatypeFacetException( "Value of maxLength = '" + fMaxLength + 290 "' must be <= the value of base.maxLength = '" + strBase.fMaxLength + "'."); 291 } 292 } 293 } 294 295 296 checkBaseFacetConstraints(); 297 298 if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_LENGTH) != 0 ) { 300 if ( (fFacetsDefined & DatatypeValidator.FACET_LENGTH) == 0 ) { 301 fFacetsDefined |= DatatypeValidator.FACET_LENGTH; 302 fLength = strBase.fLength; 303 } 304 } 305 if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_MINLENGTH) != 0 ) { 307 if ( (fFacetsDefined & DatatypeValidator.FACET_MINLENGTH) == 0 ) { 308 fFacetsDefined |= DatatypeValidator.FACET_MINLENGTH; 309 fMinLength = strBase.fMinLength; 310 } 311 } 312 if ( (strBase.fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH) != 0 ) { 314 if ( (fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH) == 0 ) { 315 fFacetsDefined |= DatatypeValidator.FACET_MAXLENGTH; 316 fMaxLength = strBase.fMaxLength; 317 } 318 } 319 if ( (fFacetsDefined & DatatypeValidator.FACET_ENUMERATION) == 0 && 321 (strBase.fFacetsDefined & DatatypeValidator.FACET_ENUMERATION) != 0 ) { 322 fFacetsDefined |= DatatypeValidator.FACET_ENUMERATION; 323 fEnumeration = strBase.fEnumeration; 324 } 325 inheritAdditionalFacets(); 326 327 fFlags |= strBase.fFlags; 329 } 330 } } 332 333 abstract protected void assignAdditionalFacets(String key, Hashtable facets) 338 throws InvalidDatatypeFacetException; 339 340 protected void inheritAdditionalFacets() { 344 } 345 346 protected void checkBaseFacetConstraints() throws InvalidDatatypeFacetException {} 351 352 protected boolean derivationList (boolean derivedByList) { 353 return derivedByList; 355 } 356 357 366 public Object validate(String content, Object state) throws InvalidDatatypeValueException { 367 checkContent( content, state, false ); 368 return null; 369 } 370 371 372 private void checkContent( String content, Object state, boolean asBase ) 373 throws InvalidDatatypeValueException { 374 if ( this.fBaseValidator != null ) { 376 ((AbstractStringValidator)fBaseValidator).checkContent(content, state, true); 378 } 379 380 if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) { 382 if ( fRegex == null || fRegex.matches( content) == false ) 383 throw new InvalidDatatypeValueException("Value '"+content+ 384 "' does not match regular expression facet '" + fPattern + "'." ); 385 } 386 387 if (fTokenType != SPECIAL_TOKEN_NONE) { 389 validateToken(fTokenType, content); 390 } 391 392 if ( asBase ) 395 return; 396 397 checkValueSpace (content); 398 int length = getLength(content); 399 400 if ( (fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH) != 0 ) { 401 if ( length > fMaxLength ) { 402 throw new InvalidDatatypeValueException("Value '"+content+ 403 "' with length '"+length+ 404 "' exceeds maximum length facet of '"+fMaxLength+"'."); 405 } 406 } 407 if ( (fFacetsDefined & DatatypeValidator.FACET_MINLENGTH) != 0 ) { 408 if ( length < fMinLength ) { 409 throw new InvalidDatatypeValueException("Value '"+content+ 410 "' with length '"+length+ 411 "' is less than minimum length facet of '"+fMinLength+"'." ); 412 } 413 } 414 415 if ( (fFacetsDefined & DatatypeValidator.FACET_LENGTH) != 0 ) { 416 if ( length != fLength ) { 417 throw new InvalidDatatypeValueException("Value '"+content+ 418 "' with length '"+length+ 419 "' is not equal to length facet '"+fLength+"'."); 420 } 421 } 422 423 if ( (fFacetsDefined & DatatypeValidator.FACET_ENUMERATION) != 0 && 424 (fEnumeration != null) ) { 425 if ( fEnumeration.contains( content ) == false ) 426 throw new InvalidDatatypeValueException("Value '"+content+"' must be one of "+fEnumeration); 427 } 428 } 429 protected int getLength( String content) { 430 return content.length(); 431 } 432 433 protected void checkValueSpace (String content) throws InvalidDatatypeValueException {} 434 435 441 public Object clone() throws CloneNotSupportedException { 442 throw new CloneNotSupportedException ("clone() is not supported in "+this.getClass().getName()); 443 } 444 445 public void setTokenType (String tokenType) { 446 fTokenType = tokenType; 447 } 448 449 450 protected static void validateToken(String tokenType, String content) throws InvalidDatatypeValueException { 451 int len; 452 if (content == null || (len = content.length()) == 0) { 453 throw new InvalidDatatypeValueException( 454 "The length of the content must be greater than 0"); 455 } 456 boolean seenErr = false; 457 if (tokenType == SPECIAL_TOKEN_NMTOKEN) { 458 seenErr = !XMLCharacterProperties.validNmtoken(content); 460 } 461 else if (tokenType == SPECIAL_TOKEN_NAME || 462 tokenType == SPECIAL_TOKEN_IDNAME || 463 tokenType == SPECIAL_TOKEN_IDREFNAME) { 464 seenErr = !XMLCharacterProperties.validName(content); 466 } else if (tokenType == SPECIAL_TOKEN_NCNAME || 467 tokenType == SPECIAL_TOKEN_IDNCNAME || 468 tokenType == SPECIAL_TOKEN_IDREFNCNAME || 469 tokenType == SPECIAL_TOKEN_ENTITY) { 470 seenErr = !XMLCharacterProperties.validNCName(content); 472 } 473 if (seenErr) { 474 throw new InvalidDatatypeValueException( 475 "Value '"+content+"' is not a valid " + tokenType); 476 } 477 } 478 } 479 | Popular Tags |