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.template.TurbineTemplate;
028import org.apache.turbine.util.RunData;
029
030/**
031 * Returns output of a Navigation module.  An instance of this is
032 * placed in the WebMacro context by the WebMacroSiteLayout.  This
033 * allows template authors to set the navigation template they'd like
034 * to place in their templates.  Here's how it's used in a
035 * template:
036 *
037 * <p><code>
038 * $navigation.setTemplate("admin_navigation.wm")
039 * </code>
040 *
041 * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
042 * @version $Id: TemplateNavigation.java 1709648 2015-10-20 17:08:10Z tv $
043 */
044public class TemplateNavigation
045{
046    /** Logging */
047    private static Log log = LogFactory.getLog(TemplateNavigation.class);
048
049    /* The RunData object. */
050    private RunData data;
051
052    /* The name of the navigation template. */
053    private String template = null;
054
055    /**
056     * Constructor
057     *
058     * @param data A Turbine RunData object.
059     */
060    public TemplateNavigation(RunData data)
061    {
062        this.data = data;
063    }
064
065    /**
066     * Set the template.
067     *
068     * @param template A String with the name of the navigation
069     * template.
070     * @return A TemplateNavigation (self).
071     */
072    public TemplateNavigation setTemplate(String template)
073    {
074        log.debug("setTemplate(" + template + ")");
075        this.template = template;
076        return this;
077    }
078
079    /**
080     * Builds the output of the navigation template.
081     *
082     * @return A String.
083     */
084    @Override
085    public String toString()
086    {
087        String module = null;
088        String returnValue = null;
089
090        try
091        {
092            if (template == null)
093            {
094                returnValue = "Navigation Template is null (Might be unset)";
095                throw new Exception(returnValue);
096            }
097
098            data.getTemplateInfo().setNavigationTemplate(template);
099            module = TurbineTemplate.getNavigationName(template);
100
101            if (module == null)
102            {
103                returnValue = "Template Service returned null for Navigation Template " + template;
104                throw new Exception(returnValue);
105            }
106
107            returnValue = NavigationLoader.getInstance().eval(data, module);
108        }
109        catch (Exception e)
110        {
111            if (returnValue == null)
112            {
113                returnValue = "Error processing navigation template: "
114                        + template + ", using module: " + module;
115            }
116            log.error(returnValue, e);
117        }
118
119        return returnValue;
120    }
121}