Source code: com/anotherbigidea/flash/writers/ActionTextWriter.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.flash.writers;
35
36 import java.io.IOException;
37 import java.io.PrintWriter;
38
39 import com.anotherbigidea.flash.SWFActionCodes;
40 import com.anotherbigidea.flash.interfaces.SWFActions;
41
42 /**
43 * A writer that implements the SWFActions interface and writes
44 * actions to a text format
45 */
46 public class ActionTextWriter implements SWFActions, SWFActionCodes
47 {
48 protected PrintWriter printer;
49 protected String indent = "";
50
51 public ActionTextWriter( PrintWriter printer )
52 {
53 this.printer = printer;
54 }
55
56 protected void print( String mnemonic, String[] args )
57 {
58 printer.print( indent + " " );
59 writePaddedString( mnemonic + " ", 15 );
60
61 if( args != null )
62 {
63 for( int i = 0; i < args.length; i++ )
64 {
65 if( i > 0 ) printer.print( ", " );
66 printer.print( args[i] );
67 }
68 }
69
70 printer.println();
71 }
72
73 protected void writePaddedString( String s, int length )
74 {
75 int pad = length - s.length();
76
77 printer.print( s );
78 while( pad > 0 )
79 {
80 printer.print( " " );
81 pad--;
82 }
83 }
84
85 public void start( int conditions ) throws IOException
86 {
87 print( "conditions", new String[] { Integer.toBinaryString( conditions ) } );
88 printer.flush();
89 }
90
91 public void end() throws IOException
92 {
93 print( "end", null );
94 printer.println();
95 }
96
97 public void done() throws IOException
98 {
99 printer.flush();
100 }
101
102 public void blob( byte[] blob ) throws IOException
103 {
104 print( "(blob)", null );
105 printer.println();
106 }
107
108 public void unknown( int code, byte[] data ) throws IOException
109 {
110 print( "unknown code =", new String[] { Integer.toString( code ) } );
111 }
112
113 public void initArray() throws IOException
114 {
115 print( "initArray", null );
116 }
117
118 public void jumpLabel( String label ) throws IOException
119 {
120 printer.println( indent + label + ":" );
121 }
122
123 public void gotoFrame( int frameNumber ) throws IOException
124 {
125 print( "gotoFrame", new String[] { Integer.toString( frameNumber ) } );
126 }
127
128 public void gotoFrame( String label ) throws IOException
129 {
130 print( "gotoFrame", new String[] { "\"" + label + "\"" } );
131 }
132
133 public void getURL( String url, String target ) throws IOException
134 {
135 print( "getURL", new String[] { "\"" + url + "\"", "\"" + target + "\"" } );
136 }
137
138 public void nextFrame() throws IOException
139 {
140 print( "nextFrame", null );
141 }
142
143 public void prevFrame() throws IOException
144 {
145 print( "previousFrame", null );
146 }
147
148 public void play() throws IOException
149 {
150 print( "play", null );
151 }
152
153 public void stop() throws IOException
154 {
155 print( "stop", null );
156 }
157
158 public void toggleQuality() throws IOException
159 {
160 print( "toggleQuality", null );
161 }
162
163 public void stopSounds() throws IOException
164 {
165 print( "stopSounds", null );
166 }
167
168 public void setTarget( String target ) throws IOException
169 {
170 print( "setTarget", new String[] { "\"" + target + "\"" } );
171 }
172
173 public void jump( String jumpLabel ) throws IOException
174 {
175 print( "jump", new String[] { "\"" + jumpLabel + "\"" } );
176 }
177
178 public void ifJump( String jumpLabel ) throws IOException
179 {
180 print( "ifJump", new String[] { "\"" + jumpLabel + "\"" } );
181 }
182
183 public void waitForFrame( int frameNumber, String jumpLabel ) throws IOException
184 {
185 print( "waitForFrame", new String[] { Integer.toString( frameNumber ),
186 "\"" + jumpLabel + "\"" } );
187 }
188
189 public void waitForFrame( String jumpLabel ) throws IOException
190 {
191 print( "waitForFrame", new String[] { "\"" + jumpLabel + "\"" } );
192 }
193
194 public void pop() throws IOException
195 {
196 print( "pop", null );
197 }
198
199 public void push( String value ) throws IOException
200 {
201 print( "push", new String[] { "\"" + value + "\"" } );
202 }
203
204 public void push( float value ) throws IOException
205 {
206 print( "push", new String[] { "float " + value } );
207 }
208
209 public void push( double value ) throws IOException
210 {
211 print( "push", new String[] { "double " + value } );
212 }
213
214 public void pushNull() throws IOException
215 {
216 print( "push", new String[] { "null" } );
217 }
218
219 public void pushRegister( int registerNumber ) throws IOException
220 {
221 print( "push", new String[] { "register( " + registerNumber + " )" } );
222 }
223
224 public void push( boolean value ) throws IOException
225 {
226 print( "push", new String[] { value ? "true" : "false" } );
227 }
228
229 public void push( int value ) throws IOException
230 {
231 print( "push", new String[] { "" + value } );
232 }
233
234 public void lookup( int dictionaryIndex ) throws IOException
235 {
236 print( "push", new String[] { "lookup( " + dictionaryIndex + " )" } );
237 }
238
239 public void add() throws IOException
240 {
241 print( "add", null );
242 }
243
244 public void substract() throws IOException
245 {
246 print( "substract", null );
247 }
248
249 public void multiply() throws IOException
250 {
251 print( "multiply", null );
252 }
253
254 public void divide() throws IOException
255 {
256 print( "divide", null );
257 }
258
259 public void equals() throws IOException
260 {
261 print( "equals", null );
262 }
263
264 public void lessThan() throws IOException
265 {
266 print( "lessThan", null );
267 }
268
269 public void and() throws IOException
270 {
271 print( "and", null );
272 }
273
274 public void or() throws IOException
275 {
276 print( "or", null );
277 }
278
279 public void not() throws IOException
280 {
281 print( "not", null );
282 }
283
284 public void stringEquals() throws IOException
285 {
286 print( "stringEquals", null );
287 }
288
289 public void stringLength() throws IOException
290 {
291 print( "stringLength", null );
292 }
293
294 public void concat() throws IOException
295 {
296 print( "concat", null );
297 }
298
299 public void substring() throws IOException
300 {
301 print( "substring", null );
302 }
303
304 public void stringLessThan() throws IOException
305 {
306 print( "stringLessThan", null );
307 }
308
309 public void stringLengthMB() throws IOException
310 {
311 print( "stringLengthMB", null );
312 }
313
314 public void substringMB() throws IOException
315 {
316 print( "substringMB", null );
317 }
318
319 public void toInteger() throws IOException
320 {
321 print( "toInteger", null );
322 }
323
324 public void charToAscii() throws IOException
325 {
326 print( "charToAscii", null );
327 }
328
329 public void asciiToChar() throws IOException
330 {
331 print( "asciiToChar", null );
332 }
333
334 public void charMBToAscii() throws IOException
335 {
336 print( "charMBToAscii", null );
337 }
338
339 public void asciiToCharMB() throws IOException
340 {
341 print( "asciiToCharMB", null );
342 }
343
344 public void call() throws IOException
345 {
346 print( "call", null );
347 }
348
349 public void getVariable() throws IOException
350 {
351 print( "getVariable", null );
352 }
353
354 public void setVariable() throws IOException
355 {
356 print( "setVariable", null );
357 }
358
359 public void getURL( int sendVars, int loadMode ) throws IOException
360 {
361 String sendVars_ = null;
362 switch( sendVars )
363 {
364 case GET_URL_SEND_VARS_GET:
365 sendVars_ = "send vars via GET";
366 break;
367
368 case GET_URL_SEND_VARS_POST:
369 sendVars_ = "send vars via POST";
370 break;
371
372 case GET_URL_SEND_VARS_NONE:
373 default:
374 sendVars_ = "no send";
375 break;
376 }
377
378 String mode = null;
379 switch( loadMode )
380 {
381 case GET_URL_MODE_LOAD_MOVIE_INTO_LEVEL :
382 mode = "load movie into level";
383 break;
384
385 case GET_URL_MODE_LOAD_MOVIE_INTO_SPRITE:
386 mode = "load movie into sprite";
387 break;
388
389 case GET_URL_MODE_LOAD_VARS_INTO_LEVEL :
390 mode = "load vars into level";
391 break;
392
393 case GET_URL_MODE_LOAD_VARS_INTO_SPRITE:
394 mode = "load vars into sprite";
395 break;
396
397 default:
398 mode = "???";
399 break;
400 }
401
402 print( "getURL", new String[] { sendVars_, mode } );
403 }
404
405 public void gotoFrame( boolean play ) throws IOException
406 {
407 print( "gotoFrame", new String[] { play ? "and play" : "and stop" } );
408 }
409
410 public void setTarget() throws IOException
411 {
412 print( "setTarget", null );
413 }
414
415 public void getProperty() throws IOException
416 {
417 print( "getProperty", null );
418 }
419
420 public void setProperty() throws IOException
421 {
422 print( "setProperty", null );
423 }
424
425 public void cloneSprite() throws IOException
426 {
427 print( "cloneSprite", null );
428 }
429
430 public void removeSprite() throws IOException
431 {
432 print( "removeSprite", null );
433 }
434
435 public void startDrag() throws IOException
436 {
437 print( "startDrag", null );
438 }
439
440 public void endDrag() throws IOException
441 {
442 print( "endDrag", null );
443 }
444
445 public void trace() throws IOException
446 {
447 print( "trace", null );
448 }
449
450 public void getTime() throws IOException
451 {
452 print( "getTime", null );
453 }
454
455 public void randomNumber() throws IOException
456 {
457 print( "randomNumber", null );
458 }
459
460 public void lookupTable( String[] values ) throws IOException
461 {
462 print( "lookupTable", null );
463
464 for( int i = 0; i < values.length; i++ )
465 {
466 printer.print( indent + " " );
467 writePaddedString( Integer.toString( i ) + ":", 5 );
468 printer.println( "\"" + values[i] + "\"" );
469 }
470 }
471
472 public void callFunction() throws IOException
473 {
474 print( "callFunction", null );
475 }
476
477 public void callMethod() throws IOException
478 {
479 print( "callMethod", null );
480 }
481
482 public void startFunction( String name, String[] paramNames ) throws IOException
483 {
484 String args = name + "(";
485
486 if( paramNames != null)
487 {
488 for( int i = 0; i < paramNames.length; i++ )
489 {
490 if( i > 0 ) args += ",";
491 args += " " + paramNames[i];
492 }
493
494 if( paramNames.length > 0 ) args += " ";
495 }
496
497 args += ")";
498
499 printer.println();
500 print( "defineFunction", new String[] { args } );
501 print( "{", null );
502
503 indent += " ";
504 }
505
506 public void endBlock() throws IOException
507 {
508 if( indent.length() <= 4 ) indent = "";
509 else if( indent.length() >= 4 ) indent = indent.substring( 4 );
510
511 print( "}", null );
512 printer.println();
513 }
514
515 public void comment( String comment ) throws IOException
516 {
517 printer.println( indent + " // " + comment );
518 }
519
520 public void defineLocalValue() throws IOException
521 {
522 print( "defineLocalValue", null );
523 }
524
525 public void defineLocal() throws IOException
526 {
527 print( "defineLocal", null );
528 }
529
530 public void deleteProperty() throws IOException
531 {
532 print( "deleteProperty", null );
533 }
534
535 public void deleteThreadVars() throws IOException
536 {
537 print( "deleteThreadVars", null );
538 }
539
540 public void enumerate() throws IOException
541 {
542 print( "enumerate", null );
543 }
544
545 public void typedEquals() throws IOException
546 {
547 print( "typedEquals", null );
548 }
549
550 public void getMember() throws IOException
551 {
552 print( "getMember", null );
553 }
554
555 public void initObject() throws IOException
556 {
557 print( "initObject", null );
558 }
559
560 public void newMethod() throws IOException
561 {
562 print( "newMethod", null );
563 }
564
565 public void newObject() throws IOException
566 {
567 print( "newObject", null );
568 }
569
570 public void setMember() throws IOException
571 {
572 print( "setMember", null );
573 }
574
575 public void getTargetPath() throws IOException
576 {
577 print( "getTargetPath", null );
578 }
579
580 public void startWith() throws IOException
581 {
582 printer.println();
583 print( "with", null );
584 print( "{", null );
585
586 indent += " ";
587 }
588
589 public void duplicate() throws IOException
590 {
591 print( "duplicate", null );
592 }
593
594 public void returnValue() throws IOException
595 {
596 print( "return", null );
597 }
598
599 public void swap() throws IOException
600 {
601 print( "swap", null );
602 }
603
604 public void storeInRegister( int registerNumber ) throws IOException
605 {
606 print( "register", new String[] { Integer.toString( registerNumber ) } );
607 }
608
609 public void convertToNumber() throws IOException
610 {
611 print( "convertToNumber", null );
612 }
613
614 public void convertToString() throws IOException
615 {
616 print( "convertToString", null );
617 }
618
619 public void typeOf() throws IOException
620 {
621 print( "typeOf", null );
622 }
623
624 public void typedAdd() throws IOException
625 {
626 print( "typedAdd", null );
627 }
628
629 public void typedLessThan() throws IOException
630 {
631 print( "typedLessThan", null );
632 }
633
634 public void modulo() throws IOException
635 {
636 print( "modulo", null );
637 }
638
639 public void bitAnd() throws IOException
640 {
641 print( "bitAnd", null );
642 }
643
644 public void bitOr() throws IOException
645 {
646 print( "bitOr", null );
647 }
648
649 public void bitXor() throws IOException
650 {
651 print( "bitXor", null );
652 }
653
654 public void shiftLeft() throws IOException
655 {
656 print( "shiftLeft", null );
657 }
658
659 public void shiftRight() throws IOException
660 {
661 print( "shiftRight", null );
662 }
663
664 public void shiftRightUnsigned() throws IOException
665 {
666 print( "shiftRightUnsigned", null );
667 }
668
669 public void decrement() throws IOException
670 {
671 print( "decrement", null );
672 }
673
674 public void increment() throws IOException
675 {
676 print( "increment", null );
677 }
678
679 public void enumerateObject() throws IOException {
680 print( "enumerateObject", null );
681 }
682
683 public void greaterThan() throws IOException {
684 print( "greaterThan", null );
685 }
686
687 public void instanceOf() throws IOException {
688 print( "instanceOf", null );
689 }
690
691 public void strictEquals() throws IOException {
692 print( "strictEquals", null );
693 }
694
695 public void stringGreaterThan() throws IOException {
696 print( "greaterThan", null );
697 }
698
699 }
700