provide some stats as json
[pnews.git] / war / src / main / java / pnews / HTML.java
index 74df5e7..4b4af8b 100644 (file)
@@ -1,8 +1,15 @@
 package pnews;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 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);
@@ -29,26 +36,35 @@ public class HTML {
                buf.append("</p>\n");
        }
        
-       private static void append(StringBuffer buf, Article a) {               
+       private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {           
                buf.append("<div class='article'>\n");
                
-               buf.append("<h2>");
+               buf.append("<div class='article-image'>\n");
                if (a.thumbnail != null) {
                        buf.append("<img class='left' src='");
                        buf.append(a.thumbnail);
                        buf.append("'/>\n");
                }
-               appendA(buf, a.title, "redirect?url="+a.link, null);
-               buf.append("</h2>\n");
+               buf.append("</div>\n");
+               
+               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);
+               buf.append("</div>\n");
                
                buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
                
+               buf.append("<div class='article-description'>\n");
                if (a.description != null) {
                        buf.append("<p>");
                        buf.append(a.description);
                        buf.append("</p>");
                }
+                buf.append("</div>\n");                
                
+                buf.append("</div>\n");
+                
                buf.append("</div>\n");         
        }
        
@@ -76,6 +92,7 @@ public class HTML {
        
        public static String toHTML(List<Article> articles, Category catActive) {
                StringBuffer buf;
+               int i;
                
                buf = new StringBuffer();
                buf.append("<!DOCTYPE html>\n");
@@ -89,8 +106,18 @@ public class HTML {
                
                appendMenu(buf, catActive);
                
-               for (Article e: articles)
-                       append(buf, e);
+               i = 0;
+               for (Article e: articles) {
+                       try {
+                                append(buf, e);
+                        } catch (UnsupportedEncodingException e1) {
+                                LOG.log(Level.SEVERE, "fail to convert article to HTML", e1);
+                        }
+                       if (i == 100)
+                               break;
+                       else
+                               i++;
+               }
                
                buf.append("</body>\n");
                buf.append("</html>\n");