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

import org.w3c.dom.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;

/**
 * The TXPI class implements the PI interface as defined by the Document Object Model (DOM),
 * and implements the Namespace interface as defined by the W3C.
 * <p>A PI Node is a "processing instruction" for use by XML4J or an application. 
 * The content of the PI Node is the entire content between the delimiters of the processing 
 * instruction.
 *
 * @version 1.1
 * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
 * @see org.w3c.dom.PI
 * @see com.ibm.xml.parser.Namespace
 * @see com.ibm.xml.parser.Child
 */
public class TXPI extends Child implements PI, Namespace {

            String  name            = null;
            String  data            = null;
            String  nsLocalName     = null;
            String  nsURI           = null;

    /**
     * Constructor.
     * @param name      The first token following the markup.
     * @param data      From the character immediately after <var>name</var> to the 
     *                    character immediately preceding the <code>?&gt;</code>.
     */
    public TXPI(String name, String data) {
        this.name = name;
        this.data = data;
    }

    /**
     * Clone this PI Node using the appropriate factory.
     * <p>This method is defined by Child.
     * @return          Cloned PI Node.
     * @see com.ibm.xml.parser.Child#clone
     */
    public synchronized Object clone() {
        checkFactory();
        TXPI pi = factory.createPI(this.name, this.data);
        pi.setFactory(getFactory());
        //pi.setNSPrefixName(getNSPrefixName());
        pi.setNSLocalName(getNSLocalName());
        pi.setNSName(getNSName());
        return pi;
    }

    /**
     * Returns that this object is a PI Node.
     * <p>This method is defined by DOM.
     * @return          PI Node indicator.
     */
    public int getNodeType() {
        return Node.PI;
    }

    /**
     * Returns the qualified name of this PI.  The name is the first token following the markup.
     * If this PI's name has a namespace prefix, the prefix will still be attached.
     * <p>This method is defined by Namespace.
     * @return          The PI name, or <var>null</var> if no name.
     * @see #setName
     */
    public String getName() {
        return this.name;
    }

    /**
     * Sets the name of the PI.  The name is the first token following the markup.
     * @param name      The PI name.
     * @see #getName
     */
    public void setName(String name) {
        this.name       = name;
        clearDigest();
    }

    /**
     * Returns the data of the PI.  The PI data is from the character immediately after the 
     * PI name to the character immediately preceding the <code>?&gt;</code>.
     * @return          The PI data, or <var>null</var> if no data.
     * @see #setData
     */
    public String getData() {
        return this.data;
    }

    /**
     * Sets the data of the PI.  The PI data is from the character immediately after the 
     * PI name to the character immediately preceding the <code>?&gt;</code>.
     * @param data      The PI data.
     * @see #getData
     */
    public void setData(String data) {
        this.data = data;
        clearDigest();
    }

    /**
     * Return all text associated with this Node without considering entities.                                           
     * <p>This method is defined by Child.
     * @return          Always returns <var>""</var>.
     * @see com.ibm.xml.parser.Child#toString
     */
    public String getText() {
        return "";
    }

    /**
     * Returns the local name of the Namespace.
     * <p>This method is defined by Namespace.
     * @return          The Namespace local name, or <var>null</var> if no local name. 
     * @see com.ibm.xml.parser.Namespace
     * @see #setNSLocalName
     */
    public String getNSLocalName() {
        return this.nsLocalName;
    }
    
    /**
     * Sets the local name of the Namespace.  
     * <p>This method is defined by Namespace.
     * @param   nsLocalName The Namespace local name.
     * @see com.ibm.xml.parser.Namespace
     * @see #getNSLocalName
     */
    public void setNSLocalName(String nsLocalName) {
        this.nsLocalName = nsLocalName;
    }
    
    /**
     * Returns the Namespace URI.  
     * <p>This method is defined by Namespace.
     * @return          The Namespace URI, or <var>null</var> if the tag name does not
     *                  contain a prefix name or XML4J parser namespace processing is disabled.
     * @see #setNSName 
     * @see com.ibm.xml.parser.Parser#setProcessNamespace
     * @see com.ibm.xml.parser.Namespace
     */
    public String getNSName() {
        return this.nsURI;
    }
    /**
     * Sets the Namespace URI.  
     * <p>This method is defined by Namespace.
     * @param   nsURI   The Namespace URI, or <var>null</var> if no name.
     * @see #getNSName 
     * @see com.ibm.xml.parser.Namespace
     */
    public void setNSName(String nsURI) {
        this.nsURI = nsURI;
    }
    
    /**
     * If namespace support is enabled and the PI's name contains a prefix name, return a string 
     * of <CODE>getNSName()+":"+getNSLocalName()</CODE>; otherwise, return <CODE>getNSLocalName()</CODE> .
     * @return          The universal name, or <var>null</var> if this PI's name does not
     *                  contain a prefix name or XML4J parser namespace processing is disabled.
     * @see #getNSName
     * @see #getNSLocalName
     * @see com.ibm.xml.parser.Parser#setProcessNamespace
     */
    public String getUniversalName() {
        return getNSName() == null ? getNSLocalName() : getNSName()+":"+getNSLocalName();
    }
}
