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.woody.binding;
18:
19: import org.apache.cocoon.woody.util.DomHelper;
20: import org.apache.cocoon.woody.Constants;
21: import org.apache.cocoon.woody.datatype.convertor.Convertor;
22: import org.apache.cocoon.i18n.I18nUtils;
23: import org.w3c.dom.Element;
24:
25: import java.util.Locale;
26:
27: /**
28: * ValueJXPathBindingBuilder provides a helper class for the Factory
29: * implemented in {@link JXPathBindingManager} that helps construct the
30: * actual {@link ValueJXPathBinding} out of the configuration in the
31: * provided configElement which looks like:
32: * <pre><code>
33: * <wb:value id="<i>widget-id</i>" path="<i>xpath-expression</i>">
34: * <!-- optional child binding to be executed upon 'save' of changed value -->
35: * <wb:on-update>
36: * <!-- any childbinding -->
37: * </wb:on-update>
38: * </wb:value>
39: * </code></pre>
40: *
41: * @version CVS $Id: ValueJXPathBindingBuilder.java 433543 2006-08-22 06:22:54Z crossley $
42: */
43: public class ValueJXPathBindingBuilder extends JXPathBindingBuilderBase {
44:
45: /**
46: * Creates an instance of {@link ValueJXPathBinding} based on the attributes
47: * and nested configuration of the provided bindingElm.
48: */
49: public JXPathBindingBase buildBinding(Element bindingElm,
50: JXPathBindingManager.Assistant assistant)
51: throws BindingException {
52:
53: try {
54: CommonAttributes commonAtts = JXPathBindingBuilderBase
55: .getCommonAttributes(bindingElm);
56: String xpath = DomHelper.getAttribute(bindingElm, "path");
57: String widgetId = DomHelper.getAttribute(bindingElm, "id");
58:
59: Element updateWrapElement = DomHelper.getChildElement(
60: bindingElm, BindingManager.NAMESPACE, "on-update");
61: JXPathBindingBase[] updateBindings = assistant
62: .makeChildBindings(updateWrapElement);
63:
64: Convertor convertor = null;
65: Locale convertorLocale = Locale.US;
66: Element convertorEl = DomHelper.getChildElement(bindingElm,
67: Constants.WD_NS, "convertor");
68: if (convertorEl != null) {
69: String datatype = DomHelper.getAttribute(convertorEl,
70: "datatype");
71: String localeStr = convertorEl.getAttribute("datatype");
72: if (!localeStr.equals("")) {
73: convertorLocale = I18nUtils.parseLocale(localeStr);
74: }
75:
76: convertor = assistant.getDatatypeManager()
77: .createConvertor(datatype, convertorEl);
78: }
79:
80: ValueJXPathBinding fieldBinding = new ValueJXPathBinding(
81: commonAtts, widgetId, xpath, updateBindings,
82: convertor, convertorLocale);
83:
84: return fieldBinding;
85: } catch (BindingException e) {
86: throw e;
87: } catch (Exception e) {
88: throw new BindingException(
89: "Error building binding defined at "
90: + DomHelper.getLocation(bindingElm), e);
91: }
92: }
93: }
|