Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.net.MailTo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private final static Pattern patternTagTitle = Pattern.compile("(?ms)(.*?)(<.*?>)");
    private final static Pattern patternEmail = Pattern.compile("\\w+(\\.\\w+)*@(\\w)+((\\.\\w{2,3})+)");

    private static String renderEmail(String bodyHtml) {
        StringBuffer bodyStringBuffer = new StringBuffer();

        Matcher matcherEmailContent = patternTagTitle.matcher(bodyHtml);
        while (matcherEmailContent.find()) {

            String processContent = matcherEmailContent.group(1);
            String htmlContent = matcherEmailContent.group(2);

            if (htmlContent.equalsIgnoreCase("</script>") || htmlContent.equalsIgnoreCase("</a>")) {
                matcherEmailContent.appendReplacement(bodyStringBuffer, processContent + htmlContent);
            } else {
                String emailContentHasProcessed = makeEmailHerf(processContent);
                matcherEmailContent.appendReplacement(bodyStringBuffer, emailContentHasProcessed + htmlContent);
            }

        }
        matcherEmailContent.appendTail(bodyStringBuffer);
        return bodyStringBuffer.toString();
    }

    private static String makeEmailHerf(String content) {
        if (content.trim().length() == 0) {
            return content;
        }
        StringBuffer emailStringBuffer = new StringBuffer();

        Matcher matcherEmail = patternEmail.matcher(content);
        while (matcherEmail.find()) {

            String email = matcherEmail.group();
            //            System.out.println("email:" + email);
            String emailToHref = "<a href=\"" + MailTo.MAILTO_SCHEME + email + "\">" + email + "</a>";
            matcherEmail.appendReplacement(emailStringBuffer, emailToHref);

        }
        matcherEmail.appendTail(emailStringBuffer);
        return emailStringBuffer.toString();
    }
}