You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xerces.apache.org by "George T. Joseph" <gt...@peakin.com> on 2000/01/07 08:33:05 UTC
New functionality for StringValidator.java
I added functionality to org/apache/xerces/validators/datatype/StringValidator.java to let Schemas validate enumeration/literals and
maxLength in datatypes based on 'string'.
Example:
<datatype name='newtype'>
<basetype name='string'>
<enumeration>
<literal>AAA</literal>
<literal>BBB</literal>
</enumeration>
</datatype>
I realize that this is already obsolete syntax based on the 12/17 Schema draft but I needed it and thought others may. The diff is
below.
George
************************************************************
*** StringValidatorOrig.java Wed Jan 05 15:12:26 2000
--- StringValidator.java Fri Jan 07 00:30:44 2000
***************
*** 58,63 ****
--- 58,65 ----
package org.apache.xerces.validators.datatype;
import java.util.Hashtable;
+ import java.util.Vector;
+ import java.util.Enumeration;
import java.util.Locale;
/**
***************
*** 71,77 ****
public class StringValidator implements InternalDatatypeValidator {
Locale fLocale = null;
!
/**
* validate that a string is a W3C string type
*
--- 73,82 ----
public class StringValidator implements InternalDatatypeValidator {
Locale fLocale = null;
! Hashtable facetData = null;
! StringValidator fBaseValidator = null;
! int fMaxLength = 0;
! boolean fIsMaxLength = false;
/**
* validate that a string is a W3C string type
*
***************
*** 84,103 ****
* not a W3C string type
*/
! public void validate(String content) throws InvalidDatatypeValueException {
! // just say yes
}
public void validate(int contentIndex) throws InvalidDatatypeValueException {
}
public void setFacets(Hashtable facets) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
}
public void setFacets(int facets[]) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
}
public void setBasetype(DatatypeValidator base) {
}
/**
--- 89,151 ----
* not a W3C string type
*/
! public void validate(String content) throws InvalidDatatypeValueException
! {
!
! if (facetData == null)return;
!
! Enumeration eee = facetData.keys();
! while(eee.hasMoreElements())
! {
! String key = (String)eee.nextElement();
! if (key.equals(DatatypeValidator.ENUMERATION))
! {
! Vector value = (Vector)facetData.get(key);
! String vvv = value.toString();
! if (!value.contains(content)) throw new InvalidDatatypeValueException("Value '"+content+"' must be one of "+vvv);
! }
! else if (key.equals(DatatypeValidator.MAXLENGTH))
! {
! if (content.length() > fMaxLength && fIsMaxLength)
! throw new InvalidDatatypeValueException("Value '"+content+"' with length '"+content.length()+"' exceeds maximum length of
"+fMaxLength+".");
! }
! }
}
public void validate(int contentIndex) throws InvalidDatatypeValueException {
}
public void setFacets(Hashtable facets) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
+ facetData=facets;
+
+ Enumeration eee = facetData.keys();
+ while(eee.hasMoreElements())
+ {
+ String key = (String)eee.nextElement();
+ if (key.equals(DatatypeValidator.MAXLENGTH))
+ {
+ int vvv;
+ String value = (String)facetData.get(key);
+ try
+ {
+ vvv = Integer.parseInt(value);
+ }
+ catch(NumberFormatException nfe)
+ {
+ throw new IllegalFacetValueException("maxLength value '"+value+"' is invalid.");
+ }
+ fMaxLength = vvv;
+ fIsMaxLength = true;
+ }
+ }
}
public void setFacets(int facets[]) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
}
public void setBasetype(DatatypeValidator base) {
+ fBaseValidator = (StringValidator) base;
+
}
/**