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