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