1 16 package org.apache.cocoon.woody.binding; 17 18 import java.util.Iterator ; 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 36 public class SimpleRepeaterJXPathBinding extends JXPathBindingBase { 37 38 private final String repeaterId; 39 private final String repeaterPath; 40 private final String 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 repeaterId, String repeaterPath, String 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 Repeater repeater = (Repeater)frmModel.getWidget(this.repeaterId); 63 64 if (this.clearOnLoad) { 65 repeater.removeRows(); 66 } 67 68 Pointer ptr = jctx.getPointer(this.repeaterPath); 70 if (ptr.getNode() != null) { 71 73 JXPathContext repeaterContext = jctx.getRelativeContext(ptr); 74 Iterator rowPointers = repeaterContext.iteratePointers(this.rowPath); 76 77 int rowNum = 0; 79 while (rowPointers.hasNext()) { 80 Repeater.RepeaterRow thisRow; 82 if (repeater.getSize() > rowNum) { 83 thisRow = repeater.getRow(rowNum); 84 } else { 85 thisRow = repeater.addRow(); 86 } 87 rowNum++; 88 89 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 Repeater repeater = (Repeater)frmModel.getWidget(this.repeaterId); 105 106 if (repeater.getSize() == 0 && this.deleteIfEmpty) { 107 jctx.removeAll(this.repeaterPath); 109 } else { 110 JXPathContext repeaterContext = 113 jctx.getRelativeContext(jctx.createPath(this.repeaterPath)); 114 115 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 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 |