Thursday, January 26, 2012

Unicode character APIs in Java

package com.struts1.tutorials.utils;

import java.util.ArrayList;
import java.util.List;

public class UnicodeCheck {
   
    public static int countUnicodeCharacters(char[] charArray) {
        if(charArray == null || charArray.length == 0)
            return 0;
        int numUnicodeChars = 0;
        for (int i = 0; i < charArray.length; i++) {
            if (Character.isLetter(charArray[i]) && !((charArray[i] >= 'a') && (charArray[i] <= 'z'))
                    && !((charArray[i] >= 'A') && (charArray[i] <= 'Z'))) {
                numUnicodeChars++;
                System.out.println("Unicode character = " + charArray[i]);
            }           
        }
        return numUnicodeChars;
    }
   

    // This does not work.
    public static int countUnicodeCharacters2(char[] charArray){
        if(charArray == null || charArray.length == 0)
            return 0;
        int numUnicodeChars = 0;
       
        System.out.println("********* Start Testing isLetter() API ************");
        for (int i = 0; i < charArray.length; i++) {
            if (Character.isLetter(charArray[i]) && !Character.isLowerCase(charArray[i]) && !Character.isUpperCase(charArray[i])) {
                numUnicodeChars++;
                System.out.println("isLetter() API : Character is unicode = " + charArray[i]);
                System.out.println("Character = " + charArray[i] +
                        " :: int value = " + i +
                        " :: getType = " + Character.getType(charArray[i]));
            }
        }
        System.out.println("********* End Testing isLetter() API ************");
        System.out.println("isLetter() API: Count of unicode characters = " + numUnicodeChars);
        return numUnicodeChars;
       
    }   
   
    public static void testcase1(char[] chArr) {
        System.out.println("Begin Testcase1()");
        for (char ch : chArr ) {
            int i = ch;
            System.out.println("Character = " + ch +
                                " :: int value = " + i +
                                " :: getType = " + Character.getType(ch));
            Character.UnicodeBlock block = Character.UnicodeBlock.of(ch);
            System.out.println("Unicode block = " + block);
            System.out.println("isUnicodeIdentifierPart = " + Character.isUnicodeIdentifierPart(ch));
            System.out.println("isUnicodeIdentifierStart = " + Character.isUnicodeIdentifierStart(ch));
        }
    }
   
    public static void main(String[] args) {
       
        List pwdList = new ArrayList();       
        pwdList.add("welcome1");
        pwdList.add("Enañol");
        pwdList.add("wel\u00f1");       
        pwdList.add("bargain12345");
        pwdList.add("....12345");
        pwdList.add("change1234");
        pwdList.add("cat12345");
               
        for (String pwd : pwdList) {
            char[] pwdChars = pwd.toCharArray();
            int count = countUnicodeCharacters(pwdChars);           
            System.out.println("Input password = " + pwd + " :: unicode chars count = " + count);
        }
    }
}



Output
=====
Input password = welcome1 :: unicode chars count = 0
Unicode character = ñ
Input password = Enañol :: unicode chars count = 1
Unicode character = ñ
Input password = welñ :: unicode chars count = 1
Input password = bargain12345 :: unicode chars count = 0
Input password = ....12345 :: unicode chars count = 0
Input password = change1234 :: unicode chars count = 0
Input password = cat12345 :: unicode chars count = 0