1 package pl.aislib.fm;
2
3 import java.util.Map;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7
8 import pl.aislib.fm.forms.BaseValidator;
9 import pl.aislib.fm.forms.Field;
10 import pl.aislib.fm.forms.config.Handler;
11 import pl.aislib.fm.forms.config.IField;
12 import pl.aislib.util.validators.AbstractValidator;
13
14 /***
15 * XML handler for handling framework's fields.
16 *
17 * @author <a href="mailto:wswiatek@ais.pl">Wojciech Swiatek</a>, AIS.PL
18 * @version $Revision: 1.2 $
19 */
20 public class FieldHandler extends pl.aislib.fm.forms.config.FieldHandler {
21
22 /***
23 * True if field is required.
24 */
25 protected boolean allRequired;
26
27
28
29
30 /***
31 * @see pl.aislib.fm.forms.config.PartialHandler#PartialHandler(Handler)
32 */
33 public FieldHandler(Handler parentHandler) {
34 super(parentHandler);
35 }
36
37
38
39
40 /***
41 * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
42 */
43 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
44 if ("field".equals(localName)) {
45 allRequired = true;
46 } else if (
47 "property".equals(localName)
48 && "required".equals(atts.getValue("name"))
49 && "false".equals(atts.getValue("value"))) {
50 allRequired = false;
51 }
52 super.startElement(namespaceURI, localName, qName, atts);
53 }
54
55
56
57
58 /***
59 * @see pl.aislib.fm.forms.config.FieldHandler#addField(IField)
60 */
61 protected void addField(IField field) {
62 Field formField = (Field) field;
63
64 if (currentBuilder != null) {
65 formField.addType(Field.FT_COMPLEX);
66 formField.setBuilder(currentBuilder);
67 formField.setBuilderMapping(currentMapping);
68
69 try {
70 populate(currentBuilder, currentProperties);
71 } catch (SAXException e) {
72 ;
73 }
74 }
75
76 if (allRequired) {
77 addDefaultValidator(field);
78 }
79
80 ((FormsHandler) parentHandler).currentForm.addField(formField);
81 }
82
83 /***
84 * @see pl.aislib.fm.forms.config.FieldHandler#createNewField(String)
85 */
86 protected IField createNewField(String fieldName) {
87 return new Field(fieldName);
88 }
89
90 /***
91 * @see pl.aislib.fm.forms.config.FieldHandler#processFieldBehavior(IField, Attributes)
92 */
93 protected void processFieldBehavior(IField field, Attributes atts) throws SAXException {
94 Field formField = (Field) field;
95
96 boolean bDynamic = atts.getValue("dynamic") != null && atts.getValue("dynamic").equals("true");
97
98 int fieldType = Field.FT_NORMAL;
99 if (bDynamic) {
100 fieldType |= Field.FT_DYNAMIC;
101 }
102
103 formField.setType(fieldType);
104 }
105
106 /***
107 * @see pl.aislib.fm.forms.config.FieldHandler#addValidator(BaseValidator, Map)
108 */
109 protected void addValidator(BaseValidator validator, Map atts) throws SAXException {
110 Field formField = (Field) currentField;
111
112 int msgCode = 0;
113 try {
114 msgCode = Integer.parseInt((String) atts.get("msg-code"));
115 } catch (NumberFormatException nfe) {
116 throw new SAXException(nfe.getMessage());
117 }
118
119 formField.addValidator(validator, msgCode);
120 }
121
122 /***
123 * Adds validator in case when no validators are strictly given in a form definition.
124 *
125 * @param field <code>IField</code> object.
126 */
127 protected void addDefaultValidator(IField field) {
128 try {
129 Field formField = (Field) field;
130 BaseValidator validator = (BaseValidator) currentValidator.getClass().newInstance();
131 populate(validator, currentProperties);
132 ((AbstractValidator) validator).setRequired(false);
133 formField.addValidator(validator, ((Integer) formField.getValidators().get(currentValidator)).intValue());
134 } catch (Exception e) {
135 ;
136 }
137 }
138
139 }