9478fa233752a77bd48a081a5928226a13699d78
[pnews.git] / war / src / main / java / pnews / HTML.java
1 package pnews;
2
3 import java.util.List;
4
5 public class HTML {
6         private static void appendA(StringBuffer buf, String child, String href, String cl) {
7                 buf.append("<a href='");
8                 buf.append(href);
9                 buf.append("'");
10                 if (cl != null) {
11                         buf.append(" class='");
12                         buf.append(cl);
13                         buf.append('\'');
14                 }
15                 buf.append('>');
16                 buf.append(child);
17                 buf.append("</a>");
18         }
19         
20         private static void appendDiv(StringBuffer buf, String child) {
21                 buf.append("<div>");
22                 buf.append(child);
23                 buf.append("</div>\n");
24         }
25         
26         private static void appendP(StringBuffer buf, String child) {
27                 buf.append("<p>");
28                 buf.append(child);
29                 buf.append("</p>\n");
30         }
31         
32         private static void append(StringBuffer buf, Article a) {               
33                 buf.append("<div class='article'>\n");
34                 
35                 buf.append("<div class='article-image'>\n");
36                 if (a.thumbnail != null) {
37                         buf.append("<img class='left' src='");
38                         buf.append(a.thumbnail);
39                         buf.append("'/>\n");
40                 }
41                 buf.append("</div>\n");
42                 
43                 buf.append("<div class='article-content'>\n");
44
45                 buf.append("<div class='article-title'>\n");
46                 appendA(buf, a.title, "redirect?url="+a.link, null);
47                 buf.append("</div>\n");
48                 
49                 buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
50                 
51                 buf.append("<div class='article-description'>\n");
52                 if (a.description != null) {
53                         buf.append("<p>");
54                         buf.append(a.description);
55                         buf.append("</p>");
56                 }
57                 buf.append("</div>\n");         
58                 
59                 buf.append("</div>\n");
60                 
61                 buf.append("</div>\n");         
62         }
63         
64         private static void appendMenu(StringBuffer buf, Category catActive) {
65                 String cl;
66                 
67                 buf.append("<nav>\n");
68                 buf.append("<ul>\n");
69
70                 for (Category cat: Category.values()) {
71                         buf.append("<li>");
72                         
73                         if (cat.equals(catActive))
74                                 cl = "active";
75                         else
76                                 cl = null;
77                         
78                         appendA(buf, cat.getId(), cat.getId(), cl);
79                         buf.append("</li>");
80                 }
81                 
82                 buf.append("</ul>\n");
83                 buf.append("</nav>\n");
84         }
85         
86         public static String toHTML(List<Article> articles, Category catActive) {
87                 StringBuffer buf;
88                 int i;
89                 
90                 buf = new StringBuffer();
91                 buf.append("<!DOCTYPE html>\n");
92                 buf.append("<html lang='fr'>\n");
93                 buf.append("<head>\n");
94                 buf.append("<meta charset=\"UTF-8\">\n");
95                 buf.append("<link rel='stylesheet' href='style.css' />\n");
96                 buf.append("<title>PNews</title>\n");
97                 buf.append("</head>\n");
98                 buf.append("<body>\n");
99                 
100                 appendMenu(buf, catActive);
101                 
102                 i = 0;
103                 for (Article e: articles) {
104                         append(buf, e);
105                         if (i == 100)
106                                 break;
107                         else
108                                 i++;
109                 }
110                 
111                 buf.append("</body>\n");
112                 buf.append("</html>\n");
113         
114                 return buf.toString();
115         }
116 }