KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > binding > RepeaterJXPathBindingBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.woody.binding;
17
18 import java.util.Locale JavaDoc;
19
20 import org.apache.cocoon.i18n.I18nUtils;
21 import org.apache.cocoon.woody.Constants;
22 import org.apache.cocoon.woody.datatype.convertor.Convertor;
23 import org.apache.cocoon.woody.util.DomHelper;
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * RepeaterJXPathBindingBuilder provides a helper class for the Factory
28  * implemented in {@link JXPathBindingManager} that helps construct the
29  * actual {@link RepeaterJXPathBinding} out of the configuration in the
30  * provided configElement which looks like:
31  * <pre><code>
32  * &lt;wb:repeater
33  * id="contacts"
34  * parent-path="contacts"
35  * row-path="contact"
36  * unique-row-id="id"
37  * unique-path="@id" &gt;
38  *
39  * &lt;wb:on-bind&gt;
40  * &lt;!-- nested bindings executed on updates AND right after the insert --&gt;
41  * &lt;/wb:on-bind&gt;
42  *
43  * &lt;wb:on-delete-row&gt;
44  * &lt;!-- nested bindings executed on deletion of row --&gt;
45  * &lt;/wb:on-delete-row&gt;
46  *
47  * &lt;wb:on-insert-row&gt;
48  * &lt;!-- nested bindings executed to prepare the insertion of a row --&gt;
49  * &lt;/wb:on-insert-row&gt;
50  *
51  * &lt;/wb:repeater&gt;
52  * </code></pre>
53  *
54  * @version CVS $Id: RepeaterJXPathBindingBuilder.java 30932 2004-07-29 17:35:38Z vgritsenko $
55  */

56 public class RepeaterJXPathBindingBuilder extends JXPathBindingBuilderBase {
57
58     /**
59      * Creates an instance of {@link RepeaterJXPathBinding} according to the
60      * attributes and nested comfiguration elements of the bindingElm.
61      *
62      * @param bindingElm
63      * @param assistant
64      * @return JXPathBindingBase
65      */

66     public JXPathBindingBase buildBinding(Element JavaDoc bindingElm,
67             JXPathBindingManager.Assistant assistant) throws BindingException {
68
69         try {
70             CommonAttributes commonAtts =
71                 JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
72
73             String JavaDoc repeaterId = DomHelper.getAttribute(bindingElm, "id");
74             String JavaDoc parentPath =
75                 DomHelper.getAttribute(bindingElm, "parent-path");
76             String JavaDoc rowPath = DomHelper.getAttribute(bindingElm, "row-path");
77             String JavaDoc rowPathForInsert =
78                 DomHelper.getAttribute(bindingElm, "row-path-insert", rowPath);
79             String JavaDoc uniqueRowId =
80                 DomHelper.getAttribute(bindingElm, "unique-row-id", null);
81             String JavaDoc uniqueRowIdPath =
82                 DomHelper.getAttribute(bindingElm, "unique-path", null);
83
84             Convertor convertor = null;
85             Locale JavaDoc convertorLocale = Locale.US;
86             Element JavaDoc convertorEl =
87                 DomHelper.getChildElement(bindingElm,
88                         Constants.WD_NS, "convertor");
89             if (convertorEl != null) {
90                 String JavaDoc datatype =
91                     DomHelper.getAttribute(convertorEl, "datatype");
92                 String JavaDoc localeStr = convertorEl.getAttribute("datatype");
93                 if (!localeStr.equals("")) {
94                     convertorLocale = I18nUtils.parseLocale(localeStr);
95                 }
96                 convertor =
97                     assistant.getDatatypeManager().createConvertor(datatype,
98                             convertorEl);
99             }
100
101             Element JavaDoc childWrapElement = DomHelper.getChildElement(bindingElm,
102                     BindingManager.NAMESPACE, "on-bind");
103             if (childWrapElement == null) {
104                 throw new BindingException(
105                       "RepeaterBinding misses '<on-bind>' child definition. " +
106                       DomHelper.getLocation(bindingElm));
107             }
108             JXPathBindingBase[] childBindings =
109                 assistant.makeChildBindings(childWrapElement);
110
111             Element JavaDoc deleteWrapElement = DomHelper.getChildElement(bindingElm,
112                     BindingManager.NAMESPACE, "on-delete-row");
113             JXPathBindingBase[] deleteBindings = null;
114             if (deleteWrapElement != null) {
115                 deleteBindings =
116                     assistant.makeChildBindings(deleteWrapElement);
117             }
118
119             Element JavaDoc insertWrapElement = DomHelper.getChildElement(bindingElm,
120                     BindingManager.NAMESPACE, "on-insert-row");
121             JXPathBindingBase insertBinding = null;
122             if (insertWrapElement != null) {
123                 insertBinding =
124                     assistant.makeChildBindings(insertWrapElement)[0];
125             }
126             /* New <wb:unique-row> child element builder */
127             Element JavaDoc uniqueFieldWrapElement = DomHelper.getChildElement(bindingElm,
128                     BindingManager.NAMESPACE, "unique-row");
129             JXPathBindingBase[] uniqueFieldBinding = null;
130             if (uniqueFieldWrapElement != null) {
131                 uniqueFieldBinding = assistant.makeChildBindings(uniqueFieldWrapElement);
132             } else if (uniqueRowId == null || uniqueRowIdPath == null) {
133                 throw new BindingException(
134                       "RepeaterBinding misses '<unique-row>' child definition. " +
135                       DomHelper.getLocation(bindingElm));
136             } else {
137                 if (this.getLogger().isInfoEnabled()) {
138                 this.getLogger().info("<wb:repeater>: The attributes 'unique-row-id' and " +
139                         "'unique-path' are deprecated. Use <unique-row> child element instead." +
140                         " Located at " + DomHelper.getLocation(bindingElm));
141                 }
142             }
143
144             RepeaterJXPathBinding repeaterBinding =
145                 new RepeaterJXPathBinding(commonAtts, repeaterId, parentPath,
146                         rowPath, rowPathForInsert, uniqueRowId,
147                         uniqueRowIdPath, convertor, convertorLocale,
148                         childBindings, insertBinding, deleteBindings, uniqueFieldBinding);
149             return repeaterBinding;
150         } catch (BindingException e) {
151             throw e;
152         } catch (Exception JavaDoc e) {
153             throw new BindingException(
154                     "Error building repeater binding defined at " +
155                     DomHelper.getLocation(bindingElm), e);
156         }
157     }
158 }
159
Popular Tags