Commit 74b2e5baf2a89616623d666c07e87435660828d5

Authored by lyandro.santana
1 parent 5e5608c1

Marcação 06-08-15

git-svn-id: https://svn.bento.ifrs.edu.br/default/ASES/ASES%20-%20Web/ASES%20-%20Web/Fontes/avaliador-api@10338 c2178572-b5ca-4887-91d2-9e3a90c7d55b
src/main/java/br/com/checker/emag/core/Evaluation.java
... ... @@ -61,6 +61,13 @@ public abstract class Evaluation {
61 61 return new Occurrence(line, column, code, error, tag,type);
62 62 }
63 63  
  64 + protected Occurrence buildOccurrence(int line, int column, String code, boolean error,
  65 + String tag,
  66 + OccurrenceClassification type,
  67 + String criterio) {
  68 + return new Occurrence( line, column, code, error, tag, type, criterio);
  69 + }
  70 +
64 71 public abstract OccurrenceClassification type ();
65 72 public abstract List<Occurrence> check();
66 73  
... ...
src/main/java/br/com/checker/emag/core/MarkEvaluation.java
... ... @@ -4,7 +4,9 @@ import java.util.ArrayList;
4 4 import java.util.Arrays;
5 5 import java.util.Collections;
6 6 import java.util.Comparator;
  7 +import java.util.HashMap;
7 8 import java.util.List;
  9 +import java.util.Map;
8 10 import java.util.regex.Pattern;
9 11  
10 12 import net.htmlparser.jericho.Attribute;
... ... @@ -13,6 +15,7 @@ import net.htmlparser.jericho.Element;
13 15 import net.htmlparser.jericho.Source;
14 16  
15 17 import org.apache.commons.lang3.StringUtils;
  18 +import org.apache.http.HttpResponse;
16 19  
17 20 import br.com.checker.emag.Occurrence;
18 21 import br.com.checker.emag.OccurrenceClassification;
... ... @@ -20,6 +23,7 @@ import br.com.checker.emag.core.SpecificRecommendation.MarkRecommendation;
20 23  
21 24 import com.jcabi.w3c.Defect;
22 25 import com.jcabi.w3c.ValidationResponse;
  26 +import com.jcabi.w3c.Validator;
