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  
27  import org.apache.ecs.ConcreteElement;
28  
29  import org.apache.turbine.modules.NavigationLoader;
30  
31  import org.apache.turbine.services.template.TurbineTemplate;
32  
33  import org.apache.turbine.util.RunData;
34  
35  /**
36   * Returns output of a Navigation module.  An instance of this is
37   * placed in the WebMacro context by the WebMacroSiteLayout.  This
38   * allows template authors to set the navigation template they'd like
39   * to place in their templates.  Here's how it's used in a
40   * template:
41   *
42   * <p><code>
43   * $navigation.setTemplate("admin_navigation.wm")
44   * </code>
45   *
46   * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
47   * @version $Id: TemplateNavigation.java 615328 2008-01-25 20:25:05Z tv $
48   */
49  public class TemplateNavigation
50  {
51      /** Logging */
52      private static Log log = LogFactory.getLog(TemplateNavigation.class);
53  
54      /* The RunData object. */
55      private RunData data;
56  
57      /* The name of the navigation template. */
58      private String template = null;
59  
60      /**
61       * Constructor
62       *
63       * @param data A Turbine RunData object.
64       */
65      public TemplateNavigation(RunData data)
66      {
67          this.data = data;
68      }
69  
70      /**
71       * Set the template.
72       *
73       * @param template A String with the name of the navigation
74       * template.
75       * @return A TemplateNavigation (self).
76       */
77      public TemplateNavigation setTemplate(String template)
78      {
79          log.debug("setTemplate(" + template + ")");
80          this.template = template;
81          return this;
82      }
83  
84      /**
85       * Builds the output of the navigation template.
86       *
87       * @return A String.
88       */
89      public String toString()
90      {
91          String module = null;
92          String returnValue = null;
93  
94          try
95          {
96              if (template == null)
97              {
98                  returnValue = "Navigation Template is null (Might be unset)";
99                  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 }