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: * ValueJXPathBindingBuilder provides a helper class for the Factory
030: * implemented in {@link JXPathBindingManager} that helps construct the
031: * actual {@link ValueJXPathBinding} out of the configuration in the
032: * provided configElement which looks like:
033: * <pre><code>
034: * <fb:value id="<i>widget-id</i>" 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:value>
040: * </code></pre>
041: *
042: * @version $Id: ValueJXPathBindingBuilder.java 517733 2007-03-13 15:37:22Z vgritsenko $
043: */
044: public class ValueJXPathBindingBuilder extends JXPathBindingBuilderBase {
045:
046: /**
047: * Creates an instance of {@link ValueJXPathBinding} based on the attributes
048: * and nested configuration of the provided bindingElm.
049: */
050: public JXPathBindingBase buildBinding(Element bindingElm,
051: JXPathBindingManager.Assistant assistant)
052: throws BindingException {
053:
054: try {
055: CommonAttributes commonAtts = JXPathBindingBuilderBase
056: .getCommonAttributes(bindingElm);
057: String xpath = DomHelper.getAttribute(bindingElm, "path",
058: null);
059: String widgetId = DomHelper.getAttribute(bindingElm, "id",
060: null);
061:
062: Convertor convertor = null;
063: Locale convertorLocale = Locale.US;
064: Element convertorEl = DomHelper.getChildElement(bindingElm,
065: FormsConstants.DEFINITION_NS, "convertor");
066: if (convertorEl != null) {
067: String datatype = DomHelper.getAttribute(convertorEl,
068: "datatype");
069: String localeStr = DomHelper.getAttribute(convertorEl,
070: "locale", null);
071: if (localeStr != null) {
072: convertorLocale = I18nUtils.parseLocale(localeStr);
073: }
074:
075: convertor = assistant.getDatatypeManager()
076: .createConvertor(datatype, convertorEl);
077: }
078:
079: // do inheritance
080: ValueJXPathBinding otherBinding = (ValueJXPathBinding) assistant
081: .getContext().getSuperBinding();
082: JXPathBindingBase[] existingUpdateBindings = null;
083: if (otherBinding != null) {
084: commonAtts = JXPathBindingBuilderBase
085: .mergeCommonAttributes(otherBinding
086: .getCommonAtts(), commonAtts);
087:
088: if (xpath == null) {
089: xpath = otherBinding.getXPath();
090: }
091: if (widgetId == null) {
092: widgetId = otherBinding.getId();
093: }
094: if (convertor == null) {
095: convertor = otherBinding.getConvertor();
096: }
097: if (convertorLocale == null) {
098: convertorLocale = otherBinding.getConvertorLocale();
099: }
100: if (convertorLocale == null) {
101: convertorLocale = otherBinding.getConvertorLocale();
102: }
103:
104: existingUpdateBindings = otherBinding
105: .getUpdateBinding().getChildBindings();
106: }
107:
108: Element updateWrapElement = DomHelper.getChildElement(
109: bindingElm, BindingManager.NAMESPACE, "on-update");
110: JXPathBindingBase[] updateBindings = assistant
111: .makeChildBindings(updateWrapElement,
112: existingUpdateBindings);
113:
114: return new ValueJXPathBinding(commonAtts, widgetId, xpath,
115: updateBindings, convertor, convertorLocale);
116: } catch (BindingException e) {
117: throw e;
118: } catch (Exception e) {
119: throw new BindingException("Error building binding", e,
120: DomHelper.getLocationObject(bindingElm));
121: }
122: }
123: }
|