/*
* $Author$
* $Id$
* This is free software, as software should be; you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* See LICENSE.txt for the full license covering this software/program/code.
*/
package isql.commands;
import isql.Command;
import util.PerlWrapper;
import isql.*;
import util.*;
import javax.swing.JTextArea;
import java.awt.Font;
import java.util.*;
/**
* The command object associated with the read command, used to read
* contents of a file into input or history area.
* @author rahul kumar <rahul_kumar@yahoo.com>
* @see XXX
*/
public class ReadCommand implements Command {
/** ctor/constructor.
*/
public ReadCommand (){
}
public String[] getCommandList(){
return commands;
}
public void execute (SQLForm form, String command, String SQLString){
_form = form;
_form.setErrorArea ('\n'+readInput(SQLString));
if (SQLString.indexOf("input ")==-1)
_form.tp.makeHistoryAreaVisible();
}
public String readInput (String command){
String[] result = PerlWrapper.perlMatch("read\\s+(input|history)\\s+from\\s+([\\w\\.]+)", command);
String tabname=null;
String filename=null;
if (result != null){
if (result.length>=2){
tabname=result[0];
filename=result[1];
if (filename.indexOf('.')==-1)
filename+=".sql";
} else return usage;
} else return "Error! "+ usage;
String content = null;
try {
// line numbers specified
if (command.indexOf('=')!=-1){
// this should be created once and shared.
NameValueParser nvp = new NameValueParser();
Map ht = (Map)nvp.getResults(command);
String s[] = ArrayUtil.split( (String)ht.get("lines"),',');
int from = 0;
int to = 999;
if (s!=null && s.length==2){
from = Integer.parseInt(s[0]);
to = Integer.parseInt(s[1]);
}
content = IsqlUtil.getFileContents(filename,from,to);
} // if =
else
content = IsqlUtil.getFileContents(filename);
}
catch (Exception exc) { System.err.println( P+" EXC 76:"+ exc.toString());
return exc.toString();
}
if (tabname.equalsIgnoreCase("input")){
_form.tp.appendInputArea('\n'+content);
} else
if (tabname.equalsIgnoreCase("history")){
_form.tp.putSQLInScrapArea('\n'+content);
}
return "Read " + content.length() + " bytes from "+ filename;
}
public String getUsage(String s){
return usage;
}
String commands[] = {"read"};
SQLForm _form;
final String usage = "read <input|history> from <filename>";
////// START INSTANCE VARIABLES //////
////// START CONSTANTS AND CLASS LEVEL VARIABLES //////
static final String P = "ReadCommand"; // used in exception strings
} // end of class
|