All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.tool.flags.Flags Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package com.clickntap.tool.flags;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONObject;

import com.clickntap.utils.AsciiUtils;
import com.clickntap.utils.ConstUtils;
import com.clickntap.utils.IOUtils;

public class Flags {

	private JSONObject countries;
	private JSONObject languages;

	public Flags() throws Exception {
		{
			InputStream in = new URL("http://clickntap.s3.amazonaws.com/ui/flags/iso-3166-1.json").openStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			IOUtils.copy(in, out);
			out.close();
			in.close();
			countries = new JSONObject(out.toString(ConstUtils.UTF_8)).getJSONObject("Results");
		}
		{
			InputStream in = new URL("http://clickntap.s3.amazonaws.com/ui/flags/iso-639-1.json").openStream();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			IOUtils.copy(in, out);
			out.close();
			in.close();
			languages = new JSONObject(out.toString(ConstUtils.UTF_8));
		}
	}

	public JSONObject getCountries() {
		return countries;
	}

	public JSONObject getLanguages() {
		return languages;
	}

	public List getCodes() {
		List codes = new ArrayList();
		String[] names = JSONObject.getNames(countries);
		for (int i = 0; i < names.length; i++) {
			codes.add(names[i]);
		}
		Collections.sort(codes);
		return codes;
	}

	public List getItems() {
		List items = new ArrayList();
		for (String code : getCodes()) {
			JSONObject item = countries.getJSONObject(code);
			try {
				Integer.parseInt(AsciiUtils.phonize(item.getString("TelPref")));
				items.add(item);
			} catch (Exception e) {
			}
		}
		return items;
	}

	public void setCountries(JSONObject data) {
		this.countries = data;
	}

	public static void main(String args[]) throws Exception {
		int n = 1;
		for (JSONObject item : new Flags().getItems()) {
			System.out.println(n + ") " + item.getJSONObject("Names").get("it") + " (+" + item.get("TelPref") + ") " + item.getJSONObject("CountryCodes").get("iso2"));
			n++;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy