Java tutorial
/* Copyright (c) 2015 ifly6 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.git.ifly6.javatelegram.util; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import org.apache.commons.lang3.StringUtils; import com.git.ifly6.nsapi.NSConnection; import com.git.ifly6.nsapi.NSRegion; import com.git.ifly6.nsapi.NSWorld; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; /** The class <code>JTelegramFetcher</code> is a collection of methods of use when querying data from the NationStates * API for the easy location of recipients through an implementable tag system. It uses lazy loading so just enough * information is queried. * * <p> * <code>JTelegramFetcher</code> allows the fetching of <code>String[]</code>s full of the list of World Assembly * delegates, World Assembly members, new players, and all of the inhabitants of a region. * </p> * * <p> * Note that all of these functions require file-system and Internet access to download and parse the files provided by * the NationStates API. For this, it will create a working directory called <code>JavaTelegram_working_directory</code> * . * </p> */ public class JInfoFetcher { private static JInfoFetcher singleton; private Map<String, Boolean> fetchedRegion = new HashMap<String, Boolean>(); private Map<String, List<String>> regionList = new HashMap<>(); private List<String> delegates; private List<String> waMembers; private JInfoFetcher() { } public static JInfoFetcher getInstance() { if (singleton == null) { singleton = new JInfoFetcher(); } return singleton; } /** Queries the NationStates API for a listing of all World Assembly delegates. * * @return <code>String[]</code> with the recipients inside * @throws IOException in case there is a problem with connecting to the NS API */ public List<String> getDelegates() throws JTelegramException { // If necessary, fetch the Delegates list. if (delegates == null) { try { List<String> delegates = new ArrayList<String>(0); URL website = new URL("http://www.nationstates.net/cgi-bin/api.cgi?wa=1&q=delegates"); String xml_raw = ""; // Read the list of Delegates from the file we just got. @SuppressWarnings("resource") Scanner xml_scan = new Scanner(website.openConnection().getInputStream()).useDelimiter("\\A"); xml_raw = xml_scan.next(); xml_scan.close(); // Remove the XML tags from that list of Delegates. xml_raw = xml_raw.replaceFirst("<WA council=\"1\"><DELEGATES>", ""); xml_raw = xml_raw.replaceFirst("</DELEGATES></WA>", ""); // Split that list into a String[] and remove all new lines. for (String each : xml_raw.split(",")) { if (!StringUtils.isEmpty(each)) { delegates.add(each); } } this.delegates = delegates; } catch (IOException e) { throw new JTelegramException("Failed to get list of delegates."); } } // Return the new or saved Delegates list. return delegates; } /** Queries the NationStates API for a listing of 50 new nations. * * @return <code>String[]</code> with the recipients inside * @throws IOException in case the NationStates API is unreachable for some reason */ public List<String> getNew() throws JTelegramException { try { NSConnection connection = new NSConnection("http://www.nationstates.net/cgi-bin/api.cgi?q=newnations"); String response = connection.connect().getResponse(); XML xml = new XMLDocument(response); String newNations = xml.xpath("/WORLD/NEWNATIONS/text()").get(0); return new ArrayList<String>(Arrays.asList(newNations.split(","))); } catch (IOException e) { throw new JTelegramException("Failed to get new nations.", e); } } /** Queries the NationStates API for a listing of all the members of a region. * * @return <code>String[]</code> with the recipients inside * @throws IOException in case the NationStates API is unreachable for some reason */ public List<String> getRegion(String region) throws JTelegramException { try { NSRegion nsRegion = new NSRegion(region); nsRegion.populateData(); if (fetchedRegion.get(region) == null) { List<String> regionMembers = nsRegion.getRegionMembers(); fetchedRegion.put(region, true); regionList.put(region, regionMembers); } return regionList.get(region); } catch (IOException e) { e.printStackTrace(); throw new JTelegramException("Failed to fetch region members for " + region); } } /** Queries the NationStates API for a listing of every single World Assembly member. * * @return <code>List<String></code> with the recipients inside * @throws IOException in case the NationStates API is unreachable for some reason */ public List<String> getWAMembers() throws JTelegramException { try { // If necessary, fetch the WA Members list. if (waMembers == null) { waMembers = NSWorld.getWAMembers(); } // Return the WA Members list. return waMembers; } catch (IOException e) { throw new JTelegramException(); } } }