KickJava   Java API By Example, From Geeks To Geeks.

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


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.design;
29
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.beans.PropertyChangeSupport JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import net.sf.jasperreports.engine.JRConstants;
37 import net.sf.jasperreports.engine.JRQueryChunk;
38 import net.sf.jasperreports.engine.base.JRBaseQuery;
39
40
41 /**
42  * @author Teodor Danciu (teodord@users.sourceforge.net)
43  * @version $Id: JRDesignQuery.java 1244 2006-04-28 11:22:38 +0300 (Fri, 28 Apr 2006) lucianc $
44  */

45 public class JRDesignQuery extends JRBaseQuery
46 {
47     /** Property change support mechanism. */
48     private transient PropertyChangeSupport JavaDoc propSupport;
49
50
51     /**
52      *
53      */

54     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
55
56
57     public static final String JavaDoc PROPERTY_LANGUAGE = "language";
58
59     /**
60      *
61      */

62     protected List JavaDoc chunks = new ArrayList JavaDoc();
63
64
65     /**
66      *
67      */

68     public JRQueryChunk[] getChunks()
69     {
70         JRQueryChunk[] chunkArray = null;
71         
72         if (chunks != null && chunks.size() > 0)
73         {
74             chunkArray = new JRQueryChunk[chunks.size()];
75             chunks.toArray(chunkArray);
76         }
77         
78         return chunkArray;
79     }
80         
81     /**
82      *
83      */

84     public void setChunks(List JavaDoc chunks)
85     {
86         this.chunks = chunks;
87     }
88         
89     /**
90      *
91      */

92     public void addChunk(JRDesignQueryChunk chunk)
93     {
94         this.chunks.add(chunk);
95     }
96         
97     /**
98      *
99      */

100     public void addTextChunk(String JavaDoc text)
101     {
102         JRDesignQueryChunk chunk = new JRDesignQueryChunk();
103         chunk.setType(JRQueryChunk.TYPE_TEXT);
104         chunk.setText(text);
105
106         this.chunks.add(chunk);
107     }
108         
109     /**
110      *
111      */

112     public void addParameterChunk(String JavaDoc text)
113     {
114         JRDesignQueryChunk chunk = new JRDesignQueryChunk();
115         chunk.setType(JRQueryChunk.TYPE_PARAMETER);
116         chunk.setText(text);
117
118         this.chunks.add(chunk);
119     }
120         
121     /**
122      *
123      */

124     public void addParameterClauseChunk(String JavaDoc text)
125     {
126         JRDesignQueryChunk chunk = new JRDesignQueryChunk();
127         chunk.setType(JRQueryChunk.TYPE_PARAMETER_CLAUSE);
128         chunk.setText(text);
129
130         this.chunks.add(chunk);
131     }
132         
133
134     /**
135      *
136      */

137     public void setText(String JavaDoc text)
138     {
139         chunks = new ArrayList JavaDoc();
140         
141         if (text != null)
142         {
143             int end = 0;
144             StringBuffer JavaDoc textChunk = new StringBuffer JavaDoc();
145             String JavaDoc parameterChunk = null;
146             String JavaDoc parameterClauseChunk = null;
147             
148             StringTokenizer JavaDoc tkzer = new StringTokenizer JavaDoc(text, "$", true);
149             String JavaDoc token = null;
150             boolean wasDelim = false;
151             while (tkzer.hasMoreTokens())
152             {
153                 token = tkzer.nextToken();
154     
155                 if (token.equals("$"))
156                 {
157                     if (wasDelim)
158                     {
159                         textChunk.append("$");
160                     }
161     
162                     wasDelim = true;
163                 }
164                 else
165                 {
166                     if ( token.startsWith("P{") && wasDelim )
167                     {
168                         end = token.indexOf('}');
169                         if (end > 0)
170                         {
171                             if (textChunk.length() > 0)
172                             {
173                                 this.addTextChunk(textChunk.toString());
174                             }
175                             parameterChunk = token.substring(2, end);
176                             this.addParameterChunk(parameterChunk);
177                             textChunk = new StringBuffer JavaDoc(token.substring(end + 1));
178                         }
179                         else
180                         {
181                             if (wasDelim)
182                             {
183                                 textChunk.append("$");
184                             }
185                             textChunk.append(token);
186                         }
187                     }
188                     else if ( token.startsWith("P!{") && wasDelim )
189                     {
190                         end = token.indexOf('}');
191                         if (end > 0)
192                         {
193                             if (textChunk.length() > 0)
194                             {
195                                 this.addTextChunk(textChunk.toString());
196                             }
197                             parameterClauseChunk = token.substring(3, end);
198                             this.addParameterClauseChunk(parameterClauseChunk);
199                             textChunk = new StringBuffer JavaDoc(token.substring(end + 1));
200                         }
201                         else
202                         {
203                             if (wasDelim)
204                             {
205                                 textChunk.append("$");
206                             }
207                             textChunk.append(token);
208                         }
209                     }
210                     else
211                     {
212                         if (wasDelim)
213                         {
214                             textChunk.append("$");
215                         }
216                         textChunk.append(token);
217                     }
218     
219                     wasDelim = false;
220                 }
221             }
222             if (wasDelim)
223             {
224                 textChunk.append("$");
225             }
226             if (textChunk.length() > 0)
227             {
228                 this.addTextChunk(textChunk.toString());
229             }
230         }
231     }
232             
233
234     /**
235      * Sets the query language.
236      *
237      * @param language the query language
238      * @see net.sf.jasperreports.engine.JRQuery#getLanguage()
239      */

240     public void setLanguage(String JavaDoc language)
241     {
242         String JavaDoc oldValue = this.language;
243         this.language = language;
244         getPropertyChangeSupport().firePropertyChange(PROPERTY_LANGUAGE, oldValue, this.language);
245     }
246
247     
248     /**
249      * Get the property change support object for this class. Because the
250      * property change support object has to be transient, it may need to be
251      * created.
252      *
253      * @return the property change support object.
254      */

255     protected PropertyChangeSupport JavaDoc getPropertyChangeSupport()
256     {
257         if (propSupport == null)
258         {
259             propSupport = new PropertyChangeSupport JavaDoc(this);
260         }
261         return propSupport;
262     }
263
264     /**
265      * Add a property listener to listen to all properties of this class.
266      * @param l The property listener to add.
267      * @see #removePropertyChangeListener(PropertyChangeListener)
268      */

269     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l)
270     {
271         getPropertyChangeSupport().addPropertyChangeListener(l);
272     }
273
274     /**
275      * Add a property listener to receive property change events for only one specific
276      * property.
277      * @param propName The property to listen to.
278      * @param l The property listener to add.
279      * @see #removePropertyChangeListener(String, PropertyChangeListener)
280      */

281     public void addPropertyChangeListener(String JavaDoc propName, PropertyChangeListener JavaDoc l)
282     {
283         getPropertyChangeSupport().addPropertyChangeListener(propName, l);
284     }
285
286     /**
287      * Remove a property change listener registered for all properties.
288      *
289      * This will only remove listeners that were added through the
290      * {@link #addPropertyChangeListener(PropertyChangeListener) addPropertyChangeListener(PropertyChangeListener)}
291      * method.
292      *
293      * @param l The listener to remove.
294      */

295     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l)
296     {
297         getPropertyChangeSupport().removePropertyChangeListener(l);
298     }
299
300     /**
301      * Remove a property change listener registered for a specific property.
302      *
303      * @param propName The property to listen to.
304      * @param l The listener to remove.
305      */

306     public void removePropertyChangeListener(String JavaDoc propName, PropertyChangeListener JavaDoc l)
307     {
308         getPropertyChangeSupport().removePropertyChangeListener(propName, l);
309     }
310 }
311
Popular Tags