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