Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    private static HashMap<String, Integer> htmlEntities = new HashMap<String, Integer>();

    public static String html2Text(String html) {
        StringBuffer filterText = new StringBuffer();

        Pattern p = Pattern.compile("<([^>]*)>");
        Matcher m = p.matcher(html);
        while (m.find()) {
            m.appendReplacement(filterText, "");
        }
        m.appendTail(filterText);

        return unescapeHTML(filterText.toString());
    }

    public static final String unescapeHTML(String source) {
        int i, j;

        boolean continueLoop;
        int skip = 0;
        do {
            continueLoop = false;
            i = source.indexOf("&", skip);
            if (i > -1) {
                j = source.indexOf(";", i);
                if (j > i) {
                    String entityToLookFor = source.substring(i + 1, j);
                    //Logger.e("key=" + entityToLookFor);
                    Integer value = htmlEntities.get(entityToLookFor);
                    if (value != null && value > 0 && value < 65535) {
                        source = source.substring(0, i) + ((char) ((int) value)) + source.substring(j + 1);
                        continueLoop = true;
                    } else {
                        skip = i + 1;
                        continueLoop = true;
                    }
                }
            }
        } while (continueLoop);
        return source;
    }
}