Ejemplo n.º 1
0
  private Source createSource(String fileName, SourceCodeBuffer sourceCodeBuffer) {
    Source result = null;

    if (sourceCodeBuffer.getBuffer() != null) {
      String sourceCode = sourceCodeBuffer.toString();

      if (configuration.keepGeneratedActionScript()) {
        try {
          FileUtil.writeFile(fileName, sourceCode);
        } catch (IOException e) {
          ThreadLocalToolkit.log(
              new VelocityException.UnableToWriteGeneratedFile(fileName, e.getMessage()));
        }
      }

      VirtualFile genFile =
          new TextFile(sourceCode, fileName, null, MimeMappings.AS, Long.MAX_VALUE);
      String shortName = fileName.substring(0, fileName.lastIndexOf('.'));

      result = new Source(genFile, "", shortName, null, false, false, false);
      result.setPathResolver(compilationUnit.getSource().getPathResolver());

      Iterator iterator = implicitIncludes.iterator();

      while (iterator.hasNext()) {
        VirtualFile virtualFile = (VirtualFile) iterator.next();
        result.addFileInclude(virtualFile);
      }
    }

    return result;
  }
Ejemplo n.º 2
0
  private Source generateStyleSource(StyleDef styleDef, ResourceContainer resources) {
    String genFileName = generateStyleSourceName(styleDef);
    Source styleSource = resources.findSource(genFileName);

    if (styleSource != null) {
      if (styleSource.getCompilationUnit() == null) {
        // if no compilationUnit, then we need to generate source so we can recompile.
        styleSource = null;
      } else {
        // C: it is safe to return because this method deals with per-app styles, like defaults.css
        // and themes.
        //    ResourceContainer will not have anything if any of the theme files is touched.
        return styleSource;
      }
    }

    //	load template
    Template template;

    try {
      template = VelocityManager.getTemplate(STYLEDEF_TEMPLATE);
    } catch (Exception exception) {
      ThreadLocalToolkit.log(new VelocityException.TemplateNotFound(STYLEDEF_TEMPLATE));
      return null;
    }

    SourceCodeBuffer out = new SourceCodeBuffer();

    try {
      VelocityUtil util = new VelocityUtil(TEMPLATE_PATH, configuration.debug(), out, null);
      VelocityContext vc = VelocityManager.getCodeGenContext(util);
      vc.put(STYLEDEF_KEY, styleDef);
      template.merge(vc, out);
    } catch (Exception e) {
      ThreadLocalToolkit.log(
          new VelocityException.GenerateException(
              compilationUnit.getSource().getRelativePath(), e.getLocalizedMessage()));
      return null;
    }

    return resources.addResource(createSource(genFileName, out));
  }