1 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 ; 26 27 32 public class AggregateFieldDefinitionBuilder extends FieldDefinitionBuilder { 33 34 public WidgetDefinition buildWidgetDefinition(Element widgetElement) throws Exception { 35 AggregateFieldDefinition definition = new AggregateFieldDefinition(); 36 setupDefinition(widgetElement, definition); 37 definition.makeImmutable(); 38 return definition; 39 } 40 41 protected void setupDefinition(Element widgetElement, AggregateFieldDefinition definition) throws Exception { 42 43 super.setupDefinition(widgetElement, definition); 45 46 Element widgetsElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "widgets", true); 48 Element [] 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 Element splitElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "split", true); 56 if(splitElement!=null) { 57 String 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 ("Invalid regular expression at " + DomHelper.getLocation(splitElement) + ": " + e.getMessage()); 64 } 65 definition.setSplitPattern(pattern, patternString); 66 } 67 68 Element [] 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 field = DomHelper.getAttribute(mapElements[i], "field"); 73 if (!definition.hasWidget(field)) { 75 throw new Exception ("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 e) { 83 throw new Exception ("Two groups are mapped to the same widget id \"" + field + "\", at " + 84 DomHelper.getLocation(mapElements[i])); 85 } 86 } 87 88 Element 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 Element combineElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "combine", true); 97 if(combineElement!=null) { 98 String combineExprString = DomHelper.getAttribute(combineElement, "expression"); 99 Expression combineExpr = null; 100 try { 101 combineExpr = expressionManager.parse(combineExprString); 102 } catch (Exception e) { 103 throw new Exception ("Problem with combine expression defined at " + 104 DomHelper.getLocation(combineElement) + ": " + e.getMessage()); 105 } 106 Class clazz = definition.getDatatype().getTypeClass(); 107 if (combineExpr.getResultType() != null && !clazz.isAssignableFrom(combineExpr.getResultType())) { 108 throw new Exception ("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 |