void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors) { var meta = dictionaryAdapter.Meta; if (meta.MetaInitializers.OfType<XPathBehavior>().FirstOrDefault() == null) { throw new InvalidOperationException(string.Format( "Interface {0} requested xpath support, but was not configured properly. " + "Did you forget to add an XPathBehavior?", meta.Type.FullName)); } var xmlMeta = dictionaryAdapter.GetXmlMeta(); if (dictionaryAdapter.This.CreateStrategy == null) { dictionaryAdapter.This.CreateStrategy = this; dictionaryAdapter.This.AddCopyStrategy(this); } if (rootXmlMeta == null) { rootXmlMeta = xmlMeta; Context.ApplyBehaviors(rootXmlMeta, behaviors); if (Parent == null) { foreach (var behavior in behaviors) { if (behavior is XPathAttribute) { var attrib = (XPathAttribute)behavior; var compiledExpression = attrib.CompiledExpression; if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root)) { break; } var navigator = Context.SelectSingleNode(compiledExpression, root); if (navigator != null) { root = navigator; break; } } } MoveOffRoot(root, XPathNodeType.Element); } } else { if (overlays == null) overlays = new Dictionary<Type, XPathContext>(); XPathContext overlay; if (overlays.TryGetValue(meta.Type, out overlay) == false) { overlay = new XPathContext().ApplyBehaviors(xmlMeta, behaviors); overlays.Add(meta.Type, overlay); } } }
private object ReadArray(XPathResult result, IDictionaryAdapter dictionaryAdapter) { var itemType = result.Type.GetElementType(); var items = result.GetNodes(itemType, type => dictionaryAdapter.GetXmlMeta(type)) .Select(node => ReadProperty(node, false, dictionaryAdapter)).ToArray(); var array = Array.CreateInstance(itemType, items.Length); items.CopyTo(array, 0); return(array); }
public static Type GetXmlSubclass(this IDictionaryAdapter dictionaryAdapter, XmlQualifiedName xmlType, Type otherType) { if (xmlType == null) { return(null); } var xmlIncludes = dictionaryAdapter.GetXmlMeta(otherType).XmlIncludes; if (xmlIncludes != null) { var subClass = from xmlInclude in xmlIncludes let xmlIncludeType = dictionaryAdapter.GetXmlMeta(xmlInclude).XmlType where xmlIncludeType.TypeName == xmlType.Name && xmlIncludeType.Namespace == xmlType.Namespace select xmlInclude; return(subClass.FirstOrDefault()); } return(null); }
private void WriteList(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter) { var arguments = result.Type.GetGenericArguments(); var itemType = arguments[0]; foreach (var item in (IEnumerable)value) { var node = result.CreateNode(itemType, item, type => dictionaryAdapter.GetXmlMeta(type)); WriteProperty(node, item, dictionaryAdapter); } }
private void WriteArray(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter) { var array = (Array)value; var itemType = array.GetType().GetElementType(); foreach (var item in array) { var node = result.CreateNode(itemType, item, type => dictionaryAdapter.GetXmlMeta(type)); WriteProperty(node, item, dictionaryAdapter); } }
void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors) { var meta = dictionaryAdapter.Meta; if (meta.MetaInitializers.OfType <XPathBehavior>().FirstOrDefault() == null) { throw new InvalidOperationException(string.Format( "Interface {0} requested xpath support, but was not configured properly. " + "Did you forget to add an XPathBehavior?", meta.Type.FullName)); } dictionaryAdapter.This.CreateStrategy = this; Context.ApplyBehaviors(behaviors); xmlMeta = dictionaryAdapter.GetXmlMeta(); if (string.IsNullOrEmpty(xmlMeta.XmlType.Namespace) == false) { Context.AddNamespace(string.Empty, xmlMeta.XmlType.Namespace); } if (Parent == null) { foreach (var behavior in behaviors) { if (behavior is XPathAttribute) { var attrib = (XPathAttribute)behavior; var compiledExpression = attrib.CompiledExpression; if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root)) { break; } var navigator = Context.SelectSingleNode(compiledExpression, root); if (navigator != null) { root = navigator; break; } } } MoveOffRoot(root, XPathNodeType.Element); } }
void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors) { var meta = dictionaryAdapter.Meta; if (meta.MetaInitializers.OfType<XPathBehavior>().FirstOrDefault() == null) { throw new InvalidOperationException(string.Format( "Interface {0} requested xpath support, but was not configured properly. " + "Did you forget to add an XPathBehavior?", meta.Type.FullName)); } dictionaryAdapter.This.CreateStrategy = this; Context.ApplyBehaviors(behaviors); xmlMeta = dictionaryAdapter.GetXmlMeta(); if (string.IsNullOrEmpty(xmlMeta.XmlType.Namespace) == false) { Context.AddNamespace(string.Empty, xmlMeta.XmlType.Namespace); } if (Parent == null) { foreach (var behavior in behaviors) { if (behavior is XPathAttribute) { var attrib = (XPathAttribute)behavior; var compiledExpression = attrib.CompiledExpression; if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root)) { break; } var navigator = Context.SelectSingleNode(compiledExpression, root); if (navigator != null) { root = navigator; break; } } } MoveOffRoot(root, XPathNodeType.Element); } }
private object ReadArray(XPathResult result, IDictionaryAdapter dictionaryAdapter) { if (result.Type == typeof(byte[])) { XPathNavigator node; if (result.GetNavigator(false, true, out node) && node != null) { return(Convert.FromBase64String(node.InnerXml)); } return(null); } var itemType = result.Type.GetElementType(); var itemNodes = result.GetNodes(itemType, type => dictionaryAdapter.GetXmlMeta(type)); if (itemNodes == null) { return(null); } var items = itemNodes.Select(node => ReadProperty(node, false, dictionaryAdapter)).ToArray(); var array = Array.CreateInstance(itemType, items.Length); items.CopyTo(array, 0); return(array); }
private XPathResult EvaluateProperty(string key, PropertyDescriptor property, IDictionaryAdapter dictionaryAdapter) { object result; XPathExpression xpath = null; object matchingBehavior = null; Func<XPathNavigator> create = null; var context = GetEffectiveContext(dictionaryAdapter); var xmlMeta = dictionaryAdapter.GetXmlMeta(property.Property.DeclaringType); var keyContext = context.CreateChild(xmlMeta, property.Behaviors); foreach (var behavior in property.Behaviors) { string name = key, ns = null; Func<XPathNavigator> matchingCreate = null; if (behavior is XmlElementAttribute) { xpath = XPathElement; var attrib = (XmlElementAttribute)behavior; if (string.IsNullOrEmpty(attrib.ElementName) == false) name = attrib.ElementName; if (string.IsNullOrEmpty(attrib.Namespace) == false) ns = attrib.Namespace; matchingCreate = () => keyContext.AppendElement(name, ns, root.Clone()); } else if (behavior is XmlAttributeAttribute) { xpath = XPathAttribute; var attrib = (XmlAttributeAttribute)behavior; if (string.IsNullOrEmpty(attrib.AttributeName) == false) name = attrib.AttributeName; if (string.IsNullOrEmpty(attrib.Namespace) == false) ns = attrib.Namespace; matchingCreate = () => keyContext.CreateAttribute(name, ns, root.Clone()); } else if (behavior is XmlArrayAttribute) { xpath = XPathElement; var attrib = (XmlArrayAttribute)behavior; if (string.IsNullOrEmpty(attrib.ElementName) == false) name = attrib.ElementName; if (string.IsNullOrEmpty(attrib.Namespace) == false) ns = attrib.Namespace; matchingCreate = () => keyContext.AppendElement(name, ns, root.Clone()); } else if (behavior is XPathAttribute) { var attrib = (XPathAttribute)behavior; xpath = attrib.CompiledExpression; } else { continue; } if (xpath != null) { keyContext.Arguments.Clear(); keyContext.Arguments.AddParam("key", "", name); keyContext.Arguments.AddParam("ns", "", ns ?? XPathContext.IgnoreNamespace); if (keyContext.Evaluate(xpath, Root, out result)) { create = matchingCreate ?? create; return new XPathResult(property, key, result, keyContext, behavior, create); } } matchingBehavior = matchingBehavior ?? behavior; create = create ?? matchingCreate; } if (xpath != null) return new XPathResult(property, key, null, keyContext, matchingBehavior, create); keyContext.Arguments.Clear(); keyContext.Arguments.AddParam("key", "", key); keyContext.Arguments.AddParam("ns", "", XPathContext.IgnoreNamespace); create = create ?? (() => keyContext.AppendElement(key, null, root)); keyContext.Evaluate(XPathElementOrAttribute, Root, out result); return new XPathResult(property, key, result, keyContext, null, create); }
private void WriteArray(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter) { var array = (Array)value; var itemType = array.GetType().GetElementType(); foreach (var item in array) { var element = item; var node = result.CreateNode(itemType, element, type => dictionaryAdapter.GetXmlMeta(type)); WriteProperty(node, ref element, dictionaryAdapter); } }
private void WriteList(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter) { var arguments = result.Type.GetGenericArguments(); var itemType = arguments[0]; foreach (var item in (IEnumerable)value) { var element = item; var node = result.CreateNode(itemType, element, type => dictionaryAdapter.GetXmlMeta(type)); WriteProperty(node, ref element, dictionaryAdapter); } }
private object ReadArray(XPathResult result, IDictionaryAdapter dictionaryAdapter) { if (result.Type == typeof(byte[])) { XPathNavigator node; if (result.GetNavigator(false, true, out node) && node != null) { return Convert.FromBase64String(node.InnerXml); } return null; } var itemType = result.Type.GetElementType(); var itemNodes = result.GetNodes(itemType, type => dictionaryAdapter.GetXmlMeta(type)); if (itemNodes == null) return null; var items = itemNodes.Select(node => ReadProperty(node, false, dictionaryAdapter)).ToArray(); var array = Array.CreateInstance(itemType, items.Length); items.CopyTo(array, 0); return array; }
private object ReadList(XPathResult result, IDictionaryAdapter dictionaryAdapter) { Type listType = null, initializerType = null; var arguments = result.Type.GetGenericArguments(); var genericDef = result.Type.GetGenericTypeDefinition(); var itemType = arguments[0]; Func<Type, XmlMetadata> getXmlMeta = type => dictionaryAdapter.GetXmlMeta(type); var itemNodes = result.GetNodes(itemType, getXmlMeta); if (itemNodes == null) return null; if (genericDef == typeof(IEnumerable<>) || genericDef == typeof(ICollection<>) || genericDef == typeof(List<>)) { listType = typeof(EditableList<>).MakeGenericType(arguments); } else if ( #if !DOTNET35 //NOTE: what about SortedSet? genericDef == typeof(ISet<>) || #endif genericDef == typeof(HashSet<>)) { listType = typeof(List<>).MakeGenericType(arguments); } else { listType = typeof(EditableBindingList<>).MakeGenericType(arguments); initializerType = typeof(BindingListInitializer<>).MakeGenericType(arguments); } var list = (IList)Activator.CreateInstance(listType); foreach (var item in itemNodes) { list.Add(ReadProperty(item, false, dictionaryAdapter)); } if ( #if !DOTNET35 //NOTE: what about SortedSet? genericDef == typeof(ISet<>) || #endif genericDef == typeof(HashSet<>)) { return Activator.CreateInstance(typeof(HashSet<>).MakeGenericType(arguments), list); } if (initializerType != null) { Func<object> addNew = () => { var node = result.CreateNode(itemType, null, getXmlMeta); return ReadProperty(node, false, dictionaryAdapter); }; Func<int, object, object> addAt = (index, item) => { var node = result.CreateNode(itemType, item, getXmlMeta); WriteProperty(node, ref item, dictionaryAdapter); return item; }; Func<int, object, object> setAt = (index, item) => { var node = result.GetNodeAt(itemType, index); WriteProperty(node, ref item, dictionaryAdapter); return item; }; Action<int> removeAt = index => { object value = list; if (dictionaryAdapter.ShouldClearProperty(result.Property, value)) { result.Remove(true); return; } result.RemoveAt(index); }; Action reset = () => { object value = list; if (dictionaryAdapter.ShouldClearProperty(result.Property, value)) { result.Remove(true); return; } WriteCollection(result, ref value, dictionaryAdapter); }; var initializer = (IValueInitializer)Activator.CreateInstance( initializerType, addAt, addNew, setAt, removeAt, reset); initializer.Initialize(dictionaryAdapter, list); } return list; }
private object ReadArray(XPathResult result, IDictionaryAdapter dictionaryAdapter) { var itemType = result.Type.GetElementType(); var itemNodes = result.GetNodes(itemType, type => dictionaryAdapter.GetXmlMeta(type)); if (itemNodes == null) return null; var items = itemNodes.Select(node => ReadProperty(node, false, dictionaryAdapter)).ToArray(); var array = Array.CreateInstance(itemType, items.Length); items.CopyTo(array, 0); return array; }
private object ReadList(XPathResult result, IDictionaryAdapter dictionaryAdapter) { Type listType = null, initializerType = null; var arguments = result.Type.GetGenericArguments(); var genericDef = result.Type.GetGenericTypeDefinition(); var itemType = arguments[0]; if (genericDef == typeof(List<>)) { listType = typeof(EditableList<>).MakeGenericType(arguments); } else { listType = typeof(EditableBindingList<>).MakeGenericType(arguments); initializerType = typeof(BindingListInitializer<>).MakeGenericType(arguments); } var list = (IList)Activator.CreateInstance(listType); Func<Type, XmlMetadata> getXmlMeta = type => dictionaryAdapter.GetXmlMeta(type); foreach (var item in result.GetNodes(itemType, getXmlMeta)) { list.Add(ReadProperty(item, false, dictionaryAdapter)); } if (initializerType != null) { Func<object> addNew = () => { var node = result.CreateNode(itemType, null, getXmlMeta); return ReadProperty(node, false, dictionaryAdapter); }; Action<int, object> addAt = (index, item) => { var node = result.CreateNode(itemType, item, getXmlMeta); WriteProperty(node, item, dictionaryAdapter); }; Action<int, object> setAt = (index, item) => { var node = result.GetNodeAt(itemType, index); WriteProperty(node, item, dictionaryAdapter); }; Action<int> removeAt = index => result.RemoveAt(index); var initializer = (IValueInitializer)Activator.CreateInstance( initializerType, addAt, addNew, setAt, removeAt); initializer.Initialize(dictionaryAdapter, list); } return list; }
void IDictionaryInitializer.Initialize(IDictionaryAdapter dictionaryAdapter, object[] behaviors) { var meta = dictionaryAdapter.Meta; if (meta.MetaInitializers.OfType <XPathBehavior>().FirstOrDefault() == null) { throw new InvalidOperationException(string.Format( "Interface {0} requested xpath support, but was not configured properly. " + "Did you forget to add an XPathBehavior?", meta.Type.FullName)); } var xmlMeta = dictionaryAdapter.GetXmlMeta(); if (dictionaryAdapter.This.CreateStrategy == null) { dictionaryAdapter.This.CreateStrategy = this; dictionaryAdapter.This.AddCopyStrategy(this); } if (rootXmlMeta == null) { rootXmlMeta = xmlMeta; Context.ApplyBehaviors(rootXmlMeta, behaviors); if (Parent == null) { foreach (var behavior in behaviors) { if (behavior is XPathAttribute) { var attrib = (XPathAttribute)behavior; var compiledExpression = attrib.CompiledExpression; if (MoveOffRoot(root, XPathNodeType.Element) == false || Context.Matches(compiledExpression, root)) { break; } var navigator = Context.SelectSingleNode(compiledExpression, root); if (navigator != null) { root = navigator; break; } } } MoveOffRoot(root, XPathNodeType.Element); } } else { if (overlays == null) { overlays = new Dictionary <Type, XPathContext>(); } XPathContext overlay; if (overlays.TryGetValue(meta.Type, out overlay) == false) { overlay = new XPathContext().ApplyBehaviors(xmlMeta, behaviors); overlays.Add(meta.Type, overlay); } } }
private XPathResult EvaluateProperty(string key, PropertyDescriptor property, IDictionaryAdapter dictionaryAdapter) { object result; XPathExpression xpath = null; object matchingBehavior = null; Func <XPathNavigator> create = null; var xmlMeta = dictionaryAdapter.GetXmlMeta(property.Property.DeclaringType); var keyContext = Context.CreateChild(xmlMeta, property.Behaviors); foreach (var behavior in property.Behaviors) { string name = key, ns = null; Func <XPathNavigator> matchingCreate = null; if (behavior is XmlElementAttribute) { xpath = XPathElement; var node = root.Clone(); var attrib = (XmlElementAttribute)behavior; if (string.IsNullOrEmpty(attrib.ElementName) == false) { name = attrib.ElementName; } if (string.IsNullOrEmpty(attrib.Namespace) == false) { ns = attrib.Namespace; } matchingCreate = () => keyContext.AppendElement(name, ns, node); } else if (behavior is XmlAttributeAttribute) { xpath = XPathAttribute; var node = root.Clone(); var attrib = (XmlAttributeAttribute)behavior; if (string.IsNullOrEmpty(attrib.AttributeName) == false) { name = attrib.AttributeName; } if (string.IsNullOrEmpty(attrib.Namespace) == false) { ns = attrib.Namespace; } matchingCreate = () => keyContext.CreateAttribute(name, ns, node); } else if (behavior is XmlArrayAttribute) { xpath = XPathElement; var node = root.Clone(); var attrib = (XmlArrayAttribute)behavior; if (string.IsNullOrEmpty(attrib.ElementName) == false) { name = attrib.ElementName; } if (string.IsNullOrEmpty(attrib.Namespace) == false) { ns = attrib.Namespace; } matchingCreate = () => keyContext.AppendElement(name, ns, node); } else if (behavior is XPathAttribute) { var attrib = (XPathAttribute)behavior; xpath = attrib.CompiledExpression; } else { continue; } if (xpath != null) { keyContext.Arguments.Clear(); keyContext.Arguments.AddParam("key", "", name); keyContext.Arguments.AddParam("ns", "", ns ?? XPathContext.IgnoreNamespace); if (keyContext.Evaluate(xpath, root, out result)) { create = matchingCreate ?? create; return(new XPathResult(property, result, keyContext, behavior, create)); } } matchingBehavior = matchingBehavior ?? behavior; create = create ?? matchingCreate; } if (xpath != null) { return(new XPathResult(property, null, keyContext, matchingBehavior, create)); } keyContext.Arguments.Clear(); keyContext.Arguments.AddParam("key", "", key); keyContext.Arguments.AddParam("ns", "", XPathContext.IgnoreNamespace); create = create ?? (() => keyContext.AppendElement(key, null, root)); keyContext.Evaluate(XPathElementOrAttribute, Root, out result); return(new XPathResult(property, result, keyContext, null, create)); }
private object ReadList(XPathResult result, IDictionaryAdapter dictionaryAdapter) { Type listType = null, initializerType = null; var arguments = result.Type.GetGenericArguments(); var genericDef = result.Type.GetGenericTypeDefinition(); var itemType = arguments[0]; Func <Type, XmlMetadata> getXmlMeta = type => dictionaryAdapter.GetXmlMeta(type); var itemNodes = result.GetNodes(itemType, getXmlMeta); if (itemNodes == null) { return(null); } if (genericDef == typeof(IEnumerable <>) || genericDef == typeof(ICollection <>) || genericDef == typeof(List <>)) { listType = typeof(EditableList <>).MakeGenericType(arguments); } else if ( #if !DOTNET35 //NOTE: what about SortedSet? genericDef == typeof(ISet <>) || #endif genericDef == typeof(HashSet <>)) { listType = typeof(List <>).MakeGenericType(arguments); } else { listType = typeof(EditableBindingList <>).MakeGenericType(arguments); initializerType = typeof(BindingListInitializer <>).MakeGenericType(arguments); } var list = (IList)Activator.CreateInstance(listType); foreach (var item in itemNodes) { list.Add(ReadProperty(item, false, dictionaryAdapter)); } if ( #if !DOTNET35 //NOTE: what about SortedSet? genericDef == typeof(ISet <>) || #endif genericDef == typeof(HashSet <>)) { return(Activator.CreateInstance(typeof(HashSet <>).MakeGenericType(arguments), list)); } if (initializerType != null) { Func <object> addNew = () => { var node = result.CreateNode(itemType, null, getXmlMeta); return(ReadProperty(node, false, dictionaryAdapter)); }; Action <int, object> addAt = (index, item) => { var node = result.CreateNode(itemType, item, getXmlMeta); WriteProperty(node, item, dictionaryAdapter); }; Action <int, object> setAt = (index, item) => { var node = result.GetNodeAt(itemType, index); WriteProperty(node, item, dictionaryAdapter); }; Action <int> removeAt = index => result.RemoveAt(index); var initializer = (IValueInitializer)Activator.CreateInstance( initializerType, addAt, addNew, setAt, removeAt); initializer.Initialize(dictionaryAdapter, list); } return(list); }