Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 29619 invoked from network); 1 Apr 2004 22:49:58 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 1 Apr 2004 22:49:58 -0000 Received: (qmail 27207 invoked by uid 500); 1 Apr 2004 22:49:44 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 27062 invoked by uid 500); 1 Apr 2004 22:49:43 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 27040 invoked by uid 500); 1 Apr 2004 22:49:43 -0000 Delivered-To: apmail-cocoon-2.1-cvs@apache.org Received: (qmail 27036 invoked from network); 1 Apr 2004 22:49:43 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 1 Apr 2004 22:49:43 -0000 Received: (qmail 29534 invoked by uid 1546); 1 Apr 2004 22:49:56 -0000 Date: 1 Apr 2004 22:49:56 -0000 Message-ID: <20040401224956.29533.qmail@minotaur.apache.org> From: mpo@apache.org To: cocoon-2.1-cvs@apache.org Subject: cvs commit: cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/binding CustomJXPathBinding.java CustomJXPathBindingBuilder.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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: * *

1. No additional configuration requirements: *


   * <fb:custom id="widget-id" path="xpath expression"
   *     class="your.package.CustomBindingX" />
   * 
* *

2. With custom configuration requirements: *


   * <fb:custom id="widget-id" path="xpath expression"
   *     builderclass="your.package.CustomBindingXBuilder" 
   *     factorymethod="makeBinding"
   * >
   *   <fb:config custom-atts="someValue">
   *     <!-- in here come the nested custom elements (recommended in own namespace) 
   *             that make up the custom config -->
   *   </fb:config>
   * </fb:context>
   * 
* * @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); } } }