1 28 29 33 package net.sf.jasperreports.engine.design; 34 35 import java.util.ArrayList ; 36 import java.util.List ; 37 import java.util.StringTokenizer ; 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 48 public class JRDesignExpression extends JRBaseExpression 49 { 50 53 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 54 55 58 protected List chunks = new ArrayList (); 59 60 61 64 public JRDesignExpression() 65 { 66 super(); 67 68 regenerateId(); 69 } 70 71 72 75 public void setValueClass(Class clazz) 76 { 77 setValueClassName(clazz.getName()); 78 } 79 80 83 public void setValueClassName(String className) 84 { 85 valueClassName = className; 86 valueClass = null; 87 } 88 89 92 public void setId(int id) 93 { 94 this.id = id; 95 } 96 97 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 117 public void setChunks(List chunks) 118 { 119 this.chunks.clear(); 120 this.chunks.addAll(chunks); 121 } 122 123 126 public void addChunk(JRDesignExpressionChunk chunk) 127 { 128 this.chunks.add(chunk); 129 } 130 131 134 public void addTextChunk(String 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 146 public void addParameterChunk(String 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 158 public void addFieldChunk(String 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 170 public void addVariableChunk(String 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 182 public void addResourceChunk(String 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 194 public void setText(String text) 195 { 196 chunks.clear(); 197 198 if (text != null) 199 { 200 int end = 0; 201 StringBuffer textChunk = new StringBuffer (); 202 203 StringTokenizer tkzer = new StringTokenizer (text, "$", true); 204 String 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 (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 (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 (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 (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 |