-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractor.java
More file actions
44 lines (38 loc) · 1.55 KB
/
Copy pathExtractor.java
File metadata and controls
44 lines (38 loc) · 1.55 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
38
39
40
41
42
43
44
public class Extractor {
/**
* This method loops through the HTML structure contained in the received param doc and returns the deepest level text.
* If the HTML structure is malformed, returns a malformed HTML message.
* @param doc string that contains the HTML content loaded from a given URL.
* @return the string that contains:
* 1. The deepest text within the HTML structure; OR
* 2. The message "malformed HTML" when doc has syntax errors
*/
public String findDeepest(String doc){
Validator validator = new Validator();
String result = null;
String[] docLines = doc.split(System.lineSeparator());
int accLevel = 0;
int lastLevel = -1;
int opened = 0;
int closed = 0;
boolean mismatch = false;
for (int i = 0; i < doc.lines().count(); i++) {
if(!docLines[i].isBlank()){
if(docLines[i].contains("<") && docLines[i].contains(">") && !docLines[i].contains("</")){
accLevel++;
opened++;
} else if((!docLines[i].contains("</")) && lastLevel < accLevel) {
result = docLines[i].trim();
lastLevel = accLevel;
} else if(docLines[i].contains("</")) {
accLevel--;
closed++;
}
}
}
mismatch = validator.isValid(doc);
if(opened != closed || opened == 0 || mismatch)
return "malformed HTML";
return result;
}
}