3 import java.io.IOException;
4 import java.io.InputStreamReader;
6 import java.io.UnsupportedEncodingException;
7 import java.time.Instant;
8 import java.time.temporal.ChronoUnit;
9 import java.time.temporal.TemporalUnit;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
19 import javax.json.Json;
20 import javax.json.JsonArray;
21 import javax.json.JsonObject;
22 import javax.json.JsonString;
23 import javax.json.JsonValue;
26 import pnews.Category;
28 import pnews.Language;
32 private Category[] categories;
33 private Language[] languages;
34 private final Set<String> blacklistedEntities = new HashSet<>();
35 private final HashMap<String, String> entityAliases = new HashMap<>();
36 private static final String CLASS_NAME = Config.class.getName();
39 * The key is the language, the value is the default category for this language.
41 private Map<String, Category> defaultCategories = new HashMap<>();
42 private static final Logger LOG = Logger.getLogger(CLASS_NAME);
44 private void loadCategories(JsonArray jcats) {
48 String id, label, title, language;
50 cats = new ArrayList<>(jcats.size());
52 for (JsonValue v: jcats) {
54 id = jcat.getString("id");
55 label = jcat.getString("label");
56 title = jcat.getString("title");
57 language = jcat.getString("language");
58 cat = new Category(id, label, title, language);
60 if (defaultCategories.get(language) == null)
61 defaultCategories.put(language, cat);
64 categories = cats.toArray(new Category[0]);
67 private void loadLanguages(JsonArray jlangs) {
72 langs = new ArrayList<>(jlangs.size());
74 for (JsonValue v: jlangs) {
75 jlang = (JsonObject)v;
76 id = jlang.getString("id");
77 langs.add(new Language(id));
80 languages = langs.toArray(new Language[0]);
83 private Category getCategory(String id) {
84 for (Category c: categories)
85 if (c.getId().equals(id))
90 private void loadEntities(JsonObject jroot) {
91 JsonObject jentities, jaliases;
93 final String METHOD_NAME = "loadEntities";
95 jentities = jroot.getJsonObject("entities");
97 jblacklist = jentities.getJsonArray("blacklist");
98 jblacklist.forEach((jv)-> {
102 blacklistedEntities.add(js.getString());
105 jaliases = jentities.getJsonObject("aliases");
106 jaliases.forEach((k, v)-> {
107 JsonArray jsources = (JsonArray)v;
109 jsources.forEach((jsource)-> {
110 entityAliases.put(((JsonString)jsource).getString(), k);
114 LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " blacklistedEntities=" + blacklistedEntities);
115 LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " entityAliases=" + entityAliases);
118 public String getEntityAlias(String entity) {
121 result = entityAliases.get(entity);
129 public void loadConfig() throws UnsupportedEncodingException {
131 JsonObject jfeeds, jroot;
136 r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
138 jroot = Json.createReader(r).readObject();
141 try { r.close(); } catch (IOException e) { };
144 loadLanguages(jroot.getJsonArray("languages"));
145 loadCategories(jroot.getJsonArray("categories"));
147 jfeeds = jroot.getJsonObject("feeds");
149 feedList = new ArrayList<Feed>(jfeeds.size());
151 jfeeds.forEach((k, v)-> {
155 JsonArray jcategories;
158 jcategories = jf.getJsonArray("categories");
159 str = jcategories.getString(0);
161 cat = getCategory(str);
164 feedList.add(new Feed(k, cat));
166 LOG.severe("Missing category: " + str);
169 feeds = feedList.toArray(new Feed[0]);
174 public boolean isBlacklistedEntity(String e) {
175 final String METHOD_NAME = "isBlacklistedEntity";
178 LOG.entering(CLASS_NAME, METHOD_NAME, e);
180 result = blacklistedEntities.contains(e);
182 LOG.exiting(CLASS_NAME, METHOD_NAME, result);
187 public boolean isObsolete(Instant instant) {
188 Instant olderInstant;
190 olderInstant = Instant.now().minus(60, ChronoUnit.DAYS);
192 if (instant.isAfter(olderInstant))
198 public Feed[] getFeeds() {
202 public Map<Category, List<Feed>> getFeedsByCategory() {
203 Map<Category, List<Feed>> result;
208 result = new HashMap<>();
211 for (Feed f: feeds) {
212 cat = f.getCategory();
214 catFeeds = result.get(cat);
215 if (catFeeds == null) {
216 catFeeds = new ArrayList<Feed>();
217 result.put(cat, catFeeds);
225 public Category[] getCategories() {
229 public Category getDefaultCategory(Language lang) {
230 return defaultCategories.get(lang.getId());
233 public Language[] getLanguages() {
237 public Language getDefaultLanguage() {
241 public static void main(String[] args) throws UnsupportedEncodingException {
249 cats = cfg.getCategories();
250 for (Category cat: cats)
251 System.out.println(cat);
253 feeds = cfg.getFeeds();
255 System.out.println("Number of feeds: " + feeds.length);
257 System.out.println(f);