时间:2010-07-23 12:45:42

标签: gwt

3 个答案:

答案 0 :(得分:5)

答案 1 :(得分:2)

我必须在工作的另一天做这件事。像前面的回答一样,你只需要添加一个链接器。以下是基于模板文件为Safari用户代理创建清单文件的示例。

// Specify the LinkerOrder as Post... this does not replace the regular GWT linker and runs after it.
@LinkerOrder(LinkerOrder.Order.POST)
public class GwtAppCacheLinker extends AbstractLinker {
  public String getDescription() {
    return "to create an HTML5 application cache manifest JSP template.";
  }

  public ArtifactSet link(TreeLogger logger, LinkerContext context, ArtifactSet artifacts) throws UnableToCompleteException {
    ArtifactSet newArtifacts = new ArtifactSet(artifacts);
    // search through each of the compilation results to find the one for Safari. Then 
    // generate application cache for that file
    for (CompilationResult compilationResult : artifacts.find(CompilationResult.class)) {
      // Only emit the safari version
      for (SelectionProperty property : context.getProperties()) {
        if (property.getName().equals("user.agent")) {
          String value = property.tryGetValue();
          // we only care about the Safari user agent in this case
          if (value != null && value.equals("safari")) {
            newArtifacts.add(createCache(logger, context, compilationResult));
            break;
          }
        }
      }
    }

    return newArtifacts;
  }

  private SyntheticArtifact createCache(TreeLogger logger, LinkerContext context, CompilationResult result)
      throws UnableToCompleteException {
    try {
      logger.log(TreeLogger.Type.INFO, "Using the Safari user agent for the manifest file.");
      // load a template JSP file into a string. This contains all of the files that we want in our cache
      // manifest and a placeholder for the GWT javascript file, which will replace with the actual file next
      String manifest = IOUtils.toString(getClass().getResourceAsStream("cache.template.manifest"));
      // replace the placeholder with the real file name
      manifest = manifest.replace("$SAFARI_HTML_FILE_CHECKSUM$", result.getStrongName());
      // return the Artifact named as the file we want to call it
      return emitString(logger, manifest, "cache.manifest.");
    } catch (IOException e) {
      logger.log(TreeLogger.ERROR, "Couldn't read cache manifest template.", e);
      throw new UnableToCompleteException();
    }
  }
}

答案 2 :(得分:1)

使用gwt2go library的GWT Application Manifest生成器来做到这一点。那很简单。 :)