Source Code Cross Referenced for FlashFile.java in  » Ajax » Laszlo-4.0.10 » org » openlaszlo » iv » flash » api » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » Laszlo 4.0.10 » org.openlaszlo.iv.flash.api 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: FlashFile.java,v 1.11 2002/08/02 03:15:17 skavish Exp $
003:         *
004:         * ==========================================================================
005:         *
006:         * The JGenerator Software License, Version 1.0
007:         *
008:         * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009:         *
010:         * Redistribution and use in source and binary forms, with or without
011:         * modification, are permitted provided that the following conditions are met:
012:         *
013:         * 1. Redistributions of source code must retain the above copyright
014:         *    notice, this list of conditions and the following disclaimer.
015:         *
016:         * 2. Redistributions in binary form must reproduce the above copyright
017:         *    notice, this list of conditions and the following disclaimer in
018:         *    the documentation and/or other materials provided with the
019:         *    distribution.
020:         *
021:         * 3. The end-user documentation included with the redistribution, if
022:         *    any, must include the following acknowlegement:
023:         *    "This product includes software developed by Dmitry Skavish
024:         *     (skavish@usa.net, http://www.flashgap.com/)."
025:         *    Alternately, this acknowlegement may appear in the software itself,
026:         *    if and wherever such third-party acknowlegements normally appear.
027:         *
028:         * 4. The name "The JGenerator" must not be used to endorse or promote
029:         *    products derived from this software without prior written permission.
030:         *    For written permission, please contact skavish@usa.net.
031:         *
032:         * 5. Products derived from this software may not be called "The JGenerator"
033:         *    nor may "The JGenerator" appear in their names without prior written
034:         *    permission of Dmitry Skavish.
035:         *
036:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039:         * DISCLAIMED.  IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047:         * SUCH DAMAGE.
048:         *
049:         */
050:
051:        package org.openlaszlo.iv.flash.api;
052:
053:        import java.io.*;
054:        import java.util.*;
055:        import java.util.zip.*;
056:        import java.awt.geom.Rectangle2D;
057:        import org.openlaszlo.iv.flash.util.*;
058:        import org.openlaszlo.iv.flash.url.*;
059:
060:        import org.openlaszlo.iv.flash.cache.*;
061:        import org.openlaszlo.iv.flash.parser.*;
062:        import org.openlaszlo.iv.flash.api.text.*;
063:        import org.openlaszlo.iv.flash.api.image.*;
064:        import org.openlaszlo.iv.flash.context.*;
065:
066:        /**
067:         * Class represents a Flash file (either .swt or .swf)
068:         *
069:         * @author Dmitry Skavish
070:         */
071:        public class FlashFile {
072:
073:            private String fileName; // file name
074:            private String fileDir; // file directory
075:
076:            private int version; // version of flash file format
077:            private Rectangle2D frameSize; // size of the movie in twixels
078:            private int frameRate; // frame rate (number of frames/second left shifted by 8)
079:            private int fileSize; // file size in bytes
080:            private Script main; // main timeline
081:
082:            private int processCount = 0;
083:            private boolean isTemplate = false; // true - swt, false - swf
084:            private FlashFile defaultSymbolFile; // file of default flash symbols
085:
086:            private String encoding; // default encoding for this file
087:
088:            private boolean fullParsing; // true - full parsing, lazy parsing is disabled
089:
090:            private IVMap defsByID = new IVMap(); // all definitions defined in the file
091:
092:            private Hashtable defsByName = new Hashtable(); // all definitions defined and named in the file
093:
094:            private Hashtable exportTable = new Hashtable(); // table of all exported assets (definitions exported by ExportAssets)
095:
096:            private Hashtable externalFiles = new Hashtable(); // all external media loaded from within the file
097:
098:            private boolean isCompressed = false; // true if the file is compressed (flash 6)
099:
100:            public FlashFile() {
101:            }
102:
103:            public String getFileName() {
104:                return fileName;
105:            }
106:
107:            public void setFileName(String fileName) {
108:                this .fileName = Util.translatePath(fileName);
109:            }
110:
111:            public String getFileDir() {
112:                return fileDir;
113:            }
114:
115:            public void setFileDir(String fileDir) {
116:                this .fileDir = Util.translatePath(fileDir);
117:            }
118:
119:            /**
120:             * Returns absolute file name
121:             *
122:             * @return absolute file name
123:             */
124:            public String getFullName() {
125:                return new File(fileDir, fileName).getAbsolutePath();
126:            }
127:
128:            public void setVersion(int version) {
129:                this .version = version;
130:            }
131:
132:            public void setFrameSize(Rectangle2D r) {
133:                this .frameSize = r;
134:            }
135:
136:            public void setFileSize(int size) {
137:                this .fileSize = size;
138:            }
139:
140:            public void setFrameRate(int fr) {
141:                this .frameRate = fr;
142:            }
143:
144:            public void setEncoding(String encoding) {
145:                this .encoding = encoding;
146:            }
147:
148:            public void setMainScript(Script main) {
149:                this .main = main;
150:            }
151:
152:            public void setCompressed(boolean isCompressed) {
153:                this .isCompressed = isCompressed;
154:            }
155:
156:            public boolean isCompressed() {
157:                return isCompressed;
158:            }
159:
160:            public int getVersion() {
161:                return version;
162:            }
163:
164:            public Rectangle2D getFrameSize() {
165:                return frameSize;
166:            }
167:
168:            public int getFileSize() {
169:                return fileSize;
170:            }
171:
172:            /**
173:             * Return framerate of the file
174:             * <P>
175:             * getFrameRate()>>8 = number of frames per second
176:             *
177:             * @return framerate of the file
178:             */
179:            public int getFrameRate() {
180:                return frameRate;
181:            }
182:
183:            public String getEncoding() {
184:                return encoding;
185:            }
186:
187:            public Script getMainScript() {
188:                return main;
189:            }
190:
191:            public void setTemplate(boolean isTemplate) {
192:                this .isTemplate = isTemplate;
193:            }
194:
195:            public void setFullParsing(boolean fullParsing) {
196:                this .fullParsing = fullParsing;
197:            }
198:
199:            public boolean isFullParsing() {
200:                return fullParsing;
201:            }
202:
203:            /**
204:             * Returns true if the file is Flash template, otherwise returns false
205:             * <p>
206:             * The criteria is not the file extention (.swt), but MM Generator special 'template' tag
207:             */
208:            public boolean isTemplate() {
209:                return isTemplate;
210:            }
211:
212:            /**
213:             * Adds specified FlashDef into export table
214:             *
215:             * @param name   linkage name
216:             * @param def    specified definition
217:             */
218:            public void addDefInAssets(String name, FlashDef def) {
219:                exportTable.put(name, def);
220:            }
221:
222:            /**
223:             * Returns FlashDef from export table by its linkage name
224:             *
225:             * @param name   linkage name of asset
226:             * @return found asset or null
227:             */
228:            public FlashDef getDefInAssets(String name) {
229:                return (FlashDef) exportTable.get(name);
230:            }
231:
232:            /**
233:             * Returns definition by ID
234:             *
235:             * @param id     definition ID
236:             * @return definition or null
237:             */
238:            public FlashDef getDef(int id) {
239:                return defsByID.get(id);
240:            }
241:
242:            /**
243:             * Adds definition by ID
244:             *
245:             * @param def    definition to be added
246:             */
247:            public void addDef(FlashDef def) {
248:                defsByID.add(def);
249:            }
250:
251:            /**
252:             * Adds definition by name
253:             *
254:             * @param name   name of the definition
255:             * @param def    definition
256:             */
257:            public void addDefToLibrary(String name, FlashDef def) {
258:                if (!PropertyManager.symCaseSensitive)
259:                    name = name.toUpperCase();
260:                defsByName.put(name, def);
261:            }
262:
263:            /**
264:             * Finds definition by name in this file and all its external files (if any)
265:             *
266:             * @param name   name of the definition
267:             * @return found definition or null
268:             */
269:            public FlashDef getDefFromLibrary(String name) {
270:                if (!PropertyManager.symCaseSensitive)
271:                    name = name.toUpperCase();
272:                FlashDef def = (FlashDef) defsByName.get(name);
273:                if (def != null)
274:                    return def;
275:                if (externalFiles.size() > 0) {
276:                    Enumeration files = externalFiles.elements();
277:                    while (files.hasMoreElements()) {
278:                        Object o = files.nextElement();
279:                        if (o instanceof  FlashFile) {
280:                            FlashFile file = (FlashFile) o;
281:                            def = file.getDefFromLibrary(name);
282:                            if (def != null)
283:                                return def;
284:                        }
285:                    }
286:                }
287:                return null;
288:            }
289:
290:            /**
291:             * Reads, parses, and adds external file to this file (if it is not here yet)
292:             *
293:             * @param fileName name of the file to be added
294:             * @param cache    if true then cache the file in the MediaCache
295:             * @return added flash file
296:             * @exception IVException
297:             */
298:            public synchronized FlashFile addExternalFile(String fileName,
299:                    boolean cache) throws IVException {
300:                try {
301:                    Object o = addExternalMedia(fileName, cache);
302:                    if (o instanceof  FlashFile)
303:                        return (FlashFile) o;
304:                    return null;
305:                } catch (IOException e) {
306:                    throw new IVException(Resource.ERRREADINGFILE,
307:                            new Object[] { fileName }, e);
308:                }
309:            }
310:
311:            /**
312:             * Reads an external media and adds it to this file (if it is not here yet) and to media cache
313:             *
314:             * @param fileName name of the file to be added
315:             * @param cache    if true then cache the file in the MediaCache
316:             * @return FlashFile or Bitmap
317:             * @exception IVException
318:             */
319:            public synchronized Object addExternalMedia(IVUrl url, boolean cache)
320:                    throws IVException, IOException {
321:                String fileName = url.getName();
322:
323:                // try to retrieve from this file's cache
324:                Object media = getExternalMedia(fileName);
325:                if (media != null)
326:                    return media;
327:
328:                // try to retrive from media cache
329:                media = MediaCache.getMedia(fileName);
330:
331:                if (media == null) {
332:                    // read the media
333:                    InputStream is = url.getInputStream();
334:                    FlashBuffer fb = new FlashBuffer(is);
335:
336:                    // detect media type
337:                    int firstByte = fb.getUByte();
338:                    if (firstByte == 'F' || firstByte == 'C') { // check for swf format
339:                        if (fb.getUByte() == 'W' && fb.getUByte() == 'S') {
340:                            String myEncoding = url.getEncoding();
341:                            media = parse(fileName, fb, isFullParsing(),
342:                                    myEncoding != null ? myEncoding
343:                                            : getEncoding());
344:                        } else {
345:                            throw new IVException(Resource.UNSUPMEDIA,
346:                                    new Object[] { fileName });
347:                        }
348:                    } else {
349:                        media = Bitmap.newBitmap(fb);
350:                        if (media == null) {
351:                            throw new IVException(Resource.UNSUPMEDIA,
352:                                    new Object[] { fileName });
353:                        }
354:                    }
355:
356:                    // add to media cache depending on url parameters and 'cache' variable
357:                    // 1.5 multiplier is because parsed data are bigger than the original ones (a little bit)
358:                    MediaCache.addMedia(url, media, (fb.getSize() * 3) / 2,
359:                            cache);
360:                }
361:
362:                // add to this file's cache
363:                addExternalMedia(fileName, media);
364:
365:                return media;
366:            }
367:
368:            /**
369:             * Reads an external media and adds it to this file (if it is not here yet) and to media cache
370:             *
371:             * @param fileName name of the file to be added
372:             * @param cache    if true then cache the file in the MediaCache
373:             * @return FlashFile or Bitmap
374:             * @exception IVException
375:             */
376:            public synchronized Object addExternalMedia(String fileName,
377:                    boolean cache) throws IVException, IOException {
378:                return addExternalMedia(IVUrl.newUrl(fileName, this ), cache);
379:            }
380:
381:            /**
382:             * Finds external media by its file name
383:             *
384:             * @param filename name of the file to be searched
385:             * @return found media or null
386:             */
387:            public Object getExternalMedia(String filename) {
388:                return externalFiles.get(filename);
389:            }
390:
391:            /**
392:             * Adds external media to this file (if it is not here yet)
393:             *
394:             * @param filename name of the file to be added
395:             * @param object media to be added
396:             */
397:            public void addExternalMedia(String filename, Object object) {
398:                externalFiles.put(filename, object);
399:            }
400:
401:            /**
402:             * Finds external file by file name
403:             *
404:             * @param filename name of the file to be searched
405:             * @return found file or null
406:             */
407:            public FlashFile getExternalFile(String filename) {
408:                Object o = getExternalMedia(filename);
409:                if (o instanceof  FlashFile) {
410:                    return (FlashFile) o;
411:                }
412:                return null;
413:            }
414:
415:            /**
416:             * Adds external file to this file (if it is not here yet)
417:             *
418:             * @param filename name of the file to be added
419:             * @param file     flash file to be added
420:             */
421:            public void addExternalFile(String filename, FlashFile file) {
422:                addExternalMedia(filename, file);
423:            }
424:
425:            /**
426:             * Returns default symbol file
427:             *
428:             * @return default symbol file
429:             */
430:            public synchronized FlashFile getDefaultSymbolFile() {
431:                if (defaultSymbolFile != null)
432:                    return defaultSymbolFile;
433:                String fileName = PropertyManager.getProperty(
434:                        "org.openlaszlo.iv.flash.defaultSymbolFile",
435:                        "bin/DefaultSymbolFile.swt");
436:                fileName = Util.getSysFile(fileName).getAbsolutePath();
437:                try {
438:                    defaultSymbolFile = addExternalFile(fileName, true);
439:                    return defaultSymbolFile;
440:                } catch (IVException e) {
441:                    Log.log(e);
442:                    return null;
443:                }
444:            }
445:
446:            /**
447:             * Searches font by its name in this file and all its external files
448:             *
449:             * @param name   font name
450:             * @return font or null
451:             */
452:            public Font getFont(String name) {
453:                for (Enumeration e = defsByID.values(); e.hasMoreElements();) {
454:                    FlashDef def = (FlashDef) e.nextElement();
455:                    if (def instanceof  FontDef) {
456:                        Font font = ((FontDef) def).getFont();
457:                        if (font.getFontName().equalsIgnoreCase(name))
458:                            return font;
459:                    }
460:                }
461:                if (externalFiles.size() > 0) {
462:                    Enumeration files = externalFiles.elements();
463:                    while (files.hasMoreElements()) {
464:                        Object o = files.nextElement();
465:                        if (o instanceof  FlashFile) {
466:                            FlashFile file = (FlashFile) o;
467:                            Font font = file.getFont(name);
468:                            if (font != null)
469:                                return font;
470:                        }
471:                    }
472:                }
473:                return null;
474:            }
475:
476:            /**
477:             * Returns all fonts defined within this file (fonts from external files are not included)
478:             *
479:             * @return vector of FontDef objects
480:             */
481:            public IVVector getLocalFonts() {
482:                IVVector v = new IVVector();
483:                Enumeration e = definitions();
484:                while (e.hasMoreElements()) {
485:                    FlashDef def = (FlashDef) e.nextElement();
486:                    if (def instanceof  FontDef)
487:                        v.addElement(((FontDef) def).getFont());
488:                }
489:                return v;
490:            }
491:
492:            /**
493:             * Finds script by name defined within this file or any external files
494:             *
495:             * @param name   script name
496:             * @return found script or null
497:             */
498:            public Script getScript(String name) {
499:                FlashDef def = getDefFromLibrary(name);
500:                if (def instanceof  Script)
501:                    return (Script) def;
502:                if (def == null)
503:                    return getScriptInAssets(name);
504:                return null;
505:            }
506:
507:            /**
508:             * Finds script by name defined as an ExportAsset within this file or any external files
509:             *
510:             * @param name   script name
511:             * @return found script or null
512:             */
513:            public Script getScriptInAssets(String name) {
514:                FlashDef def = getDefInAssets(name.toUpperCase());
515:                if (def != null && def instanceof  Script)
516:                    return (Script) def;
517:                if (externalFiles.size() > 0) {
518:                    Enumeration files = externalFiles.elements();
519:                    while (files.hasMoreElements()) {
520:                        Object o = files.nextElement();
521:                        if (o instanceof  FlashFile) {
522:                            FlashFile file = (FlashFile) o;
523:                            def = file.getScriptInAssets(name);
524:                            if (def != null)
525:                                return (Script) def;
526:                        }
527:                    }
528:                }
529:                return null;
530:            }
531:
532:            /**
533:             * Returns Enumeration of all definitions (FlashDef) defined within this file
534:             *
535:             * @return Enumeration of FlashDef
536:             */
537:            public Enumeration definitions() {
538:                return new Enumeration() {
539:                    private Enumeration e = defsByID.values();
540:                    private int names = 0;
541:
542:                    public boolean hasMoreElements() {
543:                        for (;;) {
544:                            switch (names) {
545:                            case 0:
546:                                if (e.hasMoreElements())
547:                                    return true;
548:                                e = defsByName.elements();
549:                                names = 1;
550:                            case 1:
551:                                if (e.hasMoreElements())
552:                                    return true;
553:                                names = 2;
554:                            case 2:
555:                                return false;
556:                            }
557:                        }
558:                    }
559:
560:                    public Object nextElement() {
561:                        return e.nextElement();
562:                    }
563:                };
564:            }
565:
566:            /**
567:             * Processes this file in the specified context
568:             *
569:             * @param context context to process in
570:             * @return this file
571:             * @exception IVException
572:             */
573:            public FlashFile processFile(Context context) throws IVException {
574:                if (!isTemplate() && version < 6)
575:                    return this ;
576:                processScript(main, new StandardContext(context));
577:                return this ;
578:            }
579:
580:            /**
581:             * Processes the specified script in the specified context
582:             *
583:             * @param script  specified script
584:             * @param context specified context
585:             * @return processed script
586:             * @exception IVException
587:             */
588:            public Script processScript(Script script, Context context)
589:                    throws IVException {
590:                return (Script) processObject(script, context);
591:            }
592:
593:            /**
594:             * Processes the specified object in the specified context
595:             *
596:             * @param fobj    specified object
597:             * @param context specified context
598:             * @return processed object
599:             * @exception IVException
600:             */
601:            public FlashObject processObject(FlashObject fobj, Context context)
602:                    throws IVException {
603:                processCount++;
604:                if (processCount < 50) {
605:                    if (context == null)
606:                        context = new StandardContext();
607:                    fobj.process(this , context);
608:                } else {
609:                    throw new IVException(Resource.INFINITELOOP);
610:                }
611:                processCount--;
612:                fobj.setProcessed();
613:                return fobj;
614:            }
615:
616:            /**
617:             * Generates file into FlashOutput buffer
618:             *
619:             * @return output buffer
620:             * @exception IVException
621:             */
622:            public FlashOutput generate() throws IVException {
623:                FlashOutput fob = new FlashOutput(this , getFileSize());
624:
625:                // write signature
626:                fob.writeByte(isCompressed ? 'C' : 'F');
627:                fob.writeByte('W');
628:                fob.writeByte('S');
629:                // write version
630:                fob.writeByte(version);
631:                // skip file size
632:                fob.skip(4);
633:
634:                int filesize;
635:                if (isCompressed) {
636:                    FlashOutput b_fob = new FlashOutput(this , getFileSize());
637:                    b_fob.write(frameSize); // write file rect
638:                    b_fob.writeWord(frameRate); // write frame rate
639:                    main.generate(b_fob); // write main timeline
640:                    filesize = b_fob.getSize() + 8;
641:                    try {
642:                        OutputStream os = new DeflaterOutputStream(fob
643:                                .getOutputStream());
644:                        os.write(b_fob.getBuf(), 0, b_fob.getSize());
645:                        os.close();
646:                    } catch (IOException e) {
647:                        throw new IVException(e);
648:                    }
649:                } else {
650:                    fob.write(frameSize); // write file rect
651:                    fob.writeWord(frameRate); // write frame rate
652:                    main.generate(fob); // write main timeline
653:                    filesize = fob.getSize();
654:                }
655:
656:                // write file size
657:                fob.writeDWordAt(filesize, 4);
658:
659:                return fob;
660:            }
661:
662:            /**
663:             * Generates file into FlashOutput buffer
664:             *
665:             * @return output buffer
666:             * @exception IVException
667:             */
668:            public FlashOutput generate(FontsCollector fc, FontsCollector pfc,
669:                    boolean hasPreloader) throws IVException {
670:
671:                FlashOutput fob = new FlashOutput(this , getFileSize());
672:
673:                // write signature
674:                fob.writeByte(isCompressed ? 'C' : 'F');
675:                fob.writeByte('W');
676:                fob.writeByte('S');
677:                // write version
678:                fob.writeByte(version);
679:                // skip file size
680:                fob.skip(4);
681:
682:                int filesize;
683:                if (isCompressed) {
684:                    FlashOutput b_fob = new FlashOutput(this , getFileSize());
685:                    b_fob.write(frameSize); // write file rect
686:                    b_fob.writeWord(frameRate); // write frame rate
687:                    main.generate(b_fob, fc, pfc, hasPreloader); // write main timeline
688:                    filesize = b_fob.getSize() + 8;
689:                    try {
690:                        OutputStream os = new DeflaterOutputStream(fob
691:                                .getOutputStream());
692:                        os.write(b_fob.getBuf(), 0, b_fob.getSize());
693:                        os.close();
694:                    } catch (IOException e) {
695:                        throw new IVException(e);
696:                    }
697:                } else {
698:                    fob.write(frameSize); // write file rect
699:                    fob.writeWord(frameRate); // write frame rate
700:                    main.generate(fob, fc, pfc, hasPreloader); // write main timeline
701:                    filesize = fob.getSize();
702:                }
703:
704:                // write file size
705:                fob.writeDWordAt(filesize, 4);
706:
707:                return fob;
708:            }
709:
710:            /**
711:             * Parses Flash file from input stream
712:             *
713:             * @param fileName file name, used only for establishing the file's current directory
714:             * @param is       input stream used to read the file's content
715:             * @return created flash file
716:             * @exception IVException
717:             */
718:            public static FlashFile parse(String fileName, InputStream is)
719:                    throws IVException {
720:                return parse(fileName, is, false, null);
721:            }
722:
723:            public static FlashFile parse(String fileName, InputStream is,
724:                    boolean isFullParsing, String encoding) throws IVException {
725:                FlashFile file = newFlashFileForParse(fileName, isFullParsing,
726:                        encoding);
727:
728:                try {
729:                    Parser parser = new Parser();
730:                    parser.parseStream(is, file);
731:                    return file;
732:                } catch (IOException e) {
733:                    throw new IVException(Resource.ERRPARSETEMPLATE,
734:                            new Object[] { file.getFullName() }, e);
735:                } finally {
736:                    try {
737:                        is.close();
738:                    } catch (Exception ee) {
739:                    }
740:                }
741:            }
742:
743:            /**
744:             * Parses Flash file from buffer
745:             *
746:             * @param fileName file name, used only for establishing the file's current directory
747:             * @param fb       flash buffer to read the file from
748:             * @return created flash file
749:             * @exception IVException
750:             */
751:            public static FlashFile parse(String fileName, FlashBuffer fb)
752:                    throws IVException {
753:                return parse(fileName, fb, false, null);
754:            }
755:
756:            public static FlashFile parse(String fileName, FlashBuffer fb,
757:                    boolean isFullParsing, String encoding) throws IVException {
758:                FlashFile file = newFlashFileForParse(fileName, isFullParsing,
759:                        encoding);
760:                Parser parser = new Parser();
761:                parser.parseBuffer(fb, file);
762:                return file;
763:            }
764:
765:            /**
766:             * Parses Flash file from file
767:             *
768:             * @param fileName file name of the flash file
769:             * @return created flash file
770:             * @exception IVException
771:             * @exception FileNotFoundException
772:             */
773:            public static FlashFile parse(String fileName) throws IVException,
774:                    FileNotFoundException {
775:                return parse(fileName, false, null);
776:            }
777:
778:            public static FlashFile parse(String fileName,
779:                    boolean isFullParsing, String encoding) throws IVException,
780:                    FileNotFoundException {
781:                File file = new File(fileName);
782:                String path = file.getAbsolutePath();
783:
784:                try {
785:                    FileInputStream is = new FileInputStream(file);
786:                    FlashFile flashFile = parse(path, is, isFullParsing,
787:                            encoding);
788:                    return flashFile;
789:                } catch (FileNotFoundException e) {
790:                    throw e;
791:                } catch (IOException e) {
792:                    throw new IVException(Resource.ERRPARSETEMPLATE,
793:                            new Object[] { path }, e);
794:                }
795:            }
796:
797:            /**
798:             * Creates empty flash file with default settings
799:             *
800:             * @return default flash file
801:             */
802:            public static FlashFile newFlashFile() {
803:                FlashFile file = new FlashFile();
804:                file.setVersion(5);
805:                file.setFrameSize(GeomHelper.newRectangle(0, 0, 540 * 20,
806:                        400 * 20));
807:                file.setFrameRate(12 << 8);
808:                file.setFileSize(100); // does not have much sense
809:                file.encoding = PropertyManager.defaultEncoding;
810:
811:                return file;
812:            }
813:
814:            /**
815:             * Creates a copy of this flash file
816:             *
817:             * @return copy of this flash file
818:             */
819:            public synchronized FlashFile copyFile() {
820:                FlashFile file = new FlashFile();
821:                file.setFileName(fileName);
822:                file.setFileDir(fileDir);
823:                file.setVersion(version);
824:                file.setFrameSize((Rectangle2D) frameSize.clone());
825:                file.setFrameRate(frameRate);
826:                file.setFileSize(fileSize);
827:                file.processCount = processCount;
828:                file.isTemplate = isTemplate;
829:                file.isCompressed = isCompressed;
830:                file.encoding = encoding;
831:
832:                // copy main script
833:                ScriptCopier copier = new ScriptCopier();
834:                file.main = (Script) main.getCopy(copier);
835:
836:                // copier now contains all copied definitions
837:                // copy defsByID
838:                for (Enumeration e = defsByID.values(); e.hasMoreElements();) {
839:                    FlashDef def = (FlashDef) e.nextElement();
840:                    FlashDef myDef = copier.get(def);
841:                    if (myDef != null)
842:                        file.defsByID.add(myDef);
843:                }
844:
845:                // copy defsByName
846:                for (Enumeration e = defsByName.keys(); e.hasMoreElements();) {
847:                    Object key = e.nextElement();
848:                    FlashDef def = (FlashDef) defsByName.get(key);
849:                    FlashDef myDef = copier.get(def);
850:                    if (myDef != null)
851:                        file.defsByName.put(key, myDef);
852:                }
853:
854:                // copy exportTable
855:                for (Enumeration e = exportTable.keys(); e.hasMoreElements();) {
856:                    Object key = e.nextElement();
857:                    FlashDef def = (FlashDef) exportTable.get(key);
858:                    FlashDef myDef = copier.get(def);
859:                    if (myDef != null)
860:                        file.exportTable.put(key, myDef);
861:                }
862:
863:                // it's enough just to clone external files
864:                file.externalFiles = (Hashtable) externalFiles.clone();
865:
866:                return file;
867:            }
868:
869:            public void printContent(PrintStream out) {
870:                out.println("Flash: version=" + version + " size="
871:                        + getFileSize());
872:                out.println("  frame: (" + frameSize.toString() + ")");
873:                out.println("  frameRate=" + (frameRate >> 8));
874:                main.printContent(out, "");
875:            }
876:
877:            private static FlashFile newFlashFileForParse(String fileName,
878:                    boolean isFullParsing, String encoding) {
879:                fileName = Util.translatePath(fileName);
880:                int index = fileName.lastIndexOf(Util.fileSeparator);
881:                String dir, name;
882:                if (index >= 0) {
883:                    dir = fileName.substring(0, index);
884:                    name = fileName.substring(index + 1);
885:                } else {
886:                    dir = "";
887:                    name = fileName;
888:                }
889:                FlashFile flashFile = new FlashFile();
890:                flashFile.setFileName(name);
891:                flashFile.setFileDir(dir);
892:                flashFile.setFullParsing(isFullParsing);
893:                flashFile.setEncoding(encoding != null ? encoding
894:                        : PropertyManager.defaultEncoding);
895:                return flashFile;
896:            }
897:
898:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.