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

public class UBB
{
    static Pattern p1=Pattern.compile("<([^>]*)>", Pattern.DOTALL);

    public static String clearHtmlTag(String s)
    {
        try
        {
            Matcher m = null;
            m = p1.matcher(s);
            while (m.find())
            {
                for (int i = 1; i <= m.groupCount(); i++)
                {
                    //System.out.println("找到 = " + m.group());
                    s = s.replaceAll(m.group(), "");
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return s.trim();
    }

    public static String delHtmlTag(String str)
    {
        int index1 = -1;
        int index2 = -1;
        while (true)
        {
            index1 = str.indexOf("<");
            index2 = str.indexOf(">", index1);
            if (index1 == -1)
            {
                break;
            }
            if (index2 == -1)
            {
                index2 = str.length();
            }
            else
            {
                index2 = index2 + 1;
            }
            str = str.substring(0, index1)
                  + str.substring(index2, str.length());

        }
        str = str.replaceAll("\r", "<br/>");
        str = str.replaceAll("\t", "    ");
        return str;
    }

    /**
     * 转换UBB
     * @param text
     * @return
     */
    public static String UbbDecode(String text)
    {
        text = replace(text, "tr(.*?)", "tr", "<tr>", "</tr>");

        text = replace(text, "td(.*?)", "td", "<td>", "</td>");

        text = replace(text, "table(.*?)", "table", "<table>", "</table>");

        text = replace(text, "align=(.*?)", "align", "<p align=$2>", "</align>");

        text = replace(text, "url=(.*?)", "url", "<a href='$2' target=_blank>",
                       "</a>");

        text = replace(text, "img", "img", "<img border=0 src=", "></img>");

        text = replace(text, "img=(.+?),(.+?)", "img",
                       "<img width=\"$2\" hight=\"$3\" border=0 src=",
                       "></img>");

        text = replace(text, "size=(.+?)", "size", "<font size=$2>", "</font>");

        text = replace(text, "font=(.+?)", "font", "<font face=$2>", "</font>");

        text = replace(text, "color=(.+?)", "color", "<font color=$2>",
                       "</font>");

        text = replace(text, "email=(.+?)", "email", "<a href='mailto:$2'>", "</a>");
       
        text = replace(text, "u", "<u>", "</u>");
        text = replace(text, "i", "<i>", "</i>");
        text = replace(text, "li", "<li>", "</li>");
        text = replace(text, "list=(.*?)", "<ol>", "</ol>");
        text = replace(text, "list(.*?)", "<ul>", "</ul>");
        text = replace(text, "\\*", "<li>", "</li>");
        text = replace(text, "ol", "<ol>", "</ol>");
        text = replace(text, "ul", "<ul>", "</ul>");
        text = replace(text, "b", "<b>", "</b>");
        text = replace(text, "h1", "<h1>", "</h1>");
        text = replace(text, "h2", "<h2>", "</h2>");
        text = replace(text, "h3", "<h3>", "</h3>");
        text = replace(text, "h4", "<h4>", "</h4>");
        text = replace(text, "h5", "<h5>", "</h5>");
        text = replace(text, "h6", "<h6>", "</h6>");

        return text;
    }

    public static String replace(String text, String reg, String replaceStr1,
                                 String replaceStr2)
    {
        return replace(text, reg, reg, replaceStr1, replaceStr2);
    }

    public static String replace(String text, String reg, String regEnd,
                                 String replaceStr1, String replaceStr2)
    {
        Matcher m = null;

        m = Pattern.compile(
                "(\\[" + reg + "\\])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
            .matcher(text);
        text = m.replaceAll(replaceStr1);

        m = Pattern.compile(
                "(\\[/" + regEnd + "\\])",
                Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)
            .matcher(text);
        text = m.replaceAll(replaceStr2);

        return text;
    }

    public static void main(String arg[])
    {
        //String f="   3月5日是中国开国总理周恩来诞辰110周年纪念日。在今天这样一个物质生活更加丰富、精神生活更加开放的社会里,曾被人们所崇敬的'周恩来精神'还在感动着谁?这种精神是否穿越了历史烟云,随着社会的发展更加历久弥新?人们的怀念,是否历经时代的风雨,更臻淳厚? [/b] [b]详细]\r\n   链接 周恩来精神\r\n   作为毛泽东思想的重要组成部分,'周恩来精神'一直受到广大干部群众的称赞和学习。其核心内涵包括共产主义远大理想同脚踏实地的工作作风的结合;对上负责同对下负责的结合;高度的原则性同高度的灵活性的结合。周恩来研究专家指出,'周恩来精神'内涵丰富,还包括以下四点: \r\n   勤勤恳恳,任劳任怨,'鞠躬尽瘁,死而后已'的全心全意为人民服务的精神。\r\n   严于律己,廉洁奉公,无私无我的奉献精神。\r\n   求真务实,脚踏实地,一切从实际出发的精神。\r\n   团结同志,广交朋友,调动各种积极因素一起做好工作的精神。\r\n[table=98%][tr][td][align=center][/align][align=center][size=2]1914年,中学读书时期的周恩来[/align][/td][td][align=center]\r\n1924年,周恩来在黄埔军校[/align][/td][td]221\r\n  1925年,周恩来和邓颖超结婚期间留影\r\n[/td][td]220\r\n1935年,长征到达陕北时的周恩来\r\n[/td][/tr][tr][td]\r\n1945年8月,周恩来到重庆谈判\r\n[/td][td]197\r\n1949年10月1日,周恩来和毛泽东等在天安门城楼上\r\n[/td][td]198\r\n1955年4月,周恩来在万隆会议上[/td][td]\r\n1975年1月,周恩来在第四届全国人大第一次会议上抱病作《政府工作报告》[/td][/tr][/table][/size]\r\n橱柜\r\n";
        //String f=" [table=98%][tr][td=1,1,33%]11[/td][td=1,1,33%]22[/td][td=1,1,34%]33[/td][/tr][tr][td=1,1,33%]aa[/td][td=1,1,33%]bb[/td][td=1,1,34%]cc[/td][/tr][/table]";
        String f =
                "[ul][li]这是新加的空白文章1,可以在UBB可视化编辑器中,添加和修改文章内容。[/li][/ul]";
        System.out.print(UBB.UbbDecode(f));

    }

}