-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.java
More file actions
37 lines (32 loc) · 1.24 KB
/
Validator.java
File metadata and controls
37 lines (32 loc) · 1.24 KB
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
import java.util.Stack;
public class Validator {
/**
* This method loops through the HTML structure contained in the received param doc and check if the structure is malformed.
* @param doc string that contains the HTML content loaded from a given URL.
* @return boolean False if the HTML content is not malformed, boolean True if the HTML content is malformed.
*/
public boolean isValid(String doc) {
String[] docLines = doc.split(System.lineSeparator());;
String topItem = null;
Stack<String> stack = new Stack<>();
for (int i = 0; i < doc.lines().count(); i++) {
String[] line = docLines[i].split("\\s") ;
for(String word : line) {
if(word.contains("<")&&!word.contains("</")){
stack.push(word);
}
if(word.startsWith("</")&& !stack.empty()) {
topItem = stack.peek().replaceAll("[^a-zA-Z ]", "");
if(topItem.equals(word.replaceAll("[^a-zA-Z ]", ""))){
stack.pop();
}
}
}
}
if(stack.empty()){
return false;
} else {
return true;
}
}
}