import java.awt.*;
import java.awt.event.*;
import java.applet.*;
// import XPath;
// import SimpleNode;
// import ParseException;
import java.io.*;

public class XPathApplet extends Applet {
  boolean isStandalone = false;
  Button button1 = new Button();
  TextArea textArea1 = new TextArea("", 100, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
  BorderLayout borderLayout1 = new BorderLayout();
  TextArea textArea2 = new TextArea("", 100, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public XPathApplet() {
  }
  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /**Component initialization*/
  private void jbInit() throws Exception {
    button1.setLabel("Parse");
    button1.addActionListener(new ParseBAL(this));
    textArea1.setRows(6);
    textArea1.setText("");
    this.setLayout(borderLayout1);
    textArea2.setText("Abstract Syntax Tree will appear here");
		textArea2.setRows(15);
    this.add(button1, BorderLayout.NORTH);
    this.add(textArea1, BorderLayout.CENTER);
    this.add(textArea2, BorderLayout.SOUTH);
    this.doLayout();
  }
    
  /**Start the applet*/
  public void start() {
  }
  /**Stop the applet*/
  public void stop() {
  }
  /**Destroy the applet*/
  public void destroy() {
  }
  /**Get Applet information*/
  public String getAppletInfo() {
    return "Applet Information";
  }
  /**Get parameter info*/
  public String[][] getParameterInfo() {
    return null;
  }
  
  void button1_action(ActionEvent evt) {
    
    try
    {
      String expr = textArea1.getText();
      XPath parser = new XPath(new java.io.StringBufferInputStream(expr));
      SimpleNode tree = parser.XPath2();
      if(null == tree)
        textArea2.setText("Error!");
      else
      {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(62*1024);
        PrintStream ps = new PrintStream(baos);
        tree.dump("|", ps);
        String s = new String(baos.toByteArray());
        textArea2.setText(s);
      }
    }
    catch(ParseException e)
    {
      textArea2.setText(e.getMessage());
    }
    catch(Error err)
    {
      textArea2.setText(err.getMessage());
    }
  }

}
