KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > ScriptletCode


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * ScriptletCode.java
28  *
29  * Created on 11 marzo 2004, 18.03
30  *
31  */

32
33 package it.businesslogic.ireport;
34
35 import it.businesslogic.ireport.util.*;
36 import java.util.*;
37 import java.io.*;
38 /**
39  *
40  * @author Administrator
41  *
42  */

43 public class ScriptletCode {
44    
45     public static final int GLOBAL_DECLARATIONS = 0;
46     public static final int EVENT_AFTER_COLUMN_INIT = 1;
47     public static final int EVENT_AFTER_DETAIL_EVAL = 2;
48     public static final int EVENT_AFTER_GROUP_INIT = 3;
49     public static final int EVENT_AFTER_PAGE_INIT = 4;
50     public static final int EVENT_AFTER_REPORT_INIT = 5;
51     public static final int EVENT_BEFORE_COLUMN_INIT = 6;
52     public static final int EVENT_BEFORE_DETAIL_EVAL = 7;
53     public static final int EVENT_BEFORE_GROUP_INIT = 8;
54     public static final int EVENT_BEFORE_PAGE_INIT = 9;
55     public static final int EVENT_BEFORE_REPORT_INIT = 10;
56     
57     public static final int LAST_PORTION = 10;
58     public String JavaDoc[] portion_keywords = null;
59     
60     /** all code protions */
61     protected HashMap code_portions;
62     
63     /** Creates a new instance of ScriptletCode */
64     public ScriptletCode() {
65  
66         code_portions = new HashMap();
67         portion_keywords = new String JavaDoc[LAST_PORTION+1];
68         
69         portion_keywords[GLOBAL_DECLARATIONS] = "GLOBAL_DECLARATIONS";
70         portion_keywords[EVENT_AFTER_COLUMN_INIT] = "EVENT_AFTER_COLUMN_INIT";
71         portion_keywords[EVENT_AFTER_DETAIL_EVAL] = "EVENT_AFTER_DETAIL_EVAL";
72         portion_keywords[EVENT_AFTER_GROUP_INIT] = "EVENT_AFTER_GROUP_INIT";
73         portion_keywords[EVENT_AFTER_PAGE_INIT] = "EVENT_AFTER_PAGE_INIT";
74         portion_keywords[EVENT_AFTER_REPORT_INIT] = "EVENT_AFTER_REPORT_INIT";
75         portion_keywords[EVENT_BEFORE_COLUMN_INIT]= "EVENT_BEFORE_COLUMN_INIT";
76         portion_keywords[EVENT_BEFORE_DETAIL_EVAL]= "EVENT_BEFORE_DETAIL_EVAL";
77         portion_keywords[EVENT_BEFORE_GROUP_INIT] = "EVENT_BEFORE_GROUP_INIT";
78         portion_keywords[EVENT_BEFORE_PAGE_INIT] = "EVENT_BEFORE_PAGE_INIT";
79         portion_keywords[EVENT_BEFORE_REPORT_INIT]= "EVENT_BEFORE_REPORT_INIT";
80              
81     }
82     
83     /**
84      * Return the requested portion of class
85      */

86     public String JavaDoc getPortion(int portion)
87     {
88
89         if (code_portions.get(""+portion) == null)
90         {
91             return "";
92         }
93         return (String JavaDoc)code_portions.get(""+portion);
94     }
95     
96     /**
97      * Return the requested portion of class
98      */

99     public void setPortionCode(int portion, String JavaDoc code)
100     {
101
102         code_portions.put(""+portion, code);
103     }
104     
105     /**
106      *
107      */

108     public StringBuffer JavaDoc getAll()
109     {
110         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
111         
112         String JavaDoc global_portion = getPortion( GLOBAL_DECLARATIONS).trim();
113         
114         int i = global_portion.lastIndexOf("}");
115         global_portion = global_portion.substring(0,i) + global_portion.substring(i+1);
116                 
117         s.append( global_portion );
118         s.append("\n");
119         for ( int k=1; k< LAST_PORTION+1 ; ++k)
120         {
121             s.append( "/** Begin " + portion_keywords[k] + " This line is generated by iReport. Don't modify or move please! */\n" );
122             s.append( getPortion(k) );
123             s.append( "/** End " + portion_keywords[k] + " This line is generated by iReport. Don't modify or move please! */\n" );
124         }
125         
126         s.append("\n}");
127         
128         return s;
129     }
130     
131     
132     /** Load the scriptlet from a file... */
133     public ScriptletCode(String JavaDoc filename) throws FileNotFoundException, IOException
134     {
135         this( new FileReader(filename) );
136     }
137     
138     
139     /** Load the scriptlet from a file... */
140     public ScriptletCode(java.io.InputStream JavaDoc is) throws IOException
141     {
142         this( new InputStreamReader(is) );
143     }
144     
145     public ScriptletCode(Reader in) throws IOException
146     {
147         this();
148         LineNumberReader lin = new LineNumberReader(in);
149         String JavaDoc line = "";
150         
151         int actualPortion = GLOBAL_DECLARATIONS;
152         
153         while ( (line = lin.readLine()) != null)
154         {
155             if ( line.trim().startsWith("/** Begin EVENT_AFTER_COLUMN_INIT"))
156             {
157                 actualPortion = EVENT_AFTER_COLUMN_INIT;
158                 continue;
159             }
160             else if ( line.trim().startsWith("/** Begin EVENT_AFTER_DETAIL_EVAL"))
161             {
162                 actualPortion = EVENT_AFTER_DETAIL_EVAL;
163                 continue;
164             }
165             else if ( line.trim().startsWith("/** Begin EVENT_AFTER_GROUP_INIT"))
166             {
167                 actualPortion = EVENT_AFTER_GROUP_INIT;
168                 continue;
169             }
170             else if ( line.trim().startsWith("/** Begin EVENT_AFTER_PAGE_INIT"))
171             {
172                 actualPortion = EVENT_AFTER_PAGE_INIT;
173                 continue;
174             }
175             else if ( line.trim().startsWith("/** Begin EVENT_AFTER_REPORT_INIT"))
176             {
177                 actualPortion = EVENT_AFTER_REPORT_INIT;
178                 continue;
179             }
180             else if ( line.trim().startsWith("/** Begin EVENT_BEFORE_COLUMN_INIT"))
181             {
182                 actualPortion = EVENT_BEFORE_COLUMN_INIT;
183                 continue;
184             }
185             else if ( line.trim().startsWith("/** Begin EVENT_BEFORE_DETAIL_EVAL"))
186             {
187                 actualPortion = EVENT_BEFORE_DETAIL_EVAL;
188                 continue;
189             }
190             else if ( line.trim().startsWith("/** Begin EVENT_BEFORE_GROUP_INIT"))
191             {
192                 actualPortion = EVENT_BEFORE_GROUP_INIT;
193                 continue;
194             }
195             else if ( line.trim().startsWith("/** Begin EVENT_BEFORE_PAGE_INIT"))
196             {
197                 actualPortion = EVENT_BEFORE_PAGE_INIT;
198                 continue;
199             }
200             else if ( line.trim().startsWith("/** Begin EVENT_BEFORE_REPORT_INIT"))
201             {
202                 actualPortion = EVENT_BEFORE_REPORT_INIT;
203                 continue;
204             }
205             else if ( line.trim().startsWith("/** End EVENT_"))
206             {
207                 actualPortion = GLOBAL_DECLARATIONS;
208                 continue;
209             }
210             
211             append(line, actualPortion);
212         }
213     }
214     
215     /** Append a line to the specified code portion */
216     public void append(String JavaDoc line, int portion)
217     {
218         String JavaDoc str = Misc.nvl( this.code_portions.get(""+portion),"");
219         str += line + "\n";
220         this.code_portions.put(""+portion,str);
221         
222     }
223 }
224
Popular Tags