• 【设为首页】
  • 【收藏闪客居】
当前位置:主页>Flex>文章内容
  • Flex compiler API指南(第一章)
  • 来源:e时代的狂想 作者:不会飞的鱼 2008-06-21 【

        本文介绍了 Adobe(R) Flex(TM) compiler API, 它是OEM Kit的一部分。文章的“快速开始”部分包含了一个充分的例子,让您能够轻松上路。

关于Flex compiler API

        Flex complier API是Flex OEM Kit的一部分。它可以让您使用JAVA应用程序去编译Flex应用程序。您也可以在内存中建立应用程序并编译它们,使其生成SWF文件,甚至不用在磁盘 上建立MXML文件。此外,Flex  compiler API 能够为您输出该Flex应用程序的关联报告和其他详细信息。

       您也可以使用Flex  compiler API 建立项目文件和库文件。库文件的格式为SWC,它里面定义了在应用程序中使用的一系列组件、主题文件或RSL(Runtime Shared Libraries)资源。项目内集合了Flex应用程序和Flex库。这加强了Flex应用程序及其周边资源(assets)间在编译的过程中的依赖 性。

Flex complier API的基本要求

        Flex compiler API需要具备如下条件:

  •         许可证:您必须拥有Flex Data Service 2的License才能拥有使用包括Flex complier API在内的Flex OEM Kit中任何一部分的权力。
  •         JDK:Flex compiler API需要1.4.2及以上版本的JAVA解释器和javac编译器
  •         Flex: Flex compiler API 不是一个独立的单机项目. 它需要Flex SDK 或者包含有Flex Data Service 的SDK。当然你也可以使用Flex Builder.。

    Flex Complier API内都包含了什么  

        Flex compiler API 是一个ZIP文件,里面包含了下面的资源:

  • flex-compiler-oem.jar:  Flex complier API中的flex2.tools.oem.* API。这个 JAR 文件是在ZIP文件中的 /lib 文件夹下. 当您展开ZIP文件, 您应该把这个文件放入您的 /lib 文件夹下。
  • JavaDocs:flex2.tools.oem.* 包中的公有类和接口的 API 文档。这些文件在ZIP文件中的/api文件夹下。
  • README.txt: 帮助文件是在ZIP文件的最顶层。它包括安装说明和使用Flex complier API临时许可证的信息。此外,您可以另行下载下面的文件:
  • CompilerAPIUserGuide.pdf:Flex 2 Compiler API User Guide 是一个PDF文件,它提供了API的使用文档这个文件在ZIP文件的最顶层。

快速开始

下面的例子描述了如何去创建一个简单的JAVA应用程序去编译Flex应用:

用Flex compiler API创建并编译一个新的应用程序

 1. 建立一个JAVA文件;例如MyAppCompiler.java:


  1. // java/MyAppCompiler.java  
  2.   
  3. import flex2.tools.oem.Application;  
  4.   
  5. import java.io.*;  
  6.   
  7. public class MyAppCompiler {  
  8.   
  9.     public static void main(String[] args) {  
  10.   
  11.         try {  
  12.   
  13.             Application application = new Application(new   
  14.   
  15.                 File("../apps/TestApp.mxml"));  
  16.   
  17.             application.setOutput(new   
  18.   
  19.                 File("../apps/TestApp.swf"));  
  20.   
  21.             long result = application.build(true);                
  22.   
  23.             if (result > 0) {  
  24.   
  25.                 System.out.println("COMPILE OK");              
  26.   
  27.             } else {  
  28.   
  29.                 System.out.println("COMPILE FAILED");  
  30.   
  31.             }  
  32.   
  33.         } catch (Exception ex) {  
  34.   
  35.             ex.printStackTrace();  
  36.   
  37.         }  
  38.   
  39.     }  
  40.   
  41. }  

1、在这个文件里,确保如下几点:

a. 建立一个Application对象。 b. 为Application对象设置一个输出文件。 c. 调用Application.build() 方法。 d. 检查build()方法的返回是是否大于0。

2、用您的JAVA编译器编译这个类;例如: C:\myapps>javac -classpath c:\home\dev\depot\flex\sdk\lib\flex-compiler-oem.jar MyAppCompiler.java 确保flex-compiler-oem.jar 已经加入到您的 Java classpath中。

3. 创建一个MXML 文件;例如TestApp.mxml:


  1. <?xml version="1.0"?>  
  2.   
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">  
  4.   
  5. <mx:Label text="Hello World"/>  
  6.   
  7. </mx:Application>  
