1 57 58 package org.enhydra.apache.xerces.validators.datatype; 59 60 import java.util.Enumeration ; 61 import java.util.Hashtable ; 62 63 import org.enhydra.apache.xerces.utils.regex.RegularExpression; 64 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols; 65 74 75 public class BooleanDatatypeValidator extends AbstractDatatypeValidator { 76 77 private static final String fValueSpace[] = { "false", "true", "0", "1"}; 78 79 public BooleanDatatypeValidator () throws InvalidDatatypeFacetException { 80 this( null, null, false ); } 82 83 public BooleanDatatypeValidator ( DatatypeValidator base, Hashtable facets, 84 boolean derivedByList ) throws InvalidDatatypeFacetException { 85 fBaseValidator = base; 87 if ( derivedByList ) 89 return; 90 91 if ( facets != null ) { 93 for (Enumeration e = facets.keys(); e.hasMoreElements();) { 94 String key = (String ) e.nextElement(); 95 96 if (key.equals(SchemaSymbols.ELT_PATTERN)) { 97 fFacetsDefined |= DatatypeValidator.FACET_PATTERN; 98 fPattern = (String )facets.get(key); 99 if( fPattern != null ) 100 fRegex = new RegularExpression(fPattern, "X" ); 101 } else { 102 throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_BOOLEAN_FACET, 103 DatatypeMessageProvider.MSG_NONE, new Object [] { key })); 104 } 105 } 106 } } 108 109 116 117 public Object validate(String content, Object state) throws InvalidDatatypeValueException { 118 checkContent( content, false ); 119 return null; 120 } 121 122 123 130 public int compare( String content1, String content2){ 131 if (content1.equals(content2)) { 132 return 0; 133 } 134 Boolean b1 = Boolean.valueOf(content1); 135 Boolean b2 = Boolean.valueOf(content2); 136 137 return (b1.equals(b2))?0:1; 138 } 139 140 145 146 public Hashtable getFacets(){ 147 return null; 148 } 149 150 152 155 public Object clone() throws CloneNotSupportedException { 156 throw new CloneNotSupportedException ("clone() is not supported in "+this.getClass().getName()); 157 } 158 159 160 166 private void checkContent( String content, boolean asBase ) 167 throws InvalidDatatypeValueException { 168 if ( this.fBaseValidator != null ) { 170 ((BooleanDatatypeValidator)fBaseValidator).checkContent(content, true); 172 } 173 174 if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) { 176 if ( fRegex == null || fRegex.matches( content) == false ) 177 throw new InvalidDatatypeValueException("Value'"+content+ 178 "does not match regular expression facet" + fPattern ); 179 } 180 181 if (asBase) 184 return; 185 186 boolean isContentInDomain = false; 187 for ( int i = 0;i<fValueSpace.length;i++ ) { 188 if ( content.equals(fValueSpace[i] ) ) 189 isContentInDomain = true; 190 } 191 if (isContentInDomain == false) 192 throw new InvalidDatatypeValueException( 193 getErrorString(DatatypeMessageProvider.NOT_BOOLEAN, 194 DatatypeMessageProvider.MSG_NONE, 195 new Object [] { content})); 196 } 197 } 198 | Popular Tags |