Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.text.TextUtils;

public class Main {
    /**
     * Regular expression pattern to match RFC 1738 URLs
     * List accurate as of 2013/12/18.  List taken from:
     * http://data.iana.org/TLD/tlds-alpha-by-domain.txt
     * This pattern is auto-generated by //device/tools/make-iana-tld-pattern.py
     */
    public static final Pattern WEB_URL_PATTERN = Pattern
            .compile("((?:(http|https|Http|Https):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
                    + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
                    + "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
                    + "((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}\\.)+" // named host
                    + "(?:" // plus top level domain
                    + "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])" + "|(?:biz|b[abdefghijmnorstvwyz])"
                    + "|(?:cat|com|coop|c[acdfghiklmnorsuvxyz])" + "|d[dejkmoz]" + "|(?:edu|e[ceghrstu])"
                    + "|f[ijkmor]" + "|(?:gov|g[abdefghilmnpqrstuwy])" + "|h[kmnrtu]"
                    + "|(?:info|int|i[delmnoqrst])" + "|(?:jobs|j[emop])" + "|k[eghimnprwyz]" + "|l[abcikrstuvy]"
                    + "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])" + "|(?:name|net|n[acefgilopruz])"
                    + "|(?:org|om)" + "|(?:post|pro|p[aefghklmnrstwy])" + "|qa" + "|r[eosuw]"
                    + "|s[abcdeghijklmnorstuvxyz]" + "|(?:tel|travel|t[cdfghjklmnoprtvwz])" + "|u[agkmsyz]"
                    + "|v[aceginu]" + "|w[fs]" + "|xxx" + "|y[etu]" + "|z[amw]))" + "|(?:(?:25[0-5]|2[0-4]" // or ip address
                    + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
                    + "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
                    + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}" + "|[1-9][0-9]|[0-9])))"
                    + "(?:\\:\\d{1,5})?)" // plus option port number
                    + "(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params
                    + "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?" + "(?:\\b|$)");
    private static final Pattern COLON_REGEX = Pattern.compile(":[a-z0-9+_-]{1,31}:");
    private static final Map<String, String> CHEAT_SHEET_TO_UNICODE = new HashMap<String, String>();

    public static String replaceCheatSheetEmojis(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }

        Matcher matcher = COLON_REGEX.matcher(s);
        List<int[]> linkRanges = getLinksRanges(s);
        List<int[]> potentialMatches = new ArrayList<int[]>();

        OUTER: while (matcher.find()) {

            int start = matcher.start();
            int end = matcher.end();
            for (int[] range : linkRanges) {
                if (start <= range[1] && range[0] <= end) {
                    continue OUTER;
                }
            }
            potentialMatches.add(0, new int[] { start, end });
        }
        for (int[] potentialMatch : potentialMatches) {
            String toReplace = s.substring(potentialMatch[0], potentialMatch[1]);
            String replacement = CHEAT_SHEET_TO_UNICODE.get(toReplace);

            if (!TextUtils.isEmpty(replacement)) {
                String newString = s.substring(0, potentialMatch[0]) + replacement;
                if (potentialMatch[1] <= s.length()) {
                    newString += s.substring(potentialMatch[1]);
                }
                s = newString;
            }
        }

        return s;
    }

    public static List<int[]> getLinksRanges(String s) {
        List<int[]> ranges = new ArrayList<int[]>();
        Matcher matcher = WEB_URL_PATTERN.matcher(s);

        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ranges.add(new int[] { start, end });
        }
        return ranges;
    }
}