Advertisement
s243a

SoneInserter$InsertInformation.createManifestElement

Jun 7th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. //line 365 of https://github.com/Bombe/Sone/blob/0fde4bc94b71158908f7a1ed16807985628b8619/src/main/java/net/pterodactylus/sone/core/SoneInserter.java
  2.         /**
  3. 366          * Creates a new manifest element.
  4. 367          *
  5. 368          * @param name
  6. 369          *            The name of the file
  7. 370          * @param contentType
  8. 371          *            The content type of the file
  9. 372          * @param templateName
  10. 373          *            The name of the template to render
  11. 374          * @return The manifest element
  12. 375          */
  13. 376         @SuppressWarnings("synthetic-access")
  14. 377         private ManifestElement createManifestElement(String name, String contentType, String templateName) {
  15. 378             InputStreamReader templateInputStreamReader = null;
  16. 379             Template template;
  17. 380             try {
  18. 381                 templateInputStreamReader = new InputStreamReader(getClass().getResourceAsStream(templateName), utf8Charset);
  19. 382                 template = TemplateParser.parse(templateInputStreamReader);
  20. 383             } catch (TemplateException te1) {
  21. 384                 logger.log(Level.SEVERE, String.format("Could not parse template ā€œ%sā€!", templateName), te1);
  22. 385                 return null;
  23. 386             } finally {
  24. 387                 Closer.close(templateInputStreamReader);
  25. 388             }
  26. 389
  27.  
  28. 390             TemplateContext templateContext = templateContextFactory.createTemplateContext();
  29. 391             templateContext.set("core", core);
  30. 392             templateContext.set("currentSone", soneProperties);
  31. 393             templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
  32. 394             templateContext.set("version", SonePlugin.VERSION);
  33. 395             StringWriter writer = new StringWriter();
  34. 396             RandomAccessBucket bucket = null;
  35. 397             try {
  36. 398                 template.render(templateContext, writer);
  37. 399                 bucket = new ArrayBucket(writer.toString().getBytes(Charsets.UTF_8));
  38. 400                 return new ManifestElement(name, bucket, contentType, bucket.size());
  39. 401             } catch (TemplateException te1) {
  40. 402                 logger.log(Level.SEVERE, String.format("Could not render template ā€œ%sā€!", templateName), te1);
  41. 403                 return null;
  42. 404             } finally {
  43. 405                 Closer.close(writer);
  44. 406                 if (bucket != null) {
  45. 407                     bucket.free();
  46. 408                 }
  47. 409             }
  48. 410         }
  49. 411
  50.  
  51. 412     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement