load languages from configuration file
[pnews.git] / war / src / main / java / pnews / servlet / Config.java
1 package pnews.servlet;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.Reader;
6 import java.io.UnsupportedEncodingException;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.logging.Logger;
12
13 import javax.json.Json;
14 import javax.json.JsonArray;
15 import javax.json.JsonObject;
16 import javax.json.JsonValue;
17
18 import pnews.Category;
19 import pnews.Feed;
20 import pnews.Language;
21
22 public class Config {
23         private Feed[] feeds;
24         private Category[] categories;
25         private Language[] languages;
26         private static final Logger LOG = Logger.getLogger(Config.class.getName());
27                 
28         private void loadCategories(JsonArray jcats) {
29                 List<Category> cats;
30                 JsonObject jcat;
31                 String id, label, title, language;
32                 
33                 cats = new ArrayList<>(jcats.size());
34                 
35                 for (JsonValue v: jcats) {
36                         jcat = (JsonObject)v;
37                         id = jcat.getString("id");
38                         label = jcat.getString("label");
39                         title = jcat.getString("title");
40                         language = jcat.getString("language");
41                         cats.add(new Category(id, label, title, language));
42                 }
43                 
44                 categories = cats.toArray(new Category[0]);
45         }
46
47         private void loadLanguages(JsonArray jlangs) {
48                 List<Language> langs;
49                 JsonObject jlang;
50                 String id;
51                 
52                 langs = new ArrayList<>(jlangs.size());
53                 
54                 for (JsonValue v: jlangs) {
55                         jlang = (JsonObject)v;
56                         id = jlang.getString("id");
57                         langs.add(new Language(id));
58                 }
59                 
60                 languages = langs.toArray(new Language[0]);
61         }
62         
63         private Category getCategory(String id) {
64                 for (Category c: categories)
65                         if (c.getId().equals(id))
66                                 return c;
67                 return null;
68         }
69         
70         public void loadConfig() throws UnsupportedEncodingException {
71                 Reader r;
72                 JsonObject jfeeds, jroot;
73                 List<Feed> feedList;
74                 
75                 r = null;
76                 try {
77                         r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
78                                                   "UTF-8");
79                         jroot = Json.createReader(r).readObject();
80                 } finally {
81                         if (r != null)
82                                 try { r.close(); } catch (IOException e) { };
83                 }
84                 
85                 loadLanguages(jroot.getJsonArray("languages"));
86                 loadCategories(jroot.getJsonArray("categories"));
87                 
88                 jfeeds = jroot.getJsonObject("feeds");
89                 
90                 feedList = new ArrayList<Feed>(jfeeds.size());
91
92                 jfeeds.forEach((k, v)-> {
93                         JsonObject jf;
94                         String str;
95                         Category cat;
96                         JsonArray jcategories;
97                         
98                         jf = (JsonObject)v;
99                         jcategories = jf.getJsonArray("categories");
100                         str = jcategories.getString(0);
101                         
102                         cat = getCategory(str);
103                         
104                         if (cat != null)
105                                 feedList.add(new Feed(k, cat));
106                         else
107                                 LOG.severe("Missing category: " + str);
108                 });
109                 
110                 feeds = feedList.toArray(new Feed[0]);
111         }
112         
113         public Feed[] getFeeds() {
114                 return feeds;
115         }
116         
117         public Map<Category, List<Feed>> getFeedsByCategory() {
118                 Map<Category, List<Feed>> result;
119                 Feed[] feeds;
120                 List<Feed> catFeeds;
121                 Category cat;
122                 
123                 result = new HashMap<>();
124                 
125                 feeds = getFeeds();
126                 for (Feed f: feeds) {
127                         cat = f.getCategory();
128                 
129                         catFeeds = result.get(cat);
130                         if (catFeeds == null) {
131                                 catFeeds = new ArrayList<Feed>();
132                                 result.put(cat, catFeeds);
133                         }
134                         catFeeds.add(f);
135                 }
136                 
137                 return result;
138         }
139
140         public Category[] getCategories() {
141                 return categories;
142         }
143         
144         public Category getDefaultCategory() {
145                 return categories[0];
146         }
147         
148         public Language[] getLanguages() {
149                 return languages;
150         }
151         
152         public Language getDefaultLanguage() {
153                 return languages[0];
154         }
155         
156         public static void main(String[] args) throws UnsupportedEncodingException {
157                 Config cfg;
158                 Feed[] feeds;           
159                 Category[] cats;
160
161                 cfg = new Config();
162                 cfg.loadConfig();
163                 
164                 cats = cfg.getCategories();
165                 for (Category cat: cats)
166                         System.out.println(cat);
167                 
168                 feeds = cfg.getFeeds();
169                 
170                 System.out.println("Number of feeds: " + feeds.length);
171                 for (Feed f: feeds)
172                         System.out.println(f);
173         }
174 }