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