package com.srbenoit.xml;

/**
 * An empty element, characterized by a tag of the form &lt; ... /&gt;.
 */
public class EmptyElement extends ElementBase implements Node {

    /** version number for serialization */
    private static final long serialVersionUID = 2526807668030298751L;

    /** the tag span of the element */
    public final transient TagSpan tagSpan;

    /**
     * Constructs a new <code>EmptyElement</code>. /** Constructs a new <code>Element</code>.
     *
     * @param  tag   the parsed tag name
     * @param  span  the tag span for the element's tag
     */
    public EmptyElement(final String tag, final TagSpan span) {

        super(tag);

        this.tagSpan = span;
    }

    /**
     * Generates the print representation of the node (may recursively call this method on child
     * nodes).
     *
     * @param   xml  the source XML
     * @return  the print representation
     */
    public String print(final String xml) {

        StringBuilder str;
        String key;
        String value;

        str = new StringBuilder(200);

        str.append('<');
        str.append(this.tagName);

        for (String attr : keySet()) {

            key = encode(attr);
            value = encode(get(attr));

            str.append(' ');
            str.append(key);
            str.append('=');
            str.append(value.contains("'") ? "\"" : "'");
            str.append(value);
            str.append(value.contains("'") ? "\"" : "'");
        }

        str.append("/>");

        return str.toString();
    }
}