4、用Java解释器运行一个新的Flex应用程序编译器,例如:
C:\myapps>java -classpath c:\home\dev\depot\flex\sdk\lib\flex-compiler-
oem.jar;. MyAppCompiler
确保flex-compiler-oem.jar和当前文件夹加入到您的Java classpath中。
下面的例子使用Configuration类的方法设置了一些配置选项:

  1. // java/MyConfiguringCompiler.java  
  2. import flex2.tools.oem.Application;  
  3. import flex2.tools.oem.Configuration;  
  4. import java.io.*;  
  5. public class MyConfiguringCompiler {  
  6.     public static void main(String[] args) {  
  7.         String outputRoot = "../apps/";  
  8.         try {  
  9.             Application application = new Application(new   
  10.                 File(outputRoot, "ErrorTestApp.mxml"));  
  11.             application.setOutput(new   
  12.                 File(outputRoot, "ErrorTestApp.swf"));  
  13.                   
  14.             Configuration config = application.getDefaultConfiguration();  
  15.             // Enable ActionScript optimizer.  
  16.             config.optimize(true);  
  17.             // Disable warnings.              
  18.             config.showActionScriptWarnings(false);  
  19.             config.showBindingWarnings(false);  
  20.             config.showDeprecationWarnings(false);  
  21.             config.showUnusedTypeSelectorWarnings(false);  
  22.             // Apply the new configuration to the Application.  
  23.             application.setConfiguration(config);  
  24.             long result = application.build(true);     
  25.             if (result > 0) {  
  26.                 System.out.println("COMPILE OK");              
  27.             } else {  
  28.                 System.out.println("COMPILE FAILED");  
  29.             }  
  30.         } catch (Exception ex) {  
  31.             ex.printStackTrace();  
  32.         }  
  33.     }  
  34. }  

配置编译器

        使用Flex complier API时您可以把编译选项传给编译器。您可以用Application或者Library类的getDefaultConfiguration() 方法来得到一个Configuration类的实例,然后在这个实例上设置选项,然后用setConfiguration() 方法设回到Application或Library中。

        如果要设置更多的选项,在the flex2.tools.oem.Configuration API 方法和配置选项之间的关系是1:1映射的。

例如,设置keep-generated-actionscript 编译选项为true,您可以为Configuration类的keepCompilerGeneratedActionScript()方法设置true值:

config.keepCompilerGeneratedActionScript(true);

        一些编译选项像source-path 和 library-path可以用+= 操作符去追加source path 和library path实体。使用这些编译选项时您可以用setSourcePath() 和setLibraryPath() 方法替换掉整个路径。或者,您可以使用addSourcePath() 和 addLibraryPath() 方法给这些path列表里添加path实体。

        有的情况下,Configuration 类可能没有方法对应编译选项。您就需要调用其他的类方法或者调用多个方法。比如,在warnings编译选项中并没有方法允许或者禁止所有的警告,您可以 用showActionScriptWarnings(), showBindingWarnings(), showDeprecationWarnings(), 和showUnusedTypeSelectorWarnings() 方法代替。下面的表格列出了这些可选的编译选项的设置方法:

编译选项

等价的complier API或方法

dump-config

调用Configuration.keepConfigurationReport(true) 方法然后调用Report.writeConfigurationReport()方法

include-classes 调用Library.addComponent(java.lang.String)方法
include-file 调用Library.addArchiveFile()方法
include-namespaces 调用Library.addComponent(java.net.URI) 方法
include-sources 调用Library.addComponent(VirtualLocalFile) 或者Library.addComponent(java.io.File) 方法
library-path

调用Configuration.setLibraryPath() 方法替换source path的默认值。调用Configuration.addLibraryPath() 方法为默认的source path追加新值。

link-report

调用Configuration.keepLinkReport(true) 方法,然后调用 Report.writeLinkReport()方法。

output 调用 Application.setOutput() 和 Library.setOutput()/setDirectory()方法
resource-bundle-list 调用 Report.getResourceBundleNames() 方法.
source-path

调用 Configuration.setSourcePath()方法替换source path的默认值。调用Configuration.addSourcePath() 方法为默认的source path追加新值。

version 调用 Report.getCompilerVersion() 方法
warnings 调用showActionScriptWarnings(), showBindingWarnings(), showDeprecationWarnings(), 和showUnusedTypeSelectorWarnings() 方法

关于选项和属性的优先级

       像mxmlc 和 compc这些命令, 默认的Flex compiler API配置使用config.xml文件。为了编译应用程序,编译器也会使用本地配置文件。于是Flex compiler API在flex2.tools.oem.Configuration 应用配置选项来创建最终的编译配置选项组。默认情况下,在flex-config.xml 文件里,编译器被Flex compiler API用编译选项组装默认的配置对象调用。这个文件的位置相对于mxmlc.jar文件的位置。大多情况下,这个文件是在框架目录下, 跟 JAR 文件的lib路径处于同级目录下。编译器也会使用本地配置文件(例如app_name-config.xml),或者您可以使用 addConfiguration() 指向其他位置的配置文件。您使用Flex compiler API 配置的配置选项作为命令行编译选项,拥有相同的优先级。这意味着它们比所有其他形式的选项(flex-config.xml,可选的配置文件, 本地配置文件)的优先级都要高。想查看更多关于使用配置文件的信息请查阅《Building and Deploying Flex 2 Applications》 第二章 “Using the Flex Compilers”。

清除配置

        您可以使用Application.setConguration(null) 方法清除配置。

