public bool WriteObject(XPathResult result, XPathNavigator node, object value)
		{
			var xml = new StringBuilder();
			var settings = new XmlWriterSettings
			{
				OmitXmlDeclaration = true,
				Indent = false
			};

			using (var writer = XmlWriter.Create(xml, settings))
			{
				if (value is IXmlSerializable)
				{
					SerializeCustom(writer, result, node, (IXmlSerializable)value);
				}
				else
				{
					Serialize(writer, result, node, value);
				}
				writer.Flush();
			}

			node.ReplaceSelf(xml.ToString());
			return true;
		}
		public bool WriteObject(XPathResult result, XPathNavigator node, object value)
		{
			var rootOverride = new XmlRootAttribute(node.LocalName)
			{
				Namespace = node.NamespaceURI
			};

			var xml = new StringBuilder();
			var settings = new XmlWriterSettings
			{
				OmitXmlDeclaration = true,
				Indent = false
			};
			var namespaces = new XmlSerializerNamespaces();
			namespaces.Add(string.Empty, string.Empty);
			if (string.IsNullOrEmpty(node.NamespaceURI) == false)
			{
				var prefix = result.Context.AddNamespace(node.NamespaceURI);
				namespaces.Add(prefix, node.NamespaceURI);
			}

			var serializer = new XmlSerializer(result.Type, rootOverride);

			using (var writer = XmlWriter.Create(xml, settings))
			{
				serializer.Serialize(writer, value, namespaces);
				writer.Flush();
			}

			node.ReplaceSelf(xml.ToString());

			return true;
		}
		public bool ReadObject(XPathResult result, XPathNavigator node, out object value)
		{
			using (var reader = node.ReadSubtree())
			{
				reader.MoveToContent();
				if (typeof(IXmlSerializable).IsAssignableFrom(result.Type))
				{
					value = DeserializeCustom(reader, result, node);
				}
				else
				{
					value = Deserialize(reader, result, node);
				}
			}

			return (value != null);
		}
		private static void Serialize(XmlWriter writer, XPathResult result, XPathNavigator node, object value)
		{
			var namespaces = new XmlSerializerNamespaces();
			namespaces.Add(string.Empty, string.Empty);
			if (string.IsNullOrEmpty(node.NamespaceURI) == false)
			{
				var prefix = result.Context.AddNamespace(node.NamespaceURI);
				namespaces.Add(prefix, node.NamespaceURI);
			}

			var rootOverride = new XmlRootAttribute(node.LocalName)
			{
				Namespace = node.NamespaceURI
			};

			var serializer = new XmlSerializer(result.Type, rootOverride);
			serializer.Serialize(writer, value, namespaces);
		}
		public bool ReadObject(XPathResult result, XPathNavigator node, out object value)
		{
			var rootOverride = new XmlRootAttribute(node.LocalName)
			{
				Namespace = node.NamespaceURI
			};

			var serializer = new XmlSerializer(result.Type, rootOverride);

			using (var reader = node.ReadSubtree())
			{
				reader.MoveToContent();
				if (serializer.CanDeserialize(reader))
				{
					value = serializer.Deserialize(reader);
					return true;
				}				
			}

			value = null;
			return false;
		}
Beispiel #6
0
		private bool WriteCustom(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			return result.WriteObject(value);
		}
Beispiel #7
0
		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);
			}
		}
Beispiel #8
0
		private void WriteComponent(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			var source = value as IDictionaryAdapter;
			if (source != null)
			{
				var sourceAdapter = For(source);
				if (sourceAdapter != null)
				{
					var sourceRoot = sourceAdapter.Root;
					var resultNode = result.GetNavigator(false);
					if (sourceRoot != null && resultNode != null && sourceRoot.IsSamePosition(resultNode))
						return;
				}

				var node = result.RemoveChildren();
				if (result.Type != source.Meta.Type && result.OmitPolymorphism == false)
				{
					var xmlType = source.GetXmlMeta().XmlType;
					var context = GetEffectiveContext(dictionaryAdapter);
					context.SetXmlType(xmlType.TypeName, xmlType.Namespace, node);
				}

				var element = (IDictionaryAdapter)ReadComponent(result, false, dictionaryAdapter);
				source.CopyTo(element);
				value = element;
			}
		}
Beispiel #9
0
		private void WriteFragment(XPathResult result, IXPathNavigable value)
		{
			var node = result.GetNavigator(true);
			if (node == null)
			{
				root.AppendChild(value.CreateNavigator());
			}
			else if (value != null)
			{
				node.ReplaceSelf(value.CreateNavigator());
			}
			else
			{
				node.DeleteSelf();
			}
		}
Beispiel #10
0
		private bool ReadCustom(XPathResult result, out object value)
		{
			return result.ReadObject(out value);
		}
