package traduccion;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
/**
 * <p>Title: Generación de Coreografías de Servicios Web</p>
 *
 * <p>Description: Esta clase crea un nodo necesario para la visualizacion en
 * formato de árbol </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: UCLM</p>
 *
 * @author
 * @version 1.0
 */
public class ToolNode {
    private Node nodo;
    public static String[] tipos={"none","Element","Attr","Text","CDATA","EntityRef","Entity","ProcInstr","Comment","Document","DocType","DocFragment","Notation"};

    public ToolNode(Node n){
        nodo=n;
    }

    public ToolNode getParent(){
         return new ToolNode(nodo.getParentNode());
    }

    public int ChildrenCount(){
        if(nodo.getChildNodes()!=null) return nodo.getChildNodes().getLength();
        else return 0;
    }

    public ToolNode getChildren(int i){
        if(i<=ChildrenCount()) return new ToolNode(nodo.getChildNodes().item(i));
        else return null;
    }

    public int getIndex(ToolNode n){
        int contador=ChildrenCount();
        for(int i=0; i<contador; i++){
            ToolNode ni=(ToolNode)this.getChildren(i);
            if(ni==n) return i;
        }
        return -1;
      }

      public String getAttribute(int i){
          if(i<=nodo.getAttributes().getLength()){
              return nodo.getAttributes().item(i).getNodeValue();
          }
          else return null;
      }

      public int getAttributeCount(){
          return nodo.getAttributes().getLength();
      }

      public String toString(){
          String s;
          switch(nodo.getNodeType()){
              case 0: s=" ";
                      break;
              case 1: s=nodo.getNodeName();
                  if(nodo.hasAttributes()){
                      NamedNodeMap att=nodo.getAttributes();
                      for(int i=0; i<att.getLength(); i++){
                          s+=" "+ att.item(i).getNodeName()+"="+att.item(i).getNodeValue();
                       }
                 }
                 break;
              case 3: s=nodo.getNodeValue();
                  break;
              default: s=nodo.toString();
                  break;
          }
          return s;
      }
 }
