Exemplo n.º 1
0
        public AttributeConstraint(XsdAttributeUse xsdAttributeUse, SimpleTypeRestriction simpleTypeConstraint)
        {
            Debug.Assert(simpleTypeConstraint != null);

            this.XsdAttributeUse      = xsdAttributeUse;
            this.SimpleTypeConstraint = simpleTypeConstraint;
        }
Exemplo n.º 2
0
        internal AttributeConstraint(XsdAttributeUse xsdAttributeUse, SimpleTypeRestriction simpleTypeConstraint)
        {
            Debug.Assert(simpleTypeConstraint != null);

            this.XsdAttributeUse = xsdAttributeUse;
            this.SimpleTypeConstraint = simpleTypeConstraint;
        }
Exemplo n.º 3
0
        internal AttributeConstraint(XsdAttributeUse xsdAttributeUse, SimpleTypeRestriction simpleTypeConstraint, FileFormatVersions supportedVersion)
        {
            Debug.Assert(simpleTypeConstraint != null);

            this.XsdAttributeUse = xsdAttributeUse;
            this.SimpleTypeConstraint = simpleTypeConstraint;
            this.SupportedVersion = supportedVersion;
        }
Exemplo n.º 4
0
        public AttributeConstraint(XsdAttributeUse xsdAttributeUse, SimpleTypeRestriction simpleTypeConstraint, FileFormatVersions supportedVersion)
        {
            Debug.Assert(simpleTypeConstraint != null);

            this.XsdAttributeUse      = xsdAttributeUse;
            this.SimpleTypeConstraint = simpleTypeConstraint;
            this.SupportedVersion     = supportedVersion;
        }
Exemplo n.º 5
0
            internal static void Validate(ValidationContext validationContext, SimpleTypeRestriction simpleTypeConstraint)
            {
                // first check whether there are invalid children under this OpenXmlLeafTextElement.
                EmptyComplexTypeValidator.Validate(validationContext);

                OpenXmlLeafTextElement element = (OpenXmlLeafTextElement)validationContext.Element;
                OpenXmlSimpleType      value   = element.InnerTextToValue(element.Text);
                string qname = element.XmlQualifiedName.ToString();

                SchemaTypeValidator.ValidateValue(validationContext, simpleTypeConstraint, value, qname, false);
            }
