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 final String[] languages = { "fr", "en" };
25 private static final Logger LOG = Logger.getLogger(Config.class.getName());
27 private void loadCategories(JsonArray jcats) {
30 String id, label, title, language;
32 cats = new ArrayList<>(jcats.size());
34 for (JsonValue v: jcats) {
36 id = jcat.getString("id");
37 label = jcat.getString("label");
38 title = jcat.getString("title");
39 language = jcat.getString("language");
40 cats.add(new Category(id, label, title, language));
43 categories = cats.toArray(new Category[0]);
46 private Category getCategory(String id) {
47 for (Category c: categories)
48 if (c.getId().equals(id))
53 public void loadConfig() throws UnsupportedEncodingException {
55 JsonObject jfeeds, jroot;
60 r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
62 jroot = Json.createReader(r).readObject();
65 try { r.close(); } catch (IOException e) { };
69 loadCategories(jroot.getJsonArray("categories"));
71 jfeeds = jroot.getJsonObject("feeds");
73 feedList = new ArrayList<Feed>(jfeeds.size());
75 jfeeds.forEach((k, v)-> {
79 JsonArray jcategories;
82 jcategories = jf.getJsonArray("categories");
83 str = jcategories.getString(0);
85 cat = getCategory(str);
88 feedList.add(new Feed(k, cat));
90 LOG.severe("Missing category: " + str);
93 feeds = feedList.toArray(new Feed[0]);
96 public Feed[] getFeeds() {
100 public Map<Category, List<Feed>> getFeedsByCategory() {
101 Map<Category, List<Feed>> result;
106 result = new HashMap<>();
109 for (Feed f: feeds) {
110 cat = f.getCategory();
112 catFeeds = result.get(cat);
113 if (catFeeds == null) {
114 catFeeds = new ArrayList<Feed>();
115 result.put(cat, catFeeds);
123 public Category[] getCategories() {
127 public Category getDefaultCategory() {
128 return categories[0];
131 public String[] getLanguages() {
135 public static void main(String[] args) throws UnsupportedEncodingException {
143 cats = cfg.getCategories();
144 for (Category cat: cats)
145 System.out.println(cat);
147 feeds = cfg.getFeeds();
149 System.out.println("Number of feeds: " + feeds.length);
151 System.out.println(f);