001: /*
002: * $Author$
003: * $Id$
004: * This is free software, as software should be; you can redistribute
005: * it and/or modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008:
009: * See LICENSE.txt for the full license covering this software/program/code.
010: */
011:
012: package isql.commands;
013:
014: import isql.Command;
015: import util.PerlWrapper;
016: import isql.*;
017: import util.*;
018: import javax.swing.JTextArea;
019: import java.awt.Font;
020: import java.util.*;
021:
022: /**
023: * The command object associated with the read command, used to read
024: * contents of a file into input or history area.
025: * @author rahul kumar <rahul_kumar@yahoo.com>
026: * @see XXX
027: */
028:
029: public class ReadCommand implements Command {
030:
031: /** ctor/constructor.
032: */
033: public ReadCommand() {
034: }
035:
036: public String[] getCommandList() {
037: return commands;
038: }
039:
040: public void execute(SQLForm form, String command, String SQLString) {
041: _form = form;
042: _form.setErrorArea('\n' + readInput(SQLString));
043: if (SQLString.indexOf("input ") == -1)
044: _form.tp.makeHistoryAreaVisible();
045:
046: }
047:
048: public String readInput(String command) {
049: String[] result = PerlWrapper.perlMatch(
050: "read\\s+(input|history)\\s+from\\s+([\\w\\.]+)",
051: command);
052: String tabname = null;
053: String filename = null;
054: if (result != null) {
055: if (result.length >= 2) {
056: tabname = result[0];
057: filename = result[1];
058: if (filename.indexOf('.') == -1)
059: filename += ".sql";
060: } else
061: return usage;
062: } else
063: return "Error! " + usage;
064: String content = null;
065: try {
066: // line numbers specified
067: if (command.indexOf('=') != -1) {
068: // this should be created once and shared.
069: NameValueParser nvp = new NameValueParser();
070: Map ht = (Map) nvp.getResults(command);
071: String s[] = ArrayUtil.split((String) ht.get("lines"),
072: ',');
073: int from = 0;
074: int to = 999;
075: if (s != null && s.length == 2) {
076: from = Integer.parseInt(s[0]);
077: to = Integer.parseInt(s[1]);
078: }
079: content = IsqlUtil.getFileContents(filename, from, to);
080: } // if =
081: else
082: content = IsqlUtil.getFileContents(filename);
083: } catch (Exception exc) {
084: System.err.println(P + " EXC 76:" + exc.toString());
085: return exc.toString();
086: }
087: if (tabname.equalsIgnoreCase("input")) {
088: _form.tp.appendInputArea('\n' + content);
089: } else if (tabname.equalsIgnoreCase("history")) {
090: _form.tp.putSQLInScrapArea('\n' + content);
091: }
092: return "Read " + content.length() + " bytes from " + filename;
093: }
094:
095: public String getUsage(String s) {
096: return usage;
097: }
098:
099: String commands[] = { "read" };
100: SQLForm _form;
101:
102: final String usage = "read <input|history> from <filename>";
103:
104: ////// START INSTANCE VARIABLES //////
105:
106: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
107: static final String P = "ReadCommand"; // used in exception strings
108:
109: } // end of class
|