001: package isql;
002:
003: import java.io.*;
004: import java.util.*;
005: import util.*;
006: import javax.swing.KeyStroke;
007:
008: /** reads up the rcfile and populates preferences
009: * Added hook feature on 2001-11-18 - but this is minimal. we still need
010: * to load all hooks so that they can be used at runtime. This would
011: * include table hooks, column hooks, database hooks, user hooks,
012: * database version hooks etc.
013: * RK added on 20040111 13:14:22
014: * Major restructuring done to enable us to source a file fro the main
015: * ini file. The const only initializes, and doesnt do any work (the
016: * right way). parseFile does the work.
017: */
018: public class ReadTNS {
019: // Read line by line and break into strings
020: Map htParams = new HashMap(20);
021: Map htAbbrs = new HashMap(20);
022: Map htBookmarks = new HashMap();
023: Map htLinks;
024: Map htSorts;
025: Map htFilters;
026: Bindings htBindings;
027: /** list of actions to be created */
028: List /*<Map>*/liActions;
029: /** name of base ini file.
030: * will be required by other programs to append a property.
031: */
032: String _inifile = "mysql.ini";
033: /** list of all files sourced including base one, so that user can
034: * add to any of them.
035: */
036: List /*<String>*/_sourcedfilenames = new ArrayList();
037:
038: public static void main(String args[]) {
039: Map htParams;
040: String outfile = "";
041: if (args.length == 0) {
042: System.err.println("INI Filename required !");
043: System.exit(-1);
044: }
045: outfile = args[0];
046: //System.err.println("Filename = " + outfile);
047: //htParams = (new ReadTNS(outfile)).getParams() ;
048: ReadTNS temp = new ReadTNS(outfile);
049: temp.parseFile();
050: htParams = temp.getParams();
051:
052: /* this is how to fetch parameters */
053: System.out.println("DSN: " + htParams.get("DSN"));
054: System.out.println("UID: " + htParams.get("UID"));
055: System.out.println("DRIVER: " + htParams.get("DRIVER"));
056: Map htBM = temp.getBookmarks();
057: System.out.println("sql: " + htBM.get("sql1"));
058: System.out.println("file: " + htBM.get("file1"));
059: System.out.println("system: " + htBM.get("system1"));
060:
061: }
062:
063: /** ReadTNS takes the foll parameters:
064: * String - name of file containing Oracle locations and db params -
065: * the base ini file.
066: * Ideally this should throw a FNF exception.
067: * // HashMap - to return the key and value of params. We assume one param per key
068: */
069: public ReadTNS(String outfile) {
070: _inifile = outfile;
071: }
072:
073: /** method to parse the ini file, after using the constructor that
074: * takes a filename.
075: */
076: public void parseFile() {
077: if (_inifile != null)
078: parseFile(_inifile);
079: else
080: System.err.println("ReadTNS: ERROR no file to parse");
081: }
082:
083: /** parse the given file.
084: * This file will not be stored as the ini file, since this could be
085: * a sourced file.
086: * This restructuring was done so that we can source a file from the
087: * base ini file.
088: */
089: public void parseFile(String newfile) {
090:
091: StringTokenizer st;
092: LineNumberReader lnr = null;
093: int i = 0;
094: String str;
095: try {
096:
097: lnr = new LineNumberReader(new FileReader(newfile));
098: }
099:
100: catch (FileNotFoundException e) {
101: System.err
102: .println("File not found Error !" + newfile + ".");
103:
104: return;
105: }
106: _sourcedfilenames.add(newfile); // RK added on 20040111 13:43:56
107: try {
108: while ((str = lnr.readLine()) != null) {
109: st = new StringTokenizer(str);
110: if (!st.hasMoreTokens())
111: continue;
112: String action = st.nextToken();
113: if (action.charAt(0) == '#')
114: continue; // skip comments
115: // We first need to check whether we should go in for
116: // this action or not using hooks.
117: if (action.endsWith("-hook")) {
118: String hook = action.substring(0,
119: action.length() - 5);
120: String hookvalue = (String) htParams.get(hook);
121: if (hookvalue == null) {
122: System.err.println("No such hook:" + hook);
123: continue;
124: }
125: // the next field contains the value the hook is
126: // supposed to match
127: String regexp = st.nextToken();
128: if (!PerlWrapper.isMatching(regexp, hookvalue)) {
129: //System.out.println(regexp + " did not match "+hookvalue);
130: continue;
131: }
132: // keep going - advance action to next token
133: action = st.nextToken();
134: }
135:
136: // source another file
137: // syntax: source filename
138: // RK added on 20040111 13:17:16
139: if (action.equals("source")) {
140: String anewfile = st.nextToken();
141: if (anewfile != null)
142: parseFile(anewfile);
143: return;
144: }
145: // request to set variables
146: if (action.equals("set")) {
147: // this necessitates that the 3rd word not
148: // be broken by space
149: // RK added on 20040104 20:23:05
150: // changed delimit to see if it reads till endofline
151: String key = st.nextToken();
152: String def = st.nextToken();
153: htParams.put(key, def);
154: if (SQLForm.DEBUG)
155: System.out.println("put:" + key + ":[" + def
156: + "]");
157: //htParams.put (st.nextToken(), st.nextToken("\n"));
158: } else
159: // RK added on 20040126 18:16:03
160: // added append so that we can append new connectors
161: // to ini file from application
162: if (action.equals("append")) {
163: String key = st.nextToken();
164: String tmp = (String) htParams.remove(key);
165: String rest = getRestOfLine(st);
166: if (tmp == null)
167: htParams.put(key, rest);
168: else {
169: htParams.put(key, tmp + "," + rest);
170: if (SQLForm.DEBUG) {
171: System.out.println("Appended [" + rest
172: + "] to " + tmp);
173: }
174: }
175:
176: } else if (action.equals("unset")) {
177: htParams.remove(st.nextToken());
178: } else
179: // store user defined abbreviations in a HashMap
180: if (action.equals("abbr")) {
181: String s = st.nextToken();
182: String t = "";
183:
184: // this loop collects all further tokens
185: // but it loses the tokenizer and tries
186: // to make up by putting a space. We
187: // would lose a line break!
188: while (st.hasMoreTokens()) {
189: t += st.nextToken() + ' ';
190: }
191: htAbbrs.put(s, t);
192: } else if (action.equals("bookmark")) {
193: String name = st.nextToken();
194: String s = st.nextToken();
195: // we should grow up and start reading multiple lines
196: if (s.equals("sql")) {
197: StringBuffer t = new StringBuffer(64);
198: while (st.hasMoreTokens()) {
199: t.append(st.nextToken()).append(' ');
200: }
201: htBookmarks.put(name, t.toString());
202:
203: } else if (s.equals("file")) {
204: try {
205: String sb = IsqlUtil.getFileContents(st
206: .nextToken());
207: htBookmarks.put(name, sb);
208:
209: } catch (Exception exc) {
210: System.err.println("EXC:" + exc.toString());
211: }
212: }
213: // execute a system command abd get the
214: // result as bookmark text
215: if (s.equals("system")) {
216: StringBuffer t = new StringBuffer(64);
217: while (st.hasMoreTokens()) {
218: t.append(st.nextToken()).append(' ');
219: }
220: String command[] = ArrayUtil.split(
221: t.toString(), ' ');
222: String output = IsqlUtil.getExecOutput(command);
223: htBookmarks.put(name, output);
224: }
225: } // bookmark
226: else if (action.equals("link")) {
227: StringBuffer t = new StringBuffer(64);
228: while (st.hasMoreTokens()) {
229: t.append(st.nextToken()).append(' ');
230: }
231: //System.out.println("Link:"+t);
232: String ts = t.toString();
233: int pos1 = ts.indexOf(':');
234: int pos2 = ts.indexOf(':', pos1 + 1);
235: if (htLinks == null)
236: htLinks = new HashMap();
237: String tname = ts.substring(0, pos1) + '.';
238: if ("..".equals(tname))
239: tname = "";
240: // put key as table.col and rest as
241: // value
242: htLinks.put(tname + ts.substring(pos1 + 1, pos2),
243: ts.substring(pos2 + 1));
244: } // link
245: else if (action.equals("sort")) {
246: if (htSorts == null)
247: htSorts = new HashMap();
248: StringBuffer t = new StringBuffer(64);
249: while (st.hasMoreTokens()) {
250: t.append(st.nextToken()).append(' ');
251: }
252: String ts = t.toString();
253: int pos1 = ts.indexOf(':');
254: // put table name and then sort
255: // order
256: htSorts.put(ts.substring(0, pos1), ts
257: .substring(pos1 + 1));
258: } // sort
259: else if (action.equals("filter")) {
260: if (htFilters == null)
261: htFilters = new HashMap();
262: StringBuffer t = new StringBuffer(64);
263: while (st.hasMoreTokens()) {
264: t.append(st.nextToken()).append(' ');
265: }
266: String ts = t.toString();
267: int pos1 = ts.indexOf(':');
268: // put table name and then sort
269: // order
270: htFilters.put(ts.substring(0, pos1), ts
271: .substring(pos1 + 1));
272: } // sort
273:
274: else if (action.equals("bind")) {
275: if (SQLForm.DEBUG)
276: System.out.println(" === readTNS inside BIND");
277: String scope = st.nextToken();
278: String key = st.nextToken();
279: String actionname = st.nextToken();
280: if (htBindings == null)
281: htBindings = new Bindings();
282: try {
283: htBindings.addBinding(scope, key, actionname);
284: } catch (Exception exc) {
285: System.err.println(P
286: + " L194 Error in ini file:"
287: + lnr.getLineNumber() + ". "
288: + exc.toString());
289: }
290: } else if (action.equals("action")) {
291: //syntax
292: //pattern:<name>:<prompt>:<pattern>
293: String actiontype = st.nextToken(":").trim();
294: if (!"pattern".equals(actiontype)) {
295: System.err.println("Unknown action:"
296: + actiontype);
297: return;
298: }
299: String name = null;
300: String prompt = null;
301: String pattern = null;
302: String key = null;
303: try {
304: name = st.nextToken();
305: prompt = st.nextToken();
306: pattern = st.nextToken();
307: if (SQLForm.DEBUG) {
308: System.out.println(name + ":" + prompt
309: + ":" + pattern + ".");
310: }
311: if (st.hasMoreTokens())
312: key = st.nextToken();
313: } catch (NoSuchElementException exc) {
314: System.err.println(P
315: + " L213 EXC: Syntax of action wrong."
316: + exc.toString());
317: // exc.printStackTrace();
318: }
319: Map /*<String,String>*/act = new HashMap();
320: act.put("name", name);
321: act.put("prompt", prompt);
322: act.put("pattern", pattern);
323: if (key != null)
324: act.put("key", key);
325: if (liActions == null)
326: liActions = new ArrayList();
327: liActions.add(act);
328:
329: }
330: } // while
331: }
332: /*
333: catch (NoSuchElementException e) {
334: System.err.println ("Read TNS :"+e.toString());
335: }
336: catch (IOException e) {
337: System.err.println ("Read TNS:"+ e.toString());
338: }
339: */
340: catch (Exception e) {
341: System.err.println("Read TNS:" + e.toString());
342: e.printStackTrace();
343: }
344:
345: }
346:
347: /** returns list of parameters
348: */
349: public Map getParams() {
350: return htParams;
351: }
352:
353: /** returns list of abbreviations
354: */
355: public Map getAbbreviations() {
356: return htAbbrs;
357: }
358:
359: /** returns list of bookmarks
360: */
361: public Map getBookmarks() {
362: return htBookmarks;
363: }
364:
365: /** returns list of links
366: */
367: public Map getLinks() {
368: return htLinks;
369: }
370:
371: public Map getSorts() {
372: return htSorts;
373: }
374:
375: public Map getFilters() {
376: return htFilters;
377: }
378:
379: /** return key bindings read from ini file.
380: * Format for bindings is as follows:
381: * bind <scope> <key> <action>
382: * <pre>
383: * bind . control-z sort-action
384: * bind table control-s sort-action
385: * bind frame alt-s reverse-sort-action
386: * bind (table|frame) alt-d showdata-action
387: * </pre>
388: * This map contains lists of bindings named "input", "table" and
389: * "frame", containing their respective bindings.
390: */
391: public Bindings getBindings() {
392: return htBindings;
393: }
394:
395: /** returns a list of actions for application to create actions on
396: * menu.
397: * RK added on 20040104 23:08:09
398: */
399: public List /*<Map>*/getActions() {
400: return liActions;
401: }
402:
403: public List getSourcedFileNames() {
404: return _sourcedfilenames;
405: }
406:
407: public String getBaseFileName() {
408: return _inifile;
409: }
410:
411: /** returns the rest of the line, some commands need this.
412: * we could also try just changing the tokenizer.
413: */
414: public static String getRestOfLine(StringTokenizer st) {
415: StringBuffer line = new StringBuffer(80);
416: while (st.hasMoreTokens()) {
417: line.append(st.nextToken()).append(' ');
418: }
419: return line.toString().trim();
420: }
421:
422: public static final String P = "ReadTNS";
423: }
|