/// <summary>
 /// Constructs instances when the external queries.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="schema"></param>
 /// <param name="element"></param>
 internal ConfigurationAttribute(string name, ConfigurationAttributeSchema schema, ConfigurationElement element)
 {
     _element    = element;
     Name        = name;
     Schema      = schema;
     IsProtected = schema?.IsEncrypted ?? false;
     ResetValue();
 }
 /// <summary>
 /// Constructs instances when the external queries.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="schema"></param>
 /// <param name="element"></param>
 internal ConfigurationAttribute(string name, ConfigurationAttributeSchema schema, ConfigurationElement element)
 {
     _element = element;
     Name = name;
     Schema = schema;
     IsProtected = schema?.IsEncrypted ?? false;
     var clear = this.Decrypt(this.ExtractDefaultValue());
     IsInheritedFromDefaultValue = true;
     SetValue(clear);
 }
        /// <summary>
        /// Constructs instances when the external queries.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="schema"></param>
        /// <param name="element"></param>
        internal ConfigurationAttribute(string name, ConfigurationAttributeSchema schema, ConfigurationElement element)
        {
            _element    = element;
            Name        = name;
            Schema      = schema;
            IsProtected = schema?.IsEncrypted ?? false;
            var clear = this.Decrypt(this.ExtractDefaultValue());

            IsInheritedFromDefaultValue = true;
            SetValue(clear);
        }
 /// <summary>
 /// Constructs instances from config file.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="schema"></param>
 /// <param name="value"></param>
 /// <param name="element"></param>
 internal ConfigurationAttribute(string name, ConfigurationAttributeSchema schema, string value, ConfigurationElement element)
 {
     _element = element;
     Name = name;
     Schema = schema;
     IsProtected = schema?.IsEncrypted ?? false;
     var clear = Decrypt(value).ToString();
     var raw = Schema == null ? clear : Schema.Parse(clear);
     var result = TypeMatch(raw);
     IsInheritedFromDefaultValue = (Schema == null || !Schema.IsRequired)
                                             && result.Equals(ExtractDefaultValue());
     SetValue(raw);
     _element.InnerEntity.SetAttributeValue(Name, value);
 }
Пример #5
0
        public static string Format(this ConfigurationAttributeSchema schema, object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (schema.Type == "enum")
            {
                return(schema.GetEnumValues().GetName((long)value));
            }

            if (schema.Type == "flags")
            {
                var longValue = (long)value;
                var result    = new StringBuilder();
                foreach (ConfigurationEnumValue item in schema.GetEnumValues())
                {
                    if (item.Value == 0)
                    {
                        if (longValue == 0)
                        {
                            return(item.Name);
                        }

                        continue;
                    }

                    if ((longValue & item.Value) == item.Value)
                    {
                        result.AppendFormat("{0},", item.Name);
                    }
                }

                if (result[result.Length - 1] == ',')
                {
                    result.Length--;
                }

                return(result.ToString());
            }

            if (schema.Type == "bool")
            {
                return((bool)value ? "true" : "false");
            }

            return(value.ToString());
        }
        /// <summary>
        /// Constructs instances from config file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="schema"></param>
        /// <param name="value"></param>
        /// <param name="element"></param>
        internal ConfigurationAttribute(string name, ConfigurationAttributeSchema schema, string value, ConfigurationElement element)
        {
            _element    = element;
            Name        = name;
            Schema      = schema;
            IsProtected = schema?.IsEncrypted ?? false;
            var clear  = Decrypt(value).ToString();
            var raw    = Schema == null ? clear : Schema.Parse(clear);
            var result = TypeMatch(raw);

            IsInheritedFromDefaultValue = (Schema == null || !Schema.IsRequired) &&
                                          result.Equals(ExtractDefaultValueFromSchema());
            SetValue(raw);
            _element.InnerEntity.SetAttributeValue(Name, value);
        }
