001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: AttrFunction.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.expressions.formula.sys;
033    
034    import org.jfree.formula.EvaluationException;
035    import org.jfree.formula.FormulaContext;
036    import org.jfree.formula.LibFormulaErrorValue;
037    import org.jfree.formula.function.Function;
038    import org.jfree.formula.function.ParameterCallback;
039    import org.jfree.formula.lvalues.TypeValuePair;
040    import org.jfree.formula.typing.coretypes.AnyType;
041    import org.jfree.formula.typing.coretypes.ErrorType;
042    import org.jfree.report.expressions.ReportFormulaContext;
043    import org.jfree.report.structure.Element;
044    
045    /**
046     * Creation-Date: 24.11.2006, 13:02:41
047     *
048     * @author Thomas Morgner
049     */
050    public class AttrFunction implements Function
051    {
052      public AttrFunction()
053      {
054      }
055    
056      public String getCanonicalName()
057      {
058        return "ATTR";
059      }
060    
061      public TypeValuePair evaluate(final FormulaContext context,
062                                    final ParameterCallback parameters)
063          throws EvaluationException
064      {
065        // we expect strings and will check, whether the reference for theses
066        // strings is dirty.
067        if (context instanceof ReportFormulaContext == false)
068        {
069          return new TypeValuePair(ErrorType.TYPE, new LibFormulaErrorValue
070              (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
071        }
072    
073        final ReportFormulaContext reportFormulaContext =
074            (ReportFormulaContext) context;
075        final Object declaringElement = reportFormulaContext.getDeclaringElement();
076        if (declaringElement instanceof Element == false)
077        {
078          return new TypeValuePair(ErrorType.TYPE, new LibFormulaErrorValue
079              (LibFormulaErrorValue.ERROR_REFERENCE_NOT_RESOLVABLE));
080        }
081    
082        final Element element = (Element) declaringElement;
083    
084        if (parameters.getParameterCount() == 1)
085        {
086          final String value = (String) parameters.getValue(0);
087          return new TypeValuePair(AnyType.TYPE, element.getAttribute(value));
088        }
089        else if (parameters.getParameterCount() == 2)
090        {
091          final String namespace = (String) parameters.getValue(0);
092          final String attrName = (String) parameters.getValue(1);
093          return new TypeValuePair(AnyType.TYPE,
094              element.getAttribute(namespace, attrName));
095        }
096        return new TypeValuePair(ErrorType.TYPE, new LibFormulaErrorValue
097            (LibFormulaErrorValue.ERROR_INVALID_ARGUMENT));
098      }
099    
100    }