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: * UniqueFieldJXPathBindingBuilder provides a helper class for the Factory
29: * implemented in {@link JXPathBindingManager} that helps construct the
30: * actual {@link UniqueFieldJXPathBinding} out of the configuration in the
31: * provided configElement which looks like:
32: * <pre><code>
33: * <wb:unique-field id="<i>widget-id</i>" path="<i>xpath-expression</i>">
34: * <!-- optional convertor of these field -->
35: * <wd:convertor>
36: * <!-- any convertor -->
37: * </wd:convertor>
38: * </wb:unique-field>
39: * </code></pre>
40: *
41: * @version CVS $Id: UniqueFieldJXPathBindingBuilder.java 433543 2006-08-22 06:22:54Z crossley $
42: */
43: public class UniqueFieldJXPathBindingBuilder extends
44: JXPathBindingBuilderBase {
45:
46: /**
47: * Creates an instance of {@link UniqueFieldJXPathBinding} based on the attributes
48: * and nested configuration of the provided bindingElm.
49: */
50: public JXPathBindingBase buildBinding(Element bindingElm,
51: JXPathBindingManager.Assistant assistant)
52: throws BindingException {
53:
54: try {
55: CommonAttributes commonAtts = JXPathBindingBuilderBase
56: .getCommonAttributes(bindingElm);
57: String widgetId = DomHelper.getAttribute(bindingElm, "id");
58: String xpath = DomHelper.getAttribute(bindingElm, "path");
59:
60: Convertor convertor = null;
61: Locale convertorLocale = Locale.US;
62: Element convertorEl = DomHelper.getChildElement(bindingElm,
63: Constants.WD_NS, "convertor");
64: if (convertorEl != null) {
65: String datatype = DomHelper.getAttribute(convertorEl,
66: "datatype");
67: String localeStr = convertorEl.getAttribute("datatype");
68: if (!localeStr.equals("")) {
69: convertorLocale = I18nUtils.parseLocale(localeStr);
70: }
71: convertor = assistant.getDatatypeManager()
72: .createConvertor(datatype, convertorEl);
73: }
74:
75: UniqueFieldJXPathBinding fieldBinding = new UniqueFieldJXPathBinding(
76: commonAtts, widgetId, xpath, convertor,
77: convertorLocale);
78:
79: return fieldBinding;
80: } catch (BindingException e) {
81: throw e;
82: } catch (Exception e) {
83: throw new BindingException(
84: "Error building binding defined at "
85: + DomHelper.getLocation(bindingElm), e);
86: }
87: }
88: }
|