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