/*
* FormTemplate.java
*
* Brazil project web application Framework,
* export version: 1.1
* Copyright (c) 1998-2000 Sun Microsystems, Inc.
*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License Version
* 1.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is included as the file "license.terms",
* and also available at http://www.sun.com/
*
* The Original Code is from:
* Brazil project web application Framework release 1.1.
* The Initial Developer of the Original Code is: suhler.
* Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
* All Rights Reserved.
*
* Contributor(s): cstevens, rinaldo, suhler.
*
* Version: 1.14
* Created by suhler on 98/10/26
* Last modified by suhler on 00/12/11 13:29:42
*/
package sunlabs.brazil.template;
import sunlabs.brazil.server.Server;
import java.io.Serializable;
/**
* Template class for substituting Default values into html forms
* This class is used by the TemplateHandler.
*
* The default values in form tags are replaced by the server property that
* matches the field name. The following field elements are processed:
* <ul>
* <li> <input name=<i>x</i> value=<i>y</i>>
* <li> <input type=input name=<i>x</i> value=<i>y</i>>
* <li> <input type=radio name=<i>x</i> value=<i>y</i>>
* <li> <option value=<i>x</i>>
* </ul>
* In all cases, the <code>value</code> attribute must be present.
*
* @author Stephen Uhler
* @version @(#) FormTemplate.java 1.5 99/06/30 12:23:07
*/
public class FormTemplate extends Template implements Serializable {
String selectName; // The name of the current select variable
String selectValue; // The name of the current select variable value
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) {
// System.out.println("Form Template Started");
selectName = null;
total = 0;
changed = 0;
return true;
}
/**
* 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.
*/
public void tag_input(RewriteContext hr) {
total++;
String name = hr.get("name");
if (name == null) {
return;
}
String value = hr.request.props.getProperty(name);
if (value == null) {
return;
}
changed++;
String type = hr.get("type");
if (type != null && type.equals("radio")) {
// System.out.println("Radio button: " + value + " ? " + hr.get("value"));
if (value.equals(hr.get("value"))) {
hr.put("checked", "");
} else {
hr.remove("checked");
}
} else {
hr.put("value", value);
}
}
/**
* 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(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
*/
public void tag_option(RewriteContext hr) {
String value = hr.get("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);
}
}
|