You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by br...@apache.org on 2003/07/23 17:22:07 UTC

cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding InsertBeanJXPathBinding.java InsertBeanJXPathBindingBuilder.java

bruno       2003/07/23 08:22:07

  Added:       src/blocks/woody/java/org/apache/cocoon/woody/binding
                        InsertBeanJXPathBinding.java
                        InsertBeanJXPathBindingBuilder.java
  Log:
  initial commit
  
  Revision  Changes    Path
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/InsertBeanJXPathBinding.java
  
  Index: InsertBeanJXPathBinding.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.binding;
  
  import java.lang.reflect.Method;
  
  import org.apache.cocoon.woody.formmodel.Widget;
  import org.apache.commons.jxpath.AbstractFactory;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.Pointer;
  
  /**
   * InsertBeanJXPathBinding provides an implementation of a {@link Binding} 
   * that inserts a new instance of the specified bean (classname) into the target
   * back-end model upon save. 
   * <p>
   * NOTES: <ol>
   * <li>This Binding does not perform any actions when loading.</li>
   * </ol>
   */
  public class InsertBeanJXPathBinding extends JXPathBindingBase {
  
      private final String className;
      private final String addMethodName;
  
      /**
       * Constructs InsertBeanJXPathBinding
       * @param className
       */
      public InsertBeanJXPathBinding(String className, String addMethod) {
          this.className = className;
          this.addMethodName = addMethod;
      }
  
      /**
       * Do-nothing implementation of the interface.
       */
      public void loadFormFromModel(Widget frmModel, JXPathContext jxpc) {
          // doesn't do a thing when loading.
      }
  
      /**
       * Registers a JXPath Factory on the JXPath Context.
       * <p>
       * The factory will insert a new instance of the specified bean (classname) 
       * inside this object into the target objectmodel.
       */
      public void saveFormToModel(Widget frmModel, JXPathContext jxpc) {
          jxpc.setFactory(new AbstractFactory() {
              public boolean createObject(
                  JXPathContext context,
                  Pointer pointer,
                  Object parent,
                  String name,
                  int index) {
  
                  try {
                      Object[] args = new Object[1];
                      Class[] argTypes = new Class[1];
                      
                      // instantiate the new object
                      argTypes[0] =
                          Class.forName(InsertBeanJXPathBinding.this.className);
                      args[0] = argTypes[0].newInstance();
                      // lookup the named method on the parent
                      
                      Method addMethod =
                          parent.getClass().getMethod(
                              InsertBeanJXPathBinding.this.addMethodName,
                              argTypes);
                      // invoke this method with this new beast.
                      
                      addMethod.invoke(parent, args);
                      
                      InsertBeanJXPathBinding.this.getLogger().debug(
                          "InsertBean jxpath factory executed for index."
                              + index);
                      return true;
                  } catch (Exception e) {
                      InsertBeanJXPathBinding.this.getLogger().warn("InsertBean jxpath factory failed.", e);
                      return false;
                  }
              }
          });
  
          getLogger().debug(
              "done registered factory for inserting node -- " + toString());
      }
  
      public String toString() {
          return "InsertBeanJXPathBinding [for class "
              + this.className
              + " to addMethod "
              + this.addMethodName
              + "]";
      }
  
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/binding/InsertBeanJXPathBindingBuilder.java
  
  Index: InsertBeanJXPathBindingBuilder.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.woody.binding;
  
  import org.apache.cocoon.woody.binding.JXPathBindingManager.Assistant;
  import org.apache.cocoon.woody.util.DomHelper;
  import org.w3c.dom.Element;
  
  /**
   * InsertBeanJXPathBindingBuilder provides a helper class for the Factory 
   * implemented in {@link JXPathBindingManager} that helps construct the 
   * actual {@link InsertBeanJXPathBinding} out of the configuration in the 
   * provided configElement which looks like:
   * <pre><code>
   * &lt;wb:insert-bean classname="..child-bean-class.." addmethod="..method-to-add.."/&gt; 
   * </code></pre>
   */
  public class InsertBeanJXPathBindingBuilder
      extends JXpathBindingBuilderBase {
  
      /**
       * Creates an instance of {@link InsertBeanJXPathBinding} configured 
       * with the nested template of the bindingElm. 
       */
      public JXPathBindingBase buildBinding(
          Element bindingElm,
          Assistant assistant) {
  
          try {
              String className =
                  DomHelper.getAttribute(bindingElm, "classname");
              String addMethod =
                  DomHelper.getAttribute(bindingElm, "addmethod");
  
              return new InsertBeanJXPathBinding(className, addMethod);
          } catch (Exception e) {
              getLogger().warn("Error building a insert-bean binding.", e);
              return null;
          }
      }
  
  }