package pipe.modules.predatorInvariantAnalysis;
/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.*;
import java.util.*;

public class InvariantXmlFileReader extends DefaultHandler {

  File fileName;
  String theFileName;
  boolean inscriptionFlag;
  boolean valueFlag;
  boolean subnetFlag;
  boolean markingFlag;
  String arcSource;
  String arcTarget;
  boolean tranTarget;  //true if the target is a transition
  //i.e if true then weight goes in C-
  int subnetLevel;

  Analysis data;
  boolean firstArc;
  public InvariantXmlFileReader(Analysis a){
    super();
    theFileName = "current.xml";
    fileName = new File(theFileName);
    data = a;
    inscriptionFlag = false;
    valueFlag = false;
    subnetFlag = false;
    firstArc = true;
    markingFlag = false;
    subnetLevel = 0;
    runFileReader();
  }

  public void runFileReader(){
    //System.out.println("RUNNING FILE READER");
    DefaultHandler handler = this;

    //Now use the default parser
    SAXParserFactory factory = SAXParserFactory.newInstance();

    try {
      SAXParser saxParser = factory.newSAXParser();
      saxParser.parse(fileName, handler);
    }catch (Throwable t){
      System.out.println("\nError reading file at position1");
      t.printStackTrace();
    }
  }

  public void startDocument() throws SAXException{
    //Is there anything to do at the start of the file????
    // data.clearAllVectors();
  }

  public void endDocument() throws SAXException {
    //Is there anything to do at the end of the document???
    if(firstArc)
      data.generateCPlusCMinus();
    data.addSubnetArcsToC();
  }


  //Parses start tags
  public void startElement(String namespaceURI,
                           String sName, // simple name (localName)
                           String qName, // qualified name
                           Attributes attrs)
      throws SAXException
  {
//    System.out.println("START ELEMENTTTTTT");
    String eName = sName;   //element name
//System.out.println(eName);
    //identify what the element is
    if("transition".equals(eName)){
      if (attrs != null){
        //System.out.println("transition found");
        String name = attrs.getValue(0);
        data.addTransitionName(name);
        //If invariant analysis is affected by immediate transitions then get
        //the type!!   Actually shoud immediate transitions be ignored???
      }

    }
    else
      if ("place".equals(eName)){
    if (attrs != null){
      //System.out.println("Place found");
      String name = attrs.getValue(0);
      data.addPlaceName(name);
      //System.out.println("At end of place");
    }

      }
      else
        if ("arc".equals(eName)){
      //System.out.println("At beginning of ARC");
      if (!subnetFlag && firstArc){
        firstArc = false;
        //now generate cplus and cminus
        data.generateCPlusCMinus();
      }
      if (attrs != null){
        String name = attrs.getValue(0);
        arcSource = attrs.getValue(1);
        arcTarget = attrs.getValue(2);
      }
      //now leave it until the initial marking flag is reached
        }
        else
          if ("inscription".equals(eName)){
        inscriptionFlag = true;
          }
          else
            if ("value".equals(eName)){
          valueFlag = true;
          // if (initialMarkingFlag){

          // }
            }
            else
              if ("subnet".equals(eName)){
            subnetFlag = true;
            subnetLevel++;
              }
              else
                if ("initialMarking".equals(eName)){
              markingFlag = true;
              //  System.out.println("markingFlag set true");
                }

  }
  //End of function///////////

  public void characters(char buf[], int offset, int Len){
    //System.out.println("Characters entered");
    if (valueFlag && inscriptionFlag){
      //System.out.println("Inside the if statement!!!!");
      String value = new String(buf, offset, Len);
      //System.out.println("The value is " + value);
      Integer temp = Integer.valueOf(value);
      int theValue = temp.intValue();
      //data.addArcValue(theValue);
      if (!subnetFlag)
        data.addDataToC(arcSource, arcTarget, tranTarget, theValue);
      else {
        data.subnetArcSource.addElement(arcSource);
        data.subnetArcTarget.addElement(arcTarget);
        data.subnetArcWeight.addElement(temp);
      }
    }
    //System.out.println("Characters exited");


    if (valueFlag && markingFlag){
      String value = new String(buf, offset, Len);
      // System.out.println("Inside characters - MarkingFlag and Value flag true");
      // System.out.println("value of marking is " + value);
      //Integer temp = Integer.getInteger(value);//new Integer(value);
      //System.out.println("Position A");
      Integer temp = Integer.valueOf(value);

      //  System.out.println("Piosition B");
      data.addToPlaceMarkings(temp);
      // System.out.println("Position C");
      //int theValue = temp.intValue();
      //newComponent.setValue(theValue, this);
    }

  }



  //Parses end tag elements
  public void endElement(String namespaceURI,
                         String sName, // simple name
                         String qName  // qualified name
                         )
      throws SAXException
  {
    String eName = sName;

    //identify what the element is
    if("transition".equals(eName)){
    }
    else
      if ("place".equals(eName)){
      }
      else
        if ("arc".equals(eName)){
        }
        else
          if ("inscription".equals(eName)){
        inscriptionFlag = false;
          }
          else
            if ("value".equals(eName)){
          valueFlag = false;
            }
            else
              if ("initialMarking".equals(eName)){
            markingFlag = false;
            //System.out.println("marking flag set false");
              }
              else
                if ("subnet".equals(eName)){
              subnetLevel--;
              if (subnetLevel == 0)
                subnetFlag = false;
                }
  }

}