1 16 package org.apache.cocoon.forms.binding; 17 18 import java.util.Locale ; 19 20 import org.apache.avalon.framework.logger.Logger; 21 import org.apache.cocoon.forms.datatype.convertor.Convertor; 22 import org.apache.cocoon.forms.datatype.convertor.ConversionResult; 23 import org.apache.cocoon.forms.formmodel.Widget; 24 import org.apache.commons.jxpath.JXPathContext; 25 import org.apache.commons.jxpath.JXPathException; 26 27 35 public class ValueJXPathBinding extends JXPathBindingBase { 36 37 40 private final String xpath; 41 42 45 private final String fieldId; 46 47 50 private final JXPathBindingBase updateBinding; 51 52 57 private final Convertor convertor; 58 59 62 private final Locale convertorLocale; 63 64 69 public ValueJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String widgetId, String xpath, JXPathBindingBase[] updateBindings, 70 Convertor convertor, Locale convertorLocale) { 71 super(commonAtts); 72 this.fieldId = widgetId; 73 this.xpath = xpath; 74 this.updateBinding = new ComposedJXPathBindingBase(JXPathBindingBuilderBase.CommonAttributes.DEFAULT, updateBindings); 75 this.convertor = convertor; 76 this.convertorLocale = convertorLocale; 77 } 78 79 public String getId() { return fieldId; } 80 public ComposedJXPathBindingBase getUpdateBinding() { return (ComposedJXPathBindingBase)updateBinding; } 81 82 86 public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException { 87 Widget widget = selectWidget(frmModel, this.fieldId); 88 if (widget == null) { 89 throw new BindingException("The widget with the ID [" + this.fieldId 90 + "] referenced in the binding does not exist in the form definition."); 91 } 92 93 Object value = jxpc.getValue(this.xpath); 94 if (value != null && convertor != null) { 95 if (value instanceof String ) { 96 ConversionResult conversionResult = convertor.convertFromString((String )value, convertorLocale, null); 97 if (conversionResult.isSuccessful()) 98 value = conversionResult.getResult(); 99 else 100 value = null; 101 } else { 102 getLogger().warn("Convertor ignored on backend-value which isn't of type String."); 103 } 104 } 105 106 widget.setValue(value); 107 if (getLogger().isDebugEnabled()) { 108 getLogger().debug("Done loading " + toString() + " -- value= " + value); 109 } 110 } 111 112 116 public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException { 117 Widget widget = selectWidget(frmModel, this.fieldId); 118 Object value = widget.getValue(); 119 if (value != null && convertor != null) { 120 value = convertor.convertToString(value, convertorLocale, null); 121 } 122 123 Object oldValue = jxpc.getValue(this.xpath); 124 if (getLogger().isDebugEnabled()) { 125 getLogger().debug("value= " + value + "-- oldvalue=" + oldValue); 126 } 127 128 boolean update = false; 129 130 if ((value == null && oldValue != null) || value != null && !value.equals(oldValue)) { 131 jxpc.createPathAndSetValue(this.xpath, value); 133 134 JXPathContext subContext = null; 136 try { 137 subContext = jxpc.getRelativeContext(jxpc.getPointer(this.xpath)); 138 } catch (JXPathException e) { 139 if (getLogger().isDebugEnabled()) { 142 getLogger().debug("(Ignorable) problem binding field " + widget.getRequestParameterName(), e); 143 } 144 } 145 if (subContext != null) { 146 this.updateBinding.saveFormToModel(frmModel, subContext); 147 } 148 149 update = true; 150 } 151 152 if (getLogger().isDebugEnabled()) { 153 getLogger().debug("done saving " + toString() + " -- value= " + value + " -- on-update == " + update); 154 } 155 } 156 157 public String toString() { 158 return "ValueJXPathBinding [widget=" + this.fieldId + ", xpath=" + this.xpath + "]"; 159 } 160 161 public void enableLogging(Logger logger) { 162 super.enableLogging(logger); 163 this.updateBinding.enableLogging(logger); 164 } 165 166 public String getFieldId() { 167 return this.fieldId; 168 } 169 170 public String getXPath() { 171 return this.xpath; 172 } 173 174 public Convertor getConvertor() { 175 return this.convertor; 176 } 177 178 public Locale getConvertorLocale() { 179 return this.convertorLocale; 180 } 181 } 182 | Popular Tags |