001: package org.apache.turbine.util.template;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import org.apache.ecs.ConcreteElement;
026:
027: import org.apache.turbine.modules.NavigationLoader;
028:
029: import org.apache.turbine.services.template.TurbineTemplate;
030:
031: import org.apache.turbine.util.RunData;
032:
033: /**
034: * Returns output of a Navigation module. An instance of this is
035: * placed in the WebMacro context by the WebMacroSiteLayout. This
036: * allows template authors to set the navigation template they'd like
037: * to place in their templates. Here's how it's used in a
038: * template:
039: *
040: * <p><code>
041: * $navigation.setTemplate("admin_navigation.wm")
042: * </code>
043: *
044: * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
045: * @version $Id: TemplateNavigation.java 534527 2007-05-02 16:10:59Z tv $
046: */
047: public class TemplateNavigation {
048: /** Logging */
049: private static Log log = LogFactory
050: .getLog(TemplateNavigation.class);
051:
052: /* The RunData object. */
053: private RunData data;
054:
055: /* The name of the navigation template. */
056: private String template = null;
057:
058: /**
059: * Constructor
060: *
061: * @param data A Turbine RunData object.
062: */
063: public TemplateNavigation(RunData data) {
064: this .data = data;
065: }
066:
067: /**
068: * Set the template.
069: *
070: * @param template A String with the name of the navigation
071: * template.
072: * @return A TemplateNavigation (self).
073: */
074: public TemplateNavigation setTemplate(String template) {
075: log.debug("setTemplate(" + template + ")");
076: this .template = template;
077: return this ;
078: }
079:
080: /**
081: * Builds the output of the navigation template.
082: *
083: * @return A String.
084: */
085: public String toString() {
086: String module = null;
087: String returnValue = null;
088:
089: try {
090: if (template == null) {
091: returnValue = "Navigation Template is null (Might be unset)";
092: throw new Exception(returnValue);
093: }
094:
095: data.getTemplateInfo().setNavigationTemplate(template);
096: module = TurbineTemplate.getNavigationName(template);
097:
098: if (module == null) {
099: returnValue = "Template Service returned null for Navigation Template "
100: + template;
101: throw new Exception(returnValue);
102: }
103:
104: ConcreteElement results = NavigationLoader.getInstance()
105: .eval(data, module);
106: returnValue = results.toString();
107: } catch (Exception e) {
108: if (returnValue == null) {
109: returnValue = "Error processing navigation template: "
110: + template + ", using module: " + module;
111: }
112: log.error(returnValue, e);
113: }
114:
115: return returnValue;
116: }
117: }
|