Commit afb947df768d9533391a87b6e52e77bad1f3963f

Authored by lyandro.santana
1 parent 334aed3a

Apresentação

git-svn-id: https://svn.bento.ifrs.edu.br/default/ASES/ASES%20-%20Web/ASES%20-%20Web/Fontes/avaliador-api@10497 c2178572-b5ca-4887-91d2-9e3a90c7d55b
src/main/java/br/com/checker/emag/core/PresentationEvaluation.java
... ... @@ -68,168 +68,131 @@ public class PresentationEvaluation extends Evaluation{
68 68 private List<Occurrence> checkRecommendation29() {
69 69 List<Occurrence> occurrences = new ArrayList<Occurrence>();
70 70  
  71 + String[] attributes = {"class","id", "bgcolor"};
71 72 AvaliadorContraste avaliadorContraste = new AvaliadorContraste();
72   - Color color = null;
73 73  
74   - int foreground = Integer.parseInt("000000", 16);
75   - int background = Integer.parseInt("ffffff", 16);
  74 + for (Element body : getDocument().getAllElements("body")) {
  75 + for (Element element : body.getAllElements()) {
76 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");
  77 + Color foreground = null;
  78 + Color background = null;
  79 + boolean achou_foreground = false;
  80 + boolean achou_background = false;
96 81  
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);
  82 + if(element != null ){
110 83  
111   - avaliadorContraste.setCores(new Color(background), new Color(foreground));
112   - avaliadorContraste.avaliar();
  84 + for (String attribute : attributes) {
  85 + //System.out.println("Verificando: "+attribute);
113 86  
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")) {
  87 + //Verificar IN-LINE
  88 + String styleInline = element.getAttributeValue("style");
  89 + if(styleInline != null){
  90 +
  91 + foreground = getColor(styleInline, "color:(.*?)\\;");
  92 + background = getColor(styleInline, "background:(.*?)\\;");
  93 + background = getColor(styleInline, "background-color:(.*?)\\;");
  94 +
152 95  
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;
  96 + if(foreground != null) achou_foreground = true;
  97 + if(background != null) achou_background = true;
  98 +
  99 +
  100 + }
  101 +
  102 + if(achou_foreground== false || achou_background == false){
  103 +
  104 + String valor_class = element.getAttributeValue("class");
  105 +
  106 + if(valor_class != null){
  107 +
  108 + //Verificar css interno
  109 + for (Element style : getDocument().getAllElements("style")){
  110 + foreground = getColor(style.toString(), "color:(.*?)\\;");
  111 + background = getColor(style.toString(), "background:(.*?)\\;");
  112 + background = getColor(style.toString(), "background-color:(.*?)\\;");
  113 +
168 114  
169   - avalia = getUrl()!=null || href.startsWith("http");
  115 + if(foreground != null) achou_foreground = true;
  116 + if(background != null) achou_background = true;
  117 + }
  118 +
  119 + if(achou_foreground== false || achou_background == false){
  120 +
  121 + //verificar css externo
  122 + String href = null;
  123 + boolean avalia = false;
  124 + for(Element link : getDocument().getAllElements("link")) {
  125 + href = link.getAttributeValue("href");
  126 + if(href.startsWith("www"))href= "http://"+href;
  127 +
  128 + avalia = getUrl()!=null || href.startsWith("http");
  129 +
  130 + if(href.contains(".css") && avalia) {
  131 +
  132 +
  133 + if(!href.startsWith("http")) href = getUrl()+"/"+href;
  134 +
  135 +
  136 + String content = WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent();
  137 + foreground = getColor(content, "color:(.*?)\\;");
  138 + background = getColor(content, "background:(.*?)\\;");
  139 + background = getColor(content, "background-color:(.*?)\\;");
  140 +
170 141  
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   - }
  142 + if(foreground != null) achou_foreground = true;
  143 + if(background != null) achou_background = true;
  144 +
  145 + }
  146 + }
  147 + }
  148 +
  149 + }
  150 + }
  151 +
  152 +
  153 + if(achou_foreground== true && achou_background == true){
  154 +
  155 + avaliadorContraste.setCores(foreground, background);
  156 + avaliadorContraste.avaliar();
  157 +
  158 + if(avaliadorContraste.getContraste() < new Double("4.5")){
  159 + occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1"));
  160 + break;
  161 + }
  162 +
  163 +
  164 + }else if(achou_foreground== true && achou_background == false){
  165 +
  166 + avaliadorContraste.setCores(foreground, new Color(Integer.parseInt("ffffff", 16)));
  167 + avaliadorContraste.avaliar();
  168 +
  169 + if(avaliadorContraste.getContraste() < new Double("4.5")){
  170 + occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1"));
  171 + break;
  172 + }
  173 +
  174 +
  175 + }else if(achou_foreground== false && achou_background == true){
  176 +
  177 + avaliadorContraste.setCores(new Color(Integer.parseInt("000000", 16)), background);
  178 + avaliadorContraste.avaliar();
  179 +
  180 + if(avaliadorContraste.getContraste() < new Double("4.5")){
  181 + occurrences.add(buildOccurrence("4.1", true, element.toString(), element,"1"));
  182 + break;
  183 + }
  184 +
  185 + }
  186 +
  187 + }
  188 + }
180 189 }
181 190 }
182 191  
183 192  
184 193  
  194 + /*AvaliadorContraste avaliadorContraste = new AvaliadorContraste();
185 195  
186   - /*boolean temBgcolor = false;
187   - for (Element bgcolor : getDocument().getAllElements("body")) {
188   - Attribute bgc = bgcolor.getAttributes().get("bgcolor");
189   - if(bgc != null){
190   -
191   - background = Integer.parseInt(bgc.getValue().replaceFirst("^#",""), 16);
192   -
193   - avaliadorContraste.setCores(new Color(background), new Color(foreground));
194   - avaliadorContraste.avaliar();
195   -
196   - if(avaliadorContraste.getContraste() < new Double("4.5"))
197   - occurrences.add(buildOccurrence("4.1", true, bgcolor.toString(), bgcolor,"3"));
198   -
199   - temBgcolor = true;
200   - }
201   - }
202   -
203   - for (Element bgcolor : getDocument().getAllElements("table")) {
204   - Attribute bgc = bgcolor.getAttributes().get("bgcolor");
205   - if(bgc != null){
206   -
207   - int rgb = Integer.parseInt(bgc.getValue().replaceFirst("^#",""), 16);
208   - color = new Color(rgb);
209   - avaliadorContraste.setCores(color, color);
210   - avaliadorContraste.avaliar();
211   -
212   - if(avaliadorContraste.getContraste() < new Double("4.5"))
213   - occurrences.add(buildOccurrence("4.1", true, bgcolor.toString(), bgcolor,"1"));
214   -
215   - temBgcolor = true;
216   - }
217   - }
218   - for (Element bgcolor : getDocument().getAllElements("td")) {
219   - Attribute bgc = bgcolor.getAttributes().get("bgcolor");
220   - if(bgc != null){
221   -
222   - int rgb = Integer.parseInt(bgc.getValue().replaceFirst("^#",""), 16);
223   - color = new Color(rgb);
224   - avaliadorContraste.setCores(color, color);
225   - avaliadorContraste.avaliar();
226   -
227   - if(avaliadorContraste.getContraste() < new Double("4.5"))
228   - occurrences.add(buildOccurrence("4.1", true, bgcolor.toString(), bgcolor,"1"));
229   -
230   - temBgcolor = true;
231   - }
232   - }
233 196 for (Element bgcolor : getDocument().getAllElements("tr")) {
234 197 Attribute bgc = bgcolor.getAttributes().get("bgcolor");
235 198 if(bgc != null){
... ... @@ -248,11 +211,50 @@ public class PresentationEvaluation extends Evaluation{
248 211  
249 212 /*if(!temBgcolor)
250 213 occurrences.add(new Occurrence("4.1", false, getDocument().getFirstElement().toString(),OccurrenceClassification.PRESENTATION_DESIGN));*/
  214 +
  215 +
