Commit b152e20cd5f587d3b0a41ab709860a988bf6b5dc

Authored by lyandro.santana
1 parent e6102b30

Apresentação

git-svn-id: https://svn.bento.ifrs.edu.br/default/ASES/ASES%20-%20Web/ASES%20-%20Web/Fontes/avaliador-api@10492 c2178572-b5ca-4887-91d2-9e3a90c7d55b
src/main/java/br/com/checker/emag/core/PresentationEvaluation.java
... ... @@ -3,10 +3,9 @@ package br.com.checker.emag.core;
3 3 import java.awt.Color;
4 4 import java.util.ArrayList;
5 5 import java.util.Collections;
6   -import java.util.Comparator;
7 6 import java.util.List;
8   -
9   -import fj.data.hlist.HPre.HBool;
  7 +import java.util.regex.Matcher;
  8 +import java.util.regex.Pattern;
10 9 import net.htmlparser.jericho.Attribute;
11 10 import net.htmlparser.jericho.Element;
12 11 import net.htmlparser.jericho.Source;
... ... @@ -72,22 +71,135 @@ public class PresentationEvaluation extends Evaluation{
72 71 AvaliadorContraste avaliadorContraste = new AvaliadorContraste();
73 72 Color color = null;
74 73  
75   - boolean temBgcolor = false;
  74 + int foreground = Integer.parseInt("000000", 16);
  75 + int background = Integer.parseInt("ffffff", 16);
  76 +
  77 + //verificar css in-line
  78 + for (Element element : getDocument().getAllElements()) {
  79 +
  80 + if(element != null){
  81 + String style = element.getAttributeValue("style");
  82 +
  83 + if(style != null){
  84 + if(isContrastInferior(style)){
  85 + occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1"));
  86 + }
  87 + }
  88 + }
  89 + }
  90 +
  91 + //verificar css in-line
  92 + boolean isValid = false;
  93 + for (Element element : getDocument().getAllElements("body")) {
  94 + if(element != null){
  95 + String style = element.getAttributeValue("style");
  96 +
  97 + if(style != null){
  98 + if(isContrastInferior(style)){
  99 + occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1"));
  100 + isValid = true;
  101 + }
  102 + }
  103 +
  104 + if(!isValid)
  105 + for (Element body : getDocument().getAllElements("body")) {
  106 + Attribute bgc = body.getAttributes().get("bgcolor");
  107 + if(bgc != null){
  108 +
  109 + background = Integer.parseInt(bgc.getValue().trim().replaceFirst("^#",""), 16);
  110 +
  111 + avaliadorContraste.setCores(new Color(background), new Color(foreground));
  112 + avaliadorContraste.avaliar();
  113 +
  114 + if(avaliadorContraste.getContraste() < new Double("4.5")){
  115 + occurrences.add(buildOccurrence("4.1", true, body.getStartTag().toString(), body,"1"));
  116 + isValid = true;
  117 + }
  118 + }
  119 +
  120 + if(body.getName().equals("font")){
  121 + String cor = element.getAttributeValue("color");
  122 + Color foregroundV = null;
  123 + if(!isTextCor(cor)){
  124 + if(cor.contains("rgb")){
  125 + String str = cor.replaceAll("[^-?0-9]+", " ");
  126 +
  127 + foregroundV = new Color(
  128 + Integer.parseInt(str.trim().split(" ")[0]),
  129 + Integer.parseInt(str.trim().split(" ")[1]),
  130 + Integer.parseInt(str.trim().split(" ")[2]));
  131 +
  132 + }else foregroundV = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  133 +
  134 + background = Integer.parseInt(bgc.getValue().trim().replaceFirst("^#",""), 16);
  135 +
  136 + avaliadorContraste.setCores(new Color(background), foregroundV);
  137 + avaliadorContraste.avaliar();
  138 +
  139 + if(avaliadorContraste.getContraste() < new Double("4.5")){
  140 + occurrences.add(buildOccurrence("4.1", true, body.getStartTag().toString(), body,"1"));
  141 + isValid = true;
  142 + }
  143 +
  144 + }
  145 + }
  146 + }
  147 + }
  148 + }
  149 +
  150 + //verificar css interno
  151 + for (Element css : getDocument().getAllElements("style")) {
  152 +
  153 + if(css != null){
  154 + if(isContrastInferior(css.getContent().toString())){
  155 + occurrences.add(buildOccurrence("4.1", true, css.toString(), css,"1"));
  156 + isValid = true;
  157 + }
  158 + }
  159 +
  160 + }
  161 +
  162 + //verificar css externo
  163 + String href = null;
  164 + boolean avalia = false;
  165 + for(Element link : getDocument().getAllElements("link")) {
  166 + href = link.getAttributeValue("href");
  167 + if(href.startsWith("www"))href= "http://"+href;
  168 +
  169 + avalia = getUrl()!=null || href.startsWith("http");
  170 +
  171 + if(href.contains(".css") && avalia) {
  172 +
  173 + if(!href.startsWith("http")) href = getUrl()+"/"+href;
  174 +
  175 + String content = WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent();
  176 + if(isContrastInferior(content)){
  177 + occurrences.add(buildOccurrence("4.1", true, link.toString(), link,"1"));
  178 + isValid = true;
  179 + }
  180 + }
  181 + }
  182 +
  183 +
  184 +
  185 +
  186 + /*boolean temBgcolor = false;
76 187 for (Element bgcolor : getDocument().getAllElements("body")) {
77 188 Attribute bgc = bgcolor.getAttributes().get("bgcolor");
78 189 if(bgc != null){
79 190  
80   - int rgb = Integer.parseInt(bgc.getValue().replaceFirst("^#",""), 16);
81   - color = new Color(rgb);
82   - avaliadorContraste.setCores(color, color);
  191 + background = Integer.parseInt(bgc.getValue().replaceFirst("^#",""), 16);
  192 +
  193 + avaliadorContraste.setCores(new Color(background), new Color(foreground));
83 194 avaliadorContraste.avaliar();
84 195  
85 196 if(avaliadorContraste.getContraste() < new Double("4.5"))
86   - occurrences.add(buildOccurrence("4.1", true, bgcolor.toString(), bgcolor,"1"));
  197 + occurrences.add(buildOccurrence("4.1", true, bgcolor.toString(), bgcolor,"3"));
87 198  
88 199 temBgcolor = true;
89 200 }
90   - }
  201 + }
  202 +
91 203 for (Element bgcolor : getDocument().getAllElements("table")) {
92 204 Attribute bgc = bgcolor.getAttributes().get("bgcolor");
93 205 if(bgc != null){
... ... @@ -132,12 +244,112 @@ public class PresentationEvaluation extends Evaluation{
132 244  
133 245 temBgcolor = true;
134 246 }
135   - }
  247 + } */
136 248  
137 249 /*if(!temBgcolor)
138 250 occurrences.add(new Occurrence("4.1", false, getDocument().getFirstElement().toString(),OccurrenceClassification.PRESENTATION_DESIGN));*/
139 251 Collections.sort(occurrences);
140 252 return occurrences;
  253 +
  254 + }
  255 +
  256 + private boolean isTextCor(String cor){
  257 + return cor.matches("[a-zA-Z\\s]+");
  258 + }
  259 +
  260 + private boolean isContrastInferior(String style){
  261 +
  262 + AvaliadorContraste avaliadorContraste = new AvaliadorContraste();
  263 +
  264 + /*int foreground = Integer.parseInt("000000", 16);
  265 + int background = Integer.parseInt("ffffff", 16);*/
  266 + boolean isProces = true;
  267 +
  268 + Color foregroundColor = new Color(Integer.parseInt("000000", 16));
  269 + Color backgroundColor = new Color(Integer.parseInt("ffffff", 16));
  270 +
  271 +
  272 + //foreground
  273 + Pattern colorValue = Pattern.compile("color:(.*?)\\;");
  274 +
  275 + Matcher match = colorValue.matcher(style);
  276 +
  277 + if(match.find()){
  278 +
  279 + String cor = match.group(1);
  280 +
  281 + //System.out.println("foreground: "+cor);
  282 + if(!isTextCor(match.group(1)) && !cor.contains("url")){
  283 +
  284 + if(cor.contains("rgb")){
  285 + String str = cor.replaceAll("[^-?0-9]+", " ");
  286 +
  287 + foregroundColor = new Color(
  288 + Integer.parseInt(str.trim().split(" ")[0]),
  289 + Integer.parseInt(str.trim().split(" ")[1]),
  290 + Integer.parseInt(str.trim().split(" ")[2]));
  291 +
  292 + }else foregroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  293 +
  294 + }else isProces = false;
  295 + }
  296 +
  297 + //background
  298 + Pattern backgroundValue = Pattern.compile("background:(.*?)\\;");
  299 + match = backgroundValue.matcher(style);
  300 + if(match.find()){
  301 + String cor = match.group(1);
  302 +
  303 + //System.out.println("background: "+cor);
  304 + if(!isTextCor(cor) && !cor.contains("url")){
  305 +
  306 + if(cor.contains("rgb")){
  307 + String str = cor.replaceAll("[^-?0-9]+", " ");
  308 +
  309 + backgroundColor = new Color(
  310 + Integer.parseInt(str.trim().split(" ")[0]),
  311 + Integer.parseInt(str.trim().split(" ")[1]),
  312 + Integer.parseInt(str.trim().split(" ")[2]));
  313 + }else backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  314 +
  315 + }else isProces = false;
  316 +
  317 + }
  318 +
  319 + //background
  320 + Pattern backgroundColorValue = Pattern.compile("background-color:(.*?)\\;");
  321 + match = backgroundColorValue.matcher(style);
  322 +
  323 + if(match.find()){
  324 + String cor = match.group(1);
  325 +
  326 + //System.out.println(cor);
  327 + if(!isTextCor(cor)){
  328 +
  329 + if(cor.contains("rgb") && !cor.contains("url")){
  330 + String str = cor.replaceAll("[^-?0-9]+", " ");
  331 +
  332 + backgroundColor = new Color(
  333 + Integer.parseInt(str.trim().split(" ")[0]),
  334 + Integer.parseInt(str.trim().split(" ")[1]),
  335 + Integer.parseInt(str.trim().split(" ")[2]));
  336 + }else backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  337 +
  338 + }else isProces = false;
  339 + }
  340 +
  341 + if(isProces){
  342 + avaliadorContraste.setCores(foregroundColor,backgroundColor);
  343 + avaliadorContraste.avaliar();
  344 +
  345 + if(avaliadorContraste.getContraste() < new Double("4.5")){
  346 + System.out.println("foreground: "+foregroundColor.getRGB()+" -- "+"background"+backgroundColor.getRGB()+" Resultado: "+avaliadorContraste.getContraste());
  347 + return true;
  348 + }
  349 +
  350 + }
  351 +
  352 + return false;
141 353 }
142 354  
143 355 private List<Occurrence> checkRecommendation30() {
... ... @@ -186,7 +398,7 @@ public class PresentationEvaluation extends Evaluation{
186 398  
187 399 if(!href.startsWith("http")) href = getUrl()+href;
188 400  
189   - String content = WebAgent.from(href).withGetRequest().execute().getContent();
  401 + String content = WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent();
190 402  
191 403 if (content!=null && !content.contains("a:hover") && !content.contains("a:focus")){
192 404 occurrences.add(buildOccurrence("4.4", true, link.toString(), link, "1"));
... ...