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