home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.forms.samples.bindings;
-
- import org.apache.cocoon.forms.binding.AbstractCustomBinding;
- import org.apache.cocoon.forms.binding.Binding;
- import org.apache.cocoon.forms.binding.BindingException;
- import org.apache.cocoon.forms.formmodel.Widget;
- import org.apache.cocoon.forms.util.DomHelper;
- import org.apache.commons.jxpath.JXPathContext;
- import org.w3c.dom.Element;
-
- public class CustomValueWrapBinding extends AbstractCustomBinding {
- private static final char DEFAULT_DELIMITER = '*';
- private final String prefix;
- private final String suffix;
-
- public CustomValueWrapBinding() {
- this('*');
- }
-
- public CustomValueWrapBinding(char delimiter) {
- this(delimiter, delimiter);
- }
-
- public CustomValueWrapBinding(char prefix, char suffix) {
- this.prefix = "" + prefix + prefix;
- this.suffix = "" + suffix + suffix;
- }
-
- public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
- String appValue = (String)jxpc.getValue(".");
- String formValue = null;
- if (appValue.startsWith(this.prefix) && appValue.endsWith(this.suffix) && appValue.length() >= this.prefix.length() + this.suffix.length()) {
- formValue = appValue.substring(this.prefix.length(), appValue.length() - this.suffix.length());
- }
-
- frmModel.setValue(formValue);
- }
-
- public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
- Object formValue = frmModel.getValue();
- jxpc.setValue(".", "" + this.prefix + formValue + this.suffix);
- }
-
- public static Binding createBinding(Element config) throws BindingException {
- try {
- String pfx = DomHelper.getAttribute(config, "prefixchar", (String)null);
- String sfx = DomHelper.getAttribute(config, "suffixchar", (String)null);
- char prefixChar = pfx != null ? pfx.charAt(0) : 42;
- char suffixChar = sfx != null ? sfx.charAt(0) : 42;
- return new CustomValueWrapBinding(prefixChar, suffixChar);
- } catch (Exception e) {
- throw new BindingException("Could not create instance of CustomValueWrapBinding.", e);
- }
- }
- }
-