/// <summary>
        /// Sets the value of an attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool remove = (value == null);

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (attrib.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
        /// <summary>
        /// Sets the value of a text attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfEmpty">if set to <c>true</c> the attribute is removed if the <paramref name="value"/> is empty. The attribute is always removed if <paramref name="value"/> is <c>null</c></param>
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool remove = (value == null) || (removeIfEmpty && string.IsNullOrEmpty(value));

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (!attrib.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
Exemple #3
0
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool flag;

            if (value == null)
            {
                flag = true;
            }
            else
            {
                flag = (!removeIfEmpty ? false : string.IsNullOrEmpty(value));
            }
            bool           flag1 = flag;
            StageAttribute str   = parent.Attribute(name);

            if (str != null)
            {
                if (flag1)
                {
                    str.Remove();
                    return;
                }
                if (!str.isText)
                {
                    throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag1)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }
Exemple #4
0
        public static object Unstage(StageItem item, Type targetType)
        {
            if (item is StageNull)
            {
                return(null);
            }
            IStager stager = SerializationMaster.GetStager(targetType);

            if (stager != null)
            {
                return(stager.UnstageValue(item, targetType));
            }
            StageValue stageValue = item as StageValue;

            if (stageValue == null)
            {
                StageElement stageElement = item as StageElement;
                if (stageElement == null)
                {
                    throw new SerializationException(string.Concat("Unable to unstage, the element is not supported: ", targetType.Name));
                }
                return(SerializationMaster.ReflectIn(stageElement));
            }
            IValueConverter converter = SerializationMaster.GetConverter(targetType);

            if (converter == null)
            {
                throw new SerializationException(string.Concat("Unable to unstage, no converter or stager was found for type: ", targetType.Name));
            }
            return(converter.FromString(stageValue.@value, targetType));
        }
        /// <summary>
        /// Sets the value of a text element. If it does not exist it is created.
        /// If multiple identically named items exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the item.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfNullOrEmpty">If <c>true</c> and <para<paramref name="value"/> is <c>null</c> or empty, the item is removed rather than converted to a <see cref="StageNull"/></param>
        public static void SetTextValue(this StageElement parent, string name, string value, bool removeIfNullOrEmpty = true)
        {
            bool remove = (removeIfNullOrEmpty && string.IsNullOrEmpty(value));

            var item = parent.Item(name);

            if (item == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }

                return;
            }

            if (item is StageAttribute)
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }

            var nullItem = item as StageNull;

            if (item != null)
            {
                if (value != null)
                {
                    nullItem.Remove();
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }

                return;
            }

            var valueItem = item as StageValue;

            if (item == null)
            {
                throw new InvalidOperationException("Only value elements can be set using this method.");
            }

            if (remove)
            {
                item.Remove();
            }
            else if (value == null)
            {
                item.Remove();
                parent.Add(new StageNull(name));
            }
            else if (!valueItem.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text value item.");
            }
            else
            {
                valueItem.value = SerializationMaster.ToString(value);
            }
        }
Exemple #6
0
 public static T ValueOrDefault <T>(this StageElement element, string itemName, T defaultValue = null)
 {
     if (element == null)
     {
         return(defaultValue);
     }
     return(element.Item(itemName).ValueOrDefault <T>(defaultValue));
 }
Exemple #7
0
 public static void AddAttribute(this StageElement parent, string name, object value, bool onlyIfNotNull = true)
 {
     if (onlyIfNotNull && value == null)
     {
         return;
     }
     parent.Add(SerializationMaster.ToStageAttribute(name, value));
 }
Exemple #8
0
 public static void AddTextAttribute(this StageElement parent, string name, string value, bool onlyIfNotNullOrEmpty = true)
 {
     if (onlyIfNotNullOrEmpty && string.IsNullOrEmpty(value))
     {
         return;
     }
     parent.Add(SerializationMaster.ToStageAttribute(name, value));
 }
Exemple #9
0
 public static T AttributeValue <T>(this StageElement element, string attributeName)
 {
     if (element != null)
     {
         StageAttribute stageAttribute = element.Attribute(attributeName);
         if (stageAttribute != null)
         {
             return(SerializationMaster.FromString <T>(stageAttribute.@value));
         }
     }
     throw new ArgumentException(string.Concat("No attribute by that name was found: ", attributeName));
 }
Exemple #10
0
 public static T Value <T>(this StageElement element, string itemName)
 {
     if (element != null)
     {
         StageItem stageItem = element.Item(itemName);
         if (stageItem != null)
         {
             return(stageItem.ValueOrDefault <T>(default(T)));
         }
     }
     throw new ArgumentException(string.Concat("No item by that name was found: ", itemName));
 }
