001package org.apache.turbine.util.template; 002 003 004import org.apache.logging.log4j.LogManager; 005 006/* 007 * Licensed to the Apache Software Foundation (ASF) under one 008 * or more contributor license agreements. See the NOTICE file 009 * distributed with this work for additional information 010 * regarding copyright ownership. The ASF licenses this file 011 * to you under the Apache License, Version 2.0 (the 012 * "License"); you may not use this file except in compliance 013 * with the License. You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, 018 * software distributed under the License is distributed on an 019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 020 * KIND, either express or implied. See the License for the 021 * specific language governing permissions and limitations 022 * under the License. 023 */ 024 025 026import org.apache.logging.log4j.Logger; 027import org.apache.turbine.modules.NavigationLoader; 028import org.apache.turbine.pipeline.PipelineData; 029import org.apache.turbine.services.TurbineServices; 030import org.apache.turbine.services.template.TemplateService; 031 032/** 033 * Returns output of a Navigation module. An instance of this is 034 * placed in the WebMacro context by the WebMacroSiteLayout. This 035 * allows template authors to set the navigation template they'd like 036 * to place in their templates. Here's how it's used in a 037 * template: 038 * 039 * <p><code> 040 * $navigation.setTemplate("admin_navigation.wm") 041 * </code> 042 * 043 * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a> 044 * @version $Id$ 045 */ 046public class TemplateNavigation 047{ 048 /** Logging */ 049 private static final Logger log = LogManager.getLogger(TemplateNavigation.class); 050 051 /* The PipelineData object. */ 052 private final PipelineData pipelineData; 053 054 /* The name of the navigation template. */ 055 private String template = null; 056 057 /** 058 * Constructor 059 * 060 * @param pipelineData A Turbine PipelineData object. 061 */ 062 public TemplateNavigation(PipelineData pipelineData) 063 { 064 this.pipelineData = pipelineData; 065 } 066 067 /** 068 * Set the template. 069 * 070 * @param template A String with the name of the navigation 071 * template. 072 * @return A TemplateNavigation (self). 073 */ 074 public TemplateNavigation setTemplate(String template) 075 { 076 log.debug("setTemplate({})", template); 077 this.template = template; 078 return this; 079 } 080 081 /** 082 * Builds the output of the navigation template. 083 * 084 * @return A String. 085 */ 086 @Override 087 public String toString() 088 { 089 String module = null; 090 String returnValue = null; 091 092 try 093 { 094 if (template == null) 095 { 096 returnValue = "Navigation Template is null (Might be unset)"; 097 throw new Exception(returnValue); 098 } 099 100 pipelineData.getRunData().getTemplateInfo().setNavigationTemplate(template); 101 TemplateService templateService = (TemplateService)TurbineServices.getInstance() 102 .getService(TemplateService.SERVICE_NAME); 103 module = templateService.getNavigationName(template); 104 105 if (module == null) 106 { 107 returnValue = "Template Service returned null for Navigation Template " + template; 108 throw new Exception(returnValue); 109 } 110 111 returnValue = NavigationLoader.getInstance().eval(pipelineData, module); 112 } 113 catch (Exception e) 114 { 115 if (returnValue == null) 116 { 117 returnValue = "Error processing navigation template: " 118 + template + ", using module: " + module; 119 } 120 log.error(returnValue, e); 121 } 122 123 return returnValue; 124 } 125}