All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.bliki.wiki.tags.util.TagStack Maven / Gradle / Ivy

The newest version!
package info.bliki.wiki.tags.util;

import info.bliki.htmlcleaner.BaseToken;
import info.bliki.htmlcleaner.TagNode;
import info.bliki.htmlcleaner.TagToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Stack which contains the currently generated HTML/Wiki tags from the parsing
 * process.
 *
 */
public class TagStack extends ArrayList {

    /**
     * List of generated HTML/Wiki tags.
     */
    protected ArrayList fNodeList;

    private static final long serialVersionUID = 7377721039394435077L;

    public TagStack() {
        this(40, 80);
    }

    public TagStack(int initialStackCapacity, int initialNodelistCapacity) {
        super(initialStackCapacity);
        fNodeList = new ArrayList<>(initialNodelistCapacity);
    }

    /**
     * Looks at the TagToken at the top of this stack without
     * removing it from the stack.
     *
     * @return the object at the top of this stack.
     */
    public TagToken peek() {
        return get(size() - 1);
    }

    /**
     * Removes the TagToken at the top of this stack and returns that
     * TagToken.
     *
     * @return The TagToken at the top of this stack.
     */
    public TagToken pop() {
        if (size() > 0) {
            TagToken node = remove(size() - 1);
            if (size() == 0) {
                // last element in the list
                fNodeList.add(node);
            } else {
                TagToken topNode = peek();
                if (topNode instanceof TagNode) {
                    ((TagNode) topNode).addChild(node);
                } else {
                    throw new UnsupportedOperationException("No TagNode available!");
                }
            }
            return node;
        }
        return null;
    }

    /**
     * Pushes a TagToken item onto the top of this stack.
     *
     * @param item
     *          the TagToken item to be pushed onto this stack.
     * @return true if this collection changed as a result of the
     *         call
     */
    public boolean push(TagToken item) {
        return add(item);
    }

    /**
     * Pushes a new TagNode created from the nodeString onto the top
     * of this stack.
     *
     * @param nodeString
     *          the string for the new TagNode item to be pushed onto
     *          this stack.
     * @return true if this collection changed as a result of the
     *         call
     */
    public boolean push(String nodeString) {
        return add(new TagNode(nodeString));
    }

    /**
     * Return the internal list of nodes (size maybe 0)
     *
     * @return
     */
    public List getNodeList() {
        return fNodeList;
    }

    public void append(BaseToken contentNode) {
        if (size() > 0) {
            TagToken node = peek();
            if (node instanceof TagNode) {
                ((TagNode) node).addChild(contentNode);
            } else {
                throw new UnsupportedOperationException("No TagNode available for tag: " + node.getName());
            }
        } else {
            fNodeList.add(contentNode);
        }
    }

    public void append(TagStack stack) {
        for (int i = 0; i < stack.fNodeList.size(); i++) {
            append(stack.fNodeList.get(i));
        }
        // for (int i = 0; i < stack.size(); i++) {
        // append((BaseToken) stack.get(i));
        // }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy