1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //  ADOBE SYSTEMS INCORPORATED
4 //  Copyright 2006-2007 Adobe Systems Incorporated
5 //  All Rights Reserved.
6 //
7 //  NOTICE: Adobe permits you to use, modify, and distribute this file
8 //  in accordance with the terms of the license agreement accompanying it.
9 //
10 ////////////////////////////////////////////////////////////////////////////////
11 
12 package flex.ant;
13 
14 import java.io.BufferedReader;
15 import java.io.BufferedInputStream;
16 import java.io.BufferedOutputStream;
17 import java.io.FileOutputStream;
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.PrintWriter;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.DynamicAttribute;
26 import org.apache.tools.ant.Task;
27 
28 /**
29  * Implements the <html-wrapper> Ant task.  For example:
30  * <pre>
31  * &lt;html-wrapper title="foo"
32  *             height="100"
33  *             width="100"
34  *             bgcolor="red"
35  *             application="bar"
36  *             swf="bar"
37  *             version-major="9"
38  *             version-minor="0"
39  *             version-revision="0"
40  *             history="true"
41  *             template="client-side-detection"
42  *             output="client-side-detection-with-history"/&gt;
43  * </pre>
44  */
45 public final class HtmlWrapperTask extends Task implements DynamicAttribute
46 {
47     private static final String TEMPLATE_DIR = "/templates/swfobject/";
48     private static final String INDEX_TEMPLATE_HTML = "index.template.html";
49     private static final String SWFOBJECT_JS = "swfobject.js";
50     private static final String HISTORY_CSS = "history/history.css";
51     private static final String HISTORY_JS = "history/history.js";
52     private static final String HISTORY_FRAME_HTML = "history/historyFrame.html";
53     private static final String PLAYERPRODUCTINSTALL_SWF = "playerProductInstall.swf";
54     private static final String HTML_COMMENT_DELIMITER = "--";
55 
56     private String application;
57     private String bgcolor = "white";
58     private String fileName = "index.html";
59     private String height = "400";
60     private String output;
61     private String swf;
62     private String title = "Flex Application";
63     private String width = "400";
64     private boolean history = false;
65     private boolean expressInstall = false;
66     private boolean versionDetection = true;
67 
68     // The various settings of version, browser history and express install
69     // determine how the generated template behaves - maps to the 6 template
70     // flavors that existed for Flex 3 and earlier.
71     private String versionMajor = "10";
72     private String versionMinor = "0";
73     private String versionRevision = "0";
74     private String useBrowserHistory;
75     private String expressInstallSwf = "";
76 
HtmlWrapperTask()77     public HtmlWrapperTask()
78     {
79         setTaskName("html-wrapper");
80     }
81 
execute()82     public void execute() throws BuildException
83     {
84         // Check for requirements.
85         if (swf == null)
86         {
87             throw new BuildException("The <html-wrapper> task requires the 'swf' attribute.", getLocation());
88         }
89 
90         InputStream inputStream = getInputStream();
91 
92         if (inputStream != null)
93         {
94             BufferedReader bufferedReader = null;
95             PrintWriter printWriter = null;
96             String path = null;
97 
98             try
99             {
100                 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
101 
102                 if (output != null)
103                 {
104                     File outputDir = new File(output);
105                     if (outputDir.exists() && outputDir.isDirectory())
106                     {
107                         path = output + File.separatorChar + fileName;
108                     }
109                     else
110                     {
111                         String base = getProject().getBaseDir().getAbsolutePath();
112                         outputDir = new File(base + File.separatorChar + output);
113                         if (outputDir.exists() && outputDir.isDirectory())
114                         {
115                             path = base + File.separatorChar + output + File.separatorChar + fileName;
116                         }
117                         else
118                         {
119                             throw new BuildException("output directory does not exist: " + output);
120                         }
121                     }
122                 }
123                 else
124                 {
125                     path = fileName;
126                 }
127 
128                 printWriter = new PrintWriter(new FileWriter(path));
129 
130                 String line;
131 
132                 while ((line = bufferedReader.readLine()) != null)
133                 {
134                     printWriter.println(substitute(line));
135                 }
136             }
137             catch (IOException ioException)
138             {
139                 System.err.println("Error outputting resource: " + path);
140                 ioException.printStackTrace();
141             }
142             finally
143             {
144                 try
145                 {
146                     bufferedReader.close();
147                     printWriter.close();
148                 }
149                 catch (Exception exception)
150                 {
151                 }
152             }
153         }
154         else
155         {
156             throw new BuildException("Missing resources", getLocation());
157         }
158     }
159 
getInputStream()160     private InputStream getInputStream()
161     {
162         InputStream inputStream;
163         String[] resources;
164 
165         if (history)
166         {
167         	if(expressInstall)
168         	{
169         		expressInstallSwf = PLAYERPRODUCTINSTALL_SWF;
170         		resources = new String[] {SWFOBJECT_JS, HISTORY_FRAME_HTML, HISTORY_JS, HISTORY_CSS, PLAYERPRODUCTINSTALL_SWF};
171         		versionDetection = true;
172         	}
173         	else
174         	{
175         		resources = new String[] {SWFOBJECT_JS, HISTORY_FRAME_HTML, HISTORY_JS, HISTORY_CSS};
176         	}
177 
178             useBrowserHistory = HTML_COMMENT_DELIMITER;
179         }
180         else
181         {
182         	if(expressInstall)
183         	{
184         		expressInstallSwf = PLAYERPRODUCTINSTALL_SWF;
185         		resources = new String[] {SWFOBJECT_JS, PLAYERPRODUCTINSTALL_SWF};
186         		versionDetection = true;
187         	}
188         	else
189         	{
190         		resources = new String[] {SWFOBJECT_JS};
191         	}
192 
193             useBrowserHistory = "";
194         }
195 
196         if(!versionDetection)
197         {
198             // no version checking
199             versionMajor = "0";
200             versionMinor = "0";
201             versionRevision = "0";
202             // don't install flash.
203             expressInstallSwf = "";
204         }
205 
206         inputStream = getClass().getResourceAsStream(TEMPLATE_DIR + INDEX_TEMPLATE_HTML);
207         outputResources(TEMPLATE_DIR, resources);
208 
209         return inputStream;
210     }
211 
outputResources(String resourceDir, String[] resources)212     private void outputResources(String resourceDir, String[] resources)
213     {
214         BufferedInputStream bufferedInputStream = null;
215         BufferedOutputStream bufferedOutputStream = null;
216 
217         for (int i = 0; i < resources.length; i++)
218         {
219             try
220             {
221                 InputStream inputStream = getClass().getResourceAsStream(resourceDir + resources[i]);
222                 bufferedInputStream = new BufferedInputStream(inputStream);
223                 String path = null;
224 
225                 if (output != null)
226                 {
227                     File outputDir = new File(output);
228                     if (outputDir.exists() && outputDir.isDirectory())
229                     {
230                         path = output + File.separatorChar + resources[i];
231                     }
232                     else
233                     {
234                         String base = getProject().getBaseDir().getAbsolutePath();
235                         outputDir = new File(base + File.separatorChar + output);
236                         if (outputDir.exists() && outputDir.isDirectory())
237                         {
238                             path = base + File.separatorChar + output + File.separatorChar + resources[i];
239                         }
240                         else
241                         {
242                             throw new BuildException("output directory does not exist: " + output);
243                         }
244                     }
245                 }
246                 else
247                 {
248                     path = resources[i];
249                 }
250 
251                 File file = new File(path);
252                 file.getParentFile().mkdirs();
253 
254                 bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
255 
256                 byte byteArr[]=new byte[8192];
257 
258                 int len;
259                 while ((len=bufferedInputStream.read(byteArr, 0, 8192))!=-1)
260                 {
261                     bufferedOutputStream.write(byteArr, 0, len);
262                 }
263             }
264             catch (IOException ioException)
265             {
266                 System.err.println("Error outputting resource: " + resources[i]);
267                 ioException.printStackTrace();
268             }
269             finally
270             {
271                 try
272                 {
273                     bufferedOutputStream.close();
274                     bufferedInputStream.close();
275                 }
276                 catch (Exception exception)
277                 {
278                 }
279             }
280         }
281     }
282 
setApplication(String application)283     public void setApplication(String application)
284     {
285         this.application = application;
286     }
287 
setBgcolor(String bgcolor)288     public void setBgcolor(String bgcolor)
289     {
290         this.bgcolor = bgcolor;
291     }
292 
setDynamicAttribute(String name, String value)293     public void setDynamicAttribute(String name, String value)
294     {
295         if (name.equals("version-major"))
296         {
297             versionMajor = value;
298         }
299         else if (name.equals("version-minor"))
300         {
301             versionMinor = value;
302         }
303         else if (name.equals("version-revision"))
304         {
305             versionRevision = value;
306         }
307         else if (name.equals("express-install"))
308         {
309             expressInstall = Boolean.parseBoolean(value);
310         }
311         else if (name.equals("version-detection"))
312         {
313         	versionDetection = Boolean.parseBoolean(value);
314         }
315         else
316         {
317             throw new BuildException("The <html-wrapper> task doesn't support the \""
318                                      + name + "\" attribute.", getLocation());
319         }
320     }
321 
setFile(String fileName)322     public void setFile(String fileName)
323     {
324         this.fileName = fileName;
325     }
326 
setHeight(String height)327     public void setHeight(String height)
328     {
329         this.height = height;
330     }
331 
setHistory(boolean history)332     public void setHistory(boolean history)
333     {
334         this.history = history;
335     }
336 
setOutput(String output)337     public void setOutput(String output)
338     {
339         this.output = output;
340     }
341 
setSwf(String swf)342     public void setSwf(String swf)
343     {
344         // Doctor up backslashes to fix bug 193739.
345         this.swf = swf.replace('\\', '/');
346         if (application == null)
347         {
348             application = this.swf;
349         }
350     }
351 
setTitle(String title)352     public void setTitle(String title)
353     {
354         this.title = title;
355     }
356 
setWidth(String width)357     public void setWidth(String width)
358     {
359         this.width = width;
360     }
361 
substitute(String input)362     private String substitute(String input)
363     {
364         String result = input.replaceAll("\\$\\{application\\}", application);
365         result = result.replaceAll("\\$\\{bgcolor\\}", bgcolor);
366         result = result.replaceAll("\\$\\{expressInstallSwf\\}", expressInstallSwf);
367         result = result.replaceAll("\\$\\{height\\}", height);
368         result = result.replaceAll("\\$\\{swf\\}", swf);
369         result = result.replaceAll("\\$\\{title\\}", title);
370         result = result.replaceAll("\\$\\{version_major\\}", versionMajor);
371         result = result.replaceAll("\\$\\{version_minor\\}", versionMinor);
372         result = result.replaceAll("\\$\\{version_revision\\}", versionRevision);
373         result = result.replaceAll("\\$\\{width\\}", width);
374         result = result.replaceAll("\\$\\{useBrowserHistory\\}", useBrowserHistory);
375         return result;
376     }
377 }
378