Inheritance: System.Dynamic.DynamicObject, IElasticHierarchyWrapper, INotifyPropertyChanged
        public static ElasticObject ElasticFromXElement(XElement el)
        {
            var exp = new ElasticObject();

            if (!string.IsNullOrEmpty(el.Value))
            {
                exp.InternalValue = el.Value;
            }

            exp.InternalName = el.Name.LocalName;

            foreach (var a in el.Attributes())
            {
                exp.CreateOrGetAttribute(a.Name.LocalName, a.Value);
            }

            var textNode = el.Nodes().FirstOrDefault();

            if (textNode is XText)
            {
                exp.InternalContent = textNode.ToString();
            }

            foreach (var child in el.Elements().Select(ElasticFromXElement))
            {
                child.InternalParent = exp;

                exp.AddElement(child);
            }

            return(exp);
        }
		public static XElement XElementFromElastic(ElasticObject elastic, XNamespace nameSpace = null)
		{
			// we default to empty namespace
			nameSpace = nameSpace ?? string.Empty;
			var exp = new XElement(nameSpace + elastic.InternalName);

			foreach (var a in elastic.Attributes.Where(a => a.Value.InternalValue != null))
			{
				// if we have xmlns attribute add it like XNamespace instead of regular attribute
				if (a.Key.Equals("xmlns", StringComparison.InvariantCultureIgnoreCase))
				{
					nameSpace = a.Value.InternalValue.ToString();
					exp.Name = nameSpace.GetName(exp.Name.LocalName);
				}
				else
				{
					exp.Add(new XAttribute(a.Key, a.Value.InternalValue));
				}
			}

			if (elastic.InternalContent is string)
			{
				exp.Add(new XText(elastic.InternalContent as string));
			}

			foreach (var child in elastic.Elements.Select(c => XElementFromElastic(c, nameSpace)))
			{
				exp.Add(child);
			}

			return exp;
		}
		public static ElasticObject ElasticFromXElement(XElement el)
		{
			var exp = new ElasticObject();

			if (!string.IsNullOrEmpty(el.Value))
			{
				exp.InternalValue = el.Value;
			}

			exp.InternalName = el.Name.LocalName;

			foreach (var a in el.Attributes())
			{
				exp.CreateOrGetAttribute(a.Name.LocalName, a.Value);
			}

			var textNode = el.Nodes().FirstOrDefault();

			if (textNode is XText)
			{
				exp.InternalContent = textNode.ToString();
			}

			foreach (var child in el.Elements().Select(ElasticFromXElement))
			{
				child.InternalParent = exp;

				exp.AddElement(child);
			}

			return exp;
		}
Exemple #4
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (_elasticProvider.HasAttribute(binder.Name))
            {
                result = _elasticProvider.Attribute(binder.Name).InternalValue;
            }
            else
            {
                var obj = _elasticProvider.Element(binder.Name);

                if (obj != null)
                {
                    result = obj;
                }
                else
                {
                    var exp = new ElasticObject(binder.Name, null);
                    _elasticProvider.AddElement(exp);

                    result = exp;
                }
            }

            return(true);
        }
        public static XElement XElementFromElastic(ElasticObject elastic, XNamespace nameSpace = null)
        {
            // we default to empty namespace
            nameSpace = nameSpace ?? string.Empty;
            var exp = new XElement(nameSpace + elastic.InternalName);

            foreach (var a in elastic.Attributes.Where(a => a.Value.InternalValue != null))
            {
                // if we have xmlns attribute add it like XNamespace instead of regular attribute
                if (a.Key.Equals("xmlns", StringComparison.InvariantCultureIgnoreCase))
                {
                    nameSpace = a.Value.InternalValue.ToString();
                    exp.Name  = nameSpace.GetName(exp.Name.LocalName);
                }
                else
                {
                    exp.Add(new XAttribute(a.Key, a.Value.InternalValue));
                }
            }

            if (elastic.InternalContent is string)
            {
                exp.Add(new XText(elastic.InternalContent as string));
            }

            foreach (var child in elastic.Elements.Select(c => XElementFromElastic(c, nameSpace)))
            {
                exp.Add(child);
            }

            return(exp);
        }
Exemple #6
0
        public void AddElement(ElasticObject element)
        {
            if (!_elements.ContainsKey(element.InternalName))
            {
                _elements[element.InternalName] = new List <ElasticObject>();
            }

            _elements[element.InternalName].Add(element);
        }
		public void AddElement(ElasticObject element)
		{
			if (!_elements.ContainsKey(element.InternalName))
			{
				_elements[element.InternalName] = new List<ElasticObject>();
			}

			_elements[element.InternalName].Add(element);
		}
Exemple #8
0
        public void RemoveElement(ElasticObject element)
        {
            if (!_elements.ContainsKey(element.InternalName))
            {
                return;
            }

            if (_elements[element.InternalName].Contains(element))
            {
                _elements[element.InternalName].Remove(element);
            }
        }
		public void RemoveElement(ElasticObject element)
		{
			if (!_elements.ContainsKey(element.InternalName))
			{
				return;
			}

			if (_elements[element.InternalName].Contains(element))
			{
				_elements[element.InternalName].Remove(element);
			}
		}
