KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > design > JRDesignExpression


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
29 /*
30  * Contributors:
31  * Ryan Johnson - delscovich@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.design;
34
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38
39 import net.sf.jasperreports.engine.JRConstants;
40 import net.sf.jasperreports.engine.JRExpressionChunk;
41 import net.sf.jasperreports.engine.base.JRBaseExpression;
42
43
44 /**
45  * @author Teodor Danciu (teodord@users.sourceforge.net)
46  * @version $Id: JRDesignExpression.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
47  */

48 public class JRDesignExpression extends JRBaseExpression
49 {
50     /**
51      *
52      */

53     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
54
55     /**
56      *
57      */

58     protected List JavaDoc chunks = new ArrayList JavaDoc();
59
60
61     /**
62      *
63      */

64     public JRDesignExpression()
65     {
66         super();
67
68         regenerateId();
69     }
70
71
72     /**
73      *
74      */

75     public void setValueClass(Class JavaDoc clazz)
76     {
77         setValueClassName(clazz.getName());
78     }
79
80     /**
81      *
82      */

83     public void setValueClassName(String JavaDoc className)
84     {
85         valueClassName = className;
86         valueClass = null;
87     }
88
89     /**
90      * FIXMENOW remove me?
91      */

92     public void setId(int id)
93     {
94         this.id = id;
95     }
96
97     /**
98      *
99      */

100     public JRExpressionChunk[] getChunks()
101     {
102         JRExpressionChunk[] chunkArray = null;
103         
104         if (chunks != null && chunks.size() > 0)
105         {
106             chunkArray = new JRExpressionChunk[chunks.size()];
107             chunks.toArray(chunkArray);
108         }
109         
110         return chunkArray;
111     }
112         
113     /**
114      * Clears the current list of chunks and adds the passed list of chunks. The reference
115      * to the list passed is not kept.
116      */

117     public void setChunks(List JavaDoc chunks)
118     {
119         this.chunks.clear();
120         this.chunks.addAll(chunks);
121     }
122
123     /**
124      *
125      */

126     public void addChunk(JRDesignExpressionChunk chunk)
127     {
128         this.chunks.add(chunk);
129     }
130         
131     /**
132      *
133      */

134     public void addTextChunk(String JavaDoc text)
135     {
136         JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
137         chunk.setType(JRExpressionChunk.TYPE_TEXT);
138         chunk.setText(text);
139         
140         this.chunks.add(chunk);
141     }
142         
143     /**
144      *
145      */

146     public void addParameterChunk(String JavaDoc text)
147     {
148         JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
149         chunk.setType(JRExpressionChunk.TYPE_PARAMETER);
150         chunk.setText(text);
151         
152         this.chunks.add(chunk);
153     }
154         
155     /**
156      *
157      */

158     public void addFieldChunk(String JavaDoc text)
159     {
160         JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
161         chunk.setType(JRExpressionChunk.TYPE_FIELD);
162         chunk.setText(text);
163         
164         this.chunks.add(chunk);
165     }
166         
167     /**
168      *
169      */

170     public void addVariableChunk(String JavaDoc text)
171     {
172         JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
173         chunk.setType(JRExpressionChunk.TYPE_VARIABLE);
174         chunk.setText(text);
175         
176         this.chunks.add(chunk);
177     }
178
179     /**
180      *
181      */

182     public void addResourceChunk(String JavaDoc text)
183     {
184         JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
185         chunk.setType(JRExpressionChunk.TYPE_RESOURCE);
186         chunk.setText(text);
187         
188         this.chunks.add(chunk);
189     }
190
191     /**
192      *
193      */

194     public void setText(String JavaDoc text)
195     {
196         chunks.clear();
197         
198         if (text != null)
199         {
200             int end = 0;
201             StringBuffer JavaDoc textChunk = new StringBuffer JavaDoc();
202             
203             StringTokenizer JavaDoc tkzer = new StringTokenizer JavaDoc(text, "$", true);
204             String JavaDoc token = null;
205             boolean wasDelim = false;
206             while (tkzer.hasMoreTokens())
207             {
208                 token = tkzer.nextToken();
209     
210                 if (token.equals("$"))
211                 {
212                     if (wasDelim)
213                     {
214                         textChunk.append("$");
215                     }
216     
217                     wasDelim = true;
218                 }
219                 else
220                 {
221                     if ( token.startsWith("P{") && wasDelim )
222                     {
223                         end = token.indexOf('}');
224                         if (end > 0)
225                         {
226                             if (textChunk.length() > 0)
227                             {
228                                 addTextChunk(textChunk.toString());
229                             }
230                             addParameterChunk(token.substring(2, end));
231                             textChunk = new StringBuffer JavaDoc(token.substring(end + 1));
232                         }
233                         else
234                         {
235                             if (wasDelim)
236                             {
237                                 textChunk.append("$");
238                             }
239                             textChunk.append(token);
240                         }
241                     }
242                     else if ( token.startsWith("F{") && wasDelim )
243                     {
244                         end = token.indexOf('}');
245                         if (end > 0)
246                         {
247                             if (textChunk.length() > 0)
248                             {
249                                 addTextChunk(textChunk.toString());
250                             }
251                             addFieldChunk(token.substring(2, end));
252                             textChunk = new StringBuffer JavaDoc(token.substring(end + 1));
253                         }
254                         else
255                         {
256                             if (wasDelim)
257                             {
258                                 textChunk.append("$");
259                             }
260                             textChunk.append(token);
261                         }
262                     }
263                     else if ( token.startsWith("V{") && wasDelim )
264                     {
265                         end = token.indexOf('}');
266                         if (end > 0)
267                         {
268                             if (textChunk.length() > 0)
269                             {
270                                 addTextChunk(textChunk.toString());
271                             }
272                             addVariableChunk(token.substring(2, end));
273                             textChunk = new StringBuffer JavaDoc(token.substring(end + 1));
274                         }
275                         else
276                         {
277                             if (wasDelim)
278                             {
279                                 textChunk.append("$");
280                             }
281                             textChunk.append(token);
282                         }
283                     }
284                     else if ( token.startsWith("R{") && wasDelim )
285                     {
286                         end = token.indexOf('}');
287                         if (end > 0)
288                         {
289                             if (textChunk.length() > 0)
290                             {
291                                 addTextChunk(textChunk.toString());
292                             }
293                             addResourceChunk(token.substring(2, end));
294                             textChunk = new StringBuffer JavaDoc(token.substring(end + 1));
295                         }
296                         else
297                         {
298                             if (wasDelim)
299                             {
300                                 textChunk.append("$");
301                             }
302                             textChunk.append(token);
303                         }
304                     }
305                     else
306                     {
307                         if (wasDelim)
308                         {
309                             textChunk.append("$");
310                         }
311                         textChunk.append(token);
312                     }
313     
314                     wasDelim = false;
315                 }
316             }
317             if (wasDelim)
318             {
319                 textChunk.append("$");
320             }
321             if (textChunk.length() > 0)
322             {
323                 this.addTextChunk(textChunk.toString());
324             }
325         }
326     }
327
328 }
329
Popular Tags