SHARE
TWEET

Untitled

a guest Mar 26th, 2020 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.net.URI;
  2. import java.net.URL;
  3. import java.util.Locale;
  4. import java.util.Map;
  5. import org.thymeleaf.TemplateEngine;
  6. import org.thymeleaf.context.Context;
  7. import org.thymeleaf.templatemode.TemplateMode;
  8. import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
  9.  
  10. public class Message {
  11.  
  12.   private TemplateEngine templateEngine;
  13.   private URI uri;
  14.  
  15.   private Message(TemplateEngine templateEngine, URI uri) {
  16.     this.templateEngine = templateEngine;
  17.     this.uri = uri;
  18.   }
  19.  
  20.   public String getText() {
  21.     return templateEngine
  22.         .process("template.txt", new Context(Locale.getDefault(), Map.of("uri", uri)));
  23.   }
  24.  
  25.   public static Message newInstance(URI uri) {
  26.     ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
  27.     templateResolver.setPrefix("templates/");
  28.     templateResolver.setSuffix(".txt");
  29.     templateResolver.setTemplateMode(TemplateMode.TEXT);
  30.  
  31.     TemplateEngine templateEngine = new TemplateEngine();
  32.     templateEngine.setTemplateResolver(templateResolver);
  33.  
  34.     return new Message(templateEngine, uri);
  35.   }
  36.  
  37.   public static void main(String[] args) {
  38.     System.out.println(Message.newInstance(URI.create("https://google.com")).getText());
  39.   }
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top