1 package org.apache.turbine.util.template; 2 3 18 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.ArrayList ; 23 24 import org.apache.commons.configuration.Configuration; 25 26 import org.apache.commons.lang.StringUtils; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.apache.turbine.Turbine; 32 import org.apache.turbine.TurbineConstants; 33 import org.apache.turbine.services.pull.ApplicationTool; 34 import org.apache.turbine.util.RunData; 35 36 98 public class HtmlPageAttributes 99 implements ApplicationTool 100 { 101 102 private static Log log = LogFactory.getLog(HtmlPageAttributes.class); 103 104 105 private String title; 106 107 108 private Map bodyAttributes = new HashMap (); 109 110 111 private List scripts = new ArrayList (); 112 113 114 private List styleSheets = new ArrayList (); 115 116 117 private List styles = new ArrayList (); 118 119 120 private Map metaTags = new HashMap (); 121 122 123 private Map httpEquivs = new HashMap (); 124 125 126 private String doctype = null; 127 128 131 public HtmlPageAttributes() 132 { 133 } 134 135 140 public HtmlPageAttributes(RunData data) 141 { 142 init(data); 143 } 144 145 151 public void init(Object data) 152 { 153 this.title = null; 154 this.bodyAttributes.clear(); 155 this.scripts.clear(); 156 this.styleSheets.clear(); 157 this.styles.clear(); 158 this.metaTags.clear(); 159 this.httpEquivs.clear(); 160 } 161 162 165 public void refresh() 166 { 167 } 169 170 179 public HtmlPageAttributes setTitle(String title) 180 { 181 this.title = title; 182 return this; 183 } 184 185 192 public String getTitle() 193 { 194 if (StringUtils.isEmpty(this.title)) 195 { 196 return ""; 197 } 198 return title; 199 } 200 201 209 public HtmlPageAttributes addAttribute(String name, String value) 210 { 211 log.info("Use of the addAttribute(name,value) method is deprecated. Please use " + 212 "addBodyAttribute(name,value) instead."); 213 return addBodyAttribute(name, value); 214 } 215 216 223 public HtmlPageAttributes addBodyAttribute(String name, String value) 224 { 225 this.bodyAttributes.put(name, value); 226 return this; 227 } 228 229 234 public Map getBodyAttributes() 235 { 236 return this.bodyAttributes; 237 } 238 239 245 public HtmlPageAttributes addScript(String scriptURL) 246 { 247 this.scripts.add(scriptURL); 248 return this; 249 } 250 251 258 public HtmlPageAttributes setScript(String scriptURL) 259 { 260 log.info("Use of the setScript(scriptURL) method is deprecated. Please use " + 261 "addScript(scriptURL) instead."); 262 return addScript(scriptURL); 263 } 264 265 271 public List getScripts() 272 { 273 return this.scripts; 274 } 275 276 282 public HtmlPageAttributes addStyleSheet(String styleSheetURL) 283 { 284 addStyleSheet(styleSheetURL, "screen", null, "text/css"); 285 return this; 286 } 287 288 297 public HtmlPageAttributes addStyleSheet(String styleSheetURL, 298 String media, String title, String type) 299 { 300 StyleSheet ss = new StyleSheet(styleSheetURL); 301 ss.setMedia(media); 302 ss.setTitle(title); 303 ss.setType(type); 304 this.styleSheets.add(ss); 305 return this; 306 } 307 308 315 public HtmlPageAttributes setStyleSheet(String styleSheetURL) 316 { 317 log.info("Use of the setStyleSheet(styleSheetURL) method is deprecated. Please use " + 318 "addStyleSheet(styleSheetURL) instead."); 319 return addStyleSheet(styleSheetURL); 320 } 321 322 330 public HtmlPageAttributes setStyleSheet(String styleSheetURL, String media) 331 { 332 log.info("Use of the setStyleSheet(styleSheetURL,media) method is deprecated. " + 333 "Please use addStyleSheet(styleSheetURL,media) instead."); 334 return addStyleSheet(styleSheetURL, media, null, "text/css"); 335 } 336 337 342 public List getStyleSheets() 343 { 344 return this.styleSheets; 345 } 346 347 354 public HtmlPageAttributes setStyle(String styleText) 355 { 356 log.info("Use of the setStyle(styleText) method is deprecated. Please use " + 357 "addStyle(styleText) instead."); 358 return addStyle(styleText); 359 } 360 361 367 public HtmlPageAttributes addStyle(String styleText) 368 { 369 this.styles.add(styleText); 370 return this; 371 } 372 373 378 public List getStyles() 379 { 380 return this.styles; 381 } 382 383 389 public HtmlPageAttributes setKeywords(String keywords) 390 { 391 this.metaTags.put("keywords", keywords); 392 return this; 393 } 394 395 404 public HtmlPageAttributes setHttpEquiv(String httpEquiv, String content) 405 { 406 this.httpEquivs.put(httpEquiv, content); 407 return this; 408 } 409 410 416 public HtmlPageAttributes setDescription(String description) 417 { 418 this.metaTags.put("description", description); 419 return this; 420 } 421 422 428 public HtmlPageAttributes setBackground(String url) 429 { 430 this.bodyAttributes.put("background", url); 431 return this; 432 } 433 434 442 public HtmlPageAttributes setBgColor(String color) 443 { 444 this.bodyAttributes.put("BGCOLOR", color); 445 return this; 446 } 447 448 455 public HtmlPageAttributes setTextColor(String color) 456 { 457 this.bodyAttributes.put("TEXT", color); 458 return this; 459 } 460 461 468 public HtmlPageAttributes setLinkColor(String color) 469 { 470 this.bodyAttributes.put("LINK", color); 471 return this; 472 } 473 474 480 public HtmlPageAttributes setVlinkColor(String color) 481 { 482 this.bodyAttributes.put("VLINK", color); 483 return this; 484 } 485 486 492 public HtmlPageAttributes setAlinkColor(String color) 493 { 494 this.bodyAttributes.put("ALINK", color); 495 return this; 496 } 497 498 503 public Map getHttpEquivs() 504 { 505 return this.httpEquivs; 506 } 507 508 513 public Map getMetaTags() 514 { 515 return this.metaTags; 516 } 517 518 523 public String toString() 524 { 525 return ""; 526 } 527 528 531 public class StyleSheet 532 { 533 private String url; 534 private String title; 535 private String media; 536 private String type; 537 538 543 public StyleSheet(String url) 544 { 545 setUrl(url); 546 } 547 548 553 public String getType() 554 { 555 return (StringUtils.isEmpty(type) ? "" : type); 556 } 557 558 563 public void setType(String type) 564 { 565 this.type = type; 566 } 567 568 571 public String getUrl() 572 { 573 return url; 574 } 575 576 581 private void setUrl(String url) 582 { 583 this.url = url; 584 } 585 586 591 public String getTitle() 592 { 593 return (StringUtils.isEmpty(title) ? "" : title); 594 } 595 596 601 public void setTitle(String title) 602 { 603 this.title = title; 604 } 605 606 611 public String getMedia() 612 { 613 return (StringUtils.isEmpty(media) ? "" : media); 614 } 615 616 621 public void setMedia(String media) 622 { 623 this.media = media; 624 } 625 626 } 627 628 639 public String getDefaultDoctype() 640 { 641 Configuration conf = Turbine.getConfiguration(); 642 if (doctype == null) 643 { 644 String tag = conf.getString( 645 TurbineConstants.DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_KEY, 646 TurbineConstants.DEFAULT_HTML_DOCTYPE_ROOT_ELEMENT_DEFAULT); 647 648 if (StringUtils.isEmpty(tag)) 649 { 650 doctype = ""; 651 } 652 else 653 { 654 String identifier = conf.getString( 655 TurbineConstants.DEFAULT_HTML_DOCTYPE_IDENTIFIER_KEY, 656 TurbineConstants.DEFAULT_HTML_DOCTYPE_IDENTIFIER_DEFAULT); 657 658 String uri = conf.getString( 659 TurbineConstants.DEFAULT_HTML_DOCTYPE_URI_KEY, 660 TurbineConstants.DEFAULT_HTML_DOCTYPE_URI_DEFAULT); 661 662 doctype = buildDoctype(tag, identifier, uri); 663 } 664 } 665 return doctype; 666 } 667 668 676 private String buildDoctype(String tag, String identifier, String uri) 677 { 678 StringBuffer doctypeBuf = new StringBuffer ("<!DOCTYPE "); 679 doctypeBuf.append(tag); 680 681 if (StringUtils.isNotEmpty(identifier)) 682 { 683 doctypeBuf.append(" PUBLIC \""); 684 doctypeBuf.append(identifier); 685 doctypeBuf.append("\" \""); 686 } 687 else 688 { 689 doctypeBuf.append(" SYSTEM \""); 690 } 691 692 doctypeBuf.append(uri); 693 doctypeBuf.append("\">"); 694 695 return doctypeBuf.toString(); 696 } 697 698 } 699 | Popular Tags |