1 package com.genimen.djeneric.repository.sqlparser.core; 2 3 import java.util.ArrayList ; 4 import java.util.EmptyStackException ; 5 6 public class TokenContext extends ArrayList 7 { 8 private static final long serialVersionUID = 1L; 9 public static final int STMT_TYPE_SELECT = 0; 10 public static final int STMT_TYPE_INSERT = 1; 11 public static final int STMT_TYPE_UPDATE = 2; 12 public static final int STMT_TYPE_DELETE = 3; 13 14 public static final String STMTSELECT = ".STMTSELECT"; 15 public static final String SELECT = ".SELECT"; 16 public static final String SELECTLIST = ".SELECTLIST"; 17 public static final String TABLESPEC = ".TABLESPEC"; 18 public static final String WHERE = ".WHERE"; 19 public static final String EXISTS = ".EXISTS"; 20 public static final String IN = ".IN"; 21 public static final String GROUPBY = ".GROUPBY"; 22 public static final String HAVING = ".HAVING"; 23 public static final String ORDERBY = ".ORDERBY"; 24 public static final String UNION = ".UNION"; 25 public static final String FUNCTION = ".FUNCTION"; 26 public static final String FUNCTIONARGS = ".FUNCTIONARGS"; 27 28 public static final String STMTINSERT = ".STMTINSERT"; 29 public static final String INSERTLIST = ".INSERTLIST"; 30 public static final String VALUES = ".VALUES"; 31 32 public static final String STMTUPDATE = ".STMTUPDATE"; 33 public static final String SET = ".SET"; 34 35 public static final String STMTDELETE = ".STMTDELETE"; 36 37 private int _statementIndex = -1; 38 39 public TokenContext() 40 { 41 } 42 43 public String push(String ctxt) 44 { 45 add(ctxt); 46 47 if (ctxt.equals(STMTSELECT) || ctxt.equals(STMTINSERT) || ctxt.equals(STMTUPDATE) || ctxt.equals(STMTDELETE)) _statementIndex++; 48 return ctxt; 49 } 50 51 public String pop() 52 { 53 String ctxt; 54 int len = size(); 55 56 ctxt = peek(); 57 remove(len - 1); 58 59 if (ctxt.equals(STMTSELECT) || ctxt.equals(STMTINSERT) || ctxt.equals(STMTUPDATE) || ctxt.equals(STMTDELETE)) _statementIndex--; 60 61 return ctxt; 62 } 63 64 69 public int getCurrentStatementIndex() 70 { 71 return _statementIndex; 72 } 73 74 public String peek() 75 { 76 int len = size(); 77 78 if (len == 0) throw new EmptyStackException (); 79 return (String ) get(len - 1); 80 } 81 82 public String peekAt(int pos) 83 { 84 return (String ) get(pos); 85 } 86 87 public boolean empty() 88 { 89 return size() == 0; 90 } 91 92 public String getFullContext() 93 { 94 StringBuffer current = new StringBuffer (100); 95 for (int i = 0; i < size(); i++) 96 { 97 current.append(peekAt(i)); 98 } 99 return current.toString(); 100 } 101 102 public boolean endsWith(String ctxt) 103 { 104 return getFullContext().endsWith(ctxt); 105 } 106 107 public boolean startsWith(String ctxt) 108 { 109 return getFullContext().startsWith(ctxt); 110 } 111 112 public boolean contains(String ctxt) 113 { 114 return (getFullContext().indexOf(ctxt) != -1); 115 } 116 117 public String toString() 118 { 119 String ctxt = getFullContext(); 120 if (ctxt.length() == 0) return "<NO CONTEXT>"; 121 return ctxt; 122 } 123 } | Popular Tags |