1 16 package com.blandware.atleap.webapp.taglib.core.content.editor; 17 18 import com.blandware.atleap.common.util.StringUtil; 19 import com.blandware.atleap.webapp.util.core.GlobalProperties; 20 import com.blandware.atleap.webapp.util.core.LocaleUtil; 21 import com.blandware.atleap.webapp.util.core.RequestUtil; 22 import com.blandware.atleap.webapp.util.core.WebappConstants; 23 import org.apache.commons.validator.GenericValidator; 24 import org.apache.struts.taglib.TagUtils; 25 import org.apache.struts.util.RequestUtils; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.SimpleTagSupport ; 31 import java.io.IOException ; 32 import java.io.StringWriter ; 33 import java.util.HashMap ; 34 import java.util.Locale ; 35 import java.util.Map ; 36 37 93 public class ContentEditorTag extends SimpleTagSupport { 94 95 97 100 public static final String EDITOR_TYPE_TINYMCE = "TinyMCE"; 101 102 105 public static final String EDITOR_TYPE_FCKEDITOR = "FCKEditor"; 106 107 110 public static final String EDITOR_AREA_CSS_NO_STYLESHEET = "no_stylesheet"; 111 112 115 public static final String TINY_MCE_COUNT_KEY = "com.blandware.atleap.webapp.taglib.core.content.editor.TINY_MCE_COUNT"; 116 117 120 public static final String FCKEDITOR_COUNT_KEY = "com.blandware.atleap.webapp.taglib.core.content.editor.FCKEDITOR_COUNT"; 121 122 125 protected int tinyMCECount = 0; 126 127 130 protected String type = null; 131 132 134 137 protected String id; 138 139 142 protected String basePath = null; 143 144 147 protected String defaultLanguage; 148 149 152 protected String baseHref; 153 154 157 protected String editorAreaLanguage; 158 159 162 protected Boolean fullPage; 163 164 168 protected String editorAreaCss; 169 170 173 protected String width = null; 174 175 178 protected String height = null; 179 180 189 public String getType() { 190 return type; 191 } 192 193 198 public void setType(String type) { 199 this.type = type; 200 } 201 202 203 212 public String getId() { 213 return id; 214 } 215 216 221 public void setId(String value) { 222 id = value; 223 } 224 225 234 public String getBasePath() { 235 return basePath; 236 } 237 238 243 public void setBasePath(String value) { 244 basePath = value; 245 } 246 247 256 public String getDefaultLanguage() { 257 return defaultLanguage; 258 } 259 260 265 public void setDefaultLanguage(String defaultLanguage) { 266 this.defaultLanguage = defaultLanguage; 267 } 268 269 279 public String getBaseHref() { 280 return baseHref; 281 } 282 283 289 public void setBaseHref(String baseHref) { 290 this.baseHref = baseHref; 291 } 292 293 303 public String getEditorAreaLanguage() { 304 return editorAreaLanguage; 305 } 306 307 313 public void setEditorAreaLanguage(String editorAreaLanguage) { 314 this.editorAreaLanguage = editorAreaLanguage; 315 } 316 317 327 public Boolean getFullPage() { 328 return fullPage; 329 } 330 331 337 public void setFullPage(Boolean fullPage) { 338 this.fullPage = fullPage; 339 } 340 341 351 public String getEditorAreaCss() { 352 return editorAreaCss; 353 } 354 355 361 public void setEditorAreaCss(String editorAreaCss) { 362 this.editorAreaCss = editorAreaCss; 363 } 364 365 374 public String getWidth() { 375 return width; 376 } 377 378 383 public void setWidth(String value) { 384 width = value; 385 } 386 387 396 public String getHeight() { 397 return height; 398 } 399 400 405 public void setHeight(String value) { 406 height = value; 407 } 408 409 412 public void doTag() throws JspException , IOException { 413 414 PageContext pageContext = (PageContext ) getJspContext(); 415 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 416 417 if ( !EDITOR_TYPE_FCKEDITOR.equalsIgnoreCase(type) && !EDITOR_TYPE_TINYMCE.equalsIgnoreCase(type) ) { 418 type = GlobalProperties.getInstance(pageContext.getServletContext()).getString(WebappConstants.GLOBAL_PROPERTY_WYSIWYG_EDITOR_TYPE, EDITOR_TYPE_TINYMCE); 420 if ( !EDITOR_TYPE_FCKEDITOR.equalsIgnoreCase(type) && !EDITOR_TYPE_TINYMCE.equalsIgnoreCase(type) ) { 421 type = EDITOR_TYPE_TINYMCE; 423 } 424 } 425 426 if ( EDITOR_TYPE_FCKEDITOR.equalsIgnoreCase(type) ) { 428 Integer tagNumber = (Integer ) pageContext.getAttribute(FCKEDITOR_COUNT_KEY, PageContext.REQUEST_SCOPE); 429 if ( tagNumber == null ) { 430 tagNumber = new Integer (0); 431 } else { 432 tagNumber = new Integer (tagNumber.intValue() + 1); 433 } 434 pageContext.setAttribute(FCKEDITOR_COUNT_KEY, tagNumber, PageContext.REQUEST_SCOPE); 435 } else { 436 Integer tagNumber = (Integer ) pageContext.getAttribute(TINY_MCE_COUNT_KEY, PageContext.REQUEST_SCOPE); 437 if ( tagNumber == null ) { 438 tagNumber = new Integer (0); 439 } else { 440 tagNumber = new Integer (tagNumber.intValue() + 1); 441 } 442 pageContext.setAttribute(TINY_MCE_COUNT_KEY, tagNumber, PageContext.REQUEST_SCOPE); 443 this.tinyMCECount = tagNumber.intValue(); 444 } 445 446 if ( basePath == null ) { 448 basePath = ((HttpServletRequest ) pageContext.getRequest()).getContextPath() + "/FCKeditor/"; 449 } 450 451 if ( GenericValidator.isBlankOrNull(defaultLanguage) ) { 453 Locale locale = RequestUtils.getUserLocale(request, null); 454 if ( locale == null ) { 455 defaultLanguage = LocaleUtil.getInstance(pageContext.getServletContext()).getDefaultLocale().getIdentifier(); 456 } else { 457 defaultLanguage = locale.getLanguage(); 458 } 459 } 460 461 if ( editorAreaCss == null ) { 463 editorAreaCss = request.getContextPath() + "/styles/core/core.css"; 464 } else if ( EDITOR_AREA_CSS_NO_STYLESHEET.equalsIgnoreCase(editorAreaCss) ) { 465 editorAreaCss = ""; 466 } 467 468 if ( GenericValidator.isBlankOrNull(width) ) { 470 width = "100%"; 471 } 472 473 if ( GenericValidator.isBlankOrNull(height) ) { 475 height = "400px;"; 476 } 477 478 StringWriter sw = new StringWriter (); 479 getJspBody().invoke(sw); 480 String htmlCode = null; 481 if ( EDITOR_TYPE_FCKEDITOR.equalsIgnoreCase(type) ) { 482 htmlCode = createFCKEditorHTML(sw.toString()); 483 } else { 484 htmlCode = createTinyMCEHTML(sw.toString()); 485 } 486 TagUtils.getInstance().write(pageContext, htmlCode); 487 } 488 489 494 protected boolean isCompatible() { 495 496 PageContext pageContext = (PageContext ) getJspContext(); 497 String userAgent = ((HttpServletRequest ) pageContext.getRequest()).getHeader("user-agent"); 498 if ( userAgent == null ) { 499 return false; 500 } else { 501 userAgent = userAgent.toLowerCase(); 502 } 503 504 boolean compatible = false; 505 506 if ( EDITOR_TYPE_FCKEDITOR.equalsIgnoreCase(type) ) { 508 if ( (userAgent.indexOf("msie") != -1) && (userAgent.indexOf("mac") == -1) && (userAgent.indexOf("opera") == -1) ) { 509 if ( retrieveBrowserVersion(userAgent) >= 5.5 ) { 510 compatible = true; 511 } 512 } else if ( userAgent.indexOf("gecko") != -1 ) { 513 if ( retrieveBrowserVersion(userAgent) >= 20030210 ) { 514 compatible = true; 515 } 516 } 517 } else { 518 if ( userAgent.indexOf("opera") != -1 ) { 519 compatible = false; 520 } else if ( userAgent.indexOf("msie") != -1 ) { 521 if ( retrieveBrowserVersion(userAgent) >= 5.5 ) { 522 compatible = true; 523 } 524 } else if ( userAgent.indexOf("gecko") != -1 ) { 525 compatible = true; 527 } else if ( userAgent.indexOf("mac") != -1 ) { 528 compatible = true; 529 } else { 530 compatible = false; 531 } 532 } 533 534 return compatible; 535 } 536 537 543 protected double retrieveBrowserVersion(String userAgent) { 544 if ( userAgent.indexOf("msie") > -1 ) { 545 String str = userAgent.substring(userAgent.indexOf("msie") + 5); 546 return Double.parseDouble(str.substring(0, str.indexOf(";"))); 547 } else { 548 String str = userAgent.substring(userAgent.indexOf("gecko") + 6); 549 return Double.parseDouble(str.substring(0, 8)); 550 } 551 } 552 553 554 563 protected String createFCKEditorHTML(String content) { 564 StringBuffer strEditor = new StringBuffer (); 565 566 strEditor.append("<div>"); 567 content = StringUtil.htmlEncode(content); 568 569 if ( isCompatible() ) { 570 571 if ( content != null && content.trim().length() > 0 ) { 572 strEditor.append("<input type=\"hidden\" id=\"" + id + "\" name=\"" + id + "\" value=\"" + content + "\">"); 573 } else { 574 strEditor.append("<input type=\"hidden\" id=\"" + id + "\" name=\"" + id + "\">"); 575 } 576 577 strEditor.append(createFCKConfigHTML()); 578 strEditor.append(createFCKIFrameHTML()); 579 580 } else { 581 strEditor.append("<textarea name=\"" + id + "\" rows=\"4\" cols=\"40\" style=\"width: " + width + "; height: " + height + "\" wrap=\"virtual\">" + content + "</textarea>"); 582 } 583 strEditor.append("</div>"); 584 return strEditor.toString(); 585 } 586 587 592 protected String createFCKConfigHTML() { 593 594 Map config = new HashMap (); 596 config.put("DefaultLanguage", defaultLanguage); 597 config.put("BaseHref", baseHref); 598 config.put("EditorAreaLanguage", editorAreaLanguage); 599 config.put("FullPage", fullPage); 600 config.put("EditorAreaCss", editorAreaCss); 601 602 String configStr = RequestUtil.createQueryStringFromMap(config, "&").toString(); 604 605 return "<input type=\"hidden\" id=\"" + id + "___Config\" value=\"" + configStr + "\">"; 606 } 607 608 613 protected String createFCKIFrameHTML() { 614 615 String src = basePath + "editor/fckeditor.html?InstanceName=" + id; 616 src += "&Toolbar=Default"; 617 618 StringBuffer iframe = new StringBuffer ("<iframe"); 619 iframe.append(" SRC=\"").append(src).append("\""); 620 iframe.append(" id=\"").append(id).append("\""); 621 iframe.append(" name=\"").append(id).append("\""); 622 StringBuffer style = new StringBuffer (); 623 if ( !GenericValidator.isBlankOrNull(width) ) { 624 style.append(" width: ").append(width).append(";"); 625 } 626 if ( !GenericValidator.isBlankOrNull(height) ) { 627 style.append(" height: ").append(height).append(";"); 628 } 629 if ( style.length() > 0 ) { 630 iframe.append(" style=\"").append(style).append("\""); 631 } 632 iframe.append(" frameborder=\"no\""); 633 iframe.append(" scrolling=\"no\""); 634 iframe.append(">"); 635 iframe.append("</iframe>"); 636 return iframe.toString(); 637 638 } 639 640 646 protected String createTinyMCEHTML(String content) { 647 StringBuffer htmlCode = new StringBuffer (); 648 if ( tinyMCECount == 0 ) { 649 htmlCode.append(createTinyMCEInitCode()).append("\n"); 650 } 651 652 StringBuffer languageScript = new StringBuffer ("<script language=\"javascript\">"); 654 languageScript.append("this.editorAreaLanguages[this.editorAreaLanguages.length] = "); 655 if ( !GenericValidator.isBlankOrNull(editorAreaLanguage) ) { 656 languageScript.append("\"").append(editorAreaLanguage).append("\""); 657 } else { 658 languageScript.append("null"); 659 } 660 languageScript.append(";"); 661 languageScript.append("</script>"); 662 663 StringBuffer editor = new StringBuffer ("<textarea"); 665 editor.append(" id=\"").append(id).append("\""); 666 editor.append(" name=\"").append(id).append("\""); 667 668 StringBuffer style = new StringBuffer (); 669 if ( !GenericValidator.isBlankOrNull(width) ) { 670 style.append(" width: ").append(width).append(";"); 671 } 672 if ( !GenericValidator.isBlankOrNull(height) ) { 673 style.append(" height: ").append(height).append(";"); 674 } 675 if ( style.length() > 0 ) { 676 editor.append(" style=\"").append(style).append("\""); 677 } 678 editor.append(" class=\"contentEditorInstance\""); 679 editor.append(">"); 680 681 if ( content != null ) { 683 editor.append(content).append("\n"); 684 } 685 686 editor.append("</textarea>"); 688 689 htmlCode.append(languageScript); 690 htmlCode.append(editor); 691 692 return htmlCode.toString(); 693 } 694 695 700 protected StringBuffer createTinyMCEInitCode() { 701 StringBuffer initCode = new StringBuffer (); 702 String language = "ru".equals(defaultLanguage) ? "ru_UTF-8" : defaultLanguage; 705 706 initCode.append("<script language=\"javascript\" type=\"text/javascript\">"); 707 initCode.append("tinyMCE.init({"); 708 initCode.append("theme : \"advanced\""); 709 initCode.append(", mode : \"specific_textareas\""); 710 initCode.append(", editor_selector : \"contentEditorInstance\""); 711 initCode.append(", dialog_type : \"window\""); 712 initCode.append(", remove_linebreaks : false"); 713 initCode.append(", language : \"").append(language).append("\""); 715 initCode.append(", docs_language : \"").append(language).append("\""); 716 initCode.append(", content_css : \"").append(editorAreaCss.trim()).append("\""); 717 initCode.append(", plugins : \"table,save,advhr,advimage,advlink,emotions,iespell,preview,flash,searchreplace,print,contextmenu,paste,directionality,fullscreen\""); 718 initCode.append(", theme_advanced_buttons1_add_before : \"save,separator\""); 719 initCode.append(", theme_advanced_buttons1_add : \"fontselect,fontsizeselect\""); 720 initCode.append(", theme_advanced_buttons2_add : \"separator,preview,separator,forecolor,backcolor\""); 721 initCode.append(", theme_advanced_buttons2_add_before: \"cut,copy,paste,pastetext,pasteword,separator,search,replace,separator\""); 722 initCode.append(", theme_advanced_buttons3_add_before : \"tablecontrols,separator\""); 723 initCode.append(", theme_advanced_buttons3_add : \"emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen\""); 724 initCode.append(", theme_advanced_toolbar_location : \"top\""); 725 initCode.append(", theme_advanced_toolbar_align : \"left\""); 726 initCode.append(", theme_advanced_path_location : \"bottom\""); 727 initCode.append(", extended_valid_elements : \"a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]\""); 728 initCode.append(", file_browser_callback : \"tinyMCEBrowserCallBack\""); 729 initCode.append(", init_instance_callback : \"tinyMCEInitInstanceCallBack\""); 730 if ( !GenericValidator.isBlankOrNull(baseHref) ) { 731 initCode.append(", relative_urls : true"); 732 initCode.append(", document_base_url : \"").append(baseHref).append("\""); 733 } 734 initCode.append(", apply_source_formatting : true"); 735 initCode.append("});"); 736 737 if ( !GenericValidator.isBlankOrNull(editorAreaLanguage) ) { 738 initCode.append("this.editorAreaLanguages = new Array();"); 739 } 740 741 initCode.append("</script>"); 742 743 return initCode; 744 } 745 } 746 | Popular Tags |