01: /******************************************************************************
02: * SWFUtils.java
03: * ****************************************************************************/package org.openlaszlo.utils;
04:
05: import java.awt.geom.Rectangle2D;
06: import java.awt.geom.AffineTransform;
07:
08: import java.io.*;
09:
10: import org.openlaszlo.server.LPS;
11: import org.openlaszlo.iv.flash.api.*;
12: import org.openlaszlo.iv.flash.api.action.*;
13: import org.openlaszlo.iv.flash.api.image.*;
14: import org.openlaszlo.iv.flash.api.sound.*;
15: import org.openlaszlo.iv.flash.api.text.*;
16: import org.openlaszlo.iv.flash.util.*;
17: import org.openlaszlo.iv.flash.cache.*;
18:
19: import org.apache.log4j.*;
20:
21: /**
22: * Utility class for http servlets
23: */
24: public class SWFUtils {
25:
26: private static Logger mLogger = Logger.getLogger(SWFUtils.class);
27:
28: /**
29: * @return stream
30: *
31: * FIXME: [2003-05-04 bloch] turn color into a property.
32: */
33: public static InputStream getErrorMessageSWF(String message) {
34: String w = LPS.getProperty("swf.error.msg.width", "400");
35: String h = LPS.getProperty("swf.error.msg.height", "200");
36: String p = LPS.getProperty("swf.error.msg.pixels", "14");
37:
38: int TWIP = 20;
39:
40: int wd = Integer.parseInt(w);
41: int ht = Integer.parseInt(h);
42: int pi = Integer.parseInt(p);
43:
44: try {
45: FlashFile file = FlashFile.newFlashFile();
46: Script script = new Script(1);
47: script.setMain();
48: file.setMainScript(script);
49: Frame frame = script.newFrame();
50: String fontFileName = LPS.getMiscDirectory()
51: + File.separator + "vera.fft";
52:
53: Font font = FontDef.load(fontFileName, file);
54: Text text = Text.newText();
55: TextItem item = new TextItem(message, font, pi * TWIP,
56: AlphaColor.black);
57: text.addTextItem(item);
58: text.setBounds(GeomHelper.newRectangle(0, 0, wd * TWIP, ht
59: * TWIP));
60: frame.addInstance(text, 1, new AffineTransform(), null);
61: return file.generate().getInputStream();
62: } catch (Exception e) {
63: // If this fails, we're screwed
64: return null;
65: }
66: }
67:
68: /**
69: * @return true if the input stream has a valid SWF header
70: */
71: public static boolean hasSWFHeader(InputStream is)
72: throws IOException {
73: // Code snarfed from jgenerator parser.Parser
74: //
75: byte[] fileHdr = new byte[8];
76:
77: if (is.read(fileHdr, 0, 8) != 8) {
78: return false;
79: }
80:
81: if (fileHdr[1] != 'W' || fileHdr[2] != 'S') {
82: return false;
83: }
84:
85: // FIXME: [2003-09-20 bloch]
86: // Support compressed even though we don't fullly allow it everywhere
87: if (fileHdr[0] != 'C' && fileHdr[0] != 'F') {
88: return false;
89: }
90:
91: return true;
92: }
93: }
|