Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

New parallel branch Кривчанский 395 #549

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;


public interface Commands<State> {

String getCommandName();

int getArgumentQuantity();

void implement(String args, State state) throws SomethingIsWrongException;
}
45 changes: 45 additions & 0 deletions src/ru/fizteh/fivt/students/NikolaiKrivchanskii/Shell/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parser {


public static ArrayList<String> parseCommandArgs(String params) {
ArrayList<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
Matcher regexMatcher = regex.matcher(params);
while (regexMatcher.find()) {
String param = regexMatcher.group().replaceAll("\"?([~\"]*)\"?", "$1");
matchList.add(param);
}
return matchList;
}

public static String getName(String command) {
int spiltIndex = command.indexOf(' ');
if (spiltIndex == -1) {
return command;
} else {
return command.substring(0, spiltIndex);
}
}

public static String[] parseFullCommand(String commands) {
String[] commandArray = commands.split(";");
for (int i = 0; i < commandArray.length; ++i) {
commandArray[i] = commandArray[i].trim();
}
return commandArray;
}
public static String getParameters(String command) {
int spiltIndex = command.indexOf(' ');
if (spiltIndex == -1) {
return "";
} else {
return command.substring(spiltIndex + 1);
}
}
}
90 changes: 90 additions & 0 deletions src/ru/fizteh/fivt/students/NikolaiKrivchanskii/Shell/Shell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;




