01: /*
02: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * @author www.gerdziegler.de
13: */
14: package org.ztemplates.classpath;
15:
16: import java.io.InputStream;
17: import java.util.Iterator;
18: import java.util.Set;
19:
20: import javax.servlet.ServletContext;
21:
22: public class ZClassPathItemWebapp implements ZClassPathItem {
23: private ServletContext ctx;
24:
25: private String name;
26:
27: public ZClassPathItemWebapp(ServletContext ctx, String name) {
28: this .ctx = ctx;
29: this .name = name;
30: }
31:
32: public String toString() {
33: return name;
34: }
35:
36: /**
37: * getInputStream
38: *
39: * @return InputStream
40: */
41: public InputStream getInputStream() {
42: return ctx.getResourceAsStream(name);
43: }
44:
45: /**
46: * getName
47: *
48: * @return String
49: */
50: public String getName() {
51: return name;
52: }
53:
54: /**
55: * getRelativePath
56: *
57: * @param root
58: * ClassPathItem
59: * @return String
60: */
61: public String getRelativePath(ZClassPathItem root) throws Exception {
62: return name.substring(root.getName().length());
63: }
64:
65: /**
66: * isDirectory
67: *
68: * @return boolean
69: */
70: public boolean isDirectory() {
71: return name.endsWith("/");
72: }
73:
74: /**
75: * isFile
76: *
77: * @return boolean
78: */
79: public boolean isFile() {
80: return !name.endsWith("/");
81: }
82:
83: /**
84: * list
85: *
86: * @return ClassPathItem[]
87: */
88: public ZClassPathItem[] list() {
89: Set<String> set = ctx.getResourcePaths(name);
90: ZClassPathItem[] ret = new ZClassPathItem[set.size()];
91: Iterator iter = set.iterator();
92: for (int i = 0; iter.hasNext(); i++) {
93: String crt = (String) iter.next();
94: ret[i] = new ZClassPathItemWebapp(ctx, crt);
95: }
96: return ret;
97: }
98: }
|