Пример #7
0
        internal void ParseSectionSchema(XElement element, ConfigurationElementSchema schema, string fileName)
        {
            foreach (var node in element.Nodes())
            {
                var item = node as XElement;
                if (item == null)
                {
                    continue;
                }

                if (item.Name.LocalName == "attribute")
                {
                    var attribute = new ConfigurationAttributeSchema
                    {
                        Name                = item.Attribute("name").Value,
                        Type                = item.Attribute("type").Value,
                        IsRequired          = item.Attribute("required").LoadBoolean(false),
                        IsUniqueKey         = item.Attribute("isUniqueKey").LoadBoolean(false),
                        IsCombinedKey       = item.Attribute("isCombinedKey").LoadBoolean(false),
                        IsCaseSensitive     = item.Attribute("caseSensitive").LoadBoolean(false),
                        IsEncrypted         = item.Attribute("encrypted").LoadBoolean(false),
                        AllowInfinite       = item.Attribute("allowInfinite").LoadBoolean(false),
                        TimeSpanFormat      = item.Attribute("timeSpanFormat").LoadString("string"),
                        IsExpanded          = item.Attribute("expanded").LoadBoolean(false),
                        ValidationType      = item.Attribute("validationType").LoadString(null),
                        ValidationParameter = item.Attribute("validationParameter").LoadString(null)
                    };

                    if (attribute.Type == "enum" || attribute.Type == "flags")
                    {
                        attribute.LoadEnums(item);
                    }

                    attribute.CreateValidator();

                    var defaultValue = item.Attribute("defaultValue");
                    if (defaultValue != null)
                    {
                        attribute.SetDefaultValue(defaultValue.Value);
                    }

                    if (schema == null)
                    {
                        Root.AttributeSchemas.Add(attribute);
                    }
                    else
                    {
                        schema.AttributeSchemas.Add(attribute);
                    }

                    continue;
                }

                if (item.Name.LocalName == "element")
                {
                    var name = item.Attribute("name").Value;
                    var top  = schema ?? Root;
                    ConfigurationElementSchema child = top.ChildElementSchemas[name];
                    if (child == null)
                    {
                        child = new ConfigurationElementSchema(fileName)
                        {
                            Name = name,
                            IsCollectionDefault         = item.Attribute("isCollectionDefault").LoadBoolean(false),
                            AllowUnrecognizedAttributes = top.AllowUnrecognizedAttributes
                        };
                        top.ChildElementSchemas.Add(child);
                        child.Path = string.Format("{0}/{1}", schema == null ? Name : schema.Path, child.Name);
                    }
                    else
                    {
                        // TODO: validation
                        if (item.Attribute("isCollectionDefault").LoadBoolean(false) != child.IsCollectionDefault)
                        {
                            throw new ArgumentException($"isCollectionDefault not equals: item {fileName} child {child.IsCollectionDefault} {child.FileName}");
                        }

                        if (top.AllowUnrecognizedAttributes != child.AllowUnrecognizedAttributes)
                        {
                            throw new ArgumentException($"allowUnrecognizedAttributes not equals: {fileName} child {child.AllowUnrecognizedAttributes} {child.FileName}");
                        }
                    }

                    ParseSectionSchema(item, child, fileName);
                }
                else if (item.Name.LocalName == "collection")
                {
                    var path                        = (schema == null) ? Name : schema.Path;
                    var addElementNames             = item.Attribute("addElement").LoadString(string.Empty);
                    var removeElementName           = item.Attribute("removeElement").LoadString(string.Empty);
                    var clearElementName            = item.Attribute("clearElement").LoadString(string.Empty);
                    var isMergeAppend               = item.Attribute("mergeAppend").LoadBoolean(true);
                    var allowDuplicates             = item.Attribute("allowDuplicates").LoadBoolean(false);
                    var allowUnrecognizedAttributes = item.Attribute("allowUnrecognizedAttributes").LoadBoolean(false);
                    var child                       = new ConfigurationCollectionSchema(path, addElementNames, removeElementName,
                                                                                        clearElementName, isMergeAppend, allowDuplicates, allowUnrecognizedAttributes, fileName);

                    var top = schema ?? Root;
                    if (top.CollectionSchema == null)
                    {
                        top.CollectionSchema = child;
                    }
                    else
                    {
                        top.CollectionSchema.Merge(child);
                    }

                    ParseSectionSchema(item, top.CollectionSchema.AddSchemas[0], fileName);
                    top.CollectionSchema.ReplicateAddSchema();
                }
            }
        }
        internal void ParseSectionSchema(XElement element, ConfigurationElementSchema schema)
        {
            foreach (var node in element.Nodes())
            {
                var item = node as XElement;
                if (item == null)
                {
                    continue;
                }

                if (item.Name.LocalName == "attribute")
                {
                    var attribute = new ConfigurationAttributeSchema
                    {
                        Name = item.Attribute("name").Value,
                        Type = item.Attribute("type").Value,
                        IsRequired = item.Attribute("required").LoadBoolean(false),
                        IsUniqueKey = item.Attribute("isUniqueKey").LoadBoolean(false),
                        IsCombinedKey = item.Attribute("isCombinedKey").LoadBoolean(false),
                        IsCaseSensitive = item.Attribute("caseSensitive").LoadBoolean(false),
                        IsEncrypted = item.Attribute("encrypted").LoadBoolean(false),
                        AllowInfinite = item.Attribute("allowInfinite").LoadBoolean(false),
                        TimeSpanFormat = item.Attribute("timeSpanFormat").LoadString("string"),
                        IsExpanded = item.Attribute("expanded").LoadBoolean(false),
                        ValidationType = item.Attribute("validationType").LoadString(null),
                        ValidationParameter = item.Attribute("validationParameter").LoadString(null)
                    };

                    if (attribute.Type == "enum" || attribute.Type == "flags")
                    {
                        attribute.LoadEnums(item);
                    }

                    attribute.CreateValidator();

                    var defaultValue = item.Attribute("defaultValue");
                    if (defaultValue != null)
                    {
                        attribute.SetDefaultValue(defaultValue.Value);
                    }

                    if (schema == null)
                    {
                        Root.AttributeSchemas.Add(attribute);
                    }
                    else
                    {
                        schema.AttributeSchemas.Add(attribute);
                    }

                    continue;
                }

                if (item.Name.LocalName == "element")
                {
                    var name = item.Attribute("name").Value;
                    var top = schema ?? Root;
                    ConfigurationElementSchema child = top.ChildElementSchemas[name];
                    if (child == null)
                    {
                        child = new ConfigurationElementSchema
                        {
                            Name = name,
                            IsCollectionDefault = item.Attribute("isCollectionDefault").LoadBoolean(false),
                            AllowUnrecognizedAttributes = top.AllowUnrecognizedAttributes
                        };
                        top.ChildElementSchemas.Add(child);
                        child.Path = string.Format("{0}/{1}", (schema == null ? Name : schema.Path), child.Name);
                    }
                    else
                    {
                        // TODO: validation
                        if (item.Attribute("isCollectionDefault").LoadBoolean(false) != child.IsCollectionDefault)
                        {
                            throw new ArgumentException("isCollectionDefault not equals");
                        }

                        if (top.AllowUnrecognizedAttributes != child.AllowUnrecognizedAttributes)
                        {
                            throw new ArgumentException("allowUnrecognizedAttributes not equals");
                        }
                    }

                    ParseSectionSchema(item, child);
                }
                else if (item.Name.LocalName == "collection")
                {
                    var path = (schema == null) ? Name : schema.Path;
                    var addElementNames = item.Attribute("addElement").LoadString(string.Empty);
                    var removeElementName = item.Attribute("removeElement").LoadString(string.Empty);
                    var clearElementName = item.Attribute("clearElement").LoadString(string.Empty);
                    var isMergeAppend = item.Attribute("mergeAppend").LoadBoolean(true);
                    var allowDuplicates = item.Attribute("allowDuplicates").LoadBoolean(false);
                    var allowUnrecognizedAttributes = item.Attribute("allowUnrecognizedAttributes").LoadBoolean(false);
                    var child = new ConfigurationCollectionSchema(path, addElementNames, removeElementName,
                        clearElementName, isMergeAppend, allowDuplicates, allowUnrecognizedAttributes);

                    var top = schema ?? Root;
                    if (top.CollectionSchema == null)
                    {
                        top.CollectionSchema = child;
                    }
                    else
                    {
                        top.CollectionSchema.Merge(child);
                    }

                    ParseSectionSchema(item, top.CollectionSchema.AddSchemas[0]);
                    top.CollectionSchema.ReplicateAddSchema();
                }
            }
        }