Beispiel #11
0
		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;
		}
Beispiel #12
0
		private object ReadComponent(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
		{
			XPathNavigator source;
			if (result.Property != null)
				ifExists = ifExists || dictionaryAdapter.Meta.Type.IsAssignableFrom(result.Property.PropertyType);

			if (result.GetNavigator(false, true, out source) == false || (source == null && ifExists))
			{
				return null;
			}

			XPathAdapter xpathAdapter;
			var elementType = result.Type;

			if (source != null)
			{
				if (result.XmlMeta != null)
				{
					elementType = result.XmlMeta.Type;
				}
				else
				{
					var xmlType = GetEffectiveContext(dictionaryAdapter).GetXmlType(source);
					elementType = dictionaryAdapter.GetXmlSubclass(xmlType, elementType) ?? elementType;
				}
				xpathAdapter = new XPathAdapter(source, this);
			}
			else
			{
				Func<XPathNavigator> createSource = () => result.GetNavigator(true);
				xpathAdapter = new XPathAdapter(createSource, this);
			}

			return Create(dictionaryAdapter, elementType, null, xpathAdapter);
		}
Beispiel #13
0
		private void WriteSimple(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (value == null)
			{
				result.Remove();
				if (result.Property != null)
					dictionaryAdapter.This.ExtendedProperties.Remove(result.Property.PropertyName);
				return;
			}

			var node = result.GetNavigator(true);

			if (result.Type.IsEnum || result.Type == typeof(Guid))
			{
				node.SetTypedValue(value.ToString());
			}
			else
			{
				try
				{
					node.SetTypedValue(value);
				}
				catch (InvalidCastException)
				{
					if (DefaultXmlSerializer.Instance.WriteObject(result, node, value) && result.Property != null)
					{
						dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = value;
					}
				}
			}
		}
Beispiel #14
0
		private void WriteProperty(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			var propertyType = result.Type;

			if (WriteCustom(result, value, dictionaryAdapter))
				return;

			if (propertyType == typeof(string))
			{
				WriteSimple(result, value, dictionaryAdapter);
			}
			else if (typeof(IXPathNavigable).IsAssignableFrom(propertyType))
			{
				WriteFragment(result, (IXPathNavigable)value);
			}
			else if (propertyType.IsArray || typeof(IEnumerable).IsAssignableFrom(propertyType))
			{
				WriteCollection(result, value, dictionaryAdapter);
			}
			else if (propertyType.IsInterface)
			{
				WriteComponent(result, value, dictionaryAdapter);
			}
			else
			{
				WriteSimple(result, value, dictionaryAdapter);
			}
		}
Beispiel #15
0
 public XPathResult Evaluate(XmlNode contextNode, ResultType type, XPathResult result)
 {
     return(default(XPathResult));
 }
Beispiel #16
0
		private object ReadSimple(XPathResult result)
		{
			XPathNavigator node;
			if (result.GetNavigator(false, true, out node))
			{
				if (node != null)
				{
					Type underlyingType;
					if (IsNullableType(result.Type, out underlyingType) == false)
					{
						underlyingType = result.Type;
					}

					if (underlyingType.IsEnum)
					{
						return Enum.Parse(underlyingType, node.Value);
					}
					else if (underlyingType == typeof(Guid))
					{
						return new Guid(node.Value);
					}

					try
					{
						return node.ValueAs(underlyingType);
					}
					catch (InvalidCastException)
					{
						object value;
						if (DefaultXmlSerializer.Instance.ReadObject(result, node, out value))
							return value;
					}
				}
				if (result.Result != null)
					return Convert.ChangeType(result.Result, result.Type);
			}

			return null;
		}
Beispiel #17
0
		private void WriteComponent(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (result.Property != null)
			{
				dictionaryAdapter.This.ExtendedProperties.Remove(result.Property.PropertyName);
			}

			if (value == null)
			{
				result.Remove();
				return;
			}

			var source = value as IDictionaryAdapter;
			if (source != null)
			{
				var node = result.RemoveChildren();
				if (result.Type != source.Meta.Type && result.OmitPolymorphism == false)
				{
					var xmlType = source.GetXmlMeta().XmlType;
					Context.SetXmlType(xmlType.TypeName, xmlType.Namespace, node);
				}
				var element = (IDictionaryAdapter)ReadComponent(result, false, dictionaryAdapter);
				source.CopyTo(element);
			}
		}
Beispiel #18
0
		private object ReadCollection(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
		{
			if (ifExists && result.Result == null)
				return null;

			if (result.Type.IsArray)
				return ReadArray(result, dictionaryAdapter);

			if (result.Type.IsGenericType)
				return ReadList(result, dictionaryAdapter);

			return null;
		}
Beispiel #19
0
		private void WriteCollection(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			result.Remove();

			if (value != null)
			{
				if (result.Type.IsArray)
				{
					WriteArray(result, value, dictionaryAdapter);
				}
				else if (result.Type.IsGenericType)
				{
					WriteList(result, value, dictionaryAdapter);
				}
			}
		}
Beispiel #20
0
		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;
		}
Beispiel #21
0
		private bool WriteCustom(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (result.WriteObject(value))
			{
				if (result.Property != null)
				{
					dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = value;
				}
				return true;
			}
			return false;
		}
Beispiel #22
0
		private void WriteProperty(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			var propertyType = result.Type;
			var shouldRemove = (value == null);

			if (result.Property != null)
			{
				shouldRemove = shouldRemove || dictionaryAdapter.ShouldClearProperty(result.Property, value);
			}

			if (shouldRemove)
			{
				result.Remove(true);
				value = null;
				return;
			}

			if (WriteCustom(result, value, dictionaryAdapter))
			{
				return;
			}

			if (propertyType == typeof(string))
			{
				WriteSimple(result, value, dictionaryAdapter);
			}
			else if (typeof(IXPathNavigable).IsAssignableFrom(propertyType))
			{
				WriteFragment(result, (IXPathNavigable)value);
			}
			else if (propertyType.IsArray || typeof(IEnumerable).IsAssignableFrom(propertyType))
			{
				WriteCollection(result, ref value, dictionaryAdapter);
			}
			else if (propertyType.IsInterface)
			{
				WriteComponent(result, ref value, dictionaryAdapter);
			}
			else
			{
				WriteSimple(result, value, dictionaryAdapter);
			}
		}
		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;
		}
