-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlAnalyzerFacade.java
More file actions
61 lines (50 loc) · 1.87 KB
/
Copy pathHtmlAnalyzerFacade.java
File metadata and controls
61 lines (50 loc) · 1.87 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.ArrayList;
import java.util.List;
/**
* Facade que comanda o fluxo de comandos para analise e verificação do código HTML
*/
public class HtmlAnalyzerFacade {
private final HtmlFetcher fetcher;
private final HtmlValidator validator;
private final HtmlParser parser;
public HtmlAnalyzerFacade() {
this.fetcher = new HtmlFetcher();
this.validator = new HtmlValidator();
this.parser = new HtmlParser();
}
/**
* Executa o fluxo completo de análise de HTML.
* @param url URL do HTML a ser analisado.
* @return Texto no nível mais profundo do HTML.
* @throws UrlConnectionException Se houver erro na conexão com a URL.
* @throws MalformedHtmlException Se o HTML estiver malformado.
*/
public String analyzeHtml(String url) throws UrlConnectionException, MalformedHtmlException {
String htmlContent = fetcher.fetchContent(url);
List<String> lines = preprocessHtml(htmlContent);
if (validator.isMalformedHtml(lines)) {
throw new MalformedHtmlException("malformed HTML");
}
String deepestText = parser.parse(lines);
if(deepestText.isEmpty()){
throw new MalformedHtmlException("malformed HTML");
}
return deepestText;
}
/**
* Pré-processa o conteúdo HTML: divide em linhas, remove espaços em branco e linhas vazias.
* @param htmlContent Conteúdo HTML bruto.
* @return Lista de linhas pré-processadas.
*/
private List<String> preprocessHtml(String htmlContent) {
List<String> lines = new ArrayList<>();
String[] rawLines = htmlContent.split("\n");
for (String line : rawLines) {
String trimmedLine = line.trim();
if (!trimmedLine.isEmpty()) {
lines.add(trimmedLine);
}
}
return lines;
}
}