예제 #1
0
        //Called from Load/parse and auto-typing
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager, Type rootType,
                                                    Type contentType)
        {
            XTypedElement rootElement = GetAnnotation(rootType, xe);

            if (rootElement == null)
            {
                //If not already created
                XName
                    instanceElementName =
                    xe.Name;     //Storing the name since SetName() called from the functional const of rootType will update the name to that of the root type
                XTypedElement innerType = ToXTypedElement(xe, typeManager, contentType);
                Debug.Assert(innerType != null);
                ConstructorInfo constInfo = rootType.GetConstructor(new Type[] { contentType });

                if (constInfo != null)
                {
                    rootElement = (XTypedElement)constInfo.Invoke(new object[] { innerType });
                }
                else
                {
                    throw new LinqToXsdException(contentType.ToString() +
                                                 " is not an expected content type for root element type " +
                                                 rootType.ToString());
                }

                if (!TypeValid(rootElement, instanceElementName))
                {
                    throw new LinqToXsdException("Element is not an instance of type " + rootType);
                }
            }

            return(rootElement);
        }
예제 #2
0
        //Auto-typing
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager)
        {
            if (xe == null)
            {
                return(null);
            }

            XName elementName = xe.Name;
            Type  clrType     = null;
            Dictionary <XName, Type> elementDictionary = typeManager.GlobalElementDictionary;

            if (elementDictionary.TryGetValue(elementName, out clrType))
            {
                //Check if its a root wrapper type
                Type contentType = null;
                if (typeManager.RootContentTypeMapping.TryGetValue(clrType, out contentType))
                {
                    return(ToXTypedElement(xe, typeManager, clrType, contentType));
                }

                return(ToXTypedElement(xe, typeManager, clrType));
            }

            Type xsiClrType =
                GetXsiClrType(xe,
                              typeManager); //Check for global xsi:type, tag might be a fragment or unknown element name

            if (xsiClrType != null)
            {
                return(ToXTypedElement(xe, typeManager, xsiClrType));
            }

            return(null);
        }
예제 #3
0
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager)
        {
            XTypedElement xTypedElement;

            if (xe != null)
            {
                XName elementName = xe.Name;
                Type  clrType     = null;
                if (!typeManager.GlobalElementDictionary.TryGetValue(elementName, out clrType))
                {
                    Type xsiClrType = XTypedServices.GetXsiClrType(xe, typeManager);
                    if (!(xsiClrType != null))
                    {
                        xTypedElement = null;
                    }
                    else
                    {
                        xTypedElement = XTypedServices.ToXTypedElement(xe, typeManager, xsiClrType);
                    }
                }
                else
                {
                    Type contentType = null;
                    xTypedElement = (!typeManager.RootContentTypeMapping.TryGetValue(clrType, out contentType) ? XTypedServices.ToXTypedElement(xe, typeManager, clrType) : XTypedServices.ToXTypedElement(xe, typeManager, clrType, contentType));
                }
            }
            else
            {
                xTypedElement = null;
            }
            return(xTypedElement);
        }
        private XTypedElement TypeChildElement(XElement element, Dictionary <XName, System.Type> localElementsDict,
                                               ILinqToXsdTypeManager typeManager)
        {
            Type          clrType           = null;
            XTypedElement childTypedElement = null;

            if (localElementsDict.TryGetValue(element.Name, out clrType))
            {
                Type contentType = null;
                if (typeManager.RootContentTypeMapping.TryGetValue(clrType, out contentType))
                {
                    childTypedElement = XTypedServices.ToXTypedElement(element, typeManager, clrType, contentType);
                }
                else if (typeof(XTypedElement).IsAssignableFrom(clrType))
                {
                    //It can also be simple types
                    childTypedElement = XTypedServices.ToXTypedElement(element, typeManager, clrType);
                }
            }
            else
            {
                //Type not found, fall-back to auto-typing
                childTypedElement = XTypedServices.ToXTypedElement(element, typeManager);
            }

            return(childTypedElement);
        }
예제 #5
0
        public static W Load <W, T>(string uri, ILinqToXsdTypeManager typeManager)
            where W : XTypedElement
            where T : XTypedElement
        {
            XDocument doc = XDocument.Load(uri);

            return(XTypedServices.ToXTypedElement <W, T>(doc.Root, typeManager));
        }
예제 #6
0
        public static W Load <W, T>(string uri, ILinqToXsdTypeManager typeManager)
            where T : XTypedElement where W : XTypedElement
        {
            XDocument doc    = XDocument.Load(uri);
            XElement  xeroot = doc.Root;

            return(ToXTypedElement <W, T>(xeroot, typeManager));
        }
