From 2c1cf7d90c01fce7473785277b0de213959f343c Mon Sep 17 00:00:00 2001 From: gibran Date: Wed, 15 Jun 2016 09:09:44 -0300 Subject: [PATCH] Correção do método para verificação do foco na página. --- src/main/java/br/com/checker/emag/core/PresentationEvaluation.java | 531 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------- 1 file changed, 473 insertions(+), 58 deletions(-) diff --git a/src/main/java/br/com/checker/emag/core/PresentationEvaluation.java b/src/main/java/br/com/checker/emag/core/PresentationEvaluation.java index 15ee30e..669d671 100644 --- a/src/main/java/br/com/checker/emag/core/PresentationEvaluation.java +++ b/src/main/java/br/com/checker/emag/core/PresentationEvaluation.java @@ -2,10 +2,20 @@ package br.com.checker.emag.core; import java.awt.Color; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import javax.swing.text.html.HTML; + +import com.oracle.webservices.internal.api.EnvelopeStyle.Style; + import net.htmlparser.jericho.Attribute; import net.htmlparser.jericho.Element; import net.htmlparser.jericho.Source; @@ -14,10 +24,12 @@ import br.com.checker.emag.Occurrence; import br.com.checker.emag.OccurrenceClassification; import br.com.checker.emag.core.SpecificRecommendation.PresentationRecommendation; import br.com.checker.emag.util.WebAgent; +import br.com.checker.emag.util.UrlSemArquiNoFinal; + + public class PresentationEvaluation extends Evaluation{ - private PresentationEvaluation(Source document) { super(document); } private PresentationEvaluation(Source document,String url) { @@ -55,7 +67,7 @@ public class PresentationEvaluation extends Evaluation{ public List check() { getOccurrences().clear(); - //getOccurrences().addAll(checkRecommendation29()); comentado por Gibran + //getOccurrences().addAll(checkRecommendation29()); //comentado por Gibran //getOccurrences().addAll(checkRecommendation30()); comentado por Gibran //getOccurrences().addAll(checkRecommendation31()); comentado por Gibran getOccurrences().addAll(checkRecommendation32()); @@ -64,18 +76,233 @@ public class PresentationEvaluation extends Evaluation{ } - private List checkRecommendation29() { List occurrences = new ArrayList(); + UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); + + String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal.urlSemArquivoNoFinal(getUrl()); + //verificar css externo + String href = null; + boolean avalia = false; + + List lsContent = new ArrayList(); + + for(Element link2 : getDocument().getAllElements("link")) { + href = link2.getAttributeValue("href"); + if(href.startsWith("www"))href= "http://"+ href; + + avalia = getUrl() != null || href.startsWith("http"); + + if(href.contains(".css") && avalia) { + + + if(!href.startsWith("http")) href = urlSemArquiNoFinal + "/" + href; + + + lsContent.add(WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent()); + } + } + AvaliadorContraste avaliadorContraste = new AvaliadorContraste(); + + Color corAvaliar = null; + Color foreground = null; + Color background = null; + boolean achou_foreground = false; + boolean achou_background = false; + String styleInline = ""; + String valor_id = ""; + String valor_class = ""; + List lsAttribute = new ArrayList(); + + //pega css interno caso tenha + for (Element head : getDocument().getAllElements("head")) { + for (Element estilo : head.getAllElements("style")) { + for (Attribute atribute : estilo.getAttributes()) { + lsAttribute.add(atribute); + } + } + } + + + //Pega todos elementos do "body" + for (Element body : getDocument().getAllElements("body")) { + for (Element element : body.getAllElements()) { + + //Primeiro verificar se tem css in-line + styleInline = element.getAttributeValue("style"); + if(styleInline != null){ + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(styleInline, "color:(.*?)\\Z"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + corAvaliar = getColor(styleInline, "color:(.*?)\\;"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(styleInline, "background:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(styleInline, "background:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(styleInline, "background-color:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(styleInline, "background-color:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + if(foreground != null) achou_foreground = true; + if(background != null) achou_background = true; + + }else + { + + //verifica css interno ou externo + + //Verifica se tem #id + valor_id = element.getAttributeValue("id"); + if(valor_id != null) + { + for (Attribute style : lsAttribute){ + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + System.out.println("#"+ style); + if(style.toString().equalsIgnoreCase("#"+ element.toString())) + { + corAvaliar = getColor(style.toString(), "color:(.*?)\\Z"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + corAvaliar = getColor(style.toString(), "color:(.*?)\\;"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(style.toString(), "background:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(style.toString(), "background:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(style.toString(), "background-color:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(style.toString(), "background-color:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + if(foreground != null) achou_foreground = true; + if(background != null) achou_background = true; + } + + + } + + + }else + { + valor_class = element.getAttributeValue("class"); + + } + + + corAvaliar = null; + foreground = null; + background = null; + achou_foreground = false; + achou_background = false; + + + } + } + } + return occurrences; + } + + + + + private List checkRecommendation292() { + List occurrences = new ArrayList(); + + UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); + + String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal.urlSemArquivoNoFinal(getUrl()); + //verificar css externo + String href = null; + boolean avalia = false; + + List lsContent = new ArrayList(); + + for(Element link2 : getDocument().getAllElements("link")) { + href = link2.getAttributeValue("href"); + if(href.startsWith("www"))href= "http://"+ href; + + avalia = getUrl() != null || href.startsWith("http"); + + if(href.contains(".css") && avalia) { + + + if(!href.startsWith("http")) href = urlSemArquiNoFinal + "/" + href; + + + lsContent.add(WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent()); + } + } + + //List tags = Arrays.asList("h1", "h2", "h3", "h4", "h5", "h6","a", "p","select", "option", "textarea", "button", "datalist", "keygen", + // "output", "input","label", "select","optgroup", "form","fieldset","legend"); + + // Implementação comentada, em virtude de comprometimento da performance do avaliador. Lyandro Santana - 12/11/2015 - /*String[] attributes = {"class","id", "bgcolor"}; + String[] attributes = {"bgcolor","h1", "h2", "h3", "h4", "h5", "h6","a", "p","select", "option", "textarea", "button", "datalist", "keygen", + "output", "input","label", "select","optgroup", "form","fieldset","legend"}; + AvaliadorContraste avaliadorContraste = new AvaliadorContraste(); System.out.println("Processando verificação de cor..."); - for (Element body : getDocument().getAllElements("body")) { - for (Element element : body.getAllElements()) { + for (Element body : getDocument().getAllElements("body")) { + for (String atributo : attributes) { + + for (Element element : body.getAllElements(atributo)) { + Color corAvaliar = null; Color foreground = null; Color background = null; boolean achou_foreground = false; @@ -83,17 +310,51 @@ public class PresentationEvaluation extends Evaluation{ if(element != null ){ - for (String attribute : attributes) { + + //for (String attribute : attributes) { //System.out.println("Verificando: "+attribute); //Verificar IN-LINE String styleInline = element.getAttributeValue("style"); if(styleInline != null){ + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(styleInline, "color:(.*?)\\Z"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + corAvaliar = getColor(styleInline, "color:(.*?)\\;"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } - foreground = getColor(styleInline, "color:(.*?)\\;"); - background = getColor(styleInline, "background:(.*?)\\;"); - background = getColor(styleInline, "background-color:(.*?)\\;"); + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(styleInline, "background:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(styleInline, "background:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(styleInline, "background-color:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(styleInline, "background-color:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } if(foreground != null) achou_foreground = true; if(background != null) achou_background = true; @@ -109,11 +370,50 @@ public class PresentationEvaluation extends Evaluation{ //Verificar css interno for (Element style : getDocument().getAllElements("style")){ - foreground = getColor(style.toString(), "color:(.*?)\\;"); - background = getColor(style.toString(), "background:(.*?)\\;"); - background = getColor(style.toString(), "background-color:(.*?)\\;"); + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(style.toString(), "color:(.*?)\\Z"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + corAvaliar = getColor(style.toString(), "color:(.*?)\\;"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + //foreground = getColor(style.toString(), "color:(.*?)\\;"); + //background = getColor(style.toString(), "background:(.*?)\\;"); + //background = getColor(style.toString(), "background-color:(.*?)\\;"); + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(style.toString(), "background:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(style.toString(), "background:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(style.toString(), "background-color:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + corAvaliar = getColor(style.toString(), "background-color:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + if(foreground != null) achou_foreground = true; if(background != null) achou_background = true; } @@ -121,7 +421,9 @@ public class PresentationEvaluation extends Evaluation{ if(achou_foreground== false || achou_background == false){ //verificar css externo - String href = null; + + + /*String href = null; boolean avalia = false; for(Element link : getDocument().getAllElements("link")) { href = link.getAttributeValue("href"); @@ -136,10 +438,53 @@ public class PresentationEvaluation extends Evaluation{ String content = WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent(); - foreground = getColor(content, "color:(.*?)\\;"); - background = getColor(content, "background:(.*?)\\;"); - background = getColor(content, "background-color:(.*?)\\;"); + */ + for (String content : lsContent) { + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(content.toString(), "color:(.*?)\\Z"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + corAvaliar = getColor(content.toString(), "color:(.*?)\\;"); + if(corAvaliar != null) + { + foreground = corAvaliar; + } + + //foreground = getColor(content, "color:(.*?)\\;"); + //background = getColor(content, "background:(.*?)\\;"); + //background = getColor(content, "background-color:(.*?)\\;"); + + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(content, "background:(.*?)\\Z"); + if(corAvaliar != null) + { + background = corAvaliar; + } + + corAvaliar = getColor(content, "background:(.*?)\\;"); + if(corAvaliar != null) + { + background = corAvaliar; + } + //pega o valor da cor, mesmo que no final não seja atribuido o ";" + corAvaliar = getColor(content, "background-color:(.*?)\\Z"); + if(corAvaliar != null) + { + + background = corAvaliar; + } + + corAvaliar = getColor(content, "background-color:(.*?)\\;"); + if(corAvaliar != null) + { + + background = corAvaliar; + } if(foreground != null) achou_foreground = true; if(background != null) achou_background = true; @@ -158,40 +503,43 @@ public class PresentationEvaluation extends Evaluation{ avaliadorContraste.avaliar(); if(avaliadorContraste.getContraste() < new Double("4.5")){ - occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1")); + occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"2")); break; } - }else if(achou_foreground== true && achou_background == false){ + }else if(achou_foreground == false && achou_background == true){ - avaliadorContraste.setCores(foreground, new Color(Integer.parseInt("ffffff", 16))); - avaliadorContraste.avaliar(); + //avaliadorContraste.setCores(foreground, new Color(Integer.parseInt("ffffff", 16))); + //avaliadorContraste.avaliar(); - if(avaliadorContraste.getContraste() < new Double("4.5")){ - occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1")); + //if(avaliadorContraste.getContraste() < new Double("4.5")){ + occurrences.add(buildOccurrence("4.1", false, element.toString(), element,"3")); break; - } + //} - }else if(achou_foreground== false && achou_background == true){ + }else if(achou_foreground== true && achou_background == false){ - avaliadorContraste.setCores(new Color(Integer.parseInt("000000", 16)), background); - avaliadorContraste.avaliar(); + //avaliadorContraste.setCores(foreground, new Color(Integer.parseInt("ffffff", 16))); + //avaliadorContraste.avaliar(); - if(avaliadorContraste.getContraste() < new Double("4.5")){ - occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1")); + //if(avaliadorContraste.getContraste() < new Double("4.5")){ + occurrences.add(buildOccurrence("4.1", false, element.toString(), element,"3")); break; - } + //} } } } } + System.out.println("Verificação terminada."); + return occurrences; } + //} + - System.out.println("Verificação terminada."); */ @@ -204,10 +552,10 @@ public class PresentationEvaluation extends Evaluation{ //Término da rotina de Avaliação de contraste - occurrences.add(new Occurrence("4.1", false, getDocument().getFirstElement().toString(),OccurrenceClassification.PRESENTATION_DESIGN)); - return occurrences; + // occurrences.add(new Occurrence("4.1", false, getDocument().getFirstElement().toString(),OccurrenceClassification.PRESENTATION_DESIGN)); + - } + //} private Color getColor(String css, String pattern){ @@ -231,12 +579,15 @@ public class PresentationEvaluation extends Evaluation{ Integer.parseInt(str.trim().split(" ")[0]), Integer.parseInt(str.trim().split(" ")[1]), Integer.parseInt(str.trim().split(" ")[2])); + break; }else{ //System.out.println("EXADECIMAL: "+match.group(1)); - color = new Color(Integer.parseInt(match.group(1).trim().replaceFirst("^#",""), 16)); + color = new Color(Integer.parseInt(match.group(1).trim().replaceFirst("#",""), 16)); + break; } } } catch (Exception e) { + e.printStackTrace(); return null; } } @@ -357,61 +708,125 @@ public class PresentationEvaluation extends Evaluation{ return occurrences; } + /*private String urlSemArquivoNoFinal(String url) + { + if(url != null) + { + String urlSemArquivo = ""; + boolean encontrouPonto = false; + + + for(int i = url.length()-1; i >= 0; i-- ) + { + + urlSemArquivo = url.substring(i-1,i); + + if(urlSemArquivo.equalsIgnoreCase(".")) + { + encontrouPonto = true; + + }else if(encontrouPonto && urlSemArquivo.equalsIgnoreCase("/")) + { + url = url.substring(0,i); + break; + } + else if(!encontrouPonto && urlSemArquivo.equalsIgnoreCase("/")) + { + break; + } + } + } + return url; + }*/ private List checkRecommendation32() { List occurrences = new ArrayList(); + UrlSemArquiNoFinal objetoUrlSemArquiNoFinal = new UrlSemArquiNoFinal(); + + String urlSemArquiNoFinal = objetoUrlSemArquiNoFinal.urlSemArquivoNoFinal(getUrl()); + + + //Caso seja encontrado o "a:focus" e "a:hover" n�o continua a verifica��o nos demais for's, bem como, n�o + //adiciona a ocorr�ncia + boolean focoEncontrado = false; + boolean passivelDeAvaliacao = false; + for (Element style : getDocument().getAllElements("style")) { - if(!style.toString().contains("a:focus") && !style.toString().contains("a:hover")){ - occurrences.add(buildOccurrence("4.4", true, style.toString(), style, "1")); + if(style.toString().contains("a:focus") && style.toString().contains("a:hover")){ + //occurrences.add(buildOccurrence("4.4", true, style.toString(), style, "1")); + focoEncontrado = true; break; } } + if(!focoEncontrado) + { + for (Element style : getDocument().getAllElements("a")) { + Attribute attribute = style.getAttributes().get("style"); + if(attribute != null) + if(attribute.toString().contains("a:focus") && attribute.toString().contains("a:hover")){ + //occurrences.add(buildOccurrence("4.4", true, style.toString(), style, "1")); + focoEncontrado = true; + break; + } + } - for (Element style : getDocument().getAllElements("a")) { - Attribute attribute = style.getAttributes().get("style"); - if(attribute != null) - if(!attribute.toString().contains("a:focus") && !attribute.toString().contains("a:hover")){ - occurrences.add(buildOccurrence("4.4", true, style.toString(), style, "1")); - break; - } } - - + if(!focoEncontrado) + { String href = null; - boolean avalia = false; + WebAgent webAgent; + String content = ""; + + boolean avalia = false; + for(Element link : getDocument().getAllElements("link")) { href = link.getAttributeValue("href"); - if(href.startsWith("www"))href= "http://"+href; + if(href.startsWith("www")) + { + href= "http://" + href; + } avalia = getUrl()!=null || href.startsWith("http"); if(href.contains(".css") && avalia) { - if(!href.startsWith("http")) href = getUrl()+"/"+href; + if(!href.startsWith("http")) href = urlSemArquiNoFinal + href; + href = href.replace(" ", "%20").replace("//", "/").replace(":/", "://"); + webAgent = WebAgent.from(href).withGetRequest().execute(); - String content = WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent(); - - if (content!=null && !content.contains("a:hover") && !content.contains("a:focus")){ - occurrences.add(buildOccurrence("4.4", true, link.toString(), link, "1")); + if(webAgent.getStatusCode() != 404) + { + content = webAgent.getContent(); + passivelDeAvaliacao = true; + if (content!=null && content.contains("a:hover") && content.contains("a:focus")){ + + focoEncontrado = true; break; + } + + } + } } + } + + if(!focoEncontrado && passivelDeAvaliacao) + { + occurrences.add(buildOccurrence("4.4", true, getDocument().getFirstStartTag().toString(), getDocument().getFirstElement(), "1")); + } Collections.sort(occurrences); return occurrences; } - private Occurrence buildOccurrence(String code, boolean error, - String tag, Element element, - String criterio) { + private Occurrence buildOccurrence(String code, boolean error, String tag, Element element, String criterio) { return super.buildOccurrence(code, error, tag, element, OccurrenceClassification.PRESENTATION_DESIGN,criterio); } - private Occurrence buildOccurrence(String code, boolean error, - String tag, Element element) { + private Occurrence buildOccurrence(String code, boolean error, String tag, Element element) { return super.buildOccurrence(code, error, tag, element, OccurrenceClassification.PRESENTATION_DESIGN); } -- libgit2 0.21.2