public class Shell<State> {
private final Map<String, Commands> availableCommands;
private static final String GREETING = "$ ";
State state;


public Shell(Set<Commands<?>> commands) {
Map<String, Commands> tempCommands = new HashMap<String, Commands>();
for (Commands<?> temp : commands) {
tempCommands.put(temp.getCommandName(), temp);
}
availableCommands = Collections.unmodifiableMap(tempCommands);
}

public void setShellState(State state) {
this.state = state;
}

private void runCommand(String[] data, State state) throws SomethingIsWrongException {
for (String command : data) {
if (data[0].length() == 0) {
throw new SomethingIsWrongException("Empty string.");
}
String name = Parser.getName(command);
Commands<State> usedOne = availableCommands.get(name);
String args = Parser.getParameters(command);
if (usedOne == null) {
throw new SomethingIsWrongException("Unknown command.");
}
usedOne.implement(args, state);
}
}

public void consoleWay(State state) {
Scanner forInput = new Scanner(System.in);
while (!Thread.currentThread().isInterrupted()) {
System.out.print(GREETING);
try {
String temp = forInput.nextLine();
String[] commands = Parser.parseFullCommand(temp);
runCommand(commands, state);
} catch (SomethingIsWrongException exc) {
if (exc.getMessage().equals("EXIT")) {
forInput.close();
System.exit(0);
} else {
System.err.println(exc.getMessage());
}
} catch (IllegalArgumentException | IllegalStateException e) {
System.err.println(e.getMessage());
}
}
forInput.close();
}


public void run(String[] args, Shell<State> shell) {
if (args.length != 0) {
String arg = UtilMethods.uniteItems(Arrays.asList(args), " ");
String[] commands = Parser.parseFullCommand(arg);
try {
runCommand(commands, state);
} catch (SomethingIsWrongException exc) {
if (exc.getMessage().equals("EXIT")) {
System.exit(0);
} else {
System.err.println(exc.getMessage());
System.exit(-1);
}
}
} else {
consoleWay(state);
}
System.exit(0);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

public class ShellState {
private String currentDirectory;
public ShellState(String currentDirectory) {
this.currentDirectory = currentDirectory;
}
public String getCurDir() {
return currentDirectory;
}
void changeCurDir(String newCurDir) {
currentDirectory = newCurDir;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

public abstract class SomeCommand<State> implements Commands<State> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;


public final class SomethingIsWrongException extends Exception {
public SomethingIsWrongException() {
super();
}

public SomethingIsWrongException(String message) {
super(message);
}

public SomethingIsWrongException(String message, Throwable cause) {
super(message, cause);
}

public SomethingIsWrongException(Throwable cause) {
super(cause);
}
}
100 changes: 100 additions & 0 deletions src/ru/fizteh/fivt/students/NikolaiKrivchanskii/Shell/UtilMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

public class UtilMethods {
public static final String ENCODING = "UTF-8";
public static String uniteItems(Collection<?> items, String separator) {
boolean isFirstIteration = true;
StringBuilder joinBuilder = new StringBuilder();
for (Object item: items) {
if (isFirstIteration) {
isFirstIteration = false;
} else {
joinBuilder.append(separator);
}
joinBuilder.append(item.toString());
}
return joinBuilder.toString();
}

public static void closeCalm(Closeable something) {
try {
if (something != null) {
something.close();
}
} catch (IOException e) {
System.err.println("Error aquired while trying to close " + e.getMessage());
}
}

public static void copyFile(File source, File dirDestination) throws SomethingIsWrongException {
File copy = new File(dirDestination, source.getName());
FileInputStream ofOriginal = null;
FileOutputStream ofCopy = null;
try {
copy.createNewFile();
ofOriginal = new FileInputStream(source);
ofCopy = new FileOutputStream(copy);
byte[] buf = new byte[4096]; //size of reading = 4kB
int read = ofOriginal.read(buf);
while (read > 0) {
ofCopy.write(buf, 0, read);
read = ofOriginal.read(buf);
}
} catch (FileNotFoundException e) {
throw new SomethingIsWrongException("This file or directory doesn't exist yet. " + e.getMessage());
} catch (IOException e) {
throw new SomethingIsWrongException("Error while writing/reading file. " + e.getMessage());
} finally {
closeCalm(ofOriginal);
closeCalm(ofCopy);
}
}

public static File getAbsoluteName(String fileName, ShellState state) {
File file = new File(fileName);

if (!file.isAbsolute()) {
file = new File(state.getCurDir(), fileName);
}
return file;
}
public static byte[] getBytes(String string, String encoding) throws SomethingIsWrongException {
byte[] bytes = null;
try {
bytes = string.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
throw new SomethingIsWrongException("Unable to convert string to bytes of this type: " + e.getMessage());
}
return bytes;
}

public static byte[] bytesToArray(ByteArrayOutputStream bytes) {
byte[] result = new byte[bytes.size()];
result = bytes.toByteArray();
return result;
}

public static boolean doesExist(String path) {
File file = new File(path);
return file.exists();
}

public static int countBytes(String string, String encoding) {
try {
byte[] bytes = string.getBytes(encoding);
return bytes.length;
} catch (Exception e) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.filemap;

import ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell.SomeCommand;
import ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell.SomethingIsWrongException;

public class CommitCommand<State extends FileMapShellStateInterface> extends SomeCommand<State> {

public String getCommandName() {
return "commit";
}

public int getArgumentQuantity() {
return 0;
}

public void implement(String args, State state)
throws SomethingIsWrongException {
if (state.getTable() == null) {
throw new SomethingIsWrongException("no table");
}
System.out.println(state.commit());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.fizteh.fivt.students.NikolaiKrivchanskii.filemap;

import ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell.SomeCommand;
import ru.fizteh.fivt.students.NikolaiKrivchanskii.Shell.SomethingIsWrongException;

public class ExitCommand<State extends FileMapShellStateInterface> extends SomeCommand<State> {

public String getCommandName() {
return "exit";
}

public int getArgumentQuantity() {
return 0;
}

public void implement(String args, State state)
throws SomethingIsWrongException {
/*MyTable temp = (MyTable) state.getTable();*/
if (state.getTable() != null /*&& !temp.getAutoCommit()*/) {
state.rollback();
}/* else if (temp.getAutoCommit() && state.getTable() != null) {
state.commit();
}*/
throw new SomethingIsWrongException("EXIT");
}

}
Loading