251 216 Collections.sort(occurrences);
252 217 return occurrences;
253 218  
254 219 }
255 220  
  221 +
  222 + private Color getColor(String css, String pattern){
  223 +
  224 + Color color = null;
  225 +
  226 + Pattern value = Pattern.compile(pattern);
  227 +
  228 + Matcher match = value.matcher(css);
  229 + while (match.find()){
  230 + String cor = match.group(1);
  231 +
  232 + System.out.println(cor);
  233 + try {
  234 + if(!isTextCor(match.group(1)) && !cor.contains("url")){
  235 +
  236 + if(cor.contains("rgb")){
  237 + //System.out.println("RGB: "+match.group(1));
  238 +
  239 + String str = cor.replaceAll("[^-?0-9]+", " ");
  240 + color = new Color(
  241 + Integer.parseInt(str.trim().split(" ")[0]),
  242 + Integer.parseInt(str.trim().split(" ")[1]),
  243 + Integer.parseInt(str.trim().split(" ")[2]));
  244 + }else{
  245 + //System.out.println("EXADECIMAL: "+match.group(1));
  246 + color = new Color(Integer.parseInt(match.group(1).trim().replaceFirst("^#",""), 16));
  247 + }
  248 + }
  249 + } catch (Exception e) {
  250 + return null;
  251 + }
  252 + }
  253 +
  254 + return color;
  255 + }
  256 +
  257 +
