1 28 package net.sf.jasperreports.engine.base; 29 30 import java.io.Serializable ; 31 32 import net.sf.jasperreports.engine.JRConstants; 33 import net.sf.jasperreports.engine.JRExpression; 34 import net.sf.jasperreports.engine.JRExpressionChunk; 35 import net.sf.jasperreports.engine.JRRuntimeException; 36 import net.sf.jasperreports.engine.util.JRClassLoader; 37 38 39 43 public class JRBaseExpression implements JRExpression, Serializable 44 { 45 46 47 50 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 51 52 55 protected String valueClassName = null; 56 protected int id = 0; 57 58 protected transient Class valueClass = null; 59 60 63 private JRExpressionChunk[] chunks = null; 64 65 68 private static int lastId = 0; 69 70 71 74 protected JRBaseExpression() 75 { 76 } 77 78 79 87 protected JRBaseExpression(JRExpression expression, JRBaseObjectFactory factory, Integer expressionId) 88 { 89 factory.put(expression, this); 90 91 valueClassName = expression.getValueClassName(); 92 if (expressionId == null) 93 { 94 id = expression.getId(); 95 } 96 else 97 { 98 id = expressionId.intValue(); 99 } 100 101 102 JRExpressionChunk[] jrChunks = expression.getChunks(); 103 if (jrChunks != null && jrChunks.length > 0) 104 { 105 chunks = new JRExpressionChunk[jrChunks.length]; 106 for(int i = 0; i < chunks.length; i++) 107 { 108 chunks[i] = factory.getExpressionChunk(jrChunks[i]); 109 } 110 } 111 } 112 113 114 120 protected JRBaseExpression(JRExpression expression, JRBaseObjectFactory factory) 121 { 122 this(expression, factory, null); 123 } 124 125 126 129 private static synchronized int getNextId() 130 { 131 return lastId++; 132 } 133 134 135 138 public void regenerateId() 139 { 140 id = getNextId(); 141 } 142 143 144 147 public Class getValueClass() 148 { 149 if (valueClass == null) 150 { 151 if (valueClassName != null) 152 { 153 try 154 { 155 valueClass = JRClassLoader.loadClassForName(valueClassName); 156 } 157 catch(ClassNotFoundException e) 158 { 159 throw new JRRuntimeException(e); 160 } 161 } 162 } 163 164 return valueClass; 165 } 166 167 170 public String getValueClassName() 171 { 172 return valueClassName; 173 } 174 175 178 public int getId() 179 { 180 return id; 181 } 182 183 186 public JRExpressionChunk[] getChunks() 187 { 188 return chunks; 189 } 190 191 194 public String getText() 195 { 196 String text = ""; 197 198 chunks = getChunks(); 199 if (chunks != null && chunks.length > 0) 200 { 201 StringBuffer sbuffer = new StringBuffer (); 202 203 for(int i = 0; i < chunks.length; i++) 204 { 205 switch(chunks[i].getType()) 206 { 207 case JRExpressionChunk.TYPE_PARAMETER : 208 { 209 sbuffer.append("$P{"); 210 sbuffer.append( chunks[i].getText() ); 211 sbuffer.append("}"); 212 break; 213 } 214 case JRExpressionChunk.TYPE_FIELD : 215 { 216 sbuffer.append("$F{"); 217 sbuffer.append( chunks[i].getText() ); 218 sbuffer.append("}"); 219 break; 220 } 221 case JRExpressionChunk.TYPE_VARIABLE : 222 { 223 sbuffer.append("$V{"); 224 sbuffer.append( chunks[i].getText() ); 225 sbuffer.append("}"); 226 break; 227 } 228 case JRExpressionChunk.TYPE_RESOURCE : 229 { 230 sbuffer.append("$R{"); 231 sbuffer.append( chunks[i].getText() ); 232 sbuffer.append("}"); 233 break; 234 } 235 case JRExpressionChunk.TYPE_TEXT : 236 default : 237 { 238 sbuffer.append( chunks[i].getText() ); 239 break; 240 } 241 } 242 } 243 244 text = sbuffer.toString(); 245 } 246 247 return text; 248 } 249 250 251 } 252 | Popular Tags |