private void ValidateRestrictedSimpleTypeValue (SimpleType st, ref XsDatatype dt, string normalized) { { string [] values; XsDatatype itemDatatype; SimpleType itemSimpleType; switch (st.DerivedBy) { case XmlSchemaDerivationMethod.List: SimpleTypeList listContent = st.Content as SimpleTypeList; values = normalized.Split (XmlChar.WhitespaceChars); itemDatatype = listContent.ValidatedListItemType as XsDatatype; itemSimpleType = listContent.ValidatedListItemType as SimpleType; for (int vi = 0; vi < values.Length; vi++) { string each = values [vi]; if (each == String.Empty) continue; // validate against ValidatedItemType if (itemDatatype != null) { try { itemDatatype.ParseValue (each, nameTable, nsResolver); } catch (Exception ex) { // FIXME: (wishlist) better exception handling ;-( HandleError ("List type value contains one or more invalid values.", ex); break; } } else AssessStringValid (itemSimpleType, itemSimpleType.Datatype, each); } break; case XmlSchemaDerivationMethod.Union: SimpleTypeUnion union = st.Content as SimpleTypeUnion; { string each = normalized; // validate against ValidatedItemType bool passed = false; foreach (object eachType in union.ValidatedTypes) { itemDatatype = eachType as XsDatatype; itemSimpleType = eachType as SimpleType; if (itemDatatype != null) { try { itemDatatype.ParseValue (each, nameTable, nsResolver); } catch (Exception) { // FIXME: (wishlist) better exception handling ;-( continue; } } else { try { AssessStringValid (itemSimpleType, itemSimpleType.Datatype, each); } catch (ValException) { continue; } } passed = true; break; } if (!passed) { HandleError ("Union type value contains one or more invalid values."); break; } } break; case XmlSchemaDerivationMethod.Restriction: SimpleTypeRest str = st.Content as SimpleTypeRest; // facet validation if (str != null) { /* Don't forget to validate against inherited type's facets * Could we simplify this by assuming that the basetype will also * be restriction? * */ // mmm, will check later. SimpleType baseType = st.BaseXmlSchemaType as SimpleType; if (baseType != null) { AssessStringValid(baseType, dt, normalized); } if (!str.ValidateValueWithFacets (normalized, nameTable, nsResolver)) { HandleError ("Specified value was invalid against the facets."); break; } } dt = st.Datatype; break; } } }
// 3.14.4 String Valid private object AssessStringValid (SimpleType st, XsDatatype dt, string value) { XsDatatype validatedDatatype = dt; object ret = null; if (st != null) { string normalized = validatedDatatype.Normalize (value); string [] values; XsDatatype itemDatatype; SimpleType itemSimpleType; switch (st.DerivedBy) { case XmlSchemaDerivationMethod.List: SimpleTypeList listContent = st.Content as SimpleTypeList; values = normalized.Split (XmlChar.WhitespaceChars); // LAMESPEC: Types of each element in // the returned list might be // inconsistent, so basically returning // value does not make sense without // explicit runtime type information // for base primitive type. object [] retValues = new object [values.Length]; itemDatatype = listContent.ValidatedListItemType as XsDatatype; itemSimpleType = listContent.ValidatedListItemType as SimpleType; for (int vi = 0; vi < values.Length; vi++) { string each = values [vi]; if (each == String.Empty) continue; // validate against ValidatedItemType if (itemDatatype != null) { try { retValues [vi] = itemDatatype.ParseValue (each, nameTable, nsResolver); } catch (Exception ex) { // It is inevitable and bad manner. HandleError ("List type value contains one or more invalid values.", ex); break; } } else AssessStringValid (itemSimpleType, itemSimpleType.Datatype, each); } ret = retValues; break; case XmlSchemaDerivationMethod.Union: SimpleTypeUnion union = st.Content as SimpleTypeUnion; { string each = normalized; // validate against ValidatedItemType bool passed = false; foreach (object eachType in union.ValidatedTypes) { itemDatatype = eachType as XsDatatype; itemSimpleType = eachType as SimpleType; if (itemDatatype != null) { try { ret = itemDatatype.ParseValue (each, nameTable, nsResolver); } catch (Exception) { // It is inevitable and bad manner. continue; } } else { try { ret = AssessStringValid (itemSimpleType, itemSimpleType.Datatype, each); } catch (ValException) { continue; } } passed = true; break; } if (!passed) { HandleError ("Union type value contains one or more invalid values."); break; } } break; case XmlSchemaDerivationMethod.Restriction: SimpleTypeRest str = st.Content as SimpleTypeRest; // facet validation if (str != null) { /* Don't forget to validate against inherited type's facets * Could we simplify this by assuming that the basetype will also * be restriction? * */ // mmm, will check later. SimpleType baseType = st.BaseXmlSchemaType as SimpleType; if (baseType != null) { ret = AssessStringValid (baseType, dt, value); } if (!str.ValidateValueWithFacets (value, nameTable, nsResolver)) { HandleError ("Specified value was invalid against the facets."); break; } } validatedDatatype = st.Datatype; break; } } if (validatedDatatype != null) { try { ret = validatedDatatype.ParseValue (value, nameTable, nsResolver); } catch (Exception ex) { // It is inevitable and bad manner. HandleError ("Invalidly typed data was specified", ex); } } return ret; }