Bug 308500 - add hexBinary support to Schema Validation

This commit is contained in:
doronr%us.ibm.com 2005-11-07 16:35:49 +00:00
parent 6ee0b1a705
commit 736b830c90
4 changed files with 115 additions and 3 deletions

View File

@ -71,6 +71,7 @@
#include "prprf.h"
#include "prtime.h"
#include "plbase64.h"
#include <ctype.h>
#define NS_SCHEMA_1999_NAMESPACE "http://www.w3.org/1999/XMLSchema"
#define NS_SCHEMA_2001_NAMESPACE "http://www.w3.org/2001/XMLSchema"
@ -648,6 +649,14 @@ nsSchemaValidator::ValidateRestrictionSimpletype(const nsAString & aNodeValue,
break;
}
case nsISchemaBuiltinType::BUILTIN_TYPE_HEXBINARY: {
rv = ValidateBuiltinTypeHexBinary(aNodeValue, length, isLengthDefined,
minLength, isMinLengthDefined,
maxLength, isMaxLengthDefined,
&enumerationList, &isValid);
break;
}
case nsISchemaBuiltinType::BUILTIN_TYPE_QNAME: {
rv = ValidateBuiltinTypeQName(aNodeValue, length, isLengthDefined,
minLength, isMinLengthDefined,
@ -813,6 +822,11 @@ nsSchemaValidator::ValidateBuiltinType(const nsAString & aNodeValue,
break;
}
case nsISchemaBuiltinType::BUILTIN_TYPE_HEXBINARY: {
isValid = IsValidSchemaHexBinary(aNodeValue);
break;
}
case nsISchemaBuiltinType::BUILTIN_TYPE_QNAME: {
isValid = IsValidSchemaQName(aNodeValue);
break;
@ -2777,6 +2791,81 @@ nsSchemaValidator::IsValidSchemaQName(const nsAString & aString)
return isValid;
}
// http://www.w3.org/TR/xmlschema-2/#hexBinary
nsresult
nsSchemaValidator::ValidateBuiltinTypeHexBinary(const nsAString & aNodeValue,
PRUint32 aLength,
PRBool aLengthDefined,
PRUint32 aMinLength,
PRBool aMinLengthDefined,
PRUint32 aMaxLength,
PRBool aMaxLengthDefined,
nsStringArray *aEnumerationList,
PRBool *aResult)
{
PRBool isValid = IsValidSchemaHexBinary(aNodeValue);
if (isValid) {
// For hexBinary, length is measured in octets (8 bits) of binary data. So
// one byte of binary data is represented by two hex digits, so we
// divide by 2 to get the binary data length.
PRUint32 binaryDataLength = aNodeValue.Length() / 2;
if (aLengthDefined && (binaryDataLength != aLength)) {
isValid = PR_FALSE;
LOG((" Not valid: Not the right length (%d)", binaryDataLength));
}
if (isValid && aMinLengthDefined && (binaryDataLength < aMinLength)) {
isValid = PR_FALSE;
LOG((" Not valid: Length (%d) is too small", binaryDataLength));
}
if (isValid && aMaxLengthDefined && (binaryDataLength > aMaxLength)) {
isValid = PR_FALSE;
LOG((" Not valid: Length (%d) is too large", binaryDataLength));
}
if (isValid && aEnumerationList && (aEnumerationList->Count() > 0)) {
isValid = nsSchemaValidatorUtils::HandleEnumeration(aNodeValue, *aEnumerationList);
}
}
LOG((isValid ? (" Value is valid!") : (" Value is not valid!")));
*aResult = isValid;
return NS_OK;
}
PRBool
nsSchemaValidator::IsValidSchemaHexBinary(const nsAString & aString)
{
// hex binary length has to be even
PRUint32 length = aString.Length();
if (length % 2 != 0)
return PR_FALSE;
nsAString::const_iterator start, end;
aString.BeginReading(start);
aString.EndReading(end);
PRBool isValid = PR_TRUE;
// each character has to be in [0-9a-fA-F]
while (start != end) {
PRUnichar temp = *start++;
if (!isxdigit(temp)) {
isValid = PR_FALSE;
break;
}
}
return isValid;
}
#ifdef DEBUG
void
nsSchemaValidator::DumpBaseType(nsISchemaBuiltinType *aBuiltInType)

View File

@ -238,6 +238,19 @@ private:
PRBool *aResult);
PRBool IsValidSchemaBase64Binary(const nsAString & aString, char** aDecodedString);
nsresult ValidateBuiltinTypeHexBinary(const nsAString & aNodeValue,
PRUint32 aLength,
PRBool aLengthDefined,
PRUint32 aMinLength,
PRBool aMinLengthDefined,
PRUint32 aMaxLength,
PRBool aMaxLengthDefined,
nsStringArray *aEnumerationList,
PRBool *aResult);
PRBool IsValidSchemaHexBinary(const nsAString & aString);
nsresult ValidateBuiltinTypeQName(const nsAString & aNodeValue,
PRUint32 aLength,
PRBool aLengthDefined,

View File

@ -125,7 +125,7 @@
.getService(Components.interfaces.nsISchemaLoader);
try {
schema = schemaLoader.load("file:///home/doron/mozbuilds/trunk/mozilla/extensions/schema-validation/tests/schema.xsd");
myValidator.loadSchema(schema);
myValidator.loadSchema(schema);
} catch(e){alert(e)}
//schema collection
@ -440,16 +440,21 @@
validate("VGhpcyBpcyBhIHRlc3Q=", "base64-test-1", true);
validate(" VGhpcyBpcyBhIHRlQ= ", "base64-test-1", false);
validate("VGhpcyBpcyBhIHRlc3Q=", "base64-test-2", true);
validate("VGhpcyBpcyBhIHRlc3Qh", "base64-test-2", false);
validate("VGhpcyBpcyBhIHRlc3Q=", "base64-test-3", true);
validate("VGhpcyBpcyBhIHRlc3Qh", "base64-test-3", false);
validate("aGk=", "base64-test-3", false);
validate("aGkh", "base64-test-3", true);
validate("aGkgdGhlcmUh", "base64-test-3", true);
validate("0FB7", "hexbinary-test-1", true);
validate("0FB72", "hexbinary-test-1", false);
validate("023-", "hexbinary-test-1", false);
validate("0HB7", "hexbinary-test-1", false);
validate("58758", "better-us-zipcode", true);
validate("a758", "better-us-zipcode", false);

View File

@ -321,6 +321,11 @@
</restriction>
</simpleType>
<simpleType name="hexbinary-test-1">
<restriction base='hexBinary'>
</restriction>
</simpleType>
<simpleType name="qname-test-1">
<restriction base='QName'>
<maxLength value="13"/>