01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.formmodel;
18:
19: import org.apache.avalon.framework.service.ServiceSelector;
20: import org.apache.cocoon.forms.FormsConstants;
21: import org.apache.cocoon.forms.util.DomHelper;
22: import org.w3c.dom.Element;
23:
24: /**
25: * Builder for {@link CalculatedField}s.
26: *
27: * <p>A common calculated field definition is as follows :</p>
28: * <p><code>
29: * <fd:calculatedfield id="id" [state="{invisible|output|disabled|active}"]>
30: * <fd:datatype base="...">...</fd:datatype>
31: * <fd:label>...</fd:label>
32: * <fd:value type="...">...</fd:algorithm>
33: * </fd:calculatedfield>
34: * </code></p>
35: *
36: * <p>Since it inherits from {@link org.apache.cocoon.forms.formmodel.Field},
37: * other attributes and elements may be specified, like listeners (on-value-changed, on-create etc..) or
38: * selection lists (which could make sense if the algorithm calculates one value between many possibilities).
39: * </p>
40: *
41: * <p> Note that the default state is active, althought typing in a calculated field is useless. The state invisible
42: * can be used to create fields which are used as temporary value placeholders in a chain of calculations.
43: * </p>
44: *
45: * @version $Id: CalculatedFieldDefinitionBuilder.java 449149 2006-09-23 03:58:05Z crossley $
46: */
47: public class CalculatedFieldDefinitionBuilder extends
48: FieldDefinitionBuilder {
49:
50: public WidgetDefinition buildWidgetDefinition(Element widgetElement)
51: throws Exception {
52: CalculatedFieldDefinition definition = new CalculatedFieldDefinition();
53: setupDefinition(widgetElement, definition);
54: definition.makeImmutable();
55: return definition;
56: }
57:
58: protected void setupDefinition(Element widgetElement,
59: CalculatedFieldDefinition definition) throws Exception {
60: super .setupDefinition(widgetElement, definition);
61:
62: Element algorithmElement = DomHelper.getChildElement(
63: widgetElement, FormsConstants.DEFINITION_NS, "value");
64: ServiceSelector builderSelector = (ServiceSelector) this .serviceManager
65: .lookup(CalculatedFieldAlgorithmBuilder.ROLE
66: + "Selector");
67: CalculatedFieldAlgorithmBuilder builder = null;
68: try {
69: String algorithmType = algorithmElement
70: .getAttribute("type");
71: if (algorithmType.length() == 0)
72: algorithmType = null;
73: builder = (CalculatedFieldAlgorithmBuilder) builderSelector
74: .select(algorithmType);
75: definition.setAlgorithm(builder.build(algorithmElement));
76: } finally {
77: if (builder != null) {
78: builderSelector.release(builder);
79: }
80: this.serviceManager.release(builderSelector);
81: }
82: }
83: }
|