Add a server for checking basic XML
integrity
This commit is contained in:
parent
cdc7517afa
commit
f53e58a3b8
1 changed files with 30 additions and 0 deletions
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue