Skip to content

Split XML node data of ElementInfo into NodeInfo, ElementInfo, and TextInfo  #163

@Spartan322

Description

@Spartan322

As it stands and was said by Fayti1703 the current structure of ElementInfo has too much going on in it. A text node doesn't support names, children, or attributes and an element node doesn't intrinsically represent a value/content. (leaving attributes because they're more useful on the element) As a result it makes more sense to treat them distinctly and only modify their behaviors.

Proposal:

class NodeInfo
{
    public ulong NodeId { get; }
    public XmlNodeType Type { get; set; }
    public NodeInfo Parent { get; protected set; }

    public NodeInfo(NodeInfo parent, XmlNodeType type);

    public virtual bool TrySetParent(NodeInfo info);
    public NodeInfo SetParent(NodeInfo info);

    public virtual void WriteToXML(XmlWriter writer);
    public override string ToString();
}
class ElementInfo : NodeInfo
{
    public string Name { get; set; }
    public readonly Dictionary<string, string> Attributes;
    public readonly List<NodeInfo> Children;

   public ElementInfo(string name, Dictionary<string, string> attributes = null, List<NodeInfo> children = null, NodeInfo parent = null);

    public virtual string GenerateContent();

    public virtual bool TryAttributeAsBoolean(string attribName, out bool result);
    public virtual bool TryAttributeAsInt(string attribName, out int result);
    public virtual bool TryAttributeAsFloat(string attribName, out float result);
    public bool GetAttributeAsBoolean(string attribName, bool defaultVal = default);
    public int GetAttributeAsInt(string attribName, int defaultVal = default);
    public float GetAttributeAsFloat(string attribName, float defaultVal = default);
    public bool AttributeAsBoolean(string attribName);
    public int AttributeAsInt(string attribName);
    public float AttributeAsFloat(string attribName);

    public virtual bool TryAddChild(NodeInfo child);
    public virtual bool TrySetAttribute(string key, string value);
    public ElementInfo AddChild(NodeInfo child);
    public virtual string GetAttribute(string key, string defaultValue = null);
    public ElementInfo SetAttribute(string key, string value);
}
class TextInfo : NodeInfo
{
    public string Content { get; set; }

    public TextInfo(string content, NodeInfo parent = null);

    public virtual bool TryContentAsBoolean(out bool result);
    public virtual bool TryContentAsInt(out int result);
    public virtual bool TryContentAsFloat(out float result);
    public bool GetContentAsBoolean(bool defaultVal = default);
    public int GetContentAsInt(int defaultVal = default);
    public float GetContentAsFloat(float defaultVal = default);
    public bool ContentAsBoolean();
    public int ContentAsInt();
    public float ContentAsFloat();
}

If there is anything anyone wishes to be added please comment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions