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  import org.apache.turbine.modules.NavigationLoader;
    27  import org.apache.turbine.services.TurbineServices;
    28  import org.apache.turbine.services.template.TemplateService;
    29  import org.apache.turbine.util.RunData;
    30
    31  /**
    32   * Returns output of a Navigation module.  An instance of this is
    33   * placed in the WebMacro context by the WebMacroSiteLayout.  This
    34   * allows template authors to set the navigation template they'd like
    35   * to place in their templates.  Here's how it's used in a
    36   * template:
    37   *
    38   * <p><code>
    39   * $navigation.setTemplate("admin_navigation.wm")
    40   * </code>
    41   *
    42   * @author <a href="mbryson@mont.mindspring.com">Dave Bryson</a>
    43   * @version $Id: TemplateNavigation.java 1723699 2016-01-08 11:29:21Z tv $
    44   */
    45  public class TemplateNavigation
    46  {
    47      /** Logging */
    48      private static Log log = LogFactory.getLog(TemplateNavigation.class);
    49
    50      /* The RunData object. */
    51      private RunData data;
    52
    53      /* The name of the navigation template. */
    54      private String template = null;
    55
    56      /**
    57       * Constructor
    58       *
    59       * @param data A Turbine RunData object.
    60       */
    61      public TemplateNavigation(RunData data)
    62      {
    63          this.data = data;
    64      }
    65
    66      /**
    67       * Set the template.
    68       *
    69       * @param template A String with the name of the navigation
    70       * template.
    71       * @return A TemplateNavigation (self).
    72       */
    73      public TemplateNavigation setTemplate(String template)
    74      {
    75          log.debug("setTemplate(" + template + ")");
    76          this.template = template;
    77          return this;
    78      }
    79
    80      /**
    81       * Builds the output of the navigation template.
    82       *
    83       * @return A String.
    84       */
    85      @Override
    86      public String toString()
    87      {
    88          String module = null;
    89          String returnValue = null;
    90
    91          try
    92          {
    93              if (template == null)
    94              {
    95                  returnValue = "Navigation Template is null (Might be unset)";
    96                  throw new Exception(returnValue);
    97              }
    98
    99              data.getTemplateInfo().setNavigationTemplate(template);
    100             TemplateService templateService = (TemplateService)TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
    101             module = templateService.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             returnValue = NavigationLoader.getInstance().eval(data, module);
    110         }
    111         catch (Exception e)
    112         {
    113             if (returnValue == null)
    114             {
    115                 returnValue = "Error processing navigation template: "
    116                         + template + ", using module: " + module;
    117             }
    118             log.error(returnValue, e);
    119         }
    120
    121         return returnValue;
    122     }
    123 }