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.forms.binding;
018:
019: import java.util.Locale;
020:
021: import org.apache.cocoon.forms.FormsConstants;
022: import org.apache.cocoon.forms.datatype.convertor.Convertor;
023: import org.apache.cocoon.forms.util.DomHelper;
024: import org.apache.cocoon.i18n.I18nUtils;
025:
026: import org.w3c.dom.Element;
027:
028: /**
029: * A simple multi field binding that will replace (i.e. delete then re-add all) its
030: * content.
031: * <pre><code>
032: * <fb:multi-value id="<i>widget-id</i>"
033: * parent-path="<i>xpath-expression</i>"
034: * row-path="<i>xpath-expression</i>">
035: * <!-- optional child binding to be executed upon 'save' of changed value -->
036: * <fb:on-update>
037: * <!-- any childbinding -->
038: * </fb:on-update>
039: * </fb:multi-value>
040: * </code></pre>
041: *
042: * @version $Id: MultiValueJXPathBindingBuilder.java 517733 2007-03-13 15:37:22Z vgritsenko $
043: */
044: public class MultiValueJXPathBindingBuilder extends
045: JXPathBindingBuilderBase {
046:
047: public JXPathBindingBase buildBinding(Element bindingElem,
048: JXPathBindingManager.Assistant assistant)
049: throws BindingException {
050:
051: try {
052: CommonAttributes commonAtts = JXPathBindingBuilderBase
053: .getCommonAttributes(bindingElem);
054:
055: String multiValueId = DomHelper.getAttribute(bindingElem,
056: "id", null);
057: String parentPath = DomHelper.getAttribute(bindingElem,
058: "parent-path", null);
059: String rowPath = DomHelper.getAttribute(bindingElem,
060: "row-path", null);
061:
062: Convertor convertor = null;
063: Locale convertorLocale = Locale.US;
064: Element convertorEl = DomHelper.getChildElement(
065: bindingElem, FormsConstants.DEFINITION_NS,
066: "convertor");
067: if (convertorEl != null) {
068: String datatype = DomHelper.getAttribute(convertorEl,
069: "datatype", null);
070: String localeStr = DomHelper.getAttribute(convertorEl,
071: "locale", null);
072: if (localeStr != null) {
073: convertorLocale = I18nUtils.parseLocale(localeStr);
074: }
075:
076: convertor = assistant.getDatatypeManager()
077: .createConvertor(datatype, convertorEl);
078: }
079:
080: MultiValueJXPathBinding existingBinding = (MultiValueJXPathBinding) assistant
081: .getContext().getSuperBinding();
082: JXPathBindingBase[] existingBindings = new JXPathBindingBase[0];
083: if (existingBinding != null) {
084: commonAtts = JXPathBindingBuilderBase
085: .mergeCommonAttributes(existingBinding
086: .getCommonAtts(), commonAtts);
087: existingBindings = existingBinding.getUpdateBinding()
088: .getChildBindings();
089:
090: if (multiValueId == null)
091: multiValueId = existingBinding.getId();
092: if (parentPath == null)
093: parentPath = existingBinding.getMultiValuePath();
094: if (rowPath == null)
095: rowPath = existingBinding.getRowPath();
096:
097: if (convertor == null) {
098: convertor = existingBinding.getConvertor();
099: convertorLocale = existingBinding.getLocale();
100: }
101: }
102:
103: Element updateWrapElement = DomHelper.getChildElement(
104: bindingElem, BindingManager.NAMESPACE, "on-update");
105: JXPathBindingBase[] updateBindings = assistant
106: .makeChildBindings(updateWrapElement,
107: existingBindings);
108:
109: return new MultiValueJXPathBinding(commonAtts,
110: multiValueId, parentPath, rowPath, updateBindings,
111: convertor, convertorLocale);
112: } catch (BindingException e) {
113: throw e;
114: } catch (Exception e) {
115: throw new BindingException(
116: "Error building multi value binding", e, DomHelper
117: .getLocationObject(bindingElem));
118: }
119: }
120: }
|