KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > sqlparser > core > TokenContext


1 package com.genimen.djeneric.repository.sqlparser.core;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.EmptyStackException JavaDoc;
5
6 public class TokenContext extends ArrayList JavaDoc
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 JavaDoc STMTSELECT = ".STMTSELECT";
15   public static final String JavaDoc SELECT = ".SELECT";
16   public static final String JavaDoc SELECTLIST = ".SELECTLIST";
17   public static final String JavaDoc TABLESPEC = ".TABLESPEC";
18   public static final String JavaDoc WHERE = ".WHERE";
19   public static final String JavaDoc EXISTS = ".EXISTS";
20   public static final String JavaDoc IN = ".IN";
21   public static final String JavaDoc GROUPBY = ".GROUPBY";
22   public static final String JavaDoc HAVING = ".HAVING";
23   public static final String JavaDoc ORDERBY = ".ORDERBY";
24   public static final String JavaDoc UNION = ".UNION";
25   public static final String JavaDoc FUNCTION = ".FUNCTION";
26   public static final String JavaDoc FUNCTIONARGS = ".FUNCTIONARGS";
27
28   public static final String JavaDoc STMTINSERT = ".STMTINSERT";
29   public static final String JavaDoc INSERTLIST = ".INSERTLIST";
30   public static final String JavaDoc VALUES = ".VALUES";
31
32   public static final String JavaDoc STMTUPDATE = ".STMTUPDATE";
33   public static final String JavaDoc SET = ".SET";
34
35   public static final String JavaDoc STMTDELETE = ".STMTDELETE";
36
37   private int _statementIndex = -1;
38
39   public TokenContext()
40   {
41   }
42
43   public String JavaDoc push(String JavaDoc 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 JavaDoc pop()
52   {
53     String JavaDoc 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   // This returns the index of the (sub)statement in the statement
65
// i.e.
66
// delete from .. where .. in (select .. from ..)
67
// consists of two statements; the delete is at pos 0, the select at 1
68

69   public int getCurrentStatementIndex()
70   {
71     return _statementIndex;
72   }
73
74   public String JavaDoc peek()
75   {
76     int len = size();
77
78     if (len == 0) throw new EmptyStackException JavaDoc();
79     return (String JavaDoc) get(len - 1);
80   }
81
82   public String JavaDoc peekAt(int pos)
83   {
84     return (String JavaDoc) get(pos);
85   }
86
87   public boolean empty()
88   {
89     return size() == 0;
90   }
91
92   public String JavaDoc getFullContext()
93   {
94     StringBuffer JavaDoc current = new StringBuffer JavaDoc(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 JavaDoc ctxt)
103   {
104     return getFullContext().endsWith(ctxt);
105   }
106
107   public boolean startsWith(String JavaDoc ctxt)
108   {
109     return getFullContext().startsWith(ctxt);
110   }
111
112   public boolean contains(String JavaDoc ctxt)
113   {
114     return (getFullContext().indexOf(ctxt) != -1);
115   }
116
117   public String JavaDoc toString()
118   {
119     String JavaDoc ctxt = getFullContext();
120     if (ctxt.length() == 0) return "<NO CONTEXT>";
121     return ctxt;
122   }
123 }
Popular Tags