diff --git a/src/main/java/br/com/checker/emag/AvaliadorContraste.java b/src/main/java/br/com/checker/emag/AvaliadorContraste.java index 0475ceb..4fc5157 100644 --- a/src/main/java/br/com/checker/emag/AvaliadorContraste.java +++ b/src/main/java/br/com/checker/emag/AvaliadorContraste.java @@ -35,34 +35,33 @@ public class AvaliadorContraste { private final double potenciaSrgb = 2.4; /** - * Construtor padrão que seta as cores do avaliador como - * banca e branca para caso se chame o método avaliar não ocorra - * nenhuma Exception. - */ + * Construtor padrão que seta as cores do avaliador como banca e branca para caso se chame o + * método avaliar não ocorra nenhuma Exception. + */ public AvaliadorContraste() { this.setCores(Color.WHITE, Color.WHITE); } /** - * Construtor para passar as cores que se deseja avaliar. - * @param cor1 Cor a ser avaliada - * @param cor2 Cor a ser avaliada - */ + * Construtor para passar as cores que se deseja avaliar. + * + * @param cor1 Cor a ser avaliada + * @param cor2 Cor a ser avaliada + */ public AvaliadorContraste(final Color cor1, final Color cor2) { this.setCores(cor1, cor2); } /** - * Seta as cores que serão avaliadas. - * @param cor1 Cor a ser avaliada - * @param cor2 Cor a ser avaliada - */ + * Seta as cores que serão avaliadas. + * + * @param cor1 Cor a ser avaliada + * @param cor2 Cor a ser avaliada + */ public final void setCores(final Color cor1, final Color cor2) { if (cor1 != null && cor2 != null) { - int cor1Soma = cor1.getRed() + cor1.getGreen() - + cor1.getBlue(); - int cor2Soma = cor2.getRed() + cor2.getGreen() - + cor2.getBlue(); + int cor1Soma = cor1.getRed() + cor1.getGreen() + cor1.getBlue(); + int cor2Soma = cor2.getRed() + cor2.getGreen() + cor2.getBlue(); if (cor1Soma >= cor2Soma) { this.corMaisClara = cor1; this.corMaisEscura = cor2; @@ -74,24 +73,24 @@ public class AvaliadorContraste { } /** - * Método que calcula e armazena o contraste das cores avaliadas - * no atributo 'constraste'. - */ + * Método que calcula e armazena o contraste das cores avaliadas no atributo 'constraste'. + */ public final void avaliar() { double luminecencia1; double luminecencia2; luminecencia1 = calcularLuminecencia(this.corMaisClara); luminecencia2 = calcularLuminecencia(this.corMaisEscura); - this.contraste = (luminecencia1 + this.fatorDeLuminosidade) - / (luminecencia2 + fatorDeLuminosidade); + this.contraste = + (luminecencia1 + this.fatorDeLuminosidade) / (luminecencia2 + fatorDeLuminosidade); } /** - * Método para calcular a luminecência de uma cor. - * @param cor para ser calculado a luminecência. - * @return luminecencia da cor - */ + * Método para calcular a luminecência de uma cor. + * + * @param cor para ser calculado a luminecência. + * @return luminecencia da cor + */ private double calcularLuminecencia(final Color cor) { double red; double green; @@ -107,45 +106,47 @@ public class AvaliadorContraste { if (redsrgb <= this.limiteSrgb) { red = redsrgb / this.divisoredsrgb; } else { - red = Math.pow(((redsrgb + this.fatorDeCorrecaoSrgb) - / this.divisorAlternativoSrgb), this.potenciaSrgb); + red = + Math.pow(((redsrgb + this.fatorDeCorrecaoSrgb) / this.divisorAlternativoSrgb), + this.potenciaSrgb); } if (greensrgb <= this.limiteSrgb) { green = greensrgb / this.divisoredsrgb; } else { - green = Math.pow(((greensrgb + this.fatorDeCorrecaoSrgb) - / this.divisorAlternativoSrgb), this.potenciaSrgb); + green = + Math.pow(((greensrgb + this.fatorDeCorrecaoSrgb) / this.divisorAlternativoSrgb), + this.potenciaSrgb); } if (bluesrgb <= this.limiteSrgb) { blue = bluesrgb / this.divisoredsrgb; } else { - blue = Math.pow(((bluesrgb + this.fatorDeCorrecaoSrgb) - / this.divisorAlternativoSrgb), this.potenciaSrgb); + blue = + Math.pow(((bluesrgb + this.fatorDeCorrecaoSrgb) / this.divisorAlternativoSrgb), + this.potenciaSrgb); } - return (red * this.multiplicadorDoVermelho) - + (green * this.multiplicadorDoVerde) + return (red * this.multiplicadorDoVermelho) + (green * this.multiplicadorDoVerde) + (blue * this.multiplicadorDoAzul); } /** - * Metodo que retorna o valor armazenado no atributo 'contraste'. - * @return contraste - */ + * Metodo que retorna o valor armazenado no atributo 'contraste'. + * + * @return contraste + */ public final double getContraste() { return this.contraste; } /** - * Método que retorna o valor armazenado no atributo 'contraste' - * devidamente formatado. - * @return contrasteFormatado - */ + * Método que retorna o valor armazenado no atributo 'contraste' devidamente formatado. + * + * @return contrasteFormatado + */ public final String getContrasteFormatado() { - String strContraste = String.valueOf(new DecimalFormat("#.##") - .format(this.contraste)); + String strContraste = String.valueOf(new DecimalFormat("#.##").format(this.contraste)); return strContraste.replace(".", ","); } } diff --git a/src/main/java/br/com/checker/emag/core/BehaviorEvaluation.java b/src/main/java/br/com/checker/emag/core/BehaviorEvaluation.java index ea32787..162d85d 100644 --- a/src/main/java/br/com/checker/emag/core/BehaviorEvaluation.java +++ b/src/main/java/br/com/checker/emag/core/BehaviorEvaluation.java @@ -11,344 +11,414 @@ import br.com.checker.emag.Occurrence; import br.com.checker.emag.OccurrenceClassification; import br.com.checker.emag.core.SpecificRecommendation.BehaviorRecommendation; -public class BehaviorEvaluation extends Evaluation{ - - - private BehaviorEvaluation(Source document) { super(document); } - - public static class BehaviorEvaluationBuilder extends EvaluationBuilder { - - @Override - protected BehaviorEvaluation with(Source document) { return new BehaviorEvaluation(document); } - - @Override - protected BehaviorEvaluation with(Source document,String url) { return new BehaviorEvaluation(document); } - - public SpecificRecommendation recommendation10() { return new EvaluationRecommendation10();} - public SpecificRecommendation recommendation11() { return new EvaluationRecommendation11();} - public SpecificRecommendation recommendation12() { return new EvaluationRecommendation12();} - public SpecificRecommendation recommendation13() { return new EvaluationRecommendation13();} - public SpecificRecommendation recommendation14() { return new EvaluationRecommendation14();} - public SpecificRecommendation recommendation15() { return new EvaluationRecommendation15();} - public SpecificRecommendation recommendation16() { return new EvaluationRecommendation16();} +public class BehaviorEvaluation extends Evaluation { + + + private BehaviorEvaluation(Source document) { + super(document); + } + + public static class BehaviorEvaluationBuilder extends EvaluationBuilder { + + @Override + protected BehaviorEvaluation with(Source document) { + return new BehaviorEvaluation(document); } - - protected static class EvaluationRecommendation10 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation10();} + + @Override + protected BehaviorEvaluation with(Source document, String url) { + return new BehaviorEvaluation(document); } - - protected static class EvaluationRecommendation11 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation11();} + + public SpecificRecommendation recommendation10() { + return new EvaluationRecommendation10(); } - - protected static class EvaluationRecommendation12 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation12();} + + public SpecificRecommendation recommendation11() { + return new EvaluationRecommendation11(); } - - protected static class EvaluationRecommendation13 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation13();} + + public SpecificRecommendation recommendation12() { + return new EvaluationRecommendation12(); } - - protected static class EvaluationRecommendation14 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation14();} + + public SpecificRecommendation recommendation13() { + return new EvaluationRecommendation13(); } - - protected static class EvaluationRecommendation15 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation15();} + + public SpecificRecommendation recommendation14() { + return new EvaluationRecommendation14(); } - - protected static class EvaluationRecommendation16 extends BehaviorRecommendation{ - protected List check() { return getEvaluation().checkRecommendation16();} + + public SpecificRecommendation recommendation15() { + return new EvaluationRecommendation15(); } - - public List check() { - getOccurrences().clear(); - getOccurrences().addAll(checkRecommendation10()); - getOccurrences().addAll(checkRecommendation11()); - getOccurrences().addAll(checkRecommendation12()); - getOccurrences().addAll(checkRecommendation13()); - //getOccurrences().addAll(checkRecommendation14()); comentado por Gibran - getOccurrences().addAll(checkRecommendation15()); - //getOccurrences().addAll(checkRecommendation16()); comentado por Gibran - - return getOccurrences(); + + public SpecificRecommendation recommendation16() { + return new EvaluationRecommendation16(); } - - - - private List checkRecommendation10() { -List occurrences = new ArrayList(); - - for (Element element : getDocument().getAllElements()) { - - Attribute onmousedown = null,onkeydown = null, - onmouseup = null, onkeyup = null, - onclick = null, onkeypress = null, - onmouseover = null,onfocus = null, - onmouseout = null, onblur = null,dbclick = null, ondblclick = null; - - if (element.getAttributes() != null) { - onmousedown = element.getAttributes().get("onmousedown"); - onkeydown = element.getAttributes().get("onkeydown"); - - onmouseup = element.getAttributes().get("onmouseup"); - onkeyup = element.getAttributes().get("onkeyup"); - - onclick = element.getAttributes().get("onclick"); - onkeypress = element.getAttributes().get("onkeypress"); - - onmouseover = element.getAttributes().get("onmouseover"); - onfocus = element.getAttributes().get("onfocus"); - - onmouseout = element.getAttributes().get("onmouseout"); - onblur = element.getAttributes().get("onblur"); - - dbclick = element.getAttributes().get("dbclick"); - ondblclick = element.getAttributes().get("ondblclick"); - } - - if(dbclick!=null){ - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "6"));//"2")); - } - - if(ondblclick!=null){ - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "6"));//"2")); - } - - if (onmousedown != null && onkeydown == null) { - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "2"));//"1")); - } - - if (onmouseup != null && onkeyup == null) { - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "1"));//"1")); - } - /*if (onclick != null && onkeypress == null) { - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "1")); - }*/ - if (onmouseover != null && onfocus == null) { - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "2"));//"1")); - } - if (onmouseout != null && onblur == null) { - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "2"));//"1")); - } - } - - String attributes[] = {"onafterprint","onbeforeprint","onbeforeunload","onerror", "onhashchange", "onload", "onmessage", "onoffline","ononline", "onpagehide","onpageshow","onpopstate", - "onresize", "onstorage", "onunloadnblur", "onblur","onchange", "oncontextmenu", "onfocus", "oninput","oninvalid","onreset","onsearch","onselect","onsubmit","onkeydown","onkeypress", - "onkeyup", "onclick","ondblclick", "ondrag","ondragend","ondragenter", "ondragleave", "ondragover", "ondragstart","ondrop","onmousedown", "onmousemove","onmouseout","onmouseover","onmouseup", - "onmousewheel", "onscroll", "onwheel", "oncopy", "oncut","onpaste", "onabort", "oncanplay", "oncanplaythrough","oncuechange","ondurationchange","onemptied","onended","onerror", - "onloadeddata","onloadedmetadata","onloadstart","onpause","onplay","onplaying","onprogress","onratechange","onseeked","onseeking","onstalled","onsuspend","ontimeupdate", - "onvolumechange","onwaiting","onerror","onshow","ontoggle"}; - - for(Element element : getDocument().getAllElements()){ - - if(!isTagForm(element)) - if(element != null){ - for(String attribute : attributes){ - if (element.getAttributeValue(attribute) != null) { - occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "8"));//"3")); - } - } - } - } - - return occurrences; + } + + protected static class EvaluationRecommendation10 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation10(); + } + } + + protected static class EvaluationRecommendation11 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation11(); + } + } + + protected static class EvaluationRecommendation12 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation12(); + } + } + + protected static class EvaluationRecommendation13 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation13(); + } + } + + protected static class EvaluationRecommendation14 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation14(); + } + } + + protected static class EvaluationRecommendation15 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation15(); } - - - - private List checkRecommendation11() { - List occurrences = new ArrayList(); - - //boolean script = false; - //boolean contAlter = false; - - /*if(!getDocument().getAllElements("script").isEmpty()) - script = true; - - if(script) - if(getDocument().getAllElements("noscript").isEmpty()) - occurrences.add(this.buildOccurrence("2.2", true, getDocument().getFirstElement("html").toString(), getDocument().getFirstElement("html"), "1"));*/ - - for(Element elementScript : getDocument().getAllElements("script")){ - if((!elementScript.isEmpty()) && (elementScript.getAttributeValue("noscript") == null)) - occurrences.add(this.buildOccurrence("2.2", true, elementScript.toString(), elementScript, "1")); + } + + protected static class EvaluationRecommendation16 extends BehaviorRecommendation { + protected List check() { + return getEvaluation().checkRecommendation16(); + } + } + + public List check() { + getOccurrences().clear(); + getOccurrences().addAll(checkRecommendation10()); + getOccurrences().addAll(checkRecommendation11()); + getOccurrences().addAll(checkRecommendation12()); + getOccurrences().addAll(checkRecommendation13()); + // getOccurrences().addAll(checkRecommendation14()); comentado por Gibran + getOccurrences().addAll(checkRecommendation15()); + // getOccurrences().addAll(checkRecommendation16()); comentado por Gibran + + return getOccurrences(); + } + + + + private List checkRecommendation10() { + List occurrences = new ArrayList(); + + for (Element element : getDocument().getAllElements()) { + + Attribute onmousedown = null, onkeydown = null, onmouseup = null, onkeyup = null, onclick = + null, onkeypress = null, onmouseover = null, onfocus = null, onmouseout = null, onblur = + null, dbclick = null, ondblclick = null; + + if (element.getAttributes() != null) { + onmousedown = element.getAttributes().get("onmousedown"); + onkeydown = element.getAttributes().get("onkeydown"); + + onmouseup = element.getAttributes().get("onmouseup"); + onkeyup = element.getAttributes().get("onkeyup"); + + onclick = element.getAttributes().get("onclick"); + onkeypress = element.getAttributes().get("onkeypress"); + + onmouseover = element.getAttributes().get("onmouseover"); + onfocus = element.getAttributes().get("onfocus"); + + onmouseout = element.getAttributes().get("onmouseout"); + onblur = element.getAttributes().get("onblur"); + + dbclick = element.getAttributes().get("dbclick"); + ondblclick = element.getAttributes().get("ondblclick"); } - - - if(!getDocument().getAllElements("embed").isEmpty()){ - for(Element embed : getDocument().getAllElements("embed")) - occurrences.add(this.buildOccurrence("2.2", false, embed.toString(), embed, "3")); - } - - if(!getDocument().getAllElements("applet").isEmpty()){ - for(Element applet : getDocument().getAllElements("applet")) - occurrences.add(this.buildOccurrence("2.2", false, applet.toString(), applet, "4")); + + if (dbclick != null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "6"));// "2")); } - - for (Element object : getDocument().getAllElements("object")) { - boolean isParam = false; - - for(Element element : object.getChildElements()){ - if(element.getName().equals("param")){ - isParam = true; - } - } - - if(!isParam) - occurrences.add(this.buildOccurrence("2.2", true, object.toString(), object, "2")); + + if (ondblclick != null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "6"));// "2")); } - - - - /*for (Element object : getDocument().getAllElements("object")) { - - for(Element elemnet : object.getChildElements()){ - if(!elemnet.getName().equals("param")){ - contAlter = true; - } - } - - if(!contAlter){ - occurrences.add(this.buildOccurrence("2.2", true, object.toString(), object, "2")); - contAlter = false; - } - - }*/ - - return occurrences; - } - - private List checkRecommendation12() { - List occurrences = new ArrayList(); - - boolean temMetaRefresh = false; - for (Element element : getDocument().getAllElements("meta")) { - Attribute httpEquiv = element.getAttributes().get("http-equiv"); - - if (httpEquiv != null && "refresh".equals(httpEquiv.getValue())) { - occurrences.add(this.buildOccurrence("2.3", false, element.toString(), element, "1")); - temMetaRefresh = true; - } + + if (onmousedown != null && onkeydown == null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "2"));// "1")); } - - /*if(!temMetaRefresh) - occurrences.add(new Occurrence("2.3", false, this.getDocument().getFirstElement().toString(),OccurrenceClassification.BEHAVIOR));*/ - - return occurrences; - } - - private List checkRecommendation13() { - List occurrences = new ArrayList(); - - for (Element element : getDocument().getAllElements("meta")) { - Attribute content = element.getAttributes().get("content"); - Attribute httpEquiv = element.getAttributes().get("http-equiv"); - - boolean url = false; - if(content != null) - url = content.toString().contains("url"); - - if (httpEquiv != null && "refresh".equals(httpEquiv.getValue()) && content != null && url == true) - occurrences.add(this.buildOccurrence("2.4", true, element.toString(), element, "1")); + + if (onmouseup != null && onkeyup == null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "1"));// "1")); } - - return occurrences; - } - - private List checkRecommendation14() { - List occurrences = new ArrayList(); - boolean hasBlink = false; - boolean hasMarquee = false; - for (Element blink : getDocument().getAllElements("blink")) { - occurrences.add(this.buildOccurrence("2.5", true, blink.toString(), blink)); - hasBlink = true; + /* + * if (onclick != null && onkeypress == null) { occurrences.add(this.buildOccurrence("2.1", + * true, element.toString(), element, "1")); } + */ + if (onmouseover != null && onfocus == null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "2"));// "1")); } - - for(Element marquee : getDocument().getAllElements("marquee")) { - occurrences.add(this.buildOccurrence("2.5", true, marquee.toString(), marquee)); - hasMarquee = true; + if (onmouseout != null && onblur == null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "2"));// "1")); } - - if (!hasBlink && !hasMarquee){ - occurrences.add(new Occurrence("2.5", false, getDocument().getFirstElement().toString(),OccurrenceClassification.BEHAVIOR,"1")); + } + + String attributes[] = + {"onafterprint", "onbeforeprint", "onbeforeunload", "onerror", "onhashchange", "onload", + "onmessage", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", + "onresize", "onstorage", "onunloadnblur", "onblur", "onchange", "oncontextmenu", + "onfocus", "oninput", "oninvalid", "onreset", "onsearch", "onselect", "onsubmit", + "onkeydown", "onkeypress", "onkeyup", "onclick", "ondblclick", "ondrag", "ondragend", + "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "onmousedown", + "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onscroll", + "onwheel", "oncopy", "oncut", "onpaste", "onabort", "oncanplay", "oncanplaythrough", + "oncuechange", "ondurationchange", "onemptied", "onended", "onerror", "onloadeddata", + "onloadedmetadata", "onloadstart", "onpause", "onplay", "onplaying", "onprogress", + "onratechange", "onseeked", "onseeking", "onstalled", "onsuspend", "ontimeupdate", + "onvolumechange", "onwaiting", "onerror", "onshow", "ontoggle"}; + + for (Element element : getDocument().getAllElements()) { + + if (!isTagForm(element)) + if (element != null) { + for (String attribute : attributes) { + if (element.getAttributeValue(attribute) != null) { + occurrences.add(this.buildOccurrence("2.1", true, element.toString(), element, "8"));// "3")); + } + } + } + } + + return occurrences; + } + + + + private List checkRecommendation11() { + List occurrences = new ArrayList(); + + // boolean script = false; + // boolean contAlter = false; + + /* + * if(!getDocument().getAllElements("script").isEmpty()) script = true; + * + * if(script) if(getDocument().getAllElements("noscript").isEmpty()) + * occurrences.add(this.buildOccurrence("2.2", true, + * getDocument().getFirstElement("html").toString(), getDocument().getFirstElement("html"), + * "1")); + */ + + + + if (getDocument().getAllElements("script").size() > 0) { + if (getDocument().getAllElements("body").get(0).getAllElements("noscript") == null + || getDocument().getAllElements("body").get(0).getAllElements("noscript").size() <= 0) { + // occurrences.add(this.buildOccurrence("2.2", true, elementScript.toString(), + // elementScript, "1")); + + occurrences.add(this.buildOccurrence("2.2", true, + "Observação - Sem fonte (Não existe tag