Beispiel #1
0
 @Override
 public final void emit(EmitContext context, Template<ASTNode.Template> template)
     throws TemplateException, IOException {
   GroovyTemplateEmitter emitter = new GroovyTemplateEmitter(template.getAbsolutePath().getName());
   EmitPhase tcc = new EmitPhase(context);
   tcc.emit(emitter, template.getModel());
   context.createResource(
       template.getRelativePath().getRawName() + "_", "groovy", emitter.toString());
 }
  public GroovyTemplateStub template(final String text) throws IOException, TemplateException {
    Name pkg = Name.parse("foo");
    Name name = Name.parse("index");
    Name fqn = pkg.append(name);
    Path.Absolute absolute = Path.absolute(fqn, ".gtmpl");
    Path.Relative relative = Path.relative(name, ".gtmpl");
    GroovyTemplateEmitter generator = new GroovyTemplateEmitter(fqn);
    try {
      ProcessPhase processPhase =
          new ProcessPhase(
              new SimpleProcessContext(Collections.<Path.Absolute, TemplateModel<?>>emptyMap()) {
                @Override
                public Path.Absolute resolveTemplate(Path path) throws TemplateException {
                  return null;
                }

                @Override
                public TagHandler resolveTagHandler(String name) {
                  if ("title".equals(name)) {
                    return new TitleTag();
                  } else {
                    return null;
                  }
                }

                @Override
                public MethodInvocation resolveMethodInvocation(
                    String typeName, String methodName, Map<String, String> parameterMap)
                    throws ProcessingException {
                  if (parameterMap.size() > 0) {
                    throw failure("Unexpected non empty parameter map");
                  }
                  Class clazz = AbstractTemplateTestCase.this.getClass();
                  try {
                    Method m = clazz.getMethod(methodName);
                    return new MethodInvocation(
                        clazz.getName(), m.getName(), Collections.<String>emptyList());
                  } catch (NoSuchMethodException e) {
                    // Should we thrown a CompilationException instead ?
                    throw failure(e);
                  }
                }
              });
      TemplateModel<ASTNode.Template> templateModel =
          new TemplateModel<ASTNode.Template>(ASTNode.Template.parse(text), absolute, 0, 0);
      processPhase.process(templateModel);

      // Emit
      EmitPhase emitPhase =
          new EmitPhase(
              new EmitContext() {
                @Override
                public TagHandler resolveTagHandler(String name) {
                  if ("title".equals(name)) {
                    return new TitleTag();
                  } else {
                    return null;
                  }
                }

                public void createResource(Path.Absolute path, CharSequence content)
                    throws IOException {
                  throw new UnsupportedOperationException();
                }
              });
      emitPhase.emit(generator, templateModel.getModel());
    } catch (juzu.impl.template.spi.juzu.ast.ParseException e) {
      throw failure(e);
    }
    GroovyTemplateStub stub = generator.build(fqn.toString());
    stub.init();
    return stub;
  }