internal void Merge(ConfigurationCollectionSchema child) { // TODO: validation if (string.IsNullOrEmpty(AddElementNames)) { AddElementNames = child.AddElementNames; } if (string.IsNullOrEmpty(ClearElementName)) { ClearElementName = child.ClearElementName; } if (string.IsNullOrEmpty(RemoveElementName)) { RemoveElementName = child.RemoveElementName; } // TODO: is it OK? if (RemoveSchema != null && child.RemoveSchema != null) { RemoveSchema.AllowUnrecognizedAttributes = child.RemoveSchema.AllowUnrecognizedAttributes; } if (ClearSchema != null && child.ClearSchema != null) { ClearSchema.AllowUnrecognizedAttributes = child.ClearSchema.AllowUnrecognizedAttributes; } }
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(); } } }