001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * ScriptletCode.java
028: *
029: * Created on 11 marzo 2004, 18.03
030: *
031: */
032:
033: package it.businesslogic.ireport;
034:
035: import it.businesslogic.ireport.util.*;
036: import java.util.*;
037: import java.io.*;
038:
039: /**
040: *
041: * @author Administrator
042: *
043: */
044: public class ScriptletCode {
045:
046: public static final int GLOBAL_DECLARATIONS = 0;
047: public static final int EVENT_AFTER_COLUMN_INIT = 1;
048: public static final int EVENT_AFTER_DETAIL_EVAL = 2;
049: public static final int EVENT_AFTER_GROUP_INIT = 3;
050: public static final int EVENT_AFTER_PAGE_INIT = 4;
051: public static final int EVENT_AFTER_REPORT_INIT = 5;
052: public static final int EVENT_BEFORE_COLUMN_INIT = 6;
053: public static final int EVENT_BEFORE_DETAIL_EVAL = 7;
054: public static final int EVENT_BEFORE_GROUP_INIT = 8;
055: public static final int EVENT_BEFORE_PAGE_INIT = 9;
056: public static final int EVENT_BEFORE_REPORT_INIT = 10;
057:
058: public static final int LAST_PORTION = 10;
059: public String[] portion_keywords = null;
060:
061: /** all code protions */
062: protected HashMap code_portions;
063:
064: /** Creates a new instance of ScriptletCode */
065: public ScriptletCode() {
066:
067: code_portions = new HashMap();
068: portion_keywords = new String[LAST_PORTION + 1];
069:
070: portion_keywords[GLOBAL_DECLARATIONS] = "GLOBAL_DECLARATIONS";
071: portion_keywords[EVENT_AFTER_COLUMN_INIT] = "EVENT_AFTER_COLUMN_INIT";
072: portion_keywords[EVENT_AFTER_DETAIL_EVAL] = "EVENT_AFTER_DETAIL_EVAL";
073: portion_keywords[EVENT_AFTER_GROUP_INIT] = "EVENT_AFTER_GROUP_INIT";
074: portion_keywords[EVENT_AFTER_PAGE_INIT] = "EVENT_AFTER_PAGE_INIT";
075: portion_keywords[EVENT_AFTER_REPORT_INIT] = "EVENT_AFTER_REPORT_INIT";
076: portion_keywords[EVENT_BEFORE_COLUMN_INIT] = "EVENT_BEFORE_COLUMN_INIT";
077: portion_keywords[EVENT_BEFORE_DETAIL_EVAL] = "EVENT_BEFORE_DETAIL_EVAL";
078: portion_keywords[EVENT_BEFORE_GROUP_INIT] = "EVENT_BEFORE_GROUP_INIT";
079: portion_keywords[EVENT_BEFORE_PAGE_INIT] = "EVENT_BEFORE_PAGE_INIT";
080: portion_keywords[EVENT_BEFORE_REPORT_INIT] = "EVENT_BEFORE_REPORT_INIT";
081:
082: }
083:
084: /**
085: * Return the requested portion of class
086: */
087: public String getPortion(int portion) {
088:
089: if (code_portions.get("" + portion) == null) {
090: return "";
091: }
092: return (String) code_portions.get("" + portion);
093: }
094:
095: /**
096: * Return the requested portion of class
097: */
098: public void setPortionCode(int portion, String code) {
099:
100: code_portions.put("" + portion, code);
101: }
102:
103: /**
104: *
105: */
106: public StringBuffer getAll() {
107: StringBuffer s = new StringBuffer();
108:
109: String global_portion = getPortion(GLOBAL_DECLARATIONS).trim();
110:
111: int i = global_portion.lastIndexOf("}");
112: global_portion = global_portion.substring(0, i)
113: + global_portion.substring(i + 1);
114:
115: s.append(global_portion);
116: s.append("\n");
117: for (int k = 1; k < LAST_PORTION + 1; ++k) {
118: s
119: .append("/** Begin "
120: + portion_keywords[k]
121: + " This line is generated by iReport. Don't modify or move please! */\n");
122: s.append(getPortion(k));
123: s
124: .append("/** End "
125: + portion_keywords[k]
126: + " This line is generated by iReport. Don't modify or move please! */\n");
127: }
128:
129: s.append("\n}");
130:
131: return s;
132: }
133:
134: /** Load the scriptlet from a file... */
135: public ScriptletCode(String filename) throws FileNotFoundException,
136: IOException {
137: this (new FileReader(filename));
138: }
139:
140: /** Load the scriptlet from a file... */
141: public ScriptletCode(java.io.InputStream is) throws IOException {
142: this (new InputStreamReader(is));
143: }
144:
145: public ScriptletCode(Reader in) throws IOException {
146: this ();
147: LineNumberReader lin = new LineNumberReader(in);
148: String line = "";
149:
150: int actualPortion = GLOBAL_DECLARATIONS;
151:
152: while ((line = lin.readLine()) != null) {
153: if (line.trim().startsWith(
154: "/** Begin EVENT_AFTER_COLUMN_INIT")) {
155: actualPortion = EVENT_AFTER_COLUMN_INIT;
156: continue;
157: } else if (line.trim().startsWith(
158: "/** Begin EVENT_AFTER_DETAIL_EVAL")) {
159: actualPortion = EVENT_AFTER_DETAIL_EVAL;
160: continue;
161: } else if (line.trim().startsWith(
162: "/** Begin EVENT_AFTER_GROUP_INIT")) {
163: actualPortion = EVENT_AFTER_GROUP_INIT;
164: continue;
165: } else if (line.trim().startsWith(
166: "/** Begin EVENT_AFTER_PAGE_INIT")) {
167: actualPortion = EVENT_AFTER_PAGE_INIT;
168: continue;
169: } else if (line.trim().startsWith(
170: "/** Begin EVENT_AFTER_REPORT_INIT")) {
171: actualPortion = EVENT_AFTER_REPORT_INIT;
172: continue;
173: } else if (line.trim().startsWith(
174: "/** Begin EVENT_BEFORE_COLUMN_INIT")) {
175: actualPortion = EVENT_BEFORE_COLUMN_INIT;
176: continue;
177: } else if (line.trim().startsWith(
178: "/** Begin EVENT_BEFORE_DETAIL_EVAL")) {
179: actualPortion = EVENT_BEFORE_DETAIL_EVAL;
180: continue;
181: } else if (line.trim().startsWith(
182: "/** Begin EVENT_BEFORE_GROUP_INIT")) {
183: actualPortion = EVENT_BEFORE_GROUP_INIT;
184: continue;
185: } else if (line.trim().startsWith(
186: "/** Begin EVENT_BEFORE_PAGE_INIT")) {
187: actualPortion = EVENT_BEFORE_PAGE_INIT;
188: continue;
189: } else if (line.trim().startsWith(
190: "/** Begin EVENT_BEFORE_REPORT_INIT")) {
191: actualPortion = EVENT_BEFORE_REPORT_INIT;
192: continue;
193: } else if (line.trim().startsWith("/** End EVENT_")) {
194: actualPortion = GLOBAL_DECLARATIONS;
195: continue;
196: }
197:
198: append(line, actualPortion);
199: }
200: }
201:
202: /** Append a line to the specified code portion */
203: public void append(String line, int portion) {
204: String str = Misc.nvl(this .code_portions.get("" + portion), "");
205: str += line + "\n";
206: this .code_portions.put("" + portion, str);
207:
208: }
209: }
|