001: /*
002: * PropsTemplate.java
003: *
004: * Brazil project web application Framework,
005: * export version: 1.1
006: * Copyright (c) 1998-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): cstevens, suhler.
022: *
023: * Version: 1.29
024: * Created by suhler on 98/09/14
025: * Last modified by suhler on 00/12/11 13:29:09
026: */
027:
028: package sunlabs.brazil.template;
029:
030: import sunlabs.brazil.util.Format;
031:
032: import java.util.Enumeration;
033: import java.util.Hashtable;
034: import java.util.Dictionary;
035: import java.util.Properties;
036: import java.io.Serializable;
037:
038: /**
039: * Template class for substituting request properties into an HTML page
040: * This class is used by the TemplateHandler
041: * The following request properties are used:
042: * <dl class=props>
043: * <dt>query <dd> The query parameters are placed into the request object,
044: * prefixed by the value assigned to "query".
045: * <dt>headers <dd> The mime headers are placed into the request object,
046: * prefixed by the value assigned to "headers". The values:
047: * url, query, method, and version are copied from the request
048: * object into the properties. The clients IP address is
049: * saved in the "address" property.
050: * </dl>
051: * <p>
052: * A new HTML tag,
053: * <code><property></code> is defined. It takes the following
054: * tag attributes:
055: * <dl>
056: * <dt>name <dd> The name of the property in
057: * {@link sunlabs.brazil.server.Request#props props}
058: * to replace the <code>property</code> tag with.
059: * <dt>default <dd> The value to use if the property is not defined.
060: * If no <code>default</code> is specified, the empty
061: * string is used instead.
062: * </dl>
063: *
064: * @author Stephen Uhler
065: * @version 1.0, 09/04/98
066: */
067:
068: public class PropsTemplate extends Template implements Serializable {
069: private static final String DEBUG = "debug";
070:
071: transient boolean debug;
072:
073: /**
074: * This gets called at every page, at the beginning. See if we should add
075: * the mime headers and query parameters into the request object
076: */
077:
078: public boolean init(RewriteContext hr) {
079: Properties props = hr.request.props;
080:
081: debug = (props.getProperty(hr.prefix + DEBUG) != null);
082:
083: String query = props.getProperty(hr.prefix + "query");
084: if (query != null) {
085: Dictionary h = hr.request.getQueryData(null);
086: Enumeration keys = h.keys();
087: while (keys.hasMoreElements()) {
088: String key = (String) keys.nextElement();
089: props.put(query + key, h.get(key));
090: }
091: }
092:
093: String headers = props.getProperty(hr.prefix + "headers");
094: if (headers != null) {
095: Enumeration keys = hr.request.headers.keys();
096: while (keys.hasMoreElements()) {
097: String key = (String) keys.nextElement();
098: props.put(headers + key.toLowerCase(),
099: hr.request.headers.get(key));
100: }
101: props.put(headers + "method", hr.request.method);
102: props.put(headers + "url", hr.request.url);
103: props.put(headers + "query", hr.request.query);
104: props.put(headers + "protocol", hr.request.protocol);
105: props.put(headers + "address", ""
106: + hr.request.getSocket().getInetAddress()
107: .getHostAddress());
108: }
109: return true;
110: }
111:
112: /**
113: * Convert the html tag "property" in to the request's property
114: * @param key The name of the property to substitute. Variable
115: * substitution using the style described in
116: * {@link Format#getProperty} is permitted, e.g.:
117: * <code>employee.${id}.last</code>
118: */
119:
120: public void tag_property(RewriteContext hr) {
121: String name = hr.getArgs();
122: String value = null;
123: if (name.indexOf('=') >= 0) {
124: name = hr.get("name");
125: value = hr.get("default");
126: }
127:
128: String result = null;
129: if (name != null) {
130: result = Format.getProperty(hr.request.props, name, value);
131: }
132:
133: if (result != null) {
134: hr.append(result);
135: } else if (debug) {
136: hr.append("<!-- property: no value for " + name + " -->");
137: } else {
138: hr.killToken();
139: }
140: }
141:
142: /**
143: * Insert a liteteral "<".
144: * Using the current scheme, there is no easy way to substitute into
145: * a tag parameter. So we'll invent a "magic" tag (called tag)
146: * that will allow us to create entities dynamically. Thus values
147: * can be substituted into entities by escaping the entity as in:
148: * <pre>
149: * <tag>a href=<property href></tag>
150: * </pre>
151: */
152:
153: public void tag_tag(RewriteContext hr) {
154: hr.append("<");
155: }
156:
157: /**
158: * Insert a literal ">"
159: */
160:
161: public void tag_slash_tag(RewriteContext hr) {
162: hr.append(">");
163: }
164: }
|