This commit is contained in:
happymeal2024 2025-08-05 10:29:11 +02:00
parent 2d7e0f893a
commit 6e23dd7f1a
43 changed files with 668 additions and 1 deletions

1
jcmd

@ -1 +0,0 @@
Subproject commit 52e0edc1b95565edc335a1d1c120cf7b8f4bd4c9

BIN
jcmd/.DS_Store vendored Normal file

Binary file not shown.

3
jcmd/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

13
jcmd/.idea/compiler.xml generated Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="jcmd" />
</profile>
</annotationProcessing>
</component>
</project>

7
jcmd/.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

20
jcmd/.idea/jarRepositories.xml generated Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

12
jcmd/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="temurin-23" project-jdk-type="JavaSDK" />
</project>

7
jcmd/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

0
jcmd/.mvn/jvm.config Normal file
View File

0
jcmd/.mvn/maven.config Normal file
View File

92
jcmd/pom.xml Normal file
View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.vimo</groupId>
<artifactId>jcmd</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jcmd</name>
<description>A simple jcmd.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>

BIN
jcmd/src/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -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);
}

View File

@ -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) {
}
}

View File

@ -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());
}
}
}
}

View File

@ -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<Flag> flags, List<Argument> args) {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public List<Flag> getFlags() {
return null;
}
}

View File

@ -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<Flag> flags, List<Argument> args) {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public List<Flag> getFlags() {
return null;
}
}

View File

@ -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<Flag> flags, List<Argument> args) {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public List<Flag> getFlags() {
return null;
}
}

View File

@ -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<Flag> flags, List<Argument> args) {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public List<Flag> getFlags() {
return null;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,12 @@
package de.vimo.core;
import java.util.List;
public interface Command {
int exec(List<Flag> flags, List<Argument> args);
String getName();
List<Flag> getFlags();
}

View File

@ -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;
}
}

View File

@ -0,0 +1,78 @@
package de.vimo.core;
import java.util.List;
public class Line {
private String command;
private List<String> flags;
private List<String> args;
public Line(String command, List<String> flags, List<String> args) {
this.command = command;
this.flags = flags;
this.args = args;
}
public Line(String command) {
this.command = command;
}
public String getCommand() {
return command;
}
public List<String> getFlags() {
return flags;
}
public List<String> 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<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

@ -0,0 +1,6 @@
package de.vimo.core;
public interface Parser {
Line parse(String raw);
}

View File

@ -0,0 +1,117 @@
package de.vimo.core;
import java.util.ArrayList;
import java.util.List;
public class ParserVivi implements Parser {
List<String> flags = new ArrayList<>(List.of());
List<String> 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;
}
}

26
jcmd/src/site/site.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="jcmd" xmlns="http://maven.apache.org/DECORATION/1.8.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.8.0 http://maven.apache.org/xsd/decoration-1.8.0.xsd">
<bannerLeft>
<name>jcmd</name>
<src>https://maven.apache.org/images/apache-maven-project.png</src>
<href>https://www.apache.org/</href>
</bannerLeft>
<bannerRight>
<src>https://maven.apache.org/images/maven-logo-black-on-white.png</src>
<href>https://maven.apache.org/</href>
</bannerRight>
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.7</version>
</skin>
<body>
<menu ref="parent" />
<menu ref="reports" />
</body>
</project>

View File

@ -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<String> expectedFlags, List<String> expectedArgs) {
Line line = parser.parse(input);
assertEquals(expectedCommand, line.getCommand(), "Wrong command");
assertEquals(expectedFlags, line.getFlags(), "Wrong flags");
assertEquals(expectedArgs, line.getArgs(), "Wrong args");
}
}

BIN
jcmd/target/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.