/// <summary> /// Adds single attribute to given node. /// </summary> /// <param name="fb2Attribute">Attribute to add to given node.</param> /// <returns>Current node.</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="NoAttributesAllowedException"></exception> /// <exception cref="UnexpectedAtrributeException"></exception> public Fb2Node AddAttribute(Fb2Attribute fb2Attribute) { if (fb2Attribute == null) { throw new ArgumentNullException(nameof(fb2Attribute)); } if (!AllowedAttributes.Any()) { throw new NoAttributesAllowedException(Name); } var key = fb2Attribute.Key; if (!AllowedAttributes.Contains(key)) { throw new UnexpectedAtrributeException(Name, key); } // update or insert if (TryGetAttribute(key, true, out var existingAttribute)) { var existingAttributeIndex = attributes.IndexOf(existingAttribute !); attributes[existingAttributeIndex] = fb2Attribute; // replace existing, should not be -1 } else { attributes.Add(fb2Attribute); } return(this); }
/// <summary> /// Basic Load of node - validation and populating Attributes. /// </summary> /// <param name="node">XNode to load as <c>Fb2Node</c>.</param> /// <param name="parentNode">Parent node of node being loaded, can be <see langword="null"/>.</param> /// <param name="preserveWhitespace">Is ignored during <c>Fb2Node</c> loading.</param> /// <param name="loadUnsafe">Is ignored during <c>Fb2Node</c> loading.</param> /// <exception cref="ArgumentNullException"></exception> public virtual void Load( [In] XNode node, [In] Fb2Container?parentNode = null, bool preserveWhitespace = false, bool loadUnsafe = true, bool loadNamespaceMetadata = true) { Validate(node); Parent = parentNode; if (!(node is XElement element)) { return; } var allAttributes = element.Attributes(); if (loadNamespaceMetadata) { var defaultNodeNamespace = element.GetDefaultNamespace(); var namespaceDeclarationAttributes = allAttributes.Where(a => a.IsNamespaceDeclaration); NodeMetadata = new Fb2NodeMetadata(defaultNodeNamespace, namespaceDeclarationAttributes); } if (!AllowedAttributes.Any()) { return; } var filteredAttributes = allAttributes .Where(attr => AllowedAttributes.Contains(attr.Name.LocalName, StringComparer.InvariantCultureIgnoreCase)) .Select(attr => { var attributeNamespace = loadNamespaceMetadata ? attr.Name.Namespace?.NamespaceName : null; return(new Fb2Attribute(attr.Name.LocalName, attr.Value, attributeNamespace)); }); if (!filteredAttributes.Any()) { return; } attributes.AddRange(filteredAttributes); }
private bool IsAttributeValid(string attributeName, string value) { if (AllowedAttributes != null) { // No attributes are allowed if (!AllowedAttributes.Any()) { return(false); } if (!AllowedAttributes.Contains(attributeName)) { return(false); } } if (RequiredAttributes != null) { if (value == null && RequiredAttributes.Contains(attributeName)) { // Don't delete any required attributes return(false); } } if (AllowedValues != null) { if (AllowedValues.TryGetValue(attributeName, out var allowed) && !allowed.Contains(value.Trim())) { return(false); } } if (DisallowedValues != null) { if (DisallowedValues.TryGetValue(attributeName, out var disallowed) && disallowed.Contains(value.Trim())) { return(false); } } return(true); }
private void ValidateAttributes(XElement element, SettingsFile origin) { if (AllowedAttributes != null) { if (!AllowedAttributes.Any() && element.HasAttributes) { throw new NuGetConfigurationException(string.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, string.Format(CultureInfo.CurrentCulture, Resources.NoAttributesAllowed, element.Name.LocalName, element.Attributes().Count()), origin.ConfigFilePath)); } foreach (var attribute in element.Attributes()) { if (!AllowedAttributes.Contains(attribute.Name.LocalName)) { throw new NuGetConfigurationException(string.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, string.Format(CultureInfo.CurrentCulture, Resources.AttributeNotAllowed, attribute.Name.LocalName, element.Name.LocalName), origin.ConfigFilePath)); } } } if (RequiredAttributes != null) { foreach (var requireAttribute in RequiredAttributes) { var attribute = element.Attribute(requireAttribute); if (attribute == null) { throw new NuGetConfigurationException(string.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, string.Format(CultureInfo.CurrentCulture, Resources.MissingRequiredAttribute, requireAttribute, element.Name.LocalName), origin.ConfigFilePath)); } } } if (AllowedValues != null) { foreach (var attributeValues in AllowedValues) { var attribute = element.Attribute(attributeValues.Key); if (attribute != null && !attributeValues.Value.Contains(attribute.Value.Trim())) { throw new NuGetConfigurationException(string.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, string.Format(CultureInfo.CurrentCulture, Resources.AttributeValueNotAllowed, attribute.Name.LocalName, attribute.Value.Trim(), element.Name.LocalName), origin.ConfigFilePath)); } } } if (DisallowedValues != null) { foreach (var attributeValues in DisallowedValues) { var attribute = element.Attribute(attributeValues.Key); if (attribute != null && attributeValues.Value.Contains(attribute.Value.Trim())) { throw new NuGetConfigurationException(string.Format(CultureInfo.CurrentCulture, Resources.UserSettings_UnableToParseConfigFile, string.Format(CultureInfo.CurrentCulture, Resources.AttributeValueNotAllowed, attribute.Name.LocalName, attribute.Value.Trim(), element.Name.LocalName), origin.ConfigFilePath)); } } } }