01: /*
02: * Created on Jun 15, 2005
03: */
04: package uk.org.ponder.servletutil;
05:
06: import java.lang.reflect.Field;
07: import java.util.ArrayList;
08:
09: import javax.servlet.http.HttpServletRequest;
10:
11: import uk.org.ponder.conversion.StaticLeafParser;
12: import uk.org.ponder.messageutil.TargettedMessage;
13: import uk.org.ponder.util.Logger;
14:
15: /** An obsolete class used to deserialise an HttpServletRequest onto a
16: * flat Object, predating the DataAlterationRequest architecture.
17: * @author Antranig Basman (antranig@caret.cam.ac.uk)
18: */
19: // NB used in NST!
20: public class SimpleFormDecoder {
21: public static TargettedMessage[] decodeForm(Object target,
22: HttpServletRequest hsr) {
23: Class targclass = target.getClass();
24: Field[] fields = targclass.getFields();
25: StaticLeafParser parser = StaticLeafParser.instance();
26: ArrayList messages = new ArrayList();
27: // for (Iterator pit = hsr.getParameterMap().keySet().iterator(); pit.hasNext();) {
28: // String param = (String) pit.next();
29: // String value = hsr.getParameter(param);
30: // Logger.log.info("Param name " + param + " value " + value);
31: // }
32: for (int i = 0; i < fields.length; ++i) {
33: Class fieldtype = fields[i].getType();
34: String fieldname = fields[i].getName();
35: String param = hsr.getParameter(fieldname);
36: Class parsedtype = StaticLeafParser.wrapClass(fieldtype);
37: // Logger.log.info("Field name " + fieldname + " param " + param + " type " + parsedtype);
38: if (param != null) {
39: try {
40: Object parsed = parser.parse(parsedtype, param);
41: fields[i].set(target, parsed);
42: } catch (Exception e) {
43: messages.add(new TargettedMessage(e.getMessage(),
44: e, fieldname));
45: Logger.log.warn("Error parsing field " + fieldname
46: + " for " + fieldtype, e);
47: }
48: }
49: }
50: return (TargettedMessage[]) messages
51: .toArray(new TargettedMessage[messages.size()]);
52: }
53: }
|