From 2a49578518a57b76c7b6eb8c438d664c57b19819 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Oct 2010 06:32:02 +0200 Subject: [PATCH] Add method to create a manifest element. --- .../net/pterodactylus/sone/core/SoneInserter.java | 35 +++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index ee98cb2..afd406f 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -51,6 +51,9 @@ public class SoneInserter extends AbstractService { templateFactory.addAccessor(Object.class, new ReflectionAccessor()); } + /** The UTF-8 charset. */ + private static final Charset utf8Charset = Charset.forName("UTF-8"); + /** The Freenet interface. */ private final FreenetInterface freenetInterface; @@ -178,20 +181,11 @@ public class SoneInserter extends AbstractService { * * @return The manifest entries for the Sone insert */ - @SuppressWarnings("synthetic-access") public HashMap generateManifestEntries() { HashMap manifestEntries = new HashMap(); - Charset utf8Charset = Charset.forName("UTF-8"); - /* first, create an index.html. */ - Template indexTemplate = templateFactory.createTemplate(new InputStreamReader(getClass().getResourceAsStream("/templates/insert/index.html"), utf8Charset)); - indexTemplate.set("currentSone", sone); - StringWriter indexWriter = new StringWriter(); - indexTemplate.render(indexWriter); - StringBucket indexBucket = new StringBucket(indexWriter.toString(), utf8Charset); - ManifestElement indexManifestElement = new ManifestElement("index.html", indexBucket, "text/html; charset=utf-8", indexBucket.size()); - manifestEntries.put("index.html", indexManifestElement); + manifestEntries.put("index.html", createManifestElement("index.html", "text/html; charset=utf-8", "/templates/insert/index.html")); return manifestEntries; } @@ -200,6 +194,27 @@ public class SoneInserter extends AbstractService { // PRIVATE METHODS // + /** + * Creates a new manifest element. + * + * @param name + * The name of the file + * @param contentType + * The content type of the file + * @param templateName + * The name of the template to render + * @return The manifest element + */ + @SuppressWarnings("synthetic-access") + private ManifestElement createManifestElement(String name, String contentType, String templateName) { + Template template = templateFactory.createTemplate(new InputStreamReader(getClass().getResourceAsStream(templateName), utf8Charset)); + template.set("currentSone", sone); + StringWriter writer = new StringWriter(); + template.render(writer); + StringBucket bucket = new StringBucket(writer.toString(), utf8Charset); + return new ManifestElement(name, bucket, contentType, bucket.size()); + } + } } -- 2.7.4