You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by sa...@ca.ibm.com on 2001/03/16 23:14:50 UTC

Patch - fix for [Bug 995] New - Errors not reported by the parser for an invalid pattern match in an instance document

Hi all,

The reason is that the current code only supports facets "length" and
"enumeration" for datatype ID. According to the latest datatype
specification
"http://www.w3.org/XML/Group/xmlschema-current/datatypes/datatypes.html",
it should also support the following facets: minLength, maxLength, pattern,
and whiteSpace. The attached patch file adds these features, and the change
is shown below (package org.apache.xerces.validators.datatype).

Basically all code I added was copied from StringDatatypeValidator.java. I
think we should consider redefining the class hierarchy (according to the
schema data type hierarchy) of this package to reduce repeated code and
increase maintainability.

diff -w -r1.15 IDDatatypeValidator.java
67c67
<
---
> import org.apache.xerces.utils.regex.RegularExpression;
88a89,95
>     //REVISIT: adding support for more facets
>     private int        fMaxLength        = Integer.MAX_VALUE;
>     private int        fMinLength        = 0;
>     private String     fPattern          = null;
>     private short      fWhiteSpace       = DatatypeValidator.PRESERVE;
>     private RegularExpression fRegex     = null;
>
116a124,130
>                 } else if (key.equals(SchemaSymbols.ELT_MINLENGTH) ) {
>                     fFacetsDefined += DatatypeValidator.FACET_MINLENGTH;
>                     String minLengthValue = (String)facets.get(key);
>                     try {
>                         fMinLength     = Integer.parseInt( minLengthValue
);
>                     } catch (NumberFormatException nfe) {
>                         throw new InvalidDatatypeFacetException
("minLength value '"+minLengthValue+"' is invalid.");
117a132,162
>                     if ( fMinLength < 0 )
>                         throw new InvalidDatatypeFacetException
("minLength value '"+minLengthValue+"'  must be a nonNegativeInteger.");
>
>                 } else if (key.equals(SchemaSymbols.ELT_MAXLENGTH) ) {
>                     fFacetsDefined += DatatypeValidator.FACET_MAXLENGTH;
>                     String maxLengthValue = (String)facets.get(key);
>                     try {
>                         fMaxLength     = Integer.parseInt( maxLengthValue
);
>                     } catch (NumberFormatException nfe) {
>                         throw new InvalidDatatypeFacetException
("maxLength value '"+maxLengthValue+"' is invalid.");
>                     }
>                     if ( fMaxLength < 0 )
>                         throw new InvalidDatatypeFacetException
("maxLength value '"+maxLengthValue+"'  must be a nonNegativeInteger.");
>                 } else if (key.equals(SchemaSymbols.ELT_PATTERN)) {
>                     fFacetsDefined += DatatypeValidator.FACET_PATTERN;
>                     fPattern = (String)facets.get(key);
>                     fRegex   = new RegularExpression(fPattern, "X");
>                 } else if (key.equals(SchemaSymbols.ELT_WHITESPACE)) {
>                     fFacetsDefined += DatatypeValidator.FACET_WHITESPACE;
>                     String ws = (String)facets.get(key);
>                     if (ws.equals("replace")) {
>                         fWhiteSpace = DatatypeValidator.REPLACE;
>                     }
>                     else if (ws.equals("collapse")) {
>                         fWhiteSpace = DatatypeValidator.COLLAPSE;
>                     }
>                     else {
>                         fWhiteSpace = DatatypeValidator.PRESERVE;
>                     }
>                 } else {
>                     throw new InvalidDatatypeFacetException("invalid
facet tag : " + key);
121c166,173
<
---
>             if (((fFacetsDefined & DatatypeValidator.FACET_LENGTH ) != 0
) ) {
>                 if (((fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH
) != 0 ) ) {
>                     throw new InvalidDatatypeFacetException(
>                                                             "It is an
error for both length and maxLength to be members of facets." );
>                 } else if (((fFacetsDefined &
DatatypeValidator.FACET_MINLENGTH ) != 0 ) ) {
>                     throw new InvalidDatatypeFacetException(
>                                                             "It is an
error for both length and minLength to be members of facets." );
>                 }
123a176,184
>             if ( ( (fFacetsDefined & ( DatatypeValidator.FACET_MINLENGTH
|
>
DatatypeValidator.FACET_MAXLENGTH) ) != 0 ) ) {
>                 if ( fMinLength > fMaxLength ) {
>                     throw new InvalidDatatypeFacetException( "Value of
minLength = '" + fMinLength +
>                                                                 "'must be
less than the value of maxLength = '" + fMaxLength + "'.");
>                 }
>             }
>         }
>     }
124a186,191
>     /**
>      * return value of whiteSpace facet
>      */
>     public short getWSFacet(){
>         return fWhiteSpace;
>     }
142,143d208
<         StateMessageDatatype message;
<
144a210,224
>           if ( (fFacetsDefined & DatatypeValidator.FACET_MAXLENGTH) != 0
) {
>             if ( content.length() > fMaxLength ) {
>                 throw new InvalidDatatypeValueException("Value '"
+content+
>                                                         "' with length '"
+content.length()+
>                                                         "' exceeds
maximum length facet of '"+fMaxLength+"'.");
>             }
>           }
>           if ( (fFacetsDefined & DatatypeValidator.FACET_MINLENGTH) != 0
) {
>             if ( content.length() < fMinLength ) {
>                 throw new InvalidDatatypeValueException("Value '"
+content+
>                                                         "' with length '"
+content.length()+
>                                                         "' is less than
minimum length facet of '"+fMinLength+"'." );
>             }
>           }
>
151a232
>
153c234
<               if ( fEnumeration.contains( content ) == false ) {
---
>             if ( fEnumeration.contains( content ) == false )
156,157c237,243
<             }
<             //REVISIT: should we propagate content to fBaseValidator?
---
>
>           if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 )
{
>             //RegularExpression regex = new RegularExpression(fPattern );
>             if ( fRegex == null || fRegex.matches( content) == false )
>                 throw new InvalidDatatypeValueException("Value '"
+content+
>                                                         "' does not match
regular expression facet '" + fPattern + "'." );
>           } //REVISIT: should we propagate content to fBaseValidator?
160a247,248
>         StateMessageDatatype message;
>

Cheers,
Sandy Gao
Software Developer, IBM Canada
(1-416) 448-3255
sandygao@ca.ibm.com

(See attached file: diff.txt)