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:
24: /**
25: * <p>Event handler that looks for included files relative to the path of the
26: * current template. The handler assumes that paths are separated by a forward
27: * slash "/" or backwards slash "\".
28: *
29: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain </a>
30: * @version $Id: IncludeRelativePath.java 470256 2006-11-02 07:20:36Z wglass $
31: */
32:
33: public class IncludeRelativePath implements IncludeEventHandler {
34:
35: /**
36: * Return path relative to the current template's path.
37: *
38: * @param includeResourcePath the path as given in the include directive.
39: * @param currentResourcePath the path of the currently rendering template that includes the
40: * include directive.
41: * @param directiveName name of the directive used to include the resource. (With the
42: * standard directives this is either "parse" or "include").
43:
44: * @return new path relative to the current template's path
45: */
46: public String includeEvent(String includeResourcePath,
47: String currentResourcePath, String directiveName) {
48: // if the resource name starts with a slash, it's not a relative path
49: if (includeResourcePath.startsWith("/")
50: || includeResourcePath.startsWith("\\")) {
51: return includeResourcePath;
52: }
53:
54: int lastslashpos = Math.max(currentResourcePath
55: .lastIndexOf("/"), currentResourcePath
56: .lastIndexOf("\\"));
57:
58: // root of resource tree
59: if (lastslashpos == -1) {
60: return includeResourcePath;
61: }
62:
63: // prepend path to the include path
64: return currentResourcePath.substring(0, lastslashpos) + "/"
65: + includeResourcePath;
66: }
67: }
|