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;
9 import java.util.HashSet;
10 import java.util.List;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
16 import javax.json.Json;
17 import javax.json.JsonArray;
18 import javax.json.JsonObject;
19 import javax.json.JsonString;
20 import javax.json.JsonValue;
22 import pnews.Category;
24 import pnews.Language;
28 private Category[] categories;
29 private Language[] languages;
30 private final Set<String> blacklistedEntities = new HashSet<>();
31 private static final String CLASS_NAME = Config.class.getName();
34 * The key is the language, the value is the default category for this language.
36 private Map<String, Category> defaultCategories = new HashMap<>();
37 private static final Logger LOG = Logger.getLogger(CLASS_NAME);
39 private void loadCategories(JsonArray jcats) {
43 String id, label, title, language;
45 cats = new ArrayList<>(jcats.size());
47 for (JsonValue v: jcats) {
49 id = jcat.getString("id");
50 label = jcat.getString("label");
51 title = jcat.getString("title");
52 language = jcat.getString("language");
53 cat = new Category(id, label, title, language);
55 if (defaultCategories.get(language) == null)
56 defaultCategories.put(language, cat);
59 categories = cats.toArray(new Category[0]);
62 private void loadLanguages(JsonArray jlangs) {
67 langs = new ArrayList<>(jlangs.size());
69 for (JsonValue v: jlangs) {
70 jlang = (JsonObject)v;
71 id = jlang.getString("id");
72 langs.add(new Language(id));
75 languages = langs.toArray(new Language[0]);
78 private Category getCategory(String id) {
79 for (Category c: categories)
80 if (c.getId().equals(id))
85 private void loadEntities(JsonObject jroot) {
88 final String METHOD_NAME = "loadEntities";
90 jentities = jroot.getJsonObject("entities");
91 jblacklist = jentities.getJsonArray("blacklist");
93 jblacklist.forEach((jv)-> {
97 blacklistedEntities.add(js.getString());
100 LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " blacklistedEntities=" + blacklistedEntities);
103 public void loadConfig() throws UnsupportedEncodingException {
105 JsonObject jfeeds, jroot;
110 r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
112 jroot = Json.createReader(r).readObject();
115 try { r.close(); } catch (IOException e) { };
118 loadLanguages(jroot.getJsonArray("languages"));
119 loadCategories(jroot.getJsonArray("categories"));
121 jfeeds = jroot.getJsonObject("feeds");
123 feedList = new ArrayList<Feed>(jfeeds.size());
125 jfeeds.forEach((k, v)-> {
129 JsonArray jcategories;
132 jcategories = jf.getJsonArray("categories");
133 str = jcategories.getString(0);
135 cat = getCategory(str);
138 feedList.add(new Feed(k, cat));
140 LOG.severe("Missing category: " + str);
143 feeds = feedList.toArray(new Feed[0]);
148 public boolean isBlacklistedEntity(String e) {
149 final String METHOD_NAME = "isBlacklistedEntity";
152 LOG.entering(CLASS_NAME, METHOD_NAME, e);
154 result = blacklistedEntities.contains(e);
156 LOG.exiting(CLASS_NAME, METHOD_NAME, result);
161 public Feed[] getFeeds() {
165 public Map<Category, List<Feed>> getFeedsByCategory() {
166 Map<Category, List<Feed>> result;
171 result = new HashMap<>();
174 for (Feed f: feeds) {
175 cat = f.getCategory();
177 catFeeds = result.get(cat);
178 if (catFeeds == null) {
179 catFeeds = new ArrayList<Feed>();
180 result.put(cat, catFeeds);
188 public Category[] getCategories() {
192 public Category getDefaultCategory(Language lang) {
193 return defaultCategories.get(lang.getId());
196 public Language[] getLanguages() {
200 public Language getDefaultLanguage() {
204 public static void main(String[] args) throws UnsupportedEncodingException {
212 cats = cfg.getCategories();
213 for (Category cat: cats)
214 System.out.println(cat);
216 feeds = cfg.getFeeds();
218 System.out.println("Number of feeds: " + feeds.length);
220 System.out.println(f);