internal static XTypedElement GetAnnotation(Type t, XElement xe) { XTypedElement xTypedElement; XTypedElementWrapperAnnotation xoWrapperAnnotation = xe.Annotation <XTypedElementWrapperAnnotation>(); XTypedElement xObj = null; if (xoWrapperAnnotation != null) { xObj = xoWrapperAnnotation.typedElement; if (t.IsAssignableFrom(xObj.GetType())) { xTypedElement = xObj; return(xTypedElement); } } XTypedElementAnnotation xoAnnotation = xe.Annotation <XTypedElementAnnotation>(); if (xoAnnotation != null) { xObj = xoAnnotation.typedElement; if (t.IsAssignableFrom(xObj.GetType())) { xTypedElement = xObj; return(xTypedElement); } } xTypedElement = null; return(xTypedElement); }
internal static XTypedElement GetAnnotation(Type t, XElement xe) { XTypedElementWrapperAnnotation xoWrapperAnnotation = xe.Annotation <XTypedElementWrapperAnnotation>(); XTypedElement xObj = null; if (xoWrapperAnnotation != null) { //Return the root type if the element is annotated with the root xObj = xoWrapperAnnotation.typedElement; if (t.IsAssignableFrom(xObj.GetType())) { //Check if we are asking for element wrapper return(xObj); } } XTypedElementAnnotation xoAnnotation = xe.Annotation <XTypedElementAnnotation>(); if (xoAnnotation != null) { xObj = xoAnnotation.typedElement; if (t.IsAssignableFrom(xObj.GetType())) { //Check if we are asking for type return(xObj); } } return(null); }
internal static XElement GetXElement(XTypedElement xObj, XName name, Type elementBaseType) { XElement newElement = xObj.Untyped; if (newElement.Parent != null) { //Element/XTypedElement already added to the tree, need to clone XTypedElement newElement = xObj.Clone().Untyped; } IXMetaData metaData = xObj as IXMetaData; Debug.Assert(metaData != null); if (metaData.TypeOrigin == SchemaOrigin.Fragment) { //Set correct element name as the name of the property/element newElement.Name = name; } //Does this need a type qualifier? if (xObj.GetType() != elementBaseType) { //Don't overwrite anything explicitly added var xsiType = (string)newElement.Attribute(XName.Get("type", XmlSchema.InstanceNamespace)); if (xsiType == null) { var defNs = xObj.Untyped.GetDefaultNamespace(); string typeName = null; if (metaData.SchemaName.Namespace == defNs || (defNs == XNamespace.None && metaData.SchemaName.Namespace == name.Namespace)) { typeName = metaData.SchemaName.LocalName; } else { var prefix = xObj.Untyped.GetPrefixOfNamespace(metaData.SchemaName.Namespace); if (prefix == null) { typeName = metaData.SchemaName.LocalName; } else { typeName = prefix + ":" + metaData.SchemaName.LocalName; } } newElement.Add(new XAttribute(XName.Get("type", XmlSchema.InstanceNamespace), typeName)); } } return(newElement); }
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(); } }
//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; } } }
internal static XElement GetXElement(XTypedElement xObj, XName name) { return(GetXElement(xObj, name, xObj.GetType())); }