Commit 514071c329d8607c5f24b90b7f962a809e5672d4
1 parent
b8325d32
Exists in
master
and in
1 other branch
Correção do método para verificar a luminosidade.
Showing
1 changed file
with
63 additions
and
27 deletions
Show diff stats
src/main/java/br/com/checker/emag/core/PresentationEvaluation.java
... | ... | @@ -16,6 +16,8 @@ import javax.swing.text.html.HTML; |
16 | 16 | |
17 | 17 | import org.hamcrest.core.IsNull; |
18 | 18 | |
19 | +import sun.misc.FpUtils; | |
20 | + | |
19 | 21 | import com.oracle.webservices.internal.api.EnvelopeStyle.Style; |
20 | 22 | |
21 | 23 | import net.htmlparser.jericho.Attribute; |
... | ... | @@ -113,7 +115,8 @@ public class PresentationEvaluation extends Evaluation{ |
113 | 115 | |
114 | 116 | Color corAvaliar = null; |
115 | 117 | Color foreground = null; |
116 | - Color background = null; | |
118 | + Color background = null; | |
119 | + Color backgroundDoBody = null; | |
117 | 120 | boolean achou_foreground = false; |
118 | 121 | boolean achou_background = false; |
119 | 122 | String styleInline = ""; |
... | ... | @@ -128,20 +131,19 @@ public class PresentationEvaluation extends Evaluation{ |
128 | 131 | for (Element estilo : head.getAllElements("style")) { |
129 | 132 | lsCssInterno.add(estilo); |
130 | 133 | } |
131 | - } | |
134 | + } | |
132 | 135 | |
133 | - | |
134 | 136 | //Pega todos elementos do "body" |
135 | 137 | for (Element body : getDocument().getAllElements("body")) { |
136 | 138 | for (Element element : body.getAllElements()) { |
137 | 139 | |
138 | 140 | //verificar se o "body" possui o atributo "bgcolor" |
139 | 141 | if(element.getStartTag().getName().equalsIgnoreCase("body")) |
140 | - { | |
142 | + { | |
141 | 143 | if(element.toString().contains("bgcolor")) |
142 | 144 | { |
143 | 145 | try { |
144 | - corAvaliar = new Color(Integer.parseInt(element.getAttributeValue("bgcolor").trim().replaceFirst("#",""), 16)); | |
146 | + corAvaliar = new Color(Integer.parseInt(element.getStartTag().getAttributeValue("bgcolor").trim().replaceFirst("#",""), 16)); | |
145 | 147 | if(corAvaliar != null) |
146 | 148 | { |
147 | 149 | background = corAvaliar; |
... | ... | @@ -153,7 +155,7 @@ public class PresentationEvaluation extends Evaluation{ |
153 | 155 | } |
154 | 156 | |
155 | 157 | //Primeiro verificar se tem css in-line |
156 | - styleInline = element.getAttributeValue("style"); | |
158 | + styleInline = element.getStartTag().getAttributeValue("style"); | |
157 | 159 | if(styleInline != null){ |
158 | 160 | |
159 | 161 | |
... | ... | @@ -196,8 +198,21 @@ public class PresentationEvaluation extends Evaluation{ |
196 | 198 | background = corAvaliar; |
197 | 199 | } |
198 | 200 | |
199 | - if(foreground != null) achou_foreground = true; | |
200 | - if(background != null) achou_background = true; | |
201 | + if(foreground != null) | |
202 | + { | |
203 | + achou_foreground = true; | |
204 | + } | |
205 | + | |
206 | + if(background != null) { | |
207 | + achou_background = true; | |
208 | + | |
209 | + if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
210 | + { | |
211 | + backgroundDoBody = background; | |
212 | + } | |
213 | + } | |
214 | + | |
215 | + | |
201 | 216 | |
202 | 217 | }else |
203 | 218 | { |
... | ... | @@ -290,21 +305,25 @@ public class PresentationEvaluation extends Evaluation{ |
290 | 305 | } |
291 | 306 | |
292 | 307 | if(foreground != null) |
293 | - { | |
294 | - achou_foreground = true; | |
295 | - } | |
308 | + { | |
309 | + achou_foreground = true; | |
310 | + } | |
311 | + | |
312 | + if(background != null) { | |
313 | + achou_background = true; | |
296 | 314 | |
297 | - if(background != null) | |
315 | + if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
298 | 316 | { |
299 | - achou_background = true; | |
300 | - } | |
317 | + backgroundDoBody = background; | |
318 | + } | |
319 | + } | |
301 | 320 | |
302 | 321 | if(achou_foreground || achou_background) |
303 | 322 | { |
304 | 323 | break; |
305 | 324 | } |
306 | 325 | |
307 | - } | |
326 | + } | |
308 | 327 | |
309 | 328 | |
310 | 329 | corAvaliar = null; |
... | ... | @@ -322,7 +341,7 @@ public class PresentationEvaluation extends Evaluation{ |
322 | 341 | for (String style : lsCssExterno) { |
323 | 342 | |
324 | 343 | |
325 | - if(element.getAttributeValue("id") != null || element.getAttributeValue("class") != null) | |
344 | + if(element.getStartTag().getAttributeValue("id") != null || element.getStartTag().getAttributeValue("class") != null) | |
326 | 345 | { |
327 | 346 | //Coloca todo css em apenas uma linha, depois separa por blocos |
328 | 347 | String texto = style.toString().replace("\n",""); |
... | ... | @@ -402,14 +421,18 @@ public class PresentationEvaluation extends Evaluation{ |
402 | 421 | } |
403 | 422 | |
404 | 423 | if(foreground != null) |
405 | - { | |
406 | - achou_foreground = true; | |
407 | - } | |
424 | + { | |
425 | + achou_foreground = true; | |
426 | + } | |
427 | + | |
428 | + if(background != null) { | |
429 | + achou_background = true; | |
408 | 430 | |
409 | - if(background != null) | |
431 | + if(element.getStartTag().getName().equalsIgnoreCase("body")) | |
410 | 432 | { |
411 | - achou_background = true; | |
412 | - } | |
433 | + backgroundDoBody = background; | |
434 | + } | |
435 | + } | |
413 | 436 | |
414 | 437 | if(achou_foreground || achou_background) |
415 | 438 | { |
... | ... | @@ -441,13 +464,26 @@ public class PresentationEvaluation extends Evaluation{ |
441 | 464 | avaliadorContraste.avaliar(); |
442 | 465 | |
443 | 466 | if(avaliadorContraste.getContraste() < new Double("4.5")){ |
444 | - occurrences.add(buildOccurrence("4.1", false, element.toString(), element,"2")); | |
467 | + occurrences.add(buildOccurrence("4.1", false, element.getStartTag().toString(), element,"2")); | |
445 | 468 | |
446 | 469 | } |
447 | 470 | achou_foreground = false; |
448 | 471 | achou_background = false; |
449 | 472 | |
450 | - } | |
473 | + }//Caso não ache o background, usará o padrão encontrado no body | |
474 | + else if (achou_foreground == true && achou_background == false && backgroundDoBody != null) | |
475 | + { | |
476 | + avaliadorContraste.setCores(foreground, backgroundDoBody); | |
477 | + avaliadorContraste.avaliar(); | |
478 | + | |
479 | + if(avaliadorContraste.getContraste() < new Double("4.5")){ | |
480 | + occurrences.add(buildOccurrence("4.1", false, element.getStartTag().toString(), element,"2")); | |
481 | + | |
482 | + } | |
483 | + achou_foreground = false; | |
484 | + achou_background = false; | |
485 | + }//Caso não ache o foreground, usará o padrão encontrado no body | |
486 | + | |
451 | 487 | |
452 | 488 | //Caso seja encontrado um "foreground" e não "background" e vice-versa, será adicionado um aviso para que seja avaliado manualmente |
453 | 489 | /*if((achou_foreground== true && achou_background == false) || (achou_foreground== false && achou_background == true)){ |
... | ... | @@ -501,9 +537,9 @@ public class PresentationEvaluation extends Evaluation{ |
501 | 537 | |
502 | 538 | Color color = null; |
503 | 539 | |
504 | - Pattern value = Pattern.compile(pattern); | |
505 | - | |
506 | - Matcher match = value.matcher(css.replace(";", "\n").replace("}", "").replace("!important", "")); | |
540 | + Pattern value = Pattern.compile("\\s" + pattern); | |
541 | + String cssModificado = css.replace(";", ";\n").replace("}", "").replace("!important", ""); | |
542 | + Matcher match = value.matcher(cssModificado); | |
507 | 543 | while (match.find()){ |
508 | 544 | String cor = match.group(1); |
509 | 545 | ... | ... |