/// <summary>
        /// Returns an XElement from an ElasticObject
        /// </summary>
        /// <param name="elastic"></param>
        /// <returns></returns>
        public static XElement XElementFromElastic(ElasticObject elastic)
        {
            var exp = new XElement(elastic.InternalName);


            foreach (var a in elastic.Attributes)
            {
                if (a.Value.InternalValue != null)
                {
                    exp.Add(new XAttribute(a.Key, a.Value.InternalValue));
                }
            }

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

            foreach (var c in elastic.Elements)
            {
                var child = XElementFromElastic(c);
                exp.Add(child);
            }
            return(exp);
        }
        /// <summary>
        /// Build an expando from an XElement
        /// </summary>
        /// <param name="el"></param>
        /// <returns></returns>
        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 c in el.Elements())
            {
                var child = ElasticFromXElement(c);
                child.InternalParent = exp;
                exp.AddElement(child);
            }
            return(exp);
        }
        /// <summary>
        /// Interpret a method call
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="args"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
        {
            var obj = new ElasticObject(binder.Name, null);

            AddElement(obj);
            result = obj;
            return(true);
        }
Example #4
0
 public void AddElement(ElasticObject element)
 {
     if (!elements.ContainsKey(element.InternalName))
     {
         elements[element.InternalName] = new List <ElasticObject>();
     }
     elements[element.InternalName].Add(element);
 }
Example #5
0
 public void RemoveElement(ElasticObject element)
 {
     if (elements.ContainsKey(element.InternalName))
     {
         if (elements[element.InternalName].Contains(element))
         {
             elements[element.InternalName].Remove(element);
         }
     }
 }
        /// <summary>
        /// Catch a get member invocation
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        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);
        }
        /// <summary>
        /// Interpret the invocation of a binary operation
        /// </summary>
        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);
            }
            else if (binder.Operation == ExpressionType.LeftShiftAssign && nodeType == NodeType.Attribute)
            {
                InternalValue = arg;
                result        = this;
                return(true);
            }

            else if (binder.Operation == ExpressionType.LeftShift)
            {
                if (arg is string)
                {
                    var exp = new ElasticObject(arg as string, null)
                    {
                        nodeType = NodeType.Element
                    };
                    AddElement(exp);
                    result = exp;
                    return(true);
                }

                else if (arg is ElasticObject)
                {
                    var eobj = arg as ElasticObject;
                    if (!Elements.Contains(eobj))
                    {
                        AddElement(eobj);
                    }
                    result = eobj;
                    return(true);
                }
            }

            else if (binder.Operation == ExpressionType.LessThan)
            {
                string memberName = arg as string;
                if (arg is string)
                {
                    if (!HasAttribute(memberName))
                    {
                        var att = new ElasticObject(memberName, null);
                        AddAttribute(memberName, att);
                        result = att;
                        return(true);
                    }
                    else
                    {
                        throw new InvalidOperationException("An attribute with name" + memberName + " already exists");
                    }
                }
                else if (arg is ElasticObject)
                {
                    var eobj = arg as ElasticObject;
                    AddAttribute(memberName, eobj);
                    result = eobj;
                    return(true);
                }
            }
            else if (binder.Operation == ExpressionType.GreaterThan)
            {
                if (arg is FormatType)
                {
                    result = this.ToXElement();
                    return(true);
                }
            }
            return(base.TryBinaryOperation(binder, arg, out result));
        }
 public void RemoveElement(ElasticObject element)
 {
     elasticProvider.RemoveElement(element);
 }
 public void AddElement(ElasticObject element)
 {
     element.nodeType       = NodeType.Element;
     element.InternalParent = this;
     elasticProvider.AddElement(element);
 }
 public void AddAttribute(string key, ElasticObject value)
 {
     value.nodeType       = NodeType.Attribute;
     value.InternalParent = this;
     elasticProvider.AddAttribute(key, value);
 }
Example #11
0
 public void AddAttribute(string key, ElasticObject value)
 {
     attributes.Add(key, value);
 }
 /// <summary>
 /// Converts an expando to XElement
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public static XElement ToXElement(this ElasticObject e)
 {
     return((dynamic)XElementFromElastic(e));
 }