01: /*
02: * Created on 27 Jul 2006
03: */
04: package uk.org.ponder.springutil.errors;
05:
06: import org.springframework.validation.Errors;
07: import org.springframework.validation.FieldError;
08: import org.springframework.validation.ObjectError;
09:
10: import uk.org.ponder.messageutil.TargettedMessage;
11: import uk.org.ponder.messageutil.TargettedMessageList;
12:
13: /**
14: * Provides conversion between PUC "TargettedMessage"s and Spring "Error"
15: * objects. This conversion is slightly lossy, since PUC doesn't support the
16: * idea of "objectName" or "rejectedValue", and Spring doesn't support message
17: * severities.
18: *
19: * @author Antranig Basman (antranig@caret.cam.ac.uk)
20: */
21:
22: public class SpringErrorConverter {
23:
24: public static TargettedMessage SpringErrortoTargettedMessage(
25: Object erroro) {
26: if (erroro instanceof FieldError) {
27: FieldError error = (FieldError) erroro;
28: TargettedMessage togo = new TargettedMessage(error
29: .getCodes(), error.getArguments(), error.getField());
30: return togo;
31: } else if (erroro instanceof ObjectError) {
32: ObjectError error = (ObjectError) erroro;
33: TargettedMessage togo = new TargettedMessage(error
34: .getCodes(), error.getArguments(),
35: TargettedMessage.TARGET_NONE);
36: return togo;
37: } else
38: throw new IllegalArgumentException(
39: "Cannot convert Spring Error of unknown "
40: + erroro.getClass());
41: }
42:
43: public static Object targettedMessageToSpringError(
44: TargettedMessage message) {
45: if (message.targetid.equals(TargettedMessage.TARGET_NONE)) {
46: ObjectError togo = new ObjectError("",
47: message.messagecodes, message.args, null);
48: return togo;
49: } else {
50: FieldError togo = new FieldError("", message.targetid, "",
51: false, message.messagecodes, message.args, null);
52: return togo;
53: }
54: }
55:
56: public static void appendErrors(String frontpath,
57: TargettedMessageList tml, Errors errors) {
58: tml.pushNestedPath(frontpath);
59: try {
60: for (int i = 0; i < errors.getErrorCount(); ++i) {
61: tml.addMessage(SpringErrorConverter
62: .SpringErrortoTargettedMessage(errors
63: .getAllErrors().get(i)));
64: }
65: } finally {
66: tml.popNestedPath();
67: }
68:
69: }
70: }
|