001: /*
002: * FormClientTemplate.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, rinaldo, suhler.
022: *
023: * Version: 1.11
024: * Created by suhler on 98/10/27
025: * Last modified by suhler on 00/12/11 20:25:48
026: */
027:
028: package sunlabs.brazil.template;
029:
030: import java.util.Hashtable;
031: import sunlabs.brazil.server.Server;
032:
033: /**
034: * [<i>Deprecated, use the FormTemplate and ConfigFileHandler instead</i>]<br>
035: * SAMPLE Template class for substituting Default values into html forms
036: * The data is retrieved from the client, and sent back to
037: * the client later on. This will be used for e-business cards
038: * stored on java rings/cards. This template also incorporates the functionallity
039: * of the PropsTemplate, as the current scheme doesn't allow composition of
040: * template handler classes (at least for now).
041: *
042: * If a URL contains query data, and the value of the server
043: * property "uploadContains" occurs in the URL, then all of the query data
044: * is saved in the server on behalf of the client. IF no "uploadContains"
045: * string is set, all query data is saved on the server.
046: *
047: * The following Html entities processed by this class:
048: * <dl class=tags>
049: * <dt> input
050: * <dd> if the "name" attribute has saved data, the value attribute is
051: * replaced by the caved value. This allows default form values to be
052: * replaced by previous data submitted to the server by the client.
053: * <dt> property
054: * <dd> This tag is replaced by the value of a server property.
055: * <dt> subst
056: * <dd> This tag is replaced by the value previously uploaded to the the
057: * server from the client for this value.
058: * <dt> tag, /tag
059: * <dd> Inserts a "<" or ">" respectively, allowing parameters to
060: * be substituted inside of other entities. For example, suppose
061: * the client uploaded the value <b>HOME</b> as
062: * <code>http://my.home.com/</code>, and the server withes to create a
063: * link to that page. The template fragment:
064: * "<a href=<subst HOME>gt;
065: * won't work, as nested entities are not allowed. Instead, try:
066: * "<tag>a href=<subst HOME>lt;tag>
067: * </dl>
068: *
069: * @author Stephen Uhler
070: * @version @(#) FormClientTemplate.java 1.5 99/05/24 10:14:07
071: */
072:
073: public class FormClientTemplate extends Template {
074: Hashtable data; // data sent from client, to be stored on server
075: String uploadUrl; // url's to upload data must contain this string
076: static final String UPLOAD = "uploadContains";
077:
078: public FormClientTemplate() {
079: data = new Hashtable();
080: }
081:
082: /**
083: * Save a reference to our request properties.
084: * If the URL contains the upload string, save all of the query
085: * parameters on behalf of the user.
086: */
087:
088: public boolean init(RewriteContext hr) {
089: uploadUrl = hr.request.props
090: .getProperty(hr.prefix + UPLOAD, "");
091: log(hr, "Upload url: " + uploadUrl);
092:
093: /*
094: * If this is an "upload" url, save the data, and ignore the rest
095: * of the request. I'm not sure how to identify upload url's yet.
096: * For now, anything that has query data??
097: */
098:
099: if (hr.request.url.indexOf(uploadUrl) != -1) {
100: data = hr.request.getQueryData(data);
101: log(hr, "saving data for: " + hr.request.url);
102: }
103: log(hr, "data for: " + hr.request.url + ": " + data);
104: return true;
105: }
106:
107: /**
108: * Look for <input name=[x] value=[v]>, and replace the
109: * value with the entry in the previously saved client data.
110: * @param h The attribute/value pairs for this entity.
111: */
112:
113: public void tag_input(RewriteContext hr) {
114: String name = hr.get("name");
115: if (name == null) {
116: return;
117: }
118:
119: String value = (String) data.get(name);
120: if (value == null) {
121: return;
122: }
123:
124: hr.put("value", value);
125: }
126:
127: /**
128: * Convert the html tag "property" in to the request's property.
129: * @param h Hashtable containing tag parameters
130: * "name" The property name
131: * "default" a default value, if no name available
132: */
133:
134: public void tag_property(RewriteContext hr) {
135: String name = hr.getArgs();
136: String value = null;
137:
138: if (name.indexOf('=') >= 0) {
139: name = hr.get("name");
140: value = hr.get("default");
141: }
142: hr.append(hr.request.props.getProperty(name, value));
143: }
144:
145: /**
146: * Tag to do substitution of previously uploaded data.
147: * @param name The name of the token to replace with client data.
148: */
149:
150: public void tag_subst(RewriteContext hr) {
151: String name = hr.getArgs();
152: String value = null;
153: if (name.indexOf('=') >= 0) {
154: name = hr.get("name");
155: value = hr.get("default");
156: }
157: String result = (String) data.get(name);
158: if (result == null) {
159: result = value;
160: }
161: hr.append(result);
162: }
163:
164: /**
165: * Using the current scheme, there is no easy way to substitute into
166: * a tag parameter. So we'll invent a "magic" tag (called tag)
167: * that will allow us to create entities dynamically
168: */
169:
170: public void tag_tag(RewriteContext hr) {
171: hr.append("<");
172: }
173:
174: public void tag_slash_tag(RewriteContext hr) {
175: hr.append(">");
176: }
177:
178: /**
179: * simple interface to server logging
180: */
181:
182: private void log(RewriteContext hr, String msg) {
183: hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix
184: + "formClientTemplate: " + msg);
185: }
186: }
|