/// <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> /// Removes <paramref name="fb2Attribute"/> from given node. /// </summary> /// <param name="fb2Attribute">Attribute to remove.</param> /// <returns>Current node.</returns> /// <exception cref="ArgumentNullException"></exception> public Fb2Node RemoveAttribute(Fb2Attribute fb2Attribute) { if (fb2Attribute == null) { throw new ArgumentNullException(nameof(fb2Attribute)); } if (attributes.Contains(fb2Attribute)) { attributes.Remove(fb2Attribute); } return(this); }
/// <summary> /// Checks if given node has particular <paramref name="fb2Attribute"/>. /// </summary> /// <param name="fb2Attribute"><c>Fb2Attribute</c> to look for.</param> /// <returns><see langword="true"/> if <c>Fb2Node</c> has given <paramref name="fb2Attribute"/>, otherwise <see langword="false"/>.</returns> /// <exception cref="ArgumentNullException"></exception> public bool HasAttribute(Fb2Attribute fb2Attribute) { if (fb2Attribute == null) { throw new ArgumentNullException(nameof(fb2Attribute)); } if (!attributes.Any()) { return(false); } var hasAttribute = attributes.Contains(fb2Attribute); return(hasAttribute); }
/// <summary> /// Adds single attribute using <paramref name="key"/>, <paramref name="value"/> and <paramref name="namespaceName"/>. /// </summary> /// <param name="key">Attribute key to add.</param> /// <param name="value">Attribute value to add.</param> /// <param name="namespaceName"> /// <para>Optional, can be <see langword="null"/>.</para> /// <para>NamespaceName for attribute, used by <see cref="ToXml"/> serialization.</para></param> /// <returns>Current node.</returns> public Fb2Node AddAttribute(string key, string value, string?namespaceName = null) { var fb2Attribute = new Fb2Attribute(key, value, namespaceName); return(AddAttribute(fb2Attribute)); }