1 50 51 package org.openlaszlo.iv.flash.api; 52 53 import java.io.*; 54 import java.util.*; 55 import java.util.zip.*; 56 import java.awt.geom.Rectangle2D ; 57 import org.openlaszlo.iv.flash.util.*; 58 import org.openlaszlo.iv.flash.url.*; 59 60 import org.openlaszlo.iv.flash.cache.*; 61 import org.openlaszlo.iv.flash.parser.*; 62 import org.openlaszlo.iv.flash.api.text.*; 63 import org.openlaszlo.iv.flash.api.image.*; 64 import org.openlaszlo.iv.flash.context.*; 65 66 71 public class FlashFile { 72 73 private String fileName; private String fileDir; 76 private int version; private Rectangle2D frameSize; private int frameRate; private int fileSize; private Script main; 82 private int processCount = 0; 83 private boolean isTemplate = false; private FlashFile defaultSymbolFile; 86 private String encoding; 88 private boolean fullParsing; 90 private IVMap defsByID = new IVMap(); 92 private Hashtable defsByName = new Hashtable(); 94 private Hashtable exportTable = new Hashtable(); 96 private Hashtable externalFiles = new Hashtable(); 98 private boolean isCompressed = false; 100 public FlashFile() {} 101 102 public String getFileName() { 103 return fileName; 104 } 105 public void setFileName( String fileName ) { 106 this.fileName = Util.translatePath(fileName); 107 } 108 109 public String getFileDir() { 110 return fileDir; 111 } 112 public void setFileDir( String fileDir ) { 113 this.fileDir = Util.translatePath(fileDir); 114 } 115 116 121 public String getFullName() { 122 return new File( fileDir, fileName ).getAbsolutePath(); 123 } 124 125 public void setVersion( int version ) { 126 this.version = version; 127 } 128 129 public void setFrameSize( Rectangle2D r ) { 130 this.frameSize = r; 131 } 132 133 public void setFileSize( int size ) { 134 this.fileSize = size; 135 } 136 137 public void setFrameRate( int fr ) { 138 this.frameRate = fr; 139 } 140 141 public void setEncoding( String encoding ) { 142 this.encoding = encoding; 143 } 144 145 public void setMainScript( Script main ) { 146 this.main = main; 147 } 148 149 public void setCompressed( boolean isCompressed ) { 150 this.isCompressed = isCompressed; 151 } 152 153 public boolean isCompressed() { 154 return isCompressed; 155 } 156 157 public int getVersion() { 158 return version; 159 } 160 161 public Rectangle2D getFrameSize() { 162 return frameSize; 163 } 164 165 public int getFileSize() { 166 return fileSize; 167 } 168 169 176 public int getFrameRate() { 177 return frameRate; 178 } 179 180 public String getEncoding() { 181 return encoding; 182 } 183 184 public Script getMainScript() { 185 return main; 186 } 187 188 public void setTemplate( boolean isTemplate ) { 189 this.isTemplate = isTemplate; 190 } 191 192 public void setFullParsing( boolean fullParsing ) { 193 this.fullParsing = fullParsing; 194 } 195 196 public boolean isFullParsing() { 197 return fullParsing; 198 } 199 200 205 public boolean isTemplate() { 206 return isTemplate; 207 } 208 209 215 public void addDefInAssets( String name, FlashDef def ) { 216 exportTable.put(name, def); 217 } 218 219 225 public FlashDef getDefInAssets( String name ) { 226 return (FlashDef) exportTable.get(name); 227 } 228 229 235 public FlashDef getDef( int id ) { 236 return defsByID.get(id); 237 } 238 239 244 public void addDef( FlashDef def ) { 245 defsByID.add(def); 246 } 247 248 254 public void addDefToLibrary( String name, FlashDef def ) { 255 if( !PropertyManager.symCaseSensitive ) name = name.toUpperCase(); 256 defsByName.put(name, def); 257 } 258 259 265 public FlashDef getDefFromLibrary( String name ) { 266 if( !PropertyManager.symCaseSensitive ) name = name.toUpperCase(); 267 FlashDef def = (FlashDef) defsByName.get(name); 268 if( def != null ) return def; 269 if( externalFiles.size() > 0 ) { 270 Enumeration files = externalFiles.elements(); 271 while( files.hasMoreElements() ) { 272 Object o = files.nextElement(); 273 if( o instanceof FlashFile ) { 274 FlashFile file = (FlashFile) o; 275 def = file.getDefFromLibrary( name ); 276 if( def != null ) return def; 277 } 278 } 279 } 280 return null; 281 } 282 283 291 public synchronized FlashFile addExternalFile( String fileName, boolean cache ) throws IVException { 292 try { 293 Object o = addExternalMedia(fileName, cache); 294 if( o instanceof FlashFile) return (FlashFile) o; 295 return null; 296 } catch( IOException e ) { 297 throw new IVException(Resource.ERRREADINGFILE, new Object [] {fileName}, e); 298 } 299 } 300 301 309 public synchronized Object addExternalMedia( IVUrl url, boolean cache ) throws IVException, IOException { 310 String fileName = url.getName(); 311 312 Object media = getExternalMedia( fileName ); 314 if( media != null ) return media; 315 316 media = MediaCache.getMedia( fileName ); 318 319 if( media == null ) { 320 InputStream is = url.getInputStream(); 322 FlashBuffer fb = new FlashBuffer(is); 323 324 int firstByte = fb.getUByte(); 326 if( firstByte == 'F' || firstByte == 'C' ) { if( fb.getUByte() == 'W' && fb.getUByte() == 'S' ) { 328 String myEncoding = url.getEncoding(); 329 media = parse(fileName, fb, isFullParsing(), myEncoding!=null?myEncoding:getEncoding()); 330 } else { 331 throw new IVException( Resource.UNSUPMEDIA, new Object [] {fileName} ); 332 } 333 } else { 334 media = Bitmap.newBitmap(fb); 335 if( media == null ) { 336 throw new IVException( Resource.UNSUPMEDIA, new Object [] {fileName} ); 337 } 338 } 339 340 MediaCache.addMedia( url, media, (fb.getSize()*3)/2, cache ); 343 } 344 345 addExternalMedia(fileName, media); 347 348 return media; 349 } 350 351 359 public synchronized Object addExternalMedia( String fileName, boolean cache ) throws IVException, IOException { 360 return addExternalMedia(IVUrl.newUrl(fileName, this), cache); 361 } 362 363 369 public Object getExternalMedia( String filename ) { 370 return externalFiles.get( filename ); 371 } 372 373 379 public void addExternalMedia( String filename, Object object ) { 380 externalFiles.put( filename, object ); 381 } 382 383 389 public FlashFile getExternalFile( String filename ) { 390 Object o = getExternalMedia(filename); 391 if( o instanceof FlashFile ) { 392 return (FlashFile) o; 393 } 394 return null; 395 } 396 397 403 public void addExternalFile( String filename, FlashFile file ) { 404 addExternalMedia(filename, file); 405 } 406 407 412 public synchronized FlashFile getDefaultSymbolFile() { 413 if( defaultSymbolFile != null ) return defaultSymbolFile; 414 String fileName = PropertyManager.getProperty("org.openlaszlo.iv.flash.defaultSymbolFile","bin/DefaultSymbolFile.swt"); 415 fileName = Util.getSysFile(fileName).getAbsolutePath(); 416 try { 417 defaultSymbolFile = addExternalFile( fileName, true ); 418 return defaultSymbolFile; 419 } catch( IVException e ) { 420 Log.log(e); 421 return null; 422 } 423 } 424 425 431 public Font getFont( String name ) { 432 for( Enumeration e = defsByID.values(); e.hasMoreElements(); ) { 433 FlashDef def = (FlashDef) e.nextElement(); 434 if( def instanceof FontDef ) { 435 Font font = ((FontDef)def).getFont(); 436 if( font.getFontName().equalsIgnoreCase(name) ) return font; 437 } 438 } 439 if( externalFiles.size() > 0 ) { 440 Enumeration files = externalFiles.elements(); 441 while( files.hasMoreElements() ) { 442 Object o = files.nextElement(); 443 if( o instanceof FlashFile ) { 444 FlashFile file = (FlashFile) o; 445 Font font = file.getFont( name ); 446 if( font != null ) return font; 447 } 448 } 449 } 450 return null; 451 } 452 453 458 public IVVector getLocalFonts() { 459 IVVector v = new IVVector(); 460 Enumeration e = definitions(); 461 while( e.hasMoreElements() ) { 462 FlashDef def = (FlashDef) e.nextElement(); 463 if( def instanceof FontDef ) v.addElement( ((FontDef)def).getFont() ); 464 } 465 return v; 466 } 467 468 474 public Script getScript( String name ) { 475 FlashDef def = getDefFromLibrary( name ); 476 if( def instanceof Script ) return (Script) def; 477 if ( def == null ) return getScriptInAssets( name ); 478 return null; 479 } 480 481 487 public Script getScriptInAssets( String name ) { 488 FlashDef def = getDefInAssets(name.toUpperCase()); 489 if( def != null && def instanceof Script ) return (Script) def; 490 if( externalFiles.size() > 0 ) { 491 Enumeration files = externalFiles.elements(); 492 while( files.hasMoreElements() ) { 493 Object o = files.nextElement(); 494 if( o instanceof FlashFile ) { 495 FlashFile file = (FlashFile) o; 496 def = file.getScriptInAssets( name ); 497 if( def != null ) return (Script) def; 498 } 499 } 500 } 501 return null; 502 } 503 504 509 public Enumeration definitions() { 510 return new Enumeration() { 511 private Enumeration e = defsByID.values(); 512 private int names = 0; 513 public boolean hasMoreElements() { 514 for(;;) { 515 switch(names) { 516 case 0: 517 if( e.hasMoreElements() ) return true; 518 e = defsByName.elements(); 519 names = 1; 520 case 1: 521 if( e.hasMoreElements() ) return true; 522 names = 2; 523 case 2: 524 return false; 525 } 526 } 527 } 528 public Object nextElement() { 529 return e.nextElement(); 530 } 531 }; 532 } 533 534 541 public FlashFile processFile( Context context ) throws IVException { 542 if( !isTemplate() && version < 6 ) return this; 543 processScript( main, new StandardContext( context ) ); 544 return this; 545 } 546 547 555 public Script processScript( Script script, Context context ) throws IVException { 556 return (Script) processObject(script, context); 557 } 558 559 567 public FlashObject processObject( FlashObject fobj, Context context ) throws IVException { 568 processCount++; 569 if( processCount < 50 ) { 570 if( context == null ) context = new StandardContext(); 571 fobj.process(this, context); 572 } else { 573 throw new IVException(Resource.INFINITELOOP); 574 } 575 processCount--; 576 fobj.setProcessed(); 577 return fobj; 578 } 579 580 586 public FlashOutput generate() throws IVException { 587 FlashOutput fob = new FlashOutput(this, getFileSize()); 588 589 fob.writeByte( isCompressed?'C':'F' ); 591 fob.writeByte( 'W' ); 592 fob.writeByte( 'S' ); 593 fob.writeByte( version ); 595 fob.skip(4); 597 598 int filesize; 599 if( isCompressed ) { 600 FlashOutput b_fob = new FlashOutput(this, getFileSize()); 601 b_fob.write( frameSize ); b_fob.writeWord( frameRate ); main.generate( b_fob ); filesize = b_fob.getSize()+8; 605 try { 606 OutputStream os = new DeflaterOutputStream(fob.getOutputStream()); 607 os.write(b_fob.getBuf(), 0, b_fob.getSize()); 608 os.close(); 609 } catch( IOException e ) { 610 throw new IVException(e); 611 } 612 } else { 613 fob.write( frameSize ); fob.writeWord( frameRate ); main.generate( fob ); filesize = fob.getSize(); 617 } 618 619 fob.writeDWordAt( filesize, 4 ); 621 622 return fob; 623 } 624 625 626 632 public FlashOutput generate(FontsCollector fc, FontsCollector pfc, 633 boolean hasPreloader) 634 throws IVException { 635 636 FlashOutput fob = new FlashOutput(this, getFileSize()); 637 638 fob.writeByte( isCompressed?'C':'F' ); 640 fob.writeByte( 'W' ); 641 fob.writeByte( 'S' ); 642 fob.writeByte( version ); 644 fob.skip(4); 646 647 int filesize; 648 if( isCompressed ) { 649 FlashOutput b_fob = new FlashOutput(this, getFileSize()); 650 b_fob.write( frameSize ); b_fob.writeWord( frameRate ); main.generate( b_fob, fc, pfc, hasPreloader ); filesize = b_fob.getSize()+8; 654 try { 655 OutputStream os = new DeflaterOutputStream(fob.getOutputStream()); 656 os.write(b_fob.getBuf(), 0, b_fob.getSize()); 657 os.close(); 658 } catch( IOException e ) { 659 throw new IVException(e); 660 } 661 } else { 662 fob.write( frameSize ); fob.writeWord( frameRate ); main.generate( fob, fc, pfc, hasPreloader ); filesize = fob.getSize(); 666 } 667 668 fob.writeDWordAt( filesize, 4 ); 670 671 return fob; 672 } 673 674 675 683 public static FlashFile parse( String fileName, InputStream is ) throws IVException { 684 return parse(fileName, is, false, null); 685 } 686 687 public static FlashFile parse( String fileName, InputStream is, boolean isFullParsing, String encoding ) throws IVException { 688 FlashFile file = newFlashFileForParse( fileName, isFullParsing, encoding ); 689 690 try { 691 Parser parser = new Parser(); 692 parser.parseStream( is, file ); 693 return file; 694 } catch( IOException e ) { 695 throw new IVException(Resource.ERRPARSETEMPLATE, new Object [] {file.getFullName()}, e); 696 } finally { 697 try { 698 is.close(); 699 } catch( Exception ee ) {} 700 } 701 } 702 703 711 public static FlashFile parse( String fileName, FlashBuffer fb ) throws IVException { 712 return parse(fileName, fb, false, null); 713 } 714 715 public static FlashFile parse( String fileName, FlashBuffer fb, boolean isFullParsing, String encoding ) throws IVException { 716 FlashFile file = newFlashFileForParse(fileName, isFullParsing, encoding); 717 Parser parser = new Parser(); 718 parser.parseBuffer( fb, file ); 719 return file; 720 } 721 722 730 public static FlashFile parse( String fileName ) throws IVException, FileNotFoundException { 731 return parse(fileName, false, null); 732 } 733 734 public static FlashFile parse( String fileName, boolean isFullParsing, String encoding ) 735 throws IVException, FileNotFoundException 736 { 737 File file = new File(fileName); 738 String path = file.getAbsolutePath(); 739 740 try { 741 FileInputStream is = new FileInputStream(file); 742 FlashFile flashFile = parse(path, is, isFullParsing, encoding); 743 return flashFile; 744 } catch( FileNotFoundException e ) { 745 throw e; 746 } catch( IOException e ) { 747 throw new IVException(Resource.ERRPARSETEMPLATE, new Object [] {path}, e); 748 } 749 } 750 751 756 public static FlashFile newFlashFile() { 757 FlashFile file = new FlashFile(); 758 file.setVersion( 5 ); 759 file.setFrameSize( GeomHelper.newRectangle(0, 0, 540*20, 400*20) ); 760 file.setFrameRate( 12<<8 ); 761 file.setFileSize( 100 ); file.encoding = PropertyManager.defaultEncoding; 763 764 return file; 765 } 766 767 772 public synchronized FlashFile copyFile() { 773 FlashFile file = new FlashFile(); 774 file.setFileName( fileName ); 775 file.setFileDir( fileDir ); 776 file.setVersion( version ); 777 file.setFrameSize( (Rectangle2D ) frameSize.clone() ); 778 file.setFrameRate( frameRate ); 779 file.setFileSize( fileSize ); 780 file.processCount = processCount; 781 file.isTemplate = isTemplate; 782 file.isCompressed = isCompressed; 783 file.encoding = encoding; 784 785 ScriptCopier copier = new ScriptCopier(); 787 file.main = (Script) main.getCopy( copier ); 788 789 for( Enumeration e = defsByID.values(); e.hasMoreElements(); ) { 792 FlashDef def = (FlashDef) e.nextElement(); 793 FlashDef myDef = copier.get( def ); 794 if( myDef != null ) file.defsByID.add( myDef ); 795 } 796 797 for( Enumeration e = defsByName.keys(); e.hasMoreElements(); ) { 799 Object key = e.nextElement(); 800 FlashDef def = (FlashDef) defsByName.get( key ); 801 FlashDef myDef = copier.get( def ); 802 if( myDef != null ) file.defsByName.put( key, myDef ); 803 } 804 805 for( Enumeration e = exportTable.keys(); e.hasMoreElements(); ) { 807 Object key = e.nextElement(); 808 FlashDef def = (FlashDef) exportTable.get(key); 809 FlashDef myDef = copier.get( def ); 810 if( myDef != null ) file.exportTable.put( key, myDef ); 811 } 812 813 file.externalFiles = (Hashtable) externalFiles.clone(); 815 816 return file; 817 } 818 819 public void printContent( PrintStream out ) { 820 out.println( "Flash: version="+version+" size="+getFileSize() ); 821 out.println( " frame: ("+frameSize.toString()+")" ); 822 out.println( " frameRate="+(frameRate>>8) ); 823 main.printContent( out, "" ); 824 } 825 826 private static FlashFile newFlashFileForParse( String fileName, boolean isFullParsing, String encoding ) { 827 fileName = Util.translatePath(fileName); 828 int index = fileName.lastIndexOf( Util.fileSeparator ); 829 String dir, name; 830 if( index >= 0 ) { 831 dir = fileName.substring(0, index); 832 name = fileName.substring(index+1); 833 } else { 834 dir = ""; 835 name = fileName; 836 } 837 FlashFile flashFile = new FlashFile(); 838 flashFile.setFileName( name ); 839 flashFile.setFileDir( dir ); 840 flashFile.setFullParsing(isFullParsing); 841 flashFile.setEncoding(encoding!=null?encoding:PropertyManager.defaultEncoding); 842 return flashFile; 843 } 844 845 } 846 847 | Popular Tags |