• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Validation.ValidationContext类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中DocumentFormat.OpenXml.Validation.ValidationContext的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext类的具体用法?C# ValidationContext怎么用?C# ValidationContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ValidationContext类属于DocumentFormat.OpenXml.Validation命名空间,在下文中一共展示了ValidationContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Validate

        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            string minAttributeValue = context.Element.GetAttributeValueEx(_minAttributeLocalName, _minAttributeNamesapce);

            double minValue;

            //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 (!double.TryParse(minAttributeValue, out minValue))
            {
                return null;
            }

            string maxAttributeValue = context.Element.GetAttributeValueEx(_maxAttributeLocalName, _maxAttributeNamesapce);

            double maxValue;

            //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 (!double.TryParse(maxAttributeValue, out maxValue))
            {
                return null;
            }

            if (minValue <= maxValue)
            {
                return null;
            }

            string errorId = ""; //todo: add error id
            string errorMessage = ""; //todo: add error message

            return new ValidationErrorInfo() { Id = errorId, ErrorType = ValidationErrorType.Semantic, Node = context.Element, Description = errorMessage };
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:34,代码来源:AttributeMinMaxConstraint.cs


示例2: Validate

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

            //if the attribute is omited, semantic validation will do nothing 
            if (attributeValue == null || string.IsNullOrEmpty(attributeValue.InnerText))
            {
                return null;
            }

            if (_values.Where(v => string.Compare(v, attributeValue.InnerText, !_caseSensitive, System.Globalization.CultureInfo.InvariantCulture) == 0).Count() == 0)
            {
                _values.Add(attributeValue.InnerText);
                return null;
            }

            return new ValidationErrorInfo()
            {
                Id = "Sem_UniqueAttributeValue",
                ErrorType = ValidationErrorType.Semantic,
                Node = context.Element,
                Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_UniqueAttributeValue,
                                            GetAttributeQualifiedName(context.Element, _attribute), attributeValue.InnerText)
            };
        }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:30,代码来源:UniqueAttributeValueConstraint.cs


示例3: GetReferencedPart

        protected static OpenXmlPart GetReferencedPart(ValidationContext context, string path)
        {
            if (path == ".")
            {
                return context.Part;
            }

            string[] parts = path.Split('/');

            if (string.IsNullOrEmpty(parts[0]))
            {
                return GetPartThroughPartPath(context.Package.Parts, parts.Skip(1).ToArray()); //absolute path
            }
            else if (parts[0] == "..")
            {
                IEnumerable<OpenXmlPart> iterator = new OpenXmlPackagePartIterator(context.Package);

                IEnumerable<OpenXmlPart> refParts = iterator.Where(p => p.Parts.Select(r => r.OpenXmlPart.PackagePart.Uri)
                                                            .Contains(context.Part.PackagePart.Uri));

                Debug.Assert(refParts.Count() == 1);

                return refParts.First();
            }
            else
            {
                return GetPartThroughPartPath(context.Part.Parts, parts); //relative path
            }
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:29,代码来源:SemanticConstraint.cs


示例4: TryMatch

 /// <summary>
 /// Try match the particle.
 /// </summary>
 /// <param name="particleMatchInfo"></param>
 /// <param name="validationContext">The context information for validation.</param>
 public override void TryMatch(ParticleMatchInfo particleMatchInfo, ValidationContext validationContext)
 {
     // maxOccurs of xsd:any can only be 1
     Debug.Assert(this.ParticleConstraint.MaxOccurs == 1);
      
     this.TryMatchOnce(particleMatchInfo, validationContext);
 }
开发者ID:ErykJaroszewicz,项目名称:Open-XML-SDK,代码行数:12,代码来源:AllParticleValidator.cs


示例5: Validate

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

            //if the attribute is omited, semantic validation will do nothing 
            if (attributeValue == null || string.IsNullOrEmpty(attributeValue.InnerText))
            {
                return null;
            }

            int index;
            if (!int.TryParse(attributeValue, out index))
            {
                return null; //if attribute is not int, schema validation will cover this error.
            }

            if (index < GetRefElementCount(context) + _indexBase)
            {
                return null;
            }

            return new ValidationErrorInfo()
            {
                Id = "Sem_MissingIndexedElement",
                ErrorType = ValidationErrorType.Semantic,
                Node = context.Element,
                RelatedPart = this._relatedPart,
                RelatedNode = null,
                Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_MissingIndexedElement, 
                                            _refElementName,context.Element.LocalName, 
                                            GetAttributeQualifiedName(context.Element, _attribute), 
                                            _relatedPart == null? _refPartType : _relatedPart.PackagePart.Uri.ToString(), index)
            };
        }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:34,代码来源:IndexReferenceConstraint.cs


