cleanup, moved to net.wpitchoune package
[pnews.git] / war / src / main / java / net / wpitchoune / pnews / servlet / HTML.java
diff --git a/war/src/main/java/net/wpitchoune/pnews/servlet/HTML.java b/war/src/main/java/net/wpitchoune/pnews/servlet/HTML.java
new file mode 100644 (file)
index 0000000..28d6fac
--- /dev/null
@@ -0,0 +1,187 @@
+package net.wpitchoune.pnews.servlet;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.rometools.rome.io.FeedException;
+
+import net.wpitchoune.pnews.Article;
+import net.wpitchoune.pnews.Category;
+import net.wpitchoune.pnews.Config;
+import net.wpitchoune.pnews.EntityStat;
+import net.wpitchoune.pnews.Language;
+
+public class HTML {
+        private static final String CLASS_NAME= HTML.class.getName();
+        private static final Logger LOG = Logger.getLogger(CLASS_NAME);
+        
+       private static void appendA(StringBuffer buf, String child, String href, String cl) {
+               buf.append("<a href='");
+               buf.append(href);
+               buf.append("'");
+               if (cl != null) {
+                       buf.append(" class='");
+                       buf.append(cl);
+                       buf.append('\'');
+               }
+               buf.append('>');
+               buf.append(child);
+               buf.append("</a>");
+       }
+       
+       private static void appendDiv(StringBuffer buf, String child) {
+               buf.append("<div>");
+               buf.append(child);
+               buf.append("</div>\n");
+       }
+       
+       private static void appendP(StringBuffer buf, String child) {
+               buf.append("<p>");
+               buf.append(child);
+               buf.append("</p>\n");
+       }
+       
+       private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {           
+               buf.append("<div class='article'>\n");
+               
+               buf.append("<div class='article-image'>\n");
+               if (a.getThumbnail() != null) {
+                       buf.append("<img class='left' src='");
+                       buf.append(a.getThumbnail());
+                       buf.append("'/>\n");
+               }
+               buf.append("</div>\n");
+               
+               buf.append("<div class='article-content'>\n");
+
+               buf.append("<div class='article-title'>\n");
+               appendA(buf, a.getTitle(), "/redirect?url=" + URLEncoder.encode(a.getLink(), "UTF-8"), null);
+               buf.append("</div>\n");
+               
+               buf.append("<div class='article-info'>" + a.getWebsite() + " - " + a.getPublicationDate() + "</div>");
+               
+               buf.append("<div class='article-description'>\n");
+               if (a.getDescription() != null) {
+                       buf.append("<p>");
+                       if (a.getDescription().length() < 512) {
+                               buf.append(a.getDescription());
+                       } else {
+                               buf.append(a.getDescription().substring(0, 512));
+                               buf.append("[..]");
+                       }
+                       buf.append("</p>");
+               }
+                buf.append("</div>\n");                
+               
+                buf.append("</div>\n");
+                
+               buf.append("</div>\n");         
+       }
+       
+       private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
+               String cl;
+               
+               buf.append("<nav>\n");
+               buf.append("<ul>\n");
+
+               for (Category cat: cfg.getCategories()) {
+                       if (!cat.getLanguage().equals(catActive.getLanguage()))
+                               continue;
+                       
+                       buf.append("<li>");
+                       
+                       if (cat.equals(catActive))
+                               cl = "active";
+                       else
+                               cl = null;
+                       
+                       appendA(buf, cat.getLabel(), cat.getURL(), cl);
+                       buf.append("</li>");
+               }
+               
+               for (Language l: cfg.getLanguages())
+                       buf.append("<li><a href='" + l.toURL() + "'>" + l.getLabel() + "</a></li>");
+                
+               buf.append("</ul>\n");
+               
+               buf.append("</nav>\n");
+       }
+       
+       private static String toURL(Category catActive, String entity) {
+               try {
+                       return catActive.getURL() + "?entity=" + URLEncoder.encode(entity, "UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       LOG.log(Level.SEVERE, "Failed to generate link to entity " + entity, e);
+                       return catActive.getURL();
+               }
+       }
+       
+       public static String toHTML(List<Article> articles, Category catActive, String entityActive, Config cfg, ArticleProvider provider) {
+               StringBuffer buf;
+               int i;
+               List<EntityStat> entities;
+               String cl;
+               
+               buf = new StringBuffer();
+               buf.append("<!DOCTYPE html>\n");
+               buf.append("<html lang='fr'>\n");
+               buf.append("<head>\n");
+               buf.append("<meta charset=\"UTF-8\">\n");
+               buf.append("<link rel='stylesheet' href='/style.css' />\n");
+               buf.append("<title>");
+               buf.append(catActive.getTitle());
+               buf.append(" - PNews</title>\n");
+               buf.append("</head>\n");
+               buf.append("<body>\n");
+               
+               appendMenu(buf, catActive, cfg);
+               
+               try {
+                       entities = provider.getEntityStats(catActive);
+
+                       if (entities.size() > 0) {
+                               buf.append("<nav>");
+                               buf.append("<ul>");
+                               i = 0;
+                               for (EntityStat s: entities) {
+                                       buf.append("<li>");
+                                       if (entityActive != null && s.getEntity().equals(entityActive))
+                                               cl = "active";
+                                       else
+                                               cl = null;
+                                       appendA(buf, s.getEntity(), toURL(catActive, s.getEntity()), cl);
+                                       buf.append("</li>\n");
+                                       i++;
+                                       if (i > 10)
+                                               break;
+                               }                               
+                               buf.append("</ul>\n");
+                               buf.append("</nav>\n");
+                       }
+                } catch (IllegalArgumentException | FeedException | IOException e2) {
+                        LOG.log(Level.SEVERE, "Failed to get entities", e2);
+                }
+               
+               i = 0;
+               for (Article e: articles) {
+                       try {
+                                append(buf, e);
+                        } catch (UnsupportedEncodingException e1) {
+                                LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
+                        }
+                       if (i == 100)
+                               break;
+                       else
+                               i++;
+               }
+               
+               buf.append("</body>\n");
+               buf.append("</html>\n");
+       
+               return buf.toString();
+       }
+}