当我使用context.tryCreateResource创建一个新文件时,为什么GWT生成器不会将我的文件保存到磁盘?

时间:2013-03-06 16:34:25

标签: java gwt

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

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

我的函数创建了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文件了 延迟绑定失败。

1 个答案:

答案 0 :(得分:-2)

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