fixed url of a rss
[pnews.git] / war / src / main / java / pnews / servlet / HTML.java
index b78d29b..42564e8 100644 (file)
@@ -1,13 +1,18 @@
 package 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 pnews.Article;
 import pnews.Category;
+import pnews.EntityStat;
+import pnews.Language;
 
 public class HTML {
         private static final String CLASS_NAME= HTML.class.getName();
@@ -53,7 +58,7 @@ public class HTML {
                buf.append("<div class='article-content'>\n");
 
                buf.append("<div class='article-title'>\n");
-               appendA(buf, a.title, "redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
+               appendA(buf, a.title, "/redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
                buf.append("</div>\n");
                
                buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
@@ -71,13 +76,16 @@ public class HTML {
                buf.append("</div>\n");         
        }
        
-       private static void appendMenu(StringBuffer buf, Category catActive) {
+       private static void appendMenu(StringBuffer buf, Category catActive, Config cfg) {
                String cl;
                
                buf.append("<nav>\n");
                buf.append("<ul>\n");
 
-               for (Category cat: Category.values()) {
+               for (Category cat: cfg.getCategories()) {
+                       if (!cat.getLanguage().equals(catActive.getLanguage()))
+                               continue;
+                       
                        buf.append("<li>");
                        
                        if (cat.equals(catActive))
@@ -85,36 +93,68 @@ public class HTML {
                        else
                                cl = null;
                        
-                       appendA(buf, cat.getId(), cat.getId(), cl);
+                       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");
        }
        
-       public static String toHTML(List<Article> articles, Category catActive) {
+       public static String toHTML(List<Article> articles, Category catActive, Config cfg, ArticleProvider provider) {
                StringBuffer buf;
                int i;
+               Category[] cats;
+               List<EntityStat> entities;
                
                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>PNews</title>\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);
+               cats = cfg.getCategories();
+               
+               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>");
+                                       buf.append(s.getEntity());
+                                       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, "fail to convert article to HTML", e1);
+                                LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
                         }
                        if (i == 100)
                                break;