1 package org.jahia.clipbuilder.html.web.html.Impl.JDom; 2 3 import java.util.*; 4 import java.util.Iterator ; 5 6 import javax.swing.text.html.*; 7 8 import org.jahia.clipbuilder.html.web.*; 9 import org.jahia.clipbuilder.html.bean.*; 10 import org.jahia.clipbuilder.html.util.*; 11 import org.jahia.clipbuilder.html.web.Url.*; 12 import org.jahia.clipbuilder.html.web.html.Impl.*; 13 import org.jahia.clipbuilder.html.web.Constant.*; 14 import org.jahia.clipbuilder.html.web.Url.URLWrapper; 15 16 import org.jdom.*; 17 import org.jahia.clipbuilder.html.struts.Util.Constants; 18 import org.jahia.clipbuilder.html.web.Url.UrlEncoderIF; 19 20 25 public class JDomHTMLTransformer extends DefaultHTMLTransformer { 26 private JDomHTMLDocument document; 27 private String formParentId = ""; 28 private int formParentPos = 0; 29 private UrlEncoderIF encoder; 30 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JDomHTMLTransformer.class); 31 32 33 41 public JDomHTMLTransformer(HTMLDocumentBuilder builder, JDomHTMLDocument jdoc, boolean enableCSS, boolean processTransform) { 42 super(builder, enableCSS); 43 setDocument(jdoc); 44 if (processTransform) { 45 buildTransformedDocument(); 46 } 47 48 } 49 50 51 52 57 public void setDocument(JDomHTMLDocument jdoc) { 58 this.document = jdoc; 59 jdoc.setTransformer(this); 60 } 61 62 63 64 70 public void setDocumentWhithParamsLabel(org.jdom.Document documentWhithParamsLabel) { 71 getDocument().setDocumentWhithParamsLabel(documentWhithParamsLabel); 72 } 73 74 75 80 public void setTransformedDocument(org.jdom.Document transformedDocument) { 81 getDocument().setTransformedDocument(transformedDocument); 82 } 83 84 85 90 public void setUserDocument(org.jdom.Document userDocument) { 91 getDocument().setUserDocument(userDocument); 92 } 93 94 95 96 101 public void setChewDocument(org.jdom.Document doc) { 102 this.getDocument().setChewDocument(doc); 103 } 104 105 106 111 public void setUrlEncoder(UrlEncoderIF encoder) { 112 this.encoder = encoder; 113 } 114 115 116 121 public JDomHTMLDocument getDocument() { 122 return document; 123 } 124 125 126 127 133 public org.jdom.Document getDocumentWhithParamsLabel() { 134 return this.getDocument().getDocumentWhithParamsLabel(); 135 } 136 137 138 143 public UrlBean getUrlBean() { 144 return getDocument().getUrlBean(); 145 } 146 147 148 153 public UrlEncoderIF getUrlEncoder() { 154 return encoder; 155 } 156 157 158 161 public void buildDocumentWhithLabel() { 162 if (hasBeenTransformed()) { 163 Element root = getTransformedDocument().getRootElement(); 165 Element rootClone = cloneWithoutChidren(root); 166 org.jdom.Document doc = new org.jdom.Document(); 168 doc.setRootElement(rootClone); 169 setDocumentWhithParamsLabel(doc); 170 processOnlyInputChildren(root, rootClone); 172 173 } 174 else { 175 logger.error("[ Document not yet transformed ]"); 176 } 177 178 } 179 180 181 184 public void buildTransformedDocument() { 185 186 logger.debug("[Begin process transform] "); 187 188 org.jdom.Document originalDoc = getDocument().getOriginalDocument(); 190 191 walkThrougthChildrenForTransform(originalDoc.getRootElement(), "", 0); 193 194 org.jdom.Document transformedDocument = (Document) getDocument().getOriginalDocument().clone(); 196 197 Element html = processHTMLElement(transformedDocument); 199 200 processHeadElement(html); 202 203 processTitleElement(html); 205 206 processBodyElement(transformedDocument); 208 209 setTransformedDocument(transformedDocument); 211 212 formParentPos = 0; 213 logger.debug("[Process transform finish swhith success "); 214 215 } 216 217 218 221 public void buildUserDocument() { 222 logger.debug("[Begin process transform] "); 223 224 org.jdom.Document originalDoc = getDocument().getOriginalDocument(); 226 227 walkThrougthChildrenForUser(originalDoc.getRootElement(), "", 0); 229 230 org.jdom.Document userDocument = (Document) getDocument().getOriginalDocument().clone(); 232 233 setUserDocument(userDocument); 235 236 logger.debug("[Process transform finish swhith success "); 237 238 } 239 240 241 242 247 public void buildChewDocument(String tagName) { 248 logger.debug("[ Begin chew transform ]"); 249 250 Element chewRoot = walkThrougthChildrenForChew(getDocument().getTransformedDocument().getRootElement(), 0); 252 253 setChewDocument(new org.jdom.Document(chewRoot)); 255 256 logger.debug("[Building chew document finish whith success "); 257 258 } 259 260 261 266 public boolean hasBeenTransformed() { 267 return (getTransformedDocument() != null); 268 } 269 270 271 278 private org.jdom.Document getTransformedDocument() { 279 280 return this.getDocument().getTransformedDocument(); 281 } 282 283 284 291 private Element getElementByTag(Element parent, String tag) { 292 List children = parent.getChildren(); 293 Iterator i = children.iterator(); 294 while (i.hasNext()) { 295 Element ele = (Element) i.next(); 296 if (ele.getName().equalsIgnoreCase(tag)) { 297 logger.debug("[" + tag + " Element found]"); 298 return ele; 299 } 300 logger.debug("[Parent Element is " + ele.getName() + " ]"); 301 } 302 logger.error("[" + tag + " Element not found]"); 303 return null; 304 } 305 306 307 313 private Element processHTMLElement(Document transformedDocument) { 314 Element html = null; 315 try { 316 html = transformedDocument.getRootElement(); 318 html.setName(HTML.Tag.DIV.toString()); 319 html.setAttribute("class", CssUtilities.HTML_CLASS); 321 return html; 322 } 323 catch (Exception ex) { 324 addParsingErrors("HTML: " + ex.toString()); 325 } 326 finally { 327 return html; 328 } 329 330 } 331 332 333 338 private void processHeadElement(Element html) { 339 try { 341 Element head = html.getChild(HTML.Tag.HEAD.toString()); 342 head.setName(HTML.Tag.DIV.toString()); 343 } 344 catch (Exception ex) { 345 addParsingErrors("Head: " + ex.toString()); 346 } 347 348 } 349 350 351 356 private void processTitleElement(Element html) { 357 try { 359 Element title = html.getChild(HTML.Tag.TITLE.toString()); 360 Parent parentTitle = title.getParent(); 361 parentTitle.removeContent(title); 362 } 363 catch (Exception ex) { 364 logger.warn("Title not found: " + ex.toString()); 365 } 366 367 } 368 369 370 375 private void processBodyElement(Document transformedDocument) { 376 try { 377 Element body = null; 379 body = getElementByTag(transformedDocument.getRootElement(), HTML.Tag.BODY.toString()); 380 body.setName(HTML.Tag.DIV.toString()); 381 Attribute classAtt = body.getAttribute(HTML.Attribute.CLASS.toString()); 382 if (classAtt == null) { 383 body.setAttribute("class", CssUtilities.BODY_CLASS); 384 } 385 else { 386 String classValue = classAtt.getValue(); 387 body.setAttribute("class", CssUtilities.BODY_CLASS + " " + classValue); 388 } 389 } 390 catch (Exception ex) { 391 addParsingErrors("Exception: " + ex.toString()); 392 } 393 394 } 395 396 397 398 405 private Element createLinkChewEle(Element ele, int position) { 406 logger.debug("Chew Ele tags."); 407 408 Element linkEle = new Element("a"); 410 String idAtt = ele.getAttributeValue("id"); 411 String hash = HashUtilities.buildTagHash(idAtt, position); 412 linkEle.setAttribute(HTML.Attribute.HREF.toString(), getHrefChewLink(hash)); 413 Element imgEle = new Element("img"); 415 imgEle.setAttribute(HTML.Attribute.SRC.toString(), getImageChewLinkSrcValue()); 416 linkEle.addContent(imgEle); 418 419 Element chewEleRes = (Element) ele.clone(); 421 chewEleRes.removeContent(); 422 chewEleRes.addContent(linkEle); 423 return chewEleRes; 424 } 425 426 427 433 private Element cloneWithoutChidren(Element ele) { 434 Element clone = (Element) ele.clone(); 435 clone.detach(); 436 List children = clone.getChildren(); 437 children.removeAll(children); 438 return clone; 439 } 440 441 442 448 private void processOnlyInputChildren(Element parent, Element parentClone) { 449 logger.debug("[process " + parent.getQualifiedName() + "]"); 450 List children = parent.getContent(); 452 Iterator i = children.iterator(); 453 while (i.hasNext()) { 454 Content content = (Content) i.next(); 455 if (content instanceof Element) { 456 Element ele = (Element) content; 457 Element clone = cloneWithoutChidren(ele); 459 processOnlyInputChildren(ele, clone); 460 parentClone.addContent(clone); 462 if (ele.getName().equalsIgnoreCase(HTML.Tag.INPUT.toString())) { 464 String type = ele.getAttributeValue(HTML.Attribute.TYPE.toString()); 466 if (editableInputParameter(type)) { 467 addParameterLabel(clone); 468 } 469 } 470 } 471 else { 472 parentClone.addContent((Content) content.clone()); 473 } 474 } 475 } 476 477 478 479 486 private void walkThrougthChildrenForTransform(Element parent, String formParentName, int nbLink) { 487 String parentName = parent.getName(); 489 490 List children = parent.getChildren(); 492 Iterator i = children.iterator(); 493 while (i.hasNext()) { 494 try { 495 Element ele = (Element) i.next(); 496 walkThrougthChildrenForTransform(ele, formParentName, nbLink); 497 498 if (ele.getName().equalsIgnoreCase(HTML.Tag.META.toString())) { 500 processMetaElement(ele); 501 } 502 503 if (ele.getAttributeValue("href") != null) { 505 if (!(ele.getAttributeValue("rel") != null && ele.getAttributeValue("rel").equalsIgnoreCase("stylesheet"))) { 507 processTagWhithHrefAttribute(ele, nbLink); 508 nbLink++; 509 } 510 511 } 512 513 if (ele.getAttributeValue("src") != null) { 515 processTagWhithSrcAttribute(ele); 516 } 517 518 if (ele.getAttributeValue("action") != null) { 520 formParentName = ele.getAttributeValue("name"); 521 if (formParentName == null) { 522 formParentName = ""; 523 } 524 formParentId = ele.getAttributeValue("id"); 525 if (formParentId == null) { 526 formParentId = ""; 527 } 528 529 processTagFORM(ele); 530 formParentPos++; 531 } 532 533 if (ele.getName().equalsIgnoreCase(HTML.Tag.INPUT.toString())) { 535 int paramPosition = 0; 536 processInputElement(ele, formParentName, formParentId, formParentPos, paramPosition); 537 } 538 539 if (ele.getName().equalsIgnoreCase(HTML.Tag.LINK.toString())) { 541 String relValue = ele.getAttributeValue(HTML.Attribute.REL.toString()); 542 if (relValue != null && relValue.equalsIgnoreCase("stylesheet")) { 543 processLinkElement(ele); 544 } 545 } 546 547 if (ele.getName().equalsIgnoreCase(HTML.Tag.STYLE.toString())) { 549 processStyleElement(ele); 550 } 551 552 if (ele.getName().equalsIgnoreCase(HTML.Tag.SCRIPT.toString())) { 554 if (ele.getAttributeValue(HTML.Attribute.SRC.toString()) != null) { 555 processImportedJavascript(ele); 556 } 557 else { 558 processIncludedJavascript(parentName, ele); 559 } 560 561 } 562 } 563 catch (WebClippingException ex) { 564 addParsingErrors(ex.getType() + ": " + ex.toString()); 565 } 566 catch (Exception ex) { 567 addParsingErrors("Exception: " + ex.toString()); 568 } 569 } 570 571 } 572 573 574 581 private void walkThrougthChildrenForUser(Element parent, String formParentName, int nbLink) { 582 String parentName = parent.getName(); 584 585 List children = parent.getChildren(); 587 Iterator i = children.iterator(); 588 while (i.hasNext()) { 589 try { 590 Element ele = (Element) i.next(); 591 walkThrougthChildrenForTransform(ele, formParentName, nbLink); 592 593 if (ele.getName().equalsIgnoreCase(HTML.Tag.META.toString())) { 595 processMetaElement(ele); 596 } 597 598 if (ele.getAttributeValue("href") != null) { 600 if (!(ele.getAttributeValue("rel") != null && ele.getAttributeValue("rel").equalsIgnoreCase("stylesheet"))) { 602 processTagWhithHrefAttribute(ele, nbLink); 603 nbLink++; 604 } 605 606 } 607 608 if (ele.getAttributeValue("action") != null) { 610 formParentName = ele.getAttributeValue("name"); 611 if (formParentName == null) { 612 formParentName = ""; 613 } 614 formParentId = ele.getAttributeValue("id"); 615 if (formParentId == null) { 616 formParentId = ""; 617 } 618 619 processTagFORM(ele); 620 formParentPos++; 621 } 622 623 if (ele.getName().equalsIgnoreCase(HTML.Tag.INPUT.toString())) { 625 int paramPosition = 0; 626 processInputElement(ele, formParentName, formParentId, formParentPos, paramPosition); 627 } 628 629 } 630 catch (WebClippingException ex) { 631 addParsingErrors(ex.getType() + ": " + ex.toString()); 632 } 633 catch (Exception ex) { 634 addParsingErrors("Exception: " + ex.toString()); 635 } 636 } 637 638 } 639 640 641 642 649 private Element walkThrougthChildrenForChew(Element parent, int position) { 650 logger.debug("[ Walk througth Children For Chew ]"); 651 652 Element root = (Element) parent.clone(); 653 root.removeContent(); 655 root.setText(parent.getTextTrim()); 656 657 List children = parent.getContent(); 659 Iterator i = children.iterator(); 660 while (i.hasNext()) { 661 Content content = (Content) i.next(); 662 if (content instanceof Element) { 663 Element ele = (Element) (content.clone()); 664 if (ele.getName().equalsIgnoreCase( 665 HTML.Tag.TABLE.toString())) { 666 Element chewEle = createLinkChewEle(ele, position); 668 chewEle.setAttribute(HTML.Attribute.BORDER.toString(), "1"); 669 chewEle.addContent(walkThrougthChildrenForChew(ele, position)); 670 root.addContent(chewEle); 671 position++; 672 } 673 else { 674 Element clone = walkThrougthChildrenForChew(ele, position); 675 root.addContent(clone); 676 } 677 } 678 else { 679 root.addContent((Content) content.clone()); 680 } 681 } 682 683 return root; 684 } 685 686 687 693 private void processLinkElement(Element ele) throws WebClippingException { 694 if (isEnableCSS()) { 695 try { 696 ele.setName(HTML.Tag.STYLE.toString()); 698 699 String href = ele.getAttributeValue("href"); 701 String media = ele.getAttributeValue("media"); 702 703 List atts = ele.getAttributes(); 705 for (int i = 0; i < atts.size(); i++) { 706 ele.removeAttribute((Attribute) atts.get(i)); 707 } 708 ele.removeContent(); 709 710 logger.debug("[ Linked CSS --> included Css ]"); 712 String content = getImportCssContent(href, media); 713 Comment style = new Comment(content); 714 ele.addContent(style); 715 ele.setText(content); 716 717 } 718 catch (WebClippingException ex) { 719 throw ex; 720 } 721 catch (Exception ex) { 722 throw new WebClippingException("Link", ex); 723 } 724 } 725 else { 727 logger.debug("[ CSS removed ]"); 728 } 729 730 } 731 732 733 739 private void processStyleElement(Element ele) throws WebClippingException { 740 741 try { 742 logger.debug("[ Process Inline css ]"); 744 String content = getInlineCssContent(ele.getTextTrim()); 745 ele.setText(content); 746 747 } 748 catch (WebClippingException ex) { 749 throw ex; 750 } 751 catch (Exception ex) { 752 throw new WebClippingException("Style", ex); 753 } 754 755 } 756 757 758 759 765 private void processImportedJavascript(Element ele) throws Exception { 766 String src = ele.getAttributeValue("src"); 767 ele.setText(getImportJavascriptContent(src, Constants.WEB_BROWSER_SHOW_BROWSE)); 768 ele.removeAttribute("src"); 769 } 770 771 772 778 private void processIncludedJavascript(String parentName, Element scriptEle) { 779 if (parentName.equalsIgnoreCase(HTML.Tag.BODY.toString())) { 781 return; 782 } 783 784 } 785 786 787 794 private void processTagWhithHrefAttribute(Element ele, int position) throws WebClippingException { 795 try { 796 String href = ele.getAttributeValue("href"); 797 String pos = "" + position; 798 String tagName = ele.getName(); 799 String tagId = ele.getAttributeValue("id"); 800 if (tagId == null) { 801 ele.setAttribute("id", HashUtilities.buildLinkHash(href, tagId, pos)); 802 } 803 804 ele.setAttribute("href", this.getRewritedHrefValue(tagName, href, tagId, pos, Constants.WEB_BROWSER_SHOW_BROWSE)); 805 806 } 807 catch (Exception ex) { 808 throw new WebClippingException("Attribute Href", ex); 809 } 810 } 811 812 813 819 private void processTagFORM(Element ele) throws WebClippingException { 820 try { 821 String url = ele.getAttributeValue("action"); 823 String method = ele.getAttributeValue("method"); 824 String formName = ele.getAttributeValue("name"); 825 String formId = ele.getAttributeValue("id"); 826 String enctype = ele.getAttributeValue("enctype"); 827 ele.setAttribute("action", getRewritedActionValue(url, method, formName, formId, enctype, formParentPos, Constants.WEB_BROWSER_SHOW_BROWSE)); 828 ele.setAttribute("method", "post"); 829 830 873 } 874 catch (Exception ex) { 875 throw new WebClippingException("Form tag", ex); 876 } 877 878 } 879 880 881 886 private void processMetaElement(Element ele) { 887 String httpEquiv = ele.getAttributeValue(HTML.Attribute.HTTPEQUIV.toString()); 889 if (httpEquiv != null) { 890 if ("refresh".equals(httpEquiv.toLowerCase())) { 892 String content = ele.getAttributeValue(HTML.Attribute.CONTENT.toString()); 893 int urlIndex = content.toLowerCase().indexOf("url"); 895 if (urlIndex != -1) { 896 String contentBeforeURL = content.substring(0, urlIndex).trim(); 898 logger.debug("[ Content before url " + contentBeforeURL + " ]"); 899 900 int equalIndex = urlIndex + 3; 902 String contentURL = content.substring(equalIndex + 1).trim(); 903 logger.debug("[ BEFORE meta http-equiv=[" + httpEquiv + "] content=[" + content + "] ]"); 904 logger.debug("[ Refresh url is [" + contentURL + " ] ]"); 905 906 String refreshURL = URLUtilities.getHrefAbsoluteValue(this.getDocument().getUrlBean().getBaseURL(), contentURL); 908 getDocument().getUrlBean().setRedirectUrl(URLUtilities.getURL(refreshURL)); 909 String [] params = {refreshURL, "metaLink"}; 910 String hash = HashUtilities.buildLinkHash(refreshURL, "", ""); 911 URLWrapper urlWrapper = new URLWrapper(refreshURL, WebConstants.FROM_META, "GET", "", hash); 912 UrlEncoderIF encoder = getUrlEncoder(); 914 encoder.setAction("browse"); 915 encoder.setUrlWrapper(urlWrapper); 916 917 String encodedUrl = encoder.getEncodedUrl(); 918 919 ele.setAttribute(HTML.Attribute.CONTENT.toString(), contentBeforeURL + "url=" + encodedUrl); 921 logger.debug("[ AFTER meta http-equiv=[" + httpEquiv + "] content=[" + contentBeforeURL + "url=" + encodedUrl + "] ]+"); 922 } 923 } 924 } 925 926 } 927 928 929 930 940 private void processInputElement(Element ele, String formParentName, String formParentId, int formParentPos, int paramPosition) throws WebClippingException { 941 try { 942 String type = ele.getAttributeValue(HTML.Attribute.TYPE.toString()); 943 String visibility = Boolean.TRUE.toString(); 944 type = notNullValueForType(type); 945 946 String name = notNullValueForType(ele.getAttributeValue(HTML.Attribute.NAME.toString())); 948 String possibleValue = notNullValueForType(ele.getAttributeValue(HTML.Attribute.VALUE.toString())); 949 950 recordFormParam(type, formParentName, formParentId, formParentPos, name, possibleValue, visibility, paramPosition); 952 } 953 catch (Exception ex) { 954 throw new WebClippingException("Parser, Input Element", ex); 955 } 956 957 } 958 959 960 961 967 private void addParameterLabel(Element ele) { 968 Element parentEle = ele.getParentElement(); 969 int pos = parentEle.indexOf(ele); 970 String type = ele.getAttributeValue(HTML.Attribute.TYPE.toString()); 971 String name = ele.getAttributeValue(HTML.Attribute.NAME.toString()); 972 String value = ele.getAttributeValue(HTML.Attribute.VALUE.toString()); 973 974 Element labelEle = new Element(HTML.Tag.I.toString()); 977 labelEle.setText(getLabelText(name, value, type)); 978 parentEle.addContent(pos, labelEle); 979 } 980 981 982 983 989 private Element buildInputElement(Attribute[] att) { 990 Element inputElement = new Element(HTML.Tag.INPUT.toString()); 991 for (int i = 0; i < att.length; i++) { 992 inputElement.setAttribute(att[i]); 993 } 994 return inputElement; 995 } 996 997 998 1004 private void processTagWhithSrcAttribute(Element ele) throws WebClippingException { 1005 try { 1006 String src = ele.getAttributeValue("src"); 1007 ele.setAttribute("src", relatifToAbsolute(src)); 1008 } 1009 catch (Exception ex) { 1010 throw new WebClippingException("Attribut src", ex); 1011 } 1012 } 1013 1014} 1015 | Popular Tags |