Inheritance: SimpleTypeValidator
示例#1
0
        protected void SetListValue(object value, XmlSchemaDatatype datatype)
        {
            string   strValue = ListSimpleTypeValidator.ToString(value);
            XElement element  = this.GetUntyped();

            element.Value = strValue;
        }
        private static object ParseUnionValue(string value, XElement element, XName itemXName,
                                              SimpleTypeValidator typeDef, ContainerType containerType)
        {
            //Parse the string value based on the member types
            UnionSimpleTypeValidator unionDef = typeDef as UnionSimpleTypeValidator;

            Debug.Assert(unionDef != null);
            SimpleTypeValidator matchingType = null;
            object    typedValue;
            Exception e = unionDef.TryParseValue(value, NameTable, new XNamespaceResolver(element), out matchingType,
                                                 out typedValue);

            ListSimpleTypeValidator listType = matchingType as ListSimpleTypeValidator;

            if (listType != null)
            {
                SimpleTypeValidator itemType = listType.ItemType;
                return(new XListContent <object>((IList)typedValue, element, itemXName, containerType,
                                                 itemType.DataType));
            }

            Debug.Assert(e == null);

            return(typedValue);
        }
        private void SetUnionCatchAll(object value,
                                      string propertyName,
                                      XTypedElement container,
                                      XName itemXName,
                                      SimpleTypeValidator typeDef,
                                      SchemaOrigin origin)
        {
            UnionSimpleTypeValidator unionDef = typeDef as UnionSimpleTypeValidator;

            Debug.Assert(unionDef != null);
            SimpleTypeValidator matchingType = null;
            object    typedValue;
            Exception e = unionDef.TryParseValue(value,
                                                 XTypedServices.NameTable,
                                                 new XNamespaceResolver(container.GetUntyped()),
                                                 out matchingType,
                                                 out typedValue);

            if (e != null)
            {
                throw new LinqToXsdException(propertyName, e.Message);
            }
            else
            {
                if (matchingType is ListSimpleTypeValidator)
                {
                    ListSimpleTypeValidator listType = matchingType as ListSimpleTypeValidator;
                    switch (origin)
                    {
                    case SchemaOrigin.Element:
                        SetListElement(itemXName, value, listType.ItemType.DataType);
                        break;

                    case SchemaOrigin.Text:
                        SetListValue(value, listType.ItemType.DataType);
                        break;

                    case SchemaOrigin.Attribute:
                        SetListAttribute(itemXName, value, listType.ItemType.DataType);
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    switch (origin)
                    {
                    case SchemaOrigin.Element: SetElement(itemXName, value, matchingType.DataType); break;

                    case SchemaOrigin.Text: SetValue(value, matchingType.DataType); break;

                    case SchemaOrigin.Attribute: SetAttribute(itemXName, value, matchingType.DataType); break;

                    default: break;
                    }
                }
            }
        }
示例#4
0
        internal override Exception CheckValueFacets(object value, SimpleTypeValidator type)
        {
            Exception linqToXsdFacetException;

            Xml.Schema.Linq.RestrictionFacets facets = type.RestrictionFacets;
            if ((facets == null ? false : facets.HasValueFacets))
            {
                IList     listValue = null;
                Exception e         = ListSimpleTypeValidator.ToList(value, ref listValue);
                if (e == null)
                {
                    int length = listValue.Count;
                    XmlSchemaDatatype datatype             = type.DataType;
                    Xml.Schema.Linq.RestrictionFlags flags = facets.Flags;
                    if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Enumeration) != 0)
                    {
                        if (!this.MatchEnumeration(value, facets.Enumeration, datatype))
                        {
                            linqToXsdFacetException = new LinqToXsdFacetException(Xml.Schema.Linq.RestrictionFlags.Enumeration, facets.Enumeration, value);
                            return(linqToXsdFacetException);
                        }
                    }
                    if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.Length) != 0)
                    {
                        if (length != facets.Length)
                        {
                            linqToXsdFacetException = new LinqToXsdFacetException(Xml.Schema.Linq.RestrictionFlags.Length, (object)facets.Length, value);
                            return(linqToXsdFacetException);
                        }
                    }
                    if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MaxLength) != 0)
                    {
                        if (length > facets.MaxLength)
                        {
                            linqToXsdFacetException = new LinqToXsdFacetException(Xml.Schema.Linq.RestrictionFlags.MaxLength, (object)facets.MaxLength, value);
                            return(linqToXsdFacetException);
                        }
                    }
                    if ((int)(flags & Xml.Schema.Linq.RestrictionFlags.MinLength) != 0)
                    {
                        if (length < facets.MinLength)
                        {
                            linqToXsdFacetException = new LinqToXsdFacetException(Xml.Schema.Linq.RestrictionFlags.MinLength, (object)facets.MinLength, value);
                            return(linqToXsdFacetException);
                        }
                    }
                    linqToXsdFacetException = null;
                }
                else
                {
                    linqToXsdFacetException = e;
                }
            }
            else
            {
                linqToXsdFacetException = null;
            }
            return(linqToXsdFacetException);
        }
