provide some stats as json
[pnews.git] / war / src / main / java / pnews / HTML.java
1 package pnews;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.List;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 public class HTML {
10         private static final String CLASS_NAME= HTML.class.getName();
11         private static final Logger LOG = Logger.getLogger(CLASS_NAME);
12         
13         private static void appendA(StringBuffer buf, String child, String href, String cl) {
14                 buf.append("<a href='");
15                 buf.append(href);
16                 buf.append("'");
17                 if (cl != null) {
18                         buf.append(" class='");
19                         buf.append(cl);
20                         buf.append('\'');
21                 }
22                 buf.append('>');
23                 buf.append(child);
24                 buf.append("</a>");
25         }
26         
27         private static void appendDiv(StringBuffer buf, String child) {
28                 buf.append("<div>");
29                 buf.append(child);
30                 buf.append("</div>\n");
31         }
32         
33         private static void appendP(StringBuffer buf, String child) {
34                 buf.append("<p>");
35                 buf.append(child);
36                 buf.append("</p>\n");
37         }
38         
39         private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {           
40                 buf.append("<div class='article'>\n");
41                 
42                 buf.append("<div class='article-image'>\n");
43                 if (a.thumbnail != null) {
44                         buf.append("<img class='left' src='");
45                         buf.append(a.thumbnail);
46                         buf.append("'/>\n");
47                 }
48                 buf.append("</div>\n");
49                 
50                 buf.append("<div class='article-content'>\n");
51
52                 buf.append("<div class='article-title'>\n");
53                 appendA(buf, a.title, "redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
54                 buf.append("</div>\n");
55                 
56                 buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
57                 
58                 buf.append("<div class='article-description'>\n");
59                 if (a.description != null) {
60                         buf.append("<p>");
61                         buf.append(a.description);
62                         buf.append("</p>");
63                 }
64                 buf.append("</div>\n");         
65                 
66                 buf.append("</div>\n");
67                 
68                 buf.append("</div>\n");         
69         }
70         
71         private static void appendMenu(StringBuffer buf, Category catActive) {
72                 String cl;
73                 
74                 buf.append("<nav>\n");
75                 buf.append("<ul>\n");
76
77                 for (Category cat: Category.values()) {
78                         buf.append("<li>");
79                         
80                         if (cat.equals(catActive))
81                                 cl = "active";
82                         else
83                                 cl = null;
84                         
85                         appendA(buf, cat.getId(), cat.getId(), cl);
86                         buf.append("</li>");
87                 }
88                 
89                 buf.append("</ul>\n");
90                 buf.append("</nav>\n");
91         }
92         
93         public static String toHTML(List<Article> articles, Category catActive) {
94                 StringBuffer buf;
95                 int i;
96                 
97                 buf = new StringBuffer();
98                 buf.append("<!DOCTYPE html>\n");
99                 buf.append("<html lang='fr'>\n");
100                 buf.append("<head>\n");
101                 buf.append("<meta charset=\"UTF-8\">\n");
102                 buf.append("<link rel='stylesheet' href='style.css' />\n");
103                 buf.append("<title>PNews</title>\n");
104                 buf.append("</head>\n");
105                 buf.append("<body>\n");
106                 
107                 appendMenu(buf, catActive);
108                 
109                 i = 0;
110                 for (Article e: articles) {
111                         try {
112                                 append(buf, e);
113                         } catch (UnsupportedEncodingException e1) {
114                                 LOG.log(Level.SEVERE, "fail to convert article to HTML", e1);
115                         }
116                         if (i == 100)
117                                 break;
118                         else
119                                 i++;
120                 }
121                 
122                 buf.append("</body>\n");
123                 buf.append("</html>\n");
124         
125                 return buf.toString();
126         }
127 }