001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.woody.formmodel;
018:
019: import org.apache.excalibur.xml.sax.XMLizable;
020: import org.apache.oro.text.regex.Pattern;
021:
022: import org.outerj.expression.Expression;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: /**
029: * The {@link WidgetDefinition} part of a AggregateField widget, see {@link AggregateField} for more information.
030: *
031: * @version $Id: AggregateFieldDefinition.java 433543 2006-08-22 06:22:54Z crossley $
032: */
033: public class AggregateFieldDefinition extends FieldDefinition {
034:
035: /**
036: * Defines expression which combines values of nested fields into this value
037: */
038: private Expression combineExpr;
039:
040: /**
041: * Regular expression which splits this value on the values of the nested fields.
042: * It is compiled into the {@link #splitPattern}.
043: */
044: private String splitRegexp;
045:
046: /**
047: * Compiled pattern out of the {@link #splitRegexp} regular expression.
048: */
049: private Pattern splitPattern;
050:
051: /**
052: * Message to be displayed when the {@link #setSplitPattern(Pattern, String) splitPattern}
053: * does not match what the user entered. Optional.
054: */
055: protected XMLizable splitFailMessage;
056:
057: /**
058: * List containing instances of {@link SplitMapping}, i.e. the mapping between
059: * a group (paren) from the regular expression and corresponding field id.
060: */
061: private List splitMappings = new ArrayList();
062:
063: /**
064: *
065: */
066: private ContainerDefinitionDelegate container = new ContainerDefinitionDelegate(
067: this );
068:
069: public void addWidgetDefinition(WidgetDefinition widgetDefinition)
070: throws DuplicateIdException {
071: container.addWidgetDefinition(widgetDefinition);
072: }
073:
074: public boolean hasWidget(String id) {
075: return container.hasWidget(id);
076: }
077:
078: protected void setCombineExpression(Expression expression) {
079: combineExpr = expression;
080: }
081:
082: public Expression getCombineExpression() {
083: return combineExpr;
084: }
085:
086: protected void setSplitPattern(Pattern pattern, String regexp) {
087: this .splitPattern = pattern;
088: this .splitRegexp = regexp;
089: }
090:
091: public Pattern getSplitPattern() {
092: return splitPattern;
093: }
094:
095: public String getSplitRegexp() {
096: return splitRegexp;
097: }
098:
099: public XMLizable getSplitFailMessage() {
100: return splitFailMessage;
101: }
102:
103: protected void setSplitFailMessage(XMLizable splitFailMessage) {
104: this .splitFailMessage = splitFailMessage;
105: }
106:
107: protected void addSplitMapping(int group, String fieldId) {
108: splitMappings.add(new SplitMapping(group, fieldId));
109: }
110:
111: public Iterator getSplitMappingsIterator() {
112: return splitMappings.iterator();
113: }
114:
115: public Widget createInstance() {
116: AggregateField aggregateField = new AggregateField(this );
117:
118: Iterator fieldDefinitionIt = container.getWidgetDefinitions()
119: .iterator();
120: while (fieldDefinitionIt.hasNext()) {
121: FieldDefinition fieldDefinition = (FieldDefinition) fieldDefinitionIt
122: .next();
123: aggregateField.addField((Field) fieldDefinition
124: .createInstance());
125: }
126:
127: return aggregateField;
128: }
129:
130: public static class SplitMapping {
131: private int group;
132: private String fieldId;
133:
134: public SplitMapping(int group, String fieldId) {
135: this .group = group;
136: this .fieldId = fieldId;
137: }
138:
139: public int getGroup() {
140: return group;
141: }
142:
143: public String getFieldId() {
144: return fieldId;
145: }
146: }
147: }
|