示例#5
0
        internal override Exception TryParseValue(object value, NameTable nameTable, XNamespaceResolver resolver, out SimpleTypeValidator matchingType, out object typedValue)
        {
            Exception exception;
            Exception e = null;

            typedValue   = null;
            matchingType = null;
            if ((base.RestrictionFacets == null ? false : base.RestrictionFacets.HasLexicalFacets))
            {
                string parsedString = null;
                e = this.TryParseString(value, nameTable, resolver, out parsedString);
                if (e == null)
                {
                    e = this.facetsChecker.CheckLexicalFacets(ref parsedString, value, nameTable, resolver, this);
                }
            }
            if (e == null)
            {
                e = this.facetsChecker.CheckValueFacets(value, this);
            }
            if (e == null)
            {
                IList listItems = null;
                e = ListSimpleTypeValidator.ToList(value, ref listItems);
                if (e == null)
                {
                    foreach (object listItem in listItems)
                    {
                        object typedItemValue = null;
                        SimpleTypeValidator itemMatchingType = null;
                        e = this.itemType.TryParseValue(listItem, nameTable, resolver, out itemMatchingType, out typedItemValue);
                        if (e != null)
                        {
                            exception = e;
                            return(exception);
                        }
                    }
                    typedValue   = listItems;
                    matchingType = this;
                    exception    = null;
                }
                else
                {
                    exception = e;
                }
            }
            else
            {
                exception = e;
            }
            return(exception);
        }
        internal override bool MatchEnumeration(object value, ArrayList enumeration, XmlSchemaDatatype datatype)
        {
            string strValue = ListSimpleTypeValidator.ToString(value);

            foreach (object correctArray in enumeration)
            {
                if (strValue.Equals(correctArray))
                {
                    return(true);
                }
            }
            return(false);
        }
        protected void SetListElementWithValidation(XName name, object value, string propertyName, SimpleTypeValidator typeDef)
        {
            object typedValue;
            SimpleTypeValidator matchingType = null;
            Exception           e            = typeDef.TryParseValue(value, XTypedServices.NameTable, new XNamespaceResolver(this.Untyped), out matchingType, out typedValue);

            if (e != null)
            {
                throw new LinqToXsdException(propertyName, e.Message);
            }
            ListSimpleTypeValidator listDef = typeDef as ListSimpleTypeValidator;

            Debug.Assert(listDef != null);
            this.SetListElement(name, typedValue, listDef.ItemType.DataType);
        }
示例#8
0
        private void SaveValue()
        {
            switch (containerType)
            {
            case ContainerType.Element:
                containerElement.Value = ListSimpleTypeValidator.ToString(this.items);
                return;

            case ContainerType.Attribute:
                XAttribute attr = containerElement.Attribute(itemXName);
                Debug.Assert(attr != null);
                attr.Value = ListSimpleTypeValidator.ToString(this.items);
                return;
            }
        }
