--- /dev/null
+package pnews.servlet;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import pnews.Article;
+import pnews.Category;
+
+public class HTML {
+ private static final String CLASS_NAME= HTML.class.getName();
+ private static final Logger LOG = Logger.getLogger(CLASS_NAME);
+
+ private static void appendA(StringBuffer buf, String child, String href, String cl) {
+ buf.append("<a href='");
+ buf.append(href);
+ buf.append("'");
+ if (cl != null) {
+ buf.append(" class='");
+ buf.append(cl);
+ buf.append('\'');
+ }
+ buf.append('>');
+ buf.append(child);
+ buf.append("</a>");
+ }
+
+ private static void appendDiv(StringBuffer buf, String child) {
+ buf.append("<div>");
+ buf.append(child);
+ buf.append("</div>\n");
+ }
+
+ private static void appendP(StringBuffer buf, String child) {
+ buf.append("<p>");
+ buf.append(child);
+ buf.append("</p>\n");
+ }
+
+ private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {
+ buf.append("<div class='article'>\n");
+
+ buf.append("<div class='article-image'>\n");
+ if (a.thumbnail != null) {
+ buf.append("<img class='left' src='");
+ buf.append(a.thumbnail);
+ buf.append("'/>\n");
+ }
+ buf.append("</div>\n");
+
+ buf.append("<div class='article-content'>\n");
+
+ buf.append("<div class='article-title'>\n");
+ appendA(buf, a.title, "redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
+ buf.append("</div>\n");
+
+ buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
+
+ buf.append("<div class='article-description'>\n");
+ if (a.description != null) {
+ buf.append("<p>");
+ buf.append(a.description);
+ buf.append("</p>");
+ }
+ buf.append("</div>\n");
+
+ buf.append("</div>\n");
+
+ buf.append("</div>\n");
+ }
+
+ private static void appendMenu(StringBuffer buf, Category catActive) {
+ String cl;
+
+ buf.append("<nav>\n");
+ buf.append("<ul>\n");
+
+ for (Category cat: Category.values()) {
+ buf.append("<li>");
+
+ if (cat.equals(catActive))
+ cl = "active";
+ else
+ cl = null;
+
+ appendA(buf, cat.getId(), cat.getId(), cl);
+ buf.append("</li>");
+ }
+
+ buf.append("</ul>\n");
+ buf.append("</nav>\n");
+ }
+
+ public static String toHTML(List<Article> articles, Category catActive) {
+ StringBuffer buf;
+ int i;
+
+ buf = new StringBuffer();
+ buf.append("<!DOCTYPE html>\n");
+ buf.append("<html lang='fr'>\n");
+ buf.append("<head>\n");
+ buf.append("<meta charset=\"UTF-8\">\n");
+ buf.append("<link rel='stylesheet' href='style.css' />\n");
+ buf.append("<title>PNews</title>\n");
+ buf.append("</head>\n");
+ buf.append("<body>\n");
+
+ appendMenu(buf, catActive);
+
+ i = 0;
+ for (Article e: articles) {
+ try {
+ append(buf, e);
+ } catch (UnsupportedEncodingException e1) {
+ LOG.log(Level.SEVERE, "fail to convert article to HTML", e1);
+ }
+ if (i == 100)
+ break;
+ else
+ i++;
+ }
+
+ buf.append("</body>\n");
+ buf.append("</html>\n");
+
+ return buf.toString();
+ }
+}