cn.cuizuoli.gotour.utils.HtmlHelper.java Source code

Java tutorial

Introduction

Here is the source code for cn.cuizuoli.gotour.utils.HtmlHelper.java

Source

/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.cuizuoli.gotour.utils;

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

import org.apache.commons.lang.StringUtils;

/**
 * HtmlHelper
 * @author cuizuoli
 */
public class HtmlHelper {

    private final static Pattern TITLE_PATTERN = Pattern.compile("^\\s*(\\S+)\\s*$");
    private final static Pattern TABLE_PATTERN = Pattern.compile("^\\s*(\\S+)\\s+(\\S+)\\s*$");
    private final static Pattern LIST_PATTERN = Pattern.compile("^(\\d+?)\\s*(\\S+)\\s*$");

    /**
     * toTable
     * @param text
     * @return
     */
    public static String toTable(String text) {
        String[] lines = StringUtils.split(text, "\r\n");
        StringBuffer tableBuffer = new StringBuffer();
        for (String line : lines) {
            Matcher tileMatcher = TITLE_PATTERN.matcher(line);
            if (tileMatcher.matches()) {
                String h4 = new StringBuffer().append("<h4>").append(tileMatcher.group(1)).append("</h4>")
                        .append("\n").append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">")
                        .append("\n").toString();
                tableBuffer.append(h4);
            }
            Matcher tableMatcher = TABLE_PATTERN.matcher(line);
            if (tableMatcher.matches()) {
                String tr = new StringBuffer().append("<tr>").append("\n").append("<th>")
                        .append(tableMatcher.group(1)).append("</th>").append("\n").append("<td>")
                        .append(tableMatcher.group(2)).append("</td>").append("\n").append("</tr>").append("\n")
                        .toString();
                tableBuffer.append(tr);
            }
        }
        tableBuffer.append("</table>\n");
        String table = tableBuffer.toString();
        table = StringUtils.replace(table, "<h4>", "</table>\n<h4>");
        table = StringUtils.replaceOnce(table, "</table>\n<h4>", "<h4>");
        if (!StringUtils.contains(table, "<table")) {
            table = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n" + table;
        }
        return table;
    }

    /**
     * toList
     * @param text
     * @return
     */
    public static String toList(String text) {
        String[] lines = StringUtils.split(text, "\r\n");
        StringBuffer listBuffer = new StringBuffer();
        listBuffer.append("<ul>").append("\n");
        for (String line : lines) {
            Matcher listMatcher = LIST_PATTERN.matcher(line);
            if (listMatcher.matches()) {
                String li = listMatcher.group(1) + listMatcher.group(2);
                if (StringUtils.contains(li, "")) {
                    li = "<strong>" + StringUtils.replace(li, "", "</strong>");
                }
                listBuffer.append("<li>").append(li).append("</li>").append("\n");
            }
        }
        listBuffer.append("</ul>").append("\n");
        return listBuffer.toString();
    }

}