read categories information from the configuration
[pnews.git] / war / src / main / java / pnews / servlet / Config.java
index a77eb88..7e2b642 100644 (file)
@@ -3,14 +3,17 @@ package pnews.servlet;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Logger;
 
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
+import javax.json.JsonValue;
 
 import pnews.Category;
 import pnews.Feed;
@@ -18,26 +21,55 @@ import pnews.Feed;
 public class Config {
         private Feed[] feeds;
         private Category[] categories;
+        private static final Logger LOG = Logger.getLogger(Config.class.getName());
                 
-        public void loadConfig() {
+        private void loadCategories(JsonArray jcats) {
+                List<Category> cats;
+                JsonObject jcat;
+                String id, label, title, language;
+                
+                cats = new ArrayList<>(jcats.size());
+                
+                for (JsonValue v: jcats) {
+                        jcat = (JsonObject)v;
+                        id = jcat.getString("id");
+                        label = jcat.getString("label");
+                        title = jcat.getString("title");
+                        language = jcat.getString("language");
+                        cats.add(new Category(id, label, title, language));
+                }
+                
+                categories = cats.toArray(new Category[0]);
+        }
+        
+        private Category getCategory(String id) {
+                for (Category c: categories)
+                        if (c.getId().equals(id))
+                                return c;
+                return null;
+        }
+        
+        public void loadConfig() throws UnsupportedEncodingException {
                 Reader r;
                 JsonObject jfeeds, jroot;
                 List<Feed> feedList;
-                Map<String, Category> cats;
                 
                 r = null;
                 try {
-                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"));
+                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
+                                                  "UTF-8");
                         jroot = Json.createReader(r).readObject();
                 } finally {
                         if (r != null)
                                 try { r.close(); } catch (IOException e) { };
                 }
                 
+                
+                loadCategories(jroot.getJsonArray("categories"));
+                
                 jfeeds = jroot.getJsonObject("feeds");
                 
                 feedList = new ArrayList<Feed>(jfeeds.size());
-                cats = new HashMap<>();
 
                 jfeeds.forEach((k, v)-> {
                         JsonObject jf;
@@ -49,17 +81,15 @@ public class Config {
                         jcategories = jf.getJsonArray("categories");
                         str = jcategories.getString(0);
                         
-                        cat = cats.get(str);
-                        if (cat == null) {
-                                cat = new Category(str);
-                                cats.put(str, cat);
-                        }
+                        cat = getCategory(str);
                         
-                        feedList.add(new Feed(k, cat));                                           
+                        if (cat != null)
+                                feedList.add(new Feed(k, cat));
+                        else
+                                LOG.severe("Missing category: " + str);
                 });
                 
                 feeds = feedList.toArray(new Feed[0]);
-                categories = cats.values().toArray(new Category[0]);
         }
         
         public Feed[] getFeeds() {
@@ -97,7 +127,7 @@ public class Config {
                 return categories[0];
         }
         
-        public static void main(String[] args) {
+        public static void main(String[] args) throws UnsupportedEncodingException {
                 Config cfg;
                 Feed[] feeds;           
                 Category[] cats;