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