Exemplo n.º 1
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);
				}
			}
		}
Exemplo n.º 2
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);
				}
			}
		}
Exemplo n.º 3
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;
			}
		}
Exemplo n.º 4
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;
		}
Exemplo n.º 5
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();
			}
		}
Exemplo n.º 6
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;
		}
Exemplo n.º 7
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);
		}
Exemplo n.º 8
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;
					}
				}
			}
		}
Exemplo n.º 9
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();
		}
Exemplo n.º 10
0
		private object ReadComponent(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
		{
			XPathAdapter xpathAdapter;
			var source = result.GetNavigator(false);
			if (source == null && ifExists) return null;
		
			var elementType = result.Type;
			if (source != null)
			{
				if (result.XmlMeta != null)
				{
					elementType = result.XmlMeta.Type;
				}
				else
				{
					var xmlType = Context.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);
			}

			var component = Create(dictionaryAdapter, elementType, null, xpathAdapter);

			if (result.Property != null)
				dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = component;

			return component;
		}
Exemplo n.º 11
0
		private void WritePrimitive(XPathResult result, object value)
		{
			if (value == null)
			{
				result.Remove();
			}
			else if (result.Type.IsEnum)
			{
				result.GetNavigator(true).SetTypedValue(value.ToString());
			}
			else
			{
				try
				{
					result.GetNavigator(true).SetTypedValue(value);
				}
				catch (InvalidCastException)
				{
					var converter = TypeDescriptor.GetConverter(result.Type);
					if (converter != null && converter.CanConvertTo(typeof(string)))
					{
						value = converter.ConvertToString(value);
						result.GetNavigator(true).SetTypedValue(value);
					}
				}
			}
		}
Exemplo n.º 12
0
		private object ReadComponent(XPathResult result, bool ifExists, IDictionaryAdapter dictionaryAdapter)
		{
			XPathAdapter adapter;
			Func<XPathNavigator> createSource = null;
			var source = result.GetNavigator(false);
			if (source == null && ifExists) return null;
		
			var elementType = result.Type;
			if (source != null)
			{
				if (result.XmlMeta != null)
				{
					elementType = result.XmlMeta.Type;
				}
				else
				{
					var xmlType = context.GetXmlType(source);
					elementType = dictionaryAdapter.GetXmlSubclass(xmlType, elementType) ?? elementType;
				}
				adapter = new XPathAdapter(source, this);
			}
			else
			{
				if (createSource == null)
				{
					createSource = () => result.GetNavigator(true);
				}
				adapter = new XPathAdapter(createSource, this);
			}

			var component = dictionaryAdapter.This.Factory.GetAdapter(elementType, new Hashtable(), 
				new DictionaryDescriptor().AddBehavior(XPathBehavior.Instance, adapter));

			if (result.Property != null)
			{
				dictionaryAdapter.This.ExtendedProperties[result.Property.PropertyName] = component;
			}

			return component;
		}
Exemplo n.º 13
0
		private object ReadPrimitive(XPathResult result)
		{
			var node = result.GetNavigator(false);
			if (node != null)
			{
				if (result.Type.IsEnum)
				{
					return Enum.Parse(result.Type, node.Value);
				}
				try
				{
					return node.ValueAs(result.Type);
				}
				catch (InvalidCastException)
				{
					var converter = TypeDescriptor.GetConverter(result.Type);
					if (converter != null && converter.CanConvertFrom(typeof(string)))
					{
						return converter.ConvertFromString(node.Value);
					}	
				}
			}
			if (result.Result != null)
			{
				return Convert.ChangeType(result.Result, result.Type);
			}
			return null;
		}
Exemplo n.º 14
0
		private object ReadSimple(XPathResult result)
		{
			var node = result.GetNavigator(false);
			if (node != null)
			{
				if (result.Type.IsEnum)
					return Enum.Parse(result.Type, node.Value);

				try
				{
					return node.ValueAs(result.Type);
				}
				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;
		}