/* Generated By:JavaCC: Do not edit this line. Parser.java */
package org.plenix.jsp.parser;

import com.ibm.xml.parser.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;

import org.plenix.jsp.Util;

public class Parser implements ParserConstants {
  private TXDocument document = null;
  private String language = null;
  private String version = null;

  public static final String JSP_NODE_NAME = "jsp-script";

  public String getLanguage()
  {
    return language;
  }

  public String getVersion()
  {
    return version;
  }

  public Document getDocument()
  {
    return document;
  }

  public static void main(String args[]) throws ParseException
  {
    // Parse arguments
    Reader input = null;
    PrintWriter output = null;

    String usage =
      "org.plenix.jsp.Parser " +
        "[ -i <input file> ] " +
        "[ -o <output file> ]";

    String Parser = "stdin";

    try
    {
      int argc = 0;

      // Input file
      if (argc < args.length && args[argc].equals("-i"))
      {
        if (++argc < args.length)
        {
          try
          {
            Parser = args[argc++];
            input = new FileReader(Parser);
          }
          catch (Exception e)
          {
            System.err.println("Error opening file " + args[argc - 1] + ": " + e.getMessage());
            System.exit(1);
          }
        }
        else
        {
          throw new ParseException(usage);
        }
      }
      else
      {
        input = new InputStreamReader(System.in);
      }

      // Output file
      if (argc < args.length && args[argc].equals("-o"))
      {
        if (++argc < args.length)
        {
          try
          {
            FileOutputStream fos = new FileOutputStream(args[argc++]);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            output = new PrintWriter(osw);
          }
          catch (Exception e)
          {
            System.err.println("Error opening file " + args[argc - 1] + ": " + e.getMessage());
            System.exit(1);
          }
        }
        else
        {
          throw new ParseException(usage);
        }
      }
      else
      {
        output = new PrintWriter(new OutputStreamWriter(System.out), true);
      }
    }
    catch (Exception e)
    {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    try
    {
      Parser parser = new Parser(input);

      parser.parse(Parser);

      Document document = parser.getDocument();

      ((TXDocument) document).printWithFormat(output);
      output.flush();
    }
    catch (Exception e)
    {
      System.err.println(e.getClass().getName());
      e.printStackTrace(System.err);
      System.exit(1);
    }
  }

  private Element createTextElement(String name, String data)
  {
    Element element = document.createElement(name);
    Text text = document.createTextNode(data);
    element.appendChild(text);

    return element;
  }

/* *** PARSER *** */
  final public void parse(String name) throws ParseException {
  language = System.getProperty("polyjsp.default.language");
  version = System.getProperty("polyjsp.default.version");

  if (language == null)
  {
    language = "java";
  }

  if (version == null)
  {
    version = "0.92";
  }

  document = new TXDocument();

  Element root = document.createElement(JSP_NODE_NAME);
  root.setAttribute("name", name);
  document.appendChild(root);

  Element body = document.createElement("body");

  Node node = null;
    label_1:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case BEGIN_DIRECTIVE:
        node = directive();
                             root.appendChild(node);
        break;
      case BEGIN_DECLARATION:
        node = declaration();
                               root.appendChild(node);
        break;
      case BEGIN_SCRIPTLET:
      case BEGIN_INCLUSION:
      case COMMENT:
      case PCDATA:
      case BEGIN_EMPTY_TAG:
      case OPEN_TAG:
        node = bodyRegion();
                              body.appendChild(node);
        break;
      default:
        jj_la1[0] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case BEGIN_DIRECTIVE:
      case BEGIN_SCRIPTLET:
      case BEGIN_DECLARATION:
      case BEGIN_INCLUSION:
      case COMMENT:
      case PCDATA:
      case BEGIN_EMPTY_TAG:
      case OPEN_TAG:
        ;
        break;
      default:
        jj_la1[1] = jj_gen;
        break label_1;
      }
    }
    jj_consume_token(0);
    root.appendChild(body);
  }

  final public Node bodyRegion() throws ParseException {
  Node  node;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case BEGIN_EMPTY_TAG:
      node = emptyTag();
      break;
    case OPEN_TAG:
      node = nonEmptyTag();
      break;
    case BEGIN_SCRIPTLET:
      node = scriptlet();
      break;
    case BEGIN_INCLUSION:
      node = inclusion();
      break;
    case COMMENT:
    case PCDATA:
      node = boilerplate();
      break;
    default:
      jj_la1[2] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    {if (true) return node;}
    throw new Error("Missing return statement in function");
  }

  final public Node boilerplate() throws ParseException {
  String boilerplate;
  Vector vector = new Vector();
    label_2:
    while (true) {
      boilerplate = pcData();
      vector.addElement(boilerplate);
      if (jj_2_1(2)) {
        ;
      } else {
        break label_2;
      }
    }
    int size = vector.size();
    DocumentFragment node = document.createDocumentFragment();

    for (int i= 0; i < size; i++)
    {
      String line = (String) vector.elementAt(i);
      node.appendChild(createTextElement("boilerplate", Util.stringEncode(line)));
    }

    {if (true) return node;}
    throw new Error("Missing return statement in function");
  }

  final public Node directive() throws ParseException {
  Attr attr;
  Node node = null;
    jj_consume_token(BEGIN_DIRECTIVE);
    attr = attribute();
    String name = attr.getName();
    String value = attr.getValue();

    if (name.equalsIgnoreCase("language"))
    {
      language = value.toLowerCase();
    }
    else if (name.equalsIgnoreCase("version"))
    {
      version = value.toLowerCase();
      TagDefinition.setVersion(version);
    }
    else
    {
      node = createTextElement(name, value);
    }
    jj_consume_token(END_TAG);
    {if (true) return node;}
    throw new Error("Missing return statement in function");
  }

  final public Element declaration() throws ParseException {
  Token t = null;
  String codeText = null;
    jj_consume_token(BEGIN_DECLARATION);
    codeText = code();
    jj_consume_token(END_DECLARATION);
     {if (true) return createTextElement("declaration", codeText.trim());}
    throw new Error("Missing return statement in function");
  }

  final public Element emptyTag() throws ParseException {
  Token token = null;
  Element element = null;
  String name = null;
  Attr attr = null;
    token = jj_consume_token(BEGIN_EMPTY_TAG);
    name = token.image.substring(1).toLowerCase();
    element = document.createElement(name);
    label_3:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case ATTRIBUTE_NAME:
        ;
        break;
      default:
        jj_la1[3] = jj_gen;
        break label_3;
      }
      attr = attribute();
                         element.setAttributeNode(attr);
    }
    jj_consume_token(END_TAG);
    {if (true) return element;}
    throw new Error("Missing return statement in function");
  }

  final public Element nonEmptyTag() throws ParseException {
  Node node = null;
  Attr attr = null;
  Token token = null;
  Element element = null;
  Element body = document.createElement("body");
    token = jj_consume_token(OPEN_TAG);
    String name = token.image.substring(1).toLowerCase();
    element = document.createElement(name);
    label_4:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case ATTRIBUTE_NAME:
        ;
        break;
      default:
        jj_la1[4] = jj_gen;
        break label_4;
      }
      attr = attribute();
                         element.setAttributeNode(attr);
    }
    jj_consume_token(END_TAG);
    label_5:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case BEGIN_SCRIPTLET:
      case BEGIN_INCLUSION:
      case COMMENT:
      case PCDATA:
      case BEGIN_EMPTY_TAG:
      case OPEN_TAG:
        ;
        break;
      default:
        jj_la1[5] = jj_gen;
        break label_5;
      }
      node = bodyRegion();
                         body.appendChild(node);
    }
    element.appendChild(body);
    jj_consume_token(CLOSE_TAG);
    {if (true) return element;}
    throw new Error("Missing return statement in function");
  }

  final public Element scriptlet() throws ParseException {
  Token token = null;
  Element element = null;
  String name = null;
  String code = null;
    token = jj_consume_token(BEGIN_SCRIPTLET);
      if (token.image.substring(2).equals("="))
      {
        name = "expression";
      }
      else
      {
        name = "scriptlet";
      }
    code = code();
    jj_consume_token(END_SCRIPTLET);
      if (name.equals("expression"))
      {
        code = code.trim();
      }

      element = document.createElement(name);
      Text text = document.createTextNode(code);
      element.appendChild(text);

      {if (true) return element;}
    throw new Error("Missing return statement in function");
  }

  final public Element inclusion() throws ParseException {
  Attr attr = null;
  Element element = null;
    jj_consume_token(BEGIN_INCLUSION);
                      element = document.createElement("include");
    label_6:
    while (true) {
      attr = attribute();
                         element.setAttributeNode(attr);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case ATTRIBUTE_NAME:
        ;
        break;
      default:
        jj_la1[6] = jj_gen;
        break label_6;
      }
    }
    jj_consume_token(END_TAG);
    {if (true) return element;}
    throw new Error("Missing return statement in function");
  }

  final public Attr attribute() throws ParseException {
  Attr attr = null;
  Token token = null;
  String name = null;
  String value = null;
    token = jj_consume_token(ATTRIBUTE_NAME);
                             name = token.image.toLowerCase();
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case EQUALS:
      jj_consume_token(EQUALS);
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case ATTRIBUTE_VALUE:
        token = jj_consume_token(ATTRIBUTE_VALUE);
          value = Util.unquote(token.image);
        break;
      case ATTRIBUTE_NAME:
        token = jj_consume_token(ATTRIBUTE_NAME);
          value = token.image;
        break;
      default:
        jj_la1[7] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      break;
    default:
      jj_la1[8] = jj_gen;
      ;
    }
    attr = document.createAttribute(name);
    attr.setValue(value);

    {if (true) return attr;}
    throw new Error("Missing return statement in function");
  }

  final public String pcData() throws ParseException {
  Token token = null;
    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    case PCDATA:
      token = jj_consume_token(PCDATA);
      break;
    case COMMENT:
      token = jj_consume_token(COMMENT);
      break;
    default:
      jj_la1[9] = jj_gen;
      jj_consume_token(-1);
      throw new ParseException();
    }
    {if (true) return token.image;}
    throw new Error("Missing return statement in function");
  }

  final public String code() throws ParseException {
  Token token = null;
  StringBuffer buffer = new StringBuffer();
    label_7:
    while (true) {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case UNQUOTED_CODE:
        token = jj_consume_token(UNQUOTED_CODE);
          buffer.append(token.image);
        break;
      case STRING:
        token = jj_consume_token(STRING);
          buffer.append(token.image);
        break;
      default:
        jj_la1[10] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case STRING:
      case UNQUOTED_CODE:
        ;
        break;
      default:
        jj_la1[11] = jj_gen;
        break label_7;
      }
    }
      {if (true) return buffer.toString();}
    throw new Error("Missing return statement in function");
  }

  final private boolean jj_2_1(int xla) {
    jj_la = xla; jj_lastpos = jj_scanpos = token;
    boolean retval = !jj_3_1();
    jj_save(0, xla);
    return retval;
  }

  final private boolean jj_3_1() {
    if (jj_3R_8()) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
    return false;
  }

  final private boolean jj_3R_10() {
    if (jj_scan_token(COMMENT)) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
    return false;
  }

  final private boolean jj_3R_9() {
    if (jj_scan_token(PCDATA)) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
    return false;
  }

  final private boolean jj_3R_8() {
    Token xsp;
    xsp = jj_scanpos;
    if (jj_3R_9()) {
    jj_scanpos = xsp;
    if (jj_3R_10()) return true;
    if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
    } else if (jj_la == 0 && jj_scanpos == jj_lastpos) return false;
    return false;
  }

  public ParserTokenManager token_source;
  ASCII_CharStream jj_input_stream;
  public Token token, jj_nt;
  private int jj_ntk;
  private Token jj_scanpos, jj_lastpos;
  private int jj_la;
  public boolean lookingAhead = false;
  private boolean jj_semLA;
  private int jj_gen;
  final private int[] jj_la1 = new int[12];
  final private int[] jj_la1_0 = {0x1f8,0x1f8,0x1d0,0x200,0x200,0x1d0,0x200,0xa00,0x400,0x180,0x30000,0x30000,};
  final private int[] jj_la1_1 = {0x30,0x30,0x30,0x0,0x0,0x30,0x0,0x0,0x0,0x0,0x0,0x0,};
  final private JJCalls[] jj_2_rtns = new JJCalls[1];
  private boolean jj_rescan = false;
  private int jj_gc = 0;

  public Parser(java.io.InputStream stream) {
    jj_input_stream = new ASCII_CharStream(stream, 1, 1);
    token_source = new ParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 12; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public void ReInit(java.io.InputStream stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 12; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public Parser(java.io.Reader stream) {
    jj_input_stream = new ASCII_CharStream(stream, 1, 1);
    token_source = new ParserTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 12; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 12; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public Parser(ParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 12; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  public void ReInit(ParserTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 12; i++) jj_la1[i] = -1;
    for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  }

  final private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken;
    if ((oldToken = token).next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      if (++jj_gc > 100) {
        jj_gc = 0;
        for (int i = 0; i < jj_2_rtns.length; i++) {
          JJCalls c = jj_2_rtns[i];
          while (c != null) {
            if (c.gen < jj_gen) c.first = null;
            c = c.next;
          }
        }
      }
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }

  final private boolean jj_scan_token(int kind) {
    if (jj_scanpos == jj_lastpos) {
      jj_la--;
      if (jj_scanpos.next == null) {
        jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
      } else {
        jj_lastpos = jj_scanpos = jj_scanpos.next;
      }
    } else {
      jj_scanpos = jj_scanpos.next;
    }
    if (jj_rescan) {
      int i = 0; Token tok = token;
      while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
      if (tok != null) jj_add_error_token(kind, i);
    }
    return (jj_scanpos.kind != kind);
  }

  final public Token getNextToken() {
    if (token.next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

  final public Token getToken(int index) {
    Token t = lookingAhead ? jj_scanpos : token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  }

  final private int jj_ntk() {
    if ((jj_nt=token.next) == null)
      return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  }

  private java.util.Vector jj_expentries = new java.util.Vector();
  private int[] jj_expentry;
  private int jj_kind = -1;
  private int[] jj_lasttokens = new int[100];
  private int jj_endpos;

  private void jj_add_error_token(int kind, int pos) {
    if (pos >= 100) return;
    if (pos == jj_endpos + 1) {
      jj_lasttokens[jj_endpos++] = kind;
    } else if (jj_endpos != 0) {
      jj_expentry = new int[jj_endpos];
      for (int i = 0; i < jj_endpos; i++) {
        jj_expentry[i] = jj_lasttokens[i];
      }
      boolean exists = false;
      for (java.util.Enumeration enum = jj_expentries.elements(); enum.hasMoreElements();) {
        int[] oldentry = (int[])(enum.nextElement());
        if (oldentry.length == jj_expentry.length) {
          exists = true;
          for (int i = 0; i < jj_expentry.length; i++) {
            if (oldentry[i] != jj_expentry[i]) {
              exists = false;
              break;
            }
          }
          if (exists) break;
        }
      }
      if (!exists) jj_expentries.addElement(jj_expentry);
      if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
    }
  }

  final public ParseException generateParseException() {
    jj_expentries.removeAllElements();
    boolean[] la1tokens = new boolean[39];
    for (int i = 0; i < 39; i++) {
      la1tokens[i] = false;
    }
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 12; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
          if ((jj_la1_1[i] & (1<<j)) != 0) {
            la1tokens[32+j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 39; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.addElement(jj_expentry);
      }
    }
    jj_endpos = 0;
    jj_rescan_token();
    jj_add_error_token(0, 0);
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = (int[])jj_expentries.elementAt(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  }

  final public void enable_tracing() {
  }

  final public void disable_tracing() {
  }

  final private void jj_rescan_token() {
    jj_rescan = true;
    for (int i = 0; i < 1; i++) {
      JJCalls p = jj_2_rtns[i];
      do {
        if (p.gen > jj_gen) {
          jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
          switch (i) {
            case 0: jj_3_1(); break;
          }
        }
        p = p.next;
      } while (p != null);
    }
    jj_rescan = false;
  }

  final private void jj_save(int index, int xla) {
    JJCalls p = jj_2_rtns[index];
    while (p.gen > jj_gen) {
      if (p.next == null) { p = p.next = new JJCalls(); break; }
      p = p.next;
    }
    p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
  }

  static final class JJCalls {
    int gen;
    Token first;
    int arg;
    JJCalls next;
  }

}
