/*
   Tamot --- Task Modelling Editor
   Copyright (C) 2000 Commonwealth Scientific and Industrial Research Organisation
   (CSIRO), Australia. All rights reserved. 

   The Software, owned by CSIRO Australia, has been developed by the CSIRO 
   Intelligent Interactive Technology group. 
   For more information, please see http://www.cmis.csiro.au/iit/.

   The Software is a beta-test version for internal research and evaluation purposes 
   by the Licensee. The Software is still at a development stage. 
   It has not undergone complete testing and may have inherent errors,
   bugs or deficiencies that can affect the operation of the Software. 

   We encourage your feedback and suggestions. Any bugs / suggestions found please email to
   Shijian.Lu@cmis.csiro.au and Thomas.Lo@cmis.csiro.au. 

   After you have signed the Beta-Test Software License Agreement with the CSIRO,
   CSIRO grants you a non-exclusive, non-transferable license to use it only for your 
   personal, non-commercial and lawful end use. Implied licenses are negated. 

   Warranty Disclaimer : 

   CSIRO supplies the Software on an "as is" basis and makes no warranties that use of
   the Software by the Licensee will not infringe any third party’s 
   Intellectual Property Rights nor that the Software will be of merchantable quality,
   or suitable for a particular purpose. CSIRO excludes all terms, conditions and
   warranties implied by custom, the general law or statute in relation to the
   Software.
*/

package domainModel;


import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import taskModellingTool.*;

import org.w3c.dom.*;
import com.ibm.xml.parsers.*;

/** 
* XIrregularNoun.java, it acts as a agent for communication
* between the IrregularNoun IrregularNoun classes and DOM Dmconcept element
* as defined by the NewDTDFile3.dtd
*/
public class XIrregularVerb
{
	private Document doc = null;

	public XIrregularVerb(Document aDoc){
		doc = aDoc;	
	}

	/** 
	* setDoc(), 
	* @PARAM: aDoc, the DOM doc 
	* @return void
	*/
	public void setDoc(Document aDoc)
  	{
		doc = aDoc;

  	} //setDoc()
	
	/** 
	* getDoc(), 
	* @return Document
	*/
	public Document getDoc()
  	{
		return doc;

  	} //getDoc()

	
	/** 
	* parse(), parses a XML document, if successful,
	* it'll procude a DOM document
	* @PARAM: uri, the file name of a XML doc to be parsed
	* @return void
	*/
	public void parse(String uri)
  	{
   	try
    	{
	      DOMParser parser = new DOMParser();
	      parser.parse(uri);
	      doc = parser.getDocument();
    	}
	   catch (Exception e)
	   {
	      System.err.println("Sorry, an error occurred: " + e);
	   }

  	} //parse()

  /** Prints the specified node, recursively. */
  public void printDOMTree(org.w3c.dom.Node node) 
  {
    int type = node.getNodeType();
    switch (type)
    {
      // print the document element
      case org.w3c.dom.Node.DOCUMENT_NODE: 
        {
          System.out.println("<?xml version=\"1.0\" ?>");
          
          //print doctype infor
          DocumentType tp= ((Document)node).getDoctype();
          if (tp!= null && 
          		tp.getSystemId() != null &&
          			tp.getName() != null){
          				
          		System.out.println("<!DOCTYPE " + 
          			tp.getName()+ " SYSTEM \""+
          			tp.getSystemId() + "\">");
          }

          printDOMTree(((Document)node).getDocumentElement());
          break;
        }

        // print element with attributes
      case org.w3c.dom.Node.ELEMENT_NODE: 
        {
          System.out.print("<");
          System.out.print(node.getNodeName());
          NamedNodeMap attrs = node.getAttributes();
          for (int i = 0; i < attrs.getLength(); i++)
          {
            org.w3c.dom.Node attr = attrs.item(i);
            System.out.print(" " + attr.getNodeName() + 
                             "=\"" + attr.getNodeValue() + 
                             "\"");
          }
          System.out.print(">");

          NodeList children = node.getChildNodes();
          if (children != null)
          {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
              printDOMTree(children.item(i));
          }

          break;
        }

        // handle entity reference nodes
      case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: 
        {
          System.out.print("&");
          System.out.print(node.getNodeName());
          System.out.print(";");
          break;
        }

        // print cdata sections
      case org.w3c.dom.Node.CDATA_SECTION_NODE: 
        {
          System.out.print("<![CDATA[");
          System.out.print(node.getNodeValue());
          System.out.print("]]>");
          break;
        }

        // print text
      case org.w3c.dom.Node.TEXT_NODE: 
        {
          System.out.print(node.getNodeValue());
          break;
        }

        // print processing instruction
      case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE: 
        {
          System.out.print("<?");
          System.out.print(node.getNodeName());
          String data = node.getNodeValue();
          {
            System.out.print(" ");
            System.out.print(data);
          }
          System.out.print("?>");
          break;
        }
    }

    if (type == org.w3c.dom.Node.ELEMENT_NODE)
    {
      System.out.print("</");
      System.out.print(node.getNodeName());
      System.out.print('>');
      System.out.println();

    }
  } // printDOMTree(Node)

  	
  	
