1 16 package org.apache.cocoon.woody.formmodel; 17 18 import org.apache.cocoon.woody.Constants; 19 import org.apache.cocoon.woody.FormContext; 20 import org.apache.cocoon.woody.formmodel.AggregateFieldDefinition.SplitMapping; 21 import org.apache.cocoon.woody.util.I18nMessage; 22 import org.apache.cocoon.woody.validation.ValidationError; 23 import org.apache.cocoon.xml.AttributesImpl; 24 import org.apache.excalibur.xml.sax.XMLizable; 25 import org.apache.oro.text.regex.MatchResult; 26 import org.apache.oro.text.regex.PatternMatcher; 27 import org.apache.oro.text.regex.Perl5Matcher; 28 29 import org.outerj.expression.ExpressionException; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.SAXException ; 32 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Locale ; 38 import java.util.Map ; 39 40 59 public class AggregateField extends Field { 60 61 64 private List fields = new ArrayList (); 65 66 69 private Map fieldsById = new HashMap (); 70 71 72 public AggregateField(AggregateFieldDefinition definition) { 73 super(definition); 74 } 75 76 public final AggregateFieldDefinition getAggregateFieldDefinition() { 77 return (AggregateFieldDefinition)super.definition; 78 } 79 80 protected void addField(Field field) { 81 field.setParent(this); 82 fields.add(field); 83 fieldsById.put(field.getId(), field); 84 } 85 86 public Iterator getChildren() { 87 return fields.iterator(); 88 } 89 90 public void readFromRequest(FormContext formContext) { 91 String newEnteredValue = formContext.getRequest().getParameter(getFullyQualifiedId()); 92 if (newEnteredValue != null) { 93 super.readFromRequest(formContext); 95 if (needsParse) { 96 setFieldsValues(enteredValue); 97 } 98 } else { 99 boolean needsParse = false; 101 for (Iterator i = fields.iterator(); i.hasNext();) { 102 Field field = (Field)i.next(); 103 field.readFromRequest(formContext); 104 needsParse |= field.needsParse; 105 } 106 if (needsParse) { 107 combineFields(); 108 } 109 } 110 } 111 112 public void setValue(Object newValue) { 113 super.setValue(newValue); 114 if (needsValidate) { 115 setFieldsValues(enteredValue); 116 } 117 } 118 119 122 private boolean fieldsHaveValues() { 123 for (Iterator i = fields.iterator(); i.hasNext();) { 124 Field field = (Field)i.next(); 125 if (field.getValue() != null) { 126 return true; 127 } 128 } 129 return false; 130 } 131 132 136 private void setFieldsValues(String value) { 137 if (value == null) { 138 resetFieldsValues(); 139 } else { 140 PatternMatcher matcher = new Perl5Matcher(); 141 if (matcher.matches(value, getAggregateFieldDefinition().getSplitPattern())) { 142 MatchResult matchResult = matcher.getMatch(); 143 Iterator iterator = getAggregateFieldDefinition().getSplitMappingsIterator(); 144 while (iterator.hasNext()) { 145 SplitMapping splitMapping = (SplitMapping)iterator.next(); 146 String result = matchResult.group(splitMapping.getGroup()); 147 148 Field field = (Field)fieldsById.get(splitMapping.getFieldId()); 150 field.readFromRequest(result); 151 } 152 } else { 153 resetFieldsValues(); 154 } 155 } 156 } 157 158 public void combineFields() { 159 try { 160 Object value = getAggregateFieldDefinition().getCombineExpression().evaluate(new ExpressionContextImpl(this, true)); 161 super.setValue(value); 162 } catch (CannotYetResolveWarning e) { 163 super.setValue(null); 164 } catch (ExpressionException e) { 165 super.setValue(null); 166 } catch (ClassCastException e) { 167 super.setValue(null); 168 } 169 } 170 171 174 private void resetFieldsValues() { 175 for (Iterator i = fields.iterator(); i.hasNext();) { 176 Field field = (Field)i.next(); 177 field.setValue(null); 178 } 179 } 180 181 public boolean validate(FormContext formContext) { 182 if ((enteredValue != null) != fieldsHaveValues()) { 183 XMLizable failMessage = getAggregateFieldDefinition().getSplitFailMessage(); 184 if (failMessage != null) { 185 validationError = new ValidationError(failMessage); 186 } else { 187 validationError = new ValidationError(new I18nMessage("aggregatedfield.split-failed", 188 new String [] { getAggregateFieldDefinition().getSplitRegexp() }, 189 Constants.I18N_CATALOGUE)); 190 } 191 return false; 192 } 193 194 for (Iterator i = fields.iterator(); i.hasNext();) { 196 Field field = (Field)i.next(); 197 if (!field.validate(formContext)) { 198 validationError = field.getValidationError(); 199 return false; 200 } 201 } 202 203 return super.validate(formContext); 204 } 205 206 207 private static final String AGGREGATEFIELD_EL = "aggregatefield"; 208 private static final String VALUE_EL = "value"; 209 private static final String VALIDATION_MSG_EL = "validation-message"; 210 211 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 212 AttributesImpl aggregatedFieldAttrs = new AttributesImpl(); 213 aggregatedFieldAttrs.addCDATAAttribute("id", getFullyQualifiedId()); 214 aggregatedFieldAttrs.addCDATAAttribute("required", String.valueOf(getAggregateFieldDefinition().isRequired())); 215 contentHandler.startElement(Constants.WI_NS, AGGREGATEFIELD_EL, Constants.WI_PREFIX_COLON + AGGREGATEFIELD_EL, aggregatedFieldAttrs); 216 217 if (enteredValue != null || value != null) { 218 contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS); 219 String stringValue; 220 if (value != null) { 221 stringValue = getDatatype().convertToString(value, locale); 222 } else { 223 stringValue = enteredValue; 224 } 225 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 226 contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL); 227 } 228 229 if (validationError != null) { 231 contentHandler.startElement(Constants.WI_NS, VALIDATION_MSG_EL, Constants.WI_PREFIX_COLON + VALIDATION_MSG_EL, Constants.EMPTY_ATTRS); 232 validationError.generateSaxFragment(contentHandler); 233 contentHandler.endElement(Constants.WI_NS, VALIDATION_MSG_EL, Constants.WI_PREFIX_COLON + VALIDATION_MSG_EL); 234 } 235 236 definition.generateDisplayData(contentHandler); 238 239 if (selectionList != null) { 241 selectionList.generateSaxFragment(contentHandler, locale); 242 } else if (getFieldDefinition().getSelectionList() != null) { 243 getFieldDefinition().getSelectionList().generateSaxFragment(contentHandler, locale); 244 } 245 contentHandler.endElement(Constants.WI_NS, AGGREGATEFIELD_EL, Constants.WI_PREFIX_COLON + AGGREGATEFIELD_EL); 246 } 247 248 public void generateLabel(ContentHandler contentHandler) throws SAXException { 249 definition.generateLabel(contentHandler); 250 } 251 252 public Widget getWidget(String id) { 253 return (Widget)fieldsById.get(id); 254 } 255 } 256 | Popular Tags |