multi languages support
[pnews.git] / war / src / main / java / pnews / servlet / HTML.java
1 package pnews.servlet;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.List;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 import pnews.Article;
10 import pnews.Category;
11
12 public class HTML {
13         private static final String CLASS_NAME= HTML.class.getName();
14         private static final Logger LOG = Logger.getLogger(CLASS_NAME);
15         
16         private static void appendA(StringBuffer buf, String child, String href, String cl) {
17                 buf.append("<a href='");
18                 buf.append(href);
19                 buf.append("'");
20                 if (cl != null) {
21                         buf.append(" class='");
22                         buf.append(cl);
23                         buf.append('\'');
24                 }
25                 buf.append('>');
26                 buf.append(child);
27                 buf.append("</a>");
28         }
29         
30         private static void appendDiv(StringBuffer buf, String child) {
31                 buf.append("<div>");
32                 buf.append(child);
33                 buf.append("</div>\n");
34         }
35         
36         private static void appendP(StringBuffer buf, String child) {
37                 buf.append("<p>");
38                 buf.append(child);
39                 buf.append("</p>\n");
40         }
41         
42         private static void append(StringBuffer buf, Article a) throws UnsupportedEncodingException {           
43                 buf.append("<div class='article'>\n");
44                 
45                 buf.append("<div class='article-image'>\n");
46                 if (a.thumbnail != null) {
47                         buf.append("<img class='left' src='");
48                         buf.append(a.thumbnail);
49                         buf.append("'/>\n");
50                 }
51                 buf.append("</div>\n");
52                 
53                 buf.append("<div class='article-content'>\n");
54
55                 buf.append("<div class='article-title'>\n");
56                 appendA(buf, a.title, "/redirect?url=" + URLEncoder.encode(a.link, "UTF-8"), null);
57                 buf.append("</div>\n");
58                 
59                 buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
60                 
61                 buf.append("<div class='article-description'>\n");
62                 if (a.description != null) {
63                         buf.append("<p>");
64                         buf.append(a.description);
65                         buf.append("</p>");
66                 }
67                 buf.append("</div>\n");         
68                 
69                 buf.append("</div>\n");
70                 
71                 buf.append("</div>\n");         
72         }
73         
74         private static void appendMenu(StringBuffer buf, Category catActive, Category[] cats) {
75                 String cl;
76                 
77                 buf.append("<nav>\n");
78                 buf.append("<ul>\n");
79
80                 for (Category cat: cats) {
81                         buf.append("<li>");
82                         
83                         if (cat.equals(catActive))
84                                 cl = "active";
85                         else
86                                 cl = null;
87                         
88                         appendA(buf, cat.getLabel(), cat.getURL(), cl);
89                         buf.append("</li>");
90                 }
91                 buf.append("<li><a href='/en'>EN</a></li>");
92                 buf.append("<li><a href='/fr'>FR</a></li>");            
93                 buf.append("</ul>\n");
94                 
95                 buf.append("</nav>\n");
96         }
97         
98         public static String toHTML(List<Article> articles, Category catActive, Category[] cats) {
99                 StringBuffer buf;
100                 int i;
101                 
102                 buf = new StringBuffer();
103                 buf.append("<!DOCTYPE html>\n");
104                 buf.append("<html lang='fr'>\n");
105                 buf.append("<head>\n");
106                 buf.append("<meta charset=\"UTF-8\">\n");
107                 buf.append("<link rel='stylesheet' href='/style.css' />\n");
108                 buf.append("<title>");
109                 buf.append(catActive.getTitle());
110                 buf.append(" - PNews</title>\n");
111                 buf.append("</head>\n");
112                 buf.append("<body>\n");
113                 
114                 appendMenu(buf, catActive, cats);
115                 
116                 i = 0;
117                 for (Article e: articles) {
118                         try {
119                                 append(buf, e);
120                         } catch (UnsupportedEncodingException e1) {
121                                 LOG.log(Level.SEVERE, "Failed to convert article to HTML", e1);
122                         }
123                         if (i == 100)
124                                 break;
125                         else
126                                 i++;
127                 }
128                 
129                 buf.append("</body>\n");
130                 buf.append("</html>\n");
131         
132                 return buf.toString();
133         }
134 }