Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TemplateNavigation |
|
| 3.0;3 |
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: TemplateNavigation.java 1854797 2019-03-04 20:41:39Z tv $ | |
45 | */ | |
46 | public class TemplateNavigation | |
47 | { | |
48 | /** Logging */ | |
49 | 3 | 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 | 6 | private String template = null; |
56 | ||
57 | /** | |
58 | * Constructor | |
59 | * | |
60 | * @param pipelineData A Turbine PipelineData object. | |
61 | */ | |
62 | public TemplateNavigation(PipelineData pipelineData) | |
63 | 6 | { |
64 | 6 | this.pipelineData = pipelineData; |
65 | 6 | } |
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 | 0 | log.debug("setTemplate({})", template); |
77 | 0 | this.template = template; |
78 | 0 | 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 | 0 | String module = null; |
90 | 0 | String returnValue = null; |
91 | ||
92 | try | |
93 | { | |
94 | 0 | if (template == null) |
95 | { | |
96 | 0 | returnValue = "Navigation Template is null (Might be unset)"; |
97 | 0 | throw new Exception(returnValue); |
98 | } | |
99 | ||
100 | 0 | pipelineData.getRunData().getTemplateInfo().setNavigationTemplate(template); |
101 | 0 | TemplateService templateService = (TemplateService)TurbineServices.getInstance() |
102 | 0 | .getService(TemplateService.SERVICE_NAME); |
103 | 0 | module = templateService.getNavigationName(template); |
104 | ||
105 | 0 | if (module == null) |
106 | { | |
107 | 0 | returnValue = "Template Service returned null for Navigation Template " + template; |
108 | 0 | throw new Exception(returnValue); |
109 | } | |
110 | ||
111 | 0 | returnValue = NavigationLoader.getInstance().eval(pipelineData, module); |
112 | } | |
113 | 0 | catch (Exception e) |
114 | { | |
115 | 0 | if (returnValue == null) |
116 | { | |
117 | 0 | returnValue = "Error processing navigation template: " |
118 | + template + ", using module: " + module; | |
119 | } | |
120 | 0 | log.error(returnValue, e); |
121 | 0 | } |
122 | ||
123 | 0 | return returnValue; |
124 | } | |
125 | } |