为应用程序加入资源(assets)

        您可以使用Configuration类中的方法为应用程序加入资源(assets),例如,addExterns(), addIncludes(), 和setTheme()。使用这些方法,您可以加入外部的主题(external themes),类库, 类, RSLs, 其他类型的资源。

       下面的例子使用Configuration类中的setTheme() 方法为现有应用程序加入主题:


  1. // java/MyThemeCompiler.java  
  2.   
  3. import flex2.tools.oem.Application;  
  4.   
  5. import flex2.tools.oem.Configuration;  
  6.   
  7. import java.io.*;  
  8.   
  9. public class MyThemeCompiler {  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         String assetRoot = "../assets/";  
  14.   
  15.         String outputRoot = "../apps/";  
  16.   
  17.         try {  
  18.   
  19.             File[] themeFile = new File[]   
  20.   
  21.                 {new File(assetRoot, "myTheme.css")};  
  22.   
  23.             Application application =   
  24.   
  25.                 new Application(new File(outputRoot,   
  26.   
  27.                 "TestAppWithAssets.mxml"));  
  28.   
  29.             application.setOutput(new File(outputRoot,  
  30.   
  31.                 "TestAppWithAssets.swf"));  
  32.   
  33.             Configuration config = application.getDefaultConfiguration();  
  34.   
  35.             config.setTheme(themeFile);              
  36.   
  37.             application.setConfiguration(config);  
  38.   
  39.             long result = application.build(true);     
  40.   
  41.             if (result > 0) {  
  42.   
  43.                 System.out.println("COMPILE OK");              
  44.   
  45.             } else {  
  46.   
  47.                 System.out.println("COMPILE FAILED");  
  48.   
  49.             }  
  50.   
  51.         } catch (Exception ex) {  
  52.   
  53.             ex.printStackTrace();  
  54.   
  55.         }  
  56.   
  57.     }  
  58.   
  59. }  

        在下面的例子中,使用了addLibraryPath()方法加入包含组件的SWC文件,资源(assets)目录 包含了MyComponents.swc 文件:


  1. // java/MyLibraryPathCompiler.java  
  2.   
  3. import flex2.tools.oem.Application;  
  4.   
  5. import flex2.tools.oem.Configuration;  
  6.   
  7. import java.io.*;  
  8.   
  9. public class MyLibraryPathCompiler {  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         String assetRoot = "../assets/";  
  14.   
  15.         String outputRoot = "../apps/";  
  16.   
  17.         try {  
  18.   
  19.             Application application = new Application(new   
  20.   
  21.                 File(outputRoot, "TestAppWithAllAssets.mxml"));  
  22.   
  23.             application.setOutput(new   
  24.   
  25.                 File(outputRoot, "TestAppWithAllAssets.swf"));  
  26.   
  27.             Configuration config = application.getDefaultConfiguration();  
  28.   
  29.             File[] libFile = new File[]   
  30.   
  31.                 {new File(assetRoot, "MyComponents.swc")};  
  32.   
  33.             config.addLibraryPath(libFile);  
  34.   
  35.             application.setConfiguration(config);  
  36.   
  37.             long result = application.build(true);     
  38.   
  39.             if (result > 0) {  
  40.   
  41.                 System.out.println("COMPILE OK");              
  42.   
  43.             } else {  
  44.   
  45.                 System.out.println("COMPILE FAILED");  
  46.   
  47.             }  
  48.   
  49.         } catch (Exception ex) {  
  50.   
  51.             ex.printStackTrace();  
  52.   
  53.         }  
  54.   
  55.     }  
  56.   
  57. }  
    在下面的例子中,使用了addSourcePath() 方法加入包含MXML或不在SWC中的ActionScript组件文件的目录。
资源(assets)文件夹中包含了一些在TestSourcePathApp 应用程序中使用的MXML文件:

  1. // java/MySourcePathAppCompiler.java  
  2. import flex2.tools.oem.Application;  
  3. import flex2.tools.oem.Configuration;  
  4. import java.io.*;  
  5. public class MySourcePathAppCompiler {  
  6.     public static void main(String[] args) {  
  7.         String assetRoot = "../assets/";  
  8.         String outputRoot = "../apps/";  
  9.         try {  
  10.             File[] sourcePath = new File[] {new File(assetRoot)};  
  11.             Application application = new Application(new   
  12.                 File(outputRoot, "TestSourcePathApp.mxml"));  
  13.             application.setOutput(new   
  14.                 File(outputRoot, "TestSourcePathApp.swf"));  
  15.               
  16.             Configuration config = application.getDefaultConfiguration();  
  17.             // The source path can be a directory.   
  18.             // All MXML and AS files in that directory are added   
  19.             // to the source path.  
  20.             config.addSourcePath(sourcePath);  
  21.             application.setConfiguration(config);  
  22.             long result = application.build(true);     
  23.             if (result > 0) {  
  24.                 System.out.println("COMPILE OK");              
  25.             } else {  
  26.                 System.out.println("COMPILE FAILED");  
  27.             }  
  28.         } catch (Exception ex) {  
  29.             ex.printStackTrace();  
  30.         }  
  31.     }  
  32. }  
    到此,您也可以使用Flex complier API创建库或组件了。



上一篇:Flex中的常见的效果   下一篇:Flex启动过程分析
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)