1 package org.jahia.clipbuilder.html.web.html.Impl.HTMLParser; 2 3 import org.jahia.clipbuilder.html.bean.*; 4 import javax.swing.text.html.HTML ; 5 import org.htmlparser.tags.*; 6 import org.htmlparser.Tag; 7 import org.jahia.clipbuilder.html.util.*; 8 import org.jahia.clipbuilder.html.web.Constant.*; 9 import org.jahia.clipbuilder.html.struts.Util.Constants; 10 import org.htmlparser.Node; 11 import org.htmlparser.util.NodeList; 12 import org.htmlparser.scanners.ScriptScanner; 13 14 19 public class UserBuilderVisitor extends TransformBuilderVisitor { 20 21 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(UserBuilderVisitor.class); 22 23 24 29 public UserBuilderVisitor(HTMLParserTransformer transformer) { 30 super(transformer); 31 ScriptScanner.STRICT = false; 32 } 33 34 35 40 public void visitStringNode(String t) { 41 if (t.toLowerCase().indexOf("onload") > -1) { 42 t = "waza"; 43 } 44 } 45 46 47 52 public void visitTag(Tag tag) { 53 if (tag instanceof Html) { 54 processHtmlTag(tag); 55 56 } 57 58 processTargetAtt(tag); 59 60 if (tag instanceof BaseHrefTag) { 61 processBaseHrefTag((BaseHrefTag) tag); 62 } 63 64 for (int i = 0; i < WebConstants.JAVASCRIPT_EVENT_NAMES_ARRAY.length; i++) { 66 String name = WebConstants.JAVASCRIPT_EVENT_NAMES_ARRAY[i]; 67 String value = tag.getAttribute(name); 68 if (value != null) { 69 String url = getTransformer().getDocument().getUrlBean().getRedirectUrl().toExternalForm(); 70 String newValue = getTransformer().getRefactoredJavascript(url, Constants.WEB_BROWSER_SHOW_BROWSE, value); 71 tag.setAttribute(name, newValue); 72 } 73 } 74 75 if (tag.getAttribute("src") != null) { 78 try { 79 processSrcAttribute(tag); 80 } 81 catch (WebClippingException ex) { 82 getTransformer().addParsingErrors(ex.getMessage()); 83 } 84 } 85 86 if (tag instanceof MetaTag) { 87 try { 88 processMetaTag((MetaTag) tag, Constants.WEB_BROWSER_SHOW_BROWSE); 89 } 90 catch (WebClippingException ex) { 91 ex.printStackTrace(); 92 getTransformer().addParsingErrors(ex.getMessage()); 93 } 94 } 95 96 if (tag instanceof BodyTag) { 98 106 } 107 else if (tag.getAttribute("href") != null) { 109 logger.debug("href att found, value is: " + tag.getAttribute("href")); 110 try { 111 112 nbLinks = processHrefAttribute(tag, nbLinks, Constants.WEB_BROWSER_SHOW_BROWSE); 113 logger.debug("Nb link = " + nbLinks); 114 115 } 116 catch (WebClippingException ex) { 117 getTransformer().addParsingErrors(ex.getMessage()); 118 } 119 120 } 121 else if (tag instanceof MetaTag) { 123 try { 124 processMetaTag((MetaTag) tag, Constants.WEB_BROWSER_SHOW_TEST); 125 } 126 catch (WebClippingException ex) { 127 getTransformer().addParsingErrors(ex.getMessage()); 128 } 129 } 130 131 else if (tag instanceof FormTag) { 133 try { 134 processFormTag((FormTag) tag, Constants.WEB_BROWSER_SHOW_TEST); 135 } 136 catch (WebClippingException ex) { 137 getTransformer().addParsingErrors(ex.getMessage()); 138 } 139 } 140 141 else if (tag instanceof InputTag) { 143 try { 144 processInputTag((InputTag) tag); 145 } 146 catch (WebClippingException ex) { 147 getTransformer().addParsingErrors(ex.getMessage()); 148 } 149 } 150 else if (tag instanceof SelectTag) { 151 processSelectTag((SelectTag) tag); 152 } 153 154 else if (tag instanceof StyleTag) { 156 try { 157 processStyleTag((StyleTag) tag); 158 } 159 catch (WebClippingException ex) { 160 getTransformer().addParsingErrors(ex.getMessage()); 161 } 162 } 163 164 else if (tag instanceof ScriptTag) { 166 logger.debug("[ Process script tag ]"); 167 if (getTransformer().isRemoveBodyScript() || getTransformer().isRemoveHeadScriptTag()) { 168 tag.setChildren(new NodeList()); 169 tag.removeAttribute("src"); 170 } 171 else { 172 try { 173 processScriptTag((ScriptTag) tag); 174 } 175 catch (WebClippingException ex) { 176 getTransformer().addParsingErrors(ex.getMessage()); 177 logger.error("Rewrite javascript error"); 178 ex.printStackTrace(); 179 } 180 } 181 } 182 183 } 184 185 186 195 196 protected int processHrefAttribute(Tag tag, int position, String webBrowserAction) throws WebClippingException { 197 String href = tag.getAttribute("href"); 199 String absoluteHref = href; 200 if (href == null) { 201 logger.error("there is no value for href attribut"); 202 } 203 try { 204 absoluteHref = getTransformer().relatifToAbsolute(href); 205 logger.debug("absolute url is: " + absoluteHref); 206 } 207 catch (Exception ex) { 208 logger.error("error has occured: " + ex.toString()); 209 } 211 finally { 212 int newposition = super.processHrefAttribute(tag, position, webBrowserAction); 214 if (!tag.getTagName().equalsIgnoreCase("meta") && !tag.getTagName().equalsIgnoreCase("link")) { 215 216 String continualClipping = getTransformer().getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping(); 217 int ccInt = Integer.parseInt(continualClipping); 219 220 switch (ccInt) { 222 case 223 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE: 224 225 tag.removeAttribute("href"); 227 break; 228 case 229 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP: 230 231 tag.setAttribute("target", "_blank"); 233 case 235 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP: 236 237 tag.setAttribute("target", "_blank"); 239 240 tag.setAttribute("href", absoluteHref); 242 case 243 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME: 244 break; 245 case 246 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME: 247 248 tag.setAttribute("href", absoluteHref); 250 break; 251 } 252 253 } 254 return newposition; 255 } 256 } 257 258 259 266 protected void processFormTag(FormTag tag, String webBrowserAction) throws WebClippingException { 267 String actionUrl = tag.getAttribute("action"); 269 String method = tag.getFormMethod(); 270 String encrypt = tag.getAttribute("enctype"); 271 String absoluteActionUrl = actionUrl; 272 try { 273 if (actionUrl == null) { 274 logger.warn("action att is null !!"); 275 String name = tag.getFormName(); 276 String id = tag.getAttribute("id"); 277 logger.warn("Name Form: " + name + ", Form id: " + id); 278 } 279 else { 280 absoluteActionUrl = getTransformer().relatifToAbsolute(actionUrl); 281 } 282 } 283 catch (Exception ex) { 284 logger.debug("error has occured: " + ex.toString()); 285 ex.printStackTrace(); 286 } 287 finally { 288 super.processFormTag(tag, webBrowserAction); 289 String continualClipping = getTransformer().getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping(); 290 int ccInt = Integer.parseInt(continualClipping); 291 switch (ccInt) { 292 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE: 293 break; 296 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP: 297 tag.setAttribute("target", "_blank"); 299 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP: 301 tag.setAttribute("target", "_blank"); 303 304 if (method != null) { 306 tag.setAttribute("method", method); 307 } 308 if (encrypt != null) { 309 tag.setAttribute("enctype", encrypt); 310 } 311 tag.setAttribute("action", absoluteActionUrl); 312 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME: 313 break; 314 case org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME: 315 if (method != null) { 318 tag.setAttribute("method", method); 319 } 320 if (encrypt != null) { 321 tag.setAttribute("enctype", encrypt); 322 } 323 tag.setAttribute("action", absoluteActionUrl); 324 break; 325 } 326 } 327 328 } 329 330 331 332 337 private void processHtmlTag(Tag tag) { 338 setFirstNode(tag); 339 } 340 341 342 345 351 356 private void processSelectTag(SelectTag tag) { 357 String name = tag.getAttribute(HTML.Attribute.NAME.toString()); 358 if (name == null) { 359 name = ""; 360 } 361 int paramPosition = getTransformer().getPosAndUpdateInputHash(name); 362 FormParamBean fBean = getTransformer().getFormParamBean(formParentName, formParentId, formParentPosition, name, paramPosition); 363 String formHash = HashUtilities.buildFormHash(formParentName, formParentId, formParentPosition); 364 365 if (fBean == null) { 366 logger.warn("FormParamBean not found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type")); 367 return; 368 } 369 370 logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type")); 371 logger.debug("Used value: " + fBean.getUsedValue()); 372 String usedValue = fBean.getUsedValue(); 373 374 NodeList it = tag.getChildren(); 376 for (int i = 0; i < it.size(); i++) { 377 Node childNode = it.elementAt(i); 378 if (childNode instanceof OptionTag) { 379 OptionTag op = (OptionTag) childNode; 380 String possibleValue = op.getValue(); 381 if (possibleValue.equalsIgnoreCase(usedValue)) { 382 op.setAttribute(HTML.Attribute.SELECTED.toString(), "selected"); 383 } 384 else { 385 op.removeAttribute(HTML.Attribute.SELECTED.toString()); 386 } 387 } 388 } 389 } 390 391 392 395 396 402 private void processInputTag(InputTag tag) throws WebClippingException { 403 logger.debug(" Input tag found"); 404 try { 405 String name = tag.getAttribute(HTML.Attribute.NAME.toString()); 406 407 if (name == null) { 408 name = ""; 409 } 410 int paramPosition = getTransformer().getPosAndUpdateInputHash(name); 412 FormParamBean fBean = getTransformer().getFormParamBean(formParentName, formParentId, formParentPosition, name, paramPosition); 413 String formHash = HashUtilities.buildFormHash(formParentName, formParentId, formParentPosition); 414 415 if (fBean == null) { 416 logger.warn("FormParamBean not found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type")); 417 return; 418 } 419 420 String fBeanUpdate = fBean.getUpdate(); 422 logger.debug("Param: " + fBean.getName() + " whith type: " + fBean.getType() + " update: " + fBeanUpdate); 423 if (fBeanUpdate.equalsIgnoreCase("true")) { 424 String inputValue = tag.getAttribute("value"); 425 getTransformer().updateInputValue(fBean, inputValue); 426 logger.debug("Updated"); 427 return; 428 } 429 logger.debug("Not Updated"); 430 logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + tag.getAttribute("type")); 431 432 logger.debug("Used value: " + fBean.getUsedValue()); 433 435 } 436 catch (Exception ex) { 437 throw new WebClippingException("Parser, Input Element", ex); 438 } 439 finally { 440 return; 441 } 442 443 } 444 445 } 446 | Popular Tags |