View Javadoc

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