tags.
* The "value" parameter must be specified in order for selection to occurr
*
* 1.4 99/05/24-10:13:36 (suhler)
* - better documentation
* - added ,
*
* 1.3 99/03/30-09:33:56 (suhler)
* documentation update
*
* 1.2 98/10/26-11:09:36 (suhler)
* Fixed diagnostic output
*
* 1.2 98/10/26-09:53:53 (Codemgr)
* SunPro Code Manager data about conflicts, renames, etc...
* Name history : 2 1 handlers/templates/FormTemplate.java
* Name history : 1 0 templates/FormTemplate.java
*
* 1.1 98/10/26-09:53:52 (suhler)
* date and time created 98/10/26 09:53:52 by suhler
*
*/
package sunlabs.brazil.template;
import sunlabs.brazil.server.Server;
import java.util.Enumeration;
/**
* Template class for substituting default values into html forms.
* This class is used by the TemplateHandler.
*
* The default values in form elements are replaced by the request property that
* matches the field name. The following field elements are processed:
*
* <input name=x value=y >
* <input type=input name=x value=y >
* <input type=radio name=x value=y >
* <option value=x >
*
* In all cases, the value
attribute must be present.
* additional information is provided below.
*
* If the enclosing <form> tag has the attribute "prepend", then
* "prepend" is tacked on the front of each variable name before its
* value is looked-up. The "prepend" attribute is then removed from the
* form tag.
*
* @author Stephen Uhler
* @version @(#)FormTemplate.java 2.4
*/
public class FormTemplate extends Template {
String selectName; // The name of the current select variable
String selectValue; // The name of the current select variable value
String prepend = ""; // What to prepend the values with
int total; // total for elements examined
int changed; // elements whose initial values have been changed
/**
* Save a reference to our request properties.
*/
public boolean init(RewriteContext hr) {
selectName = null;
total = 0;
changed = 0;
return true;
}
/**
* Look for a "prepend" attrubute, remember its value, then remove it
* from the tag.
*/
public void tag_form(RewriteContext hr) {
String p = hr.get("prepend");
if (p != null) {
hr.remove("prepend");
if (p.endsWith(".")) {
prepend = p;
} else {
prepend = p + ".";
}
} else {
prepend = "";
}
hr.substAttributeValues();
}
/**
* Forget about the "prepend" value
*/
public void tag_slash_form(RewriteContext hr) {
prepend = "";
}
/**
* Look for <input name=[x] value=[v]> and replace the
* value with the entry in the request properties. If no value is supplied,
* no substitution is done.
* If value
contains any ${..} constructs, the substituted
* value is used instead of the value in the corrosponding request property.
*/
public void tag_input(RewriteContext hr) {
total++;
String origValue = hr.get("value");
if (origValue != null) {
hr.put("value", origValue);
}
String name = hr.get("name");
if (name == null) {
return;
} else {
hr.put("name", name);
}
String value = hr.request.props.getProperty(prepend + name);
if (value == null) {
return;
}
changed++;
String type = hr.get("type", false);
if ("radio".equals(type) || "checkbox".equals(type)) {
log(hr,type + " " + value + " ? " + hr.get("value", false));
if (value.equals(origValue)) {
hr.put("checked", "");
} else {
hr.remove("checked");
}
} else {
hr.put("value", value);
String size = hr.get("size");
if (size != null) {
hr.put("size",size);
}
}
}
/**
* Remember the variable name for the next group of option tags.
*/
public void tag_select(RewriteContext hr) {
selectName = hr.get("name");
if (selectName != null) {
selectValue = hr.request.props.getProperty(prepend + selectName);
hr.put("name", selectName);
log(hr,
"For selection (" + selectName + ") have value :"
+ selectValue);
}
}
/**
* Forget the variable name for the next group of option tags
*/
public void tag_slash_select(RewriteContext hr) {
selectName = null;
}
/**
* Look at the option tag, set the "selected" attribute as needed.
* In order for this to work, the VALUE tag *must* be used
* Do ${...} substitutions on the value.
*/
public void tag_option(RewriteContext hr) {
String value = hr.get("value");
if (value != null) {
hr.put("value", value);
}
if (selectName == null || selectValue == null || value == null) {
return;
}
if (value.equals(selectValue)) {
hr.put("selected", "");
} else {
hr.remove("selected");
}
}
/**
* This is for debugging only !!
*/
public boolean done(RewriteContext hr) {
log(hr, hr.request.url + " (form template) " + changed + "/" + total + " changed");
return true;
}
/**
* simple interface to server logging
*/
private void log(RewriteContext hr, String msg) {
hr.request.log(Server.LOG_DIAGNOSTIC,
hr.prefix + "formTemplate: " + msg);
}
}