KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > samples > bindings > CustomValueWrapBinding


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
25
26 /**
27  * CustomValueWrapBinding
28  */

29 public class CustomValueWrapBinding extends AbstractCustomBinding {
30
31     private static final char DEFAULT_DELIMITER = '*';
32     private final String JavaDoc prefix;
33     private final String JavaDoc 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     /**
49      * This unwraps the value from the model by removing the 2 prefix and suffix-chars
50      * before setting it onto the model
51      *
52      * Method signature and semantics complies to {@link AbstractCustomBinding#doLoad(Widget, JXPathContext)}
53      */

54     public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
55         String JavaDoc appValue = (String JavaDoc)jxpc.getValue(".");
56         String JavaDoc 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     /**
67      * This wraps the value from the form between 2 prefix and suffix-chars
68      * before saving to the model
69      *
70      * Method signature and semantics complies to {@link AbstractCustomBinding#doSave(Widget, JXPathContext)}
71      */

72     public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
73         Object JavaDoc formValue = frmModel.getValue();
74         jxpc.setValue(".", "" + this.prefix + formValue + this.suffix);
75     }
76     
77     
78     /**
79      * Builds the actual binding class based on its XML config.
80      *
81      * @param config the {@link Element} holding the config for the binding to create.
82      * @return the configured binding
83      * @throws BindingException when the creation fails
84      */

85     public static Binding createBinding(Element JavaDoc config) throws BindingException{
86         
87         try {
88             String JavaDoc pfx = DomHelper.getAttribute(config, "prefixchar", null);
89             String JavaDoc 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 JavaDoc e) {
96             throw new BindingException("Could not create instance of CustomValueWrapBinding." ,e);
97         }
98     }
99 }
100
Popular Tags