示例6: Validate

        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            OpenXmlSimpleType attributeValue = context.Element.Attributes[_refAttribute];

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

            if (GetReferencedAttributes(context).Contains(attributeValue.InnerText))
            {
                return null;
            }

            return new ValidationErrorInfo()
            {
                Id = "Sem_MissingReferenceElement",
                ErrorType = ValidationErrorType.Semantic,
                Node = context.Element,
                RelatedPart = this._relatedPart,
                RelatedNode = null,
                Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, 
                                            ValidationResources.Sem_MissingReferenceElement, _elementName, context.Element.LocalName, 
                                            GetAttributeQualifiedName(context.Element, _refAttribute), _relatedPart == null ? _partPath : _relatedPart.PackagePart.Uri.ToString(), attributeValue.InnerText)
            };
        }
开发者ID:ErykJaroszewicz,项目名称:Open-XML-SDK,代码行数:26,代码来源:ReferenceExistConstraint.cs


示例7: Validate

        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            OpenXmlSimpleType attributeValue = context.Element.Attributes[_rIdAttribute];

            //if the attribute is omited, semantic validation will do nothing 
            if (attributeValue == null || string.IsNullOrEmpty(attributeValue.InnerText))
            {
                return null;
            }

            if (context.Part.PackagePart.RelationshipExists(attributeValue.InnerText))
            {
                return null;
            }
            else
            {
                string errorDescription = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_InvalidRelationshipId,
                                                        attributeValue, GetAttributeQualifiedName(context.Element, _rIdAttribute));

                return new ValidationErrorInfo()
                {
                    Id = "Sem_InvalidRelationshipId",
                    ErrorType = ValidationErrorType.Semantic,
                    Node = context.Element,
                    Description = errorDescription
                };
            }
        }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:28,代码来源:RelationshipExistConstraint.cs


示例8: Validate

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

            //if the attribute is omited, semantic validation will do nothing 
            if (attributeValue == null || string.IsNullOrEmpty(attributeValue.InnerText))
            {
                return null;
            }

            Regex regex = new Regex(this._pattern);
            if (regex.IsMatch(attributeValue.InnerText))
            {
                return null;
            }

            string subMsg = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sch_PatternConstraintFailed, _pattern);
            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.InnerText, subMsg)
            };
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:26,代码来源:AttributeValuePatternConstraint.cs


