home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / CustomValueWrapBinding.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  2.9 KB  |  56 lines

  1. package org.apache.cocoon.forms.samples.bindings;
  2.  
  3. import org.apache.cocoon.forms.binding.AbstractCustomBinding;
  4. import org.apache.cocoon.forms.binding.Binding;
  5. import org.apache.cocoon.forms.binding.BindingException;
  6. import org.apache.cocoon.forms.formmodel.Widget;
  7. import org.apache.cocoon.forms.util.DomHelper;
  8. import org.apache.commons.jxpath.JXPathContext;
  9. import org.w3c.dom.Element;
  10.  
  11. public class CustomValueWrapBinding extends AbstractCustomBinding {
  12.    private static final char DEFAULT_DELIMITER = '*';
  13.    private final String prefix;
  14.    private final String suffix;
  15.  
  16.    public CustomValueWrapBinding() {
  17.       this('*');
  18.    }
  19.  
  20.    public CustomValueWrapBinding(char delimiter) {
  21.       this(delimiter, delimiter);
  22.    }
  23.  
  24.    public CustomValueWrapBinding(char prefix, char suffix) {
  25.       this.prefix = "" + prefix + prefix;
  26.       this.suffix = "" + suffix + suffix;
  27.    }
  28.  
  29.    public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
  30.       String appValue = (String)jxpc.getValue(".");
  31.       String formValue = null;
  32.       if (appValue.startsWith(this.prefix) && appValue.endsWith(this.suffix) && appValue.length() >= this.prefix.length() + this.suffix.length()) {
  33.          formValue = appValue.substring(this.prefix.length(), appValue.length() - this.suffix.length());
  34.       }
  35.  
  36.       frmModel.setValue(formValue);
  37.    }
  38.  
  39.    public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
  40.       Object formValue = frmModel.getValue();
  41.       jxpc.setValue(".", "" + this.prefix + formValue + this.suffix);
  42.    }
  43.  
  44.    public static Binding createBinding(Element config) throws BindingException {
  45.       try {
  46.          String pfx = DomHelper.getAttribute(config, "prefixchar", (String)null);
  47.          String sfx = DomHelper.getAttribute(config, "suffixchar", (String)null);
  48.          char prefixChar = pfx != null ? pfx.charAt(0) : 42;
  49.          char suffixChar = sfx != null ? sfx.charAt(0) : 42;
  50.          return new CustomValueWrapBinding(prefixChar, suffixChar);
  51.       } catch (Exception e) {
  52.          throw new BindingException("Could not create instance of CustomValueWrapBinding.", e);
  53.       }
  54.    }
  55. }
  56.