1 package org.jahia.clipbuilder.html.web.html.Impl; 2 import java.util.*; 3 import java.util.Map ; 4 5 import javax.swing.text.html.*; 6 7 import org.jahia.clipbuilder.html.bean.*; 8 import org.jahia.clipbuilder.html.util.*; 9 import org.jahia.clipbuilder.html.web.Constant.*; 10 import org.jahia.clipbuilder.html.web.Url.*; 11 import org.jahia.clipbuilder.html.web.html.*; 12 import org.jahia.clipbuilder.html.web.html.Impl.ExtractorFilter.*; 13 import org.jahia.clipbuilder.html.web.HTMLDocumentBuilder; 14 15 20 public abstract class DefaultHTMLTransformer implements HTMLTransformer { 21 22 private List lastParsingErrors = new ArrayList(); 23 private boolean enableCSS; 24 private Hashtable inputNameHash = new Hashtable(); 25 private boolean removeMetaTag; 26 private boolean removeHeadScriptTag; 27 private boolean removeBodyScript; 28 private boolean removeJavascriptEvent; 29 private boolean refactorJavascriptCode; 30 private boolean refactorJavascriptEvent; 31 private HTMLDocumentBuilder builder; 32 33 36 public static String PARAM_CHEW_WEBCLIPPING_ACTION = "webClippingAction=chewPreview"; 37 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DefaultHTMLTransformer.class); 38 private static String [] notEditableParameter = {WebConstants.TYPE_HIDDEN, WebConstants.TYPE_FILE, WebConstants.TYPE_SUBMIT}; 40 41 42 43 49 public DefaultHTMLTransformer(HTMLDocumentBuilder builder, boolean enableCSS) { 50 enableCSS(enableCSS); 51 this.setBuilder(builder); 52 } 53 54 55 60 public void setBuilder(HTMLDocumentBuilder builder) { 61 this.builder = builder; 62 } 63 64 65 70 public UrlEncoderIF getUrlEncoder() { 71 return getBuilder().getUrlEncoder(); 72 } 73 74 75 76 82 public String getHrefChewLink(String hash) { 83 String paramWebClipping = PARAM_CHEW_WEBCLIPPING_ACTION; 84 return getUrlEncoder().getLocalUrl("/selectPart.do?" + paramWebClipping + "&" + ChewExtractorFilter.getHashParamName() + "=" + hash); 86 } 87 88 89 94 public String getImageChewLinkSrcValue() { 95 return getUrlEncoder().getLocalUriResource("/jsp/clipbuilder/html/images/select.gif"); 97 } 98 99 100 105 public boolean isEnableCSS() { 106 return this.enableCSS; 107 } 108 109 110 114 119 123 128 131 139 150 public String getRewritedHrefValue(String ownerTag, String href, String id, String position, String webBrowserAction) throws Exception { 151 if (!isRewritableUrl(href)) { 153 return href; 154 } 155 156 String from = ""; 158 String hash = ""; 159 if (ownerTag.equalsIgnoreCase(HTML.Tag.A.toString())) { 160 from = WebConstants.FROM_LINK; 161 hash = HashUtilities.buildLinkHash(href, id, position); 162 163 } 164 else if (ownerTag.equalsIgnoreCase(HTML.Tag.AREA.toString())) { 165 from = WebConstants.FROM_AREA; 166 hash = HashUtilities.buildLinkHash(href, id, position); 167 } 168 else if (ownerTag.equalsIgnoreCase(HTML.Tag.META.toString())) { 169 from = WebConstants.FROM_META; 170 hash = HashUtilities.buildLinkHash(href, id, position); 171 } 172 else if (ownerTag.equalsIgnoreCase("link")) { 173 return href = URLUtilities.getUrlAbsoluteValue(getUrlBean().getRedirectUrl().toExternalForm(), href); 174 } 175 176 if (isAnUrl(href)) { 178 String mimeType = ""; 180 URLWrapper wrapper = new URLWrapper(href, from, "GET", mimeType, hash); 181 182 UrlEncoderIF encoder = getUrlEncoder(); 185 encoder.setAction(webBrowserAction); 186 encoder.setUrlWrapper(wrapper); 187 return href = encoder.getEncodedUrl(); 188 } 189 190 href = URLUtilities.getUrlAbsoluteValue(getUrlBean().getRedirectUrl().toExternalForm(), href); 192 return href; 193 } 194 195 196 197 207 public FormParamBean getFormParamBean(String formParentName, String formParentId, int formParentPos, String name, int paramPosition) { 208 FormParamBean fBean = getUrlBean().getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, name, paramPosition); 209 return fBean; 210 } 211 212 213 220 public int getPosAndUpdateInputHash(String name) { 221 Integer pos = (Integer ) inputNameHash.get(name); 222 if (pos == null) { 223 pos = new Integer (0); 224 225 } 226 else { 227 pos = new Integer (pos.intValue() + 1); 228 } 229 inputNameHash.put(name, pos); 230 return pos.intValue(); 231 } 232 233 234 235 249 public String getRewritedActionValue(String action, String method, String formName, String formId, String encType, int formPos, String webBrowserAction) throws Exception { 250 if (method == null || method.equalsIgnoreCase("")) { 252 method = "GET"; 253 } 254 if (formName == null) { 255 formName = ""; 256 } 257 if (formId == null) { 258 formId = ""; 259 } 260 261 formName = notNullValueForType(formName); 262 formId = notNullValueForType(formId); 263 logger.debug("Rewrite action form: " + action + "," + method + "," + formName + "," + formId); 264 265 if (!isRewritableUrl(action)) { 267 return action; 268 } 269 270 action = URLUtilities.getHrefAbsoluteValue(getUrlBean().getRedirectUrl(), action); 272 273 String hash = HashUtilities.buildFormHash(formName, formId, formPos); 275 URLWrapper wrapper = new URLWrapper(action, WebConstants.FROM_FORM, method, encType, hash); 276 UrlEncoderIF encoder = getUrlEncoder(); 278 encoder.setAction(webBrowserAction); 279 encoder.setUrlWrapper(wrapper); 280 281 return encoder.getEncodedUrl(); 282 } 283 284 285 286 294 public String getImportCssContent(String url, String media) throws Exception { 295 url = relatifToAbsolute(url); 296 String content = org.jahia.clipbuilder.html.web.http.SimpleHttpSecureClient.getContentAsString(url); 297 298 String refactoredContent = CssUtilities.getRefactoredCssContent(url, content, media); 300 logger.debug("[ Refactored css Content from " + url + "\n " + refactoredContent + "]"); 301 return refactoredContent; 302 } 303 304 305 314 public String getImportJavascriptContent(String src, String webBrowserAction) throws Exception { 315 logger.debug("Imported Javacript from: " + src); 316 src = relatifToAbsolute(src); 317 318 String content = org.jahia.clipbuilder.html.web.http.SimpleHttpSecureClient.getContentAsString(src); 319 String refactoredContent = getRefactoredJavascript(src, webBrowserAction, content); 320 return refactoredContent; 321 } 322 323 324 325 330 public java.util.List getLastParsingErrors() { 331 return lastParsingErrors; 332 } 333 334 335 340 public boolean isRemoveMetaTag() { 341 return removeMetaTag; 342 } 343 344 345 351 public boolean isRemoveHeadScriptTag() { 352 return removeHeadScriptTag; 353 } 354 355 356 361 public boolean isRemoveBodyScript() { 362 return removeBodyScript; 363 } 364 365 366 372 public boolean isRemoveJavascriptEvent() { 373 return removeJavascriptEvent; 374 } 375 376 377 383 public boolean isRefactorJavascriptCode() { 384 return refactorJavascriptCode; 385 } 386 387 388 394 public boolean isRefactorJavascriptEvent() { 395 return refactorJavascriptEvent; 396 } 397 398 399 404 public HTMLDocumentBuilder getBuilder() { 405 return builder; 406 } 407 408 409 410 417 public String getInlineCssContent(String cssContent) throws Exception { 418 String content = CssUtilities.getRefactoredCssContent(getUrlBean().getAbsoluteUrlValue(), cssContent, null); 420 return content; 422 } 423 424 425 426 435 public String getRefactoredJavascript(String url, String webBrowserAction, String content) { 436 String refactoredContent = JavascriptUtilities.getRefactoredJavascript(getUrlEncoder(), url, webBrowserAction, content); 437 return refactoredContent; 438 } 439 440 441 448 public String refactorCssStyleRule(String styleRule) throws Exception { 449 String content = CssUtilities.getRefactoredStyleRule(getUrlBean().getAbsoluteUrlValue(), styleRule); 451 return content; 453 } 454 455 456 462 public void removeJavascriptEvent(boolean removeJavascriptEvent) { 463 this.removeJavascriptEvent = removeJavascriptEvent; 464 } 465 466 467 473 public void refactorJavascriptEvent(boolean refactorJavascriptEvent) { 474 this.refactorJavascriptEvent = refactorJavascriptEvent; 475 } 476 477 478 484 public void refactorJavascriptCode(boolean refactorJavascriptCode) { 485 this.refactorJavascriptCode = refactorJavascriptCode; 486 } 487 488 489 494 public void removeMetaTag(boolean remove) { 495 this.removeMetaTag = remove; 496 } 497 498 499 504 public void removeHeadScriptTag(boolean remove) { 505 this.removeHeadScriptTag = remove; 506 } 507 508 509 514 public void removeBodyScript(boolean remove) { 515 this.removeBodyScript = remove; 516 } 517 518 519 522 public void resetInputHash() { 523 inputNameHash = new Hashtable(); 524 } 525 526 527 533 public void updateInputValue(FormParamBean fBean, String inputValue) { 534 logger.warn("FormParamBean type = hidden. Update Used Value value."); 535 String oldValue = fBean.getUsedValue(); 536 537 540 if (inputValue == null || inputValue.equalsIgnoreCase("")) { 541 fBean.setUsedValue(oldValue); 542 } 543 else { 544 fBean.setUsedValue(inputValue); 545 } 546 } 547 548 549 554 public void enableCSS(boolean enableCSS) { 555 this.enableCSS = enableCSS; 556 } 557 558 559 565 public void addParsingErrors(String error) { 566 getLastParsingErrors().add(error); 567 } 568 569 570 573 public void clearParsingErrors() { 574 getLastParsingErrors().clear(); 575 } 576 577 578 579 592 public void recordFormParam(String type, String formParentName, String formParentId, int formParentPosition, String name, String possibleValue, String visibility, int paramPosition) throws Exception { 593 UrlBean uBean = getUrlBean(); 595 String automaticUpdate = "false"; 596 visibility = "false"; 597 598 if (name.equalsIgnoreCase("sourceUrl")) { 600 logger.debug("source input"); 601 return; 602 } 603 if (name.equalsIgnoreCase("httpMethod")) { 604 logger.debug("httpMethod input"); 605 return; 606 } 607 608 if (name.equalsIgnoreCase("webClippingAction")) { 609 logger.debug("webClippingAction input"); 610 return; 611 } 612 613 if (type != null) { 615 if (!editableInputParameter(type)) { 616 automaticUpdate = "true"; 617 } 618 619 } 620 621 if (formParentName == null) { 622 formParentName = ""; 623 } 624 if (formParentId == null) { 625 formParentId = ""; 626 logger.debug(" Form Parent Id null ---> set empty"); 627 } 628 629 FormParamBean fBean = uBean.addFormParameter(formParentName, formParentId, formParentPosition, name, possibleValue, type, visibility, paramPosition); 630 fBean.setUpdate(automaticUpdate); 631 } 632 633 634 635 642 public String relatifToAbsolute(String attValue) throws Exception { 643 logger.debug("relatif To Absolute of :" + attValue); 644 return URLUtilities.getUrlAbsoluteValue(getUrlBean().getRedirectUrl().toExternalForm(), attValue); 645 } 646 647 648 654 public String notNullValueForType(String value) { 655 if (value == null) { 656 value = WebConstants.TYPE_TEXT; 657 } 658 return value; 659 } 660 661 662 669 public boolean editableInputParameter(String type) { 670 if (type != null) { 672 for (int j = 0; j < notEditableParameter.length; j++) { 673 if (type.equalsIgnoreCase(notEditableParameter[j])) { 674 return false; 675 } 676 } 677 } 678 else { 679 return false; 681 } 682 return true; 683 } 684 685 686 695 public void addFrame(List framesList, String name, String id, String src) { 696 FrameBean frameBean = new FrameBean(); 698 frameBean.setName(name); 699 frameBean.setId(id); 700 frameBean.setSrc(src); 701 String hash = HashUtilities.buildFrameHash(name, id, src); 702 frameBean.setHash(hash); 703 framesList.add(frameBean); 704 705 URLWrapper uw = new URLWrapper(); 707 uw.setFrom(WebConstants.FROM_FRAME); 708 uw.setSourceUrl(src); 709 uw.setMethod("Get"); 710 URLMap map = getBuilder().getUrlMap(); 711 map.addSourceUrl(uw, hash); 712 } 713 714 715 716 722 private boolean isRewritableUrl(String href) { 723 for (int i = 0; i < WebConstants.NOT_URL.length; i++) { 724 if (href.indexOf(WebConstants.NOT_URL[i]) == 0) { 725 return false; 726 } 727 } 728 return true; 729 } 730 731 732 738 private boolean isAnUrl(String href) { 739 if (href.indexOf("javascript:") > 0) { 740 return false; 741 } 742 if (href.indexOf(";") > 0) { 743 return false; 744 } 745 746 return true; 747 } 748 749 750 758 public static String getLabelText(String name, String value, String type) { 759 String text = "[Param=" + name + "]"; 760 if (type != null && type.equalsIgnoreCase(WebConstants.TYPE_RADIO)) { 761 text = "[Param=" + name + ", value=\'" + value + "\']"; 762 } 763 return text; 764 } 765 766 767 768 773 public static class NotYetTransformed 774 extends Exception { 775 778 public static final String MESSAGE = "The transform has'nt been performed !. \n processTransform() must be called at least once after setDocument() or new JDomHTMLTransformer(jdoc,false)."; 779 780 781 784 public NotYetTransformed() { 785 super(MESSAGE); 786 } 787 } 788 789 } 790 | Popular Tags |