01: package net.sourceforge.squirrel_sql.fw.completion.util;
02:
03: import org.apache.log4j.helpers.BoundedFIFO;
04:
05: import java.util.ArrayList;
06: import java.util.StringTokenizer;
07:
08: public class CompletionParser {
09: private static final String DEREF_CHAR = ".";
10:
11: private ArrayList<String> _tokens = new ArrayList<String>();
12: private String _stringToParse;
13: private int _stringToParsePosition;
14: private String _stringToReplace;
15: private String _textTillCarret;
16:
17: public CompletionParser(String textTillCarret) {
18: this (textTillCarret, false);
19: }
20:
21: public CompletionParser(String textTillCarret,
22: boolean completeQualified) {
23: _textTillCarret = textTillCarret;
24: _stringToParse = CompletionUtils
25: .getStringToParse(textTillCarret);
26: _stringToParsePosition = CompletionUtils
27: .getStringToParsePosition(textTillCarret);
28:
29: StringTokenizer st = new StringTokenizer(_stringToParse,
30: DEREF_CHAR);
31: _tokens = new ArrayList<String>();
32: while (st.hasMoreTokens()) {
33: _tokens.add(st.nextToken());
34: }
35:
36: if (textTillCarret.endsWith(DEREF_CHAR) || 0 == _tokens.size()) {
37: _tokens.add("");
38: }
39:
40: if (completeQualified) {
41: _stringToReplace = _stringToParse;
42: } else {
43: _stringToReplace = _tokens.get(_tokens.size() - 1);
44: }
45:
46: }
47:
48: /**
49: * True when _stringToParse contains a DEREF_CHAR.
50: */
51: public boolean isQualified() {
52: return 1 < _tokens.size();
53: }
54:
55: public String getStringToParse() {
56: return _stringToParse;
57: }
58:
59: public String getToken(int index) {
60: return _tokens.get(index);
61: }
62:
63: public int size() {
64: return _tokens.size();
65: }
66:
67: public int getStringToParsePosition() {
68: return _stringToParsePosition;
69: }
70:
71: public String getStringToReplace() {
72: return _stringToReplace;
73: }
74:
75: public int getReplacementStart() {
76: return _textTillCarret.length() - _stringToReplace.length();
77: }
78:
79: public String getTextTillCarret() {
80: return _textTillCarret;
81: }
82:
83: public String getLastToken() {
84: return _tokens.get(_tokens.size() - 1);
85: }
86:
87: public String getAllButFirst() {
88: String ret = _tokens.get(1);
89: for (int i = 2; i < _tokens.size(); i++) {
90: ret += DEREF_CHAR + _tokens.get(i);
91: }
92: return ret;
93: }
94: }
|