KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 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.cocoon.forms.FormsConstants;
19 import org.apache.cocoon.forms.util.DomHelper;
20 import org.apache.excalibur.xml.sax.XMLizable;
21 import org.apache.oro.text.regex.MalformedPatternException;
22 import org.apache.oro.text.regex.Pattern;
23 import org.apache.oro.text.regex.Perl5Compiler;
24 import org.outerj.expression.Expression;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * Builds {@link AggregateFieldDefinition}s.
29  *
30  * @version $Id: AggregateFieldDefinitionBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
31  */

32 public class AggregateFieldDefinitionBuilder extends FieldDefinitionBuilder {
33
34     public WidgetDefinition buildWidgetDefinition(Element JavaDoc widgetElement) throws Exception JavaDoc {
35         AggregateFieldDefinition definition = new AggregateFieldDefinition();
36         setupDefinition(widgetElement, definition);
37         definition.makeImmutable();
38         return definition;
39     }
40     
41     protected void setupDefinition(Element JavaDoc widgetElement, AggregateFieldDefinition definition) throws Exception JavaDoc {
42         
43         // parse the field definition
44
super.setupDefinition(widgetElement, definition);
45
46         // make children fields
47
Element JavaDoc widgetsElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "widgets", true);
48         Element JavaDoc[] fieldElements = DomHelper.getChildElements(widgetsElement, FormsConstants.DEFINITION_NS, "field");
49         for (int i = 0; i < fieldElements.length; i++) {
50             FieldDefinition fieldDefinition = (FieldDefinition)buildAnotherWidgetDefinition(fieldElements[i]);
51             definition.addWidgetDefinition(fieldDefinition);
52         }
53
54         // compile splitpattern
55
Element JavaDoc splitElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "split", true);
56         if(splitElement!=null) {
57             String JavaDoc patternString = DomHelper.getAttribute(splitElement, "pattern");
58             Perl5Compiler compiler = new Perl5Compiler();
59             Pattern pattern = null;
60             try {
61                 pattern = compiler.compile(patternString, Perl5Compiler.READ_ONLY_MASK);
62             } catch (MalformedPatternException e) {
63                 throw new Exception JavaDoc("Invalid regular expression at " + DomHelper.getLocation(splitElement) + ": " + e.getMessage());
64             }
65             definition.setSplitPattern(pattern, patternString);
66         }
67
68         // read split mappings
69
Element JavaDoc[] mapElements = DomHelper.getChildElements(splitElement, FormsConstants.DEFINITION_NS, "map");
70         for (int i = 0; i < mapElements.length; i++) {
71             int group = DomHelper.getAttributeAsInteger(mapElements[i], "group");
72             String JavaDoc field = DomHelper.getAttribute(mapElements[i], "field");
73             // check that this field exists
74
if (!definition.hasWidget(field)) {
75                 throw new Exception JavaDoc("Unknown widget id \"" + field + "\", at " +
76                                     DomHelper.getLocation(mapElements[i]));
77             }
78             
79             try {
80                 definition.addSplitMapping(group, field);
81                 System.out.println("Aggregate: addSplitMapping("+group+","+field+")");
82             } catch(RuntimeException JavaDoc e) {
83                 throw new Exception JavaDoc("Two groups are mapped to the same widget id \"" + field + "\", at " +
84                         DomHelper.getLocation(mapElements[i]));
85             }
86         }
87
88         // read split fail message (if any)
89
Element JavaDoc failMessageElement = DomHelper.getChildElement(splitElement, FormsConstants.DEFINITION_NS, "failmessage");
90         if (failMessageElement != null) {
91             XMLizable failMessage = DomHelper.compileElementContent(failMessageElement);
92             definition.setSplitFailMessage(failMessage);
93         }
94
95         // compile combine expression
96
Element JavaDoc combineElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "combine", true);
97         if(combineElement!=null) {
98             String JavaDoc combineExprString = DomHelper.getAttribute(combineElement, "expression");
99             Expression combineExpr = null;
100             try {
101                 combineExpr = expressionManager.parse(combineExprString);
102             } catch (Exception JavaDoc e) {
103                 throw new Exception JavaDoc("Problem with combine expression defined at " +
104                                     DomHelper.getLocation(combineElement) + ": " + e.getMessage());
105             }
106             Class JavaDoc clazz = definition.getDatatype().getTypeClass();
107             if (combineExpr.getResultType() != null && !clazz.isAssignableFrom(combineExpr.getResultType())) {
108                 throw new Exception JavaDoc("The result of the combine expression should be " + clazz.getName() + ", at " +
109                                     DomHelper.getLocation(combineElement));
110             }
111             definition.setCombineExpression(combineExpr);
112         }
113     }
114 }
115
Popular Tags