1 16 package org.apache.cocoon.forms.formmodel; 17 18 import org.apache.excalibur.xml.sax.XMLizable; 19 import org.apache.oro.text.regex.Pattern; 20 21 import org.outerj.expression.Expression; 22 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 35 public class AggregateFieldDefinition extends FieldDefinition { 36 37 40 private Expression combineExpr; 41 42 46 private String splitRegexp; 47 48 51 private Pattern splitPattern; 52 53 57 protected XMLizable splitFailMessage; 58 59 63 private List splitMappings = new ArrayList (); 64 65 68 private Set mappedFields = new HashSet (); 69 70 73 private WidgetDefinitionList container = new WidgetDefinitionList(this); 74 75 78 public void initializeFrom(WidgetDefinition definition) throws Exception { 79 super.initializeFrom(definition); 80 81 if(definition instanceof AggregateFieldDefinition) { 82 AggregateFieldDefinition other = (AggregateFieldDefinition)definition; 83 84 this.combineExpr = other.combineExpr; 85 this.splitRegexp = other.splitRegexp; 86 this.splitPattern = other.splitPattern; 87 this.splitFailMessage = other.splitFailMessage; 88 89 Iterator defs = other.container.getWidgetDefinitions().iterator(); 90 while(defs.hasNext()) { 91 container.addWidgetDefinition((WidgetDefinition)defs.next()); 92 } 93 94 Collections.copy(this.splitMappings,other.splitMappings); 95 96 Iterator fields = other.mappedFields.iterator(); 97 while(fields.hasNext()) { 98 this.mappedFields.add(fields.next()); 99 } 100 101 } else { 102 throw new Exception ("Definition to inherit from is not of the right type! (at "+getLocation()+")"); 103 } 104 } 105 106 public void addWidgetDefinition(WidgetDefinition widgetDefinition) throws DuplicateIdException { 107 checkMutable(); 108 container.addWidgetDefinition(widgetDefinition); 109 } 110 111 114 public void checkCompleteness() throws IncompletenessException { 115 super.checkCompleteness(); 116 117 if(this.container.size()==0) 118 throw new IncompletenessException("AggregateField doesn't have any child widgets!",this); 119 120 if(this.combineExpr==null) 121 throw new IncompletenessException("AggregateField requires a combine expression!",this); 122 123 if(this.splitPattern==null) 124 throw new IncompletenessException("AggregateField requires a split regular expression!",this); 125 126 if(this.splitMappings.size()==0) 127 throw new IncompletenessException("AggregateField requires at least one group to field mapping!",this); 128 129 List defs = container.getWidgetDefinitions(); 131 Iterator it = defs.iterator(); 132 while(it.hasNext()) { 133 ((WidgetDefinition)it.next()).checkCompleteness(); 134 } 135 } 136 137 public boolean hasWidget(String id) { 138 return container.hasWidget(id); 139 } 140 141 protected void setCombineExpression(Expression expression) { 142 checkMutable(); 143 combineExpr = expression; 144 } 145 146 public Expression getCombineExpression() { 147 return combineExpr; 148 } 149 150 protected void setSplitPattern(Pattern pattern, String regexp) { 151 checkMutable(); 152 this.splitPattern = pattern; 153 this.splitRegexp = regexp; 154 } 155 156 public Pattern getSplitPattern() { 157 return splitPattern; 158 } 159 160 public String getSplitRegexp() { 161 return splitRegexp; 162 } 163 164 public XMLizable getSplitFailMessage() { 165 return splitFailMessage; 166 } 167 168 protected void setSplitFailMessage(XMLizable splitFailMessage) { 169 checkMutable(); 170 this.splitFailMessage = splitFailMessage; 171 } 172 173 protected void addSplitMapping(int group, String fieldId) { 174 checkMutable(); 175 176 if(mappedFields.contains(fieldId)) 177 throw new RuntimeException ("Field '"+fieldId+"' is already mapped to another group!"); 178 179 mappedFields.add(fieldId); 180 181 splitMappings.add(new SplitMapping(group, fieldId)); 182 } 183 184 public Iterator getSplitMappingsIterator() { 185 return splitMappings.iterator(); 186 } 187 188 public Widget createInstance() { 189 AggregateField aggregateField = new AggregateField(this); 190 191 Iterator fieldDefinitionIt = container.getWidgetDefinitions().iterator(); 192 while (fieldDefinitionIt.hasNext()) { 193 FieldDefinition fieldDefinition = (FieldDefinition)fieldDefinitionIt.next(); 194 aggregateField.addField((Field)fieldDefinition.createInstance()); 195 } 196 197 return aggregateField; 198 } 199 200 public static class SplitMapping { 201 private int group; 202 private String fieldId; 203 204 public SplitMapping(int group, String fieldId) { 205 this.group = group; 206 this.fieldId = fieldId; 207 } 208 209 public int getGroup() { 210 return group; 211 } 212 213 public String getFieldId() { 214 return fieldId; 215 } 216 } 217 } 218 | Popular Tags |