23 27 import com.jcabi.w3c.ValidatorBuilder;
24 28  
25 29 public class MarkEvaluation extends Evaluation {
... ... @@ -102,14 +106,16 @@ public class MarkEvaluation extends Evaluation {
102 106  
103 107 if (!response.warnings().isEmpty()){
104 108 for(Defect warn :response.warnings()) {
105   - occurrences.add(buildOccurrence("1.1", false,warn.line(),warn.column(),OccurrenceClassification.MARK,"1"));
  109 + occurrences.add(buildOccurrence(warn.line(), warn.column(), "1.1", false, warn.source().toString().isEmpty() ? warn.message() : "["+warn.message()+"] "+warn.source(), OccurrenceClassification.MARK,"1"));
  110 + //occurrences.add(buildOccurrence("1.1", false,warn.line(),warn.column(),OccurrenceClassification.MARK,"1"));
106 111 //occurrences.add(this.buildOccurrence("1.1", false, warn.source().toString().isEmpty() ? warn.message() : warn.source(), getDocument().getFirstElement(), "1"));
107 112 }
108 113 }
109 114  
110 115 if(!response.errors().isEmpty()){
111 116 for(Defect error :response.errors()) {
112   - occurrences.add(buildOccurrence("1.1", true,error.line(),error.column(),OccurrenceClassification.MARK,"1"));
  117 + occurrences.add(buildOccurrence(error.line(), error.column(), "1.1", true, error.source().toString().isEmpty() ? error.message() : "["+error.message()+"] "+error.source(), OccurrenceClassification.MARK,"1"));
  118 + //occurrences.add(buildOccurrence("1.1", true,error.line(),error.column(),OccurrenceClassification.MARK,"1"));
113 119 //occurrences.add(this.buildOccurrence("1.1", true, error.source().toString().isEmpty() ? error.message() : error.source(), getDocument().getFirstElement(), "1"));
114 120 }
115 121 }
... ... @@ -134,12 +140,14 @@ public class MarkEvaluation extends Evaluation {
134 140  
135 141  
136 142 for(Defect warn :response.warnings()) {
137   - occurrences.add(buildOccurrence("1.1", false,warn.line(),warn.column(),OccurrenceClassification.MARK,"2"));
  143 + //occurrences.add(buildOccurrence("1.1", false,warn.line(),warn.column(),OccurrenceClassification.MARK,"2"));
  144 + occurrences.add(buildOccurrence(warn.line(), warn.column(), "1.1", false, warn.source().toString().isEmpty() ? warn.message() : "["+warn.message()+"] "+warn.source(), OccurrenceClassification.MARK,"2"));
138 145 //occurrences.add(this.buildOccurrence("1.1", false, warn.source(), getDocument().getFirstElement(), "2"));
139 146 }
140 147  
141 148 for(Defect error :response.errors()) {
142   - occurrences.add(buildOccurrence("1.1", false,error.line(),error.column(),OccurrenceClassification.MARK,"2"));
  149 + //occurrences.add(buildOccurrence("1.1", false,error.line(),error.column(),OccurrenceClassification.MARK,"2"));
  150 + occurrences.add(buildOccurrence(error.line(), error.column(), "1.1", true, error.source().toString().isEmpty() ? error.message() : "["+error.message()+"] "+error.source(), OccurrenceClassification.MARK,"2"));
143 151 ///occurrences.add(this.buildOccurrence("1.1", true, error.source().toString().isEmpty() ? error.message() : error.source(), getDocument().getFirstElement(), "2"));
144 152 }
145 153  
... ... @@ -291,8 +299,8 @@ public class MarkEvaluation extends Evaluation {
291 299  
292 300 //CRITERIO 2
293 301  
294   - //String[] tags = {"h6","h5","h4","h3","h2","h1"};
295   - String[] tags = {"h1","h2","h3","h4","h5","h6"};
  302 + /*String[] tags = {"h6","h5","h4","h3","h2","h1"};
  303 + //String[] tags = {"h1","h2","h3","h4","h5","h6"};
296 304  
297 305  
298 306 for(int index = 0 ; index< tags.length ; index++){
... ... @@ -301,12 +309,32 @@ public class MarkEvaluation extends Evaluation {
301 309 if(!hasCorrectHierarchy(index, tags))
302 310 occurrences.add(this.buildOccurrence("1.3", true,h.toString(), h, "2"));
303 311 }
304   - }
  312 + }*/
  313 +
305 314  
306   - if(!hasH())
307   - occurrences.add(this.buildOccurrence("1.3", true,"Sem fonte (os níveis de título não foram utilizados)", getDocument().getFirstElement(), "1"));
308 315  
  316 + List<Element> elementsObj = getDocument().getAllElements();
  317 + List<String> tagsH = new ArrayList<String>();
  318 + List<Element> elementTag = new ArrayList<Element>();
  319 +
  320 + for (Element htmlElement : elementsObj) {
  321 + if (htmlElement.getName().matches("h[1-6]")) {
  322 + tagsH.add(htmlElement.getName());
  323 + elementTag.add(htmlElement);
  324 + }
  325 + }
  326 +
  327 + for (int i = 1; i < tagsH.size(); i++) {
  328 + int atual = Integer.parseInt(tagsH.get(i).substring(1));
  329 + int anterior = Integer.parseInt(tagsH.get(i-1).substring(1));
  330 +
  331 + if(!tagsH.get(i).toString().equals(tagsH.get(i-1).toString()))
  332 + if(anterior!=(atual-1))
  333 + occurrences.add(this.buildOccurrence("1.3", true,elementTag.get(i).toString(), elementTag.get(i), "2"));
  334 + }
309 335  
  336 + if(!hasH())
  337 + occurrences.add(this.buildOccurrence("1.3", true,"Observa&ccedil;&atilde;o - Sem fonte (os n&iacute;veis de t&iacute;tulo n&atilde;o foram utilizados)", getDocument().getFirstElement(), "1"));
310 338  
311 339 //Sorting
312 340 Collections.sort(occurrences, new Comparator<Occurrence>() {
... ... @@ -319,6 +347,15 @@ public class MarkEvaluation extends Evaluation {
319 347 return occurrences;
320 348 }
321 349  
  350 + private int verificarNiveis(Integer posicao, List<String> tags){
  351 + for (int i = 0; i < posicao; i++) {
  352 + if(Integer.parseInt( tags.get(i).substring(1)) == Integer.parseInt(tags.get(posicao).substring(1))-1){
  353 + return Integer.parseInt(tags.get(i).substring(1));
  354 + }
  355 + }
  356 + return -1;
  357 + }
  358 +
322 359 private boolean hasCorrectHierarchy(int index,String[] tags) {
323 360  
324 361 for(int i = index+1;i<tags.length;i++ ){
... ... @@ -383,7 +420,7 @@ public class MarkEvaluation extends Evaluation {
383 420 occurrences.add(this.buildOccurrence("1.4", false,textarea.toString(), textarea, "3"));
384 421 }
385 422  
386   - Element section = this.getDocument().getFirstElement("section");
  423 + /*Element section = this.getDocument().getFirstElement("section");
387 424  
388 425 if(section !=null){
389 426  
... ... @@ -392,8 +429,23 @@ public class MarkEvaluation extends Evaluation {
392 429 if(this.getRow(nav) < firstSectionRow)
393 430 occurrences.add(this.buildOccurrence("1.4", false,nav.toString(), nav, "1"));
394 431 }
  432 + }*/
  433 +
  434 +
  435 +
  436 + for(Element nav : this.getDocument().getAllElements("nav")){
  437 + if(nav !=null){
  438 + int firstNavRow = this.getRow(nav);
  439 + for(Element section : this.getDocument().getAllElements("section")){
  440 + if(firstNavRow < this.getRow(section))
  441 + occurrences.add(this.buildOccurrence("1.4", false,section.getStartTag().toString(), section, "1"));
  442 + }
  443 + }
395 444 }
396 445  
  446 +
  447 +
  448 +
397 449 return occurrences;
398 450 }
399 451  
... ... @@ -411,9 +463,9 @@ public class MarkEvaluation extends Evaluation {
411 463 private List<Occurrence> checkRecommendation5() {
412 464 List<Occurrence> occurrences = new ArrayList<Occurrence>();
413 465  
414   - Element firstLink = getDocument().getFirstElement("href", Pattern.compile(".*"));
  466 + Element firstLink = getDocument().getFirstElement("href", Pattern.compile(".*#.*"));
415 467  
416   - if(firstLink != null && firstLink.getAttributeValue("href").substring(0,1).equals("#"))
  468 + if(firstLink != null)
417 469 occurrences.add(this.buildOccurrence("1.5", false, firstLink.toString(), firstLink, "4"));
418 470  
419 471 /*if(firstLink == null){
... ... @@ -479,9 +531,25 @@ public class MarkEvaluation extends Evaluation {
479 531  
480 532 for (Element link : getDocument().getAllElements("a")) {
481 533 href = link.getAttributeValue("href");
482   - if(href != null && href.contains("#"))
483   - if(!hasAnchor(href.substring(1)))
  534 +
  535 + /*if(href != null && (href.length() > 1 && !href.substring(0,2).equals("?#"))){
  536 + ///occurrences.add(this.buildOccurrence("1.5", true, link.toString(), link, "2"));
  537 + if(!(href.length()==1 && href.substring(1).equals("#")))
  538 + if(href.substring(1).equals("#") && !this.hasLinkSemAncora(href))
  539 + occurrences.add(this.buildOccurrence("1.5", true, link.toString(), link, "2"));
  540 + }*/
  541 +
  542 + if(href != null && href.length() > 1 && !href.substring(0,2).equals("?#")){
  543 + if(href.substring(0,1).equals("#"))
  544 + if(!this.hasLinkRefAncora(link))
484 545 occurrences.add(this.buildOccurrence("1.5", true, link.toString(), link, "2"));
  546 + }
  547 +
  548 +
  549 +
  550 + /*if(href != null && href.contains("#"))
  551 + if(!hasAnchor(href.substring(1)))
  552 + occurrences.add(this.buildOccurrence("1.5", true, link.toString(), link, "2"));*/
485 553 /*}else{
486 554 occurrences.add(this.buildOccurrence("1.5", true, link.toString(), link, "1"));
487 555 }*/
... ... @@ -501,10 +569,10 @@ public class MarkEvaluation extends Evaluation {
501 569 }
502 570  
503 571 if(!existAnchor)
504   - occurrences.add(this.buildOccurrence("1.5", true, "Sem código fonte a ser exibido. Não existe(m) âncora(s) na página", getDocument().getFirstElement(), "1"));
  572 + occurrences.add(this.buildOccurrence("1.5", true, "Observa&ccedil;&atilde;o - Sem fonte (n&atilde;o foram encontrados &acirc;ncoras que permitam saltar pelas diferentes se&ccedil;&otilde;es da p&aacute;gina)", getDocument().getFirstElement(), "1"));
505 573  
506 574 if(!existAcessKey)
507   - occurrences.add(this.buildOccurrence("1.5", true, "Sem código fonte a ser exibido. Não existe(m) accesskey(s) na página", getDocument().getFirstElement(), "3"));
  575 + occurrences.add(this.buildOccurrence("1.5", true, "Observa&ccedil;&atilde;o - Sem fonte ( n&atilde;o existe(m) accesskey(s) na página)", getDocument().getFirstElement(), "3"));
508 576  
509 577 for (Element elemento : getDocument().getAllElements("accesskey", Pattern.compile(".*"))) {
510 578 if(duplicatedAcessKey(elemento))
... ... @@ -514,6 +582,20 @@ public class MarkEvaluation extends Evaluation {
514 582 return occurrences;
515 583 }
516 584  
  585 + private boolean hasLinkRefAncora(Element link) {
  586 + boolean existeAncora = false;
  587 + String href = link.getAttributeValue("href");
  588 + String ancora = href.substring(1, href.length());
  589 +
  590 + for (Element a : getDocument().getAllElements("a")){
  591 + if(a.getAttributeValue("name") != null && a.getAttributeValue("name").equals(ancora)){
  592 + existeAncora = true;
  593 + }
  594 + }
  595 +
  596 + return existeAncora;
  597 + }
  598 +
517 599 private boolean hasAcessKey(Element element) {
518 600 String acessKey = element.getAttributeValue("accesskey");
519 601  
... ... @@ -551,12 +633,26 @@ public class MarkEvaluation extends Evaluation {
551 633 List<Occurrence> occurrences = new ArrayList<Occurrence>();
552 634  
553 635 for (Element table : getDocument().getAllElements("table"))
554   - occurrences.add(this.buildOccurrence("1.6", false, table.getStartTag()+"</table>", table, "1"));
  636 + occurrences.add(this.buildOccurrence("1.6", false, table.getStartTag().toString(), table, "1"));
  637 +
  638 +
  639 + int firstFormRow = 0;
  640 +
  641 + List<Integer> linhasOcorrForm = new ArrayList<Integer>();
555 642  
556 643 for (Element table : getDocument().getAllElements("table")){
557   - Element tagsForm = table.getFirstElement("form");
558   - if(tagsForm != null)
559   - occurrences.add(this.buildOccurrence("1.6", true, table.getStartTag()+"</table>", table, "2"));
  644 + //Element tagsForm = table.getFirstElement("form");
  645 +
  646 + Element form = table.getContent().getFirstElement("form");
  647 +
  648 + if(form != null){
  649 + firstFormRow = this.getRow(form);
  650 + if(!linhasOcorrForm.contains(firstFormRow)){
  651 + occurrences.add(this.buildOccurrence("1.6", true, form.getStartTag().toString(), form, "2"));
  652 + linhasOcorrForm.add(firstFormRow);
  653 + }
  654 + }
  655 +
560 656 }
561 657  
562 658 return occurrences;
... ... @@ -599,16 +695,16 @@ public class MarkEvaluation extends Evaluation {
599 695 Element footer = getDocument().getFirstElement("footer");
600 696  
601 697 if(header == null)
602   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe tag <HEADER>", getDocument().getFirstElement(),"1"));
  698 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe tag <HEADER>)", getDocument().getFirstElement(),"1"));
603 699  
604 700 if(nav == null)
605   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe tag <NAV>", getDocument().getFirstElement(),"2"));
  701 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe tag <NAV>)", getDocument().getFirstElement(),"1"));
606 702  
607 703 if(section == null)
608   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe tag <SECTION>", getDocument().getFirstElement(),"3"));
  704 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe tag <SECTION>)", getDocument().getFirstElement(),"1"));
609 705  
610 706 if(footer == null)
611   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe tag <FOOTER>", getDocument().getFirstElement(),"4"));
  707 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe tag <FOOTER>)", getDocument().getFirstElement(),"1"));
612 708  
613 709  
614 710 boolean hasBanner = false;
... ... @@ -631,13 +727,13 @@ public class MarkEvaluation extends Evaluation {
631 727 }
632 728  
633 729 if(!hasBanner)
634   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe LANDMARKS na página - banner", getDocument().getFirstElement(),"1"));
  730 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe LANDMARKS na p&aacute;gina - banner)", getDocument().getFirstElement(),"2"));
635 731  
636 732 if(!hasNavigation)
637   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe LANDMARKS na página - navigation", getDocument().getFirstElement(),"6"));
  733 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe LANDMARKS na p&aacute;gina - navigation)", getDocument().getFirstElement(),"2"));
638 734  
639 735 if(!hasMain)
640   - occurrences.add(this.buildOccurrence("1.8", false, "Não existe LANDMARKS na página - main", getDocument().getFirstElement(),"7"));
  736 + occurrences.add(this.buildOccurrence("1.8", false, "Observa&ccedil;&atilde;o - Sem fonte (N&atilde;o existe LANDMARKS na p&aacute;gina - main)", getDocument().getFirstElement(),"2"));
641 737 }
642 738  
643 739  
... ...