PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/test/java/org/fusesource/scalate/demo/UseScalateFromJava.java

http://github.com/scalate/scalate
Java | 97 lines | 62 code | 15 blank | 20 comment | 5 complexity | fd3236aec76e01880eb8fcd0f2d34b24 MD5 | raw file
 1/**
 2 * Copyright (C) 2009-2010 the original author or authors.
 3 * See the notice.md file distributed with this work for additional
 4 * information regarding copyright ownership.
 5 *
 6 * Licensed under the Apache License, Version 2.0 (the "License");
 7 * you may not use this file except in compliance with the License.
 8 * You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.fusesource.scalate.demo;
19
20import org.fusesource.scalate.japi.TemplateEngineFacade;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26import java.io.OutputStream;
27import java.io.PrintStream;
28import java.util.HashMap;
29import java.util.Map;
30
31/**
32 * Shows how to use Scalate from a Java class
33 */
34public class UseScalateFromJava extends TemplateEngineFacade {
35
36    private static final Logger LOGGER = LoggerFactory.getLogger(UseScalateFromJava.class);
37
38    public static class LoggingOutputStream extends OutputStream {
39
40        private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
41        private final Logger logger;
42        private final LogLevel level;
43
44        public enum LogLevel {
45            TRACE, DEBUG, INFO, WARN, ERROR,
46        }
47
48        public LoggingOutputStream(Logger logger, LogLevel level) {
49            this.logger = logger;
50            this.level = level;
51        }
52
53        @Override
54        public void write(int b) {
55            if (b == '\n') {
56                String line = baos.toString();
57                baos.reset();
58
59                switch (level) {
60                    case TRACE:
61                        logger.trace(line);
62                        break;
63                    case DEBUG:
64                        logger.debug(line);
65                        break;
66                    case ERROR:
67                        logger.error(line);
68                        break;
69                    case INFO:
70                        logger.info(line);
71                        break;
72                    case WARN:
73                        logger.warn(line);
74                        break;
75                }
76            } else {
77                baos.write(b);
78            }
79        }
80
81    }
82
83    public static void main(String[] args) {
84        if (args.length <= 0) {
85            LOGGER.info("Usage: UseScalateFromJava templateUri");
86            System.exit(1);
87        }
88        TemplateEngineFacade engine = new TemplateEngineFacade();
89
90        Map<String,Object> attributes = new HashMap<>();
91        attributes.put("name", "James Strachan");
92
93        OutputStream out = new LoggingOutputStream(LOGGER, LoggingOutputStream.LogLevel.INFO);
94        engine.layout(args[0], out, attributes);
95    }
96
97}