KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > SWFUtils


1 /******************************************************************************
2  * SWFUtils.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.utils;
11
12 import java.awt.geom.Rectangle2D JavaDoc;
13 import java.awt.geom.AffineTransform JavaDoc;
14
15 import java.io.*;
16
17 import org.openlaszlo.server.LPS;
18 import org.openlaszlo.iv.flash.api.*;
19 import org.openlaszlo.iv.flash.api.action.*;
20 import org.openlaszlo.iv.flash.api.image.*;
21 import org.openlaszlo.iv.flash.api.sound.*;
22 import org.openlaszlo.iv.flash.api.text.*;
23 import org.openlaszlo.iv.flash.util.*;
24 import org.openlaszlo.iv.flash.cache.*;
25
26 import org.apache.log4j.*;
27
28 /**
29  * Utility class for http servlets
30  */

31 public class SWFUtils {
32
33     private static Logger mLogger = Logger.getLogger(SWFUtils.class);
34
35     /**
36      * @return stream
37      *
38      * FIXME: [2003-05-04 bloch] turn color into a property.
39      */

40     public static InputStream getErrorMessageSWF(String JavaDoc message) {
41         String JavaDoc w = LPS.getProperty("swf.error.msg.width", "400");
42         String JavaDoc h = LPS.getProperty("swf.error.msg.height", "200");
43         String JavaDoc p = LPS.getProperty("swf.error.msg.pixels", "14");
44
45         int TWIP = 20;
46
47         int wd = Integer.parseInt(w);
48         int ht = Integer.parseInt(h);
49         int pi = Integer.parseInt(p);
50
51         try {
52             FlashFile file = FlashFile.newFlashFile();
53             Script script = new Script(1);
54             script.setMain();
55             file.setMainScript(script);
56             Frame frame = script.newFrame();
57             String JavaDoc fontFileName = LPS.getMiscDirectory() +
58                 File.separator + "vera.fft";
59                          
60             Font font = FontDef.load(fontFileName, file);
61             Text text = Text.newText();
62             TextItem item = new TextItem(message, font, pi*TWIP, AlphaColor.black);
63             text.addTextItem(item);
64             text.setBounds(GeomHelper.newRectangle(0, 0, wd*TWIP, ht*TWIP));
65             frame.addInstance(text, 1, new AffineTransform JavaDoc(), null);
66             return file.generate().getInputStream();
67         } catch (Exception JavaDoc e) {
68             // If this fails, we're screwed
69
return null;
70         }
71     }
72
73     /**
74      * @return true if the input stream has a valid SWF header
75      */

76     public static boolean hasSWFHeader(InputStream is) throws IOException {
77         // Code snarfed from jgenerator parser.Parser
78
//
79
byte[] fileHdr = new byte[8];
80
81         if( is.read(fileHdr, 0, 8) != 8 ) {
82             return false;
83         }
84
85         if( fileHdr[1] != 'W' || fileHdr[2] != 'S' ) {
86             return false;
87         }
88
89         // FIXME: [2003-09-20 bloch]
90
// Support compressed even though we don't fullly allow it everywhere
91
if( fileHdr[0] != 'C' && fileHdr[0] != 'F' ) {
92             return false;
93         }
94
95         return true;
96     }
97 }
98
Popular Tags