示例#9
0
        private void SaveValue()
        {
            switch (this.containerType)
            {
            case ContainerType.Attribute:
            {
                XAttribute attr = this.containerElement.Attribute(this.itemXName);
                Debug.Assert(attr != null);
                attr.Value = ListSimpleTypeValidator.ToString(this.items);
                break;
            }

            case ContainerType.Element:
            {
                this.containerElement.Value = ListSimpleTypeValidator.ToString(this.items);
                break;
            }
            }
        }
        private static object ParseUnionValue(string value, XElement element, XName itemXName, SimpleTypeValidator typeDef, ContainerType containerType)
        {
            object typedValue;
            object objs;
            UnionSimpleTypeValidator unionDef = typeDef as UnionSimpleTypeValidator;

            Debug.Assert(unionDef != null);
            SimpleTypeValidator     matchingType = null;
            Exception               e            = unionDef.TryParseValue(value, XTypedServices.NameTable, new XNamespaceResolver(element), out matchingType, out typedValue);
            ListSimpleTypeValidator listType     = matchingType as ListSimpleTypeValidator;

            if (listType == null)
            {
                Debug.Assert(e == null);
                objs = typedValue;
            }
            else
            {
                SimpleTypeValidator itemType = listType.ItemType;
                objs = new XListContent <object>((IList)typedValue, element, itemXName, containerType, itemType.DataType);
            }
            return(objs);
        }
        protected void SetListValueWithValidation(object value,
                                                  string propertyName,
                                                  SimpleTypeValidator typeDef)
        {
            //This is for list type, will be saved as space-separated strings
            ListSimpleTypeValidator listDef = typeDef as ListSimpleTypeValidator;

            Debug.Assert(listDef != null);

            object typedValue;
            SimpleTypeValidator matchingType = null;

            Exception e = typeDef.TryParseValue(value, XTypedServices.NameTable, new XNamespaceResolver(this.GetUntyped()), out matchingType, out typedValue);

            if (e == null)
            {
                SetListValue(value, matchingType.DataType);
            }
            else
            {
                throw new LinqToXsdException(propertyName, e.Message);
            }
        }
示例#12
0
 protected void SetListElement(XName name, object value, XmlSchemaDatatype datatype)
 {
     this.SetElement(name, ListSimpleTypeValidator.ToString(value), datatype);
 }
        internal override Exception CheckValueFacets(object value, SimpleTypeValidator type)
        {
            RestrictionFacets facets = type.RestrictionFacets;

            if (facets == null || !facets.HasValueFacets)
            {
                return(null);
            }
            //Check for facets allowed on lists - Length, MinLength, MaxLength
            //value is a list
            IList     listValue = null;
            Exception e         = ListSimpleTypeValidator.ToList(value, ref listValue);

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

            int length = listValue.Count;

            XmlSchemaDatatype datatype = type.DataType;
            RestrictionFlags  flags    = facets.Flags;

            if ((flags & RestrictionFlags.Enumeration) != 0)
            {
                ArrayList enums = facets.Enumeration;

                if (!MatchEnumeration(value, enums, datatype))
                {
                    return(new LinqToXsdFacetException(RestrictionFlags.Enumeration,
                                                       facets.Enumeration,
                                                       value));
                }
            }

            if ((flags & RestrictionFlags.Length) != 0)
            {
                if (length != facets.Length)
                {
                    return(new LinqToXsdFacetException(RestrictionFlags.Length, facets.Length, value));
                }
            }

            if ((flags & RestrictionFlags.MaxLength) != 0)
            {
                if (length > facets.MaxLength)
                {
                    return(new LinqToXsdFacetException(RestrictionFlags.MaxLength, facets.MaxLength, value));
                }
            }

            if ((flags & RestrictionFlags.MinLength) != 0)
            {
                if (length < facets.MinLength)
                {
                    return(new LinqToXsdFacetException(RestrictionFlags.MinLength, facets.MinLength, value));
                }
            }

            return(null);
        }
示例#14
0
 protected void SetListAttribute(XName name,
                                 object value,
                                 XmlSchemaDatatype datatype)
 {
     SetAttribute(name, ListSimpleTypeValidator.ToString(value), datatype);
 }