001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: /*
030: * Contributors:
031: * Ryan Johnson - delscovich@users.sourceforge.net
032: */
033: package net.sf.jasperreports.engine.design;
034:
035: import java.util.ArrayList;
036: import java.util.List;
037: import java.util.StringTokenizer;
038:
039: import net.sf.jasperreports.engine.JRConstants;
040: import net.sf.jasperreports.engine.JRExpressionChunk;
041: import net.sf.jasperreports.engine.base.JRBaseExpression;
042:
043: /**
044: * @author Teodor Danciu (teodord@users.sourceforge.net)
045: * @version $Id: JRDesignExpression.java 1676 2007-03-28 14:51:03Z lucianc $
046: */
047: public class JRDesignExpression extends JRBaseExpression {
048: /**
049: *
050: */
051: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
052:
053: /**
054: *
055: */
056: protected List chunks = new ArrayList();
057:
058: /**
059: *
060: */
061: public JRDesignExpression() {
062: super ();
063:
064: regenerateId();
065: }
066:
067: /**
068: *
069: */
070: public void setValueClass(Class clazz) {
071: setValueClassName(clazz.getName());
072: }
073:
074: /**
075: *
076: */
077: public void setValueClassName(String className) {
078: valueClassName = className;
079: valueClass = null;
080: }
081:
082: /**
083: * FIXMENOW remove me?
084: */
085: public void setId(int id) {
086: this .id = id;
087: }
088:
089: /**
090: *
091: */
092: public JRExpressionChunk[] getChunks() {
093: JRExpressionChunk[] chunkArray = null;
094:
095: if (chunks != null && chunks.size() > 0) {
096: chunkArray = new JRExpressionChunk[chunks.size()];
097: chunks.toArray(chunkArray);
098: }
099:
100: return chunkArray;
101: }
102:
103: /**
104: * Clears the current list of chunks and adds the passed list of chunks. The reference
105: * to the list passed is not kept.
106: */
107: public void setChunks(List chunks) {
108: this .chunks.clear();
109: this .chunks.addAll(chunks);
110: }
111:
112: /**
113: *
114: */
115: public void addChunk(JRDesignExpressionChunk chunk) {
116: this .chunks.add(chunk);
117: }
118:
119: protected void addChunk(byte type, String text) {
120: JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
121: chunk.setType(type);
122: chunk.setText(text);
123:
124: this .chunks.add(chunk);
125: }
126:
127: /**
128: *
129: */
130: public void addTextChunk(String text) {
131: JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
132: chunk.setType(JRExpressionChunk.TYPE_TEXT);
133: chunk.setText(text);
134:
135: this .chunks.add(chunk);
136: }
137:
138: /**
139: *
140: */
141: public void addParameterChunk(String text) {
142: JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
143: chunk.setType(JRExpressionChunk.TYPE_PARAMETER);
144: chunk.setText(text);
145:
146: this .chunks.add(chunk);
147: }
148:
149: /**
150: *
151: */
152: public void addFieldChunk(String text) {
153: JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
154: chunk.setType(JRExpressionChunk.TYPE_FIELD);
155: chunk.setText(text);
156:
157: this .chunks.add(chunk);
158: }
159:
160: /**
161: *
162: */
163: public void addVariableChunk(String text) {
164: JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
165: chunk.setType(JRExpressionChunk.TYPE_VARIABLE);
166: chunk.setText(text);
167:
168: this .chunks.add(chunk);
169: }
170:
171: /**
172: *
173: */
174: public void addResourceChunk(String text) {
175: JRDesignExpressionChunk chunk = new JRDesignExpressionChunk();
176: chunk.setType(JRExpressionChunk.TYPE_RESOURCE);
177: chunk.setText(text);
178:
179: this .chunks.add(chunk);
180: }
181:
182: /**
183: *
184: */
185: public void setText(String text) {
186: chunks.clear();
187:
188: if (text != null) {
189: StringBuffer textChunk = new StringBuffer();
190:
191: StringTokenizer tkzer = new StringTokenizer(text, "$", true);
192: int behindDelims = 0;
193: while (tkzer.hasMoreTokens()) {
194: String token = tkzer.nextToken();
195:
196: if (token.equals("$")) {
197: if (behindDelims > 0) {
198: textChunk.append("$");
199: }
200:
201: ++behindDelims;
202: } else {
203: byte chunkType = JRExpressionChunk.TYPE_TEXT;
204: if (behindDelims > 0) {
205: if (token.startsWith("P{")) {
206: chunkType = JRExpressionChunk.TYPE_PARAMETER;
207: } else if (token.startsWith("F{")) {
208: chunkType = JRExpressionChunk.TYPE_FIELD;
209: } else if (token.startsWith("V{")) {
210: chunkType = JRExpressionChunk.TYPE_VARIABLE;
211: } else if (token.startsWith("R{")) {
212: chunkType = JRExpressionChunk.TYPE_RESOURCE;
213: }
214: }
215:
216: if (chunkType == JRExpressionChunk.TYPE_TEXT) {
217: if (behindDelims > 0) {
218: textChunk.append("$");
219: }
220: textChunk.append(token);
221: } else {
222: int end = token.indexOf('}');
223: if (end > 0) {
224: if (behindDelims > 1) {
225: textChunk.append(token);
226: } else {
227: if (textChunk.length() > 0) {
228: addTextChunk(textChunk.toString());
229: }
230:
231: addChunk(chunkType, token.substring(2,
232: end));
233: textChunk = new StringBuffer(token
234: .substring(end + 1));
235: }
236: } else {
237: if (behindDelims > 0) {
238: textChunk.append("$");
239: }
240: textChunk.append(token);
241: }
242: }
243:
244: behindDelims = 0;
245: }
246: }
247:
248: if (behindDelims > 0) {
249: textChunk.append("$");
250: }
251:
252: if (textChunk.length() > 0) {
253: this.addTextChunk(textChunk.toString());
254: }
255: }
256: }
257:
258: }
|