72f0b4b3b514573f650f09701220c0b7b73ec241
[pnews.git] / war / src / main / java / pnews / servlet / HTML.java
1 package pnews.servlet;
2
3 import java.io.IOException;
4 import java.io.UnsupportedEncodingException;
5 import java.net.URLEncoder;
6 import java.util.List;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9
10 import com.rometools.rome.io.FeedException;
11
12 import pnews.Article;
13 import pnews.Category;
14 import pnews.EntityStat;
15 import pnews.Language;
16
17 public class HTML {
18         private static final String CLASS_NAME= HTML.class.getName();
19         private static final Logger LOG = Logger.getLogger(CLASS_NAME);
20         
21         private static void appendA(StringBuffer buf, String child, String href, String cl) {
22                 buf.append("<a href='");
23                 buf.append(href);
24                 buf.append("'");
25                 if (cl != null) {
26                         buf.append(" class='");
27                         buf.append(cl);
28                         buf.append('\'');
29                 }
30                 buf.append('>');
31                 buf.append(child);
32                 buf.append("</a>");
33         }
34         
35         private static void appendDiv(StringBuffer buf, String child) {
36                 buf.append("<div>");
37                 buf.append(child);
38                 buf.append("</div>\n");
39         }
40         
41         private static void appendP(StringBuffer buf, String child) {
42                 buf.append("<p>");
43                 buf.append(child);
44                 buf.append("</p>\n");
45         }
46         
47         private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {           
48                 buf.append("<div class='article'>\n");
49                 
50                 buf.append("<div class='article-image'>\n");
51                 if (a.thumbnail != null) {
52                         buf.append("<img class='left' src='");
53                         buf.append(a.thumbnail);
54                         buf.append("'/>\n");
55                 }
56                 buf.append("</div>\n");
57                 
58                 buf.append("<div class='article-content'>\n");
59
60                 buf.append("<div class='article-title'>\n");
61                 appendA(buf, a.title, "/redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
62                 buf.append("</div>\n");
63                 
64                 buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
65                 
66                 buf.append("<div class='article-description'>\n");
67                 if (a.description != null) {
68                         buf.append("<p>");
69                         buf.append(a.description);
70                         buf.append("</p>");
71                 }
72                 buf.append("</div>\n");         
73                 
74                 buf.append("</div>\n");
75                 
76                 buf.append("</div>\n");         
77         }
78         
79         private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
80                 String cl;
81                 
82                 buf.append("<nav>\n");
83                 buf.append("<ul>\n");
84
85                 for (Category cat: cfg.getCategories()) {
86                         if (!cat.getLanguage().equals(catActive.getLanguage()))
87                                 continue;
88                         
89                         buf.append("<li>");
90                         
91                         if (cat.equals(catActive))
92                                 cl = "active";
93                         else
94                                 cl = null;
95                         
96                         appendA(buf, cat.getLabel(), cat.getURL(), cl);
97                         buf.append("</li>");
98                 }
99                 
100                 for (Language l: cfg.getLanguages())
101                         buf.append("<li><a href='" + l.toURL() + "'>" + l.getLabel() + "</a></li>");
102                 
103                 buf.append("</ul>\n");
104                 
105                 buf.append("</nav>\n");
106         }
107         
108         private static String toURL(Category catActive, String entity) {
109                 try {
110                         return catActive.getURL() + "?entity=" + URLEncoder.encode(entity, "UTF-8");
111                 } catch (UnsupportedEncodingException e) {
112                         LOG.log(Level.SEVERE, "Failed to generate link to entity " + entity, e);
113                         return catActive.getURL();
114                 }
115         }
116         
117         public static String toHTML(List<Article> articles, Category catActive, String entityActive, Config cfg, ArticleProvider provider) {
118                 StringBuffer buf;
119                 int i;
120                 List<EntityStat> entities;
121                 String cl;
122                 
123                 buf = new StringBuffer();
124                 buf.append("<!DOCTYPE html>\n");
125                 buf.append("<html lang='fr'>\n");
126                 buf.append("<head>\n");
127                 buf.append("<meta charset=\"UTF-8\">\n");
128                 buf.append("<link rel='stylesheet' href='/style.css' />\n");
129                 buf.append("<title>");
130                 buf.append(catActive.getTitle());
131                 buf.append(" - PNews</title>\n");
132                 buf.append("</head>\n");
133                 buf.append("<body>\n");
134                 
135                 appendMenu(buf, catActive, cfg);
136                 
137                 try {
138                         entities = provider.getEntityStats(catActive);
139
140                         if (entities.size() > 0) {
141                                 buf.append("<nav>");
142                                 buf.append("<ul>");
143                                 i = 0;
144                                 for (EntityStat s: entities) {
145                                         buf.append("<li>");
146                                         if (entityActive != null && s.getEntity().equals(entityActive))
147                                                 cl = "active";
148                                         else
149                                                 cl = null;
150                                         appendA(buf, s.getEntity(), toURL(catActive, s.getEntity()), cl);
151                                         buf.append("</li>\n");
152                                         i++;
153                                         if (i > 10)
154                                                 break;
155                                 }                               
156                                 buf.append("</ul>\n");
157                                 buf.append("</nav>\n");
158                         }
159                 } catch (IllegalArgumentException | FeedException | IOException e2) {
160                         LOG.log(Level.SEVERE, "Failed to get entities", e2);
161                 }
162                 
163                 i = 0;
164                 for (Article e: articles) {
165                         try {
166                                 append(buf, e);
167                         } catch (UnsupportedEncodingException e1) {
168                                 LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
169                         }
170                         if (i == 100)
171                                 break;
172                         else
173                                 i++;
174                 }
175                 
176                 buf.append("</body>\n");
177                 buf.append("</html>\n");
178         
179                 return buf.toString();
180         }
181 }