/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. */
  18. package org.fusesource.scalate.demo;
  19. import org.fusesource.scalate.japi.TemplateEngineFacade;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import java.io.PrintStream;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. /**
  29. * Shows how to use Scalate from a Java class
  30. */
  31. public class UseScalateFromJava extends TemplateEngineFacade {
  32. private static final Logger LOGGER = LoggerFactory.getLogger(UseScalateFromJava.class);
  33. public static class LoggingOutputStream extends OutputStream {
  34. private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
  35. private final Logger logger;
  36. private final LogLevel level;
  37. public enum LogLevel {
  38. TRACE, DEBUG, INFO, WARN, ERROR,
  39. }
  40. public LoggingOutputStream(Logger logger, LogLevel level) {
  41. this.logger = logger;
  42. this.level = level;
  43. }
  44. @Override
  45. public void write(int b) {
  46. if (b == '\n') {
  47. String line = baos.toString();
  48. baos.reset();
  49. switch (level) {
  50. case TRACE:
  51. logger.trace(line);
  52. break;
  53. case DEBUG:
  54. logger.debug(line);
  55. break;
  56. case ERROR:
  57. logger.error(line);
  58. break;
  59. case INFO:
  60. logger.info(line);
  61. break;
  62. case WARN:
  63. logger.warn(line);
  64. break;
  65. }
  66. } else {
  67. baos.write(b);
  68. }
  69. }
  70. }
  71. public static void main(String[] args) {
  72. if (args.length <= 0) {
  73. LOGGER.info("Usage: UseScalateFromJava templateUri");
  74. System.exit(1);
  75. }
  76. TemplateEngineFacade engine = new TemplateEngineFacade();
  77. Map<String,Object> attributes = new HashMap<>();
  78. attributes.put("name", "James Strachan");
  79. OutputStream out = new LoggingOutputStream(LOGGER, LoggingOutputStream.LogLevel.INFO);
  80. engine.layout(args[0], out, attributes);
  81. }
  82. }