Exemple #11
0
        private static StageElement ReflectOut(string elementName, object item)
        {
            var itemType  = item.GetType();
            var nameSplit = itemType.AssemblyQualifiedName.Split(',');
            var element   = new StageElement(elementName, new StageAttribute("type", string.Concat(nameSplit[0], ",", nameSplit[1]), true));

            var properties = from p in itemType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                             let attrib = p.GetAttribute <ApexSerializationAttribute>(true)
                                          where attrib != null && p.CanRead && p.CanWrite && (attrib.excludeMask & _excludeMask) == 0
                                          select new AIPropInfo
            {
                prop         = p,
                defaultValue = attrib.defaultValue
            };

            foreach (var p in properties)
            {
                var val = p.prop.GetValue(item, null);
                if (val != null && !val.Equals(p.defaultValue))
                {
                    StageItem propElement;
                    if (TryStage(p.prop.Name, val, out propElement))
                    {
                        element.Add(propElement);
                    }
                }
            }

            var fields = from f in itemType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         let attrib = f.GetAttribute <ApexSerializationAttribute>(false)
                                      where attrib != null && (attrib.excludeMask & _excludeMask) == 0
                                      select new AIFieldInfo
            {
                field        = f,
                defaultValue = attrib.defaultValue
            };

            foreach (var f in fields)
            {
                var val = f.field.GetValue(item);
                if (val != null && !val.Equals(f.defaultValue))
                {
                    StageItem propElement;
                    if (TryStage(f.field.Name, val, out propElement))
                    {
                        element.Add(propElement);
                    }
                }
            }

            return(element);
        }
Exemple #12
0
        private static object ReflectIn(StageElement element)
        {
            object obj;
            object obj1;
            object obj2;
            string str = element.AttributeValueOrDefault <string>("type", null);

            if (str == null)
            {
                throw new SerializationException("Invalid structure detected, missing type info.");
            }
            Type type = Type.GetType(str, true);

            try
            {
                obj = Activator.CreateInstance(type, true);
            }
            catch (MissingMethodException missingMethodException1)
            {
                MissingMethodException missingMethodException = missingMethodException1;
                throw new SerializationException(string.Format("Unable to create type {0}, ensure it has a parameterless constructor", type.Name), missingMethodException);
            }
            IInitializeAfterDeserialization initializeAfterDeserialization = obj as IInitializeAfterDeserialization;

            if (initializeAfterDeserialization != null)
            {
                if (SerializationMaster._requiresInit == null)
                {
                    throw new InvalidOperationException("An entity requires initialization but was unable to register, call UnstageAndInitialize instead.");
                }
                SerializationMaster._requiresInit.Add(initializeAfterDeserialization);
            }
            foreach (PropertyInfo serializedProperty in SerializationMaster.GetSerializedProperties(type))
            {
                StageItem stageItem = element.Item(serializedProperty.Name);
                if (stageItem == null || !SerializationMaster.TryUnstage(stageItem, serializedProperty.PropertyType, out obj1))
                {
                    continue;
                }
                serializedProperty.SetValue(obj, obj1, null);
            }
            foreach (FieldInfo serializedField in SerializationMaster.GetSerializedFields(type))
            {
                StageItem stageItem1 = element.Item(serializedField.Name);
                if (stageItem1 == null || !SerializationMaster.TryUnstage(stageItem1, serializedField.FieldType, out obj2))
                {
                    continue;
                }
                serializedField.SetValue(obj, obj2);
            }
            return(obj);
        }
        /// <summary>
        /// Gets the converted value of the specified attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns>The value of the attribute.</returns>
        /// <exception cref="System.ArgumentException">If no attribute by that name was found.</exception>
        public static T AttributeValue <T>(this StageElement element, string attributeName)
        {
            if (element != null)
            {
                var attrib = element.Attribute(attributeName);
                if (attrib != null)
                {
                    return(SerializationMaster.FromString <T>(attrib.value));
                }
            }

            throw new ArgumentException("No attribute by that name was found: " + attributeName);
        }
        /// <summary>
        /// Gets the converted value of the specified item.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="itemName">Name of the value.</param>
        /// <returns>The value of the item.</returns>
        /// <exception cref="System.ArgumentException">If no value item by that name was found.</exception>
        public static T Value <T>(this StageElement element, string itemName)
        {
            if (element != null)
            {
                var item = element.Item(itemName);
                if (item != null)
                {
                    return(ValueOrDefault <T>(item));
                }
            }

            throw new ArgumentException("No item by that name was found: " + itemName);
        }