예제 #7
0
        public static W Load <W, T>(TextReader reader, ILinqToXsdTypeManager typeManager)
            where W : XTypedElement
            where T : XTypedElement
        {
            XDocument doc = XDocument.Load(reader);

            return(XTypedServices.ToXTypedElement <W, T>(doc.Root, typeManager));
        }
예제 #8
0
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager, System.Type t)
        {
            if (xe == null)
            {
                return(null);
            }

            if (!t.IsSubclassOf(typeof(XTypedElement)))
            {
                throw new InvalidOperationException("Type t is not a subtype of XTypedElement");
            }

            if (typeManager == null)
            {
                throw new ArgumentNullException("typeManager");
            }

            XTypedElement
                xoSubType = GetAnnotation(t,
                                          xe); //Try getting back as the type first, optimized for the cases where xsi:type cannot appear

            if (xoSubType == null)
            {
                //Try xsi:type and lookup in the typeDictionary
                Type clrType = GetXsiClrType(xe, typeManager);
                if (clrType != null)
                {
                    xoSubType = GetAnnotation(clrType, xe);
                    if (xoSubType != null)
                    {
                        return(xoSubType);
                    }

                    if (!t.IsAssignableFrom(clrType))
                    {
                        //xsi:type is not subtype of schema type
                        clrType = t;
                    }
                }
                else
                {
                    //xsi:type not present or CLRType not found for xsi:type name
                    clrType = t;
                }

                if (clrType.IsAbstract)
                {
                    throw new InvalidOperationException("Cannot cast XElement to an abstract type");
                }

                ConstructorInfo constrInfo = clrType.GetConstructor(System.Type.EmptyTypes);
                xoSubType         = (XTypedElement)constrInfo.Invoke(null);
                xoSubType.Untyped = xe;
                xe.AddAnnotation(new XTypedElementAnnotation(xoSubType));
            }

            return(xoSubType);
        }
예제 #9
0
        public static W Load <W, T>(System.IO.TextReader reader, ILinqToXsdTypeManager typeManager)
            where T : XTypedElement
            where W : XTypedElement
        {
            XDocument doc    = XDocument.Load(reader);
            XElement  xeroot = doc.Root;

            return(ToXTypedElement <W, T>(xeroot, typeManager));
        }
예제 #10
0
        internal static Type GetXsiClrType(XElement xe, ILinqToXsdTypeManager typeManager)
        {
            XName typeName = XTypedServices.GetXsiType(xe);
            Type  clrType  = null;

            if (typeName != null)
            {
                typeManager.GlobalTypeDictionary.TryGetValue(typeName, out clrType);
            }
            return(clrType);
        }
        public static XTypedSubstitutedList <T> Initialize(XTypedElement container, ILinqToXsdTypeManager typeManager, IEnumerable <T> typedObjects, params XName[] itemXNames)
        {
            XTypedSubstitutedList <T> typedList = new XTypedSubstitutedList <T>(container, typeManager, itemXNames);

            typedList.Clear();
            foreach (T typedItem in typedObjects)
            {
                typedList.Add(typedItem);
            }
            return(typedList);
        }
예제 #12
0
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager, Type t)
        {
            XTypedElement xTypedElement;

            if (xe != null)
            {
                if (!t.IsSubclassOf(typeof(XTypedElement)))
                {
                    throw new InvalidOperationException("Type t is not a subtype of XTypedElement");
                }
                if (typeManager == null)
                {
                    throw new ArgumentNullException("typeManager");
                }
                XTypedElement xoSubType = XTypedServices.GetAnnotation(t, xe);
                if (xoSubType == null)
                {
                    Type clrType = XTypedServices.GetXsiClrType(xe, typeManager);
                    if (!(clrType != null))
                    {
                        clrType = t;
                    }
                    else
                    {
                        xoSubType = XTypedServices.GetAnnotation(clrType, xe);
                        if (xoSubType != null)
                        {
                            xTypedElement = xoSubType;
                            return(xTypedElement);
                        }
                        if (!t.IsAssignableFrom(clrType))
                        {
                            clrType = t;
                        }
                    }
                    if (clrType.IsAbstract)
                    {
                        throw new InvalidOperationException("Cannot cast XElement to an abstract type");
                    }
                    ConstructorInfo constrInfo = clrType.GetConstructor(Type.EmptyTypes);
                    xoSubType         = (XTypedElement)constrInfo.Invoke(null);
                    xoSubType.Untyped = xe;
                    xe.AddAnnotation(new XTypedElementAnnotation(xoSubType));
                }
                xTypedElement = xoSubType;
            }
            else
            {
                xTypedElement = null;
            }
            return(xTypedElement);
        }