256 258 private boolean isTextCor(String cor){
257 259 return cor.matches("[a-zA-Z\\s]+");
258 260 }
... ... @@ -289,7 +291,7 @@ public class PresentationEvaluation extends Evaluation{
289 291 Integer.parseInt(str.trim().split(" ")[1]),
290 292 Integer.parseInt(str.trim().split(" ")[2]));
291 293  
292   - }else foregroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  294 + }else if(isTextCor(cor.trim().replaceFirst("^#","")))foregroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
293 295  
294 296 }else isProces = false;
295 297 }
... ... @@ -310,7 +312,7 @@ public class PresentationEvaluation extends Evaluation{
310 312 Integer.parseInt(str.trim().split(" ")[0]),
311 313 Integer.parseInt(str.trim().split(" ")[1]),
312 314 Integer.parseInt(str.trim().split(" ")[2]));
313   - }else backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  315 + }else if(isTextCor(cor.trim().replaceFirst("^#",""))) backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
314 316  
315 317 }else isProces = false;
316 318  
... ... @@ -333,7 +335,7 @@ public class PresentationEvaluation extends Evaluation{
333 335 Integer.parseInt(str.trim().split(" ")[0]),
334 336 Integer.parseInt(str.trim().split(" ")[1]),
335 337 Integer.parseInt(str.trim().split(" ")[2]));
336   - }else backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
  338 + }else if(isTextCor(cor.trim().replaceFirst("^#",""))) backgroundColor = new Color(Integer.parseInt(cor.trim().replaceFirst("^#",""), 16));
337 339  
338 340 }else isProces = false;
339 341 }
... ... @@ -396,7 +398,7 @@ public class PresentationEvaluation extends Evaluation{
396 398  
397 399 if(href.contains(".css") && avalia) {
398 400  
399   - if(!href.startsWith("http")) href = getUrl()+href;
  401 + if(!href.startsWith("http")) href = getUrl()+"/"+href;
400 402  
401 403 String content = WebAgent.from(href.replace(" ", "%20")).withGetRequest().execute().getContent();
402 404  
... ...