001package org.apache.turbine.services.jsp.util;
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.services.TurbineServices;
029import org.apache.turbine.services.template.TemplateService;
030import org.apache.turbine.util.RunData;
031
032/**
033 * Returns output of a Navigation module. An instance of this is placed in the
034 * request by the JspLayout. This allows template authors to
035 * set the navigation template they'd like to place in their templates.<br>
036 * Here's how it's used in a JSP template:<br>
037 * <code>
038 * &lt;%useBean id="navigation" class="JspNavigation" scope="request" %&gt;
039 * ...
040 * &lt;%= navigation.setTemplate("admin_navigation.jsp") %&gt;
041 * </code>
042 * @author <a href="john.mcnally@clearink.com">John D. McNally</a>
043 * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
044 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
045 * @version $Id$
046 */
047public class JspNavigation
048{
049    /** Logging */
050    private static Logger log = LogManager.getLogger(JspNavigation.class);
051
052    /* The RunData object */
053    private final RunData data;
054
055    /**
056     * Constructor
057     *
058     * @param data Turbine request data
059     */
060    public JspNavigation(RunData data)
061    {
062        this.data = data;
063    }
064
065    /**
066     * builds the output of the navigation template
067     * @param template the name of the navigation template
068     */
069    public void setTemplate(String template)
070    {
071        data.getTemplateInfo().setNavigationTemplate(template);
072        String module = null;
073        try
074        {
075            TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
076            module = templateService.getNavigationName(template);
077            NavigationLoader.getInstance().exec(data, module);
078        }
079        catch (Exception e)
080        {
081            String message = "Error processing navigation template:" +
082                    template + " using module: " + module;
083            log.error(message, e);
084            try
085            {
086                data.getResponse().getWriter().print(message);
087            }
088            catch (java.io.IOException ioe)
089            {
090                // ignore
091            }
092        }
093    }
094}