예제 #13
0
        internal static System.Type GetXsiClrType(XElement xe, ILinqToXsdTypeManager typeManager)
        {
            XName typeName = GetXsiType(xe);
            Type  clrType  = null;

            if (typeName != null)
            {
                Dictionary <XName, Type> typeDictionary = typeManager.GlobalTypeDictionary;
                typeDictionary.TryGetValue(typeName, out clrType);
            }

            return(clrType);
        }
예제 #14
0
        public static XTypedElement ToSubstitutedXTypedElement(XTypedElement parentType,
                                                               ILinqToXsdTypeManager typeManager, params XName[] substitutedMembers)
        {
            XElement substElement  = null;
            XElement parentElement = parentType.Untyped;
            int      index         = 0;

            while (substElement == null && index < substitutedMembers.Length)
            {
                substElement = parentType.GetElement(substitutedMembers[index++]);
            }

            if (substElement != null)
            {
                return(XTypedServices.ToXTypedElement(substElement, typeManager));
            }

            return(null);
        }
예제 #15
0
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager, Type rootType, Type contentType)
        {
            XTypedElement rootElement = XTypedServices.GetAnnotation(rootType, xe);

            if (rootElement == null)
            {
                XName         instanceElementName = xe.Name;
                XTypedElement innerType           = XTypedServices.ToXTypedElement(xe, typeManager, contentType);
                Debug.Assert(innerType != null);
                ConstructorInfo constInfo = rootType.GetConstructor(new Type[] { contentType });
                if (!(constInfo != null))
                {
                    throw new LinqToXsdException(string.Concat(contentType.ToString(), " is not an expected content type for root element type ", rootType.ToString()));
                }
                object[] objArray = new object[] { innerType };
                rootElement = (XTypedElement)constInfo.Invoke(objArray);
                if (!XTypedServices.TypeValid(rootElement, instanceElementName))
                {
                    throw new LinqToXsdException(string.Concat("Element is not an instance of type ", rootType));
                }
            }
            return(rootElement);
        }
        //XML Query axes
        IEnumerable <T> IXTyped.Descendants <T>()
        {
            XTypedElement currentObject = this as XTypedElement;
            Type          lookupType    = typeof(T);

            //Metadata
            IXMetaData schemaMetaData = currentObject as IXMetaData;
            Dictionary <XName, System.Type> localElementsDict = null;
            ILinqToXsdTypeManager           typeManager       = schemaMetaData.TypeManager;
            Dictionary <XName, Type>        typeDictionary    = typeManager.GlobalTypeDictionary;

            //FSM
            XName    matchingName     = null;
            WildCard matchingWildCard = null;
            int      currentState     = FSM.InvalidState;

            XElement parentElement             = null;
            Stack <XTypedElement> elementStack = new Stack <XTypedElement>();

            while (true)
            {
                schemaMetaData = currentObject as IXMetaData;
                FSM fsm = currentObject.ValidationStates;
                if (fsm != null)
                {
                    StartFsm();
                    currentState = fsm.Start;
                }

                Debug.Assert(schemaMetaData != null);
                localElementsDict = schemaMetaData.LocalElementsDictionary;
                parentElement     = currentObject.Untyped;

                matchingName     = null;
                matchingWildCard = null;

                XTypedElement childObject  = null;
                bool          validContent = true;

                foreach (XElement childElement in parentElement.Elements())
                {
                    bool isTypeT = IsAnnoatedElemTypeOf <T>(childElement, out childObject);

                    if (fsm != null)
                    {
                        //Always execute FSM no matter whether we find an element of type T
                        currentState = FsmMakeTransition(currentState, childElement.Name, out matchingName,
                                                         out matchingWildCard);
                        if (currentState == FSM.InvalidState)
                        {
                            validContent = false;
                            break;
                        }
                    }

                    if (!isTypeT)
                    {
                        //check dictionary
                        if (fsm != null && matchingWildCard != null)
                        {
                            childObject = XTypedServices.ToXTypedElement(childElement, typeManager); //Auto-typing
                        }
                        else
                        {
                            childObject = TypeChildElement(childElement, localElementsDict, typeManager);
                        }

                        if (childObject != null)
                        {
                            Type runtimeType = childObject.GetType();
                            if (lookupType.IsAssignableFrom(runtimeType))
                            {
                                isTypeT = true;
                            }
                            else
                            {
                                //Check content type
                                Type contentType = null;
                                if (typeManager.RootContentTypeMapping.TryGetValue(runtimeType, out contentType) &&
                                    lookupType.IsAssignableFrom(contentType))
                                {
                                    childObject = GetContentType(childObject);
                                    isTypeT     = true;
                                }
                            }
                        }
                    }

                    if (isTypeT)
                    {
                        yield return((T)childObject);
                    }

                    if (childObject != null)
                    {
                        elementStack.Push(childObject);
                    }
                }

                if (validContent && elementStack.Count > 0)
                {
                    currentObject = elementStack.Pop();
                }
                else
                {
                    break;
                }
            }
        }
