Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    public final static String ProtocolKey_ACTION_DIAL = "tel:";
    private final static Pattern patternTagTitle = Pattern.compile("(?ms)(.*?)(<.*?>)");
    private final static Pattern patternTelNo = Pattern
            .compile("((\\d{8,14})|(\\d{3,4}-)?\\d{7,8})|(13[0-9]{9})|(\\d{2,4}-\\d{2,4}-\\d{2,4})");

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

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

            String processContent = matcherTelContent.group(1);
            String htmlContent = matcherTelContent.group(2);
            if (htmlContent.equalsIgnoreCase("</script>") || htmlContent.equalsIgnoreCase("</a>")) {
                matcherTelContent.appendReplacement(bodyStringBuffer, processContent + htmlContent);
            } else {
                String telContentHasProcessed = makeTelNoHerf(processContent);
                matcherTelContent.appendReplacement(bodyStringBuffer, telContentHasProcessed + htmlContent);
            }
        }
        matcherTelContent.appendTail(bodyStringBuffer);
        return bodyStringBuffer.toString();
    }

    private static String makeTelNoHerf(String telContent) {
        if (telContent.trim().length() == 0) {
            return telContent;
        }
        StringBuffer telNoStringBuffer = new StringBuffer();

        Matcher matcherTelNo = patternTelNo.matcher(telContent);
        while (matcherTelNo.find()) {

            String telNo = matcherTelNo.group();
            String telNoToHerf = "<a href=\"" + ProtocolKey_ACTION_DIAL + telNo + "\">" + telNo + "</a>";
            matcherTelNo.appendReplacement(telNoStringBuffer, telNoToHerf);

        }
        matcherTelNo.appendTail(telNoStringBuffer);
        return telNoStringBuffer.toString();
    }
}