mirror of
https://github.com/13hannes11/archive.git
synced 2024-09-03 21:50:58 +02:00
added all past projects
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
|
||||
<fileset name="all" enabled="true" check-config-name="Programmieren_Assignment06_WS_2015" local="false">
|
||||
<file-match-pattern match-pattern="." include-pattern="true"/>
|
||||
</fileset>
|
||||
</fileset-config>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Assignment6C_HtmlParser</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
Binary file not shown.
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="false">
|
||||
<output url="file://$MODULE_DIR$/bin" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
|
||||
</component>
|
||||
</module>
|
||||
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.
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>head
|
||||
</head>
|
||||
<title>Titel</title>
|
||||
<!-- Titel der Seite -->
|
||||
<body>Inhalt des HTML-Dokumentes Zeile1</body>
|
||||
<body>Inhalt des
|
||||
HTML-Dokumentes
|
||||
Zeile2</body>
|
||||
<!-- Ende des HTML-Dokumentes -->
|
||||
</html>
|
||||
@@ -0,0 +1,75 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Enum Command.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public enum Command {
|
||||
|
||||
/** set command. */
|
||||
SEARCH("search", 1),
|
||||
|
||||
/** tag coommand */
|
||||
TAG("tag", 1),
|
||||
|
||||
/** quit command. */
|
||||
QUIT("quit", 0),
|
||||
|
||||
/** invalid command. */
|
||||
INVALID("", -1);
|
||||
|
||||
/** The command. */
|
||||
private final String command;
|
||||
|
||||
/** The param count. */
|
||||
private final int paramCount;
|
||||
|
||||
/**
|
||||
* Instantiates a new command.
|
||||
*
|
||||
* @param command
|
||||
* the command
|
||||
* @param paramCount
|
||||
* the param count
|
||||
*/
|
||||
private Command(final String command, final int paramCount) {
|
||||
this.paramCount = paramCount;
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the command.
|
||||
*
|
||||
* @return the command
|
||||
*/
|
||||
public String getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the param count.
|
||||
*
|
||||
* @return the param count
|
||||
*/
|
||||
public int getParamCount() {
|
||||
return paramCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts string to command.
|
||||
*
|
||||
* @param str
|
||||
* the str
|
||||
* @return the command
|
||||
*/
|
||||
public static Command convertToCommand(final String str) {
|
||||
for (final Command command : Command.values()) {
|
||||
if (command.getCommand().equals(str)) {
|
||||
return command;
|
||||
}
|
||||
}
|
||||
return INVALID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Class CommandLineParser.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CommandLineParser {
|
||||
private static final String COMMAND_NOT_FOUND = "not valid command. Use: 'search <word>', 'tag <tag>', 'quit'";
|
||||
private static final String WRONG_PARAMETER_COUNT = "the command expects %d parameter";
|
||||
private boolean readyToQuit;
|
||||
private final Tag tag;
|
||||
|
||||
/**
|
||||
* Instantiates a new command line parser.
|
||||
*
|
||||
* @param fileContent
|
||||
* the file content
|
||||
*/
|
||||
public CommandLineParser(final String fileContent) {
|
||||
readyToQuit = false;
|
||||
tag = new Tag(fileContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the command line parser
|
||||
*/
|
||||
public void run() {
|
||||
while (!readyToQuit) {
|
||||
runCommand(Terminal.readLine());
|
||||
}
|
||||
}
|
||||
|
||||
private void runCommand(final String command) {
|
||||
if (command == null) {
|
||||
error(COMMAND_NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
final String[] arr = command.split("[\\s]");
|
||||
String[] parameters;
|
||||
if (arr.length > 1) {
|
||||
parameters = arr[1].split("[\\,]");
|
||||
} else {
|
||||
parameters = new String[0];
|
||||
}
|
||||
|
||||
final Command c = Command.convertToCommand(arr[0]);
|
||||
// If parameter count does not match command
|
||||
if (parameters.length != c.getParamCount() && !c.equals(Command.INVALID)) {
|
||||
error(String.format(WRONG_PARAMETER_COUNT, c.getParamCount()));
|
||||
return;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case QUIT:
|
||||
readyToQuit = true;
|
||||
break;
|
||||
case SEARCH:
|
||||
search(parameters);
|
||||
break;
|
||||
case TAG:
|
||||
tag(parameters);
|
||||
break;
|
||||
default:
|
||||
error(COMMAND_NOT_FOUND);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void search(final String[] parameters) {
|
||||
if (!parameters[0].matches("[a-zA-Z0-9_-]+")) {
|
||||
error("the parameters of search have to consist of letters, numbers, '_' and '-'");
|
||||
return;
|
||||
}
|
||||
final String str = tag.toString();
|
||||
|
||||
int count = str.split(" " + parameters[0].toLowerCase() + " ").length - 1;
|
||||
if (str.startsWith(parameters[0].toLowerCase())) {
|
||||
count++;
|
||||
}
|
||||
if (str.endsWith(parameters[0].toLowerCase())) {
|
||||
count++;
|
||||
}
|
||||
Terminal.printLine(Integer.toString(count));
|
||||
}
|
||||
|
||||
private void tag(final String[] parameters) {
|
||||
if (!parameters[0].matches("[a-z0-9]+") || parameters[0].equals("head")) {
|
||||
error("the parameters of 'tag' have to consist of lower case letters and/or numbers "
|
||||
+ "and searches for 'head' are forbidden");
|
||||
return;
|
||||
}
|
||||
Terminal.printLine(tag.getTagText(parameters[0]).trim().toLowerCase());
|
||||
}
|
||||
|
||||
private void error(final String str) {
|
||||
Terminal.printLine("Error, " + str);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Helper class for reading text files.
|
||||
*
|
||||
* @author IPD Reussner, KIT
|
||||
* @author ITI Sinz, KIT
|
||||
* @version 1.1
|
||||
*/
|
||||
public final class FileInputHelper {
|
||||
|
||||
/**
|
||||
* Private constructor to avoid instantiation.
|
||||
*/
|
||||
private FileInputHelper() {
|
||||
// intentionally left blank
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the specified file and returns its content as a String array, where
|
||||
* the first array field contains the
|
||||
* file's first line, the second field contains the second line, and so on.
|
||||
*
|
||||
* @param file
|
||||
* the file to be read
|
||||
* @return the content of the file
|
||||
*/
|
||||
public static String read(final String file) {
|
||||
final StringBuilder result = new StringBuilder();
|
||||
|
||||
FileReader in = null;
|
||||
try {
|
||||
in = new FileReader(file);
|
||||
} catch (final FileNotFoundException e) {
|
||||
Terminal.printLine("Error, " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
final BufferedReader reader = new BufferedReader(in);
|
||||
try {
|
||||
String line = reader.readLine();
|
||||
while (line != null) {
|
||||
result.append(line);
|
||||
line = reader.readLine();
|
||||
if (line != null) {
|
||||
result.append("\n");
|
||||
}
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
Terminal.printLine("Error, " + e.getMessage());
|
||||
System.exit(1);
|
||||
} finally {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (final IOException e) {
|
||||
// no need for handling this exception
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Class Main.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public final class Main {
|
||||
|
||||
/**
|
||||
* Instantiates a new main.
|
||||
*/
|
||||
private Main() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The main method.
|
||||
*
|
||||
* @param args
|
||||
* the arguments
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
if (args.length != 1) {
|
||||
Terminal.printLine("Error, wrong number of parameters");
|
||||
}
|
||||
final String path = args[0];
|
||||
final CommandLineParser cParser = new CommandLineParser(FileInputHelper.read(path));
|
||||
cParser.run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
/**
|
||||
* The Class Pair.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*
|
||||
* @param <T>
|
||||
* the generic type
|
||||
* @param <U>
|
||||
* the generic type
|
||||
*/
|
||||
public class Pair<T, U> {
|
||||
|
||||
private final T first;
|
||||
private final U second;
|
||||
|
||||
/**
|
||||
* Instantiates a new pair.
|
||||
*
|
||||
* @param first
|
||||
* the first
|
||||
* @param second
|
||||
* the second
|
||||
*/
|
||||
public Pair(final T first, final U second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first element of the pair.
|
||||
*
|
||||
* @return the first
|
||||
*/
|
||||
public T getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the second element of the pair.
|
||||
*
|
||||
* @return the second
|
||||
*/
|
||||
public U getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The Class Tag.
|
||||
*
|
||||
* @author Hannes Kuchelmeister
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Tag {
|
||||
private final String name;
|
||||
private final ArrayList<Pair<Integer, String>> text;
|
||||
private final ArrayList<Tag> subTags;
|
||||
|
||||
/**
|
||||
* Instantiates a new tag.
|
||||
*
|
||||
* @param str
|
||||
* the string which starts with the tag and ends with the tag
|
||||
*/
|
||||
public Tag(final String str) {
|
||||
final String noEnter = str.replace("\n", " ");
|
||||
text = new ArrayList<Pair<Integer, String>>();
|
||||
|
||||
subTags = new ArrayList<Tag>();
|
||||
final String[] splitted = noEnter.split("(?<=" + ">)|(?=(</)" + ")");
|
||||
name = getTagName(splitted[0].trim());
|
||||
|
||||
int openTag = -1;
|
||||
String subTagName = "";
|
||||
for (int i = 1; i < splitted.length; i++) {
|
||||
// closing tag
|
||||
if (splitted[i].trim().matches("[<][\\/]([a-z0-9]|)+[>]") && openTag >= 0) {
|
||||
if (getTagName(splitted[i].trim()).equals(subTagName)) {
|
||||
String sub = "";
|
||||
for (int j = openTag; j <= i; j++) {
|
||||
sub += splitted[j];
|
||||
}
|
||||
subTags.add(new Tag(sub));
|
||||
openTag = -1;
|
||||
}
|
||||
}
|
||||
// opening tag
|
||||
if (splitted[i].trim().matches("[</][a-z0-9]+[>]") && openTag == -1) {
|
||||
openTag = i;
|
||||
subTagName = getTagName(splitted[i].trim());
|
||||
}
|
||||
// text
|
||||
if (splitted[i].trim().matches("([a-zA-Z0-9_-](\\s[a-zA-Z0-9_-])?)+") && openTag == -1) {
|
||||
final String[] tmpStrArr = splitted[i].split(" ");
|
||||
for (final String string : tmpStrArr) {
|
||||
this.text.add(new Pair<Integer, String>(new Integer(subTags.size()), string.toLowerCase()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches f.
|
||||
*
|
||||
* @param tagName
|
||||
* the tag name
|
||||
* @return the string
|
||||
*/
|
||||
public String getTagText(final String tagName) {
|
||||
if (this.name.equals(tagName)) {
|
||||
return this.toString();
|
||||
}
|
||||
|
||||
String ret = "";
|
||||
for (final Tag tag : subTags) {
|
||||
if (!tag.getTagText(tagName).equals("")) {
|
||||
ret += tag.getTagText(tagName) + "\n";
|
||||
}
|
||||
}
|
||||
return ret.trim();
|
||||
}
|
||||
|
||||
private String getTagName(final String tag) {
|
||||
String tmp = "";
|
||||
for (final char c : tag.toCharArray()) {
|
||||
if (c != '<' && c != '>' && c != '/') {
|
||||
tmp += Character.toString(c);
|
||||
}
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the corresponding text to a tag
|
||||
*/
|
||||
private String intToText(final int i) {
|
||||
String ret = "";
|
||||
for (final Pair<Integer, String> pair : text) {
|
||||
if (pair.getFirst() == i) {
|
||||
ret += pair.getSecond() + " ";
|
||||
}
|
||||
}
|
||||
return ret.trim();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String str = "";
|
||||
for (int i = 0; i < subTags.size() || i < text.size(); i++) {
|
||||
str += intToText(i);
|
||||
if (i < subTags.size()) {
|
||||
str += subTags.get(i).toString() + " ";
|
||||
}
|
||||
}
|
||||
return str.trim();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package edu.kit.informatik;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* This class provides some simple methods for input/output from and to a terminal.
|
||||
*
|
||||
* Never modify this class, never upload it to Praktomat. This is only for your local use. If an assignment tells you to
|
||||
* use this class for input and output never use System.out or System.in in the same assignment.
|
||||
*
|
||||
* @author ITI, VeriAlg Group
|
||||
* @author IPD, SDQ Group
|
||||
* @version 4
|
||||
*/
|
||||
public final class Terminal {
|
||||
|
||||
/**
|
||||
* BufferedReader for reading from standard input line-by-line.
|
||||
*/
|
||||
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
/**
|
||||
* Private constructor to avoid object generation.
|
||||
*/
|
||||
private Terminal() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a String to the standard output.
|
||||
*
|
||||
* The String out must not be null.
|
||||
*
|
||||
* @param out
|
||||
* The string to be printed.
|
||||
*/
|
||||
public static void printLine(String out) {
|
||||
System.out.println(out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a line from standard input.
|
||||
*
|
||||
* Returns null at the end of the standard input.
|
||||
*
|
||||
* Use Ctrl+D to indicate the end of the standard input.
|
||||
*
|
||||
* @return The next line from the standard input or null.
|
||||
*/
|
||||
public static String readLine() {
|
||||
try {
|
||||
return in.readLine();
|
||||
} catch (IOException e) {
|
||||
/*
|
||||
* rethrow unchecked (!) exception to prevent students from being forced to use Exceptions before they have
|
||||
* been introduced in the lecture.
|
||||
*/
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>head
|
||||
</head>
|
||||
<title>Titel</title>
|
||||
<!-- Titel der Seite -->
|
||||
<body>Inhalt des HTML-Dokumentes Zeile1</body>
|
||||
<body>Inhalt des
|
||||
HTML-Dokumentes
|
||||
Zeile2</body>
|
||||
<!-- Ende des HTML-Dokumentes -->
|
||||
</html>
|
||||
Reference in New Issue
Block a user