Source for org.jfree.report.expressions.formula.sys.AttrFunction

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: AttrFunction.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.expressions.formula.sys;
  33: 
  34: import org.jfree.formula.EvaluationException;
  35: import org.jfree.formula.FormulaContext;
  36: import org.jfree.formula.LibFormulaErrorValue;
  37: import org.jfree.formula.function.Function;
  38: import org.jfree.formula.function.ParameterCallback;
  39: import org.jfree.formula.lvalues.TypeValuePair;
  40: import org.jfree.formula.typing.coretypes.AnyType;
  41: import org.jfree.formula.typing.coretypes.ErrorType;
  42: import org.jfree.report.expressions.ReportFormulaContext;
  43: import org.jfree.report.structure.Element;
  44: 
  45: /**
  46:  * Creation-Date: 24.11.2006, 13:02:41
  47:  *
  48:  * @author Thomas Morgner
  49:  */
  50: public class AttrFunction implements Function
  51: {
  52:   public AttrFunction()
  53:   {
  54:   }
  55: 
  56:   public String getCanonicalName()
  57:   {
  58:     return "ATTR";
  59:   }
  60: 
  61:   public TypeValuePair evaluate(final FormulaContext context,
  62:                                 final ParameterCallback parameters)
  63:       throws EvaluationException
  64:   {
  65:     // we expect strings and will check, whether the reference for theses
  66:     // strings is dirty.
  67:     if (context instanceof ReportFormulaContext == false)
  68:     {
  69:       return new TypeValuePair(ErrorType.TYPE, new LibFormulaErrorValue
  70:           (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
  71:     }
  72: 
  73:     final ReportFormulaContext reportFormulaContext =
  74:         (ReportFormulaContext) context;
  75:     final Object declaringElement = reportFormulaContext.getDeclaringElement();
  76:     if (declaringElement instanceof Element == false)
  77:     {
  78:       return new TypeValuePair(ErrorType.TYPE, new LibFormulaErrorValue
  79:           (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
  80:     }
  81: 
  82:     final Element element = (Element) declaringElement;
  83: 
  84:     if (parameters.getParameterCount() == 1)
  85:     {
  86:       final String value = (String) parameters.getValue(0);
  87:       return new TypeValuePair(AnyType.TYPE, element.getAttribute(value));
  88:     }
  89:     else if (parameters.getParameterCount() == 2)
  90:     {
  91:       final String namespace = (String) parameters.getValue(0);
  92:       final String attrName = (String) parameters.getValue(1);
  93:       return new TypeValuePair(AnyType.TYPE,
  94:           element.getAttribute(namespace, attrName));
  95:     }
  96:     return new TypeValuePair(ErrorType.TYPE, new LibFormulaErrorValue
  97:         (LibFormulaErrorValue.ERROR_INVALID_ARGUMENT));
  98:   }
  99: 
 100: }