001: package net.sourceforge.squirrel_sql.plugins.syntax.netbeans;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISyntaxHighlightTokenMatcher;
004: import net.sourceforge.squirrel_sql.client.session.ISession;
005: import net.sourceforge.squirrel_sql.client.session.SQLTokenListener;
006: import net.sourceforge.squirrel_sql.client.session.schemainfo.SchemaInfo;
007: import net.sourceforge.squirrel_sql.client.session.schemainfo.CaseInsensitiveString;
008:
009: import java.util.Hashtable;
010: import java.util.Vector;
011:
012: public class SqlSyntaxHighlightTokenMatcher implements
013: ISyntaxHighlightTokenMatcher {
014: private ISession _sess;
015: private NetbeansSQLEditorPane _editorPane;
016:
017: private CaseInsensitiveString _caseInsensitiveStringBuffer = new CaseInsensitiveString();
018:
019: private Hashtable<CaseInsensitiveString, String> _knownTables = new Hashtable<CaseInsensitiveString, String>();
020:
021: private Vector<SQLTokenListener> _sqlTokenListeners = new Vector<SQLTokenListener>();
022:
023: public SqlSyntaxHighlightTokenMatcher(ISession sess,
024: NetbeansSQLEditorPane editorPane) {
025:
026: _sess = sess;
027: _editorPane = editorPane;
028: }
029:
030: public boolean isTable(char[] buffer, int offset, int len) {
031: _caseInsensitiveStringBuffer.setCharBuffer(buffer, offset, len);
032:
033: // No new here, method is called very often
034: //if(_sess.getSchemaInfo().isTable(s))
035:
036: int tableExtRes = _sess.getSchemaInfo().isTableExt(
037: _caseInsensitiveStringBuffer);
038: if (SchemaInfo.TABLE_EXT_COLS_LOADED_BEFORE == tableExtRes
039: || SchemaInfo.TABLE_EXT_COLS_LOADED_IN_THIS_CALL == tableExtRes) {
040:
041: // _knownTables is just a cache to prevent creating a new String each time
042: String table = _knownTables
043: .get(_caseInsensitiveStringBuffer);
044: if (null == table) {
045: table = new String(buffer, offset, len);
046: _knownTables.put(new CaseInsensitiveString(table),
047: table);
048: }
049:
050: if (SchemaInfo.TABLE_EXT_COLS_LOADED_IN_THIS_CALL == tableExtRes) {
051: _editorPane.repaint();
052: }
053:
054: fireTableOrViewFound(table);
055:
056: return true;
057: }
058: return false;
059: }
060:
061: private void fireTableOrViewFound(String tableOrViewName) {
062: for (int i = 0; i < _sqlTokenListeners.size(); i++) {
063: SQLTokenListener sqlTokenListener = _sqlTokenListeners
064: .elementAt(i);
065: sqlTokenListener.tableOrViewFound(tableOrViewName);
066: }
067: }
068:
069: public void addSQLTokenListener(SQLTokenListener tl) {
070: removeSQLTokenListener(tl);
071: _sqlTokenListeners.add(tl);
072: }
073:
074: public boolean isFunction(char[] buffer, int offset, int len) {
075: _caseInsensitiveStringBuffer.setCharBuffer(buffer, offset, len);
076:
077: if (_sess.getSchemaInfo().isFunction(
078: _caseInsensitiveStringBuffer)) {
079: return true;
080: }
081: if (_sess.getSchemaInfo().isProcedure(
082: _caseInsensitiveStringBuffer)) {
083: return true;
084: }
085:
086: return false;
087: }
088:
089: public boolean isDataType(char[] buffer, int offset, int len) {
090: _caseInsensitiveStringBuffer.setCharBuffer(buffer, offset, len);
091:
092: if (_sess.getSchemaInfo().isDataType(
093: _caseInsensitiveStringBuffer)) {
094: return true;
095: }
096: return false;
097: }
098:
099: public boolean isStatementSeparator(char[] buffer, int offset,
100: int len) {
101: _caseInsensitiveStringBuffer.setCharBuffer(buffer, offset, len);
102:
103: String statSep = _sess.getQueryTokenizer()
104: .getSQLStatementSeparator();
105:
106: if (statSep.length() != len) {
107: return false;
108: }
109:
110: // no new here, method is called very often.
111: for (int i = 0; i < statSep.length(); ++i) {
112: if (buffer[offset + i] != statSep.charAt(i)) {
113: return false;
114: }
115: }
116:
117: return true;
118: }
119:
120: public boolean isColumn(char[] buffer, int offset, int len) {
121: // No new here, method is called very often
122: //String s = new String(buffer, offset, len);
123:
124: _caseInsensitiveStringBuffer.setCharBuffer(buffer, offset, len);
125:
126: if (_sess.getSchemaInfo()
127: .isColumn(_caseInsensitiveStringBuffer)) {
128: return true;
129: }
130: return false;
131: }
132:
133: public boolean isKeyword(char[] buffer, int offset, int len) {
134: // No new here, method is called very often
135: // String s = new String(buffer, offset, len);
136:
137: _caseInsensitiveStringBuffer.setCharBuffer(buffer, offset, len);
138: if (_sess.getSchemaInfo().isKeyword(
139: _caseInsensitiveStringBuffer)) {
140: return true;
141: }
142: return false;
143:
144: }
145:
146: public void removeSQLTokenListener(SQLTokenListener tl) {
147: _sqlTokenListeners.remove(tl);
148: }
149:
150: }
|