1 package org.apache.turbine.services.template; 2 3 18 19 import java.io.File ; 20 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.apache.commons.configuration.Configuration; 26 27 import org.apache.commons.lang.StringUtils; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import org.apache.turbine.Turbine; 33 import org.apache.turbine.TurbineConstants; 34 import org.apache.turbine.modules.LayoutLoader; 35 import org.apache.turbine.modules.Loader; 36 import org.apache.turbine.modules.NavigationLoader; 37 import org.apache.turbine.modules.PageLoader; 38 import org.apache.turbine.modules.ScreenLoader; 39 import org.apache.turbine.services.InitializationException; 40 import org.apache.turbine.services.TurbineBaseService; 41 import org.apache.turbine.services.factory.TurbineFactory; 42 import org.apache.turbine.services.servlet.TurbineServlet; 43 import org.apache.turbine.services.template.mapper.BaseTemplateMapper; 44 import org.apache.turbine.services.template.mapper.ClassMapper; 45 import org.apache.turbine.services.template.mapper.DirectMapper; 46 import org.apache.turbine.services.template.mapper.DirectTemplateMapper; 47 import org.apache.turbine.services.template.mapper.LayoutTemplateMapper; 48 import org.apache.turbine.services.template.mapper.Mapper; 49 import org.apache.turbine.services.template.mapper.ScreenTemplateMapper; 50 import org.apache.turbine.util.RunData; 51 import org.apache.turbine.util.TurbineException; 52 import org.apache.turbine.util.uri.URIConstants; 53 54 174 public class TurbineTemplateService 175 extends TurbineBaseService 176 implements TemplateService 177 { 178 179 private static Log log = LogFactory.getLog(TurbineTemplateService.class); 180 181 182 public static final int PAGE_KEY = 0; 183 184 185 public static final String PAGE_NAME = "page"; 186 187 188 public static final int SCREEN_KEY = 1; 189 190 191 public static final String SCREEN_NAME = "screen"; 192 193 194 public static final int LAYOUT_KEY = 2; 195 196 197 public static final String LAYOUT_NAME = "layout"; 198 199 200 public static final int NAVIGATION_KEY = 3; 201 202 203 public static final String NAVIGATION_NAME = "navigation"; 204 205 206 public static final int LAYOUT_TEMPLATE_KEY = 4; 207 208 209 public static final String LAYOUT_TEMPLATE_NAME = "layout.template"; 210 211 212 public static final int SCREEN_TEMPLATE_KEY = 5; 213 214 215 public static final String SCREEN_TEMPLATE_NAME = "screen.template"; 216 217 218 public static final int NAVIGATION_TEMPLATE_KEY = 6; 219 220 221 public static final String NAVIGATION_TEMPLATE_NAME = "navigation.template"; 222 223 224 public static final int TEMPLATE_TYPES = 7; 225 226 227 private Mapper [] mapperRegistry = null; 228 229 235 protected static final String NO_FILE_EXT = TemplateService.DEFAULT_EXTENSION_VALUE; 236 237 238 239 private boolean useCache = false; 240 241 242 private String defaultExtension; 243 244 245 private String defaultTemplate; 246 247 255 private Map templateEngineRegistry = null; 256 257 260 public TurbineTemplateService() 261 { 262 } 263 264 270 public void init() 271 throws InitializationException 272 { 273 Configuration config = getConfiguration(); 275 276 defaultExtension = config.getString(TemplateService.DEFAULT_EXTENSION_KEY, 278 TemplateService.DEFAULT_EXTENSION_VALUE); 279 280 defaultTemplate = config.getString(TemplateService.DEFAULT_TEMPLATE_KEY, 281 TemplateService.DEFAULT_TEMPLATE_VALUE); 282 283 useCache = Turbine.getConfiguration().getBoolean(TurbineConstants.MODULE_CACHE_KEY, 286 TurbineConstants.MODULE_CACHE_DEFAULT); 287 288 log.debug("Default Extension: " + defaultExtension); 289 log.debug("Default Template: " + defaultTemplate); 290 log.debug("Use Caching: " + useCache); 291 292 templateEngineRegistry = Collections.synchronizedMap(new HashMap ()); 293 294 initMapper(config); 295 setInit(true); 296 } 297 298 303 public boolean isCaching() 304 { 305 return useCache; 306 } 307 308 315 public String getDefaultExtension() 316 { 317 return StringUtils.isNotEmpty(defaultExtension) ? defaultExtension : ""; 318 } 319 320 327 public String getExtension(String template) 328 { 329 if (StringUtils.isEmpty(template)) 330 { 331 return getDefaultExtension(); 332 } 333 334 int dotIndex = template.indexOf(EXTENSION_SEPARATOR); 335 336 return (dotIndex < 0) ? getDefaultExtension() : template.substring(dotIndex + 1); 337 } 338 339 340 346 public String getDefaultTemplate() 347 { 348 StringBuffer sb = new StringBuffer (); 349 sb.append(defaultTemplate); 350 if (StringUtils.isNotEmpty(defaultExtension)) 351 { 352 sb.append(EXTENSION_SEPARATOR); 353 sb.append(getDefaultExtension()); 354 } 355 return sb.toString(); 356 } 357 358 364 public String getDefaultPage() 365 { 366 return getDefaultPageName(getDefaultTemplate()); 367 } 368 369 375 public String getDefaultScreen() 376 { 377 return getDefaultScreenName(getDefaultTemplate()); 378 } 379 380 386 public String getDefaultLayout() 387 { 388 return getDefaultLayoutName(getDefaultTemplate()); 389 } 390 391 397 public String getDefaultNavigation() 398 { 399 return getDefaultNavigationName(getDefaultTemplate()); 400 } 401 402 408 public String getDefaultLayoutTemplate() 409 { 410 return getDefaultLayoutTemplateName(getDefaultTemplate()); 411 } 412 413 421 public String getDefaultPageName(String template) 422 { 423 return ((Mapper) mapperRegistry[PAGE_KEY]).getDefaultName(template); 424 } 425 426 434 public String getDefaultScreenName(String template) 435 { 436 return ((Mapper) mapperRegistry[SCREEN_KEY]).getDefaultName(template); 437 } 438 439 447 public String getDefaultLayoutName(String template) 448 { 449 return ((Mapper) mapperRegistry[LAYOUT_KEY]).getDefaultName(template); 450 } 451 452 460 public String getDefaultNavigationName(String template) 461 { 462 return ((Mapper) mapperRegistry[NAVIGATION_KEY]).getDefaultName(template); 463 } 464 465 473 public String getDefaultLayoutTemplateName(String template) 474 { 475 return ((Mapper) mapperRegistry[LAYOUT_TEMPLATE_KEY]).getDefaultName(template); 476 } 477 478 485 public String getDefaultPageName(RunData data) 486 { 487 String template = data.getParameters().get(URIConstants.CGI_TEMPLATE_PARAM); 488 return (template != null) ? 489 getDefaultPageName(template) : getDefaultPage(); 490 } 491 492 499 public String getDefaultLayoutName(RunData data) 500 { 501 String template = data.getParameters().get(URIConstants.CGI_TEMPLATE_PARAM); 502 return (template != null) ? 503 getDefaultLayoutName(template) : getDefaultLayout(); 504 } 505 506 514 public String getScreenName(String template) 515 throws Exception 516 { 517 return ((Mapper) mapperRegistry[SCREEN_KEY]).getMappedName(template); 518 } 519 520 528 public String getLayoutName(String template) 529 throws Exception 530 { 531 return ((Mapper) mapperRegistry[LAYOUT_KEY]).getMappedName(template); 532 } 533 534 542 public String getNavigationName(String template) 543 throws Exception 544 { 545 return ((Mapper) mapperRegistry[NAVIGATION_KEY]).getMappedName(template); 546 } 547 548 557 public String getScreenTemplateName(String template) 558 throws Exception 559 { 560 return ((Mapper) mapperRegistry[SCREEN_TEMPLATE_KEY]).getMappedName(template); 561 } 562 563 571 public String getLayoutTemplateName(String template) 572 throws Exception 573 { 574 return ((Mapper) mapperRegistry[LAYOUT_TEMPLATE_KEY]).getMappedName(template); 575 } 576 577 586 public String getNavigationTemplateName(String template) 587 throws Exception 588 { 589 return ((Mapper) mapperRegistry[NAVIGATION_TEMPLATE_KEY]).getMappedName(template); 590 } 591 592 603 public String [] translateTemplatePaths(String [] templatePaths) 604 { 605 for (int i = 0; i < templatePaths.length; i++) 606 { 607 templatePaths[i] = TurbineServlet.getRealPath(templatePaths[i]); 608 } 609 return templatePaths; 610 } 611 612 621 public boolean templateExists(String template, 622 String [] templatePaths) 623 { 624 for (int i = 0; i < templatePaths.length; i++) 625 { 626 if (new File (templatePaths[i], template).exists()) 627 { 628 return true; 629 } 630 } 631 return false; 632 } 633 634 640 public synchronized void registerTemplateEngineService(TemplateEngineService service) 641 { 642 String [] exts = service.getAssociatedFileExtensions(); 643 644 for (int i = 0; i < exts.length; i++) 645 { 646 templateEngineRegistry.put(exts[i], service); 647 } 648 } 649 650 657 public TemplateEngineService getTemplateEngineService(String template) 658 { 659 return (TemplateEngineService) templateEngineRegistry.get(getExtension(template)); 660 } 661 662 670 private void registerMapper(int templateKey, Mapper mapper) 671 { 672 mapper.init(); 673 mapperRegistry[templateKey] = mapper; 674 } 675 676 683 private void initMapper(Configuration conf) 684 throws InitializationException 685 { 686 mapperRegistry = new Mapper [TEMPLATE_TYPES]; 691 692 String [] mapperNames = new String [] { 693 PAGE_NAME,SCREEN_NAME, LAYOUT_NAME, 694 NAVIGATION_NAME, LAYOUT_TEMPLATE_NAME, SCREEN_TEMPLATE_NAME, NAVIGATION_TEMPLATE_NAME 695 }; 696 697 String [] mapperClasses = new String [] { 698 DirectMapper.class.getName(), 699 ClassMapper.class.getName(), 700 ClassMapper.class.getName(), 701 ClassMapper.class.getName(), 702 LayoutTemplateMapper.class.getName(), 703 ScreenTemplateMapper.class.getName(), 704 DirectTemplateMapper.class.getName() 705 }; 706 707 int [] mapperCacheSize = new int [] { 708 0, 709 conf.getInt( 710 TurbineConstants.SCREEN_CACHE_SIZE_KEY, 711 TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT), 712 conf.getInt( 713 TurbineConstants.LAYOUT_CACHE_SIZE_KEY, 714 TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT), 715 conf.getInt( 716 TurbineConstants.NAVIGATION_CACHE_SIZE_KEY, 717 TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT), 718 conf.getInt( 719 TurbineConstants.LAYOUT_CACHE_SIZE_KEY, 720 TurbineConstants.LAYOUT_CACHE_SIZE_DEFAULT), 721 conf.getInt( 722 TurbineConstants.SCREEN_CACHE_SIZE_KEY, 723 TurbineConstants.SCREEN_CACHE_SIZE_DEFAULT), 724 conf.getInt( 725 TurbineConstants.NAVIGATION_CACHE_SIZE_KEY, 726 TurbineConstants.NAVIGATION_CACHE_SIZE_DEFAULT) 727 }; 728 729 String [] mapperDefaultProperty = new String [] { 730 TemplateEngineService.DEFAULT_PAGE, 731 TemplateEngineService.DEFAULT_SCREEN, 732 TemplateEngineService.DEFAULT_LAYOUT, 733 TemplateEngineService.DEFAULT_NAVIGATION, 734 TemplateEngineService.DEFAULT_LAYOUT_TEMPLATE, 735 TemplateEngineService.DEFAULT_SCREEN_TEMPLATE, 736 TemplateEngineService.DEFAULT_NAVIGATION_TEMPLATE 737 }; 738 739 char [] mapperSeparator = new char [] { '.', '.', '.', '.', '/', '/', '/' }; 740 741 Loader [] mapperLoader = new Loader [] { 742 PageLoader.getInstance(), 743 ScreenLoader.getInstance(), 744 LayoutLoader.getInstance(), 745 NavigationLoader.getInstance(), 746 null, null, null}; 747 748 String [] mapperPrefix = new String [] { 749 null, null, null, null, 750 TurbineConstants.LAYOUT_PREFIX, 751 TurbineConstants.SCREEN_PREFIX, 752 TurbineConstants.NAVIGATION_PREFIX }; 753 754 for (int i = 0; i < TEMPLATE_TYPES; i++) 755 { 756 StringBuffer mapperProperty = new StringBuffer (); 757 mapperProperty.append("mapper."); 758 mapperProperty.append(mapperNames[i]); 759 mapperProperty.append(".class"); 760 761 String mapperClass = 762 conf.getString(mapperProperty.toString(), mapperClasses[i]); 763 764 log.info("Using " + mapperClass + " to map " + mapperNames[i] + " elements"); 765 766 Mapper tm = null; 767 768 try 769 { 770 tm = (Mapper) TurbineFactory.getInstance(mapperClass); 771 } 772 catch (TurbineException te) 773 { 774 throw new InitializationException("", te); 775 } 776 777 tm.setUseCache(useCache); 778 tm.setCacheSize(mapperCacheSize[i]); 779 tm.setDefaultProperty(mapperDefaultProperty[i]); 780 tm.setSeparator(mapperSeparator[i]); 781 782 if ((mapperLoader[i] != null) && (tm instanceof ClassMapper)) 783 { 784 ((ClassMapper) tm).setLoader(mapperLoader[i]); 785 } 786 787 if ((mapperPrefix[i] != null) && (tm instanceof BaseTemplateMapper)) 788 { 789 ((BaseTemplateMapper) tm).setPrefix(mapperPrefix[i]); 790 } 791 792 registerMapper(i, tm); 793 } 794 } 795 } 796 | Popular Tags |