Commit ba67768f042dae9666f1929bd7fa58cbe36cc337

Authored by rodrigo.lima
1 parent 9b1a8f05

git-svn-id: https://svn.bento.ifrs.edu.br/default/ASES/ASES%20-%20Web/ASES%20-%2…

…0Web/Fontes/avaliador-api@10513 c2178572-b5ca-4887-91d2-9e3a90c7d55b
src/main/java/br/com/checker/emag/Occurrence.java
... ... @@ -58,11 +58,11 @@ public @Setter @Getter class Occurrence implements Comparable<Occurrence>{
58 58  
59 59  
60 60 public boolean isCssEvaluation(){
61   - return this.tag!=null && this.tag.startsWith("www") && this.criterio.equals("2");
  61 + return (this.tag!=null && this.criterio.equals("2")) && (this.tag.startsWith("www") || this.tag.startsWith("http")) ;
62 62 }
63 63  
64 64 public boolean isHtmlEvaluation(){
65   - return this.tag!=null && this.tag.startsWith("www") && this.criterio.equals("1");
  65 + return (this.tag!=null && this.criterio.equals("1")) && (this.tag.startsWith("www") || this.tag.startsWith("http")) ;
66 66 }
67 67  
68 68 public String getTag() {
... ...
src/main/java/br/com/checker/emag/core/MarkEvaluation.java
... ... @@ -5,6 +5,7 @@ import java.util.Arrays;
5 5 import java.util.Collections;
6 6 import java.util.Comparator;
7 7 import java.util.List;
  8 +import java.util.regex.Matcher;
8 9 import java.util.regex.Pattern;
9 10  
10 11 import net.htmlparser.jericho.Attribute;
... ... @@ -18,13 +19,14 @@ import br.com.checker.emag.OccurrenceClassification;
18 19 import br.com.checker.emag.core.SpecificRecommendation.MarkRecommendation;
19 20 import br.com.checker.emag.util.WebAgent;
20 21  
21   -import com.jcabi.w3c.Defect;
22   -import com.jcabi.w3c.ValidationResponse;
23   -import com.jcabi.w3c.ValidatorBuilder;
  22 +import com.google.gson.Gson;
  23 +import com.google.gson.GsonBuilder;
24 24  
25 25 public class MarkEvaluation extends Evaluation {
26 26  
27   -
  27 + private static String CSS_VALIDATOR_URL = "http://www.css-validator.org/validator?uri=#{url}&warning=0&output=soap12";
  28 + private static String HTML_VALIDATOR_URL = "https://validator.w3.org/nu/?doc=#{url}&out=json";
  29 +
28 30 private MarkEvaluation(Source document) { super(document); }
29 31  
30 32 private MarkEvaluation(Source document,String url) {
... ... @@ -102,17 +104,28 @@ public class MarkEvaluation extends Evaluation {
102 104 return getOccurrences();
103 105 }
104 106  
  107 +
105 108 private List<Occurrence> checkRecommendation1() {
106 109 List<Occurrence> occurrences = new ArrayList<Occurrence>();
107 110  
108   - if(getUrl()!=null){
109   - String url = getUrl().substring(7);
110   - occurrences.add(buildOccurrence("1.1", false, url, getDocument().getFirstElement(), "1"));
111   - }
112 111  
113   - if(getUrl()!=null){
114   - String url = getUrl().substring(7);
115   - occurrences.add(buildOccurrence("1.1", false, url, getDocument().getFirstElement().getFirstElement(), "2"));
  112 + String url = getUrl();
  113 + if(url!=null){
  114 +
  115 + int[] errosWarningsCss = getErrorCount(true, url);
  116 + int[] errosWarningsHtml = getErrorCount(false, url);
  117 +
  118 + if(errosWarningsHtml[1] > 0)
  119 + occurrences.add(buildOccurrence("1.1", false, url, getDocument().getFirstElement(), "1"));
  120 +
  121 + if(errosWarningsCss[1] > 0)
  122 + occurrences.add(buildOccurrence("1.1", false, url, getDocument().getFirstElement().getFirstElement(), "2"));
  123 +
  124 + if(errosWarningsHtml[0] > 0)
  125 + occurrences.add(buildOccurrence("1.1", true, url, getDocument().getFirstElement(), "1"));
  126 +
  127 + if(errosWarningsCss[0] > 0)
  128 + occurrences.add(buildOccurrence("1.1", true, url, getDocument().getFirstElement().getFirstElement(), "2"));
116 129 }
117 130  
118 131 for (Element element : getDocument().getAllElements()) {
... ... @@ -770,5 +783,88 @@ public class MarkEvaluation extends Evaluation {
770 783 return super.buildOccurrence(code, error, tag, element, OccurrenceClassification.MARK,criterio);
771 784 }
772 785  
  786 + public int[] getErrorCount(boolean isCss,String url){
  787 + int errors = 0;
  788 + int warnings = 0;
  789 +
  790 + try{
  791 + if(isCss){
  792 + String content = WebAgent.from(CSS_VALIDATOR_URL.replace("#{url}", url)).withGetRequest().execute().getContent();
  793 + Matcher m = Pattern.compile("<m:errorcount>(\\d)*</m:errorcount>",Pattern.MULTILINE).matcher(content);
  794 + if(m.find())
  795 + errors = Integer.valueOf(m.group(0).replace("<m:errorcount>", "").replace("</m:errorcount>", ""));
  796 +
  797 + m = Pattern.compile("<m:warningcount>(\\d)*</m:warningcount>",Pattern.MULTILINE).matcher(content);
  798 +
  799 + if(m.find())
  800 + warnings = Integer.valueOf(m.group(0).replace("<m:warningcount>", "").replace("</m:warningcount>", ""));
  801 +
  802 + }else{
  803 +
  804 + String content = WebAgent.from(HTML_VALIDATOR_URL.replace("#{url}", url)).withGetRequest().execute().getContent();
  805 + Gson g = new GsonBuilder().create();
  806 + HtmlValidation a = g.fromJson(content, HtmlValidation.class);
  807 + int[] errorsWarnings = a.getQtdWarningsErros();
  808 + errors = errorsWarnings[1];
  809 + warnings = errorsWarnings[0];
  810 +
  811 + }
  812 + }catch(Exception e){
  813 + e.printStackTrace();
  814 + }
  815 +
  816 + return new int[]{errors,warnings};
  817 + }
  818 +
773 819 public OccurrenceClassification type () { return OccurrenceClassification.MARK;}
  820 +
  821 + public class HtmlValidation {
  822 +
  823 + List<Message> messages = new ArrayList<HtmlValidation.Message>();
  824 +
  825 +
  826 + public List<Message> getMessages() {
  827 + return messages;
  828 + }
  829 +
  830 + public void setMessages(List<Message> messages) {
  831 + this.messages = messages;
  832 + }
  833 +
  834 + public int[] getQtdWarningsErros(){
  835 + int warnings=0;
  836 + int erros = 0;
  837 + for(Message message: this.messages ){
  838 + if(message.isError())
  839 + erros++;
  840 + else if (message.isWarning())
  841 + warnings++;
  842 + }
  843 +
  844 + return new int[]{warnings,erros};
  845 + }
  846 +
  847 + public HtmlValidation(){}
  848 +
  849 + public class Message{
  850 +
  851 + private String type;
  852 +
  853 + private String subType;
  854 +
  855 + public String getType() { return type; }
  856 +
  857 + public void setType(String type) { this.type = type; }
  858 +
  859 + public String getSubType() { return subType; }
  860 +
  861 + public void setSubType(String subType) { this.subType = subType; }
  862 +
  863 + public boolean isWarning(){ return "info".equals(type) && "warning".equals(this.subType); }
  864 +
  865 + public boolean isError(){ return "error".equals(this.type);}
  866 +
  867 + }
  868 + }
  869 +
774 870 }
... ...