Flex compiler API指南(第二章)

来源:互联网 发布:icloud和阿里云 编辑:程序博客网 时间:2024/06/02 00:38

第二章 日志和报告

    Adobe Flex Complier API为您生成报告(Reports) 并提供在编译过程中的进度和日志等信息。

   使用自定义日志

    您可以使用编译器获取错误信息。您可以自定义一个logger并为这个application指派logger,告诉这个application您正在编译。

   使用自定义日志的具体步骤如下:

    1、新建一个JAVA类,实现flex2.tools.oem.Logger接口;例如:

   

// java/SimpleLogger.javaimport flex2.tools.oem.Message;import flex2.tools.oem.Logger;import java.util.*;  public class SimpleLogger implements Logger {    SimpleLogger() {        System.out.println("----------------------------------");    }    public void log(Message msg, int errorCode, String source) {        System.out.println(msg);        System.out.println("----------------------------------");    }}

    这个类必须实现log方法,它有3个参数:message,errorCode和source。

    2、在您的JAVA应用里,调用接口的setLogger()方法为这个Applocation指派logger;例如:

    application.setLogger(new SimpleLogger());

    如果您没有调用setLogger()方法,则编译器记录log信息到标准输出。

    3、编译并运行例子

    4、测试这个logger,在您的例子中加入一些错误的语法;例如:

<?xml version="1.0"?><!-- apps/ErrorTestApp.mxml --><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">    <mx:Script>        // Generates a warning because there is no return type.        public function doSomethingWrong() {            // Generates an error because x lacks a type.            var x;        }    </mx:Script>    <mx:Label text="Hello World"/></mx:Application>

    例子可能输入如下信息:

----------------------------------ERROR: Loading configuration file C:/home/dev/depot/flex/sdk/frameworks/flex-config.xml--------------------------------------------------------------------ERROR: return value for function 'doSomethingWrong' has no type declaration.----------------------------------ERROR: variable 'x' has no type declaration.----------------------------------

    使用消息类中的getLevel()、getPath(),getLine()和getColumn(),您可以获得其他信息,比如错误级别(error level)和错误地点(location of error),下面的例子比上面的例子输出了更多的信息:

   

// java/ComplexLogger.javaimport flex2.tools.oem.Message;import flex2.tools.oem.Logger;import java.util.*;import java.io.*;  public class ComplexLogger implements Logger {    ComplexLogger() {        String LOGFILENAME = "output.txt";        try {            System.setOut(new PrintStream(new                 FileOutputStream(LOGFILENAME)));        } catch (Exception e) {             System.out.println("There was an error creating                 the log file.");        }        System.out.println("Ran at : " + new Date());        System.out.println("----------------------------------");    }    public void log(Message msg, int errorCode, String source) {        if (msg.getLevel() == "info") {            // Suppress info messages.        } else {            System.out.println("ERROR  : " + errorCode);            System.out.println("MESSAGE: " + msg);            System.out.println("SOURCE : " + source);            System.out.println("LEVEL  : " + msg.getLevel());            System.out.println("PATH   : " + msg.getPath());            System.out.println("LINE   : " + msg.getLine());            System.out.println("COLUMN : " + msg.getColumn());            System.out.println("----------------------------------");        }    }}

    不是所有的编译错误都有明确的解释。例如,在您的MXML代码中发生错误,就没有错误码,所以Logger返回 -1 。 而AS代码,无论何时,都会返回错误代码。

    您也可以使用Report Class在编译正在执行的时候查看Message对象。

   创建报告

    Flex compiler API包括了对application、library或正在编译的项目创建报告的能力。在报告中,您可以包含应用的详细信息,信息包括链接、资源、依赖、库和资源绑定(resource bundle)等,也包括背景颜色,高度,宽度等应用程序的增量信息。

    您可以用Application和Library对象的getReport()方法来获得报告。这个方法返回一个Report类实例,使用它就可以输出关于编译目标的信息。

    在您使用Report类生成报告之前,您必须构建应用程序或库,这意味着您必须在调用任何报告t方法之前调用build()方法。

    下面的例子将输出报告信息到标准输出:

   

