1

我尝试使用 GWT 延迟绑定生成器生成一个 java 文件和一个包含许多按钮的 ui.xml 文件。

我的 java 文件被生成并写入我的 -gen 位置。但是我的-gen文件夹中没有对应的ui.xml文件,不知道为什么。

我创建 ui.xml 文件的函数如下所示:

public String generate(TreeLogger logger, GeneratorContext context,
      String typeName) throws UnableToCompleteException {

    try {
      SourceWriter sw = getSourceWriter(typeName, context, logger);
      assert(sw != null);
      // after the file got created, I write the class content
      /* ... */ UiBinder Java code

      createUiXMLFile(typeName, context, logger);

      sw.commit(logger); 
      // after this command the java class file is written is to -gen folder
      System.out.println("class '" + typeName +  "Generated' was created succesfully");
      return typeName + "Generated";
    } catch(Exception e) {
      e.printStackTrace();
      return null;
    }
}


public void createUiXMLFile(String typeName, GeneratorContext context, 
        TreeLogger logger) throws Exception {

    // gets the type given by the String typeName
    JClassType classType = context.getTypeOracle().getType(typeName);
    // gets the package in which the new class should get created
    String packageName = classType.getPackage().getName();
    // gets the name of the class without the package name
    String simpleName = classType.getSimpleSourceName();
    simpleName = simpleName + "Generated";
    // for us to see what classes were generated by this generator

    OutputStream os = context.tryCreateResource(logger, 
          packageName.replace(".", "/")+"/"+simpleName+".ui.xml");
    // it does also not work if I just use
    // context.tryCreateResource(logger, simpleName+".ui.xml");

    ByteArrayOutputStream baus = new ByteArrayOutputStream();
    // just for testing, that I wrote the XML code into the PrintWriter
    PrintWriter pw = new PrintWriter(baus);

    pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    /* the UiBinder XML code ... */
    pw.println("</ui:UiBinder>");
    pw.flush();

    FileOutputStream fout = new FileOutputStream("C:\\...\\view\\UnicodeCharViewGenerated.ui.xml");
    fout.write(baus.toByteArray());
    fout.close();

    os.write(baus.toByteArray());
    context.commitResource(logger, os);
    // even after doing the commit the file is not written to the -gen location
}

我想我忘了调用任何函数来将新文件添加到 Oracle;但我不知道我应该调用什么方法。

如果我使用 FileOutputStream 并将 ui.xml 文件直接写入我的 Eclipse 工作区,那么它就可以工作。但是,如果我将此行注释掉,编译器将找不到 .ui.xml 文件并且延迟绑定失败。

4

1 回答 1

-2

据我通过 java script(gwt) 了解,我们无法将内容(字节数组)写入系统的驱动器。

于 2013-03-06T17:21:04.387 回答