Beispiel #1
0
  public <M extends Serializable> Template<? extends M> resolveTemplate(
      Path.Relative originPath, Path.Relative path) {

    // A class cast here would mean a terrible issue
    Template<M> template = (Template<M>) templates.get(path);

    //
    if (template == null) {

      // Get source
      Resource<Timestamped<Content>> resolved = resolveResource(path);
      if (resolved == null) {
        throw TemplateMetaModel.TEMPLATE_NOT_RESOLVED.failure(path);
      }

      //
      TemplateProvider<M> provider = (TemplateProvider<M>) resolverProvider(path.getExt());

      // Parse to AST
      M templateAST;
      try {
        templateAST =
            provider.parse(new ParseContext(), resolved.content.getObject().getCharSequence());
      } catch (TemplateException e) {
        throw TemplateMetaModel.TEMPLATE_SYNTAX_ERROR.failure(path);
      }

      // Add template to application
      template =
          new Template<M>(originPath, templateAST, path, resolved.path, resolved.content.getTime());

      //
      templates.put(path, template);

      // Process template
      try {
        provider.process(this, template);
      } catch (TemplateException e) {
        throw TemplateMetaModel.TEMPLATE_VALIDATION_ERROR.failure(path);
      }
    }

    //
    return template;
  }
Beispiel #2
0
  private <M extends Serializable> void resolvedQualified(
      TemplateProvider<?> provider, Template<M> template, Element[] elements) {

    //
    TemplatesMetaModel metaModel = application.getChild(TemplatesMetaModel.KEY);

    //
    Path.Relative path = template.getRelativePath();
    if (classCache.containsKey(path)) {
      log.log("Template class " + path + " was found in cache");
      return;
    }

    //
    Path.Absolute resolvedPath = metaModel.resolvePath(path);

    //
    Writer writer = null;
    try {
      // Template qualified class
      FileObject classFile =
          application.getProcessingContext().createSourceFile(resolvedPath.getName(), elements);
      writer = classFile.openWriter();
      writer.append("package ").append(resolvedPath.getDirs()).append(";\n");
      writer.append("import ").append(TemplateDescriptor.class.getCanonicalName()).append(";\n");
      writer.append("import ").append(TemplatePlugin.class.getCanonicalName()).append(";\n");
      writer.append("@").append(Generated.class.getName()).append("({})\n");
      writer
          .append("@")
          .append(juzu.Path.class.getName())
          .append("(\"")
          .append(path.getValue())
          .append("\")\n");
      writer
          .append("public class ")
          .append(path.getRawName())
          .append(" extends ")
          .append(juzu.template.Template.class.getName())
          .append("\n");
      writer.append("{\n");
      writer.append("@javax.inject.Inject\n");
      writer
          .append("public ")
          .append(path.getRawName())
          .append("(")
          .append(TemplatePlugin.class.getSimpleName())
          .append(" templatePlugin")
          .append(")\n");
      writer.append("{\n");
      writer
          .append("super(templatePlugin, \"")
          .append(path.getValue())
          .append("\"")
          .append(", ")
          .append(provider.getTemplateStubType().getName())
          .append(".class);\n");
      writer.append("}\n");

      //
      writer
          .append("public static final ")
          .append(TemplateDescriptor.class.getName())
          .append(" DESCRIPTOR = new ")
          .append(TemplateDescriptor.class.getName())
          .append("(")
          .append(resolvedPath.getName())
          .append(".class,")
          .append(provider.getTemplateStubType().getName())
          .append(".class")
          .append(");\n");

      //
      String baseBuilderName = juzu.template.Template.Builder.class.getCanonicalName();
      if (template.getParameters() != null) {
        // Implement abstract method with this class Builder covariant return type
        writer.append("public Builder builder() {\n");
        writer.append("return new Builder();\n");
        writer.append("}\n");

        // Covariant return type of with()
        writer.append("public Builder with() {\n");
        writer.append("return (Builder)super.with();\n");
        writer.append("}\n");

        // Setters on builders
        writer.append("public class Builder extends ").append(baseBuilderName).append("\n");
        writer.append("{\n");
        for (String paramName : template.getParameters()) {
          writer
              .append("public Builder ")
              .append(paramName)
              .append("(Object ")
              .append(paramName)
              .append(") {\n");
          writer.append("set(\"").append(paramName).append("\",").append(paramName).append(");\n");
          writer.append("return this;\n");
          writer.append(("}\n"));
        }
        writer.append("}\n");
      } else {
        // Implement abstract factory method
        writer.append("public ").append(baseBuilderName).append(" builder() {\n");
        writer.append("return new ").append(baseBuilderName).append("();\n");
        writer.append("}\n");
      }

      // Close class
      writer.append("}\n");

      //
      classCache.put(path, classFile);

      //
      log.log(
          "Generated template class "
              + path
              + " as "
              + classFile.toUri()
              + " with originating elements "
              + Arrays.asList(elements));
    } catch (IOException e) {
      throw TemplateMetaModel.CANNOT_WRITE_TEMPLATE_CLASS.failure(e, elements[0], path);
    } finally {
      Tools.safeClose(writer);
    }
  }