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: AbstractExpression.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    package org.jfree.report.expressions;
032    
033    import java.util.Locale;
034    
035    import org.jfree.report.DataRow;
036    import org.jfree.report.flow.ReportContext;
037    import org.jfree.report.flow.ReportStructureRoot;
038    import org.jfree.report.i18n.ResourceBundleFactory;
039    import org.jfree.report.structure.Element;
040    import org.jfree.util.Configuration;
041    
042    /**
043     * A baseclass for simple, non-positionally parametrized expressions.
044     *
045     * @author Thomas Morgner
046     */
047    public abstract class AbstractExpression implements Expression
048    {
049      private transient ExpressionRuntime runtime;
050      private String name;
051      private boolean deepTraversing;
052      private boolean precompute;
053      private boolean preserve;
054    
055      protected AbstractExpression()
056      {
057      }
058    
059      /**
060       * Returns the name of the expression. An expression without a name cannot be
061       * referenced from outside the element.
062       *
063       * @return the function name.
064       */
065      public String getName()
066      {
067        return name;
068      }
069    
070      /**
071       * Sets the name of the expression.
072       *
073       * @param name the name.
074       */
075      public void setName(final String name)
076      {
077        this.name = name;
078      }
079    
080      /**
081       * Clones the expression, expression should be reinitialized after the
082       * cloning. <P> Expression maintain no state, cloning is done at the beginning
083       * of the report processing to disconnect the used expression from any other
084       * object space.
085       *
086       * @return A clone of this expression.
087       * @throws CloneNotSupportedException this should never happen.
088       */
089      public Object clone() throws CloneNotSupportedException
090      {
091        return super.clone();
092      }
093    
094      /**
095       * Return a new instance of this expression. The copy is initialized and uses
096       * the same parameters as the original, but does not share any objects.
097       *
098       * @return a copy of this function.
099       */
100      public Expression getInstance()
101      {
102        try
103        {
104          final AbstractExpression abstractExpression = (AbstractExpression) clone();
105          abstractExpression.runtime = null;
106          return abstractExpression;
107        }
108        catch (CloneNotSupportedException cne)
109        {
110          return null;
111        }
112      }
113    
114      /**
115       * Defines the DataRow used in this expression. The dataRow is set when the
116       * report processing starts and can be used to access the values of functions,
117       * expressions and the reports datasource.
118       *
119       * @param runtime the runtime information for the expression
120       */
121      public void setRuntime(final ExpressionRuntime runtime)
122      {
123        this.runtime = runtime;
124      }
125    
126      public ExpressionRuntime getRuntime()
127      {
128        return runtime;
129      }
130    
131      /**
132       * Returns the current {@link DataRow}.
133       *
134       * @return the data row.
135       */
136      protected DataRow getDataRow()
137      {
138        if (runtime == null)
139        {
140          return null;
141        }
142        return runtime.getDataRow();
143      }
144    
145      protected ResourceBundleFactory getResourceBundleFactory()
146      {
147        if (runtime == null)
148        {
149          return null;
150        }
151        return runtime.getResourceBundleFactory();
152      }
153    
154      protected  Configuration getReportConfiguration()
155      {
156        if (runtime == null)
157        {
158          return null;
159        }
160        return runtime.getConfiguration();
161      }
162    
163      protected Locale getParentLocale ()
164      {
165        if (runtime == null)
166        {
167          return null;
168        }
169    
170        final Object declaringParent = runtime.getDeclaringParent();
171        if (declaringParent instanceof Element)
172        {
173          final Element declaringElement = (Element) declaringParent;
174          return declaringElement.getLocale();
175        }
176    
177        final ReportContext reportContext = runtime.getReportContext();
178        final ReportStructureRoot reportStructureRoot = reportContext.getReportStructureRoot();
179        return reportStructureRoot.getLocale();
180      }
181    
182      public boolean isPrecompute()
183      {
184        return precompute;
185      }
186    
187      public void setPrecompute(final boolean precompute)
188      {
189        this.precompute = precompute;
190      }
191    
192      public boolean isDeepTraversing()
193      {
194        return deepTraversing;
195      }
196    
197      public void setDeepTraversing(final boolean deepTraversing)
198      {
199        this.deepTraversing = deepTraversing;
200      }
201    
202      public boolean isPreserve()
203      {
204        return preserve;
205      }
206    
207      public void setPreserve(final boolean preserve)
208      {
209        this.preserve = preserve;
210      }
211    }