forked from dkomanov/fizteh-java-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'shell' of https://github.com/Oktosha/fizteh-java-2014 i…
…nto Oktosha-shell Conflicts: build.xml src/ru/fizteh/fivt/students/Gudkov Konstantin 394/shell/ShellMain.java
- Loading branch information
Showing
19 changed files
with
573 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
*.class | ||
|
||
# Mobile Tools for Java (J2ME) | ||
|
12 changes: 0 additions & 12 deletions
12
src/ru/fizteh/fivt/students/Gudkov Konstantin 394/shell/ShellMain.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Command; | ||
|
||
/** | ||
* A simple class which holds name of command and it's arguments. | ||
*/ | ||
public class Command { | ||
public Command(String commandString) { | ||
commandString = commandString.trim(); | ||
String[] split = commandString.split("\\s+"); | ||
name = split[0]; | ||
args = new String[split.length - 1]; | ||
System.arraycopy(split, 1, args, 0, split.length - 1); | ||
} | ||
public String name; | ||
public String[] args; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandArgumentSyntaxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; | ||
|
||
/** | ||
* The exception which is thrown when there is a syntax error in arguments | ||
* given to command of ConsoleUtility. | ||
*/ | ||
public class CommandArgumentSyntaxException extends ConsoleUtilitySyntaxException { | ||
public CommandArgumentSyntaxException(String s) { | ||
super(s); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/CommandIsNotSupportedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; | ||
|
||
/** | ||
* Created by DKolodzey on 30.09.14. | ||
*/ | ||
public class CommandIsNotSupportedException extends ConsoleUtilitySyntaxException { | ||
public CommandIsNotSupportedException(String s) { | ||
super(s); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; | ||
|
||
import ru.fizteh.fivt.students.Oktosha.Command.Command; | ||
|
||
/** | ||
* ConsoleUtility is an interface which should be implemented by Shell, | ||
* DbMain or whatever else ConsoleUtility run by Executor. | ||
*/ | ||
public interface ConsoleUtility { | ||
void run(Command cmd) throws CommandIsNotSupportedException, | ||
CommandArgumentSyntaxException, | ||
ConsoleUtilityRuntimeException; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; | ||
|
||
/** | ||
* Parent class for all exceptions thrown by ConsoleUtility | ||
*/ | ||
public class ConsoleUtilityException extends Exception { | ||
public ConsoleUtilityException(String s) { | ||
super(s); | ||
} | ||
public ConsoleUtilityException(String s, Exception e) { | ||
super(s, e); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilityRuntimeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; | ||
|
||
/** | ||
* This exception is thrown when an error during running command occurs. | ||
*/ | ||
public class ConsoleUtilityRuntimeException extends ConsoleUtilityException { | ||
public ConsoleUtilityRuntimeException(String s) { | ||
super(s); | ||
} | ||
public ConsoleUtilityRuntimeException(String s, Exception e) { | ||
super(s, e); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/ru/fizteh/fivt/students/Oktosha/ConsoleUtility/ConsoleUtilitySyntaxException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ru.fizteh.fivt.students.Oktosha.ConsoleUtility; | ||
|
||
/** | ||
* Child class of ConsoleUtilityException. | ||
* It is thrown when command has invalid syntax. | ||
*/ | ||
|
||
public class ConsoleUtilitySyntaxException extends ConsoleUtilityException { | ||
ConsoleUtilitySyntaxException(String s) { | ||
super(s); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/ru/fizteh/fivt/students/Oktosha/Executor/Executor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Executor; | ||
|
||
import ru.fizteh.fivt.students.Oktosha.Command.Command; | ||
|
||
/** | ||
* A class which executes an instance of ConsoleUtility | ||
*/ | ||
public abstract class Executor { | ||
|
||
static final int SYNTAX_ERROR = 1; | ||
static final int COMMAND_RUNTIME_ERROR = 2; | ||
|
||
protected static Command[] parse(String s) { | ||
String[] commandStrings = s.split(";"); | ||
Command[] res = new Command[commandStrings.length]; | ||
for (int i = 0; i < res.length; ++i) { | ||
res[i] = new Command(commandStrings[i]); | ||
} | ||
return res; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Executor; | ||
|
||
/** | ||
* Parent class for all exceptions thrown by executor. | ||
*/ | ||
public class ExecutorException extends Exception { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/ru/fizteh/fivt/students/Oktosha/Executor/ExecutorParseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Executor; | ||
|
||
/** | ||
* This exception is thrown when parsed command is empty | ||
*/ | ||
public class ExecutorParseException extends ExecutorException { | ||
} |
36 changes: 36 additions & 0 deletions
36
src/ru/fizteh/fivt/students/Oktosha/Executor/InteractiveExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Executor; | ||
|
||
import ru.fizteh.fivt.students.Oktosha.Command.Command; | ||
import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtility; | ||
import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilityException; | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* InteractiveExecutor is a child of Executor | ||
* which runs ConsoleUtility in interactive mode | ||
*/ | ||
public class InteractiveExecutor extends Executor { | ||
public static void execute(ConsoleUtility utility) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.print("$ "); | ||
System.out.flush(); | ||
while (sc.hasNextLine()) { | ||
String commandsString = sc.nextLine(); | ||
try { | ||
Command[] commands = parse(commandsString); | ||
for (Command cmd : commands) { | ||
utility.run(cmd); | ||
} | ||
} catch (ConsoleUtilityException e) { | ||
System.err.println(e.getMessage()); | ||
System.err.flush(); | ||
System.out.flush(); | ||
} finally { | ||
System.out.print("$ "); | ||
System.out.flush(); | ||
System.err.flush(); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/ru/fizteh/fivt/students/Oktosha/Executor/PackageExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Executor; | ||
|
||
import ru.fizteh.fivt.students.Oktosha.Command.Command; | ||
import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtility; | ||
import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilityRuntimeException; | ||
import ru.fizteh.fivt.students.Oktosha.ConsoleUtility.ConsoleUtilitySyntaxException; | ||
|
||
/** | ||
* Class which runs console utility in package mode | ||
*/ | ||
public class PackageExecutor extends Executor { | ||
public static void execute(ConsoleUtility utility, String[] args) { | ||
StringBuilder builder = new StringBuilder(); | ||
for (String str : args) { | ||
builder.append(" "); | ||
builder.append(str); | ||
} | ||
String commandsString = builder.toString(); | ||
try { | ||
Command[] commands = parse(commandsString); | ||
for (Command cmd : commands) { | ||
utility.run(cmd); | ||
} | ||
} catch (ConsoleUtilitySyntaxException e) { | ||
System.err.println(e.getMessage()); | ||
System.exit(Executor.SYNTAX_ERROR); | ||
} catch (ConsoleUtilityRuntimeException e) { | ||
System.err.println(e.getMessage()); | ||
System.exit(Executor.COMMAND_RUNTIME_ERROR); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/ru/fizteh/fivt/students/Oktosha/Shell/CpFileVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Shell; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.*; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
|
||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
|
||
/** | ||
* Class which copies a directory recursively | ||
*/ | ||
|
||
public class CpFileVisitor extends SimpleFileVisitor<Path> { | ||
private Path source; | ||
private Path target; | ||
CpFileVisitor(Path source, Path target) { | ||
this.source = source; | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { | ||
Files.createDirectory(target.resolve(source.relativize(dir))); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
Files.copy(file, target.resolve(source.relativize(file)), REPLACE_EXISTING); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/ru/fizteh/fivt/students/Oktosha/Shell/MvFileVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Shell; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.*; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
|
||
/** | ||
* Class which moves a directory recursively | ||
*/ | ||
|
||
public class MvFileVisitor extends SimpleFileVisitor<Path> { | ||
private Path source; | ||
private Path target; | ||
MvFileVisitor(Path source, Path target) { | ||
this.source = source; | ||
this.target = target; | ||
} | ||
|
||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { | ||
Files.createDirectory(target.resolve(source.relativize(dir))); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
Files.move(file, target.resolve(source.relativize(file))); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | ||
if (exc != null) { | ||
throw exc; | ||
} | ||
Files.delete(dir); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
src/ru/fizteh/fivt/students/Oktosha/Shell/RmFileVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ru.fizteh.fivt.students.Oktosha.Shell; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.*; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
|
||
/** | ||
* Class which deletes a directory recursively | ||
*/ | ||
|
||
public class RmFileVisitor extends SimpleFileVisitor<Path> { | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
Files.delete(file); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | ||
if (exc != null) { | ||
throw exc; | ||
} | ||
Files.delete(dir); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
} |
Oops, something went wrong.