View Javadoc

1   package org.apache.velocity.app.event.implement;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.app.event.IncludeEventHandler;
23  import org.apache.velocity.runtime.RuntimeServices;
24  import org.apache.velocity.util.RuntimeServicesAware;
25  import org.apache.velocity.util.StringUtils;
26  
27  /**
28   * Simple event handler that checks to see if an included page is available.
29   * If not, it includes a designated replacement page instead.
30   *
31   * <P>By default, the name of the replacement page is "notfound.vm", however this
32   * page name can be changed by setting the Velocity property
33   * <code>eventhandler.include.notfound</code>, for example:
34   * <code>
35   * <PRE>
36   * eventhandler.include.notfound = error.vm
37   * </PRE>
38   * </code>
39   *
40   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
41   * @version $Id: IncludeNotFound.java 470256 2006-11-02 07:20:36Z wglass $
42   */
43  
44  public class IncludeNotFound implements IncludeEventHandler,RuntimeServicesAware {
45  
46      private static final String DEFAULT_NOT_FOUND = "notfound.vm";
47      private static final String PROPERTY_NOT_FOUND = "eventhandler.include.notfound";
48      private RuntimeServices rs = null;
49      String notfound;
50  
51      /**
52       * Chseck to see if included file exists, and display "not found" page if it
53       * doesn't. If "not found" page does not exist, log an error and return
54       * null.
55       * 
56       * @param includeResourcePath
57       * @param currentResourcePath
58       * @param directiveName
59       * @return message.
60       */
61      public String includeEvent(
62          String includeResourcePath,
63          String currentResourcePath,
64          String directiveName)
65      {
66  
67          /**
68           * check to see if page exists
69           */
70          boolean exists = (rs.getLoaderNameForResource(includeResourcePath) != null);
71          if (!exists)
72          {
73              if (rs.getLoaderNameForResource(notfound) == null)
74              {
75                  return notfound;
76  
77              }
78              else
79              {
80                  /**
81                   * can't find not found, so display nothing
82                   */
83                  rs.getLog().error("Can't find include not found page: " + notfound);
84                  return null;
85              }
86  
87          }
88          else
89              return includeResourcePath;
90      }
91  
92  
93      /**
94       * @see org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
95       */
96      public void setRuntimeServices(RuntimeServices rs)
97      {
98           this.rs = rs;
99           notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND));
100      }
101 
102 }