1 package org.jahia.clipbuilder.html.web.html.Impl.Dom; 2 3 import java.util.*; 4 5 import javax.swing.text.html.*; 6 import javax.xml.parsers.*; 7 8 import org.jahia.clipbuilder.html.bean.*; 9 import org.jahia.clipbuilder.html.web.Constant.*; 10 import org.jahia.clipbuilder.html.struts.Util.Constants; 11 import org.jahia.clipbuilder.html.web.html.Impl.*; 12 import org.apache.xerces.dom.*; 13 import org.w3c.dom.*; 14 import org.jahia.clipbuilder.html.util.*; 15 import org.jahia.clipbuilder.html.util.*; 16 import org.jahia.clipbuilder.html.web.HTMLDocumentBuilder; 17 18 23 public class DomHTMLTransformer extends DefaultHTMLTransformer { 24 28 public final String FRAME_ELE_XPATH = "//frame"; 29 30 private DomHTMLDocument document; 31 32 private final String LINK_ELE_XPATH = "//link[@rel='stylesheet']|//link[@type='text/css']"; 34 private final String INPUT_ELE_XPATH = "//input"; 35 private final String HREF_ATT_XPATH = "//@href"; 36 private final String STYLE_ATT_XPATH = "//@style"; 37 private final String SRC_ATT_XPATH = "//@src"; 38 private final String FORM_ELE_XPATH = "//form"; 39 private final String META_ELE_XPATH = "//meta"; 40 private final String SYTLE_ELE_XPATH = "//style"; 41 private final String SCRIPT_ELE_XPATH = "//script"; 42 private final String TARGET_ATT_XPATH = "//@target"; 43 private final String BASE_ELE_XPATH = "//base"; 44 private final String JAVASCRIPT_EVENT_XPATH = "//@onabort|//@onblur|//@onchange|//@onclick|//@ondblClick|//@ondragdrop|//@onerror|//@onfocus|//@onkeydown|//@onkeypress|//@onkeyup|//@onload|//@onmousedown|//@onmousemove|//@onmouseout|//@onmouseover|//@onmouseup|//@onmove|//@onreset| //@onresize|//@onselect|//@onsubmit|//@onunload"; 45 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DomHTMLTransformer.class); 46 47 48 49 56 public DomHTMLTransformer(HTMLDocumentBuilder builder, DomHTMLDocument document, boolean enableCss) { 57 super(builder, enableCss); 58 setDocument(document); 59 } 60 61 62 67 public void setDocument(DomHTMLDocument document) { 68 this.document = document; 69 70 } 71 72 73 74 79 public void setUrlBean(UrlBean urlBean) { 80 this.getDocument().setUrlBean(urlBean); 81 } 82 83 84 89 public DomHTMLDocument getDocument() { 90 return document; 91 } 92 93 94 99 public UrlBean getUrlBean() { 100 return getDocument().getUrlBean(); 101 } 102 103 104 105 110 public void buildChewDocument(String tagName) { 111 logger.debug("[ Build chew document ]"); 112 logger.debug("[ Build Transformed document ]"); 113 Document transformedDoc = getDocument().getTransformedDocument(); 114 Document chewDocument = null; 115 116 try { 117 chewDocument = DomUtilities.copy(transformedDoc); 118 119 processChewElement(chewDocument, tagName); 121 122 } 123 124 catch (FactoryConfigurationError ex) { 125 logger.error("[ FactoryConfigurationError " + ex + " ]"); 126 ex.printStackTrace(); 127 } 128 catch (Exception ex) { 129 logger.error("[ Exception " + ex + " ]"); 130 ex.printStackTrace(); 131 } 132 133 getDocument().setChewDocument(chewDocument); 135 136 } 137 138 139 142 public void buildUserDocument() { 143 Document originalDoc = getDocument().getW3cOriginalDocument(); 144 Document userDocument = DomUtilities.copy(originalDoc); 145 146 try { 147 148 processMetaTag(userDocument, Constants.WEB_BROWSER_SHOW_TEST); 149 processUserBodyElement(userDocument); 150 processTargetAttribute(userDocument); 152 153 processBaseElement(userDocument); 155 156 processUserFormElement(userDocument, Constants.WEB_BROWSER_SHOW_TEST); 158 159 processUserInputElement(userDocument); 161 162 165 processAllUserHrefAttribute(userDocument, Constants.WEB_BROWSER_SHOW_TEST); 167 processJavascriptEvent(userDocument, Constants.WEB_BROWSER_SHOW_TEST); 168 processSytleElement(userDocument); 169 processSrcAttribute(userDocument); 170 171 } 172 173 catch (FactoryConfigurationError ex) { 174 logger.error("[ FactoryConfigurationError " + ex + " ]"); 175 ex.printStackTrace(); 176 } 177 catch (Exception ex) { 178 logger.error("[ Exception " + ex + " ]"); 179 ex.printStackTrace(); 180 } 181 182 finally { 183 184 getDocument().setUserDocument(userDocument); 186 } 187 188 } 189 190 191 192 195 public void buildDocumentWhithLabel() { 196 logger.debug("[ Build Document whith label ]"); 197 Document transformedDoc = getDocument().getTransformedDocument(); 198 Document doc = null; 199 try { 200 doc = DomUtilities.copy(transformedDoc); 202 203 NodeList inputList = DomUtilities.getNodeListByXPath(doc, INPUT_ELE_XPATH); 205 206 for (int i = 0; i < inputList.getLength(); i++) { 208 Element currentEle = (Element) inputList.item(i); 210 String type = currentEle.getAttribute("type"); 211 if (editableInputParameter(type)) { 212 if (!type.equalsIgnoreCase(WebConstants.TYPE_SUBMIT) && !type.equalsIgnoreCase(WebConstants.TYPE_IMAGE)) { 213 insertLabel(currentEle); 214 } 215 } 216 217 } 218 } 219 catch (DOMException ex) { 220 logger.error("[ DOMException " + ex.getMessage() + " ]"); 221 ex.printStackTrace(); 222 } 223 catch (FactoryConfigurationError ex) { 224 logger.error("[ FactoryConfigurationError " + ex.getMessage() + " ]"); 225 ex.printStackTrace(); 226 } 227 228 finally { 229 getDocument().setDocumentWithLabel(doc); 231 } 232 } 233 234 235 238 public void buildTransformedDocument() { 239 logger.debug("[ Build Transformed document ]"); 240 Document originalDoc = getDocument().getW3cOriginalDocument(); 241 Document transformedDocument = DomUtilities.copy(originalDoc); 243 244 try { 245 processMetaTag(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE); 248 249 processTargetAttribute(transformedDocument); 251 252 processStyleAttribute(transformedDocument); 254 255 processBaseElement(transformedDocument); 257 258 processInputElement(transformedDocument); 260 processSelectElement(transformedDocument); 261 262 processFormElement(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE); 264 265 268 272 275 processSrcAttribute(transformedDocument); 278 processSytleElement(transformedDocument); 279 processScriptElement(transformedDocument); 280 processAllHrefAttribute(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE); 281 processJavascriptEvent(transformedDocument, Constants.WEB_BROWSER_SHOW_BROWSE); 282 processFrameElement(transformedDocument); 283 284 } 285 286 catch (FactoryConfigurationError ex) { 287 logger.error("[ FactoryConfigurationError " + ex + " ]"); 288 ex.printStackTrace(); 289 } 290 catch (Exception ex) { 291 logger.error("[ Exception " + ex + " ]"); 292 ex.printStackTrace(); 293 } 294 295 finally { 296 297 getDocument().setTransformedDocument(transformedDocument); 299 } 300 301 } 302 303 304 305 312 private void processAllUserHrefAttribute(Document transformedDocument, String webBrowseAction) throws WebClippingException { 313 logger.debug("[ Process href att ]"); 314 NodeList atts = DomUtilities.getNodeListByXPath(transformedDocument, HREF_ATT_XPATH); 315 int pos = 0; 316 for (int i = 0; i < atts.getLength(); i++) { 317 Attr att = (Attr) atts.item(i); 319 Element parent = att.getOwnerElement(); 320 String ownerTagName = parent.getTagName(); 321 String href = att.getValue(); 322 String absoluteHref = null; 323 try { 324 absoluteHref = this.relatifToAbsolute(href); 325 pos = processHrefAtt(webBrowseAction, pos, att); 326 } 327 catch (Exception ex) { 328 ex.printStackTrace(); 329 logger.error("Error has occurred"); 330 } 331 332 if (!ownerTagName.equalsIgnoreCase("meta") && !ownerTagName.equalsIgnoreCase("link")) { 333 String continualClipping = getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping(); 334 int ccInt = Integer.parseInt(continualClipping); 336 337 switch (ccInt) { 339 case 340 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE: 341 342 break; 345 case 346 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP: 347 348 parent.setAttribute("target", "_blank"); 350 case 352 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP: 353 354 parent.setAttribute("target", "_blank"); 356 357 parent.setAttribute("href", absoluteHref); 359 case 360 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME: 361 break; 362 case 363 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME: 364 365 parent.setAttribute("href", absoluteHref); 367 break; 368 } 369 370 } 371 } 372 373 } 374 375 376 377 384 private void processMetaTag(Document doc, String webBrowserAction) throws WebClippingException { 385 try { 386 NodeList metaList = DomUtilities.getNodeListByXPath(doc, META_ELE_XPATH); 387 for (int i = 0; i < metaList.getLength(); i++) { 388 389 Element metaEle = (Element) metaList.item(i); 391 if (isRemoveMetaTag()) { 392 metaEle.removeAttribute(HTML.Attribute.HTTPEQUIV.toString()); 393 } 394 else { 395 396 String httpEquiv = metaEle.getAttribute(HTML.Attribute.HTTPEQUIV.toString()); 397 if (httpEquiv != null) { 398 if ("refresh".equals(httpEquiv.toLowerCase())) { 400 String content = metaEle.getAttribute(HTML.Attribute.CONTENT.toString()); 401 int urlIndex = content.toLowerCase().indexOf("url"); 403 if (urlIndex != -1) { 404 String contentBeforeURL = content.substring(0, urlIndex).trim(); 406 logger.debug("[ Content before url " + contentBeforeURL + " ]"); 407 408 int equalIndex = urlIndex + 3; 410 String contentURL = content.substring(equalIndex + 1).trim(); 411 logger.debug("[ BEFORE meta http-equiv=[" + httpEquiv + "] content=[" + content + "] ]"); 412 logger.debug("[ Refresh url is [" + contentURL + " ] ]"); 413 String encodedUrl = getRewritedHrefValue("meta", contentURL, "metaId", "metaLink", webBrowserAction); 415 416 metaEle.setAttribute(HTML.Attribute.CONTENT.toString(), contentBeforeURL + "url=" + encodedUrl); 418 logger.debug("[ AFTER meta http-equiv=[" + httpEquiv + "] content=[" + contentBeforeURL + "url=" + encodedUrl + "] ]+"); 419 } 420 } 421 } 422 } 423 424 } 425 } 426 catch (Exception ex) { 427 throw new WebClippingException("Meta", ex); 428 } 429 finally { 430 return; 431 } 432 433 } 434 435 436 443 private String addChewLinkElementChild(Element ele, int position) { 444 Document ownerDoc = ele.getOwnerDocument(); 445 String idAtt = ele.getAttribute("id"); 446 Element linkEle = ownerDoc.createElement("a"); 448 linkEle.setAttribute(HTML.Attribute.TARGET.toString(), "_parent"); 449 450 String hash = HashUtilities.buildTagHash(idAtt, position); 452 ele.setAttribute("id", hash); 453 String linkHref = getHrefChewLink(hash); 454 linkEle.setAttribute(HTML.Attribute.HREF.toString(), linkHref); 455 456 Element imgEle = ownerDoc.createElement("img"); 458 String chewImagePath = getImageChewLinkSrcValue(); 459 imgEle.setAttribute(HTML.Attribute.SRC.toString(), chewImagePath); 460 461 linkEle.appendChild(imgEle); 463 ele.appendChild(linkEle); 464 465 return linkHref; 466 } 467 468 469 474 private void processUserBodyElement(Document doc) { 475 logger.debug(" body tag found"); 476 Element body = (Element) doc.getElementsByTagName("body").item(0); 477 String onload = body.getAttribute("onload"); 478 String clickOnload = ""; 479 UrlBean uBean = getUrlBean(); 481 ClipperBean cBean = uBean.getClipperBean(); 482 UrlBean nextBean = cBean.getNextRecordedUrlBean(uBean); 483 484 if (nextBean != null) { 486 String from = nextBean.getFrom(); 487 String fromHash = nextBean.getHash(); 488 if (from.equalsIgnoreCase(WebConstants.FROM_LINK)) { 490 clickOnload = "document.getElementById('" + fromHash + "').click();"; 492 } 493 else { 494 FormParamBean fBean = uBean.getSubmittedParamBean(fromHash); 496 if (fBean != null) { 497 String name = fBean.getName(); 498 String formParentName = fBean.getFormParentName(); 499 String formParentId = fBean.getFormParentId(); 500 if (formParentName == null || formParentName.equalsIgnoreCase("")) { 501 clickOnload = "document.getElementById('" + formParentId + "')" + "." + name + ".click();"; 502 } 503 else { 504 clickOnload = "document." + formParentName + "." + name + ".click();"; 505 } 506 } 507 else { 508 logger.warn("submit button not found"); 509 String submittedFormName = HashUtilities.getFormNameFromHash(fromHash); 511 if (submittedFormName != null) { 512 clickOnload = "document." + submittedFormName + ".submit();"; 513 } 514 else { 515 String submittedFormId = HashUtilities.getFormIdFromHash(fromHash); 516 clickOnload = "document.getElementById('" + submittedFormId + "')" + ".submit();"; 517 } 518 519 } 520 } 521 if (onload == null) { 522 onload = clickOnload; 523 } 524 else { 525 onload = onload + ";" + clickOnload; 526 } 527 body.setAttribute("onload", onload); 528 logger.debug("[New Onload: " + onload + " ]"); 530 } 531 532 } 533 534 535 541 private void processAllHrefAttribute(Document transformedDocument, String webBrowseAction) { 542 logger.debug("[ Process href att ]"); 543 NodeList atts = DomUtilities.getNodeListByXPath(transformedDocument, HREF_ATT_XPATH); 544 int pos = 0; 545 for (int i = 0; i < atts.getLength(); i++) { 546 try { 547 Attr att = (Attr) atts.item(i); 548 pos = processHrefAtt(webBrowseAction, pos, att); 549 } 550 catch (Exception ex) { 551 ex.printStackTrace(); 552 addParsingErrors("href attribute rewriting error: " + ex.toString()); 553 } 554 } 555 556 } 557 558 559 569 private int processHrefAtt(String webBrowseAction, int pos, Attr att) throws 570 DOMException, Exception { 571 String ownerTagName = att.getOwnerElement().getTagName(); 572 String position = "" + pos; 573 String id = att.getOwnerElement().getAttribute("id"); 574 if (ownerTagName.equalsIgnoreCase("a")) { 575 String linkHash = HashUtilities.buildLinkHash(att.getNodeValue(), id, position); 576 if (id == null || id.equalsIgnoreCase("")) { 577 att.getOwnerElement().setAttribute("id", linkHash); 578 } 579 att.getOwnerElement().removeAttribute("target"); 580 pos++; 581 } 582 att.setNodeValue(getRewritedHrefValue(ownerTagName, att.getNodeValue(), id, position, webBrowseAction)); 583 return pos; 584 } 585 586 587 592 private void processStyleAttribute(Document doc) { 593 logger.debug("[ Process href att ]"); 594 NodeList atts = DomUtilities.getNodeListByXPath(doc, STYLE_ATT_XPATH); 595 for (int i = 0; i < atts.getLength(); i++) { 596 try { 597 Attr att = (Attr) atts.item(i); 598 att.setNodeValue(refactorCssStyleRule(att.getNodeValue())); 599 } 600 catch (Exception ex) { 601 ex.printStackTrace(); 602 addParsingErrors("href attribute rewriting error: " + ex.toString()); 603 } 604 } 605 606 } 607 608 609 610 616 private void processJavascriptEvent(Document doc, String webBrowseAction) { 617 logger.debug("[ Process href att ]"); 618 NodeList atts = DomUtilities.getNodeListByXPath(doc, JAVASCRIPT_EVENT_XPATH); 619 for (int i = 0; i < atts.getLength(); i++) { 620 Attr att = (Attr) atts.item(i); 621 Element ownerElement = att.getOwnerElement(); 622 try { 623 boolean refactor = isRefactorJavascriptEvent(); 625 if (refactor) { 626 String url = getDocument().getUrlBean().getRedirectUrl().toExternalForm(); 627 String value = att.getValue(); 628 String newValue = getRefactoredJavascript(url, webBrowseAction, value); 629 att.setValue(newValue); 630 } 631 else { 632 boolean remove = isRemoveJavascriptEvent(); 634 if (remove) { 635 ownerElement.removeAttributeNode(att); 636 } 637 } 638 } 639 catch (Exception ex) { 640 ex.printStackTrace(); 641 addParsingErrors("href attribute rewriting error: " + ex.toString()); 642 } 643 } 644 645 } 646 647 648 649 652 667 672 private void processSrcAttribute(Document doc) { 673 logger.debug("[ Process action src ]"); 674 NodeList atts = DomUtilities.getNodeListByXPath(doc, SRC_ATT_XPATH); 675 for (int i = 0; i < atts.getLength(); i++) { 676 Node att = atts.item(i); 677 try { 678 att.setNodeValue(relatifToAbsolute(att.getNodeValue())); 679 } 680 catch (Exception ex) { 681 ex.printStackTrace(); 682 addParsingErrors("Src rewriting error: " + ex.toString()); 683 } 684 } 685 686 } 687 688 689 694 private void processTargetAttribute(Document doc) { 695 NodeList atts = DomUtilities.getNodeListByXPath(doc, TARGET_ATT_XPATH); 696 for (int i = 0; i < atts.getLength(); i++) { 697 Attr att = (Attr) atts.item(i); 698 Element ele = att.getOwnerElement(); 699 ele.removeAttributeNode(att); 700 } 701 702 } 703 704 705 710 private void processBaseElement(Document doc) { 711 NodeList eles = DomUtilities.getNodeListByXPath(doc, BASE_ELE_XPATH); 712 for (int i = 0; i < eles.getLength(); i++) { 713 Element ele = (Element) eles.item(i); 714 ele.removeAttribute("href"); 715 } 716 717 } 718 719 720 726 private boolean processFrameElement(Document doc) { 727 NodeList eles = DomUtilities.getNodeListByXPath(doc, FRAME_ELE_XPATH); 728 boolean result = false; 729 List framesList = new ArrayList(); 730 for (int i = 0; i < eles.getLength(); i++) { 731 Element ele = (Element) eles.item(i); 732 String name = ele.getAttribute("name"); 733 String id = ele.getAttribute("id"); 734 String src = ele.getAttribute("src"); 735 result = true; 736 737 addFrame(framesList, name, id, src); 738 } 739 getDocument().setFramesList(framesList); 740 return result; 741 } 742 743 744 745 750 private void processSytleElement(Document doc) { 751 logger.debug("[ Process StyleTag ]"); 752 NodeList eleList = DomUtilities.getNodeListByXPath(doc, this.SYTLE_ELE_XPATH); 753 754 for (int i = 0; i < eleList.getLength(); i++) { 755 Node ele = eleList.item(i); 756 NodeList children = ele.getChildNodes(); 757 Node child = null; 758 for (int j = 0; j < children.getLength(); j++) { 760 child = children.item(j); 761 762 if (child instanceof Text) { 764 logger.debug("Sytle Text found"); 765 Text tChild = (Text) child; 766 try { 767 String content = getInlineCssContent(child.getNodeValue()); 768 Comment comment = doc.createComment(content); 769 ele.appendChild(comment); 770 ele.removeChild(tChild); 771 break; 772 } 773 catch (Exception ex) { 774 ex.printStackTrace(); 775 } 776 } 777 else { 778 if (child instanceof Comment) { 780 logger.debug("Style set as comment"); 781 Comment cChild = (Comment) child; 782 try { 783 String comment = getInlineCssContent(cChild.getNodeValue()); 784 cChild.setNodeValue(comment); 785 } 786 catch (Exception ex) { 787 ex.printStackTrace(); 788 } 789 } 790 else { 791 logger.debug("Child is not a Text, it's a: " + child.toString()); 792 } 793 } 794 795 } 796 797 } 798 799 } 800 801 802 807 private void processScriptElement(Document doc) { 808 logger.debug("[ Process StyleTag ]"); 809 NodeList eleList = DomUtilities.getNodeListByXPath(doc, SCRIPT_ELE_XPATH); 810 for (int i = 0; i < eleList.getLength(); i++) { 811 Element ele = (Element) eleList.item(i); 812 if (isRemoveBodyScript() || isRemoveHeadScriptTag()) { 813 ele.removeAttribute("src"); 814 } 815 816 NodeList children = ele.getChildNodes(); 817 Node child = null; 818 for (int j = 0; j < children.getLength(); j++) { 820 child = children.item(j); 821 if (isRemoveBodyScript() || isRemoveHeadScriptTag()) { 822 ele.removeChild(child); 823 } 824 else { 825 if (child instanceof Text) { 827 logger.debug("script Text found"); 828 Text tChild = (Text) child; 829 try { 830 String baseUrl = getUrlBean().getAbsoluteUrlValue(); 831 String refactoredJavascript = getRefactoredJavascript(baseUrl, Constants.WEB_BROWSER_SHOW_BROWSE, child.getNodeValue()); 832 Comment comment = doc.createComment(refactoredJavascript); 833 ele.appendChild(comment); 834 ele.removeChild(tChild); 835 break; 836 } 837 catch (Exception ex) { 838 ex.printStackTrace(); 839 } 840 } 841 } 842 } 843 844 String src = ele.getAttribute(HTML.Attribute.SRC.toString()); 846 862 } 863 864 } 865 866 867 868 873 private void processLinkElement(Document doc) { 874 logger.debug("[ Process link element ]"); 875 NodeList elements = DomUtilities.getNodeListByXPath(doc, LINK_ELE_XPATH); 876 for (int i = 0; i < elements.getLength(); i++) { 877 try { 878 Element ele = (Element) elements.item(i); 880 Document ownerDoc = ele.getOwnerDocument(); 881 ((DocumentImpl) ownerDoc).renameNode(ele, "", "style"); 882 883 if (isEnableCSS()) { 884 String href = ele.getAttribute(HTML.Attribute.HREF.toString()); 886 String media = ele.getAttribute("media"); 887 ele.removeAttribute(HTML.Attribute.HREF.toString()); 888 ele.removeAttribute("media"); 889 String content = getImportCssContent(href, media); 890 Comment comment = ownerDoc.createComment(content); 891 ele.appendChild(comment); 892 893 } 894 else { 895 Node p = ele.getParentNode(); 897 p.removeChild(ele); 898 } 899 } 900 catch (Exception ex) { 901 ex.printStackTrace(); 902 addParsingErrors("CSS Parsing error: " + ex.toString()); 903 } 904 } 905 906 } 907 908 909 915 private void processChewElement(Document doc, String tagName) { 916 logger.debug("[ Process link element ]"); 917 NodeList elements = DomUtilities.getNodeListByXPath(doc, "//" + tagName); 918 for (int i = 0; i < elements.getLength(); i++) { 919 try { 920 Element ele = (Element) elements.item(i); 922 ele.setAttribute(HTML.Attribute.BORDER.toString(), "1"); 923 addChewLinkElementChild(ele, i); 924 } 925 catch (Exception ex) { 926 ex.printStackTrace(); 927 addParsingErrors("CSS Parsing error: " + ex.toString()); 928 } 929 } 930 931 } 932 933 934 935 940 private void processInputElement(Document doc) { 941 NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH); 942 for (int formParentPos = 0; formParentPos < formList.getLength(); formParentPos++) { 943 Element form = (Element) formList.item(formParentPos); 944 String formParentName = form.getAttribute(HTML.Attribute.NAME.toString()); 945 String formParentId = form.getAttribute(HTML.Attribute.ID.toString()); 946 947 NodeList inputList = form.getElementsByTagName(HTML.Tag.INPUT.toString()); 948 if (inputList == null) { 949 logger.warn("Input list is empty for form whith name: " + formParentName); 950 951 } 952 else { 953 for (int inputPos = 0; inputPos < inputList.getLength(); inputPos++) { 954 try { 955 Element ele = (Element) inputList.item(inputPos); 956 String type = ele.getAttribute(HTML.Attribute.TYPE.toString()); 957 String visibility = Boolean.TRUE.toString(); 959 String name = notNullValueForType(ele.getAttribute(HTML.Attribute.NAME.toString())); 962 int pos = getPosAndUpdateInputHash(name); 963 String possibleValue = notNullValueForType(ele.getAttribute(HTML.Attribute.VALUE.toString())); 964 type = notNullValueForType(type); 965 recordFormParam(type, formParentName, formParentId, formParentPos, name, possibleValue, visibility, pos); 966 } 967 catch (Exception ex) { 968 addParsingErrors("Parser, Input Element: " + ex.toString()); 969 } 970 971 } 972 } 973 resetInputHash(); 975 } 976 977 } 978 979 980 985 private void processSelectElement(Document doc) { 986 NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH); 987 988 for (int formParentPos = 0; formParentPos < formList.getLength(); formParentPos++) { 990 Element form = (Element) formList.item(formParentPos); 991 String formParentName = form.getAttribute(HTML.Attribute.NAME.toString()); 992 String formParentId = form.getAttribute(HTML.Attribute.ID.toString()); 993 994 NodeList selectList = form.getElementsByTagName(HTML.Tag.SELECT.toString()); 995 if (selectList == null) { 996 logger.warn("Select list is empty for form whith name: " + formParentName); 997 998 } 999 else { 1000 for (int selectPos = 0; selectPos < selectList.getLength(); selectPos++) { 1002 try { 1003 Element ele = (Element) selectList.item(selectPos); 1004 String type = WebConstants.TYPE_SELECT; 1005 String visibility = Boolean.TRUE.toString(); 1006 String name = notNullValueForType(ele.getAttribute(HTML.Attribute.NAME.toString())); 1007 type = notNullValueForType(type); 1008 int pos = getPosAndUpdateInputHash(name); 1009 NodeList optionList = form.getElementsByTagName(HTML.Tag.OPTION.toString()); 1011 for (int optionPos = 0; optionPos < optionList.getLength(); optionPos++) { 1012 Element optEle = (Element) optionList.item(optionPos); 1013 String value = optEle.getAttribute("value"); 1014 String possibleValue = notNullValueForType(value); 1015 recordFormParam(type, formParentName, formParentId, formParentPos, name, possibleValue, visibility, pos); 1016 } 1017 1018 } 1019 catch (Exception ex) { 1020 addParsingErrors("Parser, Input Element: " + ex.toString()); 1021 } 1022 1023 } 1024 } 1025 1026 resetInputHash(); 1028 } 1029 1030 } 1031 1032 1033 1034 1039 private void processUserInputElement(Document doc) { 1040 NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH); 1041 1045 for (int formPos = 0; formPos < formList.getLength(); formPos++) { 1046 Element form = (Element) formList.item(formPos); 1047 String formParentName = form.getAttribute(HTML.Attribute.NAME.toString()); 1048 String formParentId = form.getAttribute(HTML.Attribute.ID.toString()); 1049 1050 NodeList inputList = form.getElementsByTagName(HTML.Tag.INPUT.toString()); 1051 1055 if (inputList == null) { 1056 logger.warn("Input list is empty for form whith name: " + formParentName); 1057 } 1058 else { 1059 1060 for (int inputPos = 0; inputPos < inputList.getLength(); inputPos++) { 1061 Element inputEle = (Element) inputList.item(inputPos); 1062 String name = inputEle.getAttribute(HTML.Attribute.NAME.toString()); 1063 int pos = getPosAndUpdateInputHash(name); 1064 FormParamBean fBean = getFormParamBean(formParentName, formParentId, formPos, name, pos); 1065 String formHash = HashUtilities.buildFormHash(formParentName, formParentId, formPos); 1066 1067 if (fBean == null) { 1068 logger.warn("FormParamBean not found for hash " + formHash + " and input name: " + name + " whith type: " + inputEle.getAttribute("type")); 1069 1070 } 1071 else { 1072 1078 String type = fBean.getType(); 1079 1080 String fBeanUpdate = fBean.getUpdate(); 1082 logger.debug("Param: " + fBean.getName() + " whith type: " + fBean.getType() + " update: " + fBeanUpdate); 1083 if (fBeanUpdate.equalsIgnoreCase("true")) { 1084 String inputValue = inputEle.getAttribute("value"); 1085 updateInputValue(fBean, inputValue); 1086 1087 } 1088 else { 1089 logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + inputEle.getAttribute("type")); 1090 logger.debug("Used value: " + fBean.getUsedValue()); 1091 } 1093 } 1094 } 1095 } 1096 resetInputHash(); 1098 } 1099 1100 } 1101 1102 1103 1104 1109 private void processUserSelectElement(Document doc) { 1110 NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH); 1111 for (int formPos = 0; formPos < formList.getLength(); formPos++) { 1112 Element form = (Element) formList.item(formPos); 1113 String formParentName = form.getAttribute(HTML.Attribute.NAME.toString()); 1114 String formParentId = form.getAttribute(HTML.Attribute.ID.toString()); 1115 NodeList selectList = form.getElementsByTagName(HTML.Tag.SELECT.toString()); 1116 if (selectList == null) { 1117 logger.warn("Input list is empty for form whith name: " + formParentName); 1118 1119 } 1120 else { 1121 for (int selectPos = 0; selectPos < selectList.getLength(); selectPos++) { 1123 Element selectEle = (Element) selectList.item(selectPos); 1124 String name = selectEle.getAttribute(HTML.Attribute.NAME.toString()); 1125 int pos = getPosAndUpdateInputHash(name); 1126 FormParamBean fBean = getFormParamBean(formParentName, formParentId, formPos, name, pos); 1127 String formHash = HashUtilities.buildFormHash(formParentName, formParentId, formPos); 1128 1129 if (fBean == null) { 1130 logger.warn("FormParamBean not found for hash " + formHash + " and input name: " + name + " whith type: " + selectEle.getAttribute("type")); 1131 } 1132 else { 1133 1134 logger.debug("FormParamBean found for hash " + formHash + " and input name " + name + " whith type " + selectEle.getAttribute("type")); 1135 logger.debug("Used value: " + fBean.getUsedValue()); 1136 String usedValue = fBean.getUsedValue(); 1137 NodeList optionList = form.getElementsByTagName(HTML.Tag.OPTION.toString()); 1139 for (int optionPos = 0; optionPos < optionList.getLength(); optionPos++) { 1140 Element optEle = (Element) optionList.item(optionPos); 1141 String value = optEle.getAttribute("value"); 1142 if (value.equalsIgnoreCase(usedValue)) { 1143 optEle.setAttribute("selected", "selected"); 1144 } 1145 else { 1146 1147 optEle.removeAttribute("selected"); 1148 } 1149 } 1150 } 1151 } 1152 } 1153 1154 resetInputHash(); 1156 } 1157 1158 } 1159 1160 1161 1162 1168 private void processTitleElement(DocumentImpl doc) throws DOMException { 1169 Element title = (Element) doc.getElementsByTagName(HTML.Tag.TITLE.toString()).item(0); 1171 if (title != null) { 1172 DomUtilities.removeNode(title); 1173 } 1175 else { 1176 } 1178 } 1179 1180 1181 1187 private void processFormElement(Document doc, String webBrowserAction) { 1188 1190 NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH); 1191 for (int formPos = 0; formPos < formList.getLength(); formPos++) { 1192 try { 1193 Element formEle = (Element) formList.item(formPos); 1194 String url = formEle.getAttribute("action"); 1195 String method = formEle.getAttribute("method"); 1196 String formName = formEle.getAttribute("name"); 1197 String formId = formEle.getAttribute("id"); 1198 String enctype = formEle.getAttribute("enctype"); 1199 1200 formEle.setAttribute("action", getRewritedActionValue(url, method, formName, formId, enctype, formPos, webBrowserAction)); 1201 1202 formEle.setAttribute("method", "post"); 1204 1205 formEle.removeAttribute("target"); 1207 1208 formEle.removeAttribute("enctype"); 1210 1211 } 1212 catch (Exception ex) { 1213 addParsingErrors("href attribute rewriting error: " + ex.toString()); 1214 } 1215 } 1216 } 1217 1218 1219 1225 private void processUserFormElement(Document doc, String webBrowserAction) { 1226 1228 NodeList formList = DomUtilities.getNodeListByXPath(doc, FORM_ELE_XPATH); 1229 for (int formPos = 0; formPos < formList.getLength(); formPos++) { 1230 try { 1231 Element formEle = (Element) formList.item(formPos); 1232 String url = formEle.getAttribute("action"); 1233 String method = formEle.getAttribute("method"); 1234 String formName = formEle.getAttribute("name"); 1235 String formId = formEle.getAttribute("id"); 1236 String enctype = formEle.getAttribute("enctype"); 1237 1238 formEle.setAttribute("action", getRewritedActionValue(url, method, formName, formId, enctype, formPos, webBrowserAction)); 1239 1240 formEle.setAttribute("method", "post"); 1242 1243 formEle.removeAttribute("target"); 1245 1246 formEle.removeAttribute("enctype"); 1248 1249 String continualClipping = getUrlBean().getClipperBean().getConfigurationBean().getPortletContinualClipping(); 1250 int ccInt = Integer.parseInt(continualClipping); 1252 1253 switch (ccInt) { 1255 case 1256 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_FALSE: 1257 1258 break; 1261 case 1262 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_POPUP: 1263 1264 formEle.setAttribute("target", "_blank"); 1266 case 1268 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_POPUP: 1269 1270 formEle.setAttribute("target", "_blank"); 1272 1273 if (method != null) { 1275 formEle.setAttribute("method", method); 1276 } 1277 if (enctype != null) { 1278 formEle.setAttribute("enctype", enctype); 1279 } 1280 formEle.setAttribute("action", relatifToAbsolute(url)); 1281 1282 case 1283 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_ACTIF_IFRAME: 1284 break; 1285 case 1286 org.jahia.clipbuilder.html.util.Constants.PORTLET_CONTINUAL_PASSIF_IFRAME: 1287 if (method != null) { 1289 formEle.setAttribute("method", method); 1290 } 1291 if (enctype != null) { 1292 formEle.setAttribute("enctype", enctype); 1293 } 1294 formEle.setAttribute("action", relatifToAbsolute(url)); 1295 1296 break; 1297 } 1298 1299 } 1300 catch (Exception ex) { 1301 addParsingErrors("href attribute rewriting error: " + ex.toString()); 1302 } 1303 } 1304 } 1305 1306 1307 1308 1313 private void insertLabel(Element ele) { 1314 String type = ele.getAttribute(HTML.Attribute.TYPE.toString()); 1315 String name = ele.getAttribute(HTML.Attribute.NAME.toString()); 1316 String value = ele.getAttribute(HTML.Attribute.VALUE.toString()); 1317 1318 Document doc = ele.getOwnerDocument(); 1320 Element labelEle = createInputLabel(doc, type, name, value); 1321 1322 ele.getParentNode().insertBefore(labelEle, ele); 1324 } 1325 1326 1327 1328 1337 private Element createInputLabel(Document originalDoc, String type, String name, String value) { 1338 Element labelEle = originalDoc.createElement(HTML.Tag.I.toString()); 1340 Text text = originalDoc.createTextNode("[Param=" + name + "]"); 1342 if (type != null && type.equalsIgnoreCase(WebConstants.TYPE_RADIO)) { 1343 text = originalDoc.createTextNode("[Param=" + name + ", value=\'" + value + "\']"); 1344 } 1345 labelEle.appendChild(text); 1346 return labelEle; 1347 } 1348 1349} 1350 | Popular Tags |