Exemplo n.º 1
0
        private static void ValidateValue(ValidationContext validationContext, ReadOnlyArray <IValidator> validators, OpenXmlSimpleType value, AttributeMetadata state, bool isAttribute)
        {
            var errors = validationContext.Errors.Count;

            using (validationContext.Stack.Push(value, state, isAttribute))
            {
                foreach (var validator in validators)
                {
                    validator.Validate(validationContext);

                    // Break early if validation has hit an error
                    if (errors != validationContext.Errors.Count)
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected static bool AttributeValueEquals(OpenXmlSimpleType type, string value, bool ignoreCase)
        {
            HexBinaryValue hexValue = type as HexBinaryValue;
            if (hexValue != null)
            {
                if (!hexValue.HasValue)
                {
                    return true;
                }

                return Convert.ToInt64(hexValue.Value, 16) == Convert.ToInt64(value, 16);
            }

            BooleanValue boolValue = type as BooleanValue;
            if (boolValue != null)
            {
                if (!boolValue.HasValue)
                {
                    return false;
                }

                if (CompareBooleanValue(boolValue.Value, value))
                {
                    return true;
                }
            }

            OnOffValue onOffValue = type as OnOffValue;
            if(onOffValue != null)
            {
                if (!onOffValue.HasValue)
                {
                    return false;
                }

                if (CompareBooleanValue(onOffValue.Value, value))
                {
                    return true;
                }
            }

            TrueFalseValue trueFalseValue = type as TrueFalseValue;
            if(trueFalseValue != null)
            {
                if (!trueFalseValue.HasValue)
                {
                    return false;
                }

                if (CompareBooleanValue(trueFalseValue.Value, value))
                {
                    return true;
                }
            }

            TrueFalseBlankValue trueFalseBlankValue = type as TrueFalseBlankValue;
            if (trueFalseBlankValue != null)
            {
                if (!trueFalseBlankValue.HasValue)
                {
                    return false;
                }

                if (CompareBooleanValue(trueFalseBlankValue.Value, value))
                {
                    return true;
                }
            }

            if (ignoreCase)
            {
                return string.Equals(value, type.InnerText, StringComparison.OrdinalIgnoreCase);
            }
            else
            {
                return string.Equals(value, type.InnerText, StringComparison.Ordinal);
            }
        }
Exemplo n.º 3
0
 private static void Write(TextWriter writer, string propertyName, OpenXmlSimpleType propertyValue)
 {
     if (propertyValue == null || !propertyValue.HasValue)
         return;
     Write(writer, propertyName, propertyValue.InnerText);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Validate the value to all the constraints.
        /// </summary>
        /// <param name="attributeValue">The value to be validated.</param>
        /// <returns>An bit flag, a bit is set on if the corresponding constraint is failed.</returns>
        public RestrictionField Validate(OpenXmlSimpleType attributeValue)
        {
            RestrictionField resultFlag = RestrictionField.None;

            if ((RestrictionField & RestrictionField.Pattern) == RestrictionField.Pattern)
            {
                if (!IsPatternValid(attributeValue))
                {
                    resultFlag |= RestrictionField.Pattern;
                }
            }

            if ((RestrictionField & RestrictionField.Length) == RestrictionField.Length)
            {
                if (!IsLengthValid(attributeValue))
                {
                    resultFlag |= RestrictionField.Length;
                }
            }

            if ((RestrictionField & RestrictionField.MinLength) == RestrictionField.MinLength)
            {
                if (!IsMinLengthValid(attributeValue))
                {
                    resultFlag |= RestrictionField.MinLength;
                }
            }

            if ((RestrictionField & RestrictionField.MaxLength) == RestrictionField.MaxLength)
            {
                if (!IsMaxLengthValid(attributeValue))
                {
                    resultFlag |= RestrictionField.MaxLength;
                }
            }

            if ((RestrictionField & RestrictionField.MinInclusive) == RestrictionField.MinInclusive)
            {
                if (!IsMinInclusiveValid(attributeValue))
                {
                    resultFlag |= RestrictionField.MinInclusive;
                }
            }

            if ((RestrictionField & RestrictionField.MinExclusive) == RestrictionField.MinExclusive)
            {
                if (!IsMinExclusiveValid(attributeValue))
                {
                    resultFlag |= RestrictionField.MinExclusive;
                }
            }

            if ((RestrictionField & RestrictionField.MaxInclusive) == RestrictionField.MaxInclusive)
            {
                if (!IsMaxInclusiveValid(attributeValue))
                {
                    resultFlag |= RestrictionField.MaxInclusive;
                }
            }

            if ((RestrictionField & RestrictionField.MaxExclusive) == RestrictionField.MaxExclusive)
            {
                if (!IsMaxExclusiveValid(attributeValue))
                {
                    resultFlag |= RestrictionField.MaxExclusive;
                }
            }

            return(resultFlag);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Validate whether the "length" constraint is ok.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns>True if the length of the value is same as defined.</returns>
 /// <remarks>
 /// A value in a ·value space· is facet-valid with respect to ·length·, determined as follows:
 /// 1 if the {variety} is ·atomic· then
 ///   1.1 if {primitive type definition} is string or anyURI, then the length of the value, as measured in characters ·must· be equal to {value};
 ///   1.2 if {primitive type definition} is hexBinary or base64Binary, then the length of the value, as measured in octets of the binary data, ·must· be equal to {value};
 ///   1.3 if {primitive type definition} is QName or NOTATION, then any {value} is facet-valid.
 /// 2 if the {variety} is ·list·, then the length of the value, as measured in list items, ·must· be equal to {value}
 /// </remarks>
 public virtual bool IsMinLengthValid(OpenXmlSimpleType attributeValue)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 6
0
        protected static bool AttributeValueEquals(OpenXmlSimpleType type, string value, bool ignoreCase)
        {
            if (type is HexBinaryValue hexValue)
            {
                if (!hexValue.HasValue)
                {
                    return(true);
                }

                return(Convert.ToInt64(hexValue.Value, 16) == Convert.ToInt64(value, 16));
            }

            if (type is BooleanValue boolValue)
            {
                if (!boolValue.HasValue)
                {
                    return(false);
                }

                if (CompareBooleanValue(boolValue.Value, value))
                {
                    return(true);
                }
            }

            if (type is OnOffValue onOffValue)
            {
                if (!onOffValue.HasValue)
                {
                    return(false);
                }

                if (CompareBooleanValue(onOffValue.Value, value))
                {
                    return(true);
                }
            }

            if (type is TrueFalseValue trueFalseValue)
            {
                if (!trueFalseValue.HasValue)
                {
                    return(false);
                }

                if (CompareBooleanValue(trueFalseValue.Value, value))
                {
                    return(true);
                }
            }

            if (type is TrueFalseBlankValue trueFalseBlankValue)
            {
                if (!trueFalseBlankValue.HasValue)
                {
                    return(false);
                }

                if (CompareBooleanValue(trueFalseBlankValue.Value, value))
                {
                    return(true);
                }
            }

            if (ignoreCase)
            {
                return(string.Equals(value, type.InnerText, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                return(string.Equals(value, type.InnerText, StringComparison.Ordinal));
            }
        }
Exemplo n.º 7
0
        internal static OpenXmlSimpleType CreateTargetValueObject(RedirectedRestriction redirectedRestriction)
        {
            OpenXmlSimpleType simpleValue = null;

            switch (redirectedRestriction.AttributeId)
            {
            // property name: Width
            case 2389:
                simpleValue = new StringValue();
                break;

            // property name: VerticalSpace
            case 2391:
                simpleValue = new StringValue();
                break;

            // property name: HorizontalSpace
            case 2392:
                simpleValue = new StringValue();
                break;

            // property name: X
            case 2396:
                simpleValue = new StringValue();
                break;

            // property name: Y
            case 2398:
                simpleValue = new StringValue();
                break;

            // property name: Before
            case 2411:
                simpleValue = new StringValue();
                break;

            // property name: After
            case 2414:
                simpleValue = new StringValue();
                break;

            // property name: Line
            case 2417:
                simpleValue = new StringValue();
                break;

            // property name: Left
            case 2419:
                simpleValue = new StringValue();
                break;

            // property name: Start
            case 2420:
                simpleValue = new StringValue();
                break;

            // property name: Right
            case 2423:
                simpleValue = new StringValue();
                break;

            // property name: End
            case 2424:
                simpleValue = new StringValue();
                break;

            // property name: Hanging
            case 2427:
                simpleValue = new StringValue();
                break;

            // property name: FirstLine
            case 2429:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2475:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2476:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2591:
                simpleValue = new StringValue();
                break;

            // property name: Distance
            case 2645:
                simpleValue = new StringValue();
                break;

            // property name: Space
            case 2652:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2682:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2688:
                simpleValue = new StringValue();
                break;

            // property name: Space
            case 2689:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2706:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2734:
                simpleValue = new StringValue();
                break;

            // property name: LegacySpace
            case 2740:
                simpleValue = new StringValue();
                break;

            // property name: LegacyIndent
            case 2741:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2771:
                simpleValue = new StringValue();
                break;

            // property name: Percent
            case 2849:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2874:
                simpleValue = new EnumValue <DocumentFormat.OpenXml.Wordprocessing.StylePaneSortMethodsValues>();
                break;

            // property name: FontSize
            case 2926:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2988:
                simpleValue = new StringValue();
                break;

            // property name: Delay
            case 2993:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3073:
                simpleValue = new UInt32Value();
                break;

            // property name: AutoAdvance
            case 3081:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3082:
                simpleValue = new UInt32Value();
                break;

            // property name: ShapeId
            case 3086:
                simpleValue = new UInt32Value();
                break;

            // property name: ShapeId
            case 3091:
                simpleValue = new UInt32Value();
                break;

            // property name: AdvanceAfterTime
            case 3152:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3155:
                simpleValue = new UInt32Value();
                break;

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

            return(simpleValue);
        }
Exemplo n.º 8
0
 /// <inheritdoc />
 public override bool ValidateValueType(OpenXmlSimpleType attributeValue)
 {
     return(IsValidQName(attributeValue.InnerText));
 }
Exemplo n.º 9
0
 public ValidatorContext ToContext(OpenXmlSimpleType simple, ElementProperty <OpenXmlSimpleType> state, bool isAttribute)
 {
     return(new ValidatorContext(simple, FileFormat, Part, Element, state, isAttribute, McContext, AddError));
 }
Exemplo n.º 10
0
        private static void ValidateValue(ValidationContext validationContext, ValidatorCollection validators, OpenXmlSimpleType value, ElementProperty <OpenXmlSimpleType> state, bool isAttribute)
        {
            var errors = validationContext.Errors.Count;

            using (validationContext.Stack.Push(value, state, isAttribute))
            {
                foreach (var validator in validators)
                {
                    validator.Validate(validationContext);

                    // Break early if validation has hit an error
                    if (errors != validationContext.Errors.Count)
                    {
                        return;
                    }
                }
            }
        }
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>
 /// Get the lenght of the attribute value according to the xsd type.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns></returns>
 /// <remarks>
 /// A value in a ·value space· is facet-valid with respect to ·length·, determined as follows:
 /// 2 if the {variety} is ·list·, then the length of the value, as measured in list items, ·must· be equal to {value}
 /// </remarks>
 internal int GetValueLength(OpenXmlSimpleType attributeValue)
 {
     Debug.Assert(false);
     return(0);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Validate whether the "length" constraint is ok.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns>True if the length of the value is same as defined.</returns>
 /// <remarks>
 /// A value in a ·value space· is facet-valid with respect to ·length·, determined as follows:
 /// 2 if the {variety} is ·list·, then the length of the value, as measured in list items, ·must· be equal to {value}
 /// </remarks>
 public bool IsMaxLengthValid(OpenXmlSimpleType attributeValue)
 {
     Debug.Assert(false);
     return(true);
 }
Exemplo n.º 14
0
        /// <inheritdoc />
        public override bool ValidateValueType(OpenXmlSimpleType attributeValue)
        {
            Debug.Assert(attributeValue != null);

            return(Validate(attributeValue.InnerText));
        }
        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            OpenXmlSimpleType attributeValue = context.Element.Attributes[_attribute];

            if (attributeValue == null || !attributeValue.HasValue || string.IsNullOrEmpty(attributeValue.InnerText))
            {
                return(null);
            }

            double value;

            //If value cannot be converted into double, that means attribute type is not correct.
            //That's job of schema validation, semantic validation will do nothing to avoid throw duplicated error.
            if (!GetAttrNumVal(attributeValue, out value))
            {
                return(null);
            }

            string minValueString;
            string maxValueString;

            if (attributeValue is HexBinaryValue)
            {
                minValueString = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:X}", (long)_minValue);
                maxValueString = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:X}", (long)_maxValue);
            }
            else
            {
                minValueString = _minValue.ToString(System.Globalization.CultureInfo.CurrentUICulture);
                maxValueString = _maxValue.ToString(System.Globalization.CultureInfo.CurrentUICulture);
            }

            string subMsg = null;

            if (_minInclusive)
            {
                if (!(_minValue <= value))
                {
                    subMsg = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sch_MinInclusiveConstraintFailed, minValueString);
                }
            }
            else
            {
                if (!(_minValue < value))
                {
                    subMsg = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sch_MinExclusiveConstraintFailed, minValueString);
                }
            }

            if (_maxInclusive)
            {
                if (!(value <= _maxValue))
                {
                    subMsg = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxInclusiveConstraintFailed, maxValueString);
                }
            }
            else
            {
                if (!(value < _maxValue))
                {
                    subMsg = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sch_MaxExclusiveConstraintFailed, maxValueString);
                }
            }

            if (subMsg == null)
            {
                return(null);
            }
            else
            {
                return(new ValidationErrorInfo()
                {
                    Id = "Sem_AttributeValueDataTypeDetailed",
                    ErrorType = ValidationErrorType.Schema,
                    Node = context.Element,
                    Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_AttributeValueDataTypeDetailed,
                                                GetAttributeQualifiedName(context.Element, _attribute), attributeValue, subMsg),
                });
            }
        }
Exemplo n.º 16
0
        internal static OpenXmlSimpleType CreateTargetValueObject(RedirectedRestriction redirectedRestriction)
        {
            OpenXmlSimpleType simpleValue = null;

            switch (redirectedRestriction.AttributeId)
            {
            // property name: Width
            case 2391:
                simpleValue = new StringValue();
                break;

            // property name: VerticalSpace
            case 2393:
                simpleValue = new StringValue();
                break;

            // property name: HorizontalSpace
            case 2394:
                simpleValue = new StringValue();
                break;

            // property name: X
            case 2398:
                simpleValue = new StringValue();
                break;

            // property name: Y
            case 2400:
                simpleValue = new StringValue();
                break;

            // property name: Before
            case 2413:
                simpleValue = new StringValue();
                break;

            // property name: After
            case 2416:
                simpleValue = new StringValue();
                break;

            // property name: Line
            case 2419:
                simpleValue = new StringValue();
                break;

            // property name: Left
            case 2421:
                simpleValue = new StringValue();
                break;

            // property name: Start
            case 2422:
                simpleValue = new StringValue();
                break;

            // property name: Right
            case 2425:
                simpleValue = new StringValue();
                break;

            // property name: End
            case 2426:
                simpleValue = new StringValue();
                break;

            // property name: Hanging
            case 2429:
                simpleValue = new StringValue();
                break;

            // property name: FirstLine
            case 2431:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2477:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2478:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2593:
                simpleValue = new StringValue();
                break;

            // property name: Distance
            case 2647:
                simpleValue = new StringValue();
                break;

            // property name: Space
            case 2654:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2684:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2690:
                simpleValue = new StringValue();
                break;

            // property name: Space
            case 2691:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2708:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2736:
                simpleValue = new StringValue();
                break;

            // property name: LegacySpace
            case 2742:
                simpleValue = new StringValue();
                break;

            // property name: LegacyIndent
            case 2743:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2773:
                simpleValue = new StringValue();
                break;

            // property name: Percent
            case 2851:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2876:
                simpleValue = new EnumValue <DocumentFormat.OpenXml.Wordprocessing.StylePaneSortMethodsValues>();
                break;

            // property name: FontSize
            case 2928:
                simpleValue = new StringValue();
                break;

            // property name: Val
            case 2990:
                simpleValue = new StringValue();
                break;

            // property name: Delay
            case 2995:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3075:
                simpleValue = new UInt32Value();
                break;

            // property name: AutoAdvance
            case 3083:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3084:
                simpleValue = new UInt32Value();
                break;

            // property name: ShapeId
            case 3088:
                simpleValue = new UInt32Value();
                break;

            // property name: ShapeId
            case 3093:
                simpleValue = new UInt32Value();
                break;

            // property name: AdvanceAfterTime
            case 3154:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3157:
                simpleValue = new UInt32Value();
                break;

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

            return(simpleValue);
        }
Exemplo n.º 17
0
        internal static OpenXmlSimpleType CreateTargetValueObject(RedirectedRestriction redirectedRestriction)
        {
            OpenXmlSimpleType simpleValue = null;

            switch (redirectedRestriction.AttributeId)
            {
            // property name: Width
            case 2387:
                simpleValue = new UInt32Value();
                break;

            // property name: VerticalSpace
            case 2389:
                simpleValue = new UInt32Value();
                break;

            // property name: HorizontalSpace
            case 2390:
                simpleValue = new UInt32Value();
                break;

            // property name: X
            case 2394:
                simpleValue = new Int32Value();
                break;

            // property name: Y
            case 2396:
                simpleValue = new Int32Value();
                break;

            // property name: Before
            case 2409:
                simpleValue = new UInt32Value();
                break;

            // property name: After
            case 2412:
                simpleValue = new UInt32Value();
                break;

            // property name: Line
            case 2415:
                simpleValue = new Int32Value();
                break;

            // property name: Left
            case 2417:
                simpleValue = new Int32Value();
                break;

            // property name: Start
            case 2418:
                simpleValue = new Int32Value();
                break;

            // property name: Right
            case 2421:
                simpleValue = new Int32Value();
                break;

            // property name: End
            case 2422:
                simpleValue = new Int32Value();
                break;

            // property name: Hanging
            case 2425:
                simpleValue = new UInt32Value();
                break;

            // property name: FirstLine
            case 2427:
                simpleValue = new UInt32Value();
                break;

            // property name: Val
            case 2473:
                simpleValue = new Int32Value();
                break;

            // property name: Val
            case 2474:
                simpleValue = new UInt32Value();
                break;

            // property name: Width
            case 2589:
                simpleValue = new Int32Value();
                break;

            // property name: Distance
            case 2643:
                simpleValue = new UInt32Value();
                break;

            // property name: Space
            case 2650:
                simpleValue = new UInt32Value();
                break;

            // property name: Val
            case 2680:
                simpleValue = new StringValue();
                break;

            // property name: Width
            case 2686:
                simpleValue = new UInt32Value();
                break;

            // property name: Space
            case 2687:
                simpleValue = new UInt32Value();
                break;

            // property name: Width
            case 2704:
                simpleValue = new UInt32Value();
                break;

            // property name: Val
            case 2732:
                simpleValue = new UInt32Value();
                break;

            // property name: LegacySpace
            case 2738:
                simpleValue = new UInt32Value();
                break;

            // property name: LegacyIndent
            case 2739:
                simpleValue = new Int32Value();
                break;

            // property name: Val
            case 2769:
                simpleValue = new Int32Value();
                break;

            // property name: Percent
            case 2847:
                simpleValue = new Int32Value();
                break;

            // property name: Val
            case 2872:
                simpleValue = new HexBinaryValue();
                break;

            // property name: FontSize
            case 2924:
                simpleValue = new Int32Value();
                break;

            // property name: Val
            case 2986:
                simpleValue = new StringValue();
                break;

            // property name: Delay
            case 2991:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3071:
                simpleValue = new StringValue();
                break;

            // property name: AutoAdvance
            case 3079:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3080:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3084:
                simpleValue = new StringValue();
                break;

            // property name: ShapeId
            case 3089:
                simpleValue = new StringValue();
                break;

            // property name: AdvanceAfterTime
            case 3150:
                simpleValue = new UInt32Value();
                break;

            // property name: ShapeId
            case 3153:
                simpleValue = new StringValue();
                break;

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

            return(simpleValue);
        }
        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            OpenXmlSimpleType attributeValue = context.Element.Attributes[_attribute];

            if (attributeValue == null)
            {
                return(null);
            }

            foreach (string value in _values)
            {
                if (AttributeValueEquals(attributeValue, value, false))
                {
                    return(null);
                }
            }

            OpenXmlSimpleType conditionAttributeValue = context.Element.Attributes[_conditionAttribute];

            if (conditionAttributeValue == null)
            {
                return(null);
            }

            foreach (string value in _otherValues)
            {
                if (AttributeValueEquals(conditionAttributeValue, value, false))
                {
                    string attributeValueString = "'" + _values[0] + "'";
                    if (_values.Length > 1)
                    {
                        for (int i = 1; i < _values.Length - 1; i++)
                        {
                            attributeValueString += ", '" + _values[i] + "'";
                        }

                        attributeValueString += " or '" + _values[_values.Length - 1] + "'";
                    }

                    string otherAttributeValueString = "'" + _otherValues[0] + "'";
                    if (_otherValues.Length > 1)
                    {
                        for (int i = 1; i < _otherValues.Length - 1; i++)
                        {
                            otherAttributeValueString += ", '" + _otherValues[i] + "'";
                        }

                        otherAttributeValueString += " or '" + _otherValues[_otherValues.Length - 1] + "'";
                    }

                    return(new ValidationErrorInfo()
                    {
                        Id = "Sem_AttributeValueConditionToAnother",
                        ErrorType = ValidationErrorType.Semantic,
                        Node = context.Element,
                        Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_AttributeValueConditionToAnother,
                                                    GetAttributeQualifiedName(context.Element, _attribute), attributeValueString,
                                                    GetAttributeQualifiedName(context.Element, _conditionAttribute), otherAttributeValueString,
                                                    GetAttributeQualifiedName(context.Element, _attribute), attributeValue)
                    });
                }
            }

            return(null);
        }
Exemplo n.º 19
0
        internal static OpenXmlSimpleType[] CreatePossibleMembers(UnionValueRestriction unionValueRestriction)
        {
            OpenXmlSimpleType[] simpleValues = new OpenXmlSimpleType[unionValueRestriction.UnionTypes.Length];

            switch (unionValueRestriction.UnionId)
            {
            // ST_AnimationDgmBuildType
            case 25:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Drawing.AnimationBuildValues>();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Drawing.AnimationDiagramOnlyBuildValues>();
                break;

            // ST_AnimationChartBuildType
            case 27:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Drawing.AnimationBuildValues>();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Drawing.AnimationChartOnlyBuildValues>();
                break;

            // ST_AdjCoordinate
            case 45:
                simpleValues[0] = new Int64Value();
                simpleValues[1] = new StringValue();
                break;

            // ST_AdjAngle
            case 46:
                simpleValues[0] = new Int32Value();
                simpleValues[1] = new StringValue();
                break;

            // ST_LayoutShapeType
            case 145:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Drawing.ShapeTypeValues>();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.OutputShapeValues>();
                break;

            // ST_ParameterVal
            case 183:
                simpleValues[0]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.HorizontalAlignmentValues>();
                simpleValues[1]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.VerticalAlignmentValues>();
                simpleValues[2]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ChildDirectionValues>();
                simpleValues[3]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ChildAlignmentValues>();
                simpleValues[4]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.SecondaryChildAlignmentValues>();
                simpleValues[5]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.LinearDirectionValues>();
                simpleValues[6]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.SecondaryLinearDirectionValues>();
                simpleValues[7]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.StartingElementValues>();
                simpleValues[8]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.BendPointValues>();
                simpleValues[9]  = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ConnectorRoutingValues>();
                simpleValues[10] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ArrowheadStyleValues>();
                simpleValues[11] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ConnectorDimensionValues>();
                simpleValues[12] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.RotationPathValues>();
                simpleValues[13] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.CenterShapeMappingValues>();
                simpleValues[14] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.NodeHorizontalAlignmentValues>();
                simpleValues[15] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.NodeVerticalAlignmentValues>();
                simpleValues[16] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.FallbackDimensionValues>();
                simpleValues[17] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.TextDirectionValues>();
                simpleValues[18] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.PyramidAccentPositionValues>();
                simpleValues[19] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.PyramidAccentTextMarginValues>();
                simpleValues[20] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.TextBlockDirectionValues>();
                simpleValues[21] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.TextAnchorHorizontalValues>();
                simpleValues[22] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.TextAnchorVerticalValues>();
                simpleValues[23] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.TextAlignmentValues>();
                simpleValues[24] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.AutoTextRotationValues>();
                simpleValues[25] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.GrowDirectionValues>();
                simpleValues[26] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.FlowDirectionValues>();
                simpleValues[27] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ContinueDirectionValues>();
                simpleValues[28] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.BreakpointValues>();
                simpleValues[29] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.OffsetValues>();
                simpleValues[30] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.HierarchyAlignmentValues>();
                simpleValues[31] = new Int32Value();
                simpleValues[32] = new DoubleValue();
                simpleValues[33] = new BooleanValue();
                simpleValues[34] = new StringValue();
                simpleValues[35] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ConnectorPointValues>();
                break;

            // ST_ModelId
            case 184:
                simpleValues[0] = new Int32Value();
                simpleValues[1] = new StringValue();
                break;

            // ST_FunctionValue
            case 207:
                simpleValues[0] = new Int32Value();
                simpleValues[1] = new BooleanValue();
                simpleValues[2] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.DirectionValues>();
                simpleValues[3] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.HierarchyBranchStyleValues>();
                simpleValues[4] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.AnimateOneByOneValues>();
                simpleValues[5] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.AnimationLevelStringValues>();
                simpleValues[6] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.ResizeHandlesStringValues>();
                break;

            // ST_FunctionArgument
            case 209:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Drawing.Diagrams.VariableValues>();
                break;

            // ST_NonZeroDecimalNumber
            case 368:
                simpleValues[0] = new Int32Value();
                simpleValues[1] = new Int32Value();
                break;

            // ST_MarkupId
            case 375:
                simpleValues[0] = new Int32Value();
                simpleValues[1] = new Int32Value();
                break;

            // ST_HexColor
            case 404:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Wordprocessing.AutomaticColorValues>();
                simpleValues[1] = new HexBinaryValue();
                break;

            // ST_DecimalNumberOrPercent
            case 507:
                simpleValues[0] = new StringValue();
                simpleValues[1] = new Int32Value();
                break;

            // ST_SignedHpsMeasure_O14
            case 525:
                simpleValues[0] = new IntegerValue();
                simpleValues[1] = new StringValue();
                break;

            // ST_HpsMeasure_O14
            case 528:
                simpleValues[0] = new UInt32Value();
                simpleValues[1] = new StringValue();
                break;

            // ST_SignedTwipsMeasure_O14
            case 531:
                simpleValues[0] = new IntegerValue();
                simpleValues[1] = new StringValue();
                break;

            // ST_TwipsMeasure_O14
            case 534:
                simpleValues[0] = new UInt32Value();
                simpleValues[1] = new StringValue();
                break;

            // ST_TransitionEightDirectionType
            case 544:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Presentation.TransitionSlideDirectionValues>();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Presentation.TransitionCornerDirectionValues>();
                break;

            // ST_TLTimeAnimateValueTime
            case 561:
                simpleValues[0] = new Int32Value();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Presentation.IndefiniteTimeDeclarationValues>();
                break;

            // ST_TLTime_O12
            case 603:
                simpleValues[0] = new UInt32Value();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Presentation.IndefiniteTimeDeclarationValues>();
                break;

            // ST_TLTime_O14
            case 604:
                simpleValues[0] = new StringValue();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Presentation.IndefiniteTimeDeclarationValues>();
                break;

            // ST_PublishDate
            case 689:
                simpleValues[0] = new DateTimeValue();
                simpleValues[1] = new DateTimeValue();
                simpleValues[2] = new StringValue();
                break;

            // ST_ChannelDataPoint
            case 697:
                simpleValues[0] = new DecimalValue();
                simpleValues[1] = new BooleanValue();
                break;

            // ST_ChannelPropertyName
            case 701:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.InkML.StandardChannelPropertyNameValues>();
                simpleValues[1] = new StringValue();
                break;

            // ST_BrushPropertyName
            case 704:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.InkML.StandardBrushPropertyNameValues>();
                simpleValues[1] = new StringValue();
                break;

            // ST_ChannelName
            case 707:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.InkML.StandardChannelNameValues>();
                simpleValues[1] = new StringValue();
                break;

            // ST_Units
            case 719:
                simpleValues[0]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardLengthUnitsValues>();
                simpleValues[1]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardPerLengthUnitsValues>();
                simpleValues[2]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardTimeUnitsValues>();
                simpleValues[3]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardPerTimeUnitsValues>();
                simpleValues[4]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardMassForceUnitsValues>();
                simpleValues[5]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardPerMassForceUnitsValues>();
                simpleValues[6]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardAngleUnitsValues>();
                simpleValues[7]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardPerAngleUnitsValues>();
                simpleValues[8]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardOtherUnitsValues>();
                simpleValues[9]  = new EnumValue <DocumentFormat.OpenXml.InkML.StandardPerOtherUnitsValues>();
                simpleValues[10] = new StringValue();
                break;

            // ST_BrushPropertyValue
            case 732:
                simpleValues[0] = new DecimalValue();
                simpleValues[1] = new BooleanValue();
                simpleValues[2] = new EnumValue <DocumentFormat.OpenXml.InkML.PenTipShapeValues>();
                simpleValues[3] = new EnumValue <DocumentFormat.OpenXml.InkML.RasterOperationValues>();
                simpleValues[4] = new StringValue();
                break;

            // ST_Ref
            case 746:
                simpleValues[0] = new StringValue();
                simpleValues[1] = new UInt32Value();
                break;

            // ST_CtxNodeType
            case 747:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Office2010.Ink.KnownContextNodeTypeValues>();
                simpleValues[1] = new StringValue();
                break;

            // ST_SemanticType
            case 750:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Office2010.Ink.KnownSemanticTypeValues>();
                simpleValues[1] = new UInt32Value();
                break;

            // ST_PointsOrInt
            case 753:
                simpleValues[0] = new ListValue <StringValue>();
                simpleValues[1] = new Int32Value();
                break;

            // ST_TransitionCornerAndCenterDirectionType
            case 766:
                simpleValues[0] = new EnumValue <DocumentFormat.OpenXml.Presentation.TransitionCornerDirectionValues>();
                simpleValues[1] = new EnumValue <DocumentFormat.OpenXml.Office2010.PowerPoint.TransitionCenterDirectionTypeValues>();
                break;

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

            Debug.Assert(simpleValues.Length > 0);

            return(simpleValues);
        }
Exemplo n.º 20
0
        internal static OpenXmlSimpleType[] CreatePossibleMembers(UnionValueRestriction unionValueRestriction)
        {
            OpenXmlSimpleType[] simpleValues = new OpenXmlSimpleType[unionValueRestriction.UnionTypes.Length];

            switch (unionValueRestriction.UnionId)
            {
                // ST_AnimationDgmBuildType
                case 25:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Drawing.AnimationBuildValues>();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Drawing.AnimationDiagramOnlyBuildValues>();
                    break;

                // ST_AnimationChartBuildType
                case 27:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Drawing.AnimationBuildValues>();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Drawing.AnimationChartOnlyBuildValues>();
                    break;

                // ST_AdjCoordinate
                case 45:
                    simpleValues[0] = new Int64Value();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_AdjAngle
                case 46:
                    simpleValues[0] = new Int32Value();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_LayoutShapeType
                case 145:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Drawing.ShapeTypeValues>();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.OutputShapeValues>();
                    break;

                // ST_ParameterVal
                case 183:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.HorizontalAlignmentValues>();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.VerticalAlignmentValues>();
                    simpleValues[2] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ChildDirectionValues>();
                    simpleValues[3] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ChildAlignmentValues>();
                    simpleValues[4] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.SecondaryChildAlignmentValues>();
                    simpleValues[5] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.LinearDirectionValues>();
                    simpleValues[6] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.SecondaryLinearDirectionValues>();
                    simpleValues[7] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.StartingElementValues>();
                    simpleValues[8] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.BendPointValues>();
                    simpleValues[9] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ConnectorRoutingValues>();
                    simpleValues[10] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ArrowheadStyleValues>();
                    simpleValues[11] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ConnectorDimensionValues>();
                    simpleValues[12] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.RotationPathValues>();
                    simpleValues[13] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.CenterShapeMappingValues>();
                    simpleValues[14] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.NodeHorizontalAlignmentValues>();
                    simpleValues[15] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.NodeVerticalAlignmentValues>();
                    simpleValues[16] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.FallbackDimensionValues>();
                    simpleValues[17] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.TextDirectionValues>();
                    simpleValues[18] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.PyramidAccentPositionValues>();
                    simpleValues[19] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.PyramidAccentTextMarginValues>();
                    simpleValues[20] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.TextBlockDirectionValues>();
                    simpleValues[21] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.TextAnchorHorizontalValues>();
                    simpleValues[22] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.TextAnchorVerticalValues>();
                    simpleValues[23] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.TextAlignmentValues>();
                    simpleValues[24] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.AutoTextRotationValues>();
                    simpleValues[25] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.GrowDirectionValues>();
                    simpleValues[26] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.FlowDirectionValues>();
                    simpleValues[27] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ContinueDirectionValues>();
                    simpleValues[28] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.BreakpointValues>();
                    simpleValues[29] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.OffsetValues>();
                    simpleValues[30] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.HierarchyAlignmentValues>();
                    simpleValues[31] = new Int32Value();
                    simpleValues[32] = new DoubleValue();
                    simpleValues[33] = new BooleanValue();
                    simpleValues[34] = new StringValue();
                    simpleValues[35] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ConnectorPointValues>();
                    break;

                // ST_ModelId
                case 184:
                    simpleValues[0] = new Int32Value();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_FunctionValue
                case 207:
                    simpleValues[0] = new Int32Value();
                    simpleValues[1] = new BooleanValue();
                    simpleValues[2] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.DirectionValues>();
                    simpleValues[3] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.HierarchyBranchStyleValues>();
                    simpleValues[4] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.AnimateOneByOneValues>();
                    simpleValues[5] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.AnimationLevelStringValues>();
                    simpleValues[6] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.ResizeHandlesStringValues>();
                    break;

                // ST_FunctionArgument
                case 209:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Drawing.Diagrams.VariableValues>();
                    break;

                // ST_NonZeroDecimalNumber
                case 368:
                    simpleValues[0] = new Int32Value();
                    simpleValues[1] = new Int32Value();
                    break;

                // ST_MarkupId
                case 375:
                    simpleValues[0] = new Int32Value();
                    simpleValues[1] = new Int32Value();
                    break;

                // ST_HexColor
                case 404:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Wordprocessing.AutomaticColorValues>();
                    simpleValues[1] = new HexBinaryValue();
                    break;

                // ST_DecimalNumberOrPercent
                case 507:
                    simpleValues[0] = new StringValue();
                    simpleValues[1] = new Int32Value();
                    break;

                // ST_SignedHpsMeasure_O14
                case 525:
                    simpleValues[0] = new IntegerValue();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_HpsMeasure_O14
                case 528:
                    simpleValues[0] = new UInt32Value();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_SignedTwipsMeasure_O14
                case 531:
                    simpleValues[0] = new IntegerValue();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_TwipsMeasure_O14
                case 534:
                    simpleValues[0] = new UInt32Value();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_TransitionEightDirectionType
                case 544:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Presentation.TransitionSlideDirectionValues>();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Presentation.TransitionCornerDirectionValues>();
                    break;

                // ST_TLTimeAnimateValueTime
                case 561:
                    simpleValues[0] = new Int32Value();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Presentation.IndefiniteTimeDeclarationValues>();
                    break;

                // ST_TLTime_O12
                case 603:
                    simpleValues[0] = new UInt32Value();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Presentation.IndefiniteTimeDeclarationValues>();
                    break;

                // ST_TLTime_O14
                case 604:
                    simpleValues[0] = new StringValue();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.Presentation.IndefiniteTimeDeclarationValues>();
                    break;

                // ST_PublishDate
                case 689:
                    simpleValues[0] = new DateTimeValue();
                    simpleValues[1] = new DateTimeValue();
                    simpleValues[2] = new StringValue();
                    break;

                // ST_ChannelDataPoint
                case 697:
                    simpleValues[0] = new DecimalValue();
                    simpleValues[1] = new BooleanValue();
                    break;

                // ST_ChannelPropertyName
                case 701:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardChannelPropertyNameValues>();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_BrushPropertyName
                case 704:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardBrushPropertyNameValues>();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_ChannelName
                case 707:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardChannelNameValues>();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_Units
                case 719:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardLengthUnitsValues>();
                    simpleValues[1] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardPerLengthUnitsValues>();
                    simpleValues[2] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardTimeUnitsValues>();
                    simpleValues[3] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardPerTimeUnitsValues>();
                    simpleValues[4] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardMassForceUnitsValues>();
                    simpleValues[5] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardPerMassForceUnitsValues>();
                    simpleValues[6] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardAngleUnitsValues>();
                    simpleValues[7] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardPerAngleUnitsValues>();
                    simpleValues[8] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardOtherUnitsValues>();
                    simpleValues[9] = new EnumValue<DocumentFormat.OpenXml.InkML.StandardPerOtherUnitsValues>();
                    simpleValues[10] = new StringValue();
                    break;

                // ST_BrushPropertyValue
                case 732:
                    simpleValues[0] = new DecimalValue();
                    simpleValues[1] = new BooleanValue();
                    simpleValues[2] = new EnumValue<DocumentFormat.OpenXml.InkML.PenTipShapeValues>();
                    simpleValues[3] = new EnumValue<DocumentFormat.OpenXml.InkML.RasterOperationValues>();
                    simpleValues[4] = new StringValue();
                    break;

                // ST_Ref
                case 746:
                    simpleValues[0] = new StringValue();
                    simpleValues[1] = new UInt32Value();
                    break;

                // ST_CtxNodeType
                case 747:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Office2010.Ink.KnownContextNodeTypeValues>();
                    simpleValues[1] = new StringValue();
                    break;

                // ST_SemanticType
                case 750:
                    simpleValues[0] = new EnumValue<DocumentFormat.OpenXml.Office2010.Ink.KnownSemanticTypeValues>();
                    simpleValues[1] = new UInt32Value();
                    break;

                // ST_PointsOrInt
                case 753:
                    simpleValues[0] = new ListValue<StringValue>();
                    simpleValues[1] = new Int32Value();
                    break;

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

            Debug.Assert(simpleValues.Length > 0);

            return simpleValues;
        }
Exemplo n.º 21
0
 /// <summary>
 /// Validating the specified value is valid according the XsdType.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns>False if the specified value is not valid.</returns>
 public virtual bool ValidateValueType(OpenXmlSimpleType attributeValue)
 {
     return(attributeValue.HasValue);
 }
        /// <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.º 23
0
 /// <summary>
 /// Test whether the attribute value is valid according the patten constraint.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns></returns>
 public virtual bool IsPatternValid(OpenXmlSimpleType attributeValue) => true;
        /// <summary>
        /// Validate the attributes constraint.
        /// </summary>
        /// <param name="validationContext">The validation context.</param>
        /// <param name="schemaTypeData">The constraint data of the schema type.</param>
        private static void ValidateAttributes(ValidationContext validationContext, SchemaTypeData schemaTypeData)
        {
            var element = validationContext.Element;

            Debug.Assert(element.Attributes == null && schemaTypeData.AttributeConstraintsCount == 0 ||
                         element.Attributes.Length == schemaTypeData.AttributeConstraintsCount);

            ValidationErrorInfo errorInfo;

            // validate xsd:use on attributes
            for (int i = 0; i < schemaTypeData.AttributeConstraintsCount; i++)
            {
                var attributeConstraint = schemaTypeData.AttributeConstraints[i];

                if (attributeConstraint.SupportedVersion.Includes(validationContext.FileFormat))
                {
                    // only check the attribute constraints defined in the specified file format version.

                    switch (attributeConstraint.XsdAttributeUse)
                    {
                    case XsdAttributeUse.Required:
                        if (element.Attributes[i] == null)
                        {
                            string attributeQname = element.GetFixedAttributeQname(i).ToString();
                            // error: miss required attribute
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, "Sch_MissRequiredAttribute", attributeQname);
                            errorInfo.SetDebugField(attributeQname, "Sch_MissRequiredAttribute");
                            validationContext.AddError(errorInfo);
                        }
                        break;

                    case XsdAttributeUse.None:     // none, so use default "optional"
                    case XsdAttributeUse.Optional:
                        break;

                    case XsdAttributeUse.Prohibited:     // no "prohibited" in Ecma at now.
                    default:
                        Debug.Assert(false);
                        break;
                    }

                    if (element.Attributes[i] != null)
                    {
                        OpenXmlSimpleType attributeValue = element.Attributes[i];

                        string attributeQname = element.GetFixedAttributeQname(i).ToString();

                        ValidateValue(validationContext, attributeConstraint.SimpleTypeConstraint, attributeValue, attributeQname, true);
                    }
                }
                else
                {
                    if (element.Attributes[i] != null)
                    {
                        // The attribute is not defined in the specified version, report error.
                        if (validationContext.McContext.IsIgnorableNs(element.AttributeNamespaceIds[i]))
                        {
                            // Ignorable attribute, no error.
                        }
                        else
                        {
                            // emit error
                            string attributeQname = element.GetFixedAttributeQname(i).ToString();;
                            errorInfo = validationContext.ComposeSchemaValidationError(element, null, "Sch_UndeclaredAttribute", attributeQname);
                            errorInfo.SetDebugField(attributeQname, "Sch_UndeclaredAttribute");
                            validationContext.AddError(errorInfo);
                        }
                    }
                }
            }

            // all unknown attributes (attributes not defined in schema) are in ExtendedAttributes.
            // they should be errors
            foreach (var extendedAttribute in element.ExtendedAttributes)
            {
                if (validationContext.McContext.IsIgnorableNs(extendedAttribute.NamespaceUri))
                {
                    // Ignorable attribute, no error.
                }
                // xml:space is always allowed
                else if ("http://www.w3.org/XML/1998/namespace" == extendedAttribute.NamespaceUri)
                {
                }
                else
                {
                    // emit error
                    string attributeQname = extendedAttribute.XmlQualifiedName.ToString();
                    errorInfo = validationContext.ComposeSchemaValidationError(element, null, "Sch_UndeclaredAttribute", attributeQname);
                    errorInfo.SetDebugField(attributeQname, "Sch_UndeclaredAttribute");
                    validationContext.AddError(errorInfo);
                }
            }
        }
Exemplo n.º 25
0
 /// <summary>
 /// Validate whether the "maxInclusive" constraint is ok.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns></returns>
 public virtual bool IsMaxInclusiveValid(OpenXmlSimpleType attributeValue)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 26
0
        internal static void ValidateValue(ValidationContext validationContext, ValidatorCollection validators, OpenXmlSimpleType value, string qname, ElementProperty <OpenXmlSimpleType> state, bool isAttribute)
        {
            var element = validationContext.Element;
            var errors  = validationContext.Errors.Count;

            foreach (var validator in validators)
            {
                validator.Validate(validationContext.ToContext(value, state, isAttribute));

                // Break early if validation has hit an error
                if (errors != validationContext.Errors.Count)
                {
                    return;
                }
            }
        }
Exemplo n.º 27
0
        protected static bool GetAttrNumVal(OpenXmlSimpleType attributeValue, out double value)
        {
            HexBinaryValue hexBinaryValue = attributeValue as HexBinaryValue;
            if (hexBinaryValue != null)
            {
                long val;
                bool result = long.TryParse(hexBinaryValue.Value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out val);
                value = val;
                return result;
            }

            return double.TryParse(attributeValue.InnerText, NumberStyles.AllowDecimalPoint,
                CultureInfo.InvariantCulture, out value);
        }
Exemplo n.º 28
0
 /// <summary>
 /// Get the length of the attribute value according to the xsd type.
 /// </summary>
 /// <param name="attributeValue"></param>
 /// <returns></returns>
 /// <remarks>
 /// A value in a ·value space· is facet-valid with respect to ·length·, determined as follows:
 /// 1 if the {variety} is ·atomic· then
 ///   1.1 if {primitive type definition} is string or anyURI, then the length of the value, as measured in characters ·must· be equal to {value};
 ///   1.2 if {primitive type definition} is hexBinary or base64Binary, then the length of the value, as measured in octets of the binary data, ·must· be equal to {value};
 ///   1.3 if {primitive type definition} is QName or NOTATION, then any {value} is facet-valid.
 /// 2 if the {variety} is ·list·, then the length of the value, as measured in list items, ·must· be equal to {value}
 /// </remarks>
 internal virtual int GetValueLength(OpenXmlSimpleType attributeValue)
 {
     return(attributeValue.InnerText.Length);
 }