KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > base > JRBaseExpression


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * 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,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.base;
29
30 import java.io.Serializable JavaDoc;
31
32 import net.sf.jasperreports.engine.JRConstants;
33 import net.sf.jasperreports.engine.JRExpression;
34 import net.sf.jasperreports.engine.JRExpressionChunk;
35 import net.sf.jasperreports.engine.JRRuntimeException;
36 import net.sf.jasperreports.engine.util.JRClassLoader;
37
38
39 /**
40  * @author Teodor Danciu (teodord@users.sourceforge.net)
41  * @version $Id: JRBaseExpression.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
42  */

43 public class JRBaseExpression implements JRExpression, Serializable JavaDoc
44 {
45
46
47     /**
48      *
49      */

50     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
51
52     /**
53      *
54      */

55     protected String JavaDoc valueClassName = null;
56     protected int id = 0;
57
58     protected transient Class JavaDoc valueClass = null;
59
60     /**
61      *
62      */

63     private JRExpressionChunk[] chunks = null;
64
65     /**
66      *
67      */

68     private static int lastId = 0;
69
70
71     /**
72      *
73      */

74     protected JRBaseExpression()
75     {
76     }
77     
78     
79     /**
80      * Creates a copy of an expression.
81      *
82      * @param expression the original expression
83      * @param factory the base object factory
84      * @param expressionId if not null, the created expression will use it as ID
85      * instead of the original expressions's ID
86      */

87     protected JRBaseExpression(JRExpression expression, JRBaseObjectFactory factory, Integer JavaDoc expressionId)
88     {
89         factory.put(expression, this);
90         
91         valueClassName = expression.getValueClassName();
92         if (expressionId == null)
93         {
94             id = expression.getId();
95         }
96         else
97         {
98             id = expressionId.intValue();
99         }
100         
101         /* */
102         JRExpressionChunk[] jrChunks = expression.getChunks();
103         if (jrChunks != null && jrChunks.length > 0)
104         {
105             chunks = new JRExpressionChunk[jrChunks.length];
106             for(int i = 0; i < chunks.length; i++)
107             {
108                 chunks[i] = factory.getExpressionChunk(jrChunks[i]);
109             }
110         }
111     }
112
113     
114     /**
115      * Creates a copy of an expression.
116      *
117      * @param expression the original expression
118      * @param factory the base object factory
119      */

120     protected JRBaseExpression(JRExpression expression, JRBaseObjectFactory factory)
121     {
122         this(expression, factory, null);
123     }
124         
125
126     /**
127      *
128      */

129     private static synchronized int getNextId()
130     {
131         return lastId++;
132     }
133
134
135     /**
136      *
137      */

138     public void regenerateId()
139     {
140         id = getNextId();
141     }
142
143
144     /**
145      *
146      */

147     public Class JavaDoc getValueClass()
148     {
149         if (valueClass == null)
150         {
151             if (valueClassName != null)
152             {
153                 try
154                 {
155                     valueClass = JRClassLoader.loadClassForName(valueClassName);
156                 }
157                 catch(ClassNotFoundException JavaDoc e)
158                 {
159                     throw new JRRuntimeException(e);
160                 }
161             }
162         }
163         
164         return valueClass;
165     }
166     
167     /**
168      *
169      */

170     public String JavaDoc getValueClassName()
171     {
172         return valueClassName;
173     }
174     
175     /**
176      *
177      */

178     public int getId()
179     {
180         return id;
181     }
182
183     /**
184      *
185      */

186     public JRExpressionChunk[] getChunks()
187     {
188         return chunks;
189     }
190             
191     /**
192      *
193      */

194     public String JavaDoc getText()
195     {
196         String JavaDoc text = "";
197         
198         chunks = getChunks();
199         if (chunks != null && chunks.length > 0)
200         {
201             StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
202
203             for(int i = 0; i < chunks.length; i++)
204             {
205                 switch(chunks[i].getType())
206                 {
207                     case JRExpressionChunk.TYPE_PARAMETER :
208                     {
209                         sbuffer.append("$P{");
210                         sbuffer.append( chunks[i].getText() );
211                         sbuffer.append("}");
212                         break;
213                     }
214                     case JRExpressionChunk.TYPE_FIELD :
215                     {
216                         sbuffer.append("$F{");
217                         sbuffer.append( chunks[i].getText() );
218                         sbuffer.append("}");
219                         break;
220                     }
221                     case JRExpressionChunk.TYPE_VARIABLE :
222                     {
223                         sbuffer.append("$V{");
224                         sbuffer.append( chunks[i].getText() );
225                         sbuffer.append("}");
226                         break;
227                     }
228                     case JRExpressionChunk.TYPE_RESOURCE :
229                     {
230                         sbuffer.append("$R{");
231                         sbuffer.append( chunks[i].getText() );
232                         sbuffer.append("}");
233                         break;
234                     }
235                     case JRExpressionChunk.TYPE_TEXT :
236                     default :
237                     {
238                         sbuffer.append( chunks[i].getText() );
239                         break;
240                     }
241                 }
242             }
243
244             text = sbuffer.toString();
245         }
246         
247         return text;
248     }
249
250
251 }
252
Popular Tags