001: /*
002: * DirectoryTemplate.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 2000 Sun Microsystems, Inc.
007: *
008: * Sun Public License Notice
009: *
010: * The contents of this file are subject to the Sun Public License Version
011: * 1.0 (the "License"). You may not use this file except in compliance with
012: * the License. A copy of the License is included as the file "license.terms",
013: * and also available at http://www.sun.com/
014: *
015: * The Original Code is from:
016: * Brazil project web application Framework release 1.1.
017: * The Initial Developer of the Original Code is: suhler.
018: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019: * All Rights Reserved.
020: *
021: * Contributor(s): suhler.
022: *
023: * Version: 1.6
024: * Created by suhler on 00/07/05
025: * Last modified by suhler on 00/12/11 20:24:31
026: */
027:
028: package sunlabs.brazil.template;
029:
030: import java.io.File;
031: import java.util.Properties;
032: import sunlabs.brazil.server.FileHandler;
033: import sunlabs.brazil.server.Handler;
034: import sunlabs.brazil.server.Request;
035: import sunlabs.brazil.server.Server;
036:
037: /**
038: * Put current directory information (based on the URL) into the
039: * request properties.
040: * The <code><filelist></code> tag, if present in the document,
041: * triggers the generation of a directory and file listing, based on the
042: * current URL.
043: * <p>
044: * Template Properties:
045: * <dl class=props>
046: * <dt>prepend <dd>String to prepend to the properties "Directories" and
047: * "Files" That contain the directory and file lists respectively.
048: * <dt>delimiter<dd>Delimiter character to separate entries, defaults to " ".
049: * <dt>DirectoryName<dd>If set, use this as the directory name instead of
050: * deriving it from the URL.
051: * <dt>debug <dd>if set, a comment is emitted indicating where the
052: * file-list entitiy was encountered.
053: * <dt>[prepend]Directories<dd>List of sub-directories in current directory
054: * <dt>[prepend]Files <dd>List of files with valid suffixes in
055: * current directory.
056: * <dt>mime.xxx <dd>An indication that suffix "xxx" is valid.
057: * </dl>
058: * This class may also be used as a handler, in which case the
059: * property <code>prefix</code> is used to match the leading
060: * portion of a URL>
061: *
062: * @author Stephen Uhler
063: * @version %V% DirectoryTemplate.java
064: */
065:
066: public class DirectoryTemplate extends Template implements Handler {
067: private boolean done = false; // trigger the properties
068: private String propsPrefix;
069:
070: public boolean init(Server server, String prefix) {
071: propsPrefix = prefix;
072: return true;
073: }
074:
075: /**
076: * Compute the directory info, and add it to the request properties.
077: */
078:
079: public boolean respond(Request request) {
080: String prefix = request.props.getProperty(propsPrefix
081: + "prefix", "/");
082:
083: if (request.url.startsWith(prefix)) {
084: getFiles(request, propsPrefix);
085: }
086: return false;
087: }
088:
089: /**
090: * Reset at each page
091: */
092:
093: public boolean init(RewriteContext hr) {
094: done = false;
095: return true;
096: }
097:
098: /**
099: * Turn on the directory calculator. The presense of this tag
100: * causes the files and subdirectories in the current directory
101: * to be added to the request properties.
102: */
103:
104: public void tag_filelist(RewriteContext hr) {
105: hr.killToken();
106: if (hr.request.props.getProperty(hr.prefix + "debug") != null) {
107: hr.append("<!-- " + hr.getBody() + " -->");
108: }
109: if (!done) {
110: getFiles(hr.request, hr.prefix);
111: done = true;
112: }
113: }
114:
115: /**
116: * Generate properties containing the files and directories in
117: * the "current" directory. The current directory is taken from
118: * the "DirectoryName" request property, or derived from the URL.
119: * This functionality was culled from the FileHandler and
120: * the Directory Handler.
121: */
122:
123: public void getFiles(Request request, String prefix) {
124: // System.out.println("Generating directory entries");
125: Properties props = request.props;
126: String prepend = props.getProperty(prefix + "prepend", "");
127: String delim = props.getProperty(prefix + "delimiter", " ");
128: String dir = props.getProperty("DirectoryName");
129: File file;
130: if (dir == null) {
131: String name = FileHandler.urlToPath(request.url);
132: String root = props.getProperty(prefix + FileHandler.ROOT,
133: props.getProperty(FileHandler.ROOT, "."));
134: file = new File(root + name);
135: if (!file.isDirectory()) {
136: file = new File(file.getParent());
137: }
138: // System.out.println("Generating dir name: " + file);
139: } else {
140: file = new File(dir);
141: // System.out.println("Using dir name: " + file);
142: }
143:
144: String[] list = file.list();
145: StringBuffer dirs = new StringBuffer();
146: StringBuffer files = new StringBuffer();
147: if (list != null) {
148: String dsep = "";
149: String fsep = "";
150: for (int i = 0; i < list.length; i++) {
151: String name = list[i];
152: // System.out.println("working on: " + name);
153: int index = name.lastIndexOf(".");
154: if ((new File(file, name)).isDirectory()) {
155: dirs.append(dsep).append(name);
156: dsep = delim;
157: } else if (index > 0
158: && props.getProperty(FileHandler.MIME
159: + name.substring(index)) != null) {
160: files.append(fsep).append(name);
161: fsep = delim;
162: }
163: }
164: props.put(prepend + "Directories", dirs.toString());
165: props.put(prepend + "Files", files.toString());
166: // System.out.println("Dirs=" + dirs + "\nFiles=" + files);
167: request.log(Server.LOG_DIAGNOSTIC, prefix,
168: "Generating file and directory properties: "
169: + list.length);
170: }
171: }
172: }
|