cut very long descriptions
[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                         if (a.description.length() < 512) {
70                                 buf.append(a.description);
71                         } else {
72                                 buf.append(a.description.substring(0, 512));
73                                 buf.append("[..]");
74                         }
75                         buf.append("</p>");
76                 }
77                 buf.append("</div>\n");         
78                 
79                 buf.append("</div>\n");
80                 
81                 buf.append("</div>\n");         
82         }
83         
84         private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
85                 String cl;
86                 
87                 buf.append("<nav>\n");
88                 buf.append("<ul>\n");
89
90                 for (Category cat: cfg.getCategories()) {
91                         if (!cat.getLanguage().equals(catActive.getLanguage()))
92                                 continue;
93                         
94                         buf.append("<li>");
95                         
96                         if (cat.equals(catActive))
97                                 cl = "active";
98                         else
99                                 cl = null;
100                         
101                         appendA(buf, cat.getLabel(), cat.getURL(), cl);
102                         buf.append("</li>");
103                 }
104                 
105                 for (Language l: cfg.getLanguages())
106                         buf.append("<li><a href='" + l.toURL() + "'>" + l.getLabel() + "</a></li>");
107                 
108                 buf.append("</ul>\n");
109                 
110                 buf.append("</nav>\n");
111         }
112         
113         private static String toURL(Category catActive, String entity) {
114                 try {
115                         return catActive.getURL() + "?entity=" + URLEncoder.encode(entity, "UTF-8");
116                 } catch (UnsupportedEncodingException e) {
117                         LOG.log(Level.SEVERE, "Failed to generate link to entity " + entity, e);
118                         return catActive.getURL();
119                 }
120         }
121         
122         public static String toHTML(List<Article> articles, Category catActive, String entityActive, Config cfg, ArticleProvider provider) {
123                 StringBuffer buf;
124                 int i;
125                 List<EntityStat> entities;
126                 String cl;
127                 
128                 buf = new StringBuffer();
129                 buf.append("<!DOCTYPE html>\n");
130                 buf.append("<html lang='fr'>\n");
131                 buf.append("<head>\n");
132                 buf.append("<meta charset=\"UTF-8\">\n");
133                 buf.append("<link rel='stylesheet' href='/style.css' />\n");
134                 buf.append("<title>");
135                 buf.append(catActive.getTitle());
136                 buf.append(" - PNews</title>\n");
137                 buf.append("</head>\n");
138                 buf.append("<body>\n");
139                 
140                 appendMenu(buf, catActive, cfg);
141                 
142                 try {
143                         entities = provider.getEntityStats(catActive);
144
145                         if (entities.size() > 0) {
146                                 buf.append("<nav>");
147                                 buf.append("<ul>");
148                                 i = 0;
149                                 for (EntityStat s: entities) {
150                                         buf.append("<li>");
151                                         if (entityActive != null && s.getEntity().equals(entityActive))
152                                                 cl = "active";
153                                         else
154                                                 cl = null;
155                                         appendA(buf, s.getEntity(), toURL(catActive, s.getEntity()), cl);
156                                         buf.append("</li>\n");
157                                         i++;
158                                         if (i > 10)
159                                                 break;
160                                 }                               
161                                 buf.append("</ul>\n");
162                                 buf.append("</nav>\n");
163                         }
164                 } catch (IllegalArgumentException | FeedException | IOException e2) {
165                         LOG.log(Level.SEVERE, "Failed to get entities", e2);
166                 }
167                 
168                 i = 0;
169                 for (Article e: articles) {
170                         try {
171                                 append(buf, e);
172                         } catch (UnsupportedEncodingException e1) {
173                                 LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
174                         }
175                         if (i == 100)
176                                 break;
177                         else
178                                 i++;
179                 }
180                 
181                 buf.append("</body>\n");
182                 buf.append("</html>\n");
183         
184                 return buf.toString();
185         }
186 }