From eebe96077d6ef1784908727e1e621243d2889b04 Mon Sep 17 00:00:00 2001 From: happymeal2024 Date: Tue, 5 Aug 2025 10:50:30 +0200 Subject: [PATCH] concept250805 --- src/main/java/de/vimo/core/Line.java | 48 +----- src/main/java/de/vimo/core/ParserVivi.java | 176 ++++++++++++--------- src/test/java/de/vimo/core/ParserTest.java | 23 +-- 3 files changed, 102 insertions(+), 145 deletions(-) diff --git a/src/main/java/de/vimo/core/Line.java b/src/main/java/de/vimo/core/Line.java index bc0eeab..16b09da 100644 --- a/src/main/java/de/vimo/core/Line.java +++ b/src/main/java/de/vimo/core/Line.java @@ -4,11 +4,11 @@ import java.util.List; public class Line { - private final String command; + private String command; - private final List flags; + private List flags; - private final List args; + private List args; public Line(String command, List flags, List args) { this.command = command; @@ -28,46 +28,4 @@ public class Line { 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/src/main/java/de/vimo/core/ParserVivi.java b/src/main/java/de/vimo/core/ParserVivi.java index 2d54cf6..2e69a82 100644 --- a/src/main/java/de/vimo/core/ParserVivi.java +++ b/src/main/java/de/vimo/core/ParserVivi.java @@ -1,97 +1,117 @@ package de.vimo.core; + import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class ParserVivi implements Parser { - private int positionFlag; - private String regex = "\\s"; - private List flagsS; - private String command = null; - private String[] splitFlag; - private String result; + List flags = new ArrayList<>(List.of()); + List args = new ArrayList<>(List.of()); + @Override public Line parse(String raw) { - List flagAll = new ArrayList<>(List.of()); - List argsS = new ArrayList<>(List.of()); + 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; - String[] myArray = raw.split(regex); - - for (int i = 0; i < myArray.length; i++) { - - if (!myArray[i].isEmpty()) { - - if (myArray[0].equals("-")) { - - resultLine = null; - - } else if (myArray[i].contains("./") || myArray[i].contains("/") || - myArray[i].contains(".")) { - - argsS.add(myArray[i]); - resultLine = new Line(command, flagAll, argsS); - - } else if (myArray.length == 1 && myArray[0].equals("ls")) { - - resultLine = new Line(myArray[0], flagAll, argsS); - - } else if (myArray[0].equals("ls") && myArray[1].equals("-")) { - - resultLine = null; - - } else if (myArray[0].equals("ls") && myArray[1].equals("--")) { - - resultLine = null; - - } else if (myArray[i].equals("super") && !myArray[i].contains("-") && - positionFlag == 0) { - - command = myArray[i]; - resultLine = new Line(command, flagAll, argsS); - - } else if (myArray[i].equals("ls") || myArray[i].equals("rm") || - myArray[i].equals("cd") || myArray[i].equals("mkdir")) { - - command = myArray[i]; - resultLine = new Line(command, flagAll, argsS); - - } else if (myArray[i].contains("-")) { - positionFlag = i; - - splitFlag = myArray[i].split("-"); - - if (splitFlag[0].isEmpty()) { - result = myArray[i].replace("-", ""); - - flagsS = List.of(result.split("")); - flagAll.addAll(flagsS); - - resultLine = new Line(command, flagAll, argsS); - } else { - argsS.add(myArray[i]); - resultLine = new Line(command, flagAll, argsS); - } - - } else if (!myArray[i].contains("-") && i > positionFlag && i != 0) { - - argsS.add(myArray[i]); - resultLine = new Line(command, flagAll, argsS); - } else { - command = myArray[i]; - resultLine = null; - } - } else { - if (myArray.length == 1) { - 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/src/test/java/de/vimo/core/ParserTest.java b/src/test/java/de/vimo/core/ParserTest.java index 59eaa92..c75acd1 100644 --- a/src/test/java/de/vimo/core/ParserTest.java +++ b/src/test/java/de/vimo/core/ParserTest.java @@ -14,28 +14,7 @@ public class ParserTest { @Test public void testParser() { -// Parser parser = new Parser() { -// @Override -// public Line parse(String raw) { -// String regex = "[\\s]"; -// List flagsS = new ArrayList<>(List.of()); -// List flagAll = new ArrayList<>(List.of()); -// String result = ""; -// List argsS = new ArrayList<>(List.of()); -// String[] myArray = raw.split(regex); -// -// for (int i = 1; i < myArray.length; i++) { -// if (myArray[i].contains("-")) { -// result = myArray[i].replace("-",""); -// flagsS = List.of(result.split("")); -// flagAll.addAll(flagsS); -// } else { -// argsS.add(myArray[i]); -// } -// } -// return new Line(myArray[0], flagAll, argsS); -// } -// }; // <-- Your implementation goes here + // <-- Your implementation goes here Parser parser = new ParserVivi(); // <-- Your implementation goes here