001package io.jstach.opt.spring.webflux.example.hello;
002
003/**
004 * Generated Renderer.
005 */
006// @javax.annotation.Generated("io.jstach.apt.GenerateRendererProcessor")
007@org.springframework.stereotype.Component
008public class HelloModelView implements io.jstach.jstachio.Template.EncodedTemplate<io.jstach.opt.spring.webflux.example.hello.HelloModel>,
009    io.jstach.jstachio.TemplateInfo,
010    io.jstach.jstachio.spi.TemplateProvider,
011    io.jstach.jstachio.spi.JStachioFilter.FilterChain {
012    /**
013     * Template path.
014     * @hidden
015     */
016    public static final String TEMPLATE_PATH = "views/hello.mustache";
017
018    /**
019     * Inline template string copied.
020     * @hidden
021     */
022
023    public static final String TEMPLATE_STRING = "";
024
025    /**
026     * Template name. Do not rely on this.
027     * @hidden
028     */
029    public static final String TEMPLATE_NAME = "io.jstach.opt.spring.webflux.example.hello.HelloModelView";
030
031    /**
032     * Template charset.
033     * @hidden
034     */
035    public static final java.nio.charset.Charset TEMPLATE_CHARSET = java.nio.charset.StandardCharsets.UTF_8;
036
037    /**
038     * Template mediaType.
039     * @hidden
040     */
041    public static final String TEMPLATE_MEDIA_TYPE = "text/html";
042
043    /**
044     * The models class. Use {@link #modelClass()} instead.
045     * @hidden
046     */
047    public static final Class<?> MODEL_CLASS = io.jstach.opt.spring.webflux.example.hello.HelloModel.class;
048
049    /**
050     * The instance. Use {@link {@link #of()} instead.
051     * @hidden
052     */
053    private static final HelloModelView INSTANCE = new HelloModelView();
054
055    /**
056     * Formatter. 
057     * @hidden
058     */
059    private final io.jstach.jstachio.Formatter formatter;
060
061    /**
062     * Escaper. 
063     * @hidden
064     */
065    private final io.jstach.jstachio.Escaper escaper;
066
067    /**
068     * Renderer constructor for manual wiring.
069     * @param formatter formatter if null the static formatter will be used.
070     * @param escaper escaper if null the static escaper will be used
071     */
072    public HelloModelView(
073        /* @Nullable */ java.util.function.Function< /* @Nullable */ Object, String> formatter,
074        /* @Nullable */ java.util.function.Function<String, String> escaper) {
075        super();
076        this.formatter = __formatter(formatter);
077        this.escaper = __escaper(escaper);
078    }
079
080    private static io.jstach.jstachio.Formatter __formatter(/* @Nullable */ java.util.function.Function< /* @Nullable */ Object, String> formatter) {
081        return io.jstach.jstachio.Formatter.of(formatter != null ? formatter : io.jstach.jstachio.formatters.DefaultFormatter.provider());
082    }
083
084    private static io.jstach.jstachio.Escaper __escaper(/* @Nullable */ java.util.function.Function<String, String> escaper) {
085        return io.jstach.jstachio.Escaper.of(escaper != null ? escaper : io.jstach.jstachio.escapers.Html.provider());
086    }
087
088    /**
089     * Renderer constructor for reflection (use of() instead).
090     * For programmatic consider using {@link #of()} for a shared singleton.
091     */
092    public HelloModelView() {
093        this(null, null);
094    }
095
096    @Override
097    public StringBuilder execute(io.jstach.opt.spring.webflux.example.hello.HelloModel model, StringBuilder sb) {
098        render(model, io.jstach.jstachio.Output.of(sb), templateFormatter(), templateEscaper(), templateAppender());
099        return sb;
100    }
101
102    @Override
103    public <A extends io.jstach.jstachio.Output<E>, E extends Exception> A execute(
104        io.jstach.opt.spring.webflux.example.hello.HelloModel model, 
105        A a) throws E {
106        render(model, a, formatter, escaper, templateAppender());
107        return a;
108    }
109
110    /**
111     * Renders the passed in model.
112     * @param <A> appendable type.
113     * @param <E> error type.
114     * @param model a model assumed never to be <code>null</code>.
115     * @param a appendable to write to.
116     * @param formatter formats variables before they are passed to the escaper
117     * @param escaper used to write escaped variables
118     * @throws E if an error occurs while writing to the appendable
119     */
120    protected <A extends io.jstach.jstachio.Output<E>, E extends Exception> void execute(
121        io.jstach.opt.spring.webflux.example.hello.HelloModel model, 
122        A a, 
123        io.jstach.jstachio.Formatter formatter,
124        io.jstach.jstachio.Escaper escaper) throws E {
125        render(model, a, formatter, escaper, templateAppender());
126    }
127
128    @Override
129    public <A extends io.jstach.jstachio.Output.EncodedOutput<E>, E extends Exception> A write(
130        io.jstach.opt.spring.webflux.example.hello.HelloModel model, 
131        A outputStream) throws E {
132        encode(model, outputStream, templateFormatter(), templateEscaper(), templateAppender());
133        return outputStream;
134    }
135
136    @Override
137    public boolean supportsType(Class<?> type) {
138        return MODEL_CLASS.isAssignableFrom(type);
139    }
140
141    /**
142     * Needed for jstachio runtime.
143     * @hidden
144     */
145    @Override
146    public java.util.List<io.jstach.jstachio.Template<?>> provideTemplates(io.jstach.jstachio.TemplateConfig templateConfig ) {
147        return java.util.List.of(io.jstach.jstachio.TemplateConfig.empty() == templateConfig ? INSTANCE :  new HelloModelView(templateConfig));
148    }
149
150    @Override
151    public String templatePath() {
152        return TEMPLATE_PATH;
153    }
154    @Override
155    public String templateName() {
156        return TEMPLATE_NAME;
157    }
158    @Override
159    public java.nio.charset.Charset templateCharset() {
160        return TEMPLATE_CHARSET;
161    }
162    @Override
163    public String templateMediaType() {
164        return TEMPLATE_MEDIA_TYPE;
165    }
166    @Override
167    public String templateString() {
168        return TEMPLATE_STRING;
169    }
170    @Override
171    public Class<?> templateContentType() {
172        return io.jstach.jstachio.escapers.Html.class;
173    }
174    @Override
175    public  io.jstach.jstachio.Escaper templateEscaper() {
176        return this.escaper;
177    }
178
179    @Override
180    public io.jstach.jstachio.Formatter templateFormatter() {
181        return this.formatter;
182    }
183
184    /**
185     * Appender.
186     * @return appender for writing unescaped variables.
187     */
188    public io.jstach.jstachio.Appender templateAppender() {
189        return io.jstach.jstachio.Appender.defaultAppender();
190    }
191
192    /**
193     * Model class.
194     * @return class used as model (annotated with JStache).
195     */
196    @Override
197    public Class<?> modelClass() {
198        return MODEL_CLASS;
199    }
200
201    /**
202     * Needed for jstachio runtime.
203     * @hidden
204     */
205    @SuppressWarnings("unchecked")
206    @Override
207    public void process(Object model, Appendable appendable) throws java.io.IOException {
208        execute( (io.jstach.opt.spring.webflux.example.hello.HelloModel) model, appendable);
209    }
210
211    /**
212     * Needed for jstachio runtime.
213     * @hidden
214     */
215    @Override
216    public boolean isBroken(Object model) {
217        return !supportsType(model.getClass());
218    }
219
220    /**
221     * Renderer constructor using config.
222     * @param templateConfig config that has collaborators
223     */
224    public HelloModelView(io.jstach.jstachio.TemplateConfig templateConfig) {
225        this(templateConfig.formatter(), templateConfig.escaper());
226    }
227
228    /**
229     * Convience static factory that will reuse the same singleton instance.
230     * @return renderer same as calling no-arg constructor but is cached with singleton instance
231     */
232    public static HelloModelView of() {
233        return INSTANCE;
234    }
235
236    /**
237     * Renders the passed in model.
238     * @param <A> appendable type.
239     * @param <E> error type.
240     * @param data model
241     * @param unescapedWriter appendable to write to.
242     * @param formatter formats variables before they are passed to the escaper.
243     * @param escaper used to write escaped variables.
244     * @param appender used to write unescaped variables.
245     * @throws E if an error occurs while writing to the appendable
246     */
247    public static <A extends io.jstach.jstachio.Output<E>, E extends Exception> void render(
248        io.jstach.opt.spring.webflux.example.hello.HelloModel data, 
249        A unescapedWriter,
250        io.jstach.jstachio.Formatter formatter,
251        io.jstach.jstachio.Appender escaper,
252        io.jstach.jstachio.Appender appender) throws E {
253
254        unescapedWriter.append(
255            "<!doctype html>\n" +
256            "<html lang=\"en\">\n" +
257            "  <head>\n" +
258            "    <meta charset=\"utf-8\">\n" +
259            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" +
260            "    <link rel=\"stylesheet\" href=\"https://unpkg.com/@picocss/pico@latest/css/pico.min.css\">\n" +
261            "    <title>Hello, ");
262
263        // variable: message
264        /* RenderingContext: class io.jstach.apt.internal.context.DeclaredTypeRenderingContext */
265        /* TypeMirror: java.lang.String */
266        formatter.format(escaper, unescapedWriter, "message", data.message());
267
268        unescapedWriter.append(
269            "!</title>\n" +
270            "  </head>\n" +
271            "  <body>\n" +
272            "    <main class=\"container\">\n" +
273            "      <h1>");
274
275        // variable: message
276        /* RenderingContext: class io.jstach.apt.internal.context.DeclaredTypeRenderingContext */
277        /* TypeMirror: java.lang.String */
278        formatter.format(escaper, unescapedWriter, "message", data.message());
279
280        unescapedWriter.append(
281            "</h1>\n" +
282            "    </main>\n" +
283            "  </body>\n" +
284            "</html>");
285
286
287    }
288
289    /**
290     * Renders to an OutputStream use pre-encoded parts of the template.
291     * @param <A> output type.
292     * @param <E> error type.
293     * @param data model
294     * @param unescapedWriter stream to write to.
295     * @param formatter formats variables before they are passed to the escaper.
296     * @param escaper used to write escaped variables.
297     * @param appender used to write unescaped variables.
298     * @throws E if an error occurs while writing to the appendable
299     */
300    protected static <A extends io.jstach.jstachio.Output.EncodedOutput<E>, E extends Exception> void encode(
301        io.jstach.opt.spring.webflux.example.hello.HelloModel data, 
302        A unescapedWriter,
303        io.jstach.jstachio.Formatter formatter,
304        io.jstach.jstachio.Escaper escaper,
305        io.jstach.jstachio.Appender appender) throws E {
306
307        unescapedWriter.write(TEXT_0);
308
309        // variable: message
310        /* RenderingContext: class io.jstach.apt.internal.context.DeclaredTypeRenderingContext */
311        /* TypeMirror: java.lang.String */
312        formatter.format(escaper, unescapedWriter, "message", data.message());
313
314        unescapedWriter.write(TEXT_1);
315
316        // variable: message
317        /* RenderingContext: class io.jstach.apt.internal.context.DeclaredTypeRenderingContext */
318        /* TypeMirror: java.lang.String */
319        formatter.format(escaper, unescapedWriter, "message", data.message());
320
321        unescapedWriter.write(TEXT_2);
322
323
324    }
325    private static final byte[] TEXT_0 = (
326    "<!doctype html>\n" +
327    "<html lang=\"en\">\n" +
328    "  <head>\n" +
329    "    <meta charset=\"utf-8\">\n" +
330    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" +
331    "    <link rel=\"stylesheet\" href=\"https://unpkg.com/@picocss/pico@latest/css/pico.min.css\">\n" +
332    "    <title>Hello, ").getBytes(TEMPLATE_CHARSET);
333    private static final byte[] TEXT_1 = (
334    "!</title>\n" +
335    "  </head>\n" +
336    "  <body>\n" +
337    "    <main class=\"container\">\n" +
338    "      <h1>").getBytes(TEMPLATE_CHARSET);
339    private static final byte[] TEXT_2 = (
340    "</h1>\n" +
341    "    </main>\n" +
342    "  </body>\n" +
343    "</html>").getBytes(TEMPLATE_CHARSET);
344}