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 javax.swing.table.TableModel;
020: import java.awt.Font;
021: import java.util.*;
022: import java.io.*;
023:
024: /**
025: * The command object associated with the cache command, used to
026: * cache table information.
027: * @author rahul kumar <rahul_kumar@yahoo.com>
028: * @see XXX
029: */
030:
031: public class SaveCommand implements Command {
032:
033: /** ctor/constructor.
034: */
035: public SaveCommand() {
036: }
037:
038: public String[] getCommandList() {
039: return commands;
040: }
041:
042: public void execute(SQLForm form, String command, String SQLString) {
043: _form = form;
044: SQLTabbedPane tp = _form.tp;
045: //SQLJDBC myjdbc = _form.myjdbc;
046:
047: _form.setErrorArea('\n' + saveOutput(SQLString, tp));
048:
049: }
050:
051: public String saveOutput(String command, SQLTabbedPane tp) {
052: String[] result = PerlWrapper
053: .perlMatch(
054: "(save|append)\\s+(input|output|error|history|table)\\s+to\\s+([\\w\\.]+)",
055: command);
056: String tabname = null;
057: String filename = null;
058: String operation = null;
059: if (result != null) {
060: if (result.length == 3) {
061: tabname = result[1];
062: filename = result[2];
063: operation = result[0];
064: if (filename.indexOf('.') == -1)
065: filename += ".sql";
066: } else
067: return usage;
068: } else
069: return "Error! " + usage;
070:
071: String output = " Nothing to save!";
072: try {
073: boolean append = false;
074: if (operation.equalsIgnoreCase("append"))
075: append = true;
076: if ("on".equalsIgnoreCase((String) _form
077: .getAttribute("alwaysappend")))
078: append = true;
079: BufferedWriter bw = new BufferedWriter(new FileWriter(
080: filename, append));
081: if (tabname.equalsIgnoreCase("input")) {
082: output = tp.getInputText();
083: } else if (tabname.equalsIgnoreCase("output")) {
084: output = tp.getOutputText();
085: } else if (tabname.equalsIgnoreCase("history")) {
086: output = tp.getHistoryText();
087: } else if (tabname.equalsIgnoreCase("error")) {
088: //output=tp.getErrorText();
089: output = _form.jtError.getText();
090: } else if (tabname.equalsIgnoreCase("table")) {
091: TableModel tm = tp.getTableText();
092: String colsep = (String) _form.getAttribute("colsep");
093: if (colsep == null)
094: colsep = " |";
095: int colcount = tm.getColumnCount();
096: int rowcount = tm.getRowCount();
097: StringBuffer sb = new StringBuffer(colcount * rowcount
098: * 5);
099: for (int i = 0; i < colcount; i++) {
100: sb.append(tm.getColumnName(i)).append(colsep);
101: }
102: sb.append('\n');
103: for (int i = 0; i < rowcount; i++) {
104: for (int j = 0; j < colcount; j++)
105: sb.append(tm.getValueAt(i, j)).append(colsep);
106: sb.append('\n');
107: }
108: output = sb.toString();
109:
110: }
111: bw.write(output, 0, output.length());
112: bw.close();
113: } catch (Exception exc) {
114: System.err.println("EXC694:" + exc.toString());
115: return exc.toString();
116: }
117: return "Written " + output.length() + " bytes to file:"
118: + filename;
119: }
120:
121: final String usage = "<save|append> <input|output|history|table> to <filename>";
122:
123: public String getUsage(String s) {
124: return usage;
125: }
126:
127: String commands[] = { "save", "append" };
128: SQLForm _form;
129:
130: ////// START INSTANCE VARIABLES //////
131:
132: ////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
133: static final String P = "SaveCommand"; // used in exception strings
134:
135: } // end of class
|