KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
19
20 import org.apache.avalon.framework.logger.Logger;
21 import org.apache.cocoon.woody.formmodel.Repeater;
22 import org.apache.cocoon.woody.formmodel.Widget;
23 import org.apache.commons.jxpath.JXPathContext;
24 import org.apache.commons.jxpath.Pointer;
25
26 /**
27  * Simple binding for repeaters: on save, first deletes the target data
28  * before recreating it from scratch.
29  * <p>
30  * For a smarter binding that avoids deletion and recreation, consider
31  * {@link org.apache.cocoon.woody.binding.RepeaterJXPathBinding}
32  *
33  * @author <a HREF="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
34  * @version CVS $Id: SimpleRepeaterJXPathBinding.java 30932 2004-07-29 17:35:38Z vgritsenko $
35  */

36 public class SimpleRepeaterJXPathBinding extends JXPathBindingBase {
37
38     private final String JavaDoc repeaterId;
39     private final String JavaDoc repeaterPath;
40     private final String JavaDoc rowPath;
41     private final boolean clearOnLoad;
42     private final JXPathBindingBase rowBinding;
43     private final boolean deleteIfEmpty;
44
45     public SimpleRepeaterJXPathBinding(
46             JXPathBindingBuilderBase.CommonAttributes commonAtts,
47             String JavaDoc repeaterId, String JavaDoc repeaterPath, String JavaDoc rowPath,
48             boolean clearOnLoad, boolean deleteIfEmpty,
49             JXPathBindingBase rowBinding) {
50         super(commonAtts);
51         this.repeaterId = repeaterId;
52         this.repeaterPath = repeaterPath;
53         this.rowPath = rowPath;
54         this.rowBinding = rowBinding;
55         this.clearOnLoad = clearOnLoad;
56         this.deleteIfEmpty = deleteIfEmpty;
57     }
58
59     public void doLoad(Widget frmModel, JXPathContext jctx)
60             throws BindingException {
61         // Find the repeater and clear it
62
Repeater repeater = (Repeater)frmModel.getWidget(this.repeaterId);
63
64         if (this.clearOnLoad) {
65             repeater.removeRows();
66         }
67
68         // Move to repeater context
69
Pointer ptr = jctx.getPointer(this.repeaterPath);
70         if (ptr.getNode() != null) {
71             // There are some nodes to load from
72

73             JXPathContext repeaterContext = jctx.getRelativeContext(ptr);
74             // build a jxpath iterator for pointers
75
Iterator JavaDoc rowPointers = repeaterContext.iteratePointers(this.rowPath);
76
77             //iterate through it
78
int rowNum = 0;
79             while (rowPointers.hasNext()) {
80                 // Get a row. It is created if needed (depends on clearOnLoad)
81
Repeater.RepeaterRow thisRow;
82                 if (repeater.getSize() > rowNum) {
83                     thisRow = repeater.getRow(rowNum);
84                 } else {
85                     thisRow = repeater.addRow();
86                 }
87                 rowNum++;
88
89                 // make a jxpath sub context on the iterated element
90
Pointer jxp = (Pointer) rowPointers.next();
91                 JXPathContext rowContext = repeaterContext.getRelativeContext(jxp);
92
93                 this.rowBinding.loadFormFromModel(thisRow, rowContext);
94             }
95         }
96         if (getLogger().isDebugEnabled()) {
97             getLogger().debug("done loading rows " + toString());
98         }
99     }
100
101     public void doSave(Widget frmModel, JXPathContext jctx)
102             throws BindingException {
103         // Find the repeater
104
Repeater repeater = (Repeater)frmModel.getWidget(this.repeaterId);
105
106         if (repeater.getSize() == 0 && this.deleteIfEmpty) {
107             // Repeater is empty : erase all
108
jctx.removeAll(this.repeaterPath);
109         } else {
110             // Repeater is not empty
111
// Move to repeater context and create the path if needed
112
JXPathContext repeaterContext =
113                 jctx.getRelativeContext(jctx.createPath(this.repeaterPath));
114
115             // Delete all that is already present
116
repeaterContext.removeAll(this.rowPath);
117
118             for (int i = 0; i < repeater.getSize(); i++) {
119                 Pointer rowPtr = repeaterContext.createPath(
120                         this.rowPath + '[' + (i+1) + ']');
121                 JXPathContext rowContext =
122                     repeaterContext.getRelativeContext(rowPtr);
123                 this.rowBinding.saveFormToModel(repeater.getRow(i),
124                         rowContext);
125             }
126         }
127     }
128
129     public String JavaDoc toString() {
130         return this.getClass().getName()+ " [widget=" + this.repeaterId +
131             ", xpath=" + this.repeaterPath + "]";
132     }
133
134     public void enableLogging(Logger logger) {
135         super.enableLogging(logger);
136         this.rowBinding.enableLogging(logger);
137     }
138 }
139
Popular Tags