concept250805

This commit is contained in:
happymeal2024 2025-08-05 10:50:30 +02:00
parent 2c0ba72e17
commit eebe96077d
3 changed files with 102 additions and 145 deletions

View File

@ -4,11 +4,11 @@ import java.util.List;
public class Line {
private final String command;
private String command;
private final List<String> flags;
private List<String> flags;
private final List<String> args;
private List<String> args;
public Line(String command, List<String> flags, List<String> 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<Flag> flags;
//
// private final List<Argument> args;
//
// public Line(Command command, List<Flag> flags, List<Argument> args) {
// this.command = command;
// this.flags = flags;
// this.args = args;
// }
//
// public Command getCommand() {
// return command;
// }
//
// public List<Flag> getFlags() {
// return flags;
// }
//
// public List<Argument> getArgs() {
// return args;
// }
}

View File

@ -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<String> flagsS;
private String command = null;
private String[] splitFlag;
private String result;
List<String> flags = new ArrayList<>(List.of());
List<String> args = new ArrayList<>(List.of());
@Override
public Line parse(String raw) {
List<String> flagAll = new ArrayList<>(List.of());
List<String> 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;
}
}

View File

@ -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<String> flagsS = new ArrayList<>(List.of());
// List<String> flagAll = new ArrayList<>(List.of());
// String result = "";
// List<String> 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