示例9: Validate

        public void Validate(ValidationContext validationContext)
        {
            Debug.Assert(validationContext != null);
            Debug.Assert(validationContext.Element != null);

            this._stopValidating = false;

            ValidationTraverser.ValidatingTraverse(validationContext, this.ValidateElement, OnContextValidationFinished, this.StopSignal);
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:9,代码来源:SemanticValidator.cs


示例10: ValidateElement

 private void ValidateElement(ValidationContext context)
 {
     if (_curReg != null)
     {
         foreach (var error in _curReg.CheckConstraints(context))
         {
             context.EmitError(error);
         }
     }
 }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:10,代码来源:SemanticValidator.cs


示例11: TryMatchOnce

        /// <summary>
        /// Try match the particle once.
        /// </summary>
        /// <param name="particleMatchInfo"></param>
        /// <param name="validationContext">The context information for validation.</param>
        public override void TryMatchOnce(ParticleMatchInfo particleMatchInfo, ValidationContext validationContext)
        {
            Debug.Assert(!(particleMatchInfo.StartElement is OpenXmlMiscNode));
            
            var next = particleMatchInfo.StartElement;

            particleMatchInfo.LastMatchedElement = null;
            particleMatchInfo.Match = ParticleMatch.Nomatch;

            ParticleConstraint childConstraint;
            int constraintIndex = 0;
            int constraintTotal = this.ParticleConstraint.ChildrenParticles.Length;

            while (constraintIndex < constraintTotal && next != null)
            {
                childConstraint = this.ParticleConstraint.ChildrenParticles[constraintIndex];

                // Use Reset() instead of new() to avoid heavy memory alloction and GC.
                _childMatchInfo.Reset(next);

                childConstraint.ParticleValidator.TryMatch(_childMatchInfo, validationContext);

                // if the _childMatchInfo.StartElement is changed, it means this method of this object is called more than once on the stack.
                Debug.Assert(_childMatchInfo.StartElement == next);

                switch (_childMatchInfo.Match)
                {
                    case ParticleMatch.Nomatch:
                        // continue trying match next child constraint.
                        constraintIndex++;
                        break;

                    case ParticleMatch.Matched:
                        particleMatchInfo.Match = ParticleMatch.Matched;
                        particleMatchInfo.LastMatchedElement = _childMatchInfo.LastMatchedElement;
                        return;

                    case ParticleMatch.Partial:
                        // partial match, incomplete children.
                        particleMatchInfo.Match = ParticleMatch.Partial;
                        particleMatchInfo.LastMatchedElement = _childMatchInfo.LastMatchedElement;
                        if (validationContext.CollectExpectedChildren)
                        {
                            particleMatchInfo.SetExpectedChildren(_childMatchInfo.ExpectedChildren);
                        }
                        return;
                }
            }

            // no match
            Debug.Assert(particleMatchInfo.Match == ParticleMatch.Nomatch);
            return;
        }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:58,代码来源:ChoiceParticleValidator.cs


示例12: Validate

        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            OpenXmlSimpleType attributeValue = context.Element.Attributes[_requiredAttribute];

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

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

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

            foreach (string value in _values)
            {
                if (AttributeValueEquals(conditionAttributeValue, value, false))
                {
                    string valueString = "'" + _values[0] + "'";

                    if (_values.Length > 1)
                    {
                        for (int i = 1; i < _values.Length - 1; i++)
                        {
                            valueString += ", '" + _values[i] + "'";
                        }

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

                    

                    return new ValidationErrorInfo()
                    {
                        Id = "Sem_AttributeRequiredConditionToValue",
                        ErrorType = ValidationErrorType.Semantic,
                        Node = context.Element,
                        Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_AttributeRequiredConditionToValue,
                                                    GetAttributeQualifiedName(context.Element, _requiredAttribute),                            
                                                    GetAttributeQualifiedName(context.Element, _conditionAttribute),
                                                    valueString)
                    };
                }
            }

            return null;
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:49,代码来源:AttributeRequiredConditionToValue.cs


示例13: Validate

        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            OpenXmlElement parent = context.Element.Parent;

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

            if (parent.GetType() == this._parentType ^ !this._isValid) //TODO: (junzha) need to take ac-block into account.
            {
                return null;
            }

            return new ValidationErrorInfo() { Id = "", ErrorType = ValidationErrorType.Semantic, Node = context.Element, Description = "" };
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:16,代码来源:ParentTypeConstraint.cs


示例14: Validate

        public override ValidationErrorInfo Validate(ValidationContext context)
        {
            if (context.Element.Attributes[_attribute] != null)
            {
                return null;
            }

            return new ValidationErrorInfo() 
            { 
                Id = "Sem_MissRequiredAttribute", 
                ErrorType = ValidationErrorType.Schema, 
                Node = context.Element,
                Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sch_MissRequiredAttribute, 
                                            GetAttributeQualifiedName(context.Element, _attribute)) 
            };
        }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:16,代码来源:AttributeCannotOmitConstraint.cs


示例15: TryMatchOnce

        // ***********************************************************
        //<xsd:group ref="..." /> is valid under <xsd:complexType>
        //<xsd:complexType name="CT_HdrFtr">
        //  <xsd:group ref="EG_BlockLevelElts" minOccurs="1" maxOccurs="unbounded" />
        //</xsd:complexType>
        // ***********************************************************

        ///// <summary>
        ///// Be called on root particle of complex type.
        ///// </summary>
        ///// <param name="validationContext"></param>
        ///// <returns></returns>
        //internal override SchemaValidationResult Validate(ValidationContext validationContext)
        //{
        //    throw new InvalidOperationException();
        //}


        /// <summary>
        /// Try match the particle once.
        /// </summary>
        /// <param name="particleMatchInfo"></param>
        /// <param name="validationContext">The context information for validation.</param>
        public override void TryMatchOnce(ParticleMatchInfo particleMatchInfo, ValidationContext validationContext)
        {
            Debug.Assert(!(particleMatchInfo.StartElement is OpenXmlMiscNode));
            
            // group only contains xsd:all, xsd:choice or xsd:sequence
            Debug.Assert(this.ParticleConstraint.ChildrenParticles.Length == 1);

            var childParticle = this.ParticleConstraint.ChildrenParticles[0];

            Debug.Assert(childParticle.ParticleType == ParticleType.All ||
                        childParticle.ParticleType == ParticleType.Choice ||
                        childParticle.ParticleType == ParticleType.Sequence);

            childParticle.ParticleValidator.TryMatch(particleMatchInfo, validationContext);

            return;
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:40,代码来源:GroupParticleValidator.cs


示例16: ClearState

 public override void ClearState(ValidationContext context)
 {
     if (context == null) //initialize before validating
     {
         _stateStack.Clear();
         _values = _partLevel ? new List<string>() : null;
     }
     else //unique scope element reached
     {
         if (_values != null)
         {
             _stateStack.Push(_values);
         }
         _reg.AddCallBackMethod(context.Element, this.AdjustState);
         _values = new List<string>();
     }
 }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:17,代码来源:UniqueAttributeValueConstraint.cs


示例17: Validate

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

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

            double val;
            if (!GetAttrNumVal(attributeValue, out val))
            {
                return null;
            }

            OpenXmlSimpleType otherAttributeValue = context.Element.Attributes[_otherAttribute];

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

            double otherVal;
            if (!GetAttrNumVal(otherAttributeValue, out otherVal))
            {
                return null;
            }

            if (val < otherVal && !_canEqual || val <= otherVal && _canEqual)
            {
                return null;
            }

            string format = _canEqual ? ValidationResources.Sem_AttributeValueLessEqualToAnother : ValidationResources.Sem_AttributeValueLessEqualToAnotherEx;

            return new ValidationErrorInfo()
            {
                Id = "Sem_AttributeValueLessEqualToAnother",
                ErrorType = ValidationErrorType.Semantic,
                Node = context.Element,
                Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, format,
                                            GetAttributeQualifiedName(context.Element, _attribute), attributeValue.InnerText,
                                            GetAttributeQualifiedName(context.Element, _otherAttribute), otherAttributeValue.InnerText)
            };
        }
