1 11 package org.eclipse.ui.internal.intro.impl.html; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.net.URL ; 20 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.ui.internal.intro.impl.IIntroConstants; 23 import org.eclipse.ui.internal.intro.impl.IntroPlugin; 24 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement; 25 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement; 26 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage; 27 import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider; 28 import org.eclipse.ui.internal.intro.impl.model.IntroGroup; 29 import org.eclipse.ui.internal.intro.impl.model.IntroHTML; 30 import org.eclipse.ui.internal.intro.impl.model.IntroHead; 31 import org.eclipse.ui.internal.intro.impl.model.IntroImage; 32 import org.eclipse.ui.internal.intro.impl.model.IntroInjectedIFrame; 33 import org.eclipse.ui.internal.intro.impl.model.IntroLink; 34 import org.eclipse.ui.internal.intro.impl.model.IntroPageTitle; 35 import org.eclipse.ui.internal.intro.impl.model.IntroSeparator; 36 import org.eclipse.ui.internal.intro.impl.model.IntroText; 37 import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager; 38 import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil; 39 import org.eclipse.ui.internal.intro.impl.util.Log; 40 import org.eclipse.ui.intro.config.IIntroContentProvider; 41 import org.eclipse.ui.intro.config.IIntroContentProviderSite; 42 43 public class IntroHTMLGenerator { 44 45 private AbstractIntroPage introPage; 46 47 private IIntroContentProviderSite providerSite; 48 49 58 public HTMLElement generateHTMLforPage(AbstractIntroPage page, IIntroContentProviderSite providerSite) { 59 if (page == null) 60 return null; 61 this.introPage = page; 62 this.providerSite = providerSite; 63 64 return generateHTMLElement(); 68 } 69 70 76 77 95 private HTMLElement generateHTMLElement() { 96 int indentLevel = 0; 98 HTMLElement html = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_HTML, indentLevel, true); 99 HTMLElement head = generateHeadElement(indentLevel + 1); 100 HTMLElement body = generateBodyElement(indentLevel + 1, head); 101 html.addContent(head); 102 html.addContent(body); 103 return html; 104 } 105 106 126 private HTMLElement generateHeadElement(int indentLevel) { 127 HTMLElement head = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_HEAD, indentLevel, true); 128 head.addContent(generateTitleElement(null, indentLevel + 1)); 130 String basePath = BundleUtil.getResolvedResourceLocation(introPage.getBase(), introPage.getBundle()); 132 HTMLElement base = generateBaseElement(indentLevel + 1, basePath); 133 if (base != null) 134 head.addContent(base); 135 head.addContent(generateStyleElement(indentLevel + 1)); 137 String [] presentationStyles = IntroPlugin.getDefault().getIntroModelRoot().getPresentation() 139 .getImplementationStyles(); 140 if (presentationStyles != null && introPage.injectSharedStyle()) { 141 for (int i=0; i<presentationStyles.length; i++) 142 head.addContent(generateLinkElement(presentationStyles[i], indentLevel + 1)); 143 } 144 String pageStyle = introPage.getStyle(); 145 if (pageStyle != null) 146 head.addContent(generateLinkElement(pageStyle, indentLevel + 1)); 147 head.addContent(generateJavascriptElement(indentLevel + 1)); 149 150 String [] pageStyles = introPage.getStyles(); 152 for (int i = 0; i < pageStyles.length; i++) { 153 pageStyle = pageStyles[i]; 154 if (pageStyle != null) 155 head.addContent(generateLinkElement(pageStyle, indentLevel + 1)); 156 } 157 StringBuffer content = null; 163 IntroHead introHead = IntroPlugin.getDefault().getIntroModelRoot().getPresentation().getHead(); 164 if (introHead != null) { 165 content = readFromFile(introHead.getSrc(), introHead.getInlineEncoding()); 166 if (content != null) 167 head.addContent(content); 168 } 169 IntroHead[] htmlHeads = introPage.getHTMLHeads(); 173 for (int i = 0; i < htmlHeads.length; i++) { 174 introHead = htmlHeads[i]; 175 if (introHead != null) { 176 content = readFromFile(introHead.getSrc(), introHead.getInlineEncoding()); 177 if (content != null) 178 head.addContent(content); 179 } 180 } 181 return head; 182 } 183 184 private HTMLElement generateJavascriptElement(int indentLevel) { 185 String rel = "javascript/common.js"; String abs = BundleUtil.getResolvedResourceLocation(rel, IntroPlugin.getDefault().getBundle()); 187 HTMLElement jselement = new FormattedHTMLElement("script", indentLevel, false); jselement.addAttribute("type", "text/javascript"); jselement.addAttribute("src", abs); return jselement; 191 } 192 193 211 private HTMLElement generateBodyElement(int indentLevel, HTMLElement head) { 212 HTMLElement body = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_BODY, indentLevel, true); 213 String pageId = (introPage.getId() != null) ? introPage.getId() : IIntroHTMLConstants.DIV_ID_PAGE; 215 HTMLElement pageContentDiv = generateDivElement(pageId, indentLevel + 1); 216 if (introPage.getStyleId() != null) 217 pageContentDiv.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, introPage.getStyleId()); 218 if (introPage.getBackgroundImage() != null) 219 pageContentDiv.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, 220 "background-image : url(" + introPage.getBackgroundImage() + ")"); 222 AbstractIntroElement[] children = introPage.getChildren(); 224 for (int i = 0; i < children.length; i++) { 225 AbstractIntroElement child = children[i]; 226 HTMLElement childElement = generateIntroElement(child, indentLevel + 2); 229 if (childElement != null) { 230 addMixinStyle(childElement, child.getMixinStyle()); 231 pageContentDiv.addContent(childElement); 232 } 233 } 234 body.addContent(pageContentDiv); 235 return body; 236 } 237 238 247 private HTMLElement generateIntroElement(AbstractIntroElement element, int indentLevel) { 248 if (element == null) 249 return null; 250 if (filteredFromPresentation(element)) 253 return null; 254 switch (element.getType()) { 255 case AbstractIntroElement.GROUP: 256 return generateIntroDiv((IntroGroup) element, indentLevel); 257 case AbstractIntroElement.LINK: 258 return generateIntroLink((IntroLink) element, indentLevel); 259 case AbstractIntroElement.HTML: 260 return generateIntroHTML((IntroHTML) element, indentLevel); 261 case AbstractIntroElement.CONTENT_PROVIDER: 262 return generateIntroContent((IntroContentProvider) element, indentLevel); 263 case AbstractIntroElement.IMAGE: 264 return generateIntroImage((IntroImage) element, indentLevel); 265 case AbstractIntroElement.HR: 266 return generateIntroSeparator((IntroSeparator) element, indentLevel); 267 case AbstractIntroElement.TEXT: 268 return generateIntroText((IntroText) element, indentLevel); 269 case AbstractIntroElement.PAGE_TITLE: 270 return generateIntroTitle((IntroPageTitle) element, indentLevel); 271 case AbstractIntroElement.INJECTED_IFRAME: 272 return generateIntroInjectedIFrame((IntroInjectedIFrame) element, indentLevel); 273 default: 274 return null; 275 } 276 } 277 278 297 private HTMLElement generateIntroDiv(IntroGroup element, int indentLevel) { 298 HTMLElement divElement = generateDivElement(element.getId(), indentLevel); 300 HTMLElement childContainer = divElement; 301 if (element.getStyleId() != null) 303 divElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, element.getStyleId()); 304 if (element.getLabel() != null) { 306 if (element.isExpandable()) { 307 HTMLElement divLabel = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_SPAN, 308 indentLevel + 2, false); 309 divLabel.addContent(element.getLabel()); 310 divLabel.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, 311 "section-title"); String clientId = element.getId() + "-content"; String toggleClosedId = element.getId() + "-toggle-closed"; String toggleOpenId = element.getId() + "-toggle-open"; String href = "#"; HTMLElement link = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_ANCHOR, 317 indentLevel + 1, true); 318 link.addAttribute(IIntroHTMLConstants.ATTRIBUTE_HREF, href); 319 link.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, "section-title-link"); StringBuffer call = new StringBuffer (); 321 call.append("return (toggleSection('"); call.append(clientId); 323 call.append("','"); call.append(toggleClosedId); 325 call.append("','"); call.append(toggleOpenId); 327 call.append("'))"); link.addAttribute("onClick", call.toString()); link.addContent(divLabel); 330 divElement.addContent(link); 331 HTMLElement toggleImageClosed = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_IMG, 333 indentLevel + 2, false, false); 334 toggleImageClosed.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, toggleClosedId); 335 toggleImageClosed.addAttribute(IIntroHTMLConstants.ATTRIBUTE_SRC, BundleUtil 336 .getResolvedResourceLocation(IIntroHTMLConstants.IMAGE_SRC_BLANK, 337 IIntroConstants.PLUGIN_ID)); 338 toggleImageClosed.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, "section-toggle-image-closed"); if (element.isExpanded()) 340 toggleImageClosed.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, "display: none"); link.addContent(toggleImageClosed); 342 HTMLElement toggleImageOpen = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_IMG, 343 indentLevel + 2, false, false); 344 toggleImageOpen.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, toggleOpenId); 345 toggleImageOpen.addAttribute(IIntroHTMLConstants.ATTRIBUTE_SRC, BundleUtil 346 .getResolvedResourceLocation(IIntroHTMLConstants.IMAGE_SRC_BLANK, 347 IIntroConstants.PLUGIN_ID)); 348 toggleImageOpen.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, "section-toggle-image-open"); if (element.isExpanded()) 350 toggleImageOpen.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, "display: inline"); link.addContent(toggleImageOpen); 352 childContainer = generateDivElement(clientId, indentLevel + 1); 353 childContainer.addAttribute("class", "section-body"); if (element.isExpanded()) 355 childContainer.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, "display: block"); divElement.addContent(childContainer); 357 } else { 358 HTMLElement divLabel = generateTextElement(IIntroHTMLConstants.ELEMENT_H4, null, 359 IIntroHTMLConstants.SPAN_CLASS_DIV_LABEL, element.getLabel(), indentLevel + 1); 360 divElement.addContent(divLabel); 361 } 362 } 363 if (element.getBackgroundImage() != null) { 364 String imageUrl = element.getBackgroundImage(); 365 imageUrl = BundleUtil.getResolvedResourceLocation(element.getBase(), imageUrl, element 366 .getBundle()); 367 String style; 368 if (Platform.getWS().equals(Platform.WS_WIN32) && imageUrl.toLowerCase().endsWith(".png")) { style = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageUrl + "', sizingMethod='crop');"; } else { 373 style = "background-image : url(" + imageUrl + ")"; } 375 divElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, style); 376 } 377 AbstractIntroElement[] children = element.getChildren(); 379 for (int i = 0; i < children.length; i++) { 380 AbstractIntroElement child = children[i]; 381 HTMLElement childElement = generateIntroElement(child, indentLevel + 1); 382 if (childElement != null) { 383 addMixinStyle(childElement, child.getMixinStyle()); 384 childContainer.addContent(childElement); 385 } 386 } 387 return divElement; 388 } 389 390 private void addMixinStyle(HTMLElement element, String mixinStyle) { 391 if (mixinStyle == null) 392 return; 393 String key = "class"; String original = (String ) element.getElementAttributes().get(key); 395 if (original == null) 396 original = mixinStyle; 397 else 398 original += " " + mixinStyle; element.addAttribute(key, original); 400 } 401 402 421 private HTMLElement generateIntroLink(IntroLink element, int indentLevel) { 422 HTMLElement anchor = generateAnchorElement(element, indentLevel); 423 String blankImageURL = BundleUtil.getResolvedResourceLocation(IIntroHTMLConstants.IMAGE_SRC_BLANK, 425 IIntroConstants.PLUGIN_ID); 426 if (blankImageURL != null) { 427 anchor.addContent(generateImageElement(blankImageURL, null, IIntroHTMLConstants.IMAGE_CLASS_BG, 428 indentLevel + 1)); 429 } 430 if (element.getImg() != null) { 432 HTMLElement img = generateIntroElement(element.getImg(), indentLevel + 1); 433 if (img != null) 434 anchor.addContent(img); 435 } 436 HTMLElement imageDiv = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_DIV, indentLevel+1, false); 437 imageDiv.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, 438 IIntroHTMLConstants.LINK_EXTRA_DIV); 439 anchor.addContent(imageDiv); 440 if (element.getLabel() != null) { 442 HTMLElement label = generateSpanElement(IIntroHTMLConstants.SPAN_CLASS_LINK_LABEL, 443 indentLevel + 1); 444 label.addContent(element.getLabel()); 445 anchor.addContent(label); 446 } 447 IntroText linkText = element.getIntroText(); 448 if (linkText != null && linkText.getText() != null) { 449 HTMLElement text = generateIntroElement(linkText, indentLevel + 1); 450 if (text != null) 451 anchor.addContent(text); 452 } 453 return anchor; 454 } 455 456 468 private HTMLElement generateIntroHTML(IntroHTML element, int indentLevel) { 469 if (element.isInlined()) 470 return generateInlineIntroHTML(element, indentLevel); 471 472 return generateEmbeddedIntroHTML(element, indentLevel); 473 } 474 475 490 private HTMLElement generateIntroImage(IntroImage element, int indentLevel) { 491 HTMLElement imageElement = generateImageElement(element.getSrc(), element.getAlt(), element 492 .getStyleId(), indentLevel); 493 if (element.getId() != null) 494 imageElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, element.getId()); 495 return imageElement; 496 } 497 498 private HTMLElement generateIntroSeparator(IntroSeparator element, int indentLevel) { 499 HTMLElement hrElement = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_HR, indentLevel, false); 500 if (element.getId() != null) 501 hrElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, element.getId()); 502 if (element.getStyleId() != null) 503 hrElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, element.getStyleId()); 504 return hrElement; 505 } 506 507 525 private HTMLElement generateIntroText(IntroText element, int indentLevel) { 526 String spanClass = (element.getStyleId() != null) ? element.getStyleId() 527 : IIntroHTMLConstants.SPAN_CLASS_TEXT; 528 HTMLElement textElement = generateTextElement(IIntroHTMLConstants.ELEMENT_PARAGRAPH, element.getId(), 529 spanClass, element.getText(), indentLevel); 530 return textElement; 531 } 532 533 538 private HTMLElement generateIntroInjectedIFrame(IntroInjectedIFrame element, int indentLevel) { 539 HTMLElement iframe = generateIFrameElement(element.getIFrameURL(), "0", "auto", indentLevel); return iframe; 542 } 543 544 549 private HTMLElement generateIntroTitle(IntroPageTitle element, int indentLevel) { 550 HTMLElement titleElement = generateHeaderDiv(element.getId(), element.getStyleId(), 551 IIntroHTMLConstants.ELEMENT_H1, element.getTitle(), indentLevel); 552 return titleElement; 553 } 554 555 574 private HTMLElement generateInlineIntroHTML(IntroHTML element, int indentLevel) { 575 StringBuffer content = readFromFile(element.getSrc(), element.getInlineEncoding()); 579 if (content != null && content.length() > 0) { 580 String divClass = (element.getStyleId() != null) ? element.getStyleId() 582 : IIntroHTMLConstants.DIV_CLASS_INLINE_HTML; 583 HTMLElement divElement = generateDivElement(element.getId(), divClass, indentLevel); 584 divElement.addContent(content); 586 return divElement; 587 } 588 return null; 589 } 590 591 598 private HTMLElement generateIntroContent(IntroContentProvider element, int indentLevel) { 599 HTMLElement divElement = generateDivElement(element.getId(), 601 IIntroHTMLConstants.DIV_CLASS_PROVIDED_CONTENT, indentLevel); 602 603 IIntroContentProvider providerClass = ContentProviderManager.getInst().getContentProvider(element); 606 if (providerClass == null) 607 providerClass = ContentProviderManager.getInst().createContentProvider(element, providerSite); 609 610 if (providerClass != null) { 611 StringWriter stringWriter = new StringWriter (); 612 PrintWriter pw = new PrintWriter (stringWriter); 613 providerClass.createContent(element.getId(), pw); 615 stringWriter.flush(); 617 divElement.addContent(stringWriter.toString()); 618 pw.close(); 619 } else { 620 IntroText htmlText = element.getIntroText(); 623 if (htmlText != null && htmlText.getText() != null) { 624 String textClass = (htmlText.getStyleId() != null) ? htmlText.getStyleId() 625 : IIntroHTMLConstants.SPAN_CLASS_TEXT; 626 HTMLElement text = generateTextElement(IIntroHTMLConstants.ELEMENT_PARAGRAPH, htmlText 627 .getId(), textClass, element.getText(), indentLevel); 628 if (text != null) 629 divElement.addContent(text); 630 } 631 } 632 return divElement; 633 } 634 635 653 private HTMLElement generateEmbeddedIntroHTML(IntroHTML element, int indentLevel) { 654 HTMLElement objectElement = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_OBJECT, indentLevel, 655 true); 656 objectElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_TYPE, IIntroHTMLConstants.OBJECT_TYPE); 657 if (element.getId() != null) 658 objectElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, element.getId()); 659 if (element.getSrc() != null) 660 objectElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_DATA, element.getSrc()); 661 if (element.getStyleId() != null) 662 objectElement.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, element.getStyleId()); 663 IntroText htmlText = element.getIntroText(); 666 if (htmlText != null && htmlText.getText() != null) { 667 String textClass = (htmlText.getStyleId() != null) ? htmlText.getStyleId() 668 : IIntroHTMLConstants.SPAN_CLASS_TEXT; 669 HTMLElement text = generateTextElement(IIntroHTMLConstants.ELEMENT_PARAGRAPH, htmlText.getId(), 670 textClass, element.getText(), indentLevel); 671 if (text != null) 672 objectElement.addContent(text); 673 } 674 if (element.getIntroImage() != null) { 675 HTMLElement img = generateIntroImage(element.getIntroImage(), indentLevel); 676 if (img != null) 677 objectElement.addContent(img); 678 } 679 return objectElement; 680 } 681 682 696 private HTMLElement generateBaseElement(int indentLevel, String baseURL) { 697 HTMLElement base = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_BASE, indentLevel, true, 698 false); 699 if (baseURL != null) 700 base.addAttribute(IIntroHTMLConstants.ATTRIBUTE_HREF, baseURL); 701 return base; 702 } 703 704 717 private HTMLElement generateStyleElement(int indentLevel) { 718 HTMLElement style = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_STYLE, indentLevel, false); 719 style.addAttribute(IIntroHTMLConstants.ATTRIBUTE_TYPE, IIntroHTMLConstants.LINK_STYLE); 720 style.addContent(IIntroHTMLConstants.STYLE_HTML); 721 return style; 722 } 723 724 739 private HTMLElement generateTitleElement(String title, int indentLevel) { 740 HTMLElement titleElement = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_TITLE, indentLevel, 741 false); 742 if (title != null) 743 titleElement.addContent(title); 744 return titleElement; 745 } 746 747 762 private HTMLElement generateLinkElement(String href, int indentLevel) { 763 HTMLElement link = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_LINK, indentLevel, true, 764 false); 765 link.addAttribute(IIntroHTMLConstants.ATTRIBUTE_RELATIONSHIP, IIntroHTMLConstants.LINK_REL); 766 link.addAttribute(IIntroHTMLConstants.ATTRIBUTE_TYPE, IIntroHTMLConstants.LINK_STYLE); 767 if (href != null) 768 link.addAttribute(IIntroHTMLConstants.ATTRIBUTE_HREF, href); 769 return link; 770 } 771 772 787 private HTMLElement generateAnchorElement(IntroLink link, int indentLevel) { 788 HTMLElement anchor = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_ANCHOR, indentLevel, true); 789 if (link.getId() != null) 790 anchor.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, link.getId()); 791 if (link.getUrl() != null) 792 anchor.addAttribute(IIntroHTMLConstants.ATTRIBUTE_HREF, link.getUrl()); 793 if (link.getStyleId() != null) 794 anchor.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, link.getStyleId()); 795 else 796 anchor.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, IIntroHTMLConstants.ANCHOR_CLASS_LINK); 797 return anchor; 798 } 799 800 824 private HTMLElement generateHeaderDiv(String divId, String divClass, String headerType, 825 String spanContent, int indentLevel) { 826 HTMLElement text = generateTextElement(headerType, null, null, spanContent, indentLevel + 1); 828 HTMLElement div = generateDivElement(divId, divClass, indentLevel); 830 div.addContent(text); 831 return div; 832 } 833 834 857 private HTMLElement generateTextElement(String type, String spanID, String spanClass, String spanContent, 858 int indentLevel) { 859 HTMLElement span = new HTMLElement(IIntroHTMLConstants.ELEMENT_SPAN); 861 if (spanID != null) 862 span.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, spanID); 863 if (spanClass != null) 864 span.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, spanClass); 865 if (spanContent != null) 866 span.addContent(spanContent); 867 HTMLElement text = new FormattedHTMLElement(type, indentLevel, false); 869 text.addContent(span); 870 return text; 871 } 872 873 884 private HTMLElement generateDivElement(String divId, String divClass, int indentLevel) { 885 HTMLElement div = generateDivElement(divId, indentLevel); 886 div.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, divClass); 887 return div; 888 } 889 890 899 private HTMLElement generateDivElement(String divId, int indentLevel) { 900 HTMLElement div = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_DIV, indentLevel, true); 901 if (divId != null) 902 div.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ID, divId); 903 return div; 904 } 905 906 922 private HTMLElement generateImageElement(String imageSrc, String altText, String imageClass, 923 int indentLevel) { 924 HTMLElement image = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_IMG, indentLevel, true, 925 false); 926 boolean pngOnWin32 = imageSrc != null && Platform.getWS().equals(Platform.WS_WIN32) 927 && imageSrc.toLowerCase().endsWith(".png"); if (imageSrc == null || pngOnWin32) { 929 String blankImageURL = BundleUtil.getResolvedResourceLocation( 934 IIntroHTMLConstants.IMAGE_SRC_BLANK, IIntroConstants.PLUGIN_ID); 935 if (blankImageURL != null) { 936 image.addAttribute(IIntroHTMLConstants.ATTRIBUTE_SRC, blankImageURL); 937 if (pngOnWin32) { 938 String style = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageSrc + "', sizingMethod='image')"; image.addAttribute(IIntroHTMLConstants.ATTRIBUTE_STYLE, style); 940 } 941 } 942 } else 943 image.addAttribute(IIntroHTMLConstants.ATTRIBUTE_SRC, imageSrc); 944 if (altText == null) 945 altText = ""; image.addAttribute(IIntroHTMLConstants.ATTRIBUTE_ALT, altText); 947 if (imageClass != null) 948 image.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, imageClass); 949 return image; 950 } 951 952 968 private HTMLElement generateSpanElement(String spanClass, int indentLevel) { 969 HTMLElement span = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_SPAN, indentLevel, false); 970 span.addAttribute(IIntroHTMLConstants.ATTRIBUTE_CLASS, spanClass); 971 return span; 972 } 973 974 988 private HTMLElement generateIFrameElement(String src, String frameborder, String scrolling, 989 int indentLevel) { 990 HTMLElement iframe = new FormattedHTMLElement(IIntroHTMLConstants.ELEMENT_IFrame, indentLevel, false); 991 if (src != null) 992 iframe.addAttribute(IIntroHTMLConstants.ATTRIBUTE_SRC, src); 993 if (frameborder != null) 994 iframe.addAttribute(IIntroHTMLConstants.ATTRIBUTE_FRAMEBORDER, frameborder); 995 if (scrolling != null) 996 iframe.addAttribute(IIntroHTMLConstants.ATTRIBUTE_SCROLLING, scrolling); 997 return iframe; 998 } 999 1000 1001 1002 1003 private boolean filteredFromPresentation(AbstractIntroElement element) { 1004 if (element.isOfType(AbstractIntroElement.BASE_ELEMENT)) 1005 return ((AbstractBaseIntroElement) element).isFiltered(); 1006 1007 return false; 1008 } 1009 1010 1023 private StringBuffer readFromFile(String src, String charsetName) { 1024 if (src == null) 1025 return null; 1026 InputStream stream = null; 1027 StringBuffer content = new StringBuffer (); 1028 BufferedReader reader = null; 1029 try { 1030 URL url = new URL (src); 1031 stream = url.openStream(); 1032 if (charsetName == null) 1036 reader = new BufferedReader (new InputStreamReader (stream)); 1037 else 1038 reader = new BufferedReader (new InputStreamReader (stream, charsetName)); 1039 while (true) { 1040 int character = reader.read(); 1041 if (character == -1) break; 1044 else if (character == PluginIdParser.SUBSTITUTION_BEGIN) { PluginIdParser parser = new PluginIdParser(character, reader); 1047 String text = parser.parsePluginId(); 1053 if (text != null) 1054 content.append(text); 1055 } else { 1056 if (character > 0x00 && character < 0xffff) 1058 content.append((char) character); 1059 else 1060 content.append(character); 1061 } 1062 } 1063 } catch (Exception exception) { 1064 Log.error("Error reading from file", exception); } finally { 1066 try { 1067 if (reader != null) 1068 reader.close(); 1069 if (stream != null) 1070 stream.close(); 1071 } catch (IOException e) { 1072 Log.error("Error closing input stream", e); return null; 1074 } 1075 } 1076 return content; 1077 } 1078 1079 1096 private static class PluginIdParser { 1097 1098 private BufferedReader reader; 1099 1100 private static final char SUBSTITUTION_BEGIN = '$'; 1101 1102 private static final char SUBSTITUTION_END = '$'; 1103 1104 private StringBuffer tokenContent; 1107 1108 private StringBuffer pluginId; 1111 1112 protected PluginIdParser(char tokenBegin, BufferedReader bufferedreader) { 1113 reader = bufferedreader; 1114 tokenContent = new StringBuffer (tokenBegin); 1115 pluginId = new StringBuffer (); 1116 } 1117 1118 protected PluginIdParser(int tokenBegin, BufferedReader bufferedreader) { 1119 reader = bufferedreader; 1120 tokenContent = new StringBuffer (); 1121 pluginId = new StringBuffer (); 1122 if (tokenBegin > 0x00 && tokenBegin < 0xffff) 1124 tokenContent.append((char) tokenBegin); 1125 } 1126 1127 1139 protected String parsePluginId() { 1140 if (reader == null || tokenContent == null || pluginId == null) 1141 return null; 1142 1143 try { 1144 reader.mark(0x400); 1149 if (findValidPluginSegment()) { 1150 String pluginPath = getPluginPath(); 1151 if (pluginPath == null) { 1152 return tokenContent.toString(); 1158 } 1159 return pluginPath; 1160 } 1161 1162 reader.reset(); 1165 return tokenContent.toString(); 1166 1167 } catch (IOException exception) { 1168 Log.error("Error reading from file", exception); return tokenContent.toString(); 1170 } 1171 } 1172 1173 1186 private boolean findValidPluginSegment() { 1187 final char[] PLUGIN_SEGMENT = { 'p', 'l', 'u', 'g', 'i', 'n', ':' }; 1188 char[] streamContent = new char[PLUGIN_SEGMENT.length]; 1189 try { 1190 int peek = reader.read(streamContent, 0, PLUGIN_SEGMENT.length); 1191 if ((peek == PLUGIN_SEGMENT.length) 1192 && (HTMLUtil.equalCharArrayContent(streamContent, PLUGIN_SEGMENT))) { 1193 tokenContent.append(streamContent); 1195 return true; 1196 } 1197 return false; 1200 } catch (IOException exception) { 1201 Log.error("Error reading from file", exception); return false; 1203 } 1204 } 1205 1206 1218 private String getPluginPath() { 1219 try { 1220 while (true) { 1221 int nextChar = reader.read(); 1222 1223 if (nextChar == -1) { 1224 return null; 1226 } else if (nextChar == SUBSTITUTION_END) { String path = BundleUtil.getResolvedBundleLocation(pluginId.toString()); 1232 1233 if (path == null) 1237 reader.reset(); 1238 1239 return path; 1240 } else { reader.mark(0x400); 1245 if (nextChar > 0x00 && nextChar < 0xffff) { 1250 tokenContent.append((char) nextChar); 1251 if (!Character.isWhitespace((char) nextChar)) 1254 pluginId.append((char) nextChar); 1255 } else { 1256 tokenContent.append(nextChar); 1257 pluginId.append(nextChar); 1258 } 1259 } 1260 } 1261 } catch (IOException exception) { 1262 Log.error("Error reading from file", exception); return null; 1264 } 1265 } 1266 } 1267 1268} 1269 | Popular Tags |