You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2004/01/03 17:03:11 UTC

cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/expression IsNullFunction.java NullFunction.java DefaultExpressionManager.java

vgritsenko    2004/01/03 08:03:11

  Modified:    src/blocks/woody/conf woody-expression.xconf
               src/blocks/woody/java/org/apache/cocoon/woody/expression
                        DefaultExpressionManager.java
  Added:       src/blocks/woody/java/org/apache/cocoon/woody/expression
                        IsNullFunction.java NullFunction.java
  Log:
  Implemented ability to add custom expression functions.
  Add bare necessities: Null, IsNull functions.
  
  Revision  Changes    Path
  1.2       +5 -1      cocoon-2.1/src/blocks/woody/conf/woody-expression.xconf
  
  Index: woody-expression.xconf
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/conf/woody-expression.xconf,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- woody-expression.xconf	23 Jul 2003 09:09:18 -0000	1.1
  +++ woody-expression.xconf	3 Jan 2004 16:03:11 -0000	1.2
  @@ -1,5 +1,9 @@
   <?xml version="1.0"?>
   
   <xconf xpath="/cocoon" unless="woody-expression">
  -  <woody-expression logger="woody.expression"/>
  +
  +  <woody-expression logger="woody.expression">
  +    <function name="Null" class="org.apache.cocoon.woody.expression.NullFunction"/>
  +    <function name="IsNull" class="org.apache.cocoon.woody.expression.IsNullFunction"/>
  +  </woody-expression>
   </xconf>
  
  
  
  1.3       +36 -10    cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/expression/DefaultExpressionManager.java
  
  Index: DefaultExpressionManager.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/expression/DefaultExpressionManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultExpressionManager.java	4 Jul 2003 17:55:16 -0000	1.2
  +++ DefaultExpressionManager.java	3 Jan 2004 16:03:11 -0000	1.3
  @@ -48,28 +48,54 @@
    Software Foundation, please see <http://www.apache.org/>.
   
   */
  +
   package org.apache.cocoon.woody.expression;
   
  +import org.apache.avalon.framework.component.Component;
  +import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.thread.ThreadSafe;
  +
  +import org.outerj.expression.DefaultFunctionFactory;
   import org.outerj.expression.Expression;
   import org.outerj.expression.ExpressionException;
  -import org.outerj.expression.ParseException;
   import org.outerj.expression.FormulaParser;
  -import org.apache.avalon.framework.component.Component;
  -import org.apache.avalon.framework.thread.ThreadSafe;
  +import org.outerj.expression.ParseException;
   
   /**
    * Implementation of the {@link ExpressionManager} role.
    *
  - * <p>In the future, this component should become configurable so that new, user-defined
  - * functions can be registered.
  + * Custom functions can be added using configuration elements:
  + * <pre>
  + *   &lt;function name="MyFunction" class="net.foo.MyFunction"/&gt;
  + * </pre>
  + * 
  + * @version CVS $Id$
    */
  -public class DefaultExpressionManager 
  -    implements ExpressionManager, Component, ThreadSafe {
  +public class DefaultExpressionManager
  +        implements ExpressionManager, Component, Configurable, ThreadSafe {
  +    
  +    private DefaultFunctionFactory factory;
  +    
  +    public void configure(Configuration config) throws ConfigurationException {
  +        factory = new DefaultFunctionFactory();
           
  -    public Expression parse(String expressionString) 
  -    throws ParseException, ExpressionException {
  +        Configuration[] functions = config.getChildren("function");
  +        for (int i = 0; i < functions.length; i++) {
  +            String name = functions[i].getAttribute("name");
  +            String clazz = functions[i].getAttribute("class");
  +            try {
  +                factory.registerFunction(name, Class.forName(clazz));
  +            } catch (ClassNotFoundException e) {
  +                throw new ConfigurationException("Can not find class " + clazz + " for function " + name + ": " + e);
  +            }
  +        }
  +    }
  +    
  +    public Expression parse(String expressionString) throws ParseException, ExpressionException {
           
  -        FormulaParser parser = new FormulaParser(new java.io.StringReader(expressionString)); //, functionFactory);
  +        FormulaParser parser = new FormulaParser(new java.io.StringReader(expressionString), factory);
           parser.sum();
   
           Expression expression = parser.getExpression();
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/expression/IsNullFunction.java
  
  Index: IsNullFunction.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.expression;
  
  import org.outerj.expression.AbstractExpression;
  import org.outerj.expression.Expression;
  import org.outerj.expression.ExpressionContext;
  import org.outerj.expression.ExpressionException;
  
  /**
   * Returns true if argument is null.
   *  
   * @author <a href="mailto:vadim.gritsenko@dc.gov">Vadim Gritsenko</a>
   * @version CVS $Id: IsNullFunction.java,v 1.1 2004/01/03 16:03:11 vgritsenko Exp $
   */
  public class IsNullFunction extends AbstractExpression {
  
      public Object evaluate(ExpressionContext context) throws ExpressionException {
          Object result = null;
          try {
              result = ((Expression)arguments.get(0)).evaluate(context);
          } catch (ExpressionException e) {
              // FIXME: Hack to handle null variables
              if (!e.getMessage().startsWith("Unknown variable")) {
                  throw e;
              }
          }
          return result == null? Boolean.TRUE: Boolean.FALSE;
      }
  
      public void check() throws ExpressionException {
          if (arguments.size() != 1) {
              throw new ExpressionException(getDescription() + " requires one argument.", getLine(), getColumn());
          }
      }
  
      public Class getResultType() {
          return Boolean.class;
      }
  
      public String getDescription() {
          return "IsNull function";
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/expression/NullFunction.java
  
  Index: NullFunction.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.expression;
  
  import org.outerj.expression.AbstractExpression;
  import org.outerj.expression.ExpressionContext;
  import org.outerj.expression.ExpressionException;
  
  /**
   * Returns null constant.
   *  
   * @author <a href="mailto:vadim.gritsenko@dc.gov">Vadim Gritsenko</a>
   * @version CVS $Id: NullFunction.java,v 1.1 2004/01/03 16:03:11 vgritsenko Exp $
   */
  public class NullFunction extends AbstractExpression {
  
      public Object evaluate(ExpressionContext context) throws ExpressionException {
          return null;
      }
  
      public Class getResultType() {
          return Object.class;
      }
  
      public String getDescription() {
          return "Null function";
      }
  }