Exemple #15
0
        public static void SetValue(this StageElement parent, string name, object value, bool removeIfNull = true)
        {
            bool      flag      = (!removeIfNull ? false : value == null);
            StageItem stageItem = parent.Item(name);

            if (stageItem == null)
            {
                if (!flag)
                {
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }
                return;
            }
            if (stageItem is StageAttribute)
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            StageNull stageNull = stageItem as StageNull;

            if (stageItem != null)
            {
                if (value != null)
                {
                    stageNull.Remove();
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }
                return;
            }
            StageValue str = stageItem as StageValue;

            if (stageItem == null)
            {
                throw new InvalidOperationException("Only value elements can be set using this method.");
            }
            if (flag)
            {
                stageItem.Remove();
                return;
            }
            if (value == null)
            {
                stageItem.Remove();
                parent.Add(new StageNull(name));
                return;
            }
            if (str.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextValue to set text values.");
            }
            str.@value = SerializationMaster.ToString(value);
        }
Exemple #16
0
        public static T AttributeValueOrDefault <T>(this StageElement element, string attributeName, T defaultValue = null)
        {
            if (element == null)
            {
                return(defaultValue);
            }
            StageAttribute stageAttribute = element.Attribute(attributeName);

            if (stageAttribute == null)
            {
                return(defaultValue);
            }
            return(SerializationMaster.FromString <T>(stageAttribute.@value));
        }
Exemple #17
0
        private static StageElement ReflectOut(string elementName, object item)
        {
            StageItem stageItem;
            StageItem stageItem1;
            Type      type = item.GetType();

            string[]     strArrays    = type.AssemblyQualifiedName.Split(new char[] { ',' });
            StageElement stageElement = new StageElement(elementName, new StageItem[] { new StageAttribute("type", string.Concat(strArrays[0], ",", strArrays[1]), true) });

            foreach (SerializationMaster.AIPropInfo aIPropInfo in (
                         from p in (IEnumerable <PropertyInfo>)type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         select new { p = p, attrib = p.GetAttribute <ApexSerializationAttribute>(true) }).Where((argument0) => {
                if (argument0.attrib == null || !argument0.p.CanRead)
                {
                    return(false);
                }
                return(argument0.p.CanWrite);
            }).Select((argument1) => new SerializationMaster.AIPropInfo()
            {
                prop = argument1.p,
                defaultValue = argument1.attrib.defaultValue
            }))
            {
                object value = aIPropInfo.prop.GetValue(item, null);
                if (value == null || value.Equals(aIPropInfo.defaultValue) || !SerializationMaster.TryStage(aIPropInfo.prop.Name, value, out stageItem))
                {
                    continue;
                }
                stageElement.Add(stageItem);
            }
            foreach (SerializationMaster.AIFieldInfo aIFieldInfo in
                     from f in (IEnumerable <FieldInfo>)type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                     let attrib = f.GetAttribute <ApexSerializationAttribute>(false)
                                  where attrib != null
                                  select new SerializationMaster.AIFieldInfo()
            {
                field = f,
                defaultValue = attrib.defaultValue
            })
            {
                object obj = aIFieldInfo.field.GetValue(item);
                if (obj == null || obj.Equals(aIFieldInfo.defaultValue) || !SerializationMaster.TryStage(aIFieldInfo.field.Name, obj, out stageItem1))
                {
                    continue;
                }
                stageElement.Add(stageItem1);
            }
            return(stageElement);
        }
        public IEnumerable <StageAttribute> Attributes()
        {
            StageElement stageElement = null;
            StageItem    i;

            if (stageElement._tailAttribute == null)
            {
                yield break;
            }
            for (i = stageElement._tailAttribute.next; i != stageElement._tailAttribute; i = i.next)
            {
                yield return((StageAttribute)i);
            }
            yield return((StageAttribute)i);
        }
        /// <summary>
        /// Gets the converted value of the specified attribute, or a default value if the element or the attribute is null or if it has no value.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value of the attribute, or <paramref name="defaultValue"/> if <paramref name="element"/> or the attribute is <c>null</c> or has no value.</returns>
        public static T AttributeValueOrDefault <T>(this StageElement element, string attributeName, T defaultValue = default(T))
        {
            if (element == null)
            {
                return(defaultValue);
            }

            var attrib = element.Attribute(attributeName);

            if (attrib == null)
            {
                return(defaultValue);
            }

            return(SerializationMaster.FromString <T>(attrib.value));
        }
        public IEnumerable <StageItem> Items(string name)
        {
            StageElement stageElement = null;

            if (stageElement._tailChild == null)
            {
                yield break;
            }
            StageItem stageItem = stageElement._tailChild;

            do
            {
                stageItem = stageItem.next;
                if (stageItem.name != name)
                {
                    continue;
                }
                yield return(stageItem);
            }while (stageItem != stageElement._tailChild);
        }
        public IEnumerable <StageElement> Elements(string name)
        {
            StageElement stageElement = null;

            if (stageElement._tailChild == null)
            {
                yield break;
            }
            StageItem stageItem = stageElement._tailChild;

            do
            {
                stageItem = stageItem.next;
                StageElement stageElement1 = stageItem as StageElement;
                if (stageElement1 == null || !(stageElement1.name == name))
                {
                    continue;
                }
                yield return(stageElement1);
            }while (stageItem != stageElement._tailChild);
        }
