import java.util.Scanner; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.Files; /* An instance of this class is for the purpose of editing a text file ** via a command-line interface (i.e., similar to UNIX's 'edit' utility). ** Notably, it supports undo and redo meta-commands. ** Also included is a main() method, which allows this class to be used ** as an application program. ** ** Authors: Gemini (AI), R. McCloskey */ public class CLI_Editor { // instance variables // ------------------ private Document myDoc; // the "resident" document being edited private Path path; // name/path of file that was source and/or // destination of the document being edited // Editing commands are carried out via this intermediary "command history // manager", which supplies the means by which the 'undo' and 'redo' // commands work. private CommandHistoryManager chm; // constructors // ------------ /* Establishes the document that is to be edited and the path to ** the file in which it would be written (using the 'save' command). ** (That path can be changed using the 'saveAs' command.) */ public CLI_Editor(String fileName) { initialize(fileName); } /* Establishes the document to be edited, which is initially empty. ** No path/name for the file in which the document would be written ** is established. */ public CLI_Editor() { initialize(null); } /* Supports the constructors by initializing the instance variables ** in accord with whether or not the document to be edited already ** exists in a file. */ private void initialize(String fileName) { if (fileName == null) { // a new document is to be edited path = null; myDoc = new DocumentViaList(); } else { // the document in the specified file is to be edited path = Paths.get(fileName); myDoc = new DocumentViaList(path); } chm = new CommandHistoryManager(myDoc); } // the method of interest // ---------------------- /* In response to commands entered by the user (i.e., read from ** standard inpout), the resident document is edited. */ public void edit() { Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the CLI_Editor"); System.out.println("-------------------------"); boolean keepGoing = true; while (keepGoing) { System.out.print("\n> "); String input = scanner.nextLine().trim(); if (input.equalsIgnoreCase("exit")) { keepGoing = false; } else if (input.equalsIgnoreCase("help")) { printHelp(); } else if (input.equalsIgnoreCase("undo")) { chm.undo(); } else if (input.equalsIgnoreCase("redo")) { chm.redo(); } else if (input.equalsIgnoreCase("display")) { myDoc.display(); } else if (input.equalsIgnoreCase("save")) { performSave(); } else { // handle commands having at least one operand/argument try { String[] tokens = input.split(" ", 3); String action = tokens[0].toLowerCase(); if (action.equals("saveas")) { performSaveAs(tokens[1]); } else { int lineNum = Integer.parseInt(tokens[1]); if (action.equals("insert")) { // Strip any wrapping quotes from line to insert String text = tokens[2].replaceAll("^\"|\"$", ""); chm.executeCommand(new InsertCommand(lineNum, text)); } else if (action.equals("delete")) { chm.executeCommand(new DeleteCommand(lineNum)); } else if (action.equals("replace")) { // Strip any wrapping quotes from replacement line String text = tokens[2].replaceAll("^\"|\"$", ""); chm.executeCommand(new ReplaceCommand(lineNum, text)); } else if (action.equals("display")) { int secondLineNum = Integer.parseInt(tokens[2]); myDoc.display(lineNum, secondLineNum); } else { System.out.println("Unknown command action."); } } } catch (Exception e) { System.out.println("Error parsing input."); } } } scanner.close(); System.out.println("Goodbye!"); } // utility methods // --------------- /* Saves the resident document in the file identified by the path ** most recently established. */ protected void performSave() { if (path == null) { System.out.println("Command ignored: No known destination file."); System.out.println("Use saveAs to specify a destination."); } else { try { myDoc.writeToFile(path); } catch (Exception e) { e.printStackTrace(System.out); } } } /* Assuming that the given file name does not identify an existing file, ** the resident document is saved in a new file of that name, and that ** name becomes the "established path" (for future calls to performSave()). ** If a file with the given name already exists, a warning is displayed ** and no saving of the document occurs. */ protected void performSaveAs(String fileName) { try { Path newPath = Paths.get(fileName); if (Files.exists(newPath)) { System.out.println("Command ignored: File of that name already exists."); } else { // Change path for future saves path = newPath; myDoc.writeToFile(path); } } catch (Exception e) { e.printStackTrace(System.out); } } /* Displays the commands available to the user. */ protected void printHelp() { System.out.println("Command templates:"); System.out.println(" insert "); System.out.println(" delete "); System.out.println(" replace "); System.out.println(" display "); System.out.println(" display"); System.out.println(" saveAs "); System.out.println(" save"); System.out.println(" undo"); System.out.println(" redo"); System.out.println(" exit"); } /* Allows a user to carry out a session in which a document is ** edited and saved. If an argument is provided, it is taken to ** be the path/name of a file containing the document to be edited. ** Otherwise editing begins with an empty document. */ public static void main(String[] args) { CLI_Editor editor; if (args.length == 0) { // Edit starting with a blank document editor = new CLI_Editor(); } else { // Edit the document residing in the file named // by the command-line argument. editor = new CLI_Editor(args[0]); } editor.edit(); } }