Beispiel #24
0
		private void WriteSimple(XPathResult result, object value, IDictionaryAdapter dictionaryAdapter)
		{
			var node = result.GetNavigator(true);

			if (result.Type.IsEnum || result.Type == typeof(Guid))
			{
				node.SetTypedValue(value.ToString());
			}
			else
			{
				try
				{
					node.SetTypedValue(value);
				}
				catch (InvalidCastException)
				{
					DefaultXmlSerializer.Instance.WriteObject(result, node, value);
				}
			}
		}
		private void WriteCollection(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			result.Remove(value == null);

			if (value != null)
			{
				if (result.Type.IsArray)
				{
					WriteArray(result, value, dictionaryAdapter);
				}
				else if (result.Type.IsGenericType)
				{
					WriteList(result, value, dictionaryAdapter);
				}
				if (result.Property != null)
				{
					value = ((IDictionaryPropertyGetter)this).GetPropertyValue(dictionaryAdapter, result.Key, null, result.Property, false);
				}
			}
		}
Beispiel #26
0
		private void WriteCollection(XPathResult result, ref object value, IDictionaryAdapter dictionaryAdapter)
		{
			if (value is byte[])
			{
				var node = result.GetNavigator(true);
				node.SetValue(Convert.ToBase64String((byte[])value));
				return;
			}

			result.Remove(value == null);

			if (value != null)
			{
				if (result.Type.IsArray)
				{
					WriteArray(result, value, dictionaryAdapter);
				}
				else if (result.Type.IsGenericType)
				{
					WriteList(result, value, dictionaryAdapter);
				}
				if (result.Property != null)
				{
					value = ((IDictionaryPropertyGetter)this).GetPropertyValue(dictionaryAdapter, result.Key, null, result.Property, false);
				}
			}
		}
 public XPathResult Evaluate(XmlNode contextNode, ResultType type, XPathResult result)
 {
     return default(XPathResult);
 }
Beispiel #28
0
		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);
			}
		}
Beispiel #29
0
		private object ReadProperty(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
		{
			object value;
			var propertyType = result.Type;

			if (ReadCustom(result, out value))
				return value;

			if (propertyType != typeof(string))
			{
				if (typeof(IXPathNavigable).IsAssignableFrom(propertyType))
					return ReadFragment(result);

				if (propertyType.IsArray || typeof(IEnumerable).IsAssignableFrom(propertyType))
					return ReadCollection(result, ifExists, dictionaryAdapter);
				
				if (propertyType.IsInterface)
					return ReadComponent(result, ifExists, dictionaryAdapter);
			}

			return ReadSimple(result);
		}
Beispiel #30
0
 public static XPathResult Evaluate(string expression, XmlNode contextNode, XPathNSResolver resolver, ushort type, XPathResult result)
 {
     return default(XPathResult);
 }
Beispiel #31
0
		private object ReadFragment(XPathResult result)
		{
			XPathNavigator node;
			result.GetNavigator(false, true, out node);
			if (node == null) return null;
			if (result.Type == typeof(XmlElement))
			{
				var document = new XmlDocument();
				document.Load(node.ReadSubtree());
				return document.DocumentElement;
			}
			return node.Clone();
		}