KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > parser > Parser


1 /*
2  * $Id: Parser.java,v 1.4 2002/03/17 03:51:34 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.parser;
52
53 import java.io.*;
54 import java.util.*;
55 import java.util.zip.*;
56
57 import org.openlaszlo.iv.flash.api.*;
58 import org.openlaszlo.iv.flash.util.*;
59
60
61 public final class Parser extends FlashBuffer {
62
63     private int tagStartPos;
64     private int tagDataPos;
65     private int tagLength;
66     private int tagEndPos;
67     private int tagCode;
68
69     private byte[] temp_bufb;
70     private char[] temp_bufc;
71
72     // file
73
private FlashFile file;
74
75     public Parser() {
76     }
77
78     public int getTag() {
79         tagStartPos = getPos();
80         int code = getUWord();
81         int length = code & 0x3f;
82         code = code >> 6;
83
84         if( length == 0x3f ) {
85             length = getUDWord();
86         }
87
88         tagDataPos = getPos();
89         tagLength = length;
90         tagCode = code;
91         tagEndPos = tagDataPos+length;
92         return code;
93     }
94
95     public void addDef( FlashDef def ) {
96         file.addDef(def);
97     }
98
99     public FlashDef getDef( int id ) {
100         return file.getDef(id);
101     }
102
103     public void addDefToLibrary( String JavaDoc name, FlashDef def ) {
104         file.addDefToLibrary( name, def );
105     }
106
107     public FlashDef getDefFromLibrary( String JavaDoc name ) {
108         return file.getDefFromLibrary( name );
109     }
110
111     public int getTagStartPos() {
112         return tagStartPos;
113     }
114     public int getTagDataPos() {
115         return tagDataPos;
116     }
117     public int getTagLength() {
118         return tagLength;
119     }
120     public int getTagEndPos() {
121         return tagEndPos;
122     }
123     public int getTagCode() {
124         return tagCode;
125     }
126
127     public byte[] getTempByteBuf( int size ) {
128         if( temp_bufb == null || temp_bufb.length < size ) {
129             temp_bufb = new byte[size];
130         }
131         return temp_bufb;
132     }
133
134     public char[] getTempCharBuf( int size ) {
135         if( temp_bufc == null || temp_bufc.length < size ) {
136             temp_bufc = new char[size];
137         }
138         return temp_bufc;
139     }
140
141     public FlashObject newUnknownTag() {
142         return new UnparsedTag( tagCode, getBuf(), tagStartPos, tagEndPos );
143     }
144
145     public void skipLastTag() {
146          setPos( tagEndPos );
147     }
148
149     public FlashFile getFile() {
150         return file;
151     }
152
153     /**
154      * Parse input stream
155      */

156     public void parseStream( InputStream is, FlashFile file ) throws IVException, IOException {
157
158         byte[] fileHdr = new byte[8];
159
160         if( is.read(fileHdr, 0, 8) != 8 ) {
161             throw new IVException( Resource.CANTREADHEADER, new Object JavaDoc[] {file.getFullName()} );
162         }
163
164         if( fileHdr[1] != 'W' || fileHdr[2] != 'S' ) {
165             throw new IVException( Resource.ILLEGALHEADER, new Object JavaDoc[] {file.getFullName()} );
166         }
167
168         boolean isCompressed = false;
169         if( fileHdr[0] == 'C' ) {
170             isCompressed = true;
171         } else if( fileHdr[0] != 'F' ) {
172             throw new IVException( Resource.ILLEGALHEADER, new Object JavaDoc[] {file.getFullName()} );
173         }
174
175         // get the file size.
176
int fileSize = Util.getUDWord(fileHdr[4], fileHdr[5], fileHdr[6], fileHdr[7]);
177
178         if( fileSize < 21 ) {
179             throw new IVException( Resource.FILETOOSHORT, new Object JavaDoc[] {file.getFullName()} );
180         }
181
182         FlashBuffer fb;
183         try {
184             fb = new FlashBuffer(fileSize+8);
185         } catch( OutOfMemoryError JavaDoc e ) {
186             throw new IVException( Resource.FILETOOBIG, new Object JavaDoc[] {file.getFullName()} );
187         }
188
189         fb.writeArray(fileHdr, 0, 8);
190
191         if( isCompressed ) {
192             is = new InflaterInputStream(is);
193         }
194
195         fb.write(is);
196
197         file.setCompressed(isCompressed);
198         _parseBuffer(fb, file);
199     }
200
201     /**
202      * Parse FlashBuffer
203      */

204     public void parseBuffer( FlashBuffer fob, FlashFile file ) throws IVException {
205         boolean isCompressed = false;
206
207         if( fob.getBuf()[0] == 'C' ) {
208             isCompressed = true;
209         } else if( fob.getBuf()[0] != 'F' ) {
210             throw new IVException( Resource.ILLEGALHEADER, new Object JavaDoc[] {file.getFullName()} );
211         }
212
213         if( isCompressed ) { // decompress buffer
214
int fileSize = fob.getDWordAt(4);
215
216             if( fileSize < 21 ) {
217                 throw new IVException( Resource.FILETOOSHORT, new Object JavaDoc[] {file.getFullName()} );
218             }
219
220             FlashBuffer fb;
221             try {
222                 fb = new FlashBuffer(fileSize+8);
223             } catch( OutOfMemoryError JavaDoc e ) {
224                 throw new IVException( Resource.FILETOOBIG, new Object JavaDoc[] {file.getFullName()} );
225             }
226
227             fb.writeArray(fob.getBuf(), 0, 8);
228
229             try {
230                 fb.write( new InflaterInputStream(fob.getInputStream(8)) );
231             } catch( IOException e ) {
232                 throw new IVException(e);
233             }
234             fob = fb;
235         }
236
237         file.setCompressed(isCompressed);
238         _parseBuffer(fob, file);
239     }
240
241     /**
242      */

243     private void _parseBuffer( FlashBuffer fb, FlashFile file ) throws IVException {
244         this.file = file;
245
246         init( fb.getBuf(), 0, fb.getSize() );
247
248         // get version
249
file.setVersion( (int) getByteAt(3) );
250
251         setPos(8);
252
253         // get frame size
254
file.setFrameSize( getRect() );
255
256         // get frame rate
257
file.setFrameRate( getUWord() );
258
259         // parse main script
260
file.setMainScript( Script.parse( this, true ) );
261     }
262
263 }
264
Popular Tags