01: package org.apache.velocity.app.event.implement;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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,
45: RuntimeServicesAware {
46:
47: private static final String DEFAULT_NOT_FOUND = "notfound.vm";
48: private static final String PROPERTY_NOT_FOUND = "eventhandler.include.notfound";
49: private RuntimeServices rs = null;
50: String notfound;
51:
52: /**
53: * Chseck to see if included file exists, and display "not found" page if it
54: * doesn't. If "not found" page does not exist, log an error and return
55: * null.
56: *
57: * @param includeResourcePath
58: * @param currentResourcePath
59: * @param directiveName
60: * @return message.
61: */
62: public String includeEvent(String includeResourcePath,
63: String currentResourcePath, String directiveName) {
64:
65: /**
66: * check to see if page exists
67: */
68: boolean exists = (rs
69: .getLoaderNameForResource(includeResourcePath) != null);
70: if (!exists) {
71: if (rs.getLoaderNameForResource(notfound) == null) {
72: return notfound;
73:
74: } else {
75: /**
76: * can't find not found, so display nothing
77: */
78: rs.getLog().error(
79: "Can't find include not found page: "
80: + notfound);
81: return null;
82: }
83:
84: } else
85: return includeResourcePath;
86: }
87:
88: /**
89: * @see org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
90: */
91: public void setRuntimeServices(RuntimeServices rs) {
92: this.rs = rs;
93: notfound = StringUtils.nullTrim(rs.getString(
94: PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND));
95: }
96:
97: }
|