Exemplo n.º 6
0
            internal static void Validate(ValidationContext validationContext, SimpleTypeRestriction simpleTypeConstraint)
            {
                // first check whether there are invalid children under this OpenXmlLeafTextElement.
                EmptyComplexTypeValidator.Validate(validationContext);

                var element = (OpenXmlLeafTextElement)validationContext.Element;
                var value   = element.InnerTextToValue(element.Text);
                var qname   = element.XmlQualifiedName.ToString();
                var state   = new ElementProperty <OpenXmlSimpleType>(element.NamespaceId, element.LocalName, 0, element.ElementData.Info.Validators, new ElementPropertyAccessor <OpenXmlSimpleType>(_ => value, (_, __) => throw new NotImplementedException(), value.GetType()));

                SchemaTypeValidator.ValidateValue(validationContext, element.ElementData.Info.Validators, value, qname, state, false);
            }
        private static void SetFileFormat(SimpleTypeRestriction simpleType, FileFormatVersions fileFormat)
        {
            if (simpleType == null)
            {
                return;
            }

            simpleType.FileFormat = fileFormat;

            SetFileFormat((simpleType as UnionValueRestriction)?.UnionTypes, fileFormat);
            SetFileFormat((simpleType as RedirectedRestriction)?.TargetRestriction, fileFormat);
            SetFileFormat((simpleType as ListValueRestriction)?.ListItemType, fileFormat);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the SchemaTypeData.
 /// </summary>
 /// <param name="openxmlTypeId"></param>
 /// <param name="attributeConstraints"></param>
 /// <param name="simpleTypeConstraint"></param>
 /// <remarks>
 /// For simple type or complex type which is simple content (no particle children).
 /// </remarks>
 internal SchemaTypeData(int openxmlTypeId, AttributeConstraint[] attributeConstraints, SimpleTypeRestriction simpleTypeConstraint)
     : this(openxmlTypeId, attributeConstraints)
 {
     Debug.Assert(simpleTypeConstraint != null);
     this.SimpleTypeConstraint = simpleTypeConstraint;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Validate the value accoding to the simpleTypeConstraint.
        /// </summary>
        /// <param name="validationContext">The validation context.</param>
        /// <param name="simpleTypeConstraint">The constraint data of the simple type.</param>
        /// <param name="value">The value to be validated.</param>
        /// <param name="qname">The QualifiedName to be used in the error message.</param>
        /// <param name="isAttribute">Error message targeting attribute (or element).</param>
        internal static void ValidateValue(ValidationContext validationContext, SimpleTypeRestriction simpleTypeConstraint,
                                           OpenXmlSimpleType value, string qname, bool isAttribute)
        {
            var    element = validationContext.Element;
            string errorMessageResourceId;
            ValidationErrorInfo errorInfo;
            string subMessage;

            // special case, the type is different in Office2007 and Office2010.
            if (simpleTypeConstraint is RedirectedRestriction redirectRestriction)
            {
                var targetValue = redirectRestriction.ConvertValue(value);
                ValidateValue(validationContext, redirectRestriction.TargetRestriction, targetValue, qname, isAttribute);
                return;
            }

            if (isAttribute)
            {
                errorMessageResourceId = "Sch_AttributeValueDataTypeDetailed";
            }
            else
            {
                errorMessageResourceId = "Sch_ElementValueDataTypeDetailed";
            }

            // first, check whether the string is valid accoding the primitive type
            if (!simpleTypeConstraint.ValidateValueType(value))
            {
                if (simpleTypeConstraint.IsEnum)
                {
                    // enum is wrong
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, ValidationResources.Sch_EnumerationConstraintFailed);
                    errorInfo.SetDebugField(isAttribute? qname : null, "Sch_EnumerationConstraintFailed");
                }
                else if (simpleTypeConstraint.XsdType == XsdType.Union)
                {
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, isAttribute ? "Sch_AttributeUnionFailedEx" : "Sch_ElementUnionFailedEx", qname, value.InnerText);
                    errorInfo.SetDebugField(isAttribute? qname : null, null);
                }
                else if (string.IsNullOrEmpty(value.InnerText))
                {
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, isAttribute ? ValidationResources.Sch_EmptyAttributeValue : ValidationResources.Sch_EmptyElementValue);
                    errorInfo.SetDebugField(isAttribute? qname : null, isAttribute ? "Sch_EmptyAttributeValue" : "Sch_EmptyElementValue");
                }
                else if (simpleTypeConstraint.XsdType == XsdType.SpecialBoolean)
                {
                    // special boolean is ST_OnOff which is enum in the schema.
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, ValidationResources.Sch_EnumerationConstraintFailed);
                    errorInfo.SetDebugField(isAttribute ? qname : null, "Sch_EnumerationConstraintFailed");
                }
                else if (simpleTypeConstraint.IsList)
                {
                    // List
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, string.Empty);
                    errorInfo.SetDebugField(isAttribute? qname : null, null);
                }
                else
                {
                    subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_StringIsNotValidValue, value.InnerText, simpleTypeConstraint.ClrTypeName);
                    errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                    errorInfo.SetDebugField(isAttribute? qname : null, "Sch_StringIsNotValidValue");
                }
                validationContext.AddError(errorInfo);
            }
            else
            {
                bool validateConstraints = true;

                switch (simpleTypeConstraint.XsdType)
                {
                case XsdType.Enum:
                case XsdType.Boolean:
                case XsdType.DateTime:
                case XsdType.SpecialBoolean:
                    Debug.Assert(simpleTypeConstraint.Pattern == null);
                    Debug.Assert(simpleTypeConstraint.RestrictionField == RestrictionField.None);

                    // no other facets.
                    validateConstraints = false;
                    break;

                case XsdType.NonNegativeInteger:
                case XsdType.PositiveInteger:
                case XsdType.Byte:
                case XsdType.UnsignedByte:
                case XsdType.Short:
                case XsdType.UnsignedShort:
                case XsdType.Int:
                case XsdType.UnsignedInt:
                case XsdType.Long:
                case XsdType.UnsignedLong:
                case XsdType.Float:
                case XsdType.Double:
                case XsdType.Decimal:
                case XsdType.Integer:     // TODO: integer should be decimal, while in current the CodeGen generate Int32 instead.
                    Debug.Assert(simpleTypeConstraint.Pattern == null);
                    Debug.Assert((simpleTypeConstraint.RestrictionField & RestrictionField.LengthRestriction) == RestrictionField.None);
                    break;

                case XsdType.String:
                case XsdType.Token:
                case XsdType.HexBinary:
                case XsdType.Base64Binary:
                case XsdType.AnyURI:
                case XsdType.QName:
                case XsdType.ID:                            // no pattern defined for numeric type in Ecma376
                case XsdType.NCName:
                case XsdType.IDREF:
                case XsdType.Language:
                    Debug.Assert((simpleTypeConstraint.RestrictionField & RestrictionField.MinMaxRestriction) == RestrictionField.None);
                    break;

                case XsdType.List:
                    Debug.Assert(simpleTypeConstraint.Pattern == null);
                    Debug.Assert(simpleTypeConstraint.RestrictionField == RestrictionField.None);

                    // no other facets in current Ecma376.
                    validateConstraints = false;

                    break;

                case XsdType.Union:
                    Debug.Assert(simpleTypeConstraint.Pattern == null);
                    Debug.Assert(simpleTypeConstraint.RestrictionField == RestrictionField.None);

                    // no other facets.
                    validateConstraints = false;
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }

                if (validateConstraints)
                {
                    var errorRestriction = simpleTypeConstraint.Validate(value);
                    if (errorRestriction != RestrictionField.None)
                    {
                        if ((errorRestriction & RestrictionField.MinInclusive) == RestrictionField.MinInclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MinInclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MinInclusive));
                            errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MinInclusiveConstraintFailed");
                            validationContext.AddError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.MinExclusive) == RestrictionField.MinExclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MinExclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MinExclusive));
                            errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MinExclusiveConstraintFailed");
                            validationContext.AddError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.MaxInclusive) == RestrictionField.MaxInclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxInclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MaxInclusive));
                            errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MaxInclusiveConstraintFailed");
                            validationContext.AddError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.MaxExclusive) == RestrictionField.MaxExclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxExclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MaxExclusive));
                            errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MaxExclusiveConstraintFailed");
                            validationContext.AddError(errorInfo);
                        }
                        if ((errorRestriction & RestrictionField.Length) == RestrictionField.Length)
                        {
                            // length is not ok.
                            if (string.IsNullOrEmpty(value.InnerText))
                            {
                                errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, isAttribute ? ValidationResources.Sch_EmptyAttributeValue : ValidationResources.Sch_EmptyElementValue);
                                errorInfo.SetDebugField(isAttribute? qname : null, isAttribute ? "Sch_EmptyAttributeValue" : "Sch_EmptyElementValue");
                                validationContext.AddError(errorInfo);
                            }
                            else
                            {
                                subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_LengthConstraintFailed, simpleTypeConstraint.XsdType.GetXsdDataTypeName(), simpleTypeConstraint.GetRestrictionValue(RestrictionField.Length));
                                errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                                errorInfo.SetDebugField(isAttribute? qname : null, "Sch_LengthConstraintFailed");
                                validationContext.AddError(errorInfo);
                            }
                        }

                        if ((errorRestriction & RestrictionField.MinLength) == RestrictionField.MinLength)
                        {
                            // min length is not ok.
                            if (string.IsNullOrEmpty(value.InnerText))
                            {
                                errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, isAttribute ? ValidationResources.Sch_EmptyAttributeValue : ValidationResources.Sch_EmptyElementValue);
                                errorInfo.SetDebugField(isAttribute? qname : null, isAttribute ? "Sch_EmptyAttributeValue" : "Sch_EmptyElementValue");
                                validationContext.AddError(errorInfo);
                            }
                            else
                            {
                                subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MinLengthConstraintFailed, simpleTypeConstraint.XsdType.GetXsdDataTypeName(), simpleTypeConstraint.GetRestrictionValue(RestrictionField.MinLength));
                                errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                                errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MinLengthConstraintFailed");
                                validationContext.AddError(errorInfo);
                            }
                        }

                        if ((errorRestriction & RestrictionField.MaxLength) == RestrictionField.MaxLength)
                        {
                            // max length is not ok.
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxLengthConstraintFailed, simpleTypeConstraint.XsdType.GetXsdDataTypeName(), simpleTypeConstraint.GetRestrictionValue(RestrictionField.MaxLength));
                            errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MaxLengthConstraintFailed");
                            validationContext.AddError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.Pattern) == RestrictionField.Pattern)
                        {
                            // pattern is not ok.
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_PatternConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.Pattern));
                            errorInfo  = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_PatternConstraintFailed");
                            validationContext.AddError(errorInfo);
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
            internal static void Validate(ValidationContext validationContext, SimpleTypeRestriction simpleTypeConstraint)
            {
                // first check whether there are invalid children under this OpenXmlLeafTextElement.
                EmptyComplexTypeValidator.Validate(validationContext);

                OpenXmlLeafTextElement element = (OpenXmlLeafTextElement)validationContext.Element;
                OpenXmlSimpleType value = element.InnerTextToValue(element.Text);
                string qname = element.XmlQualifiedName.ToString();

                SchemaTypeValidator.ValidateValue(validationContext, simpleTypeConstraint, value, qname, false);
            }