Exemple #10
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var obj = new ElasticObject(binder.Name, null);

            foreach (var a in args)
            {
                foreach (var p in a.GetType().GetProperties())
                {
                    AddAttribute(p.Name, p.GetValue(a, null));
                }
            }

            AddElement(obj);
            result = obj;

            return(true);
        }
		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			if (_elasticProvider.HasAttribute(binder.Name))
			{
				result = _elasticProvider.Attribute(binder.Name).InternalValue;
			}
			else
			{
				var obj = _elasticProvider.Element(binder.Name);

				if (obj != null)
				{
					result = obj;
				}
				else
				{
					var exp = new ElasticObject(binder.Name, null);
					_elasticProvider.AddElement(exp);

					result = exp;
				}
			}

			return true;
		}
		public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
		{
			if (binder.Operation == ExpressionType.LeftShiftAssign && _nodeType == NodeType.Element)
			{
				InternalContent = arg;
				result = this;
				return true;
			}

			if (binder.Operation == ExpressionType.LeftShiftAssign && _nodeType == NodeType.Attribute)
			{
				InternalValue = arg;
				result = this;
				return true;
			}

			switch (binder.Operation)
			{
				case ExpressionType.LeftShift:
				{
					if (arg is string)
					{
						var exp = new ElasticObject(arg as string, null)
						{
							_nodeType = NodeType.Element
						};

						AddElement(exp);
						result = exp;
						return true;
					}

					if (!(arg is ElasticObject))
					{
						return base.TryBinaryOperation(binder, arg, out result);
					}

					var eobj = arg as ElasticObject;

					if (!Elements.Contains(eobj))
					{
						AddElement(eobj);
					}

					result = eobj;

					return true;
				}
				case ExpressionType.LessThan:
				{
					var memberName = "";

					if (arg is string)
					{
						memberName = arg as string;

						if (HasAttribute(memberName))
						{
							throw new InvalidOperationException("An attribute with name" + memberName + " already exists");
						}

						var att = new ElasticObject(memberName, null);
						AddAttribute(memberName, att);
						result = att;

						return true;
					}

					if (!(arg is ElasticObject))
					{
						return base.TryBinaryOperation(binder, arg, out result);
					}

					var eobj = arg as ElasticObject;

					AddAttribute(memberName, eobj);
					result = eobj;

					return true;
				}
				case ExpressionType.GreaterThan:
				{
					if (!(arg is FormatType))
					{
						return base.TryBinaryOperation(binder, arg, out result);
					}

					result = this.ToXElement();

					return true;
				}
			}

			return base.TryBinaryOperation(binder, arg, out result);
		}
Exemple #13
0
 public void RemoveElement(ElasticObject element)
 {
     _elasticProvider.RemoveElement(element);
 }
Exemple #14
0
 public void AddElement(ElasticObject element)
 {
     element._nodeType      = NodeType.Element;
     element.InternalParent = this;
     _elasticProvider.AddElement(element);
 }
Exemple #15
0
 public void AddAttribute(string key, ElasticObject value)
 {
     value._nodeType      = NodeType.Attribute;
     value.InternalParent = this;
     _elasticProvider.AddAttribute(key, value);
 }
		public void AddElement(ElasticObject element)
		{
			element._nodeType = NodeType.Element;
			element.InternalParent = this;
			_elasticProvider.AddElement(element);
		}
		public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
		{
			var obj = new ElasticObject(binder.Name, null);

			foreach (var a in args)
			{
				foreach (var p in a.GetType().GetProperties())
				{
					AddAttribute(p.Name, p.GetValue(a, null));
				}
			}

			AddElement(obj);
			result = obj;

			return true;
		}
Exemple #18
0
 public void AddAttribute(string key, ElasticObject value)
 {
     _attributes.Add(key, value);
 }
		public void AddAttribute(string key, ElasticObject value)
		{
			_attributes.Add(key, value);
		}
		public void AddAttribute(string key, ElasticObject value)
		{
			value._nodeType = NodeType.Attribute;
			value.InternalParent = this;
			_elasticProvider.AddAttribute(key, value);
		}
Exemple #21
0
        public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
        {
            if (binder.Operation == ExpressionType.LeftShiftAssign && _nodeType == NodeType.Element)
            {
                InternalContent = arg;
                result          = this;
                return(true);
            }

            if (binder.Operation == ExpressionType.LeftShiftAssign && _nodeType == NodeType.Attribute)
            {
                InternalValue = arg;
                result        = this;
                return(true);
            }

            switch (binder.Operation)
            {
            case ExpressionType.LeftShift:
            {
                if (arg is string)
                {
                    var exp = new ElasticObject(arg as string, null)
                    {
                        _nodeType = NodeType.Element
                    };

                    AddElement(exp);
                    result = exp;
                    return(true);
                }

                if (!(arg is ElasticObject))
                {
                    return(base.TryBinaryOperation(binder, arg, out result));
                }

                var eobj = arg as ElasticObject;

                if (!Elements.Contains(eobj))
                {
                    AddElement(eobj);
                }

                result = eobj;

                return(true);
            }

            case ExpressionType.LessThan:
            {
                var memberName = "";

                if (arg is string)
                {
                    memberName = arg as string;

                    if (HasAttribute(memberName))
                    {
                        throw new InvalidOperationException("An attribute with name" + memberName + " already exists");
                    }

                    var att = new ElasticObject(memberName, null);
                    AddAttribute(memberName, att);
                    result = att;

                    return(true);
                }

                if (!(arg is ElasticObject))
                {
                    return(base.TryBinaryOperation(binder, arg, out result));
                }

                var eobj = arg as ElasticObject;

                AddAttribute(memberName, eobj);
                result = eobj;

                return(true);
            }

            case ExpressionType.GreaterThan:
            {
                if (!(arg is FormatType))
                {
                    return(base.TryBinaryOperation(binder, arg, out result));
                }

                result = this.ToXElement();

                return(true);
            }
            }

            return(base.TryBinaryOperation(binder, arg, out result));
        }
		public void RemoveElement(ElasticObject element)
		{
			_elasticProvider.RemoveElement(element);
		}
 public static XElement ToXElement(this ElasticObject e)
 {
     return(XElementFromElastic(e));
 }