예제 #17
0
        public static XTypedElement ToXTypedElement(XElement xe, ILinqToXsdTypeManager typeManager, System.Type t)
        {
            if (xe == null)
            {
                return(null);
            }

            if (!t.IsSubclassOf(typeof(XTypedElement)))
            {
                throw new InvalidOperationException("Type t is not a subtype of XTypedElement");
            }

            if (typeManager == null)
            {
                throw new ArgumentNullException("typeManager");
            }

            //Try getting back as the type first, optimized for the cases where xsi:type cannot appear
            XTypedElement xoSubType = GetAnnotation(t, xe);

            if (xoSubType == null)
            {
                //Try xsi:type and lookup in the typeDictionary
                Type clrType = GetXsiClrType(xe, typeManager);
                if (clrType != null)
                {
                    xoSubType = GetAnnotation(clrType, xe);
                    if (xoSubType != null)
                    {
                        return(xoSubType);
                    }

                    if (!t.IsAssignableFrom(clrType))
                    {
                        //xsi:type is not subtype of schema type
                        clrType = t;
                    }
                }
                else
                {
                    //xsi:type not present or CLRType not found for xsi:type name
                    clrType = t;
                }

                if (clrType.IsAbstract)
                {
                    throw new InvalidOperationException("Cannot cast XElement to an abstract type");
                }

                // get public or instance
                var             exactBinding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                ConstructorInfo constrInfo   = clrType.GetConstructor(exactBinding, null, Type.EmptyTypes, null);
                if (constrInfo == null)
                {
                    throw new TypeAccessException($"There is no public/internal parameter-less constructor for type:  {clrType.FullName}");
                }

                xoSubType         = (XTypedElement)constrInfo.Invoke(null);
                xoSubType.Untyped = xe;
                xe.AddAnnotation(new XTypedElementAnnotation(xoSubType));
            }

            return(xoSubType);
        }
예제 #18
0
 public static T ToXTypedElement <T>(XElement xe, ILinqToXsdTypeManager typeManager)
     where T : XTypedElement
 {
     return((T)(XTypedServices.ToXTypedElement(xe, typeManager, typeof(T)) as T));
 }
예제 #19
0
 public static W ToXTypedElement <W, T>(XElement xe, ILinqToXsdTypeManager typeManager)
     where W : XTypedElement
     where T : XTypedElement
 {
     return((W)XTypedServices.ToXTypedElement(xe, typeManager, typeof(W), typeof(T)));
 }
예제 #20
0
        public static XTypedElement ToSubstitutedXTypedElement(XTypedElement parentType, ILinqToXsdTypeManager typeManager, params XName[] substitutedMembers)
        {
            XTypedElement xTypedElement;
            XElement      substElement  = null;
            XElement      parentElement = parentType.Untyped;
            int           index         = 0;

            while (true)
            {
                if ((substElement != null ? true : index >= (int)substitutedMembers.Length))
                {
                    break;
                }
                int num = index;
                index        = num + 1;
                substElement = parentType.GetElement(substitutedMembers[num]);
            }
            if (substElement == null)
            {
                xTypedElement = null;
            }
            else
            {
                xTypedElement = XTypedServices.ToXTypedElement(substElement, typeManager);
            }
            return(xTypedElement);
        }
예제 #21
0
 public static W Parse <W, T>(string xml, ILinqToXsdTypeManager typeManager)
     where W : XTypedElement
     where T : XTypedElement
 {
     return(XTypedServices.ToXTypedElement <W, T>(XElement.Parse(xml), typeManager));
 }
예제 #22
0
        private XTypedElement TypeChildElement(XElement element, Dictionary <XName, Type> localElementsDict, ILinqToXsdTypeManager typeManager)
        {
            Type          clrType           = null;
            XTypedElement childTypedElement = null;

            if (!localElementsDict.TryGetValue(element.Name, out clrType))
            {
                childTypedElement = XTypedServices.ToXTypedElement(element, typeManager);
            }
            else
            {
                Type contentType = null;
                if (typeManager.RootContentTypeMapping.TryGetValue(clrType, out contentType))
                {
                    childTypedElement = XTypedServices.ToXTypedElement(element, typeManager, clrType, contentType);
                }
                else if (typeof(XTypedElement).IsAssignableFrom(clrType))
                {
                    childTypedElement = XTypedServices.ToXTypedElement(element, typeManager, clrType);
                }
            }
            return(childTypedElement);
        }
