Пример #1
0
        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);
        }
Пример #2
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);
        }
Пример #3
0
        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 void AddElement(ElasticObject element)
        {
            if (!_elements.ContainsKey(element.InternalName))
            {
                _elements[element.InternalName] = new List <ElasticObject>();
            }

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

            if (_elements[element.InternalName].Contains(element))
            {
                _elements[element.InternalName].Remove(element);
            }
        }
Пример #6
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);
        }
Пример #7
0
 public static XElement ToXElement(this ElasticObject e)
 {
     return(XElementFromElastic(e));
 }
Пример #8
0
 public void RemoveElement(ElasticObject element)
 {
     _elasticProvider.RemoveElement(element);
 }
Пример #9
0
 public void AddElement(ElasticObject element)
 {
     element._nodeType      = NodeType.Element;
     element.InternalParent = this;
     _elasticProvider.AddElement(element);
 }
Пример #10
0
 public void AddAttribute(string key, ElasticObject value)
 {
     value._nodeType      = NodeType.Attribute;
     value.InternalParent = this;
     _elasticProvider.AddAttribute(key, value);
 }
Пример #11
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));
        }
Пример #12
0
 public void AddAttribute(string key, ElasticObject value)
 {
     _attributes.Add(key, value);
 }