1 16 package org.apache.cocoon.woody.binding; 17 18 import org.apache.avalon.framework.logger.Logger; 19 import org.apache.cocoon.woody.formmodel.Widget; 20 import org.apache.cocoon.woody.datatype.convertor.Convertor; 21 import org.apache.commons.jxpath.JXPathContext; 22 import org.apache.commons.jxpath.JXPathException; 23 24 import java.util.Locale ; 25 26 34 public class ValueJXPathBinding extends JXPathBindingBase { 35 36 39 private final String xpath; 40 41 44 private final String fieldId; 45 46 49 private final JXPathBindingBase updateBinding; 50 51 56 private final Convertor convertor; 57 58 61 private final Locale convertorLocale; 62 63 68 public ValueJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String widgetId, String xpath, JXPathBindingBase[] updateBindings, 69 Convertor convertor, Locale convertorLocale) { 70 super(commonAtts); 71 this.fieldId = widgetId; 72 this.xpath = xpath; 73 this.updateBinding = new ComposedJXPathBindingBase(JXPathBindingBuilderBase.CommonAttributes.DEFAULT, updateBindings); 74 this.convertor = convertor; 75 this.convertorLocale = convertorLocale; 76 } 77 78 82 public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException { 83 Widget widget = frmModel.getWidget(this.fieldId); 84 if (widget == null) { 85 throw new BindingException("The widget with the ID [" + this.fieldId 86 + "] referenced in the binding does not exist in the form definition."); 87 } 88 89 Object value = jxpc.getValue(this.xpath); 90 if (value != null && convertor != null) { 91 if (value instanceof String ) { 92 value = convertor.convertFromString((String )value, convertorLocale, null); 93 } else { 94 getLogger().warn("Convertor ignored on backend-value which isn't of type String."); 95 } 96 } 97 98 widget.setValue(value); 99 if (getLogger().isDebugEnabled()) { 100 getLogger().debug("Done loading " + toString() + " -- value= " + value); 101 } 102 } 103 104 108 public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException { 109 Widget widget = frmModel.getWidget(this.fieldId); 110 Object value = widget.getValue(); 111 if (value != null && convertor != null) { 112 value = convertor.convertToString(value, convertorLocale, null); 113 } 114 115 Object oldValue = jxpc.getValue(this.xpath); 116 if (getLogger().isDebugEnabled()) { 117 getLogger().debug("value= " + value + "-- oldvalue=" + oldValue); 118 } 119 120 boolean update = false; 121 122 if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) { 123 jxpc.createPathAndSetValue(this.xpath, value); 125 126 JXPathContext subContext = null; 128 try { 129 subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath)); 130 } catch (JXPathException e) { 131 if (getLogger().isDebugEnabled()) { 134 getLogger().debug("(Ignorable) problem binding field " + widget.getFullyQualifiedId(), e); 135 } 136 } 137 if (subContext != null) { 138 this.updateBinding.saveFormToModel(frmModel, subContext); 139 } 140 141 update = true; 142 } 143 144 if (getLogger().isDebugEnabled()) { 145 getLogger().debug("done saving " + toString() + " -- value= " + value + " -- on-update == " + update); 146 } 147 } 148 149 public String toString() { 150 return "ValueJXPathBinding [widget=" + this.fieldId + ", xpath=" + this.xpath + "]"; 151 } 152 153 public void enableLogging(Logger logger) { 154 super.enableLogging(logger); 155 this.updateBinding.enableLogging(logger); 156 } 157 } 158 | Popular Tags |