1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.compilers;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31
32 import net.sf.jasperreports.engine.JRExpression;
33 import net.sf.jasperreports.engine.JRExpressionChunk;
34 import net.sf.jasperreports.engine.JRExpressionCollector;
35 import net.sf.jasperreports.engine.type.ExpressionTypeEnum;
36
37 /**
38  * @author Lucian Chirita (lucianc@users.sourceforge.net)
39  */

40 public class ReportExpressionsCompiler
41 {
42     
43     private static final ReportExpressionsCompiler INSTANCE = new ReportExpressionsCompiler();
44     
45     public static ReportExpressionsCompiler instance()
46     {
47         return INSTANCE;
48     }
49
50     private Map<String, DirectExpressionEvaluation> constantExpressionEvaluations;
51     
52     public ReportExpressionsCompiler()
53     {
54         constantExpressionEvaluations = constantEvaluations();
55     }
56     
57     protected Map<String, DirectExpressionEvaluation> constantEvaluations()
58     {
59         Map<String, DirectExpressionEvaluation> constantEvaluations = new HashMap<>();
60         //used by builtin variables
61         constantEvaluations.put("new java.lang.Integer(0)"new ConstantExpressionEvaluation(Integer.valueOf(0)));
62         constantEvaluations.put("new java.lang.Integer(1)"new ConstantExpressionEvaluation(Integer.valueOf(1)));
63         //general constants
64         constantEvaluations.put("false"new ConstantExpressionEvaluation(Boolean.FALSE));
65         constantEvaluations.put("true"new ConstantExpressionEvaluation(Boolean.TRUE));
66         constantEvaluations.put("Boolean.FALSE"new ConstantExpressionEvaluation(Boolean.FALSE));
67         constantEvaluations.put("Boolean.TRUE"new ConstantExpressionEvaluation(Boolean.TRUE));
68         constantEvaluations.put("\"\""new ConstantExpressionEvaluation(""));
69         return constantEvaluations;
70     }
71     
72     public ReportExpressionsCompilation getExpressionsCompilation(JRExpressionCollector expressionCollector)
73     {
74         List<JRExpression> sourceExpressions = new ArrayList<>();
75         
76         Map<Integer, DirectExpressionEvaluation> directEvaluations = new HashMap<>();
77         List<JRExpression> expressions = expressionCollector.getExpressions();
78         for (Iterator<JRExpression> it = expressions.iterator(); it.hasNext();)
79         {
80             JRExpression expression = it.next();
81             DirectExpressionEvaluation directEvaluation = directEvaluation(expression);
82             if (directEvaluation == null)
83             {
84                 sourceExpressions.add(expression);
85             }
86             else
87             {
88                 Integer expressionId = expressionCollector.getExpressionId(expression);
89                 directEvaluations.put(expressionId, directEvaluation);
90             }
91         }
92         return new ReportExpressionsCompilation(sourceExpressions,
93                 directEvaluations);
94     }
95
96     protected DirectExpressionEvaluation directEvaluation(JRExpression expression)
97     {
98         DirectExpressionEvaluation directEvaluation = null;
99         if (expression.getType() == ExpressionTypeEnum.SIMPLE_TEXT)
100         {
101             directEvaluation = new SimpleTextEvaluation(expression.getChunks());
102         }
103         else
104         {
105             JRExpressionChunk[] chunks = expression.getChunks();
106             if (chunks == null || chunks.length == 0)
107             {
108                 directEvaluation = ConstantExpressionEvaluation.nullEvaluation();
109             }
110             else if (chunks.length == 1)
111             {
112                 JRExpressionChunk chunk = chunks[0];
113                 switch (chunk.getType())
114                 {
115                 case JRExpressionChunk.TYPE_PARAMETER:
116                     directEvaluation = new ParameterEvaluation(chunk.getText());
117                     break;
118                 case JRExpressionChunk.TYPE_FIELD:
119                     directEvaluation = new FieldEvaluation(chunk.getText());
120                     break;
121                 case JRExpressionChunk.TYPE_VARIABLE:
122                     directEvaluation = new VariableEvaluation(chunk.getText());
123                     break;
124                 case JRExpressionChunk.TYPE_RESOURCE:
125                     directEvaluation = new ResourceEvaluation(chunk.getText());
126                     break;
127                 case JRExpressionChunk.TYPE_TEXT:
128                     directEvaluation = constantExpressionEvaluations.get(chunk.getText());
129                 default:
130                     break;
131                 }
132             }
133         }
134         return directEvaluation;
135     }
136
137 }
138