1 6 7 11 12 package org.openlaszlo.xml.internal; 13 14 import java.io.*; 15 import java.util.*; 16 17 import org.openlaszlo.iv.flash.util.*; 18 import org.openlaszlo.iv.flash.api.action.*; 19 import org.openlaszlo.iv.flash.api.*; 20 import org.openlaszlo.compiler.CompilationError; 21 import org.openlaszlo.utils.ChainedException; 22 import org.openlaszlo.utils.FileUtils; 23 import org.openlaszlo.utils.HashIntTable; 24 25 import org.apache.log4j.*; 26 27 33 public abstract class DataCommon { 34 35 static public String ROOT_NODE_INSTANTIATOR_FN = "_rootndi"; 36 static public String RESULTSET_NODE_INSTANTIATOR_FN = "_resultsetndi"; 37 static public String BODY_NODE_INSTANTIATOR_FN = "_bodyndi"; 38 static public String NODE_INSTANTIATOR_FN = "_m"; 39 static public String TEXT_INSTANTIATOR_FN = "_t"; 40 static public String ROOT_NODE_FINAL_FN = "_finishndi"; 41 42 47 static public final void pushStringData(String s, FlashBuffer body, DataContext dc) { 48 int idx = maybeInternString(s, dc); 49 if (idx != -1) { 50 body.writeByte(Actions.PushData); 53 body.writeWord(2); 54 writeOffset(idx, body); 55 } else { 56 body.writeByte(Actions.PushData); 60 body.writeWord(getByteLength(s, dc.encoding) + 2); body.writeByte(0); body.writeStringZ(s, dc.encoding); 63 } 64 } 65 66 static public int getByteLength (String s, String encoding) { 67 try { 68 byte buf[] = s.getBytes(encoding); 69 return buf.length; 70 } catch (UnsupportedEncodingException e) { 71 throw new RuntimeException ("Got UnsupportedEncodingException for charset "+encoding 72 +" on string '"+s+"': "+e.getMessage()); 73 } 74 } 75 76 77 87 static public final void _pushStringData(String s, FlashBuffer body, DataContext dc) { 88 int idx = maybeInternString(s, dc); 89 if (idx != -1) { 90 body._writeByte(Actions.PushData); 93 body._writeWord(2); 94 writeOffset(idx, body); 95 } else { 96 body._writeByte(Actions.PushData); 100 body._writeWord(getByteLength(s, dc.encoding) + 2); body._writeByte(0); body._writeStringZ(s, dc.encoding); 103 } 104 } 105 106 120 121 static public final void pushMergedStringData(String s, FlashBuffer body, DataContext dc) { 122 int idx = maybeInternString(s, dc); 123 if (idx != -1) { 124 writeOffset(idx, body); 126 } else { 127 body.writeByte(0); body.writeStringZ(s, dc.encoding); 131 } 132 } 133 134 135 148 149 static public final void _pushMergedStringData(String s, FlashBuffer body, DataContext dc) { 150 int idx = maybeInternString(s, dc); 151 if (idx != -1) { 152 _writeOffset(idx, body); 154 } else { 155 body._writeByte(0); body._writeStringZ(s, dc.encoding); 159 } 160 } 161 162 175 static public final void pushMergedStringDataSymbol(String s, FlashBuffer body, DataContext dc) { 176 int idx = internString(s, dc); 177 if (idx != -1) { 178 writeOffset(idx, body); 180 } else { 181 body.writeByte(0); body.writeStringZ(s, dc.encoding); 185 } 186 } 187 188 189 197 static public final void _pushMergedStringDataSymbol(String s, FlashBuffer body, DataContext dc) { 198 int idx = internString(s, dc); 199 if (idx != -1) { 200 writeOffset(idx, body); 202 } else { 203 body._writeByte(0); body._writeStringZ(s, dc.encoding); 207 } 208 } 209 210 211 214 static public final void writeFlashData(FlashBuffer body, byte[] data, int destPos) { 215 byte[] buf = body.getBuf(); 217 System.arraycopy(data, 0, buf, destPos, data.length); 218 body.setPos(destPos + data.length); 219 } 220 221 222 225 final static public byte[] makeStringPool(DataContext dc) { 226 HashIntTable pool = dc.cpool; 227 int nstrings = pool.size(); 229 if (nstrings > (1<<16 - 1)) { 231 throw new RuntimeException ("more than 64k strings in constant pool"); 232 } 233 String [] sortArray = new String [nstrings]; 234 Enumeration keys = pool.keys(); 235 int poolsize = 0; 236 while ( keys.hasMoreElements() ) { 238 String key = (String ) keys.nextElement(); 239 int v = pool.get(key); 240 sortArray[v] = key; 242 poolsize += (getByteLength(key, dc.encoding) + 1); } 244 245 byte pooldata[] = new byte[poolsize]; 246 int pos = 0; 247 for (int i = 0; i < nstrings; i++) { 248 String s = sortArray[i]; 249 byte chars[]; 252 try { 253 chars = s.getBytes(dc.encoding); 254 } catch (UnsupportedEncodingException e) { 255 chars = s.getBytes(); 256 } 257 int len = chars.length; 258 System.arraycopy(chars, 0, pooldata, pos, len); 260 pos += len; 261 pooldata[pos++] = 0; } 263 return pooldata; 264 } 265 266 272 static public final int addStringConstant(String s, DataContext dc) { 273 int size = dc.cpool.size(); 274 dc.cpool.put(s, size); 275 dc.pool_data_length += (getByteLength(s, dc.encoding) + 1); 276 return size; 277 } 278 279 282 static public final int getStringIndex(String s, DataContext dc) { 283 return dc.cpool.get(s); 284 } 285 286 292 static public final int maybeInternString(String s, DataContext dc) { 293 int idx = dc.cpool.get(s); 294 if (idx >= 0) { 295 return idx; 296 } 297 if (dc.pool_data_length >= 65532) { 298 return -1; 299 } 300 boolean seen = dc.cpool_first.containsKey(s); 301 if (seen) { 302 int size = dc.cpool.size(); 303 dc.pool_data_length += (getByteLength(s, dc.encoding) + 1); 305 if (dc.pool_data_length >= 65532) { 307 return -1; 308 } 309 dc.cpool.put(s, size); 310 return size; 311 } else { 312 dc.cpool_first.put(s, 1); 313 return(-1); 314 } 315 } 316 317 323 static public final int internString(String s, DataContext dc) { 324 int idx = dc.cpool.get(s); 325 if (idx >= 0) { 326 return idx; 327 } 328 if (dc.pool_data_length >= 65532) { 329 return -1; 330 } 331 int size = dc.cpool.size(); 332 dc.pool_data_length += (getByteLength(s, dc.encoding) + 1); 334 if (dc.pool_data_length >= 65532) { 336 return -1; 337 } 338 dc.cpool.put(s, size); 339 return size; 340 } 341 342 347 static public final void writeOffset(int o, FlashBuffer fb) { 348 if (o > 255) { 349 fb.writeByte(9); 350 fb.writeWord(o); 351 } else { 352 fb.writeByte(8); 353 fb.writeByte(o); 354 } 355 } 356 357 358 365 static public final void _writeOffset(int o, FlashBuffer fb) { 366 if (o > 255) { 367 fb._writeByte(9); 368 fb._writeWord(o); 369 } else { 370 fb._writeByte(8); 371 fb._writeByte(o); 372 } 373 } 374 375 378 379 static public void printProgram (Program program) { 380 FlashBuffer body = program.body(); 382 byte[] buf = body.getBuf(); 383 System.out.print("char data[] = {\n "); 384 for (int i=0; i < body.getSize(); i++) { 385 if (i % 10 == 0) { 386 System.out.print("\n "); 387 } 388 if (i < buf.length-1) { 389 System.out.print("(byte) 0x"+Integer.toHexString((buf[i]>>4 ) & 0xf) + Integer.toHexString(buf[i] & 0xf) + ", "); 390 } else { 391 System.out.print("(byte) 0x"+Integer.toHexString((buf[i]>>4 ) & 0xf) + Integer.toHexString(buf[i] & 0xf)); 392 } 393 } 394 System.out.println("\n};"); 395 } 396 } 397 398 | Popular Tags |