PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/main/java/com/google/sitebricks/rendering/control/TextWidget.java

http://github.com/dhanji/sitebricks
Java | 43 lines | 28 code | 10 blank | 5 comment | 1 complexity | 335f480401e76bd9e98508d686275f4e MD5 | raw file
 1package com.google.sitebricks.rendering.control;
 2
 3import com.google.sitebricks.Renderable;
 4import com.google.sitebricks.Respond;
 5import com.google.sitebricks.compiler.EvaluatorCompiler;
 6import com.google.sitebricks.compiler.ExpressionCompileException;
 7import com.google.sitebricks.compiler.Token;
 8import com.google.sitebricks.rendering.SelfRendering;
 9import net.jcip.annotations.ThreadSafe;
10
11import java.util.Collections;
12import java.util.List;
13import java.util.Set;
14
15/**
16 * @author Dhanji R. Prasanna (dhanji@gmail.com)
17 */
18@ThreadSafe @SelfRendering
19class TextWidget implements Renderable {
20    private final List<Token> tokenizedTemplate;     //TODO store some metrics to allocate buffers later
21
22    TextWidget(String template, EvaluatorCompiler compiler) throws ExpressionCompileException {
23
24        //compile token stream
25        tokenizedTemplate = compiler.tokenizeAndCompile(template);
26    }
27
28    public void render(Object bound, Respond respond) {
29
30        //render template from tokens
31        StringBuilder builder = new StringBuilder();
32        for (Token token : tokenizedTemplate) {
33            builder.append(token.render(bound));
34        }
35
36        respond.write(builder.toString());
37    }
38
39
40    public <T extends Renderable> Set<T> collect(Class<T> clazz) {
41        return Collections.emptySet();
42    }
43}