Commit 7d72f216cf8adfeb3f81ea28db6cd309589ccbb1
1 parent
02956882
Exists in
master
Limpesa de código e adição do lombok.jar
Showing
3 changed files
with
852 additions
and
779 deletions
Show diff stats
No preview for this file type
pom.xml
src/main/java/br/com/checker/emag/core/PresentationEvaluation.java
... | ... | @@ -26,784 +26,857 @@ import br.com.checker.emag.core.SpecificRecommendation.PresentationRecommendatio |
26 | 26 | import br.com.checker.emag.util.WebAgent; |
27 | 27 | import br.com.checker.emag.util.UrlSemArquiNoFinal; |
28 | 28 | |
29 | +public class PresentationEvaluation extends Evaluation { | |
29 | 30 | |
31 | + private PresentationEvaluation(Source document) { | |
32 | + super(document); | |
33 | + } | |
30 | 34 | |
31 | -public class PresentationEvaluation extends Evaluation { | |
32 | - | |
33 | - private PresentationEvaluation(Source document) { super(document); } | |
34 | - | |
35 | - private PresentationEvaluation(Source document,String url) { | |
36 | - super(document,url); | |
37 | - } | |
38 | - | |
39 | - public static class PresentationEvaluationBuilder extends EvaluationBuilder { | |
40 | - | |
41 | - @Override | |
42 | - protected PresentationEvaluation with(Source document) { return new PresentationEvaluation(document); } | |
43 | - | |
44 | - @Override | |
45 | - protected PresentationEvaluation with(Source document,String url) { return new PresentationEvaluation(document,url); } | |
46 | - | |
47 | - public SpecificRecommendation recommendation29() { return new EvaluationRecommendation29();} | |
48 | - public SpecificRecommendation recommendation30() { return new EvaluationRecommendation30();} | |
49 | - public SpecificRecommendation recommendation31() { return new EvaluationRecommendation31();} | |
50 | - public SpecificRecommendation recommendation32() { return new EvaluationRecommendation32();} | |
51 | - } | |
52 | - | |
53 | - | |
54 | - protected static class EvaluationRecommendation29 extends PresentationRecommendation{ | |
55 | - protected List<Occurrence> check() { return getEvaluation().checkRecommendation29();} | |
56 | - } | |
57 | - | |
58 | - protected static class EvaluationRecommendation30 extends PresentationRecommendation{ | |
59 | - protected List<Occurrence> check() { return getEvaluation().checkRecommendation30();} | |
60 | - } | |
61 | - protected static class EvaluationRecommendation31 extends PresentationRecommendation{ | |
62 | - protected List<Occurrence> check() { return getEvaluation().checkRecommendation31();} | |
63 | - } | |
64 | - protected static class EvaluationRecommendation32 extends PresentationRecommendation{ | |
65 | - protected List<Occurrence> check() { return getEvaluation().checkRecommendation32();} | |
66 | - } | |
67 | - | |
68 | - public List<Occurrence> check() { | |
69 | - getOccurrences().clear(); | |
70 | - getOccurrences().addAll(checkRecommendation29()); //comentado por Gibran | |
71 | - //getOccurrences().addAll(checkRecommendation30()); comentado por Gibran | |
72 | - //getOccurrences().addAll(checkRecommendation31()); comentado por Gibran | |
73 | - getOccurrences().addAll(checkRecommendation32()); | |
74 | - | |
75 | - return getOccurrences(); | |
76 | - } | |
77 | - | |
78 | - | |
79 | - private List<Occurrence> checkRecommendation29() { | |
80 | - List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
81 | - | |
82 | - UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); | |
83 | - | |
84 | - String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal.urlSemArquivoNoFinal(getUrl()); | |
85 | - //verificar css externo | |
86 | - String href = null; | |
87 | - boolean avalia = false; | |
88 | - | |
89 | - //Adiciona todo css externo em uma lista | |
90 | - List<String> lsCssExterno = new ArrayList<String>(); | |
91 | - | |
92 | - for(Element link2 : getDocument().getAllElements("link")) { | |
93 | - href = link2.getAttributeValue("href"); | |
94 | - if(href.startsWith("www"))href= "http://"+ href; | |
95 | - | |
96 | - avalia = getUrl() != null || href.startsWith("http"); | |
97 | - | |
98 | - if(href.contains(".css") && avalia) { | |
99 | - | |
100 | - | |
101 | - if(!href.startsWith("http")) href = urlSemArquiNoFinal + "/" + href; | |
102 | - | |
103 | - | |
104 | - lsCssExterno.add(WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent()); | |
105 | - | |
106 | - } | |
107 | - } | |
108 | - //Fim Adiciona todo css externo em uma lista | |
109 | - | |
110 | - AvaliadorContraste avaliadorContraste = new AvaliadorContraste(); | |
111 | - | |
112 | - Color corAvaliar = null; | |
113 | - Color foreground = null; | |
114 | - Color background = null; | |
115 | - Color backgroundDoBody = null; | |
116 | - boolean achou_foreground = false; | |
117 | - boolean achou_background = false; | |
118 | - String styleInline = ""; | |
119 | - String procurarPorId = ""; | |
120 | - String procurarPorClasse = ""; | |
121 | - String cssParaBuscarCor = ""; | |
122 | - | |
123 | - List<Element> lsCssInterno = new ArrayList<Element>(); | |
124 | - | |
125 | - //pega css interno caso tenha | |
126 | - for (Element head : getDocument().getAllElements("head")) { | |
127 | - for (Element estilo : head.getAllElements("style")) { | |
128 | - lsCssInterno.add(estilo); | |
129 | - } | |
130 | - } | |
131 | - | |
132 | - //Pega todos elementos do "body" | |
133 | - for (Element body : getDocument().getAllElements("body")) { | |
134 | - for (Element element : body.getAllElements()) { | |
135 | - | |
136 | - //verificar se o "body" possui o atributo "bgcolor" | |
137 | - if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
138 | - { | |
139 | - if(element.toString().contains("bgcolor")) | |
140 | - { | |
141 | - try { | |
142 | - corAvaliar = new Color(Integer.parseInt(element.getStartTag().getAttributeValue("bgcolor").trim().replaceFirst("#",""), 16)); | |
143 | - if(corAvaliar != null) | |
144 | - { | |
145 | - background = corAvaliar; | |
146 | - } | |
147 | - } catch (Exception e) { | |
148 | - e.printStackTrace(); | |
149 | - } | |
150 | - } | |
151 | - } | |
152 | - | |
153 | - //Primeiro verificar se tem css in-line | |
154 | - styleInline = element.getStartTag().getAttributeValue("style"); | |
155 | - if(styleInline != null){ | |
156 | - | |
157 | - | |
158 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
159 | - corAvaliar = getColor(styleInline, "color:(.*?)\\Z"); | |
160 | - if(corAvaliar != null) | |
161 | - { | |
162 | - foreground = corAvaliar; | |
163 | - } | |
164 | - | |
165 | - corAvaliar = getColor(styleInline, "color:(.*?)\\;"); | |
166 | - if(corAvaliar != null) | |
167 | - { | |
168 | - foreground = corAvaliar; | |
169 | - } | |
170 | - | |
171 | - | |
172 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
173 | - corAvaliar = getColor(styleInline, "background:(.*?)\\Z"); | |
174 | - if(corAvaliar != null) | |
175 | - { | |
176 | - background = corAvaliar; | |
177 | - } | |
178 | - | |
179 | - corAvaliar = getColor(styleInline, "background:(.*?)\\;"); | |
180 | - if(corAvaliar != null) | |
181 | - { | |
182 | - background = corAvaliar; | |
183 | - } | |
184 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
185 | - corAvaliar = getColor(styleInline, "background-color:(.*?)\\Z"); | |
186 | - if(corAvaliar != null) | |
187 | - { | |
188 | - background = corAvaliar; | |
189 | - } | |
190 | - | |
191 | - corAvaliar = getColor(styleInline, "background-color:(.*?)\\;"); | |
192 | - if(corAvaliar != null) | |
193 | - { | |
194 | - background = corAvaliar; | |
195 | - } | |
196 | - | |
197 | - if(foreground != null) | |
198 | - { | |
199 | - achou_foreground = true; | |
200 | - } | |
201 | - | |
202 | - if(background != null) { | |
203 | - achou_background = true; | |
204 | - | |
205 | - if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
206 | - { | |
207 | - backgroundDoBody = background; | |
208 | - } | |
209 | - } | |
210 | - | |
211 | - | |
212 | - | |
213 | - }else | |
214 | - { | |
215 | - procurarPorId = "#" + element.getStartTag().getAttributeValue("id"); | |
216 | - procurarPorClasse = "." + element.getStartTag().getAttributeValue("class"); | |
217 | - | |
218 | - //verifica css interno | |
219 | - | |
220 | - //Verifica se tem #id | |
221 | - for (Element style : lsCssInterno) { | |
222 | - | |
223 | - //String pattern = ""; | |
224 | - | |
225 | - | |
226 | - //Coloca todo css em apenas uma linha, depois separa por blocos | |
227 | - String texto = style.toString().replace("\n",""); | |
228 | - texto = texto.replace(">", ">\n").replace("*/", "\n").replace(" {", "{").replace("}", " }\n").replace(" :", ":"); | |
229 | - cssParaBuscarCor = ""; | |
230 | - | |
231 | - | |
232 | - //vevifica 1º pelo "id" | |
233 | - if(!procurarPorId.equalsIgnoreCase("#null")) | |
234 | - { | |
235 | - cssParaBuscarCor = getElementoCss(texto, procurarPorId); | |
236 | - } | |
237 | - | |
238 | - //Se não encontrar pelo "id" verifica pela classe e elemento | |
239 | - if(cssParaBuscarCor.isEmpty() && !procurarPorClasse.equalsIgnoreCase(".null")) | |
240 | - { | |
241 | - cssParaBuscarCor = getElementoCss(texto, procurarPorClasse + element.getStartTag().getName()); | |
242 | - } | |
243 | - | |
244 | - //Se não encontrar pela classe e elemento, verifica pela classe | |
245 | - if(cssParaBuscarCor.isEmpty() && !procurarPorClasse.equalsIgnoreCase(".null")) | |
246 | - { | |
247 | - cssParaBuscarCor = getElementoCss(texto, procurarPorClasse); | |
248 | - } | |
249 | - //Se não encontrar pela classe, verifica pelo elemento | |
250 | - if(cssParaBuscarCor.isEmpty()) | |
251 | - { | |
252 | - cssParaBuscarCor = getElementoCss(texto, element.getStartTag().getName()); | |
253 | - } | |
254 | - | |
255 | - | |
256 | - | |
257 | - if(!cssParaBuscarCor.isEmpty()) | |
258 | - { | |
259 | - if(cssParaBuscarCor.contains("color:")) | |
260 | - { | |
261 | - corAvaliar = getColor(cssParaBuscarCor, "color:(.*?)\\Z"); | |
262 | - if(corAvaliar != null) | |
263 | - { | |
264 | - foreground = corAvaliar; | |
265 | - | |
266 | - } | |
267 | - | |
268 | - corAvaliar = getColor(cssParaBuscarCor, "color:(.*?)\\;"); | |
269 | - if(corAvaliar != null) | |
270 | - { | |
271 | - foreground = corAvaliar; | |
272 | - } | |
273 | - } | |
274 | - | |
275 | - if(cssParaBuscarCor.contains("background:") || cssParaBuscarCor.contains("background-color:")) | |
276 | - { | |
277 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
278 | - corAvaliar = getColor(cssParaBuscarCor, "background:(.*?)\\Z"); | |
279 | - if(corAvaliar != null) | |
280 | - { | |
281 | - background = corAvaliar; | |
282 | - } | |
283 | - | |
284 | - corAvaliar = getColor(cssParaBuscarCor, "background:(.*?)\\;"); | |
285 | - if(corAvaliar != null) | |
286 | - { | |
287 | - background = corAvaliar; | |
288 | - } | |
289 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
290 | - corAvaliar = getColor(cssParaBuscarCor, "background-color:(.*?)\\Z"); | |
291 | - if(corAvaliar != null) | |
292 | - { | |
293 | - background = corAvaliar; | |
294 | - } | |
295 | - | |
296 | - corAvaliar = getColor(cssParaBuscarCor, "background-color:(.*?)\\;"); | |
297 | - if(corAvaliar != null) | |
298 | - { | |
299 | - background = corAvaliar; | |
300 | - } | |
301 | - } | |
302 | - | |
303 | - if(foreground != null) | |
304 | - { | |
305 | - achou_foreground = true; | |
306 | - } | |
307 | - | |
308 | - if(background != null) { | |
309 | - achou_background = true; | |
310 | - | |
311 | - if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
312 | - { | |
313 | - backgroundDoBody = background; | |
314 | - } | |
315 | - } | |
316 | - | |
317 | - if(achou_foreground || achou_background) | |
318 | - { | |
319 | - break; | |
320 | - } | |
321 | - | |
322 | - } | |
323 | - | |
324 | - | |
325 | - corAvaliar = null; | |
326 | - foreground = null; | |
327 | - background = null; | |
328 | - cssParaBuscarCor = ""; | |
329 | - | |
330 | - } | |
331 | - | |
332 | - if(!achou_foreground && !achou_background) | |
333 | - { | |
334 | - //verifica css externo | |
335 | - | |
336 | - //Verifica se tem #id | |
337 | - for (String style : lsCssExterno) { | |
338 | - | |
339 | - if(style != null) | |
340 | - { | |
341 | - if(element.getStartTag().getAttributeValue("id") != null || element.getStartTag().getAttributeValue("class") != null) | |
342 | - { | |
343 | - //Coloca todo css em apenas uma linha, depois separa por blocos | |
344 | - String texto = style.toString().replace("\n",""); | |
345 | - texto = texto.replace(">", ">\n").replace("*/", "\n").replace(" {", "{").replace("}", " }\n").replace(" :", ":"); | |
346 | - cssParaBuscarCor = ""; | |
347 | - | |
348 | - //vevifica 1º pelo "id" | |
349 | - if(!procurarPorId.equalsIgnoreCase("#null")) | |
350 | - { | |
351 | - cssParaBuscarCor = getElementoCss(texto, procurarPorId); | |
352 | - } | |
353 | - | |
354 | - //Se não encontrar pelo "id" verifica pela classe e elemento | |
355 | - if(cssParaBuscarCor.isEmpty() && !procurarPorClasse.equalsIgnoreCase(".null")) | |
356 | - { | |
357 | - cssParaBuscarCor = getElementoCss(texto, procurarPorClasse + element.getStartTag().getName()); | |
358 | - } | |
359 | - | |
360 | - //Se não encontrar pela classe e elemento, verifica pela classe | |
361 | - if(cssParaBuscarCor.isEmpty() && !procurarPorClasse.equalsIgnoreCase(".null")) | |
362 | - { | |
363 | - cssParaBuscarCor = getElementoCss(texto, procurarPorClasse); | |
364 | - } | |
365 | - //Se não encontrar pela classe, verifica pelo elemento | |
366 | - if(cssParaBuscarCor.isEmpty()) | |
367 | - { | |
368 | - cssParaBuscarCor = getElementoCss(texto, element.getStartTag().getName()); | |
369 | - } | |
370 | - | |
371 | - | |
372 | - | |
373 | - if(!cssParaBuscarCor.isEmpty()) | |
374 | - { | |
375 | - if(cssParaBuscarCor.contains("color:")) | |
376 | - { | |
377 | - corAvaliar = getColor(cssParaBuscarCor, "color:(.*?)\\Z"); | |
378 | - if(corAvaliar != null) | |
379 | - { | |
380 | - foreground = corAvaliar; | |
381 | - | |
382 | - } | |
383 | - | |
384 | - corAvaliar = getColor(cssParaBuscarCor, "color:(.*?)\\;"); | |
385 | - if(corAvaliar != null) | |
386 | - { | |
387 | - foreground = corAvaliar; | |
388 | - } | |
389 | - | |
390 | - } | |
391 | - | |
392 | - if(cssParaBuscarCor.contains("background:") || cssParaBuscarCor.contains("background-color:")) | |
393 | - { | |
394 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
395 | - corAvaliar = getColor(cssParaBuscarCor, "background:(.*?)\\Z"); | |
396 | - if(corAvaliar != null) | |
397 | - { | |
398 | - background = corAvaliar; | |
399 | - } | |
400 | - | |
401 | - corAvaliar = getColor(cssParaBuscarCor, "background:(.*?)\\;"); | |
402 | - if(corAvaliar != null) | |
403 | - { | |
404 | - background = corAvaliar; | |
405 | - } | |
406 | - //pega o valor da cor, mesmo que no final não seja atribuido o ";" | |
407 | - corAvaliar = getColor(cssParaBuscarCor, "background-color:(.*?)\\Z"); | |
408 | - if(corAvaliar != null) | |
409 | - { | |
410 | - background = corAvaliar; | |
411 | - } | |
412 | - | |
413 | - corAvaliar = getColor(cssParaBuscarCor, "background-color:(.*?)\\;"); | |
414 | - if(corAvaliar != null) | |
415 | - { | |
416 | - background = corAvaliar; | |
417 | - } | |
418 | - } | |
419 | - | |
420 | - if(foreground != null) | |
421 | - { | |
422 | - achou_foreground = true; | |
423 | - } | |
424 | - | |
425 | - if(background != null) { | |
426 | - achou_background = true; | |
427 | - | |
428 | - if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
429 | - { | |
430 | - backgroundDoBody = background; | |
431 | - } | |
432 | - } | |
433 | - | |
434 | - if(achou_foreground || achou_background) | |
435 | - { | |
436 | - break; | |
437 | - } | |
438 | - | |
439 | - } | |
440 | - | |
441 | - } | |
442 | - } | |
443 | - | |
444 | - corAvaliar = null; | |
445 | - foreground = null; | |
446 | - background = null; | |
447 | - cssParaBuscarCor = ""; | |
448 | - | |
449 | - | |
450 | - } | |
451 | - } | |
452 | - | |
453 | - | |
454 | - } | |
455 | - procurarPorId = ""; | |
456 | - procurarPorClasse = ""; | |
457 | - | |
458 | - if(achou_foreground== true && achou_background == true){ | |
459 | - | |
460 | - avaliadorContraste.setCores(foreground, background); | |
461 | - avaliadorContraste.avaliar(); | |
462 | - | |
463 | - if(avaliadorContraste.getContraste() < new Double("4.5")){ | |
464 | - occurrences.add(buildOccurrence("4.1", false, element.getStartTag().toString(), element,"2")); | |
465 | - | |
466 | - } | |
467 | - achou_foreground = false; | |
468 | - achou_background = false; | |
469 | - | |
470 | - }//Caso não ache o background, usará o padrão encontrado no body | |
471 | - else if (achou_foreground == true && achou_background == false && backgroundDoBody != null) | |
472 | - { | |
473 | - avaliadorContraste.setCores(foreground, backgroundDoBody); | |
474 | - avaliadorContraste.avaliar(); | |
475 | - | |
476 | - if(avaliadorContraste.getContraste() < new Double("4.5")){ | |
477 | - occurrences.add(buildOccurrence("4.1", false, element.getStartTag().toString(), element,"2")); | |
478 | - | |
479 | - } | |
480 | - achou_foreground = false; | |
481 | - achou_background = false; | |
482 | - }//Caso não ache o foreground, usará o padrão encontrado no body | |
483 | - | |
484 | - | |
485 | - //Caso seja encontrado um "foreground" e não "background" e vice-versa, será adicionado um aviso para que seja avaliado manualmente | |
486 | - /*if((achou_foreground== true && achou_background == false) || (achou_foreground== false && achou_background == true)){ | |
487 | - | |
488 | - occurrences.add(buildOccurrence("4.1", false, element.toString(), element,"2")); | |
489 | - | |
490 | - achou_foreground = false; | |
491 | - achou_background = false; | |
492 | - | |
493 | - }*/ | |
494 | - } | |
495 | - } | |
496 | - return occurrences; | |
497 | - } | |
498 | - | |
499 | - | |
500 | - private String getElementoCss(String css, String procurarPor){ | |
501 | - | |
502 | - String regex = "\\W" + procurarPor + "\\{.*\\}"; | |
503 | - String retorno = ""; | |
504 | - | |
505 | - try { | |
506 | - Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE); | |
507 | - | |
508 | - Matcher matcher = pattern.matcher(css); | |
509 | - if(matcher.find()){ | |
510 | - retorno = matcher.group(); | |
511 | - } else { | |
512 | - | |
513 | - regex = procurarPor + ",.*\\}"; | |
514 | - | |
515 | - pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE); | |
516 | - | |
517 | - matcher = pattern.matcher(css); | |
518 | - | |
519 | - if(matcher.find()){ | |
520 | - retorno = matcher.group(); | |
521 | - } else { | |
522 | - retorno = ""; | |
523 | - } | |
524 | - } | |
525 | - } catch (Exception e) { | |
526 | - retorno = ""; | |
527 | - } | |
528 | - | |
529 | - | |
530 | - return retorno; | |
531 | - } | |
532 | - | |
533 | - private Color getColor(String css, String pattern){ | |
534 | - | |
535 | - Color color = null; | |
536 | - | |
537 | - Pattern value = Pattern.compile("\\s" + pattern); | |
538 | - String cssModificado = css.replace(";", ";\n").replace("}", "").replace("!important", ""); | |
539 | - Matcher match = value.matcher(cssModificado); | |
540 | - while (match.find()){ | |
541 | - String cor = match.group(1); | |
542 | - | |
543 | - try { | |
544 | - if(!isTextCor(match.group(1)) && !cor.contains("url")){ | |
545 | - | |
546 | - if(cor.contains("rgb")){ | |
547 | - //System.out.println("RGB: "+match.group(1)); | |
548 | - | |
549 | - String str = cor.replaceAll("[^-?0-9]+", " "); | |
550 | - color = new Color( | |
551 | - Integer.parseInt(str.trim().split(" ")[0]), | |
552 | - Integer.parseInt(str.trim().split(" ")[1]), | |
553 | - Integer.parseInt(str.trim().split(" ")[2])); | |
554 | - break; | |
555 | - }else{ | |
556 | - //System.out.println("EXADECIMAL: "+match.group(1)); | |
557 | - if(css.replaceAll(" ", "").contains(":#")) | |
558 | - { | |
559 | - color = new Color(Integer.parseInt(match.group(1).trim().replaceFirst("#",""), 16)); | |
560 | - break; | |
561 | - } | |
562 | - } | |
563 | - } | |
564 | - } catch (Exception e) { | |
565 | - e.printStackTrace(); | |
566 | - return null; | |
567 | - } | |
568 | - } | |
569 | - | |
570 | - return color; | |
571 | - } | |
572 | - | |
573 | - | |
574 | - private boolean isTextCor(String cor){ | |
575 | - return cor.matches("[a-zA-Z\\s]+"); | |
576 | - } | |
577 | - | |
578 | - private boolean isContrastInferior(String style){ | |
579 | - | |
580 | - AvaliadorContraste avaliadorContraste = new AvaliadorContraste(); | |
581 | - | |
582 | - /*int foreground = Integer.parseInt("000000", 16); | |
583 | - int background = Integer.parseInt("ffffff", 16);*/ | |
584 | - boolean isProces = true; | |
585 | - | |
586 | - Color foregroundColor = new Color(Integer.parseInt("000000", 16)); | |
587 | - Color backgroundColor = new Color(Integer.parseInt("ffffff", 16)); | |
588 | - | |
589 | - | |
590 | - //foreground | |
591 | - Pattern colorValue = Pattern.compile("color:(.*?)\\;"); | |
592 | - | |
593 | - Matcher match = colorValue.matcher(style); | |
594 | - | |
595 | - if(match.find()){ | |
596 | - | |
597 | - String cor = match.group(1); | |
598 | - | |
599 | - //System.out.println("foreground: "+cor); | |
600 | - if(!isTextCor(match.group(1)) && !cor.contains("url")){ | |
601 | - | |
602 | - if(cor.contains("rgb")){ | |
603 | - String str = cor.replaceAll("[^-?0-9]+", " "); | |
604 | - | |
605 | - foregroundColor = new Color( | |
606 | - Integer.parseInt(str.trim().split(" ")[0]), | |
607 | - Integer.parseInt(str.trim().split(" ")[1]), | |
608 | - Integer.parseInt(str.trim().split(" ")[2])); | |
609 | - | |
610 | - }else if(isTextCor(cor.trim().replaceFirst("^#","")))foregroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16)); | |
611 | - | |
612 | - }else isProces = false; | |
613 | - } | |
614 | - | |
615 | - //background | |
616 | - Pattern backgroundValue = Pattern.compile("background:(.*?)\\;"); | |
617 | - match = backgroundValue.matcher(style); | |
618 | - if(match.find()){ | |
619 | - String cor = match.group(1); | |
620 | - | |
621 | - //System.out.println("background: "+cor); | |
622 | - if(!isTextCor(cor) && !cor.contains("url")){ | |
623 | - | |
624 | - if(cor.contains("rgb")){ | |
625 | - String str = cor.replaceAll("[^-?0-9]+", " "); | |
626 | - | |
627 | - backgroundColor = new Color( | |
628 | - Integer.parseInt(str.trim().split(" ")[0]), | |
629 | - Integer.parseInt(str.trim().split(" ")[1]), | |
630 | - Integer.parseInt(str.trim().split(" ")[2])); | |
631 | - }else if(isTextCor(cor.trim().replaceFirst("^#",""))) backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16)); | |
632 | - | |
633 | - }else isProces = false; | |
634 | - | |
635 | - } | |
636 | - | |
637 | - //background | |
638 | - Pattern backgroundColorValue = Pattern.compile("background-color:(.*?)\\;"); | |
639 | - match = backgroundColorValue.matcher(style); | |
640 | - | |
641 | - if(match.find()){ | |
642 | - String cor = match.group(1); | |
643 | - | |
644 | - //System.out.println(cor); | |
645 | - if(!isTextCor(cor)){ | |
646 | - | |
647 | - if(cor.contains("rgb") && !cor.contains("url")){ | |
648 | - String str = cor.replaceAll("[^-?0-9]+", " "); | |
649 | - | |
650 | - backgroundColor = new Color( | |
651 | - Integer.parseInt(str.trim().split(" ")[0]), | |
652 | - Integer.parseInt(str.trim().split(" ")[1]), | |
653 | - Integer.parseInt(str.trim().split(" ")[2])); | |
654 | - }else if(isTextCor(cor.trim().replaceFirst("^#",""))) backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16)); | |
655 | - | |
656 | - }else isProces = false; | |
657 | - } | |
658 | - | |
659 | - if(isProces){ | |
660 | - avaliadorContraste.setCores(foregroundColor,backgroundColor); | |
661 | - avaliadorContraste.avaliar(); | |
662 | - | |
663 | - if(avaliadorContraste.getContraste() < new Double("4.5")){ | |
664 | - System.out.println("foreground: "+foregroundColor.getRGB()+" -- "+"background"+backgroundColor.getRGB()+" Resultado: "+avaliadorContraste.getContraste()); | |
665 | - return true; | |
666 | - } | |
667 | - | |
668 | - } | |
669 | - | |
670 | - return false; | |
671 | - } | |
672 | - | |
673 | - private List<Occurrence> checkRecommendation30() { | |
674 | - List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
675 | - occurrences.add(new Occurrence("4.2", false, getDocument().getFirstElement().toString(),OccurrenceClassification.PRESENTATION_DESIGN)); | |
676 | - return occurrences; | |
677 | - } | |
678 | - | |
679 | - private List<Occurrence> checkRecommendation31() { | |
680 | - List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
681 | - occurrences.add(new Occurrence("4.3", false, getDocument().getFirstElement().toString(),OccurrenceClassification.PRESENTATION_DESIGN)); | |
682 | - | |
683 | - return occurrences; | |
684 | - } | |
685 | - | |
686 | - /*private String urlSemArquivoNoFinal(String url) | |
687 | - { | |
688 | - if(url != null) | |
689 | - { | |
690 | - String urlSemArquivo = ""; | |
691 | - boolean encontrouPonto = false; | |
692 | - | |
693 | - | |
694 | - for(int i = url.length()-1; i >= 0; i-- ) | |
695 | - { | |
696 | - | |
697 | - urlSemArquivo = url.substring(i-1,i); | |
698 | - | |
699 | - if(urlSemArquivo.equalsIgnoreCase(".")) | |
700 | - { | |
701 | - encontrouPonto = true; | |
702 | - | |
703 | - }else if(encontrouPonto && urlSemArquivo.equalsIgnoreCase("/")) | |
704 | - { | |
705 | - url = url.substring(0,i); | |
706 | - break; | |
707 | - } | |
708 | - else if(!encontrouPonto && urlSemArquivo.equalsIgnoreCase("/")) | |
709 | - { | |
710 | - break; | |
711 | - } | |
712 | - } | |
713 | - } | |
714 | - return url; | |
715 | - }*/ | |
716 | - private List<Occurrence> checkRecommendation32() { | |
717 | - List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
718 | - | |
719 | - UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); | |
720 | - | |
721 | - String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal.urlSemArquivoNoFinal(getUrl()); | |
722 | - | |
723 | - | |
724 | - //Caso seja encontrado o "a:focus" e "a:hover" n�o continua a verifica��o nos demais for's, bem como, n�o | |
725 | - //adiciona a ocorr�ncia | |
726 | - boolean focoEncontrado = false; | |
727 | - boolean passivelDeAvaliacao = false; | |
728 | - | |
729 | - for (Element style : getDocument().getAllElements("style")) { | |
730 | - if(style.toString().contains("a:focus") && style.toString().contains("a:hover")){ | |
731 | - //occurrences.add(buildOccurrence("4.4", true, style.toString(), style, "1")); | |
732 | - focoEncontrado = true; | |
733 | - break; | |
734 | - } | |
735 | - } | |
736 | - if(!focoEncontrado) | |
737 | - { | |
738 | - for (Element style : getDocument().getAllElements("a")) { | |
739 | - Attribute attribute = style.getAttributes().get("style"); | |
740 | - if(attribute != null) | |
741 | - if(attribute.toString().contains("a:focus") && attribute.toString().contains("a:hover")){ | |
742 | - //occurrences.add(buildOccurrence("4.4", true, style.toString(), style, "1")); | |
743 | - focoEncontrado = true; | |
744 | - break; | |
745 | - } | |
746 | - } | |
747 | - | |
748 | - } | |
749 | - if(!focoEncontrado) | |
750 | - { | |
751 | - String href = null; | |
752 | - WebAgent webAgent; | |
753 | - String content = ""; | |
754 | - | |
755 | - boolean avalia = false; | |
756 | - | |
757 | - for(Element link : getDocument().getAllElements("link")) { | |
758 | - href = link.getAttributeValue("href"); | |
759 | - if(href.startsWith("www")) | |
760 | - { | |
761 | - href= "http://" + href; | |
762 | - } | |
763 | - | |
764 | - avalia = getUrl()!=null || href.startsWith("http"); | |
765 | - | |
766 | - if(href.contains(".css") && avalia) { | |
767 | - | |
768 | - if(!href.startsWith("http")) href = urlSemArquiNoFinal + href; | |
769 | - href = href.replace(" ", "%20").replace("//", "/").replace(":/", "://"); | |
770 | - webAgent = WebAgent.from(href).withGetRequest().execute(); | |
771 | - | |
772 | - if(webAgent.getStatusCode() != 404) | |
773 | - { | |
774 | - content = webAgent.getContent(); | |
775 | - passivelDeAvaliacao = true; | |
776 | - if (content!=null && content.contains("a:hover") && content.contains("a:focus")){ | |
777 | - | |
778 | - focoEncontrado = true; | |
779 | - break; | |
780 | - | |
781 | - } | |
782 | - | |
783 | - } | |
784 | - | |
785 | - } | |
786 | - | |
787 | - } | |
788 | - } | |
789 | - | |
790 | - if(!focoEncontrado && passivelDeAvaliacao) | |
791 | - { | |
792 | - occurrences.add(buildOccurrence("4.4", true, getDocument().getFirstStartTag().toString(), getDocument().getFirstElement(), "1")); | |
793 | - } | |
794 | - Collections.sort(occurrences); | |
795 | - | |
796 | - return occurrences; | |
797 | - } | |
798 | - | |
799 | - private Occurrence buildOccurrence(String code, boolean error, String tag, Element element, String criterio) { | |
800 | - return super.buildOccurrence(code, error, tag, element, OccurrenceClassification.PRESENTATION_DESIGN,criterio); | |
801 | - } | |
802 | - | |
803 | - | |
804 | - private Occurrence buildOccurrence(String code, boolean error, String tag, Element element) { | |
805 | - return super.buildOccurrence(code, error, tag, element, OccurrenceClassification.PRESENTATION_DESIGN); | |
806 | - } | |
807 | - | |
808 | - public OccurrenceClassification type () { return OccurrenceClassification.PRESENTATION_DESIGN;} | |
35 | + private PresentationEvaluation(Source document, String url) { | |
36 | + super(document, url); | |
37 | + } | |
38 | + | |
39 | + public static class PresentationEvaluationBuilder extends EvaluationBuilder { | |
40 | + | |
41 | + @Override | |
42 | + protected PresentationEvaluation with(Source document) { | |
43 | + return new PresentationEvaluation(document); | |
44 | + } | |
45 | + | |
46 | + @Override | |
47 | + protected PresentationEvaluation with(Source document, String url) { | |
48 | + return new PresentationEvaluation(document, url); | |
49 | + } | |
50 | + | |
51 | + public SpecificRecommendation recommendation29() { | |
52 | + return new EvaluationRecommendation29(); | |
53 | + } | |
54 | + | |
55 | + public SpecificRecommendation recommendation30() { | |
56 | + return new EvaluationRecommendation30(); | |
57 | + } | |
58 | + | |
59 | + public SpecificRecommendation recommendation31() { | |
60 | + return new EvaluationRecommendation31(); | |
61 | + } | |
62 | + | |
63 | + public SpecificRecommendation recommendation32() { | |
64 | + return new EvaluationRecommendation32(); | |
65 | + } | |
66 | + } | |
67 | + | |
68 | + protected static class EvaluationRecommendation29 extends | |
69 | + PresentationRecommendation { | |
70 | + protected List<Occurrence> check() { | |
71 | + return getEvaluation().checkRecommendation29(); | |
72 | + } | |
73 | + } | |
74 | + | |
75 | + protected static class EvaluationRecommendation30 extends | |
76 | + PresentationRecommendation { | |
77 | + protected List<Occurrence> check() { | |
78 | + return getEvaluation().checkRecommendation30(); | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + protected static class EvaluationRecommendation31 extends | |
83 | + PresentationRecommendation { | |
84 | + protected List<Occurrence> check() { | |
85 | + return getEvaluation().checkRecommendation31(); | |
86 | + } | |
87 | + } | |
88 | + | |
89 | + protected static class EvaluationRecommendation32 extends | |
90 | + PresentationRecommendation { | |
91 | + protected List<Occurrence> check() { | |
92 | + return getEvaluation().checkRecommendation32(); | |
93 | + } | |
94 | + } | |
95 | + | |
96 | + public List<Occurrence> check() { | |
97 | + getOccurrences().clear(); | |
98 | + getOccurrences().addAll(checkRecommendation29()); // comentado por | |
99 | + // Gibran | |
100 | + // getOccurrences().addAll(checkRecommendation30()); comentado por | |
101 | + // Gibran | |
102 | + // getOccurrences().addAll(checkRecommendation31()); comentado por | |
103 | + // Gibran | |
104 | + getOccurrences().addAll(checkRecommendation32()); | |
105 | + | |
106 | + return getOccurrences(); | |
107 | + } | |
108 | + | |
109 | + private List<Occurrence> checkRecommendation29() { | |
110 | + List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
111 | + | |
112 | + UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); | |
113 | + | |
114 | + String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal | |
115 | + .urlSemArquivoNoFinal(getUrl()); | |
116 | + // verificar css externo | |
117 | + String href = null; | |
118 | + boolean avalia = false; | |
119 | + | |
120 | + // Adiciona todo css externo em uma lista | |
121 | + List<String> lsCssExterno = new ArrayList<String>(); | |
122 | + | |
123 | + for (Element link2 : getDocument().getAllElements("link")) { | |
124 | + href = link2.getAttributeValue("href"); | |
125 | + if (href != null) { | |
126 | + if (href.startsWith("www")) | |
127 | + href = "http://" + href; | |
128 | + | |
129 | + if (href.equalsIgnoreCase("http://s.glbimg.com/jo/g1/static/live/COMPR/css/f0/2c8c3d2881f0.css")) { | |
130 | + System.out.println(href); | |
131 | + } | |
132 | + avalia = getUrl() != null || href.startsWith("http"); | |
133 | + | |
134 | + if (href.contains(".css") && avalia) { | |
135 | + | |
136 | + if (!href.startsWith("http")) | |
137 | + href = urlSemArquiNoFinal + "/" + href; | |
138 | + | |
139 | + lsCssExterno.add(WebAgent.from(href.replace(" ", "%20")) | |
140 | + .withGetRequest().execute().getContent()); | |
141 | + | |
142 | + } | |
143 | + } | |
144 | + } | |
145 | + // Fim Adiciona todo css externo em uma lista | |
146 | + | |
147 | + AvaliadorContraste avaliadorContraste = new AvaliadorContraste(); | |
148 | + | |
149 | + Color corAvaliar = null; | |
150 | + Color foreground = null; | |
151 | + Color background = null; | |
152 | + Color backgroundDoBody = null; | |
153 | + boolean achou_foreground = false; | |
154 | + boolean achou_background = false; | |
155 | + String styleInline = ""; | |
156 | + String procurarPorId = ""; | |
157 | + String procurarPorClasse = ""; | |
158 | + String cssParaBuscarCor = ""; | |
159 | + | |
160 | + List<Element> lsCssInterno = new ArrayList<Element>(); | |
161 | + | |
162 | + // pega css interno caso tenha | |
163 | + for (Element head : getDocument().getAllElements("head")) { | |
164 | + for (Element estilo : head.getAllElements("style")) { | |
165 | + lsCssInterno.add(estilo); | |
166 | + } | |
167 | + } | |
168 | + | |
169 | + // Pega todos elementos do "body" | |
170 | + for (Element body : getDocument().getAllElements("body")) { | |
171 | + for (Element element : body.getAllElements()) { | |
172 | + | |
173 | + // verificar se o "body" possui o atributo "bgcolor" | |
174 | + if (element.getStartTag().getName().equalsIgnoreCase("body")) { | |
175 | + if (element.toString().contains("bgcolor")) { | |
176 | + try { | |
177 | + corAvaliar = new Color(Integer.parseInt(element | |
178 | + .getStartTag().getAttributeValue("bgcolor") | |
179 | + .trim().replaceFirst("#", ""), 16)); | |
180 | + if (corAvaliar != null) { | |
181 | + background = corAvaliar; | |
182 | + } | |
183 | + } catch (Exception e) { | |
184 | + e.printStackTrace(); | |
185 | + } | |
186 | + } | |
187 | + } | |
188 | + | |
189 | + // Primeiro verificar se tem css in-line | |
190 | + styleInline = element.getStartTag().getAttributeValue("style"); | |
191 | + if (styleInline != null) { | |
192 | + | |
193 | + // pega o valor da cor, mesmo que no final não seja | |
194 | + // atribuido o ";" | |
195 | + corAvaliar = getColor(styleInline, "color:(.*?)\\Z"); | |
196 | + if (corAvaliar != null) { | |
197 | + foreground = corAvaliar; | |
198 | + } | |
199 | + | |
200 | + corAvaliar = getColor(styleInline, "color:(.*?)\\;"); | |
201 | + if (corAvaliar != null) { | |
202 | + foreground = corAvaliar; | |
203 | + } | |
204 | + | |
205 | + // pega o valor da cor, mesmo que no final não seja | |
206 | + // atribuido o ";" | |
207 | + corAvaliar = getColor(styleInline, "background:(.*?)\\Z"); | |
208 | + if (corAvaliar != null) { | |
209 | + background = corAvaliar; | |
210 | + } | |
211 | + | |
212 | + corAvaliar = getColor(styleInline, "background:(.*?)\\;"); | |
213 | + if (corAvaliar != null) { | |
214 | + background = corAvaliar; | |
215 | + } | |
216 | + // pega o valor da cor, mesmo que no final não seja | |
217 | + // atribuido o ";" | |
218 | + corAvaliar = getColor(styleInline, | |
219 | + "background-color:(.*?)\\Z"); | |
220 | + if (corAvaliar != null) { | |
221 | + background = corAvaliar; | |
222 | + } | |
223 | + | |
224 | + corAvaliar = getColor(styleInline, | |
225 | + "background-color:(.*?)\\;"); | |
226 | + if (corAvaliar != null) { | |
227 | + background = corAvaliar; | |
228 | + } | |
229 | + | |
230 | + if (foreground != null) { | |
231 | + achou_foreground = true; | |
232 | + } | |
233 | + | |
234 | + if (background != null) { | |
235 | + achou_background = true; | |
236 | + | |
237 | + if (element.getStartTag().getName() | |
238 | + .equalsIgnoreCase("body")) { | |
239 | + backgroundDoBody = background; | |
240 | + } | |
241 | + } | |
242 | + | |
243 | + } else { | |
244 | + procurarPorId = "#" | |
245 | + + element.getStartTag().getAttributeValue("id"); | |
246 | + procurarPorClasse = "." | |
247 | + + element.getStartTag().getAttributeValue("class"); | |
248 | + | |
249 | + // verifica css interno | |
250 | + | |
251 | + // Verifica se tem #id | |
252 | + for (Element style : lsCssInterno) { | |
253 | + | |
254 | + // String pattern = ""; | |
255 | + | |
256 | + // Coloca todo css em apenas uma linha, depois separa | |
257 | + // por blocos | |
258 | + String texto = style.toString().replace("\n", ""); | |
259 | + texto = texto.replace(">", ">\n").replace("*/", "\n") | |
260 | + .replace(" {", "{").replace("}", " }\n") | |
261 | + .replace(" :", ":"); | |
262 | + cssParaBuscarCor = ""; | |
263 | + | |
264 | + // vevifica 1º pelo "id" | |
265 | + if (!procurarPorId.equalsIgnoreCase("#null")) { | |
266 | + cssParaBuscarCor = getElementoCss(texto, | |
267 | + procurarPorId); | |
268 | + } | |
269 | + | |
270 | + // Se não encontrar pelo "id" verifica pela classe e | |
271 | + // elemento | |
272 | + if (cssParaBuscarCor.isEmpty() | |
273 | + && !procurarPorClasse.equalsIgnoreCase(".null")) { | |
274 | + cssParaBuscarCor = getElementoCss(texto, | |
275 | + procurarPorClasse | |
276 | + + element.getStartTag().getName()); | |
277 | + } | |
278 | + | |
279 | + // Se não encontrar pela classe e elemento, verifica | |
280 | + // pela classe | |
281 | + if (cssParaBuscarCor.isEmpty() | |
282 | + && !procurarPorClasse.equalsIgnoreCase(".null")) { | |
283 | + cssParaBuscarCor = getElementoCss(texto, | |
284 | + procurarPorClasse); | |
285 | + } | |
286 | + // Se não encontrar pela classe, verifica pelo elemento | |
287 | + if (cssParaBuscarCor.isEmpty()) { | |
288 | + cssParaBuscarCor = getElementoCss(texto, element | |
289 | + .getStartTag().getName()); | |
290 | + } | |
291 | + | |
292 | + if (!cssParaBuscarCor.isEmpty()) { | |
293 | + if (cssParaBuscarCor.contains("color:")) { | |
294 | + corAvaliar = getColor(cssParaBuscarCor, | |
295 | + "color:(.*?)\\Z"); | |
296 | + if (corAvaliar != null) { | |
297 | + foreground = corAvaliar; | |
298 | + | |
299 | + } | |
300 | + | |
301 | + corAvaliar = getColor(cssParaBuscarCor, | |
302 | + "color:(.*?)\\;"); | |
303 | + if (corAvaliar != null) { | |
304 | + foreground = corAvaliar; | |
305 | + } | |
306 | + } | |
307 | + | |
308 | + if (cssParaBuscarCor.contains("background:") | |
309 | + || cssParaBuscarCor | |
310 | + .contains("background-color:")) { | |
311 | + // pega o valor da cor, mesmo que no final não | |
312 | + // seja atribuido o ";" | |
313 | + corAvaliar = getColor(cssParaBuscarCor, | |
314 | + "background:(.*?)\\Z"); | |
315 | + if (corAvaliar != null) { | |
316 | + background = corAvaliar; | |
317 | + } | |
318 | + | |
319 | + corAvaliar = getColor(cssParaBuscarCor, | |
320 | + "background:(.*?)\\;"); | |
321 | + if (corAvaliar != null) { | |
322 | + background = corAvaliar; | |
323 | + } | |
324 | + // pega o valor da cor, mesmo que no final não | |
325 | + // seja atribuido o ";" | |
326 | + corAvaliar = getColor(cssParaBuscarCor, | |
327 | + "background-color:(.*?)\\Z"); | |
328 | + if (corAvaliar != null) { | |
329 | + background = corAvaliar; | |
330 | + } | |
331 | + | |
332 | + corAvaliar = getColor(cssParaBuscarCor, | |
333 | + "background-color:(.*?)\\;"); | |
334 | + if (corAvaliar != null) { | |
335 | + background = corAvaliar; | |
336 | + } | |
337 | + } | |
338 | + | |
339 | + if (foreground != null) { | |
340 | + achou_foreground = true; | |
341 | + } | |
342 | + | |
343 | + if (background != null) { | |
344 | + achou_background = true; | |
345 | + | |
346 | + if (element.getStartTag().getName() | |
347 | + .equalsIgnoreCase("body")) { | |
348 | + backgroundDoBody = background; | |
349 | + } | |
350 | + } | |
351 | + | |
352 | + if (achou_foreground || achou_background) { | |
353 | + break; | |
354 | + } | |
355 | + | |
356 | + } | |
357 | + | |
358 | + corAvaliar = null; | |
359 | + foreground = null; | |
360 | + background = null; | |
361 | + cssParaBuscarCor = ""; | |
362 | + | |
363 | + } | |
364 | + | |
365 | + if (!achou_foreground && !achou_background) { | |
366 | + // verifica css externo | |
367 | + | |
368 | + // Verifica se tem #id | |
369 | + for (String style : lsCssExterno) { | |
370 | + | |
371 | + if (style != null) { | |
372 | + if (element.getStartTag().getAttributeValue( | |
373 | + "id") != null | |
374 | + || element.getStartTag() | |
375 | + .getAttributeValue("class") != null) { | |
376 | + // Coloca todo css em apenas uma linha, | |
377 | + // depois separa por blocos | |
378 | + String texto = style.toString().replace( | |
379 | + "\n", ""); | |
380 | + texto = texto.replace(">", ">\n") | |
381 | + .replace("*/", "\n") | |
382 | + .replace(" {", "{") | |
383 | + .replace("}", " }\n") | |
384 | + .replace(" :", ":"); | |
385 | + cssParaBuscarCor = ""; | |
386 | + | |
387 | + // vevifica 1º pelo "id" | |
388 | + if (!procurarPorId | |
389 | + .equalsIgnoreCase("#null")) { | |
390 | + cssParaBuscarCor = getElementoCss( | |
391 | + texto, procurarPorId); | |
392 | + } | |
393 | + | |
394 | + // Se não encontrar pelo "id" verifica pela | |
395 | + // classe e elemento | |
396 | + if (cssParaBuscarCor.isEmpty() | |
397 | + && !procurarPorClasse | |
398 | + .equalsIgnoreCase(".null")) { | |
399 | + cssParaBuscarCor = getElementoCss( | |
400 | + texto, procurarPorClasse | |
401 | + + element.getStartTag() | |
402 | + .getName()); | |
403 | + } | |
404 | + | |
405 | + // Se não encontrar pela classe e elemento, | |
406 | + // verifica pela classe | |
407 | + if (cssParaBuscarCor.isEmpty() | |
408 | + && !procurarPorClasse | |
409 | + .equalsIgnoreCase(".null")) { | |
410 | + cssParaBuscarCor = getElementoCss( | |
411 | + texto, procurarPorClasse); | |
412 | + } | |
413 | + // Se não encontrar pela classe, verifica | |
414 | + // pelo elemento | |
415 | + if (cssParaBuscarCor.isEmpty()) { | |
416 | + cssParaBuscarCor = getElementoCss( | |
417 | + texto, element.getStartTag() | |
418 | + .getName()); | |
419 | + } | |
420 | + | |
421 | + if (!cssParaBuscarCor.isEmpty()) { | |
422 | + if (cssParaBuscarCor.contains("color:")) { | |
423 | + corAvaliar = getColor( | |
424 | + cssParaBuscarCor, | |
425 | + "color:(.*?)\\Z"); | |
426 | + if (corAvaliar != null) { | |
427 | + foreground = corAvaliar; | |
428 | + | |
429 | + } | |
430 | + | |
431 | + corAvaliar = getColor( | |
432 | + cssParaBuscarCor, | |
433 | + "color:(.*?)\\;"); | |
434 | + if (corAvaliar != null) { | |
435 | + foreground = corAvaliar; | |
436 | + } | |
437 | + | |
438 | + } | |
439 | + | |
440 | + if (cssParaBuscarCor | |
441 | + .contains("background:") | |
442 | + || cssParaBuscarCor | |
443 | + .contains("background-color:")) { | |
444 | + // pega o valor da cor, mesmo que no | |
445 | + // final não seja atribuido o ";" | |
446 | + corAvaliar = getColor( | |
447 | + cssParaBuscarCor, | |
448 | + "background:(.*?)\\Z"); | |
449 | + if (corAvaliar != null) { | |
450 | + background = corAvaliar; | |
451 | + } | |
452 | + | |
453 | + corAvaliar = getColor( | |
454 | + cssParaBuscarCor, | |
455 | + "background:(.*?)\\;"); | |
456 | + if (corAvaliar != null) { | |
457 | + background = corAvaliar; | |
458 | + } | |
459 | + // pega o valor da cor, mesmo que no | |
460 | + // final não seja atribuido o ";" | |
461 | + corAvaliar = getColor( | |
462 | + cssParaBuscarCor, | |
463 | + "background-color:(.*?)\\Z"); | |
464 | + if (corAvaliar != null) { | |
465 | + background = corAvaliar; | |
466 | + } | |
467 | + | |
468 | + corAvaliar = getColor( | |
469 | + cssParaBuscarCor, | |
470 | + "background-color:(.*?)\\;"); | |
471 | + if (corAvaliar != null) { | |
472 | + background = corAvaliar; | |
473 | + } | |
474 | + } | |
475 | + | |
476 | + if (foreground != null) { | |
477 | + achou_foreground = true; | |
478 | + } | |
479 | + | |
480 | + if (background != null) { | |
481 | + achou_background = true; | |
482 | + | |
483 | + if (element.getStartTag().getName() | |
484 | + .equalsIgnoreCase("body")) { | |
485 | + backgroundDoBody = background; | |
486 | + } | |
487 | + } | |
488 | + | |
489 | + if (achou_foreground | |
490 | + || achou_background) { | |
491 | + break; | |
492 | + } | |
493 | + | |
494 | + } | |
495 | + | |
496 | + } | |
497 | + } | |
498 | + | |
499 | + corAvaliar = null; | |
500 | + foreground = null; | |
501 | + background = null; | |
502 | + cssParaBuscarCor = ""; | |
503 | + | |
504 | + } | |
505 | + } | |
506 | + | |
507 | + } | |
508 | + procurarPorId = ""; | |
509 | + procurarPorClasse = ""; | |
510 | + | |
511 | + if (achou_foreground == true && achou_background == true) { | |
512 | + | |
513 | + avaliadorContraste.setCores(foreground, background); | |
514 | + avaliadorContraste.avaliar(); | |
515 | + | |
516 | + if (avaliadorContraste.getContraste() < new Double("4.5")) { | |
517 | + occurrences.add(buildOccurrence("4.1", false, element | |
518 | + .getStartTag().toString(), element, "2")); | |
519 | + | |
520 | + } | |
521 | + achou_foreground = false; | |
522 | + achou_background = false; | |
523 | + | |
524 | + }// Caso não ache o background, usará o padrão encontrado no | |
525 | + // body | |
526 | + else if (achou_foreground == true && achou_background == false | |
527 | + && backgroundDoBody != null) { | |
528 | + avaliadorContraste.setCores(foreground, backgroundDoBody); | |
529 | + avaliadorContraste.avaliar(); | |
530 | + | |
531 | + if (avaliadorContraste.getContraste() < new Double("4.5")) { | |
532 | + occurrences.add(buildOccurrence("4.1", false, element | |
533 | + .getStartTag().toString(), element, "2")); | |
534 | + | |
535 | + } | |
536 | + achou_foreground = false; | |
537 | + achou_background = false; | |
538 | + }// Caso não ache o foreground, usará o padrão encontrado no | |
539 | + // body | |
540 | + | |
541 | + // Caso seja encontrado um "foreground" e não "background" e | |
542 | + // vice-versa, será adicionado um aviso para que seja avaliado | |
543 | + // manualmente | |
544 | + /* | |
545 | + * if((achou_foreground== true && achou_background == false) || | |
546 | + * (achou_foreground== false && achou_background == true)){ | |
547 | + * | |
548 | + * occurrences.add(buildOccurrence("4.1", false, | |
549 | + * element.toString(), element,"2")); | |
550 | + * | |
551 | + * achou_foreground = false; achou_background = false; | |
552 | + * | |
553 | + * } | |
554 | + */ | |
555 | + } | |
556 | + } | |
557 | + return occurrences; | |
558 | + } | |
559 | + | |
560 | + private String getElementoCss(String css, String procurarPor) { | |
561 | + | |
562 | + String regex = "\\W" + procurarPor + "\\{.*\\}"; | |
563 | + String retorno = ""; | |
564 | + | |
565 | + try { | |
566 | + Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); | |
567 | + | |
568 | + Matcher matcher = pattern.matcher(css); | |
569 | + if (matcher.find()) { | |
570 | + retorno = matcher.group(); | |
571 | + } else { | |
572 | + | |
573 | + regex = procurarPor + ",.*\\}"; | |
574 | + | |
575 | + pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); | |
576 | + | |
577 | + matcher = pattern.matcher(css); | |
578 | + | |
579 | + if (matcher.find()) { | |
580 | + retorno = matcher.group(); | |
581 | + } else { | |
582 | + retorno = ""; | |
583 | + } | |
584 | + } | |
585 | + } catch (Exception e) { | |
586 | + retorno = ""; | |
587 | + } | |
588 | + | |
589 | + return retorno; | |
590 | + } | |
591 | + | |
592 | + private Color getColor(String css, String pattern) { | |
593 | + | |
594 | + Color color = null; | |
595 | + | |
596 | + Pattern value = Pattern.compile("\\s" + pattern); | |
597 | + String cssModificado = css.replace(";", ";\n").replace("}", "") | |
598 | + .replace("!important", ""); | |
599 | + Matcher match = value.matcher(cssModificado); | |
600 | + while (match.find()) { | |
601 | + String cor = match.group(1); | |
602 | + | |
603 | + try { | |
604 | + if (!isTextCor(match.group(1)) && !cor.contains("url")) { | |
605 | + | |
606 | + if (cor.contains("rgb")) { | |
607 | + // System.out.println("RGB: "+match.group(1)); | |
608 | + | |
609 | + String str = cor.replaceAll("[^-?0-9]+", " "); | |
610 | + color = new Color(Integer.parseInt(str.trim() | |
611 | + .split(" ")[0]), Integer.parseInt(str.trim() | |
612 | + .split(" ")[1]), Integer.parseInt(str.trim() | |
613 | + .split(" ")[2])); | |
614 | + break; | |
615 | + } else { | |
616 | + // System.out.println("EXADECIMAL: "+match.group(1)); | |
617 | + if (css.replaceAll(" ", "").contains(":#")) { | |
618 | + color = new Color(Integer.parseInt(match.group(1) | |
619 | + .trim().replaceFirst("#", ""), 16)); | |
620 | + break; | |
621 | + } | |
622 | + } | |
623 | + } | |
624 | + } catch (Exception e) { | |
625 | + e.printStackTrace(); | |
626 | + return null; | |
627 | + } | |
628 | + } | |
629 | + | |
630 | + return color; | |
631 | + } | |
632 | + | |
633 | + private boolean isTextCor(String cor) { | |
634 | + return cor.matches("[a-zA-Z\\s]+"); | |
635 | + } | |
636 | + | |
637 | + private boolean isContrastInferior(String style) { | |
638 | + | |
639 | + AvaliadorContraste avaliadorContraste = new AvaliadorContraste(); | |
640 | + | |
641 | + /* | |
642 | + * int foreground = Integer.parseInt("000000", 16); int background = | |
643 | + * Integer.parseInt("ffffff", 16); | |
644 | + */ | |
645 | + boolean isProces = true; | |
646 | + | |
647 | + Color foregroundColor = new Color(Integer.parseInt("000000", 16)); | |
648 | + Color backgroundColor = new Color(Integer.parseInt("ffffff", 16)); | |
649 | + | |
650 | + // foreground | |
651 | + Pattern colorValue = Pattern.compile("color:(.*?)\\;"); | |
652 | + | |
653 | + Matcher match = colorValue.matcher(style); | |
654 | + | |
655 | + if (match.find()) { | |
656 | + | |
657 | + String cor = match.group(1); | |
658 | + | |
659 | + // System.out.println("foreground: "+cor); | |
660 | + if (!isTextCor(match.group(1)) && !cor.contains("url")) { | |
661 | + | |
662 | + if (cor.contains("rgb")) { | |
663 | + String str = cor.replaceAll("[^-?0-9]+", " "); | |
664 | + | |
665 | + foregroundColor = new Color(Integer.parseInt(str.trim() | |
666 | + .split(" ")[0]), Integer.parseInt(str.trim().split( | |
667 | + " ")[1]), | |
668 | + Integer.parseInt(str.trim().split(" ")[2])); | |
669 | + | |
670 | + } else if (isTextCor(cor.trim().replaceFirst("^#", ""))) | |
671 | + foregroundColor = new Color(Integer.parseInt(cor.trim() | |
672 | + .replaceFirst("^#", ""), 16)); | |
673 | + | |
674 | + } else | |
675 | + isProces = false; | |
676 | + } | |
677 | + | |
678 | + // background | |
679 | + Pattern backgroundValue = Pattern.compile("background:(.*?)\\;"); | |
680 | + match = backgroundValue.matcher(style); | |
681 | + if (match.find()) { | |
682 | + String cor = match.group(1); | |
683 | + | |
684 | + // System.out.println("background: "+cor); | |
685 | + if (!isTextCor(cor) && !cor.contains("url")) { | |
686 | + | |
687 | + if (cor.contains("rgb")) { | |
688 | + String str = cor.replaceAll("[^-?0-9]+", " "); | |
689 | + | |
690 | + backgroundColor = new Color(Integer.parseInt(str.trim() | |
691 | + .split(" ")[0]), Integer.parseInt(str.trim().split( | |
692 | + " ")[1]), | |
693 | + Integer.parseInt(str.trim().split(" ")[2])); | |
694 | + } else if (isTextCor(cor.trim().replaceFirst("^#", ""))) | |
695 | + backgroundColor = new Color(Integer.parseInt(cor.trim() | |
696 | + .replaceFirst("^#", ""), 16)); | |
697 | + | |
698 | + } else | |
699 | + isProces = false; | |
700 | + | |
701 | + } | |
702 | + | |
703 | + // background | |
704 | + Pattern backgroundColorValue = Pattern | |
705 | + .compile("background-color:(.*?)\\;"); | |
706 | + match = backgroundColorValue.matcher(style); | |
707 | + | |
708 | + if (match.find()) { | |
709 | + String cor = match.group(1); | |
710 | + | |
711 | + // System.out.println(cor); | |
712 | + if (!isTextCor(cor)) { | |
713 | + | |
714 | + if (cor.contains("rgb") && !cor.contains("url")) { | |
715 | + String str = cor.replaceAll("[^-?0-9]+", " "); | |
716 | + | |
717 | + backgroundColor = new Color(Integer.parseInt(str.trim() | |
718 | + .split(" ")[0]), Integer.parseInt(str.trim().split( | |
719 | + " ")[1]), | |
720 | + Integer.parseInt(str.trim().split(" ")[2])); | |
721 | + } else if (isTextCor(cor.trim().replaceFirst("^#", ""))) | |
722 | + backgroundColor = new Color(Integer.parseInt(cor.trim() | |
723 | + .replaceFirst("^#", ""), 16)); | |
724 | + | |
725 | + } else | |
726 | + isProces = false; | |
727 | + } | |
728 | + | |
729 | + if (isProces) { | |
730 | + avaliadorContraste.setCores(foregroundColor, backgroundColor); | |
731 | + avaliadorContraste.avaliar(); | |
732 | + | |
733 | + if (avaliadorContraste.getContraste() < new Double("4.5")) { | |
734 | + System.out.println("foreground: " + foregroundColor.getRGB() | |
735 | + + " -- " + "background" + backgroundColor.getRGB() | |
736 | + + " Resultado: " + avaliadorContraste.getContraste()); | |
737 | + return true; | |
738 | + } | |
739 | + | |
740 | + } | |
741 | + | |
742 | + return false; | |
743 | + } | |
744 | + | |
745 | + private List<Occurrence> checkRecommendation30() { | |
746 | + List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
747 | + occurrences.add(new Occurrence("4.2", false, getDocument() | |
748 | + .getFirstElement().toString(), | |
749 | + OccurrenceClassification.PRESENTATION_DESIGN)); | |
750 | + return occurrences; | |
751 | + } | |
752 | + | |
753 | + private List<Occurrence> checkRecommendation31() { | |
754 | + List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
755 | + occurrences.add(new Occurrence("4.3", false, getDocument() | |
756 | + .getFirstElement().toString(), | |
757 | + OccurrenceClassification.PRESENTATION_DESIGN)); | |
758 | + | |
759 | + return occurrences; | |
760 | + } | |
761 | + | |
762 | + /* | |
763 | + * private String urlSemArquivoNoFinal(String url) { if(url != null) { | |
764 | + * String urlSemArquivo = ""; boolean encontrouPonto = false; | |
765 | + * | |
766 | + * | |
767 | + * for(int i = url.length()-1; i >= 0; i-- ) { | |
768 | + * | |
769 | + * urlSemArquivo = url.substring(i-1,i); | |
770 | + * | |
771 | + * if(urlSemArquivo.equalsIgnoreCase(".")) { encontrouPonto = true; | |
772 | + * | |
773 | + * }else if(encontrouPonto && urlSemArquivo.equalsIgnoreCase("/")) { url = | |
774 | + * url.substring(0,i); break; } else if(!encontrouPonto && | |
775 | + * urlSemArquivo.equalsIgnoreCase("/")) { break; } } } return url; } | |
776 | + */ | |
777 | + private List<Occurrence> checkRecommendation32() { | |
778 | + List<Occurrence> occurrences = new ArrayList<Occurrence>(); | |
779 | + | |
780 | + UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); | |
781 | + | |
782 | + String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal | |
783 | + .urlSemArquivoNoFinal(getUrl()); | |
784 | + | |
785 | + // Caso seja encontrado o "a:focus" e "a:hover" n�o continua a | |
786 | + // verifica��o nos demais for's, bem como, n�o | |
787 | + // adiciona a ocorr�ncia | |
788 | + boolean focoEncontrado = false; | |
789 | + boolean passivelDeAvaliacao = false; | |
790 | + | |
791 | + for (Element style : getDocument().getAllElements("style")) { | |
792 | + if (style.toString().contains("a:focus") | |
793 | + && style.toString().contains("a:hover")) { | |
794 | + // occurrences.add(buildOccurrence("4.4", true, | |
795 | + // style.toString(), style, "1")); | |
796 | + focoEncontrado = true; | |
797 | + break; | |
798 | + } | |
799 | + } | |
800 | + if (!focoEncontrado) { | |
801 | + for (Element style : getDocument().getAllElements("a")) { | |
802 | + Attribute attribute = style.getAttributes().get("style"); | |
803 | + if (attribute != null) | |
804 | + if (attribute.toString().contains("a:focus") | |
805 | + && attribute.toString().contains("a:hover")) { | |
806 | + // occurrences.add(buildOccurrence("4.4", true, | |
807 | + // style.toString(), style, "1")); | |
808 | + focoEncontrado = true; | |
809 | + break; | |
810 | + } | |
811 | + } | |
812 | + | |
813 | + } | |
814 | + if (!focoEncontrado) { | |
815 | + String href = null; | |
816 | + WebAgent webAgent; | |
817 | + String content = ""; | |
818 | + | |
819 | + boolean avalia = false; | |
820 | + | |
821 | + for (Element link : getDocument().getAllElements("link")) { | |
822 | + href = link.getAttributeValue("href"); | |
823 | + if (href != null) { | |
824 | + if (href.startsWith("www")) { | |
825 | + href = "http://" + href; | |
826 | + } | |
827 | + | |
828 | + avalia = getUrl() != null || href.startsWith("http"); | |
829 | + | |
830 | + if (href.contains(".css") && avalia) { | |
831 | + | |
832 | + if (!href.startsWith("http")) | |
833 | + href = urlSemArquiNoFinal + href; | |
834 | + href = href.replace(" ", "%20").replace("//", "/") | |
835 | + .replace(":/", "://"); | |
836 | + webAgent = WebAgent.from(href).withGetRequest() | |
837 | + .execute(); | |
838 | + | |
839 | + if (webAgent.getStatusCode() != 404) { | |
840 | + content = webAgent.getContent(); | |
841 | + passivelDeAvaliacao = true; | |
842 | + if (content != null && content.contains("a:hover") | |
843 | + && content.contains("a:focus")) { | |
844 | + | |
845 | + focoEncontrado = true; | |
846 | + break; | |
847 | + | |
848 | + } | |
849 | + | |
850 | + } | |
851 | + | |
852 | + } | |
853 | + } | |
854 | + } | |
855 | + } | |
856 | + | |
857 | + if (!focoEncontrado && passivelDeAvaliacao) { | |
858 | + occurrences.add(buildOccurrence("4.4", true, getDocument() | |
859 | + .getFirstStartTag().toString(), getDocument() | |
860 | + .getFirstElement(), "1")); | |
861 | + } | |
862 | + Collections.sort(occurrences); | |
863 | + | |
864 | + return occurrences; | |
865 | + } | |
866 | + | |
867 | + private Occurrence buildOccurrence(String code, boolean error, String tag, | |
868 | + Element element, String criterio) { | |
869 | + return super.buildOccurrence(code, error, tag, element, | |
870 | + OccurrenceClassification.PRESENTATION_DESIGN, criterio); | |
871 | + } | |
872 | + | |
873 | + private Occurrence buildOccurrence(String code, boolean error, String tag, | |
874 | + Element element) { | |
875 | + return super.buildOccurrence(code, error, tag, element, | |
876 | + OccurrenceClassification.PRESENTATION_DESIGN); | |
877 | + } | |
878 | + | |
879 | + public OccurrenceClassification type() { | |
880 | + return OccurrenceClassification.PRESENTATION_DESIGN; | |
881 | + } | |
809 | 882 | } | ... | ... |