Tuesday 17 July 2018

Which languages do not have both lower and upper-case letters

Java do not provide any APIs, to get the alphabets in all the supported locales. I am going to use icu4j API to get the alphabets information.

                  <!-- https://mvnrepository.com/artifact/com.ibm.icu/icu4j -->
                  <dependency>
                           <groupId>com.ibm.icu</groupId>
                           <artifactId>icu4j</artifactId>
                           <version>62.1</version>
                  </dependency>

Below application writes the languages that do not have lower, upper case to a html file.

AlphabetUtil.java
package com.smaple.app.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import com.ibm.icu.text.UnicodeSet;
import com.ibm.icu.util.LocaleData;
import com.ibm.icu.util.ULocale;

public class AlphabetUtil {

	/**
	 * Return lower case alphabets of given locale
	 * 
	 * @param locale
	 * @return
	 */
	public static String getLowerAlphabets(Locale locale) {
		if (locale == null) {
			throw new IllegalArgumentException("locale coudn't be null");
		}

		ULocale ulocale = ULocale.forLocale(locale);
		UnicodeSet set = LocaleData.getExemplarSet(ulocale, LocaleData.ES_STANDARD);

		Iterator<String> iterator = set.iterator();
		StringBuilder builder = new StringBuilder();
		while (iterator.hasNext()) {
			builder.append(iterator.next());

		}

		return builder.toString();
	}

	/**
	 * Return upper case alphabets of given locale.
	 * 
	 * @param locale
	 * @return
	 */
	public static String getUpperAlphabets(Locale locale) {
		return getLowerAlphabets(locale).toUpperCase(locale);
	}

	public static List<Locale> getLanguagesThatHasLowerAndUpper() {
		Locale[] locales = Locale.getAvailableLocales();
		List<Locale> output = new ArrayList<>();

		for (Locale locale : locales) {
			String lowerCase = getLowerAlphabets(locale);
			String upperCase = getUpperAlphabets(locale);
			if (!lowerCase.equals(upperCase)) {
				output.add(locale);
			}
		}

		return output;
	}

	/**
	 * Write the languages that support both upper and lower case to a html file.
	 * 
	 * @param filePath
	 */
	public static void writeLowerAndUpperCaseSupportedLang(String htmlFilePath) {
		if (htmlFilePath == null || htmlFilePath.isEmpty()) {
			htmlFilePath = "result.html";
		}

		StringBuilder builder = new StringBuilder();

		builder.append("<html>");
		builder.append("<head>\r\n" + "<style>\r\n" + "table, th, td {\r\n" + "    border: 1px solid black;\r\n"
				+ "}\r\n" + "</style>\r\n" + "</head>");
		builder.append("<body><table style=\"width:100%;text-align:left;background-color:gold;\"");

		builder.append("<tr><td>Locale</td>");
		builder.append("<td>Lower Case Alphabets</td>");
		builder.append("<td>Upper Case Alphabets</td></tr>");

		Locale[] locales = Locale.getAvailableLocales();

		for (Locale locale : locales) {
			String lowerCase = getLowerAlphabets(locale);
			String upperCase = getUpperAlphabets(locale);

			if (lowerCase.equals(upperCase)) {
				builder.append("<tr><td>" + locale.toString() + "</td>");
				builder.append("<td>" + lowerCase + "</td>");
				builder.append("<td>" + upperCase + "</td></tr>");
			}

		}

		writeDataToFile(htmlFilePath, builder.toString());
	}

	private static void writeDataToFile(String htmlFilePath, String data) {

		File file = new File(htmlFilePath);
		String encoding = "UTF8";

		try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), encoding)) {
			writer.write(data);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

Test.java
package com.sample.app;

import com.smaple.app.util.AlphabetUtil;

public class Test {

	public static void main(String args[]) {
		AlphabetUtil.writeLowerAndUpperCaseSupportedLang("LowerUpper.html");
	}
}

Run Test.java and open ‘LowerUpper.html’ file, you can see below html page.




You may like

No comments:

Post a Comment