KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > FlashFile


1 /*
2  * $Id: FlashFile.java,v 1.11 2002/08/02 03:15:17 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

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 JavaDoc;
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 /**
67  * Class represents a Flash file (either .swt or .swf)
68  *
69  * @author Dmitry Skavish
70  */

71 public class FlashFile {
72
73     private String JavaDoc fileName; // file name
74
private String JavaDoc fileDir; // file directory
75

76     private int version; // version of flash file format
77
private Rectangle2D JavaDoc frameSize; // size of the movie in twixels
78
private int frameRate; // frame rate (number of frames/second left shifted by 8)
79
private int fileSize; // file size in bytes
80
private Script main; // main timeline
81

82     private int processCount = 0;
83     private boolean isTemplate = false; // true - swt, false - swf
84
private FlashFile defaultSymbolFile; // file of default flash symbols
85

86     private String JavaDoc encoding; // default encoding for this file
87

88     private boolean fullParsing; // true - full parsing, lazy parsing is disabled
89

90     private IVMap defsByID = new IVMap(); // all definitions defined in the file
91

92     private Hashtable defsByName = new Hashtable(); // all definitions defined and named in the file
93

94     private Hashtable exportTable = new Hashtable(); // table of all exported assets (definitions exported by ExportAssets)
95

96     private Hashtable externalFiles = new Hashtable(); // all external media loaded from within the file
97

98     private boolean isCompressed = false; // true if the file is compressed (flash 6)
99

100     public FlashFile() {}
101
102     public String JavaDoc getFileName() {
103         return fileName;
104     }
105     public void setFileName( String JavaDoc fileName ) {
106         this.fileName = Util.translatePath(fileName);
107     }
108
109     public String JavaDoc getFileDir() {
110         return fileDir;
111     }
112     public void setFileDir( String JavaDoc fileDir ) {
113         this.fileDir = Util.translatePath(fileDir);
114     }
115
116     /**
117      * Returns absolute file name
118      *
119      * @return absolute file name
120      */

121     public String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getFrameSize() {
162         return frameSize;
163     }
164
165     public int getFileSize() {
166         return fileSize;
167     }
168
169     /**
170      * Return framerate of the file
171      * <P>
172      * getFrameRate()>>8 = number of frames per second
173      *
174      * @return framerate of the file
175      */

176     public int getFrameRate() {
177         return frameRate;
178     }
179
180     public String JavaDoc 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     /**
201      * Returns true if the file is Flash template, otherwise returns false
202      * <p>
203      * The criteria is not the file extention (.swt), but MM Generator special 'template' tag
204      */

205     public boolean isTemplate() {
206         return isTemplate;
207     }
208
209     /**
210      * Adds specified FlashDef into export table
211      *
212      * @param name linkage name
213      * @param def specified definition
214      */

215     public void addDefInAssets( String JavaDoc name, FlashDef def ) {
216         exportTable.put(name, def);
217     }
218
219     /**
220      * Returns FlashDef from export table by its linkage name
221      *
222      * @param name linkage name of asset
223      * @return found asset or null
224      */

225     public FlashDef getDefInAssets( String JavaDoc name ) {
226         return (FlashDef) exportTable.get(name);
227     }
228
229     /**
230      * Returns definition by ID
231      *
232      * @param id definition ID
233      * @return definition or null
234      */

235     public FlashDef getDef( int id ) {
236         return defsByID.get(id);
237     }
238
239     /**
240      * Adds definition by ID
241      *
242      * @param def definition to be added
243      */

244     public void addDef( FlashDef def ) {
245         defsByID.add(def);
246     }
247
248     /**
249      * Adds definition by name
250      *
251      * @param name name of the definition
252      * @param def definition
253      */

254     public void addDefToLibrary( String JavaDoc name, FlashDef def ) {
255         if( !PropertyManager.symCaseSensitive ) name = name.toUpperCase();
256         defsByName.put(name, def);
257     }
258
259     /**
260      * Finds definition by name in this file and all its external files (if any)
261      *
262      * @param name name of the definition
263      * @return found definition or null
264      */

265     public FlashDef getDefFromLibrary( String JavaDoc 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 JavaDoc 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     /**
284      * Reads, parses, and adds external file to this file (if it is not here yet)
285      *
286      * @param fileName name of the file to be added
287      * @param cache if true then cache the file in the MediaCache
288      * @return added flash file
289      * @exception IVException
290      */

291     public synchronized FlashFile addExternalFile( String JavaDoc fileName, boolean cache ) throws IVException {
292         try {
293             Object JavaDoc 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 JavaDoc[] {fileName}, e);
298         }
299     }
300
301     /**
302      * Reads an external media and adds it to this file (if it is not here yet) and to media cache
303      *
304      * @param fileName name of the file to be added
305      * @param cache if true then cache the file in the MediaCache
306      * @return FlashFile or Bitmap
307      * @exception IVException
308      */

309     public synchronized Object JavaDoc addExternalMedia( IVUrl url, boolean cache ) throws IVException, IOException {
310         String JavaDoc fileName = url.getName();
311
312         // try to retrieve from this file's cache
313
Object JavaDoc media = getExternalMedia( fileName );
314         if( media != null ) return media;
315
316             // try to retrive from media cache
317
media = MediaCache.getMedia( fileName );
318
319         if( media == null ) {
320             // read the media
321
InputStream is = url.getInputStream();
322             FlashBuffer fb = new FlashBuffer(is);
323
324             // detect media type
325
int firstByte = fb.getUByte();
326             if( firstByte == 'F' || firstByte == 'C' ) { // check for swf format
327
if( fb.getUByte() == 'W' && fb.getUByte() == 'S' ) {
328                 String JavaDoc myEncoding = url.getEncoding();
329                     media = parse(fileName, fb, isFullParsing(), myEncoding!=null?myEncoding:getEncoding());
330                 } else {
331                     throw new IVException( Resource.UNSUPMEDIA, new Object JavaDoc[] {fileName} );
332                 }
333             } else {
334                 media = Bitmap.newBitmap(fb);
335                 if( media == null ) {
336                     throw new IVException( Resource.UNSUPMEDIA, new Object JavaDoc[] {fileName} );
337                 }
338             }
339
340             // add to media cache depending on url parameters and 'cache' variable
341
// 1.5 multiplier is because parsed data are bigger than the original ones (a little bit)
342
MediaCache.addMedia( url, media, (fb.getSize()*3)/2, cache );
343         }
344
345         // add to this file's cache
346
addExternalMedia(fileName, media);
347
348         return media;
349     }
350
351     /**
352      * Reads an external media and adds it to this file (if it is not here yet) and to media cache
353      *
354      * @param fileName name of the file to be added
355      * @param cache if true then cache the file in the MediaCache
356      * @return FlashFile or Bitmap
357      * @exception IVException
358      */

359     public synchronized Object JavaDoc addExternalMedia( String JavaDoc fileName, boolean cache ) throws IVException, IOException {
360         return addExternalMedia(IVUrl.newUrl(fileName, this), cache);
361     }
362
363     /**
364      * Finds external media by its file name
365      *
366      * @param filename name of the file to be searched
367      * @return found media or null
368      */

369     public Object JavaDoc getExternalMedia( String JavaDoc filename ) {
370         return externalFiles.get( filename );
371     }
372
373     /**
374      * Adds external media to this file (if it is not here yet)
375      *
376      * @param filename name of the file to be added
377      * @param object media to be added
378      */

379     public void addExternalMedia( String JavaDoc filename, Object JavaDoc object ) {
380         externalFiles.put( filename, object );
381     }
382
383     /**
384      * Finds external file by file name
385      *
386      * @param filename name of the file to be searched
387      * @return found file or null
388      */

389     public FlashFile getExternalFile( String JavaDoc filename ) {
390         Object JavaDoc o = getExternalMedia(filename);
391         if( o instanceof FlashFile ) {
392             return (FlashFile) o;
393         }
394         return null;
395     }
396
397     /**
398      * Adds external file to this file (if it is not here yet)
399      *
400      * @param filename name of the file to be added
401      * @param file flash file to be added
402      */

403     public void addExternalFile( String JavaDoc filename, FlashFile file ) {
404         addExternalMedia(filename, file);
405     }
406
407     /**
408      * Returns default symbol file
409      *
410      * @return default symbol file
411      */

412     public synchronized FlashFile getDefaultSymbolFile() {
413         if( defaultSymbolFile != null ) return defaultSymbolFile;
414         String JavaDoc 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     /**
426      * Searches font by its name in this file and all its external files
427      *
428      * @param name font name
429      * @return font or null
430      */

431     public Font getFont( String JavaDoc 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 JavaDoc 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     /**
454      * Returns all fonts defined within this file (fonts from external files are not included)
455      *
456      * @return vector of FontDef objects
457      */

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     /**
469      * Finds script by name defined within this file or any external files
470      *
471      * @param name script name
472      * @return found script or null
473      */

474     public Script getScript( String JavaDoc 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     /**
482      * Finds script by name defined as an ExportAsset within this file or any external files
483      *
484      * @param name script name
485      * @return found script or null
486      */

487     public Script getScriptInAssets( String JavaDoc 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 JavaDoc 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     /**
505      * Returns Enumeration of all definitions (FlashDef) defined within this file
506      *
507      * @return Enumeration of FlashDef
508      */

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 JavaDoc nextElement() {
529                 return e.nextElement();
530             }
531         };
532     }
533
534     /**
535      * Processes this file in the specified context
536      *
537      * @param context context to process in
538      * @return this file
539      * @exception IVException
540      */

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     /**
548      * Processes the specified script in the specified context
549      *
550      * @param script specified script
551      * @param context specified context
552      * @return processed script
553      * @exception IVException
554      */

555     public Script processScript( Script script, Context context ) throws IVException {
556         return (Script) processObject(script, context);
557     }
558
559     /**
560      * Processes the specified object in the specified context
561      *
562      * @param fobj specified object
563      * @param context specified context
564      * @return processed object
565      * @exception IVException
566      */

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     /**
581      * Generates file into FlashOutput buffer
582      *
583      * @return output buffer
584      * @exception IVException
585      */

586     public FlashOutput generate() throws IVException {
587         FlashOutput fob = new FlashOutput(this, getFileSize());
588
589         // write signature
590
fob.writeByte( isCompressed?'C':'F' );
591         fob.writeByte( 'W' );
592         fob.writeByte( 'S' );
593         // write version
594
fob.writeByte( version );
595         // skip file size
596
fob.skip(4);
597
598         int filesize;
599         if( isCompressed ) {
600             FlashOutput b_fob = new FlashOutput(this, getFileSize());
601             b_fob.write( frameSize ); // write file rect
602
b_fob.writeWord( frameRate ); // write frame rate
603
main.generate( b_fob ); // write main timeline
604
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 ); // write file rect
614
fob.writeWord( frameRate ); // write frame rate
615
main.generate( fob ); // write main timeline
616
filesize = fob.getSize();
617         }
618
619         // write file size
620
fob.writeDWordAt( filesize, 4 );
621
622         return fob;
623     }
624
625
626     /**
627      * Generates file into FlashOutput buffer
628      *
629      * @return output buffer
630      * @exception IVException
631      */

632     public FlashOutput generate(FontsCollector fc, FontsCollector pfc,
633                                 boolean hasPreloader)
634         throws IVException {
635
636         FlashOutput fob = new FlashOutput(this, getFileSize());
637
638         // write signature
639
fob.writeByte( isCompressed?'C':'F' );
640         fob.writeByte( 'W' );
641         fob.writeByte( 'S' );
642         // write version
643
fob.writeByte( version );
644         // skip file size
645
fob.skip(4);
646
647         int filesize;
648         if( isCompressed ) {
649             FlashOutput b_fob = new FlashOutput(this, getFileSize());
650             b_fob.write( frameSize ); // write file rect
651
b_fob.writeWord( frameRate ); // write frame rate
652
main.generate( b_fob, fc, pfc, hasPreloader ); // write main timeline
653
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 ); // write file rect
663
fob.writeWord( frameRate ); // write frame rate
664
main.generate( fob, fc, pfc, hasPreloader ); // write main timeline
665
filesize = fob.getSize();
666         }
667
668         // write file size
669
fob.writeDWordAt( filesize, 4 );
670
671         return fob;
672     }
673
674
675     /**
676      * Parses Flash file from input stream
677      *
678      * @param fileName file name, used only for establishing the file's current directory
679      * @param is input stream used to read the file's content
680      * @return created flash file
681      * @exception IVException
682      */

683     public static FlashFile parse( String JavaDoc fileName, InputStream is ) throws IVException {
684         return parse(fileName, is, false, null);
685     }
686
687     public static FlashFile parse( String JavaDoc fileName, InputStream is, boolean isFullParsing, String JavaDoc 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 JavaDoc[] {file.getFullName()}, e);
696         } finally {
697             try {
698                 is.close();
699             } catch( Exception JavaDoc ee ) {}
700         }
701     }
702
703     /**
704      * Parses Flash file from buffer
705      *
706      * @param fileName file name, used only for establishing the file's current directory
707      * @param fb flash buffer to read the file from
708      * @return created flash file
709      * @exception IVException
710      */

