/*
 * (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;

import org.w3c.dom.Node;
import java.util.Vector;

/**
 * This class represents nodes pointed by an XPointer.
 *
 * @version 1.1
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 */
public class Pointed extends Vector {
    public static final int T_NODE = 0;
    public static final int T_STRINGINNODE = 1;
    public static final int T_STRING = 2;

    /**
     *
     */
    public static class Item {
        /**
         *
         * @version 1.1
         * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
         */
        public int type;
        /**
         *
         */
        public Node node;
        /**
         *
         */
        public String string;
        /**
         * valid when T_STRINGINNODE.
         */
        int offset;
        /**
         * valid when T_STRINGINNODE.
         */
        int length;

        /**
         *
         * <DL>
         *   <DT>for T_NODE:</DT>
         *   <DD>new Item(node, null, 0, 0);</DD>
         *   <DT>for T_STRINGINNODE:</DT>
         *   <DD>new Item(node, "foo", 5, 3);</DD>
         *   <DT>for T_STRING:</DT>
         *   <DD>new Item(null, "foo", 0, 0);</DD>
         * </DL>
         */
        public Item(Node node, String str, int offset, int length) {
            this.node = node;
            this.string = str;
            if (node == null)
                this.type = T_STRING;
            else if (str == null)
                this.type = T_NODE;
            else {
                this.type = T_STRINGINNODE;
                this.offset = offset;
                this.length = length;
            }
        }

        /**
         *
         */
        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof Item))
                return false;
            boolean ret;
            Item it = (Item)obj;
            if (this.type != it.type)  return false;
            switch (this.type) {
              case T_NODE:
                ret = this.node == it.node;
                break;
              case T_STRINGINNODE:
                ret = this.node == it.node && this.offset == it.offset && this.length == it.length;
                break;
              case T_STRING:
                ret = this.string.equals(it.string);
                break;
              default:
                ret = false;
            }
            return ret;
        }
    }

    /**
     *
     */
    public boolean add(Node node, String str, int offset, int length) {
        Item it = new Item(node, str, offset, length);
        int ind = indexOf(it);
        if (ind >= 0) {
            return false;
        } else {
            addElement(it);
            return true;
        }
    }
    /**
     *
     */
    public boolean add(Node node) {
        return add(node, null, 0, 0);
    }
    /**
     *
     */
    public boolean add(String str) {
        return add(null, str, 0, 0);
    }

    /**
     *
     */
    public Item item(int index) {
        return (Item)elementAt(index);
    }
}