开发者ID:RicardoLo,项目名称:Open-XML-SDK,代码行数:45,代码来源:AttributeValueLessEqualToAnother.cs


示例18: AttributeMinMaxConstraintTest

        public void AttributeMinMaxConstraintTest()
        {
            Excel.Column column = new Excel.Column();
            ValidationContext context = new ValidationContext() { Element = column };

            AttributeMinMaxConstraint constraint = new AttributeMinMaxConstraint("", "min", "", "max") ;

            column.Max = 2;
            column.Min = 1;
            Assert.Null(constraint.Validate(context)); //max > min, should pass validation

            column.Max = 2;
            column.Min = 2;
            Assert.Null(constraint.Validate(context)); //max == min, should pass validation

            column.Max = 2;
            column.Min = 3;
            Assert.NotNull(constraint.Validate(context)); //max < min, validation should be failed.
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:19,代码来源:SemanticConstraintTest.cs


示例19: Validate

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

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

            string actualType = _type;

            IEnumerable<ExternalRelationship> rels = context.Part.ExternalRelationships.Where(r => r.Id == attributeValue.InnerText);
            
            if (rels.Count() == 0)
            {
                IEnumerable<IdPartPair> pairs = context.Part.Parts.Where(p => p.RelationshipId == attributeValue.InnerText);
                if (pairs.Count() != 0)
                {
                    Debug.Assert(pairs.Count() == 1);
                    actualType = pairs.First().OpenXmlPart.RelationshipType;
                }
            }
            else
            {
                Debug.Assert(rels.Count() == 1);
                actualType = rels.First().RelationshipType;
            }

            if (actualType == _type)
            {
                return null;
            }

            return new ValidationErrorInfo()
            {
                Id = "Sem_IncorrectRelationshipType",
                ErrorType = ValidationErrorType.Semantic,
                Node = context.Element,
                Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture, ValidationResources.Sem_IncorrectRelationshipType,
                                            actualType, GetAttributeQualifiedName(context.Element, _attribute), this._type)
            };
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:42,代码来源:RelationshipTypeConstraint.cs


示例20: Validate

        /// <summary>
        /// Validate the DOM tree under the specified OpenXmlElement.
        /// </summary>
        /// <param name="schemaValidator">The schemaValidator.</param>
        /// <param name="openxmlElement">The root of the sub tree.</param>
        /// <returns>Returns the validation result in ValidationResult.</returns>
        /// <remarks>
        /// Only schema validating.
        /// </remarks>
        internal static ValidationResult Validate(this SchemaValidator schemaValidator, OpenXmlElement openxmlElement)
        {
            Debug.Assert(openxmlElement != null);

            var validationResult = new ValidationResult();

            Debug.Assert(!(openxmlElement is OpenXmlUnknownElement || openxmlElement is OpenXmlMiscNode));

            // Can not just validate AlternateContent / Choice / Fallback
            // Debug.Assert(!(openxmlElement is AlternateContent))
            Debug.Assert(!(openxmlElement is AlternateContentChoice || openxmlElement is AlternateContentFallback));

            var validationContext = new ValidationContext();
            validationContext.ValidationErrorEventHandler += validationResult.OnValidationError;

            validationContext.Element = openxmlElement;

            schemaValidator.Validate(validationContext);

            return validationResult;
        }
开发者ID:eriawan,项目名称:Open-XML-SDK,代码行数:30,代码来源:ValidatorExtension.cs



注:本文中的DocumentFormat.OpenXml.Validation.ValidationContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Dokan.DOKAN_FILE_INFO类代码示例发布时间:2022-05-24
下一篇:
C# Validation.OpenXmlValidator类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap