3 import java.io.IOException;
4 import java.io.InputStreamReader;
6 import java.io.UnsupportedEncodingException;
7 import java.util.ArrayList;
8 import java.util.HashMap;
11 import java.util.logging.Logger;
13 import javax.json.Json;
14 import javax.json.JsonArray;
15 import javax.json.JsonObject;
16 import javax.json.JsonValue;
18 import pnews.Category;
23 private Category[] categories;
24 private static final Logger LOG = Logger.getLogger(Config.class.getName());
26 private void loadCategories(JsonArray jcats) {
29 String id, label, title, language;
31 cats = new ArrayList<>(jcats.size());
33 for (JsonValue v: jcats) {
35 id = jcat.getString("id");
36 label = jcat.getString("label");
37 title = jcat.getString("title");
38 language = jcat.getString("language");
39 cats.add(new Category(id, label, title, language));
42 categories = cats.toArray(new Category[0]);
45 private Category getCategory(String id) {
46 for (Category c: categories)
47 if (c.getId().equals(id))
52 public void loadConfig() throws UnsupportedEncodingException {
54 JsonObject jfeeds, jroot;
59 r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
61 jroot = Json.createReader(r).readObject();
64 try { r.close(); } catch (IOException e) { };
68 loadCategories(jroot.getJsonArray("categories"));
70 jfeeds = jroot.getJsonObject("feeds");
72 feedList = new ArrayList<Feed>(jfeeds.size());
74 jfeeds.forEach((k, v)-> {
78 JsonArray jcategories;
81 jcategories = jf.getJsonArray("categories");
82 str = jcategories.getString(0);
84 cat = getCategory(str);
87 feedList.add(new Feed(k, cat));
89 LOG.severe("Missing category: " + str);
92 feeds = feedList.toArray(new Feed[0]);
95 public Feed[] getFeeds() {
99 public Map<Category, List<Feed>> getFeedsByCategory() {
100 Map<Category, List<Feed>> result;
105 result = new HashMap<>();
108 for (Feed f: feeds) {
109 cat = f.getCategory();
111 catFeeds = result.get(cat);
112 if (catFeeds == null) {
113 catFeeds = new ArrayList<Feed>();
114 result.put(cat, catFeeds);
122 public Category[] getCategories() {
126 public Category getDefaultCategory() {
127 return categories[0];
130 public static void main(String[] args) throws UnsupportedEncodingException {
138 cats = cfg.getCategories();
139 for (Category cat: cats)
140 System.out.println(cat);
142 feeds = cfg.getFeeds();
144 System.out.println("Number of feeds: " + feeds.length);
146 System.out.println(f);