1 16 package org.apache.cocoon.forms.samples.bindings; 17 18 import org.apache.cocoon.forms.binding.AbstractCustomBinding; 19 import org.apache.cocoon.forms.binding.Binding; 20 import org.apache.cocoon.forms.binding.BindingException; 21 import org.apache.cocoon.forms.formmodel.Widget; 22 import org.apache.cocoon.forms.util.DomHelper; 23 import org.apache.commons.jxpath.JXPathContext; 24 import org.w3c.dom.Element ; 25 26 29 public class CustomValueWrapBinding extends AbstractCustomBinding { 30 31 private static final char DEFAULT_DELIMITER = '*'; 32 private final String prefix; 33 private final String suffix; 34 35 public CustomValueWrapBinding() { 36 this(DEFAULT_DELIMITER); 37 } 38 39 public CustomValueWrapBinding(char delimiter) { 40 this(delimiter, delimiter); 41 } 42 43 public CustomValueWrapBinding(char prefix, char suffix) { 44 this.prefix = ""+ prefix + prefix; 45 this.suffix = "" + suffix + suffix; 46 } 47 48 54 public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException { 55 String appValue = (String )jxpc.getValue("."); 56 String formValue= null; 57 if (appValue.startsWith(this.prefix) 58 && appValue.endsWith(suffix) 59 && appValue.length() >= this.prefix.length() + this.suffix.length()) { 60 formValue = appValue.substring(this.prefix.length(), 61 appValue.length() - this.suffix.length()); 62 } 63 frmModel.setValue(formValue); 64 } 65 66 72 public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException { 73 Object formValue = frmModel.getValue(); 74 jxpc.setValue(".", "" + this.prefix + formValue + this.suffix); 75 } 76 77 78 85 public static Binding createBinding(Element config) throws BindingException{ 86 87 try { 88 String pfx = DomHelper.getAttribute(config, "prefixchar", null); 89 String sfx = DomHelper.getAttribute(config, "suffixchar", null); 90 91 final char prefixChar = (pfx!=null) ? pfx.charAt(0) : DEFAULT_DELIMITER; 92 final char suffixChar = (sfx!=null) ? sfx.charAt(0) : DEFAULT_DELIMITER; 93 94 return new CustomValueWrapBinding(prefixChar, suffixChar); 95 } catch (Exception e) { 96 throw new BindingException("Could not create instance of CustomValueWrapBinding." ,e); 97 } 98 } 99 } 100 | Popular Tags |