  	/** 
  	* IrregularVerbToEle, gets a IrregularVerb object as input 
  	* and return a DOM IrregularVerb element. 
  	*/
	public  Element irregularVerbToEle(IrregularVerb aCon){
		if (aCon == null) return null;
		Element elem = null;		
		try {
			String IrregularVerbTag= XDmTag.getIrregularVerbT(); //"IrregularVerbTag";
			// create the IrregularVerbTag element with the right attributes
	  		elem = doc.createElement(IrregularVerbTag);
	      elem.setAttribute(XDmTag.getDmIdT(), aCon.getID());
	      	      	// replace special XML characters
		  String sTemp =aCon.getName();
		  sTemp=XMLUtility.convertForEntities(sTemp);
		    
	      elem.setAttribute(XDmTag.getDmNameT(), sTemp);	//aCon.getName());
	      if (aCon.getSpelling() != null){
	      	sTemp =aCon.getSpelling();
		 	sTemp=XMLUtility.convertForEntities(sTemp);
	      	elem.setAttribute(XDmTag.getSpellingT(), sTemp);	//aCon.getSpelling());
	      }
	      
	      if (aCon.getProp() != null){
	      		sTemp =aCon.getProp();
		    	sTemp=XMLUtility.convertForEntities(sTemp);

	      		elem.setAttribute(XDmTag.getPropT(),sTemp);	// aCon.getProp());
	      }
	      
	      if (aCon.get1stP() != null){
	      	sTemp =aCon.get1stP();
		 	sTemp=XMLUtility.convertForEntities(sTemp);

	      	elem.setAttribute(XDmTag.get1stPersonT(),sTemp);	// aCon.get1stP());
	      }
	      if (aCon.get3rdP() != null){
	      	sTemp =aCon.get3rdP();
		 	sTemp=XMLUtility.convertForEntities(sTemp);
	      
	      	elem.setAttribute(XDmTag.get3rdPersonT(),sTemp);	// aCon.get3rdP());
	      }
	      if (aCon.getPlural() != null){
	      	sTemp =aCon.getPlural();
		 	sTemp=XMLUtility.convertForEntities(sTemp);

	      	elem.setAttribute(XDmTag.getPluralT(),sTemp);	// aCon.getPlural());
	      }
	      if (aCon.getIng() != null){
	      	sTemp =aCon.getIng();
		 	sTemp=XMLUtility.convertForEntities(sTemp);

	      	elem.setAttribute(XDmTag.getIngT(), sTemp);	//aCon.getIng());
	      }
	      if (aCon.getPast() != null){
	      	sTemp =aCon.getPast();
		 	sTemp=XMLUtility.convertForEntities(sTemp);

	      	elem.setAttribute(XDmTag.getPastT(),sTemp);	// aCon.getPast());
	      }
	      if (aCon.getPp() != null){
	      	sTemp =aCon.getPp();
		 	sTemp=XMLUtility.convertForEntities(sTemp);

	      	elem.setAttribute(XDmTag.getPpT(), sTemp);	//aCon.getPp());
	      }
	      	
		}
		catch (Exception e)
		{
			System.err.println(e);
		}
		return elem;
  	}//IrregularVerbToEle()
  	  	
