Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. final MustacheFactory mf = new DefaultMustacheFactory();
  2. [...]
  3.     private Mustache initMustache() {
  4.         log.debug("Compiling mustache with '{}'", this.templatePath);
  5.         String templateXml = getTemplateString();
  6.         return mf.compile(new StringReader(templateXml), this.templatePath);
  7.     }
  8.  
  9.     private String getTemplateString() {
  10.         String templateXml;
  11.         try (BufferedReader br = new BufferedReader(new InputStreamReader(this.templateInputStream))) {
  12.             templateXml = br.lines().collect(Collectors.joining("\n"));
  13.         } catch (IOException e) {
  14.             String message = "Cannot read template from '" + this.templateInputStream + "'";
  15.             throw new UncheckedIOException(message, e);
  16.         }
  17.         return templateXml;
  18.     }
  19.  
  20.  
  21. /*********************** AFTER ********************************/
  22.  
  23.     private Mustache getInitializedMustache() {
  24.         log.debug("Compiling mustache with '{}'", templatePath);
  25.         return Try.withResources(this::readerOfInputStream)
  26.                 .of(this::stringWithNewlineFromReader)
  27.                 .map(StringReader::new)
  28.                 .map(stringReader -> mf.compile(stringReader, templatePath))
  29.                 .getOrElseThrow(this::mustacheInitException);
  30.     }
  31.  
  32.     private String stringWithNewlineFromReader(BufferedReader reader) {
  33.         return reader
  34.                 .lines()
  35.                 .collect(Collectors.joining("\n"));
  36.     }
  37.  
  38.     private BufferedReader readerOfInputStream() {
  39.         return new BufferedReader(new InputStreamReader(this.templateInputStream));
  40.     }
  41.  
  42.     private MustacheException mustacheInitException(Throwable throwable) {
  43.         return new MustacheException(
  44.                 "Cannot init mustache template from '" + this.templatePath + "'",
  45.                 throwable
  46.         );
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement