read config from json. Many refactoring to prepare multi language support
[pnews.git] / war / src / main / java / pnews / servlet / Config.java
index 5c0c134..a77eb88 100644 (file)
@@ -16,101 +16,100 @@ import pnews.Category;
 import pnews.Feed;
 
 public class Config {
-        public static Feed[] getFeeds() {
+        private Feed[] feeds;
+        private Category[] categories;
+                
+        public void loadConfig() {
                 Reader r;
-                List<Feed> feeds;
-                JsonObject jfeeds;
+                JsonObject jfeeds, jroot;
+                List<Feed> feedList;
+                Map<String, Category> cats;
                 
                 r = null;
                 try {
                         r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"));
-                        jfeeds = Json.createReader(r).readObject();
+                        jroot = Json.createReader(r).readObject();
                 } finally {
                         if (r != null)
                                 try { r.close(); } catch (IOException e) { };
                 }
                 
-                feeds = new ArrayList<Feed>(jfeeds.size());
+                jfeeds = jroot.getJsonObject("feeds");
+                
+                feedList = new ArrayList<Feed>(jfeeds.size());
+                cats = new HashMap<>();
 
                 jfeeds.forEach((k, v)-> {
                         JsonObject jf;
+                        String str;
+                        Category cat;
                         JsonArray jcategories;
                         
                         jf = (JsonObject)v;
                         jcategories = jf.getJsonArray("categories");
-                        feeds.add(new Feed(k, Category.valueOf(jcategories.getString(0))));
-                });
+                        str = jcategories.getString(0);
+                        
+                        cat = cats.get(str);
+                        if (cat == null) {
+                                cat = new Category(str);
+                                cats.put(str, cat);
+                        }
                         
-                return feeds.toArray(new Feed[] {});
+                        feedList.add(new Feed(k, cat));                                           
+                });
+                
+                feeds = feedList.toArray(new Feed[0]);
+                categories = cats.values().toArray(new Category[0]);
+        }
+        
+        public Feed[] getFeeds() {
+                return feeds;
         }
         
-        public static Map<Category, String[]> getFeedsByCategory() {
-                Map<Category, String[]> result;
+        public Map<Category, List<Feed>> getFeedsByCategory() {
+                Map<Category, List<Feed>> result;
+                Feed[] feeds;
+                List<Feed> catFeeds;
+                Category cat;
                 
                 result = new HashMap<>();
                 
-                result.put(Category.ACTUALITE,
-                           new String[] {
-                                           "http://www.europe1.fr/var/export/rss/europe1/actus.xml",
-                                           "http://www.francetvinfo.fr/titres.rss",
-                                           "http://www.rfi.fr/general/rss",
-                                           "http://www.cnews.fr/rss/une",
-                                           "http://www.ladepeche.fr/rss/a-la-une.rss",
-                                           "https://www.franceinter.fr/rss/a-la-une.xml",
-                                           "https://www.francebleu.fr/rss/a-la-une.xml",
-                                           "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
-                           });
-                
-                result.put(Category.SPORT,
-                                new String[] { "http://www.europe1.fr/var/export/rss/europe1/sport.xml",
-                                               "http://www.sportune.fr/feed",
-                                               "http://www.france24.com/fr/sports/rss" });
-                
-                result.put(Category.FRANCE,
-                                new String[] { "http://www.france24.com/fr/france/rss",
-                                               "http://www.francetvinfo.fr/france.rss",
-                                               "http://www.rfi.fr/france/rss"});
-                
-                result.put(Category.EUROPE,
-                                new String[] { "http://www.france24.com/fr/europe/rss" });
-
-                result.put(Category.MONDE, 
-                           new String[] { "http://www.europe1.fr/var/export/rss/europe1/international.xml",
-                                          "http://www.france24.com/fr/actualites/rss" });                                           
-
-                
-                result.put(Category.ECONOMIE,
-                                new String[] { "http://www.france24.com/fr/economie/rss",
-                                               "http://www.europe1.fr/var/export/rss/europe1/economie.xml",
-                                               "http://www.rfi.fr/economie/rss" });
-                
-                result.put(Category.ESSONNE,
-                                new String[] { "http://www.tourisme-essonne.com/rss/actus/",
-                                               "http://www.ville-palaiseau.fr/rss/actualites.htm" });
-                
-                result.put(Category.PEOPLE,
-                                new String[] { "http://www.premiere.fr/rss/actu-live",
-                                               "http://www.purepeople.com/rss/news_t0.xml"                                               
-                });
+                feeds = getFeeds();
+                for (Feed f: feeds) {
+                        cat = f.getCategory();
                 
-                result.put(Category.TECHNOLOGIE,
-                                new String[] { "http://www.generation-nt.com/export/rss.xml",
-                                               "http://www.europe1.fr/var/export/rss/europe1/sciences.xml",
-                                               "http://feeds.feedburner.com/lesnumeriques/news",
-                                               "http://www.zdnet.fr/feeds/rss/actualites/",
-                                               "http://www.frandroid.com/feed",
-                                               "http://www.silicon.fr/feed",
-                                               "http://www.fredzone.org/feed",
-                                               "http://www.futura-sciences.com/rss/actualites.xml",
-                                               "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all"});
+                        catFeeds = result.get(cat);
+                        if (catFeeds == null) {
+                                catFeeds = new ArrayList<Feed>();
+                                result.put(cat, catFeeds);
+                        }
+                        catFeeds.add(f);
+                }
                 
                 return result;
         }
 
+        public Category[] getCategories() {
+                return categories;
+        }
+        
+        public Category getDefaultCategory() {
+                return categories[0];
+        }
+        
         public static void main(String[] args) {
-                Feed[] feeds;
+                Config cfg;
+                Feed[] feeds;           
+                Category[] cats;
+
+                cfg = new Config();
+                cfg.loadConfig();
                 
-                feeds = getFeeds();
+                cats = cfg.getCategories();
+                for (Category cat: cats)
+                        System.out.println(cat);
+                
+                feeds = cfg.getFeeds();
                 
                 System.out.println("Number of feeds: " + feeds.length);
                 for (Feed f: feeds)