/*
 * (C) Copyright IBM Corp. 1997-1998  All rights reserved.
 *
 * US Government Users Restricted Rights Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * IBM will not be liable for any damages suffered by you as a result
 * of using the Program. In no event will IBM be liable for any
 * special, indirect or consequential damages or lost profits even if
 * IBM has been advised of the possibility of their occurrence. IBM
 * will not be liable for any third party claims against you.
 */

package com.ibm.xml.xpointer;

/**
 * (Based on <a href="http://www.w3.org/TR/1998/WD-xptr-19980303">WD-xptr-19980303</a>)
 *
 * @version 1.1
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 */

public class StringTerm implements OtherTerm, java.io.Serializable {
    boolean isAll = false;
    int instance;
    String str;

    boolean isPosition = false;
    boolean isEnd = false;
    int position;

    int length = -1;

    /**
     *
     * @param inst no sense when allp is <code>true</code>
     * @param position no sense when endp is <code>true</code>
     */
    public StringTerm(boolean allp, int inst, String str, boolean isposition, boolean endp, int position, int length) {
        this.isAll = allp;
        this.instance = inst;
        this.str = str;
        this.isPosition = isposition;
        this.isEnd = endp;
        this.position = position;
        this.length = length;
    }

    /**
     * <code>string(all,"...")</code> or <code>string(<var>digit</var>,"...")</code>
     * @param inst no sense when allp is <code>true</code>
     */
    public StringTerm(boolean allp, int inst, String str) {
        this(allp, inst, str, false, false, -1, -1);
    }

    /**
     * <code>string(...,"...",end)</code> or <code>string(...,"...",<var>digit</code>)</code>
     * @param inst no sense when allp is <code>true</code>
     * @param position no sense when endp is <code>true</code>
     */
    public StringTerm(boolean allp, int inst, String str, boolean endp, int position) {
        this(allp, inst, str, true, endp, position, -1);
    }

    /**
     * string(<var>digit</var>,"...")
     */
    public StringTerm(int inst, String str, boolean endp) {
        this.instance = inst;
        this.str = str;
        this.isEnd = true;
    }

    public boolean isAll() {
        return this.isAll;
    }
    /**
     * @return no sense when <code>isAll()</code> is <code>true</code>
     */
    public int getInstance() {
        return this.instance;
    }
    public String getString() {
        return this.str;
    }
    public boolean hasPosition() {
        return this.isPosition;
    }
    /**
     * @return no sense when <code>hasPosition()</code> is <code>false</code>
     */
    public boolean isEnd() {
        return this.isEnd;
    }
    /**
     * @return no sense when <code>isEnd()</code> is <code>true</code> or <code>hasPosition()</code> is <code>false</code>
     */
    public int getPosition() {
        return this.position;
    }
    /**
     * @return -1 when no length parameter.
     */
    public int getLength() {
        return this.length;
    }

    /**
     *
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(XPointer.literals[XPointer.ST_STRING]);
        sb.append("(");
        sb.append(this.isAll ? XPointer.S_ALL : Integer.toString(this.instance));
        sb.append(",");
        sb.append(XPointerParser.makeSkipLit(this.str));
        if (this.isPosition) {
            sb.append(",");
            sb.append(this.isEnd ? XPointer.S_END : Integer.toString(this.position));
            if (this.length >= 0) {
                sb.append(",");
                sb.append(Integer.toString(this.length));
            }
        }
        sb.append(")");
        return sb.toString();
    }

}
