001: /*
002: * UrlNavBarTemplate.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/05/22
025: * Last modified by suhler on 00/12/11 13:30:40
026: */
027:
028: package sunlabs.brazil.template;
029:
030: import java.util.StringTokenizer;
031: import sunlabs.brazil.server.Server;
032:
033: /**
034: * Template class for dynamically generating a navigation bar
035: * by looking at portions of the url.
036: * Given url:
037: * /main/next/last/foo.html
038: * Generate the request properties for the directories:
039: * main, next, and last.
040: * The properties will be:
041: * <pre>
042: * NAV.main=/main/
043: * NAV.next=/main/next/ ....
044: * NAV.=main/next/....
045: * </pre>
046: * These properties may be incorporated into web pages using the
047: * BSLTemplate's <foreach> tag, using a delimeter of "/" to
048: * iterate over the listings.
049: * <p>
050: * The follow request properties are consulted:
051: * <dl class=props>
052: * <dt>prepend <dd>Use as a prefix on the property name, instead
053: * of "NAV.".
054: * <dt>includeDir<dd> Normally, if the URL refers to the directory (
055: * e.g. it ends with a /), no nav bar entry is generated.
056: * If this property is set, the entry is generated.
057: * </dl>
058: *
059: * @author Stephen Uhler
060: * @version %V% UrlNavBarTemplate.java
061: */
062:
063: public class UrlNavBarTemplate extends Template {
064: StringBuffer navbar; // the extracted NavBar
065: StringBuffer url; // the url to date
066:
067: /**
068: * Compute a set of properties based on the URL
069: */
070:
071: public boolean init(RewriteContext hr) {
072: url = new StringBuffer("/");
073: String prepend = hr.request.props.getProperty(hr.prefix
074: + "prepend", "NAV.");
075: String includeDir = (hr.request.props.getProperty(hr.prefix
076: + "includeDir") == null) ? "" : "x";
077: StringTokenizer st = new StringTokenizer(hr.request.url
078: + includeDir, "/");
079: int count = 0;
080: String path = null;
081: while (st.hasMoreTokens()) {
082: String dir = st.nextToken();
083: if (!st.hasMoreTokens()) {
084: break;
085: }
086: url.append(dir + "/");
087: if (url.equals(hr.request.url)) {
088: break;
089: }
090: count++;
091: path = url.toString();
092: hr.request.props.put(prepend + dir, path);
093: }
094: if (count > 0) {
095: hr.request.props.put(prepend, path.substring(1, path
096: .length() - 1));
097: }
098: hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix,
099: "Computing nav-bar entries: " + count);
100:
101: return true;
102: }
103: }
|