001: /*
002: * FormTemplate.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.14
024: * Created by suhler on 98/10/26
025: * Last modified by suhler on 00/12/11 13:29:42
026: */
027:
028: package sunlabs.brazil.template;
029:
030: import sunlabs.brazil.server.Server;
031: import java.io.Serializable;
032:
033: /**
034: * Template class for substituting Default values into html forms
035: * This class is used by the TemplateHandler.
036: *
037: * The default values in form tags are replaced by the server property that
038: * matches the field name. The following field elements are processed:
039: * <ul>
040: * <li> <input name=<i>x</i> value=<i>y</i>>
041: * <li> <input type=input name=<i>x</i> value=<i>y</i>>
042: * <li> <input type=radio name=<i>x</i> value=<i>y</i>>
043: * <li> <option value=<i>x</i>>
044: * </ul>
045: * In all cases, the <code>value</code> attribute must be present.
046: *
047: * @author Stephen Uhler
048: * @version @(#) FormTemplate.java 1.5 99/06/30 12:23:07
049: */
050: public class FormTemplate extends Template implements Serializable {
051: String selectName; // The name of the current select variable
052: String selectValue; // The name of the current select variable value
053: int total; // total for elements examined
054: int changed; // elements whose initial values have been changed
055:
056: /**
057: * Save a reference to our request properties.
058: */
059:
060: public boolean init(RewriteContext hr) {
061: // System.out.println("Form Template Started");
062: selectName = null;
063: total = 0;
064: changed = 0;
065:
066: return true;
067: }
068:
069: /**
070: * Look for <input name=[x] value=[v]> and replace the
071: * value with the entry in the request properties. If no value is supplied,
072: * no substitution is done.
073: */
074: public void tag_input(RewriteContext hr) {
075: total++;
076:
077: String name = hr.get("name");
078:
079: if (name == null) {
080: return;
081: }
082:
083: String value = hr.request.props.getProperty(name);
084:
085: if (value == null) {
086: return;
087: }
088:
089: changed++;
090: String type = hr.get("type");
091: if (type != null && type.equals("radio")) {
092: // System.out.println("Radio button: " + value + " ? " + hr.get("value"));
093: if (value.equals(hr.get("value"))) {
094: hr.put("checked", "");
095: } else {
096: hr.remove("checked");
097: }
098: } else {
099: hr.put("value", value);
100: }
101: }
102:
103: /**
104: * Remember the variable name for the next group of option tags.
105: */
106: public void tag_select(RewriteContext hr) {
107: selectName = hr.get("name");
108:
109: if (selectName != null) {
110: selectValue = hr.request.props.getProperty(selectName);
111:
112: log(hr, "For selection (" + selectName + ") have value :"
113: + selectValue);
114: }
115: }
116:
117: /**
118: * Forget the variable name for the next group of option tags
119: */
120: public void tag_slash_select(RewriteContext hr) {
121: selectName = null;
122: }
123:
124: /**
125: * Look at the option tag, set the "selected" attribute as needed.
126: * In order for this to work, the VALUE tag *must* be used
127: */
128: public void tag_option(RewriteContext hr) {
129: String value = hr.get("value");
130:
131: if (selectName == null || selectValue == null || value == null) {
132: return;
133: }
134: if (value.equals(selectValue)) {
135: hr.put("selected", "");
136: } else {
137: hr.remove("selected");
138: }
139: }
140:
141: /**
142: * This is for debugging only !!
143: */
144: public boolean done(RewriteContext hr) {
145: log(hr, hr.request.url + " (form template) " + changed + "/"
146: + total + " changed");
147:
148: return true;
149: }
150:
151: /**
152: * simple interface to server logging
153: */
154: private void log(RewriteContext hr, String msg) {
155: hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix
156: + "formTemplate: " + msg);
157: }
158: }
|