cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject cvs commit: cocoon-2.1/src/blocks/forms/samples welcome.xml
Date Tue, 06 Apr 2004 08:52:52 GMT
mpo         2004/04/06 01:52:52

  Modified:    src/blocks/forms/samples/flow bindings.js
               src/blocks/forms/samples welcome.xml
  Added:       src/blocks/forms/samples/forms/binding 05custom-bind.xml
                        05custom-def.xml 05custom-data.xml
               src/blocks/forms/java/org/apache/cocoon/forms/samples/bindings
                        CustomValueWrapBinding.java
  Log:
  Adding a sample for the javascript and custom bindings.
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/forms/samples/forms/binding/05custom-bind.xml
  
  Index: 05custom-bind.xml
  ===================================================================
  <?xml version="1.0"?>
  <!--
    Copyright 1999-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <fb:context 
    xmlns:fb="http://apache.org/cocoon/forms/1.0#binding" 
    xmlns:fd="http://apache.org/cocoon/forms/1.0#definition" 
  	path="/" >
  
  <!-- 
     | This file shows a sample of using the javascript binding. 
     -->
    <fb:javascript id="jswrap" path="jswrap-value" >
      <fb:save-form>
        var formValue = widget.getValue();
        // you can call a function from anywhere in the flowscript
        var appValue = doSaveConversion(formValue, "-");
        jxpathPointer.setValue(appValue);   
      </fb:save-form>
      <fb:load-form>
        var appValue = jxpathPointer.getValue();
        // you can call a function from anywhere in the flowscript
        var formValue = doLoadConversion(appValue, "-");
        widget.setValue(formValue);
      </fb:load-form>
    </fb:javascript>
  
    <fb:custom id="custom" path="custom-value" 
        class="org.apache.cocoon.forms.samples.bindings.CustomValueWrapBinding"/>
    
    <fb:custom id="config" path="config-value" 
        builderclass="org.apache.cocoon.forms.samples.bindings.CustomValueWrapBinding"
        factorymethod="createBinding" >
        <fb:config prefixchar="[" suffixchar="]" />      
    </fb:custom>
  
  </fb:context>
  
  
  
  1.1                  cocoon-2.1/src/blocks/forms/samples/forms/binding/05custom-def.xml
  
  Index: 05custom-def.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!--
    Copyright 1999-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <fd:form
    xmlns:fd="http://apache.org/cocoon/forms/1.0#definition">
  
    <fd:widgets>
    
      <fd:field id="jswrap" >
        <fd:label>1st Value</fd:label>
        <fd:datatype base="string" />
      </fd:field>
  
      <fd:field id="custom" >
        <fd:label>2nd Value</fd:label>
        <fd:datatype base="string" />
      </fd:field>
      
      <fd:field id="config" >
        <fd:label>3rd Value</fd:label>
        <fd:datatype base="string" />
      </fd:field>
      
    </fd:widgets>
    
  </fd:form>
  
  
  
  1.1                  cocoon-2.1/src/blocks/forms/samples/forms/binding/05custom-data.xml
  
  Index: 05custom-data.xml
  ===================================================================
  <?xml version="1.0"?>
  <!--
    Copyright 1999-2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <root>     
    <jswrap-value>--something--</jswrap-value>
    <custom-value>**something more**</custom-value>
    <config-value>[[something else]]</config-value>
  </root>
  
  
  
  1.1                  cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/samples/bindings/CustomValueWrapBinding.java
  
  Index: CustomValueWrapBinding.java
  ===================================================================
  /*
   * Copyright 1999-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  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;
  
  /**
   * CustomValueWrapBinding
   */
  public class CustomValueWrapBinding extends AbstractCustomBinding {
  
      private static final char DEFAULT_DELIMITER = '*';
      private final String prefix;
      private final String suffix;
      
      public CustomValueWrapBinding() {
          this(DEFAULT_DELIMITER);
      }
  
      public CustomValueWrapBinding(char delimiter) {
          this(delimiter, delimiter);
      }
      
      public CustomValueWrapBinding(char prefix, char suffix) {
          this.prefix = ""+ prefix + prefix;
          this.suffix = "" + suffix + suffix;
      }
  
      /**
       * This unwraps the value from the model by removing the 2 prefix and suffix-chars 
       * before setting it onto the model
       * 
       * Method signature and semantics complies to {@see AbstractCustomBinding#doLoad(Widget,
JXPathContext)}
       */
      public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
          String appValue = (String)jxpc.getValue(".");
          String formValue= null;
          if (appValue.startsWith(this.prefix) 
                  && appValue.endsWith(suffix) 
                  && appValue.length() >= this.prefix.length() + this.suffix.length())
{
              formValue = appValue.substring(this.prefix.length(), 
                                             appValue.length() - this.suffix.length());
          }        
          frmModel.setValue(formValue);
      }
  
      /**
       * This wraps the value from the form between 2 prefix and suffix-chars 
       * before saving to the model 
       * 
       * Method signature and semantics complies to {@see AbstractCustomBinding#doSave(Widget,
JXPathContext)}
       */
      public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
          Object formValue = frmModel.getValue();       
          jxpc.setValue(".", "" + this.prefix + formValue + this.suffix);        
      }
      
      
      /** 
       * Builds the actual binding class based on its XML config.
       * 
       * @param config the {@see Element} holding the config for the binding to create.
       * @return the configured binding
       * @throws BindingException when the creation fails
       */
      public static Binding createBinding(Element config) throws BindingException{
          
          try {
              String pfx = DomHelper.getAttribute(config, "prefixchar", null);
              String sfx = DomHelper.getAttribute(config, "suffixchar", null);
              
              final char prefixChar = (pfx!=null) ? pfx.charAt(0) : DEFAULT_DELIMITER;
              final char suffixChar = (sfx!=null) ? sfx.charAt(0) : DEFAULT_DELIMITER;
              
              return new CustomValueWrapBinding(prefixChar, suffixChar);
          } catch (Exception e) {
              throw new BindingException("Could not create instance of CustomValueWrapBinding."
,e);
          }
      }
  }
  
  
  
  1.4       +41 -0     cocoon-2.1/src/blocks/forms/samples/flow/bindings.js
  
  Index: bindings.js
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/samples/flow/bindings.js,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- bindings.js	5 Apr 2004 09:38:51 -0000	1.3
  +++ bindings.js	6 Apr 2004 08:52:52 -0000	1.4
  @@ -199,3 +199,44 @@
       }
       return bean;
   }
  +
  +/**
  + * sample conversion methods used by the javascript binding in '05custom'
  + */
  +
  +function doLoadConversion(backendValue, delimiter){
  +  var presuffix = "" + delimiter + delimiter;
  +  var result = "";
  +  if (backendValue.startsWith(presuffix) 
  +      && backendValue.endsWith(presuffix)
  +      && backendValue.length() >= 4) {
  +    result = backendValue.substring(2,backendValue.length() - 2);
  +  }
  +  return result;
  +}
  +
  +function doSaveConversion(formValue, delimiter){
  +  var result = "" + delimiter + delimiter + formValue + delimiter + delimiter;
  +  return result;
  +}
  +
  +/**
  + * Creates the JS Bean for sample '05custom'
  + */ 
  +function createJSBeanFor05custom() {
  +    var bean = {"jswrap-value": "--wrapped value--",
  +                "custom-value": "**custom value**",
  +                "config-value": "[[config value]]"};
  +    return bean;
  +}
  +
  +/**
  + * Creates the Java Bean for sample '05custom'
  + */ 
  +function createJavaBeanFor05custom() {
  +    var bean = new Packages.java.util.HashMap();
  +    bean.put("jswrap-value", "--wrapped value--");
  +    bean.put("custom-value", "**custom value**");
  +    bean.put("config-value", "[[config value]]");
  +    return bean;
  +}
  
  
  
  1.5       +4 -4      cocoon-2.1/src/blocks/forms/samples/welcome.xml
  
  Index: welcome.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/samples/welcome.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- welcome.xml	3 Apr 2004 08:17:42 -0000	1.4
  +++ welcome.xml	6 Apr 2004 08:52:52 -0000	1.5
  @@ -52,13 +52,13 @@
     <note>
     	Step-by-step tutorial into individual binding features:
     </note>
  -  <sample name="1. Binding Simple Values" href="binding-01value.flow">fb:*/direction
and fb:value/*</sample>
  -  <sample name="2. Lenient Binding" href="binding-02lenient.flow">*/@lenient</sample>
  +  <sample name="1. Binding Simple Values" href="binding-01value.flow">fb:*/@direction
and fb:value/*</sample>
  +  <sample name="2. Lenient Binding" href="binding-02lenient.flow">fb:*/@lenient</sample>
     <!-- TODO: provide more tutorial samples like these:
     <sample name="3. Aggregate Binding" href="binding-03aggregate.flow">fb:aggregate</sample>
     <sample name="4. Repeater Binding" href="binding-04repeater.flow">fb:repeater</sample>
  -  <sample name="5. JavaScript Binding" href="binding-05js.flow">fb:javascript</sample>
     -->
  +  <sample name="5. Custom Bindings using Java or Javascript" href="binding-05custom.flow">fb:javascript
and fb:custom</sample>
    </group>
   
    <group name="Locales">
  
  
  

Mime
View raw message