001: /*
002: * SqlKeywordHandler.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.io.File;
015: import java.io.FileInputStream;
016: import java.io.InputStream;
017: import java.sql.Connection;
018: import java.util.Collection;
019: import java.util.List;
020: import java.util.Set;
021: import java.util.TreeSet;
022: import workbench.WbManager;
023: import workbench.log.LogMgr;
024: import workbench.resource.Settings;
025: import workbench.util.StringUtil;
026: import workbench.util.TextlistReader;
027:
028: /**
029: *
030: * @author support@sql-workbench.net
031: */
032: public class SqlKeywordHandler {
033: private Set<String> keywords;
034:
035: public SqlKeywordHandler(Connection con) {
036: readKeywords(con, null);
037: }
038:
039: public SqlKeywordHandler(Connection con, String dbid) {
040: readKeywords(con, dbid);
041: }
042:
043: public Collection<String> getSqlKeywords() {
044: return this .keywords;
045: }
046:
047: /**
048: * Read the keywords for the current DBMS that the JDBC driver returns.
049: * If the driver does not return all keywords, this list can be manually
050: * extended by defining the property workbench.db.<dbid>.syntax.keywords
051: * with a comma separated list of additional keywords
052: */
053: private void readKeywords(Connection con, String dbId) {
054: this .keywords = new TreeSet<String>();
055: if (dbId != null) {
056: try {
057: String keys = con.getMetaData().getSQLKeywords();
058: List<String> keyList = StringUtil.stringToList(keys,
059: ",");
060: this .keywords.addAll(keyList);
061: } catch (Exception e) {
062: LogMgr
063: .logWarning("SqlKeywordHandler.readKeywords",
064: "Error reading SQL keywords: "
065: + e.getMessage());
066: }
067:
068: String userKeys = Settings.getInstance().getProperty(
069: "workbench.db. + " + dbId + ".syntax.keywords",
070: null);
071: if (userKeys != null) {
072: List<String> l = StringUtil.stringToList(userKeys
073: .toUpperCase(), ",");
074: this .keywords.addAll(l);
075: }
076: }
077:
078: // Read the base set of keywords (which cannot be configured)
079: try {
080: InputStream in = this .getClass().getResourceAsStream(
081: "SqlKeywords.txt");
082:
083: // TextlistReader will close the input stream
084: TextlistReader reader = new TextlistReader(in);
085: Collection<String> values = reader.getValues();
086: if (values != null)
087: this .keywords.addAll(values);
088:
089: // When running tests, the WbManager is not necessarily available
090: WbManager mgr = WbManager.getInstance();
091: if (mgr != null) {
092: File baseDir = new File(mgr.getJarPath());
093: File f = new File(baseDir, "SqlKeywords.txt");
094: if (f.exists()) {
095: LogMgr.logDebug("SqlKeywordHandler.readKeywords()",
096: "Reading addtional keywords from "
097: + f.getCanonicalPath());
098: in = new FileInputStream(f);
099: reader = new TextlistReader(in);
100: values = reader.getValues();
101: if (values != null)
102: this .keywords.addAll(values);
103: }
104: }
105: } catch (Exception e) {
106: LogMgr.logError("SqlKeywordHandler.readKeywords",
107: "Error reading SQL keywords", e);
108: }
109:
110: // Now remove any keywords that the user defined.
111:
112: try {
113: String words = Settings.getInstance()
114: .getProperty(
115: "workbench.db." + dbId
116: + ".syntax.nokeywords", null);
117: List l = StringUtil.stringToList(words, ",", true, true,
118: false);
119: this .keywords.removeAll(l);
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: public boolean isKeyword(String verb) {
126: if (verb == null)
127: return false;
128: if (this .keywords == null)
129: return false;
130: return this.keywords.contains(verb.trim().toUpperCase());
131: }
132:
133: }
|