  	/** 
  	* eleToIrregularVerb, gets a IrregularVerb object as input 
  	* and return a DOM IrregularVerb object. 
  	* 
  	*/
	public  IrregularVerb eleToIrregularVerb(Element ele){
		if (ele == null) return null;
		IrregularVerb irregularVerb = null;
		if (ele.getTagName().equals(XDmTag.getIrregularVerbT())){	// if it is a getIrregularVerb element
			try{
				String conId= ele.getAttribute(XDmTag.getDmIdT());
				if (conId == null || conId == "") System.out.println("no ID");

				String name= ele.getAttribute(XDmTag.getDmNameT());
				String spelling= ele.getAttribute(XDmTag.getSpellingT());
				String person1= ele.getAttribute(XDmTag.get1stPersonT());
				String person3= ele.getAttribute(XDmTag.get3rdPersonT());
				String plural= ele.getAttribute(XDmTag.getPluralT());
				String ing= ele.getAttribute(XDmTag.getIngT());
				String past= ele.getAttribute(XDmTag.getPastT());
				String pp= ele.getAttribute(XDmTag.getPpT());
				String prop= ele.getAttribute(XDmTag.getPropT());
				
				//System.out.println("element Tag name: "+ele.getTagName());
	         // create the IrregularVerb object
				name= XMLUtility.checkAndConvertForEntities(name);
	        	irregularVerb= new IrregularVerb(conId,name);
	         
	         if (spelling != null){
	         	spelling= XMLUtility.checkAndConvertForEntities(spelling);
	         	irregularVerb.setSpelling(spelling);
	         }
	         if (prop != null){
	         	prop= XMLUtility.checkAndConvertForEntities(prop);
	         	irregularVerb.setProp(prop);
	         }
	         
	         if (person1 != null){
	         	person1= XMLUtility.checkAndConvertForEntities(person1);
	         	irregularVerb.set1stP(person1);
	         }
	         if (person3 != null){
	         	person3= XMLUtility.checkAndConvertForEntities(person3);
	         	irregularVerb.set3rdP(person3);
	         }
	         if (plural != null){
	         	plural= XMLUtility.checkAndConvertForEntities(plural);
	         	irregularVerb.setPlural(plural);
	         }
	         if (ing != null){
	         	ing= XMLUtility.checkAndConvertForEntities(ing);
	         	irregularVerb.setIng(ing);
	         }
	         if (past != null){
	         	past= XMLUtility.checkAndConvertForEntities(past);
	         	irregularVerb.setPast(past);
	         }
	         if (pp != null){
	         	pp= XMLUtility.checkAndConvertForEntities(pp);
	         	irregularVerb.setPp(pp);
	         }
			}
			catch (Exception e)
			{
				System.err.println(e);
			}
			
		}
		return irregularVerb;
	}//eleToIrregularVerb()
  	
  	/** 
  	* add(), add an IrregularVerb element to the doc. it 
  	* first try to add before the first Entity element, 
  	* if no Entity elements present, then try to add before 
  	* the first Action element, otherwise, simply append to 
  	* the Domain_model's children list. If Domain_model element
  	* is not present, the element is not added.
  	*/
  	public boolean add(Element ele){
		if (getDoc() == null || ele == null) return false;
		if (!(XManager.append(getDoc(), ele))) return false;
	 	return true;
  	}//add()
  	
  	/** remove(), remove an element from Domain_model's chidren 
  	* list if it exists. 
  	*/
  	public void remove(String iD){
		if (doc == null || iD == null) return;
		try {
	      //get the Domain_model element.
	      Element theE = doc.getElementById(iD);
	    	if (theE != null)
	    	{	
	         theE.getParentNode().removeChild(theE);
	    	}
		}
		catch (Exception e)
		{
			System.err.println(e);
		}
  		
  	}//remove()
  	
  	
  	

  	/**
  	* Main program entry point. 
  	*/
  public static void main(String argv[]) 
  {
  	/*
    if (argv.length == 0)
    {
      System.out.println("Usage:  java domOne uri");
      System.out.println("   where uri is the URI of the XML document you want to print.");
      System.out.println("   Sample:  java domOne sonnet.xml");
      System.exit(1);
    }

    XConcept d1 = new XConcept(XManager.parse(argv[0]));
    DmConcept dia = new DmConcept("dialog", "entity type");
    DmConcept win = new DmConcept("window", "entity type");
	win.setNote("this is a test for adding concept with DOM");
    
  	dia.setParent(win);

  //	XManager.insertBAction(d1.getDoc(),d1.conToEle(win));
  //	XManager.insertBEntity(d1.getDoc(),d1.conToEle(dia));
  	d1.add(d1.conToEle(win));
  	d1.add(d1.conToEle(dia));
  	
  	Element c1 = d1.getDoc().getElementById("dmC1");
  	if (c1 == null) System.out.println("no element");
  	XManager.remove(d1.getDoc(),"dmC1");
  	if (d1.eleToCon(c1) == null) System.out.println("no concept");
  	XManager.insertBEntity(d1.getDoc(),d1.conToEle(d1.eleToCon(c1)));

  	XManager.printDOMTree(d1.getDoc());
*/
  }
}