Exemplo n.º 11
0
        internal static void ValidateValue(ValidationContext validationContext, SimpleTypeRestriction simpleTypeConstraint,
                                                                       OpenXmlSimpleType value, string qname, bool isAttribute)
        {
            var element = validationContext.Element;
            string errorMessageResourceId;
            ValidationErrorInfo errorInfo;
            string subMessage;

            // special case, the type is different in Office2007 and Office2010.
            var redirectRestriction = simpleTypeConstraint as RedirectedRestriction;
            if (redirectRestriction != null)
            {
                var targetValue = redirectRestriction.ConvertValue(value);
                ValidateValue(validationContext, redirectRestriction.TargetRestriction, targetValue, qname, isAttribute);
                return;
            }

            if (isAttribute)
            {
                errorMessageResourceId = "Sch_AttributeValueDataTypeDetailed";
            }
            else
            {
                errorMessageResourceId = "Sch_ElementValueDataTypeDetailed";
            }

            // first, check whether the string is valid accoding the primitive type
            if (!simpleTypeConstraint.ValidateValueType(value))
            {
                if (simpleTypeConstraint.IsEnum)
                {
                    // enum is wrong
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, ValidationResources.Sch_EnumerationConstraintFailed);
                    errorInfo.SetDebugField(isAttribute? qname : null, "Sch_EnumerationConstraintFailed");
                }
                else if (simpleTypeConstraint.XsdType == XsdType.Union)
                {
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, isAttribute ? "Sch_AttributeUnionFailedEx" : "Sch_ElementUnionFailedEx", qname, value.InnerText);
                    errorInfo.SetDebugField(isAttribute? qname : null, null);
                }
                else if (string.IsNullOrEmpty(value.InnerText))
                {
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, isAttribute ? ValidationResources.Sch_EmptyAttributeValue : ValidationResources.Sch_EmptyElementValue);
                    errorInfo.SetDebugField(isAttribute? qname : null, isAttribute ? "Sch_EmptyAttributeValue" : "Sch_EmptyElementValue");
                }
                else if (simpleTypeConstraint.XsdType == XsdType.SpecialBoolean)
                {
                    // special boolean is ST_OnOff which is enum in the schema.
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, ValidationResources.Sch_EnumerationConstraintFailed);
                    errorInfo.SetDebugField(isAttribute ? qname : null, "Sch_EnumerationConstraintFailed");
                }
                else if (simpleTypeConstraint.IsList)
                {
                    // List
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, string.Empty);
                    errorInfo.SetDebugField(isAttribute? qname : null, null);
                }
                else
                {
                    subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_StringIsNotValidValue, value.InnerText, simpleTypeConstraint.ClrTypeName);
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                    errorInfo.SetDebugField(isAttribute? qname : null, "Sch_StringIsNotValidValue");
                }
                validationContext.EmitError(errorInfo);
            }
            else
            {
                bool validateConstraints = true;

                switch (simpleTypeConstraint.XsdType)
                {
                    case XsdType.Enum:
                    case XsdType.Boolean:
                    case XsdType.DateTime:
                    case XsdType.SpecialBoolean:
                        Debug.Assert(simpleTypeConstraint.Pattern == null);
                        Debug.Assert(simpleTypeConstraint.RestrictionField == RestrictionField.None);

                        // no other facets.
                        validateConstraints = false;
                        break;

                    case XsdType.NonNegativeInteger:
                    case XsdType.PositiveInteger:
                    case XsdType.Byte:
                    case XsdType.UnsignedByte:
                    case XsdType.Short:
                    case XsdType.UnsignedShort:
                    case XsdType.Int:
                    case XsdType.UnsignedInt:
                    case XsdType.Long:
                    case XsdType.UnsignedLong:
                    case XsdType.Float:
                    case XsdType.Double:
                    case XsdType.Decimal:
                    case XsdType.Integer: // TODO: integer should be decimal, while in current the CodeGen generate Int32 instead.
                        Debug.Assert(simpleTypeConstraint.Pattern == null);
                        Debug.Assert((simpleTypeConstraint.RestrictionField & RestrictionField.LengthRestriction) == RestrictionField.None);
                        break;

                    case XsdType.String:
                    case XsdType.Token:
                    case XsdType.HexBinary:
                    case XsdType.Base64Binary:
                    case XsdType.AnyURI:
                    case XsdType.QName:
                    case XsdType.ID:                        // no pattern defined for numeric type in Ecma376
                    case XsdType.NCName:
                    case XsdType.IDREF:
                    case XsdType.Language:
                        Debug.Assert((simpleTypeConstraint.RestrictionField & RestrictionField.MinMaxRestriction) == RestrictionField.None);
                        break;

                    case XsdType.List:
                        Debug.Assert(simpleTypeConstraint.Pattern == null);
                        Debug.Assert(simpleTypeConstraint.RestrictionField == RestrictionField.None);

                        // no other facets in current Ecma376.
                        validateConstraints = false;

                        break;

                    case XsdType.Union:
                        Debug.Assert(simpleTypeConstraint.Pattern == null);
                        Debug.Assert(simpleTypeConstraint.RestrictionField == RestrictionField.None);

                        // no other facets.
                        validateConstraints = false;
                        break;

                    default:
                        Debug.Assert(false);
                        break;
                }

                if (validateConstraints)
                {
                    var errorRestriction = simpleTypeConstraint.Validate(value);
                    if (errorRestriction != RestrictionField.None)
                    {
                        if ((errorRestriction & RestrictionField.MinInclusive) == RestrictionField.MinInclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MinInclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MinInclusive));
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MinInclusiveConstraintFailed");
                            validationContext.EmitError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.MinExclusive) == RestrictionField.MinExclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MinExclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MinExclusive));
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MinExclusiveConstraintFailed");
                            validationContext.EmitError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.MaxInclusive) == RestrictionField.MaxInclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxInclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MaxInclusive));
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MaxInclusiveConstraintFailed");
                            validationContext.EmitError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.MaxExclusive) == RestrictionField.MaxExclusive)
                        {
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxExclusiveConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.MaxExclusive));
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MaxExclusiveConstraintFailed");
                            validationContext.EmitError(errorInfo);
                        }
                        if ((errorRestriction & RestrictionField.Length) == RestrictionField.Length)
                        {
                            // length is not ok.
                            if (string.IsNullOrEmpty(value.InnerText))
                            {
                                errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, isAttribute ? ValidationResources.Sch_EmptyAttributeValue : ValidationResources.Sch_EmptyElementValue);
                                errorInfo.SetDebugField(isAttribute? qname : null, isAttribute ? "Sch_EmptyAttributeValue" : "Sch_EmptyElementValue");
                                validationContext.EmitError(errorInfo);
                            }
                            else
                            {
                                subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_LengthConstraintFailed, simpleTypeConstraint.XsdType.GetXsdDataTypeName(), simpleTypeConstraint.GetRestrictionValue(RestrictionField.Length));
                                errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                                errorInfo.SetDebugField(isAttribute? qname : null, "Sch_LengthConstraintFailed");
                                validationContext.EmitError(errorInfo);
                            }
                        }

                        if ((errorRestriction & RestrictionField.MinLength) == RestrictionField.MinLength)
                        {
                            // min length is not ok.
                            if (string.IsNullOrEmpty(value.InnerText))
                            {
                                errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, isAttribute ? ValidationResources.Sch_EmptyAttributeValue : ValidationResources.Sch_EmptyElementValue);
                                errorInfo.SetDebugField(isAttribute? qname : null, isAttribute ? "Sch_EmptyAttributeValue" : "Sch_EmptyElementValue");
                                validationContext.EmitError(errorInfo);
                            }
                            else
                            {
                                subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MinLengthConstraintFailed, simpleTypeConstraint.XsdType.GetXsdDataTypeName(), simpleTypeConstraint.GetRestrictionValue(RestrictionField.MinLength));
                                errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                                errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MinLengthConstraintFailed");
                                validationContext.EmitError(errorInfo);
                            }
                        }

                        if ((errorRestriction & RestrictionField.MaxLength) == RestrictionField.MaxLength)
                        {
                            // max length is not ok.
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxLengthConstraintFailed, simpleTypeConstraint.XsdType.GetXsdDataTypeName(), simpleTypeConstraint.GetRestrictionValue(RestrictionField.MaxLength));
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_MaxLengthConstraintFailed");
                            validationContext.EmitError(errorInfo);
                        }

                        if ((errorRestriction & RestrictionField.Pattern) == RestrictionField.Pattern)
                        {
                            // pattern is not ok.
                            subMessage = string.Format(CultureInfo.CurrentUICulture, ValidationResources.Sch_PatternConstraintFailed, simpleTypeConstraint.GetRestrictionValue(RestrictionField.Pattern));
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, errorMessageResourceId, qname, value.InnerText, subMessage);
                            errorInfo.SetDebugField(isAttribute? qname : null, "Sch_PatternConstraintFailed");
                            validationContext.EmitError(errorInfo);
                        }
                    }
                }
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the SchemaTypeData.
 /// </summary>
 /// <param name="openxmlTypeId"></param>
 /// <param name="attributeConstraints"></param>
 /// <param name="simpleTypeConstraint"></param>
 /// <remarks>
 /// For simple type or complex type which is simple content (no particle children).
 /// </remarks>
 internal SchemaTypeData(int openxmlTypeId, AttributeConstraint[] attributeConstraints, SimpleTypeRestriction simpleTypeConstraint)
     : this(openxmlTypeId, attributeConstraints)
 {
     Debug.Assert(simpleTypeConstraint != null);
     this.SimpleTypeConstraint = simpleTypeConstraint;
 }