diff --git a/jcmd b/jcmd deleted file mode 160000 index 52e0edc..0000000 --- a/jcmd +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 52e0edc1b95565edc335a1d1c120cf7b8f4bd4c9 diff --git a/jcmd/.DS_Store b/jcmd/.DS_Store new file mode 100644 index 0000000..f8c3a1f Binary files /dev/null and b/jcmd/.DS_Store differ diff --git a/jcmd/.idea/.gitignore b/jcmd/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/jcmd/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/jcmd/.idea/compiler.xml b/jcmd/.idea/compiler.xml new file mode 100644 index 0000000..e1081f2 --- /dev/null +++ b/jcmd/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/jcmd/.idea/encodings.xml b/jcmd/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/jcmd/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/jcmd/.idea/jarRepositories.xml b/jcmd/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/jcmd/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/jcmd/.idea/misc.xml b/jcmd/.idea/misc.xml new file mode 100644 index 0000000..dab31a5 --- /dev/null +++ b/jcmd/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/jcmd/.idea/vcs.xml b/jcmd/.idea/vcs.xml new file mode 100644 index 0000000..288b36b --- /dev/null +++ b/jcmd/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/jcmd/.mvn/jvm.config b/jcmd/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 diff --git a/jcmd/.mvn/maven.config b/jcmd/.mvn/maven.config new file mode 100644 index 0000000..e69de29 diff --git a/jcmd/pom.xml b/jcmd/pom.xml new file mode 100644 index 0000000..67dee84 --- /dev/null +++ b/jcmd/pom.xml @@ -0,0 +1,92 @@ + + + + 4.0.0 + + de.vimo + jcmd + 1.0-SNAPSHOT + + jcmd + A simple jcmd. + + http://www.example.com + + + UTF-8 + 8 + 8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.13.1 + test + + + + + + + + maven-clean-plugin + 3.4.0 + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 11 + + + + + + + + + maven-project-info-reports-plugin + + + + diff --git a/jcmd/src/.DS_Store b/jcmd/src/.DS_Store new file mode 100644 index 0000000..a0c3472 Binary files /dev/null and b/jcmd/src/.DS_Store differ diff --git a/jcmd/src/main/java/de/vimo/InputTextTillCommand.java b/jcmd/src/main/java/de/vimo/InputTextTillCommand.java new file mode 100644 index 0000000..0fcbf5e --- /dev/null +++ b/jcmd/src/main/java/de/vimo/InputTextTillCommand.java @@ -0,0 +1,12 @@ +package de.vimo; + +public interface InputTextTillCommand { + + void splitTextToCommandOptionArgument(String text); + + void setCommand(String command); + + void setArgument(String argument); + + void setOption(String option); +} diff --git a/jcmd/src/main/java/de/vimo/Jcmd.java b/jcmd/src/main/java/de/vimo/Jcmd.java new file mode 100644 index 0000000..cfa1df7 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/Jcmd.java @@ -0,0 +1,48 @@ +package de.vimo; + +import java.util.Scanner; + +public class Jcmd implements InputTextTillCommand +{ + public static void main( String[] args ) + { + Scanner inputUser = new Scanner(System.in); + System.out.println("Typ your terminal command"); + + try { + while (inputUser.hasNextLine()) { + String line = inputUser.nextLine(); + if (line.equals("q")) { + break; + } + System.out.println(line); + } + } catch (IllegalStateException e) { + System.out.println(e); + } finally { + inputUser.close(); + } + + + } + + @Override + public void splitTextToCommandOptionArgument(String text) { + + } + + @Override + public void setCommand(String command) { + + } + + @Override + public void setArgument(String argument) { + + } + + @Override + public void setOption(String option) { + + } +} diff --git a/jcmd/src/main/java/de/vimo/Main.java b/jcmd/src/main/java/de/vimo/Main.java new file mode 100644 index 0000000..62f41fe --- /dev/null +++ b/jcmd/src/main/java/de/vimo/Main.java @@ -0,0 +1,22 @@ +package de.vimo; + +import de.vimo.core.Line; +import de.vimo.core.Parser; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + Parser parser = null; + + while (true) { + String raw = scanner.nextLine(); + Line line = parser.parse(raw); + + if (line.getCommand() != null) { + // line.getCommand().exec(line.getFlags(), line.getArgs()); + } + } + } +} diff --git a/jcmd/src/main/java/de/vimo/command/CdCommand.java b/jcmd/src/main/java/de/vimo/command/CdCommand.java new file mode 100644 index 0000000..6fb12f1 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/command/CdCommand.java @@ -0,0 +1,26 @@ +package de.vimo.command; + +import de.vimo.core.Argument; +import de.vimo.core.Command; +import de.vimo.core.Flag; + +import java.util.List; + +public class CdCommand implements Command { + + @Override + public int exec(List flags, List args) { + return 0; + } + + @Override + public String getName() { + return null; + } + + @Override + public List getFlags() { + return null; + } +} + diff --git a/jcmd/src/main/java/de/vimo/command/HelpCommand.java b/jcmd/src/main/java/de/vimo/command/HelpCommand.java new file mode 100644 index 0000000..7eefe80 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/command/HelpCommand.java @@ -0,0 +1,25 @@ +package de.vimo.command; + +import de.vimo.core.Argument; +import de.vimo.core.Command; +import de.vimo.core.Flag; + +import java.util.List; + +public class HelpCommand implements Command { + + @Override + public int exec(List flags, List args) { + return 0; + } + + @Override + public String getName() { + return null; + } + + @Override + public List getFlags() { + return null; + } +} \ No newline at end of file diff --git a/jcmd/src/main/java/de/vimo/command/LsCommand.java b/jcmd/src/main/java/de/vimo/command/LsCommand.java new file mode 100644 index 0000000..bb04f87 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/command/LsCommand.java @@ -0,0 +1,25 @@ +package de.vimo.command; + +import de.vimo.core.Argument; +import de.vimo.core.Command; +import de.vimo.core.Flag; + +import java.util.List; + +public class LsCommand implements Command { + + @Override + public int exec(List flags, List args) { + return 0; + } + + @Override + public String getName() { + return null; + } + + @Override + public List getFlags() { + return null; + } +} \ No newline at end of file diff --git a/jcmd/src/main/java/de/vimo/command/MkdirCommand.java b/jcmd/src/main/java/de/vimo/command/MkdirCommand.java new file mode 100644 index 0000000..606a6a3 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/command/MkdirCommand.java @@ -0,0 +1,25 @@ +package de.vimo.command; + +import de.vimo.core.Argument; +import de.vimo.core.Command; +import de.vimo.core.Flag; + +import java.util.List; + +public class MkdirCommand implements Command { + + @Override + public int exec(List flags, List args) { + return 0; + } + + @Override + public String getName() { + return null; + } + + @Override + public List getFlags() { + return null; + } +} diff --git a/jcmd/src/main/java/de/vimo/core/Argument.java b/jcmd/src/main/java/de/vimo/core/Argument.java new file mode 100644 index 0000000..c7cf009 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/core/Argument.java @@ -0,0 +1,15 @@ +package de.vimo.core; + +public class Argument { + + private final String value; + + public Argument(String value) { + this.value = value; + } + + public String getValue() { + return value; + } +} + diff --git a/jcmd/src/main/java/de/vimo/core/Command.java b/jcmd/src/main/java/de/vimo/core/Command.java new file mode 100644 index 0000000..e29e252 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/core/Command.java @@ -0,0 +1,12 @@ +package de.vimo.core; + +import java.util.List; + +public interface Command { + + int exec(List flags, List args); + + String getName(); + + List getFlags(); +} diff --git a/jcmd/src/main/java/de/vimo/core/Flag.java b/jcmd/src/main/java/de/vimo/core/Flag.java new file mode 100644 index 0000000..7139abd --- /dev/null +++ b/jcmd/src/main/java/de/vimo/core/Flag.java @@ -0,0 +1,28 @@ +package de.vimo.core; + +public class Flag { + + private final String shortName; + + private final String longName; + + private final String description; + + public Flag(String shortName, String longName, String description) { + this.shortName = shortName; + this.longName = longName; + this.description = description; + } + + public String getShortName() { + return shortName; + } + + public String getLongName() { + return longName; + } + + public String getDescription() { + return description; + } +} diff --git a/jcmd/src/main/java/de/vimo/core/Line.java b/jcmd/src/main/java/de/vimo/core/Line.java new file mode 100644 index 0000000..289f0d6 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/core/Line.java @@ -0,0 +1,78 @@ +package de.vimo.core; + +import java.util.List; + +public class Line { + + private String command; + + private List flags; + + private List args; + + public Line(String command, List flags, List args) { + this.command = command; + this.flags = flags; + this.args = args; + } + + public Line(String command) { + this.command = command; + } + + + public String getCommand() { + return command; + } + + public List getFlags() { + return flags; + } + + public List getArgs() { + return args; + } + +// @Override +// public Line parse(String raw) { +// String regex = "[\\s]"; +// String[] myArray = raw.split(regex); +// +// for (int i = 1; i < myArray.length; i++) { +// if (myArray[i].contains("-")) { +// flags.add(myArray[i]); +// } else { +// args.add(myArray[i]); +// } +// } +// +// return new Line(myArray[0], flags, args); +// } + +// +// --- Please ignore this --- +// +// private final Command command; +// +// private final List flags; +// +// private final List args; +// +// public Line(Command command, List flags, List args) { +// this.command = command; +// this.flags = flags; +// this.args = args; +// } +// +// public Command getCommand() { +// return command; +// } +// +// public List getFlags() { +// return flags; +// } +// +// public List getArgs() { +// return args; +// } +} diff --git a/jcmd/src/main/java/de/vimo/core/Parser.java b/jcmd/src/main/java/de/vimo/core/Parser.java new file mode 100644 index 0000000..23c0f8e --- /dev/null +++ b/jcmd/src/main/java/de/vimo/core/Parser.java @@ -0,0 +1,6 @@ +package de.vimo.core; + +public interface Parser { + + Line parse(String raw); +} diff --git a/jcmd/src/main/java/de/vimo/core/ParserVivi.java b/jcmd/src/main/java/de/vimo/core/ParserVivi.java new file mode 100644 index 0000000..56eff31 --- /dev/null +++ b/jcmd/src/main/java/de/vimo/core/ParserVivi.java @@ -0,0 +1,117 @@ +package de.vimo.core; + + +import java.util.ArrayList; +import java.util.List; + +public class ParserVivi implements Parser { + + List flags = new ArrayList<>(List.of()); + List args = new ArrayList<>(List.of()); + + + @Override + public Line parse(String raw) { + + flags = new ArrayList<>(List.of()); + args = new ArrayList<>(List.of()); + + String replaceWords = raw.replaceAll("\\s+"," "); + String trimWords = replaceWords.trim(); + String[] arrayWords = trimWords.split(" "); + + String command = null; + boolean commandCheck = isCommandValid(arrayWords); + boolean flagsCheck = isFlagsValid(arrayWords); + boolean argsCheck = isArgsValid(arrayWords); + + Line resultLine = null; + + if (arrayWords.length > 0) { + if (commandCheck) { + command = arrayWords[0]; + if (arrayWords.length == 1) { + resultLine = new Line(command, flags, args); + } else if (flagsCheck && !argsCheck) { + resultLine = new Line(command, flags, args); + } else if (!flagsCheck && argsCheck) { + resultLine = new Line(command, flags, args); + } else if (flagsCheck && argsCheck) { + resultLine = new Line(command, flags, args); + } + } + } + return resultLine; + } + + public boolean isArgsValid(String[] words) { + int arrayWordsLength = words.length; + boolean isArgsValid = false; + char[] charWord = new char[0]; + + for (int i = 1; i < words.length; i++) { + charWord = words[i].toCharArray(); + + if (checkCharArgs(charWord)) { + args.add(words[i]); + isArgsValid = true; + } + } + return isArgsValid; + } + + public boolean checkCharArgs(char[] charWord) { + boolean isChecked = false; + + if (charWord.length != 0) { + if (!(charWord[0] == '-')) { + isChecked = true; + } + } + return isChecked; + } + + public boolean isCommandValid(String[] arrayWords) { + int arrayWordsLength = arrayWords.length; + boolean isCommandValid = false; + + if (arrayWordsLength != 0) { + if (arrayWords[0].matches("[a-zA-Z]+")) { + isCommandValid = true; + } + } + return isCommandValid; + } + + public boolean isFlagsValid(String[] words) { + char[] charWord = new char[0]; + boolean isFlagsValid = false; + + for (int i = 1; i < words.length; i++) { + if (words[i].contains("-")) { + charWord = words[i].toCharArray(); + if (checkChar(charWord)) { + for (int j = 1; j < charWord.length; j++) { + flags.add(String.valueOf(charWord[j])); + } + isFlagsValid = true; + } + } + } + return isFlagsValid; + } + + public boolean checkChar(char[] charWord) { + boolean isChecked = false; + + if (charWord.length != 1) { + for (int i = 0; i < charWord.length; i++) { + if (charWord[0] == '-' && Character.isLetter(charWord[1])) { + isChecked = true; + break; + } + } + } + return isChecked; + } +} diff --git a/jcmd/src/site/site.xml b/jcmd/src/site/site.xml new file mode 100644 index 0000000..461f180 --- /dev/null +++ b/jcmd/src/site/site.xml @@ -0,0 +1,26 @@ + + + + + jcmd + https://maven.apache.org/images/apache-maven-project.png + https://www.apache.org/ + + + + https://maven.apache.org/images/maven-logo-black-on-white.png + https://maven.apache.org/ + + + + org.apache.maven.skins + maven-fluido-skin + 1.7 + + + + + + + \ No newline at end of file diff --git a/jcmd/src/test/java/de/vimo/core/ParserTest.java b/jcmd/src/test/java/de/vimo/core/ParserTest.java new file mode 100644 index 0000000..c75acd1 --- /dev/null +++ b/jcmd/src/test/java/de/vimo/core/ParserTest.java @@ -0,0 +1,49 @@ +package de.vimo.core; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + + +public class ParserTest +{ + @Test + public void testParser() { + // <-- Your implementation goes here + + Parser parser = new ParserVivi(); // <-- Your implementation goes here + + // test invalid strings -> should return null + assertNull(parser.parse("")); + assertNull(parser.parse("-")); + assertNull(parser.parse("ls -")); + assertNull(parser.parse("ls --")); + + // test valid string + testInput(parser, "ls", "ls", List.of(), List.of()); + testInput(parser, " ls -la", "ls", List.of("l", "a"), List.of()); + testInput(parser, "ls ./hello/world ", "ls", List.of(), List.of("./hello/world")); + testInput(parser, "ls -la ./what", "ls", List.of("l", "a"), List.of("./what")); + testInput(parser, "rm -r /bin /opt", "rm", List.of("r"), List.of("/bin", "/opt")); + testInput(parser, " cd . ", "cd", List.of(), List.of(".")); + testInput(parser, " mkdir . ", "mkdir", List.of(), List.of(".")); + testInput(parser, "super -abc -de -f super nice danke paul", "super", List.of("a", "b", "c", "d", "e", "f"), List.of("super", "nice", "danke", "paul")); + + // new tests + + testInput(parser, "print hello-world", "print", List.of(), List.of("hello-world")); + } + + private void testInput(Parser parser, String input, String expectedCommand, List expectedFlags, List expectedArgs) { + Line line = parser.parse(input); + + assertEquals(expectedCommand, line.getCommand(), "Wrong command"); + assertEquals(expectedFlags, line.getFlags(), "Wrong flags"); + assertEquals(expectedArgs, line.getArgs(), "Wrong args"); + } +} diff --git a/jcmd/target/.DS_Store b/jcmd/target/.DS_Store new file mode 100644 index 0000000..24cf8ce Binary files /dev/null and b/jcmd/target/.DS_Store differ diff --git a/jcmd/target/classes/de/vimo/InputTextTillCommand.class b/jcmd/target/classes/de/vimo/InputTextTillCommand.class new file mode 100644 index 0000000..f06ebc0 Binary files /dev/null and b/jcmd/target/classes/de/vimo/InputTextTillCommand.class differ diff --git a/jcmd/target/classes/de/vimo/Jcmd.class b/jcmd/target/classes/de/vimo/Jcmd.class new file mode 100644 index 0000000..21cd8ba Binary files /dev/null and b/jcmd/target/classes/de/vimo/Jcmd.class differ diff --git a/jcmd/target/classes/de/vimo/Main.class b/jcmd/target/classes/de/vimo/Main.class new file mode 100644 index 0000000..a0e73ec Binary files /dev/null and b/jcmd/target/classes/de/vimo/Main.class differ diff --git a/jcmd/target/classes/de/vimo/check/StringCheck.class b/jcmd/target/classes/de/vimo/check/StringCheck.class new file mode 100644 index 0000000..cdfdc21 Binary files /dev/null and b/jcmd/target/classes/de/vimo/check/StringCheck.class differ diff --git a/jcmd/target/classes/de/vimo/command/CdCommand.class b/jcmd/target/classes/de/vimo/command/CdCommand.class new file mode 100644 index 0000000..c20fbef Binary files /dev/null and b/jcmd/target/classes/de/vimo/command/CdCommand.class differ diff --git a/jcmd/target/classes/de/vimo/command/HelpCommand.class b/jcmd/target/classes/de/vimo/command/HelpCommand.class new file mode 100644 index 0000000..9ed3e5e Binary files /dev/null and b/jcmd/target/classes/de/vimo/command/HelpCommand.class differ diff --git a/jcmd/target/classes/de/vimo/command/LsCommand.class b/jcmd/target/classes/de/vimo/command/LsCommand.class new file mode 100644 index 0000000..399d3c4 Binary files /dev/null and b/jcmd/target/classes/de/vimo/command/LsCommand.class differ diff --git a/jcmd/target/classes/de/vimo/command/MkdirCommand.class b/jcmd/target/classes/de/vimo/command/MkdirCommand.class new file mode 100644 index 0000000..1f105d0 Binary files /dev/null and b/jcmd/target/classes/de/vimo/command/MkdirCommand.class differ diff --git a/jcmd/target/classes/de/vimo/core/Argument.class b/jcmd/target/classes/de/vimo/core/Argument.class new file mode 100644 index 0000000..10d3d96 Binary files /dev/null and b/jcmd/target/classes/de/vimo/core/Argument.class differ diff --git a/jcmd/target/classes/de/vimo/core/Command.class b/jcmd/target/classes/de/vimo/core/Command.class new file mode 100644 index 0000000..7f7eb5c Binary files /dev/null and b/jcmd/target/classes/de/vimo/core/Command.class differ diff --git a/jcmd/target/classes/de/vimo/core/Flag.class b/jcmd/target/classes/de/vimo/core/Flag.class new file mode 100644 index 0000000..6faa4c4 Binary files /dev/null and b/jcmd/target/classes/de/vimo/core/Flag.class differ diff --git a/jcmd/target/classes/de/vimo/core/Line.class b/jcmd/target/classes/de/vimo/core/Line.class new file mode 100644 index 0000000..793ac8a Binary files /dev/null and b/jcmd/target/classes/de/vimo/core/Line.class differ diff --git a/jcmd/target/classes/de/vimo/core/Parser.class b/jcmd/target/classes/de/vimo/core/Parser.class new file mode 100644 index 0000000..9ea8001 Binary files /dev/null and b/jcmd/target/classes/de/vimo/core/Parser.class differ diff --git a/jcmd/target/classes/de/vimo/core/ParserVivi.class b/jcmd/target/classes/de/vimo/core/ParserVivi.class new file mode 100644 index 0000000..c462804 Binary files /dev/null and b/jcmd/target/classes/de/vimo/core/ParserVivi.class differ diff --git a/jcmd/target/test-classes/de/vimo/core/ParserTest.class b/jcmd/target/test-classes/de/vimo/core/ParserTest.class new file mode 100644 index 0000000..48bb754 Binary files /dev/null and b/jcmd/target/test-classes/de/vimo/core/ParserTest.class differ