711     public static FlashFile parse( String JavaDoc fileName, FlashBuffer fb ) throws IVException {
712         return parse(fileName, fb, false, null);
713     }
714
715     public static FlashFile parse( String JavaDoc fileName, FlashBuffer fb, boolean isFullParsing, String JavaDoc 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     /**
723      * Parses Flash file from file
724      *
725      * @param fileName file name of the flash file
726      * @return created flash file
727      * @exception IVException
728      * @exception FileNotFoundException
729      */

730     public static FlashFile parse( String JavaDoc fileName ) throws IVException, FileNotFoundException {
731         return parse(fileName, false, null);
732     }
733
734     public static FlashFile parse( String JavaDoc fileName, boolean isFullParsing, String JavaDoc encoding )
735         throws IVException, FileNotFoundException
736     {
737         File file = new File(fileName);
738         String JavaDoc 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 JavaDoc[] {path}, e);
748         }
749     }
750
751     /**
752      * Creates empty flash file with default settings
753      *
754      * @return default flash file
755      */

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 ); // does not have much sense
762
file.encoding = PropertyManager.defaultEncoding;
763
764         return file;
765     }
766
767     /**
768      * Creates a copy of this flash file
769      *
770      * @return copy of this flash file
771      */

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 JavaDoc) 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         // copy main script
786
ScriptCopier copier = new ScriptCopier();
787         file.main = (Script) main.getCopy( copier );
788
789         // copier now contains all copied definitions
790
// copy defsByID
791
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         // copy defsByName
798
for( Enumeration e = defsByName.keys(); e.hasMoreElements(); ) {
799             Object JavaDoc 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         // copy exportTable
806
for( Enumeration e = exportTable.keys(); e.hasMoreElements(); ) {
807             Object JavaDoc 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         // it's enough just to clone external files
814
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 JavaDoc fileName, boolean isFullParsing, String JavaDoc encoding ) {
827         fileName = Util.translatePath(fileName);
828         int index = fileName.lastIndexOf( Util.fileSeparator );
829         String JavaDoc 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