-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChallenge16.java
65 lines (55 loc) · 1.61 KB
/
Challenge16.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package challenge16;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Stack;
import org.junit.jupiter.api.Test;
/**
* validate xml tags
*
*/
public class Challenge16 {
public static Boolean validateXml(String input) {
Boolean toReturn = true;
Stack<String> st = new Stack<>();
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("invalid input found");
}
char ch = input.charAt(0);
char chLast = input.charAt(input.length() - 1);
if (ch != '<' || chLast != '>') {
toReturn = false;
return toReturn;
}
while (true) {
int indexOfLess = input.indexOf('<');
int indexofClosing = input.indexOf('/');
int indexOfGreaterSymbol = input.indexOf('>');
if (indexOfLess != -1 && indexOfGreaterSymbol != -1 && indexofClosing != indexOfLess + 1) {
String toPush = input.substring(indexOfLess + 1, indexOfGreaterSymbol);
st.push(toPush);
} else {
indexOfLess = input.indexOf("</");
indexOfGreaterSymbol = input.indexOf(">");
String toPop = input.substring(indexOfLess + 2, indexOfGreaterSymbol);
if (!(st.pop().equals(toPop))) {
toReturn = false;
break;
}
}
input = input.substring(indexOfGreaterSymbol + 1);
if (input.length() <= 0)
break;
}
return toReturn;
}
/**
* Test.
*/
@Test
public void test() {
String str = "<a></a><b><c></c></b>";
Boolean output = Challenge16.validateXml(str);
assertTrue(output);
assertFalse(Challenge16.validateXml("dsad"));
}
}