1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.report.pentaho.parser.rpt;
24 
25 import com.sun.star.report.OfficeToken;
26 import com.sun.star.report.pentaho.OfficeNamespaces;
27 
28 import org.jfree.report.expressions.Expression;
29 import org.jfree.report.expressions.FormulaExpression;
30 import org.jfree.report.expressions.FormulaFunction;
31 
32 import org.pentaho.reporting.libraries.xmlns.parser.AbstractXmlReadHandler;
33 import org.pentaho.reporting.libraries.xmlns.parser.ParseException;
34 
35 import org.xml.sax.Attributes;
36 import org.xml.sax.SAXException;
37 
38 /**
39  * Parses a named expression. These expressions are encountered on reports and
40  * groups and compute global values. Expressions must have an unique name.
41  *
42  * @author Thomas Morgner
43  */
44 public class FunctionReadHandler extends AbstractXmlReadHandler
45 {
46 
47     private Expression expression;
48 
FunctionReadHandler()49     public FunctionReadHandler()
50     {
51     }
52 
53     /**
54      * Starts parsing.
55      *
56      * @param attrs the attributes.
57      * @throws org.xml.sax.SAXException if there is a parsing error.
58      */
startParsing(final Attributes attrs)59     protected void startParsing(final Attributes attrs)
60             throws SAXException
61     {
62         final String formula = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "formula");
63         if (formula == null)
64         {
65             throw new ParseException("Required attribute 'formula' is missing", getLocator());
66         }
67 
68         final String name = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "name");
69         if (name == null)
70         {
71             throw new ParseException("Required attribute 'name' is missing", getLocator());
72         }
73         final String initialFormula = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "initial-formula");
74         final String deepTraversing = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "deep-traversing");
75 
76         if (initialFormula != null)
77         {
78             final FormulaFunction function = new FormulaFunction();
79             function.setInitial(initialFormula);
80             function.setFormula(formula);
81             this.expression = function;
82         }
83         else
84         {
85             final FormulaExpression expression = new FormulaExpression();
86             expression.setFormula(formula);
87             this.expression = expression;
88         }
89 
90         expression.setName(name);
91         expression.setDeepTraversing(OfficeToken.TRUE.equals(deepTraversing));
92         final String preEvaluated = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "pre-evaluated");
93         expression.setPrecompute(OfficeToken.TRUE.equals(preEvaluated));
94     }
95 
96     /**
97      * Returns the object for this element or null, if this element does not
98      * create an object.
99      *
100      * @return the object.
101      */
getObject()102     public Object getObject()
103             throws SAXException
104     {
105         return getExpression();
106     }
107 
getExpression()108     public Expression getExpression()
109     {
110         return expression;
111     }
112 }
113