1 package org.apache.turbine.util.template;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
53 private RunData data;
54
55
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 }