public InterpretedTemplate(String template, String sourceName, Engine engine) {
   this.template = template;
   this.engine = engine;
   this.sourceName = sourceName;
   tokenStream =
       new TokenStream(sourceName, template, engine.getExprStartToken(), engine.getExprEndToken());
   tokenStream.prefill();
 }
  @Override
  @SuppressWarnings("unchecked")
  public synchronized Set<String> getUsedVariables() {
    if (this.usedVariables != null) {
      return this.usedVariables;
    }

    this.usedVariables = new TreeSet<String>();
    final Engine engine = new Engine();
    final ScopedMap scopedMap = new ScopedMap(Collections.EMPTY_MAP);

    ProcessListener processListener =
        new ProcessListener() {

          @Override
          public void log(TemplateContext context, Token token, Action action) {
            if (token instanceof ExpressionToken) {
              String variable = ((ExpressionToken) token).getExpression();
              if (!isLocal(variable)) {
                usedVariables.add(variable);
              }
            }
          }

          // a variable is local if any enclosing foreach has it as a step
          // variable
          private boolean isLocal(String variable) {
            for (Token token : context.scopes) {
              if (token instanceof ForEachToken) {
                String foreachVarName = ((ForEachToken) token).getVarName();
                if (foreachVarName.equals(variable)) {
                  return true;
                }
              }
            }
            return false;
          }
        };
    final Locale locale = Locale.getDefault();
    context =
        new TemplateContext(
            template,
            locale,
            sourceName,
            scopedMap,
            new DefaultModelAdaptor(),
            engine,
            engine.getErrorHandler(),
            processListener);

    transformPure(context);
    return usedVariables;
  }
Example #3
0
 /**
  * Transforms a template into an expanded output using the given model.
  *
  * @param model the model used to evaluate expressions inside the template
  * @return the expanded output
  */
 public String transform(Map<String, Object> model) {
   return transform(model, engine.getModelAdaptor(), null);
 }
 private Engine getEngine() {
   if (engine == null) {
     engine = Engine.createCompilingEngine();
     engine.registerNamedRenderer(new HtmlEscapeRenderer());
     engine.registerNamedRenderer(new LogRenderer(this));
     engine.registerNamedRenderer(new IndexRenderer());
     engine.registerNamedRenderer(new RelativePathRenderer(this));
     engine.registerNamedRenderer(new NumberRenderer());
     engine.registerNamedRenderer(new TimeRenderer());
     engine.registerNamedRenderer(new StringRenderer());
     engine.registerRenderer(Result.class, new ResultRenderer());
     engine.registerRenderer(Node.class, new NodeRenderer(engine, getTemplate("index-node.html")));
     engine.registerAnnotationProcessor(new AnnoCommandResults());
   }
   return engine;
 }
Example #5
0
 /**
  * Transforms a template into an expanded output using the given model.
  *
  * @param model the model used to evaluate expressions inside the template
  * @return the expanded output
  */
 public String transform(Map<String, Object> model, ProcessListener processListener) {
   return transform(model, engine.getModelAdaptor(), processListener);
 }