Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.HashMap;

import android.text.Html;

import android.text.SpannableString;

public class Main {
    private static HashMap<String, String[]> auths = new HashMap<String, String[]>();

    public static SpannableString linkifyGid(String gid) {
        if (gid == null) {
            return null;
        }
        SpannableString ret = new SpannableString(gid);
        String name = gid.substring(0, gid.lastIndexOf("@"));
        String authority = gid.substring(gid.lastIndexOf("@") + 1);
        String mainAuth = authority.contains("/") ? authority.substring(0, authority.indexOf("/")) : authority;

        if (auths.containsKey(mainAuth)) {

            String link;
            try {
                if (auths.get(mainAuth).length == 1) {
                    link = auths.get(mainAuth)[0];
                } else if (auths.get(mainAuth).length == 2) {
                    link = auths.get(mainAuth)[0] + URLEncoder.encode(name, "UTF-8") + auths.get(mainAuth)[1];
                } else {
                    return ret;
                }
            } catch (UnsupportedEncodingException e) {
                return ret;
            }

            ret = new SpannableString(Html.fromHtml("<a href=\"" + link + "\">" + gid + "</a>"));

        } else if (mainAuth.matches(".*\\..*")) { // if it contains a dot

            // we try a link to the supposed webpage of this auth
            ret = new SpannableString(Html.fromHtml("<a href=\"http://" + mainAuth + "\">" + gid + "</a>"));

        }

        return ret;
    }
}