// java/MyReportCompiler.javaimport flex2.tools.oem.Application;import flex2.tools.oem.Report;import flex2.tools.oem.Configuration;import flex2.tools.oem.Logger;import java.io.*;public class MyReportCompiler {    public static void main(String[] args) {        String assetRoot = "../assets/";        String outputRoot = "../apps/";        File[] themeFile = new File[]             {new File(assetRoot, "myTheme.css")};        File[] libFile = new File[]             {new File(assetRoot, "MyComponents.swc")};        try {            Application application = new                 Application(new File(outputRoot,                 "TestAppWithAllAssets.mxml"));            application.setOutput(new File(outputRoot,                 "TestAppWithAllAssets.swf"));                        application.setLogger(new ComplexLogger());            application.setProgressMeter(new MyProgressMeter());            Configuration config = application.getDefaultConfiguration();            config.setTheme(themeFile);             config.addLibraryPath(libFile);            application.setConfiguration(config);            application.build(true);                          Report report = application.getReport();            // Lists the image files that are embedded.            System.out.println("/n/nEMBEDDED ASSETS: ");            String[] cnames = report.getAssetNames(Report.COMPILER);            for (int i=0; i<cnames.length; i++) {                System.out.println(cnames[i]);                     }            // Lists the libraries that are used.            System.out.println("/nLIBRARIES: ");            String[] libs = report.getLibraryNames(Report.COMPILER);            for (int i=0; i<libs.length; i++) {                System.out.println(libs[i]);                     }                        // Lists source files, their definition names, and dependencies.            System.out.println("/nSOURCE NAMES: ");            String[] list = report.getSourceNames(Report.COMPILER);            for (int i=0; i<list.length; i++) {                System.out.println(list[i]);                         String[] defs = report.getDefinitionNames(list[i]);                System.out.println("DEFINITIONS: ");                for (int j=0; j<defs.length; j++) {                    System.out.println(defs[j]);                                                 System.out.println("   DEPENDENCIES: ");                    String[] deps = report.getDependencies(defs[j]);                    for (int k=0; k<deps.length; k++) {                        System.out.println("      " + deps[k]);                             }                    System.out.println("   PREREQS: ");                    String[] prereqs = report.getPrerequisites(defs[j]);                    for (int k=0; k<prereqs.length; k++) {                        System.out.println("      " + prereqs[k]);                             }                }            }               // Get application info.            System.out.println("/nAPPLICATION INFO: ");            System.out.println("   Background Color: " +                 "0x" + Integer.toHexString(report.getBackgroundColor()).                    toUpperCase());            System.out.println("   Height          : " +                 report.getDefaultHeight() +                 " (" +                 Math.round(100 * report.getHeightPercent()) +                 "%)");            System.out.println("   Width           : " +                 report.getDefaultWidth() +                 " (" +                 Math.round(100 * report.getWidthPercent()) +                 "%)");            System.out.println("   Page Title      : " +                 report.getPageTitle());        } catch (Exception ex) {            ex.printStackTrace();        }    }}

    输出信息大体如下:

EMBEDDED ASSETS:C:/home/depot/EN/Docs/Flex/Flex2next/oem_kit/code/assets/bird-gray.gifC:/home/depot/EN/Docs/Flex/Flex2next/oem_kit/code/assets/bird-silly.gifC:/home/depot/EN/Docs/Flex/Flex2next/oem_kit/code/assets/bird.gifLIBRARIES:C:/home/depot/EN/Docs/Flex/Flex2next/oem_kit/code/assets/MyComponents.swcC:/home/dev/depot/flex/sdk/bundles/en_US/charts_rb.swcC:/home/dev/depot/flex/sdk/bundles/en_US/framework_rb.swcC:/home/dev/depot/flex/sdk/frameworks/libs/flex.swcC:/home/dev/depot/flex/sdk/frameworks/libs/framework.swcC:/home/dev/depot/flex/sdk/frameworks/libs/playerglobal.swcSOURCE NAMES:C:/home/depot/EN/Docs/Flex/Flex2next/oem_kit/code/apps/TestAppWithAllAssets.mxmlDEFINITIONS:TestAppWithAllAssets   DEPENDENCIES:      AS3      MyButton      MyLabel      mx.controls:Image      mx.controls:Label      mx.core:UIComponentDescriptor      mx.core:mx_internal      mx.events:PropertyChangeEvent      mx.styles:CSSStyleDeclaration      mx.styles:StyleManager   PREREQS:      mx.core:ApplicationAPPLICATION INFO:   Background Color: 0x869CA7   Height          : 375 (75%)   Width           : 500 (100%)   Page Title      : Test App With All Assets

   使用报告查看消息(Message)

    您也可以在编译期间获得消息信息,消息级别包括信息(info),警告(warning)和错误(error),您可以使用Reoprt类的getMessages()方法访问Message对象数组:

   

// java/MySimpleReportCompiler.javaimport flex2.tools.oem.Application;import flex2.tools.oem.Report;import flex2.tools.oem.Message;import java.io.*;public class MySimpleReportCompiler {    public static void main(String[] args) {        try {            Application application = new Application(new                 File("../apps/ErrorTestApp.mxml"));            application.setOutput(new File("../apps/ErrorTestApp.swf"));            application.build(true);                          Report report = application.getReport();                       Message[] m = report.getMessages();                        for (int i=0; i<m.length; i++) {                System.out.println(m[i].getLevel().toUpperCase() +                     " MESSAGE " + i + ": " + m[i]);            }                    } catch (Exception ex) {            ex.printStackTrace();        }    }}

   

   观察进度

    Flex Complier API 包含一个进度尺,它可以让您轻松观察进度。

    1、使用进度尺:

   

// java/MyProgressMeter.javaimport flex2.tools.oem.ProgressMeter;public class MyProgressMeter implements ProgressMeter {    long before, after;    MyProgressMeter() {    }    public void start() {        before = System.currentTimeMillis();        System.out.print("begin...");    }    public void end() {        after = System.currentTimeMillis();        System.out.println("done");        System.out.println("Elapsed Time: " + (after - before) + "ms");    }    public void percentDone(int n) {        System.out.print(n + "...");    }}

    这个类必须实现start(), end() 和 persentDone()方法,这些方法加入了在编译过程中的计算时间的逻辑

    2、在编译Flex应用程序的JAVA应用中,调用setProgressMeter()方法为应用程序指派一个进度尺;例如:

        application.setProgressMeter(new MyProgressMeter());

    3、编译这个应用程序,进度尺大体输出如下:

begin...1...2...3...4...5...6...7...8...9...10...11...12...13...14...15...16...17...18...19...20...21...22...23...24...25...26...27...28...29...30...31...32...33...34...35...36...37...38...39...40...41...42...43...44...45...46...47...48...49...50...51...52...53...54...55...56...57...58...59...60...61...62...63...64...65...66...67...68...69...70...71...72...73...74...75...76...77...78...79...80...81...82...83...84...85...86...87...88...89...90...91...92...93...94...95...96...97...98...99...100...done

原创粉丝点击