예제 #23
0
 public static XTypedList <T> CopyFromWithValidation(IEnumerable <T> typedObjects, XTypedElement container,
                                                     XName itemXName, ILinqToXsdTypeManager typeManager, string propertyName, SimpleTypeValidator typeDef)
 {
     return(Initialize(container, typeManager, typedObjects, itemXName));
 }
예제 #24
0
 public XTypedList(XTypedElement container, ILinqToXsdTypeManager typeManager, XName itemXName) : base(container,
                                                                                                       itemXName)
 {
     this.typeManager = typeManager;
 }
예제 #25
0
        IEnumerable <T> Xml.Schema.Linq.IXTyped.Descendants <T>()
        {
            bool                     flag;
            bool                     flag1;
            bool                     flag2;
            XTypedElement            xTypedElement           = this;
            Type                     type                    = typeof(T);
            IXMetaData               xMetaDatum              = xTypedElement;
            Dictionary <XName, Type> localElementsDictionary = null;
            ILinqToXsdTypeManager    linqToXsdTypeManager    = xMetaDatum.TypeManager;
            Dictionary <XName, Type> globalTypeDictionary    = linqToXsdTypeManager.GlobalTypeDictionary;
            XName                    xName                   = null;
            WildCard                 wildCard                = null;
            int      invalidState = FSM.InvalidState;
            XElement untyped      = null;
            Stack <XTypedElement> xTypedElements = new Stack <XTypedElement>();

            while (true)
            {
                xMetaDatum = xTypedElement;
                FSM validationStates = xTypedElement.ValidationStates;
                if (validationStates != null)
                {
                    this.StartFsm();
                    invalidState = validationStates.Start;
                }
                Debug.Assert(xMetaDatum != null);
                localElementsDictionary = xMetaDatum.LocalElementsDictionary;
                untyped  = xTypedElement.Untyped;
                xName    = null;
                wildCard = null;
                XTypedElement          xTypedElement1 = null;
                bool                   flag3          = true;
                IEnumerator <XElement> enumerator     = untyped.Elements().GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        XElement current = enumerator.Current;
                        bool     flag4   = this.IsAnnoatedElemTypeOf <T>(current, out xTypedElement1);
                        if (validationStates != null)
                        {
                            invalidState = this.FsmMakeTransition(invalidState, current.Name, out xName, out wildCard);
                            if (invalidState == FSM.InvalidState)
                            {
                                goto Label0;
                            }
                        }
                        if (!flag4)
                        {
                            flag           = (validationStates == null ? true : wildCard == null);
                            xTypedElement1 = (flag ? this.TypeChildElement(current, localElementsDictionary, linqToXsdTypeManager) : XTypedServices.ToXTypedElement(current, linqToXsdTypeManager));
                            if (xTypedElement1 != null)
                            {
                                Type type1 = xTypedElement1.GetType();
                                if (!type.IsAssignableFrom(type1))
                                {
                                    Type type2 = null;
                                    flag1 = (!linqToXsdTypeManager.RootContentTypeMapping.TryGetValue(type1, out type2) ? true : !type.IsAssignableFrom(type2));
                                    if (!flag1)
                                    {
                                        xTypedElement1 = this.GetContentType(xTypedElement1);
                                        flag4          = true;
                                    }
                                }
                                else
                                {
                                    flag4 = true;
                                }
                            }
                        }
                        if (flag4)
                        {
                            yield return((T)xTypedElement1);
                        }
                        if (xTypedElement1 != null)
                        {
                            xTypedElements.Push(xTypedElement1);
                        }
                    }
                    goto Label1;
Label0:
                    flag3 = false;
                }
                finally
                {
                    if (enumerator != null)
                    {
                        enumerator.Dispose();
                    }
                }
Label1:
                flag2 = (!flag3 ? true : xTypedElements.Count <= 0);
                if (flag2)
                {
                    break;
                }
                xTypedElement = xTypedElements.Pop();
            }
        }
 public XTypedSubstitutedList(XTypedElement container, ILinqToXsdTypeManager typeManager, params XName[] itemXNames) : base(container, itemXNames)
 {
     this.typeManager = typeManager;
 }