View Javadoc

1   package org.apache.turbine.util.template;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.turbine.modules.NavigationLoader;
27  import org.apache.turbine.services.template.TurbineTemplate;
28  import org.apache.turbine.util.RunData;
29  
30  /**
31   * Returns output of a Navigation module.  An instance of this is
32   * placed in the WebMacro context by the WebMacroSiteLayout.  This
33   * allows template authors to set the navigation template they'd like
34   * to place in their templates.  Here's how it's used in a
35   * template:
36   *
37   * <p><code>
38   * $navigation.setTemplate("admin_navigation.wm")
39   * </code>
40   *
41   * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
42   * @version $Id: TemplateNavigation.java 1709648 2015-10-20 17:08:10Z tv $
43   */
44  public class TemplateNavigation
45  {
46      /** Logging */
47      private static Log log = LogFactory.getLog(TemplateNavigation.class);
48  
49      /* The RunData object. */
50      private RunData data;
51  
52      /* The name of the navigation template. */
53      private String template = null;
54  
55      /**
56       * Constructor
57       *
58       * @param data A Turbine RunData object.
59       */
60      public TemplateNavigation(RunData data)
61      {
62          this.data = data;
63      }
64  
65      /**
66       * Set the template.
67       *
68       * @param template A String with the name of the navigation
69       * template.
70       * @return A TemplateNavigation (self).
71       */
72      public TemplateNavigation setTemplate(String template)
73      {
74          log.debug("setTemplate(" + template + ")");
75          this.template = template;
76          return this;
77      }
78  
79      /**
80       * Builds the output of the navigation template.
81       *
82       * @return A String.
83       */
84      @Override
85      public String toString()
86      {
87          String module = null;
88          String returnValue = null;
89  
90          try
91          {
92              if (template == null)
93              {
94                  returnValue = "Navigation Template is null (Might be unset)";
95                  throw new Exception(returnValue);
96              }
97  
98              data.getTemplateInfo().setNavigationTemplate(template);
99              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 }