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/java/org/apache/cocoon/forms/binding CustomJXPathBinding.java CustomJXPathBindingBuilder.java
Date Thu, 01 Apr 2004 22:49:56 GMT
mpo         2004/04/01 14:49:56

  Added:       src/blocks/forms/java/org/apache/cocoon/forms/binding
                        CustomJXPathBinding.java
                        CustomJXPathBindingBuilder.java
  Log:
  Initial check-in of this new binding.
  as in http://marc.theaimsgroup.com/?t=108056921200001&r=1&w=2
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/binding/CustomJXPathBinding.java
  
  Index: CustomJXPathBinding.java
  ===================================================================
  /*
   * File CustomJXPathBinding.java 
   * created by mpo
   * on Apr 1, 2004 | 4:17:26 PM
   * 
   * (c) 2004 - Outerthought BVBA
   */
  package org.apache.cocoon.forms.binding;
  
  import org.apache.cocoon.forms.binding.JXPathBindingBuilderBase.CommonAttributes;
  import org.apache.cocoon.forms.formmodel.Widget;
  import org.apache.commons.jxpath.JXPathContext;
  
  /**
   * CustomJXPathBinding
   */
  public class CustomJXPathBinding extends JXPathBindingBase {
      
      /** 
       * The id of the cforms widget
       */
      private final String widgetId;
      
      /**
       * The path into the objectModel to select
       */
      private final String xpath;
      
      /**
       * The actual custom provided binding
       */
      private final Binding wrappedBinding;
      
      /**
       * Constructs CustomJXPathBinding
       * @param commonAtts common configuration attributes {@link JXPathBindingBase.CommonAttributes}
       * @param widgetId id of the widget to bind to
       * @param xpath jxpath expression to narrow down the context to before calling the wrapped
Binding
       * @param wrappedBinding the actual custom written Binding implementation of {@link
Binding}
       */
      public CustomJXPathBinding(CommonAttributes commonAtts, String widgetId, String xpath,
Binding wrappedBinding) {
          super(commonAtts);
          this.widgetId = widgetId;
          this.xpath = xpath;
          this.wrappedBinding = wrappedBinding;
      }
      
      /**
       * Delegates the actual loading operation to the provided Custom Binding Class
       * after narrowing down on the selected widget (@id) and context (@path)
       * @param frmModel
       * @param jxpc
       * @throws BindingException
       */
      public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException {
          Widget selectedWidget = selectWidget(frmModel);
          Object contextValue = jxpc.getValue(this.xpath);
          
          this.wrappedBinding.loadFormFromModel(selectedWidget, contextValue);
      }    
  
      /**
       * Delegates the actual saving operation to the provided Custom Binding Class
       * after narrowing down on the selected widget (@id) and context (@path)
       * @param frmModel
       * @param jxpc
       * @throws BindingException
       */
      public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException {
          Widget selectedWidget = selectWidget(frmModel);
          Object contextValue = jxpc.getValue(this.xpath);
          
          this.wrappedBinding.saveFormToModel(selectedWidget, contextValue);
      }
      
      
      /**
       * Helper method which selects down the identified widget from the formModel.
       * If no 'widgetId' is set the formModel will just be returned.
       *  
       * @param frmModel
       * @return
       * @throws BindingException
       */
      private Widget selectWidget(Widget frmModel) throws BindingException {
          if (this.widgetId == null) return frmModel;
          
          Widget selectedWidget = frmModel.getWidget(this.widgetId);            
          if (selectedWidget == null) {
              throw new BindingException("The widget with the ID [" + this.widgetId
                      + "] referenced in the binding does not exist in the form definition.");
          }
          return selectedWidget;
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/binding/CustomJXPathBindingBuilder.java
  
  Index: CustomJXPathBindingBuilder.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.binding;
  
  import java.lang.reflect.Method;
  
  import org.apache.cocoon.forms.binding.JXPathBindingManager.Assistant;
  import org.apache.cocoon.forms.util.DomHelper;
  import org.w3c.dom.Element;
  
  /**
   * CustomJXPathBindingBuilder provides a helper class for the Factory
   * implemented in {@link JXPathBindingManager} that helps construct the
   * actual {@link CustomJXPathBinding} out of the configuration in the
   * provided configElement which looks like one of the following:
   * 
   * <p> 1. No additional configuration requirements:
   * <pre><code>
   * &lt;fb:custom id="<i>widget-id</i>" path="<i>xpath expression</i>"
   *     class="your.package.CustomBindingX" /&gt;
   * </code></pre>
   *
   * <p> 2. With custom configuration requirements:
   * <pre><code>
   * &lt;fb:custom id="<i>widget-id</i>" path="<i>xpath expression</i>"
   *     builderclass="your.package.CustomBindingXBuilder" 
   *     factorymethod="makeBinding"
   * &gt;
   *   &lt;fb:config custom-atts="someValue"&gt;
   *     &lt;!-- in here come the nested custom elements (recommended in own namespace)

   *             that make up the custom config --&gt;
   *   &lt;/fb:config&gt;
   * &lt;/fb:context&gt;
   * </code></pre>
   *
   * @version CVS $Id: CustomJXPathBindingBuilder.java,v 1.1 2004/04/01 22:49:56 mpo Exp $
   */
  public class CustomJXPathBindingBuilder extends JXPathBindingBuilderBase {
      private static final Class[] DOMELEMENT_METHODARGS;
      private static final Class[] EMPTY_METHODARGS;
      
      static {
          DOMELEMENT_METHODARGS = new Class[1];
          DOMELEMENT_METHODARGS[0] = Element.class;
          EMPTY_METHODARGS = null;
      }
  
      /**
       * Builds the custom Binding class and wraps it into a CustomJXPathBinding
       * 
       * @param bindingElm configuration element describing the binding to build  
       * @param assistant helper-class for building possible nested bindings 
       * @return the freshly built binding based on the configuration element
       * @throws BindingException
       */
      public JXPathBindingBase buildBinding(Element bindingElm, 
          Assistant assistant) throws BindingException {
  
          try {
              CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(bindingElm);
              String xpath = DomHelper.getAttribute(bindingElm, "path", ".");
              String widgetId = DomHelper.getAttribute(bindingElm, "id");
  
              Object bindingInstance;
              
              String className = DomHelper.getAttribute(bindingElm, "class", null);
              if(className != null) {
                  Class clazz = Class.forName(className);
                  bindingInstance = clazz.newInstance();
                  
              } else {
                  String builderClassName = DomHelper.getAttribute(bindingElm, "builderclass");
                  String factoryMethodName = DomHelper.getAttribute(bindingElm, "factorymethod");
               
                  Element configNode = DomHelper.getChildElement(bindingElm, BindingManager.NAMESPACE,
"config");
                  
                  Class builderClass = Class.forName(builderClassName);
                  Method factoryMethod = null; 
                  Object[] args;
                  if (configNode != null) {                    
                      factoryMethod = builderClass.getMethod(factoryMethodName, DOMELEMENT_METHODARGS);
                      args = new Object[1];
                      args[1] = configNode;
                  } else {
                      factoryMethod = builderClass.getMethod(factoryMethodName, EMPTY_METHODARGS);
                      args = null;
                  }
                    
                  // we pass null to indicate that the method should be static
                  bindingInstance = factoryMethod.invoke(null, args);
              }
  
              CustomJXPathBinding customBinding = new CustomJXPathBinding(commonAtts, widgetId,
xpath, (Binding)bindingInstance);
              return customBinding;
          } catch (BindingException e) {
              throw e;
          } catch (Exception e) {
              throw new BindingException("Error building custom binding defined at " + DomHelper.getLocation(bindingElm),
e);
          }
      }
  }
  
  
  

Mime
View raw message