/*
 * TemplateFrame.java
 * 
 * Created on Oct 31, 2007, 8:09:11 PM
 */

package saxon.xslt.stacktrace;

import javax.xml.namespace.QName;
import org.xml.sax.Locator;

/**
 * Represent an XSLT stack frame for a template.
 *
 * @author Florent Georges
 */
public class TemplateFrame
        extends StackFrame
{
    /**
     * Build a new template frame.
     * 
     * @param curr_item
     *             The path to the current node if any.
     * 
     * @param pattern
     *             The pattern the template matches, if it is a template rule.
     * 
     * @param name
     *             The name of the template, if it is a named template.
     * 
     * @param mode
     *             The mode of the template, if any.
     * 
     * @param locator
     *             The SAX locator for the template.
     */
    public TemplateFrame(String curr_item, String pattern, QName name, QName mode, Locator locator)
    {
        myPath = curr_item;
        myPattern = pattern;
        myName = name;
        myMode = mode;
        myLocator = locator;
        myCalled = false;
    }

    /**
     * Return the pattern of the template, if it is a template rule.
     */
    public String getPattern()
    {
        return myPattern;
    }

    /**
     * Return the name of the template if it is a named template.
     */
    public QName getName()
    {
        return myName;
    }

    /**
     * Return the mode's name of the template, if it is a template rule and has a mode.
     */
    public QName getMode()
    {
        return myMode;
    }

    /**
     * Set the mode of the template.
     */
    public void setMode(QName mode)
    {
        myMode = mode;
    }

    /**
     * Return {@code true} if the template was called, {@code false} if it was applied.
     */
    public boolean isCalled()
    {
        return myCalled;
    }

    /**
     * Set the {@code isCalled} property of the template (see {@link #isCalled()}).
     */
    public void setCalled(boolean called)
    {
        myCalled = called;
    }

    @Override
    public String toString()
    {
        StringBuilder buf = new StringBuilder("template ");
        if ( myName != null ) {
            buf.append(displayQName(myName)).append(' ');
        }
        if ( myMode != null ) {
            buf.append('#').append(displayQName(myMode)).append(' ');
        }
        if ( myPattern != null ) {
            buf.append("matching ").append('\"').append(myPattern).append('\"');
        }
        return buf.toString();
    }

    public void acceptVisitor(StackVisitor visitor)
    {
        visitor.visitTemplateFrame(this);
        if ( getNext() != null ) {
            getNext().acceptVisitor(visitor);
        }
    }

    private String myPattern;
    private QName myName;
    private QName myMode;
    private boolean myCalled;
}
