KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > AggregateFieldDefinition


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * The {@link WidgetDefinition} part of a AggregateField widget, see {@link AggregateField} for more information.
32  *
33  * @version $Id: AggregateFieldDefinition.java 289538 2005-09-16 13:46:22Z sylvain $
34  */

35 public class AggregateFieldDefinition extends FieldDefinition {
36
37     /**
38      * Defines expression which combines values of nested fields into this value
39      */

40     private Expression combineExpr;
41
42     /**
43      * Regular expression which splits this value on the values of the nested fields.
44      * It is compiled into the {@link #splitPattern}.
45      */

46     private String JavaDoc splitRegexp;
47
48     /**
49      * Compiled pattern out of the {@link #splitRegexp} regular expression.
50      */

51     private Pattern splitPattern;
52
53     /**
54      * Message to be displayed when the {@link #setSplitPattern(Pattern, String) splitPattern}
55      * does not match what the user entered. Optional.
56      */

57     protected XMLizable splitFailMessage;
58
59     /**
60      * List containing instances of {@link SplitMapping}, i.e. the mapping between
61      * a group (paren) from the regular expression and corresponding field id.
62      */

63     private List JavaDoc splitMappings = new ArrayList JavaDoc();
64     
65     /**
66      * Set containing widgets mapped to groups to find out if a specific widget has been mapped already
67      */

68     private Set JavaDoc mappedFields = new HashSet JavaDoc();
69
70     /**
71      *
72      */

73     private WidgetDefinitionList container = new WidgetDefinitionList(this);
74
75     /**
76      * initialize this definition with the other, sort of like a copy constructor
77      */

78     public void initializeFrom(WidgetDefinition definition) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc fields = other.mappedFields.iterator();
97             while(fields.hasNext()) {
98                 this.mappedFields.add(fields.next());
99             }
100             
101         } else {
102             throw new Exception JavaDoc("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     /**
112      * checks completeness of this definition
113      */

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         // now check children's completeness
130
List JavaDoc defs = container.getWidgetDefinitions();
131         Iterator JavaDoc it = defs.iterator();
132         while(it.hasNext()) {
133             ((WidgetDefinition)it.next()).checkCompleteness();
134         }
135     }
136
137     public boolean hasWidget(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc fieldId) {
174         checkMutable();
175         
176         if(mappedFields.contains(fieldId))
177             throw new RuntimeException JavaDoc("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 JavaDoc getSplitMappingsIterator() {
185         return splitMappings.iterator();
186     }
187
188     public Widget createInstance() {
189         AggregateField aggregateField = new AggregateField(this);
190
191         Iterator JavaDoc 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 JavaDoc fieldId;
203
204         public SplitMapping(int group, String JavaDoc fieldId) {
205             this.group = group;
206             this.fieldId = fieldId;
207         }
208
209         public int getGroup() {
210             return group;
211         }
212
213         public String JavaDoc getFieldId() {
214             return fieldId;
215         }
216     }
217 }
218
Popular Tags