/*
* JETERS – Java Extensible Text Replacement System
* Copyright (C) 2006–2008 Tobias Knerr
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
package net.sf.jeters.components;
import net.sf.jeters.componentInterface.InputComponent;
import net.sf.jeters.componentInterface.OutputComponent;
import net.sf.jeters.componentInterface.UIComponent;
import net.sf.jeters.componentInterface.dataStructs.NamedDataNotAvailableException;
import net.sf.jeters.componentInterface.dataStructs.NamedDataSet;
import net.sf.jeters.componentInterface.dataStructs.UIRequest;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_Boolean;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_Output;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_String;
import net.sf.jeters.componentInterface.editables.*;
import net.sf.jeters.util.AssistedTranslatable;
import java.io.*;
/**
* default implementation of a combined input and output component
* reading from and writing to text files in the local file system.
*
* @author Tobias Knerr
*/
public class FileIO extends AssistedTranslatable
implements InputComponent<EditableText>,
OutputComponent<EditableText> {
PlainText result;
public EditableText getInput(UIComponent uiForRequests) {
String text = new String();
//ask for input file name
String inputFileName = null;
while (inputFileName == null) {
NamedDataSet reply = uiForRequests.request(new UIRequest_String(
"path", str("titleInputFilePath"), str("helpInputFilePath"),
UIRequest_String.LayoutFlag.readPath));
if (reply == null) {
return null; //cancelled - #### !! return-point !! ####
}
try {
inputFileName = reply.get("path");
} catch (NamedDataNotAvailableException ndnae) {
uiForRequests.request( new UIRequest_Output(
str("errorUserInput")));
}
}
//read text from file
try {
File inputFile = new File(inputFileName);
//check whether the file exists and is readable
if (!inputFile.exists()) {
uiForRequests.request( new UIRequest_Output(
str("errorFileDoesNotExist", inputFileName)) );
} else if (!inputFile.canRead()) {
uiForRequests.request( new UIRequest_Output(
str("errorFileNotReadable", inputFileName)) );
} else {
//Create a buffered reader
BufferedReader reader =
new BufferedReader( new FileReader(inputFile) );
boolean readingComplete = false;
while (!readingComplete) {
String line = reader.readLine();
if (line == null) {
readingComplete = true;
} else {
//add the line to the text
if (text.equals("")) {
text = line;
} else {
text += "\n" + line;
}
}
}
reader.close();
result = new PlainText(text, inputFile.getName());
}
} catch (IOException e) {
uiForRequests.request( new UIRequest_Output(
str("errorFileInput", inputFileName, e)) );
}
/* this return statement won't be reached if the user
* cancels the process, in which case null has already been returned
*/
return result;
}
public void output(EditableText text, UIComponent uiForRequests) {
boolean savingComplete = false;
boolean tryAgain = true;
/*
* repeat until either saving succeeded
* or the user decides not to try again
*/
while (!savingComplete && tryAgain) {
String outputFileName = null;
while (outputFileName == null && tryAgain) {
//get the path from user interface
NamedDataSet reply = uiForRequests.request(new UIRequest_String(
"path", str("titleOutputFilePath"),
str("helpOutputFilePath"),
UIRequest_String.LayoutFlag.writePath));
if (reply == null) { tryAgain = false; } //cancelled
else{
try {
outputFileName = reply.get("path");
} catch (NamedDataNotAvailableException e) {
uiForRequests.request(
new UIRequest_Output(str("errorUserInput")) );
}
}
}
if (tryAgain) {
tryAgain = false;
//create the File object
File outputFile = new File(outputFileName);
try {
/*
* check whether the file exists
* (if yes: ask about overwriting)
*/
if (outputFile.exists()) {
UIRequest outputRequestArray[] = {
new UIRequest_Boolean("overwrite",
str("requestOverwrite"),
str("helpOverwrite")) };
boolean overwrite =
uiForRequests.request(outputRequestArray)
.<Boolean>get("overwrite");
if (!overwrite){
outputFile = null;
tryAgain = true;
}
}
if (outputFile != null) {
//check whether the file is writable
if (!outputFile.canWrite()) {
uiForRequests.request(
new UIRequest_Output(
str("errorFileNotWritable")) );
} else {
outputFile.createNewFile();
FileWriter writer = new FileWriter(outputFile);
writer.write(text.getText());
writer.close();
savingComplete = true;
}
}
} catch (IOException e) {
uiForRequests.request(
new UIRequest_Output(
str("errorFileOutput", outputFile, e)) );
} catch (NamedDataNotAvailableException e) {
uiForRequests.request(
new UIRequest_Output(
str("invaildUserInput", e)) );
}
if (!tryAgain && !savingComplete) {
UIRequest tryAgainRequestArray[] = {
new UIRequest_Boolean(
"tryAgain", str("requestTryAgain"),
str("helpTryAgain")) };
try{
tryAgain =
uiForRequests.request(tryAgainRequestArray)
.<Boolean>get("tryAgain");
} catch ( NamedDataNotAvailableException ndnae ){
tryAgain = false; /* no further attempts to write */
}
}
}
}
}
}