Exemple #22
0
        public IEnumerable <StageElement> Elements()
        {
            StageContainer stageContainer = null;

            if (stageContainer._tailChild == null)
            {
                yield break;
            }
            StageItem stageItem = stageContainer._tailChild;

            do
            {
                stageItem = stageItem.next;
                StageElement stageElement = stageItem as StageElement;
                if (stageElement == null)
                {
                    continue;
                }
                yield return(stageElement);
            }while (stageItem != stageContainer._tailChild);
        }
        public IEnumerable <StageItem> Descendants(string name)
        {
            StageElement   stageElement   = null;
            StageItem      stageItem      = stageElement;
            StageContainer stageContainer = stageElement;

            while (true)
            {
                if (stageContainer != null)
                {
                    if (stageContainer._tailChild == null)
                    {
                        goto Label2;
                    }
                    stageItem = stageContainer._tailChild.next;
                    goto Label0;
                }
Label2:
                while (stageItem != stageElement && stageItem == stageItem.parent._tailChild)
                {
                    stageItem = stageItem.parent;
                }
                if (stageItem == stageElement)
                {
                    break;
                }
                stageItem = stageItem.next;
Label0:
                if (stageItem.name == name)
                {
                    yield return(stageItem);
                }
                stageContainer = stageItem as StageContainer;
            }
            yield break;
            goto Label2;
        }
Exemple #24
0
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool           flag = value == null;
            StageAttribute str  = parent.Attribute(name);

            if (str != null)
            {
                if (flag)
                {
                    str.Remove();
                    return;
                }
                if (str.isText && !(value is string))
                {
                    throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }
Exemple #25
0
 /// <summary>
 /// Serializes the specified stage item.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="pretty">if set to <c>true</c> the serializer will produce a reader friendly string.</param>
 /// <returns>The serialized string representation of the item.</returns>
 public static string Serialize(StageElement item, bool pretty = false)
 {
     EnsureInit();
     return(_serializer.Serialize(item, pretty));
 }
Exemple #26
0
        private static object ReflectIn(StageElement element)
        {
            var typeName = element.AttributeValueOrDefault <string>("type", null);

            if (typeName == null)
            {
                throw new SerializationException("Invalid structure detected, missing type info.");
            }

            var    itemType = Type.GetType(typeName, true);
            object instance;

            try
            {
                instance = Activator.CreateInstance(itemType, true);
            }
            catch (MissingMethodException mme)
            {
                throw new SerializationException(string.Format("Unable to create type {0}, ensure it has a parameterless constructor", itemType.Name), mme);
            }

            var forInit = instance as IInitializeAfterDeserialization;

            if (forInit != null)
            {
                if (_requiresInit == null)
                {
                    throw new InvalidOperationException("An entity requires initialization but was unable to register, call UnstageAndInitialize instead.");
                }

                _requiresInit.Add(forInit);
            }

            var properties = GetSerializedProperties(itemType);

            foreach (var p in properties)
            {
                var member = element.Item(p.Name);
                if (member == null)
                {
                    continue;
                }

                object val;
                if (TryUnstage(member, p.PropertyType, out val))
                {
                    p.SetValue(instance, val, null);
                }
            }

            var fields = GetSerializedFields(itemType);

            foreach (var f in fields)
            {
                var member = element.Item(f.Name);
                if (member == null)
                {
                    continue;
                }

                object val;
                if (TryUnstage(member, f.FieldType, out val))
                {
                    f.SetValue(instance, val);
                }
            }

            return(instance);
        }