log request info in a file accessible outstide the container
[pnews.git] / pnews / src / main / java / pnews / HTML.java
1 package pnews;
2
3 import java.util.List;
4
5 public class HTML {
6         private static void appendA(StringBuffer buf, String child, String href, String cl) {
7                 buf.append("<a href='");
8                 buf.append(href);
9                 buf.append("'");
10                 if (cl != null) {
11                         buf.append(" class='");
12                         buf.append(cl);
13                         buf.append('\'');
14                 }
15                 buf.append('>');
16                 buf.append(child);
17                 buf.append("</a>");
18         }
19         
20         private static void appendDiv(StringBuffer buf, String child) {
21                 buf.append("<div>");
22                 buf.append(child);
23                 buf.append("</div>\n");
24         }
25         
26         private static void appendP(StringBuffer buf, String child) {
27                 buf.append("<p>");
28                 buf.append(child);
29                 buf.append("</p>\n");
30         }
31         
32         private static void append(StringBuffer buf, Article a) {               
33                 buf.append("<div class='article'>\n");
34                 
35                 buf.append("<h2>");
36                 if (a.thumbnail != null) {
37                         buf.append("<img class='left' src='");
38                         buf.append(a.thumbnail);
39                         buf.append("'/>\n");
40                 }
41                 appendA(buf, a.title, "redirect?url="+a.link, null);
42                 buf.append("</h2>\n");
43                 
44                 buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
45                 
46                 if (a.description != null) {
47                         buf.append("<p>");
48                         buf.append(a.description);
49                         buf.append("</p>");
50                 }
51                 
52                 buf.append("</div>\n");         
53         }
54         
55         private static void appendMenu(StringBuffer buf, Category catActive) {
56                 String cl;
57                 
58                 buf.append("<nav>\n");
59                 buf.append("<ul>\n");
60
61                 for (Category cat: Category.values()) {
62                         buf.append("<li>");
63                         
64                         if (cat.equals(catActive))
65                                 cl = "active";
66                         else
67                                 cl = null;
68                         
69                         appendA(buf, cat.getId(), cat.getId(), cl);
70                         buf.append("</li>");
71                 }
72                 
73                 buf.append("</ul>\n");
74                 buf.append("</nav>\n");
75         }
76         
77         public static String toHTML(List<Article> articles, Category catActive) {
78                 StringBuffer buf;
79                 
80                 buf = new StringBuffer();
81                 buf.append("<!DOCTYPE html>\n");
82                 buf.append("<html lang='fr'>\n");
83                 buf.append("<head>\n");
84                 buf.append("<meta charset=\"UTF-8\">\n");
85                 buf.append("<link rel='stylesheet' href='style.css' />\n");
86                 buf.append("<title>PNews</title>\n");
87                 buf.append("</head>\n");
88                 buf.append("<body>\n");
89                 
90                 appendMenu(buf, catActive);
91                 
92                 for (Article e: articles)
93                         append(buf, e);
94                 
95                 buf.append("</body>\n");
96                 buf.append("</html>\n");
97         
98                 return buf.toString();
99         }
100 }