Compare commits
3 commits
0f808a371b
...
3081d67e0f
Author | SHA1 | Date | |
---|---|---|---|
3081d67e0f | |||
f53e58a3b8 | |||
cdc7517afa |
3 changed files with 93 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();
|
||||||
|
}
|
||||||
|
}
|
29
app/src/test/java/ch/nuth/zhaw/exbox/ADS1_3_test.java
Normal file
29
app/src/test/java/ch/nuth/zhaw/exbox/ADS1_3_test.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package ch.nuth.zhaw.exbox;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class ADS1_3_test {
|
||||||
|
BracketServer bs;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
bs = new BracketServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void test(String content, boolean expected) {
|
||||||
|
assertEquals(expected, bs.checkBrackets(content), content);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBracket() {
|
||||||
|
test("()", true);
|
||||||
|
test("(()]", false);
|
||||||
|
test("((([([])])))", true);
|
||||||
|
test("[(])", false);
|
||||||
|
test("[(3 +3)* 35 +3]* {3 +2}", true);
|
||||||
|
test("[({3 +3)* 35} +3]* {3 +2}", false);
|
||||||
|
}
|
||||||
|
}
|
34
app/src/test/java/ch/nuth/zhaw/exbox/ADS1_4_test.java
Normal file
34
app/src/test/java/ch/nuth/zhaw/exbox/ADS1_4_test.java
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package ch.nuth.zhaw.exbox;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class ADS1_4_test {
|
||||||
|
WellformedXmlServer xml;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
xml = new WellformedXmlServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void test(String content, boolean expected) {
|
||||||
|
assertEquals(expected, xml.checkWellformed(content), content);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testXmlAttributes() {
|
||||||
|
test("<a href=\"sugus\"></a>", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testXml() {
|
||||||
|
test("<a></a>", true);
|
||||||
|
test("<a>", false);
|
||||||
|
test("</a>", false);
|
||||||
|
test("<a/>", true);
|
||||||
|
test("<a><b></b></a>", true);
|
||||||
|
test("<a><b></a></b>", false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue