Add a server for checking basic XML integrity

This commit is contained in:
Manuel Thalmann 2022-09-27 13:05:36 +02:00
parent cdc7517afa
commit f53e58a3b8

View file

@ -0,0 +1,30 @@
package ch.nuth.zhaw.exbox;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
public class WellformedXmlServer implements CommandExecutor {
Pattern tagPattern = Pattern.compile("<(/?)(\\w+)( \\w+=\"[^\"]*\")*>");
@Override
public String execute(String command) throws Exception {
return Boolean.toString(checkWellformed(command)) + "\n";
}
public boolean checkWellformed(String code) {
ListStack openTags = new ListStack();
Iterable<MatchResult> results = () -> tagPattern.matcher(code).results().iterator();
for (MatchResult result : results) {
if (result.group(1).length() == 0) {
openTags.push(result.group(2));
} else if (result.group(2).equals(openTags.peek())) {
openTags.pop();
} else {
return false;
}
}
return openTags.isEmpty();
}
}