org.shaf.server.util.HtmlDecorator.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.server.util.HtmlDecorator.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * 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 org.shaf.server.util;

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

import org.shaf.core.util.Log;

import com.google.common.base.Strings;
import com.google.common.html.HtmlEscapers;

/**
 * Transforms the subset of object into the HTML representation.
 * 
 * @author Mykola Galushka
 */
public class HtmlDecorator {

    /**
     * Defines a logger.
     */
    private static final Log LOG = Log.forClass(HtmlDecorator.class);

    /**
     * Colorizes a text fragment to the specified color.
     * 
     * @param color
     *            the color to use.
     * @param text
     *            the text fragment to colorizes.
     * @return the HTML representation of text.
     */
    private static final String colorize(final String color, final String text) {
        StringBuilder html = new StringBuilder();

        html.append("<span style=\"color: " + color + ";\">");
        html.append(text);
        html.append("</span>");

        return html.toString();
    }

    /**
     * Returns the groups formed as a result of matching by using the regular
     * expression on the specified text.
     * 
     * @param regexp
     * @param text
     * @return
     */
    private static final String[] getGroups(final String regexp, final String text) {
        Matcher m = Pattern.compile(regexp).matcher(text);

        if (m.find()) {
            String[] groups = new String[m.groupCount()];

            for (int i = 0; i < m.groupCount(); i++) {
                groups[i] = m.group(i + 1);
            }
            return groups;
        } else {
            return new String[0];
        }
    }

    /**
     * Transforms the exceptions into HTML representation.
     * 
     * @param source
     *            the exception to decorate
     * @return the HTML representation of the exception.
     */
    public static final String exception2html(final String source) {
        String colorKeyword = "#000000";
        String colorMessage = "#FF0000";
        String colorClassInt = "#800000";
        String colorClassExt = "#808080";
        String colorMethod = "#000000";
        String colorSource = "#000099";
        String colorLineNumber = "#0000FF";

        StringBuilder html = new StringBuilder();

        for (String line : source.split("\n")) {
            try {
                if (!line.trim().isEmpty()) {
                    if (line.trim().startsWith("...")) {
                        html.append(colorize(colorKeyword, line));
                    } else if (line.trim().startsWith("Caused by:")) {
                        String[] groups = getGroups("Caused\\sby\\:\\s([\\w\\.\\$]+)\\:?(.*)?", line);
                        html.append(colorize(colorKeyword, "Caused by:&nbsp"));
                        html.append("<b>");
                        if (groups[0].startsWith("org.shaf")) {
                            html.append(colorize(colorClassInt, groups[0]));
                        } else {
                            html.append(colorize(colorClassExt, groups[0]));
                        }
                        if (groups.length > 1) {
                            html.append(colorize(colorMessage, ":&nbsp;"));
                            html.append(colorize(colorMessage, groups[1]));
                        }
                        html.append("</b>");
                    } else if (line.trim().startsWith("at ")) {
                        if (line.trim().contains("Native Method")) {
                            String[] groups = getGroups("\\s+at\\s([\\w\\.\\$]+)\\.([\\w]+)\\(Native\\sMethod\\)",
                                    line);

                            html.append(colorize(colorKeyword, Strings.repeat("&nbsp", 5) + "at&nbsp"));
                            html.append(colorize(colorClassExt, groups[0]));
                            html.append(colorize(colorKeyword, "."));
                            html.append(colorize(colorMethod, groups[1]));
                            html.append(colorize(colorKeyword, "("));
                            html.append(colorize(colorSource, "Native Method"));
                            html.append(colorize(colorKeyword, ")"));
                        } else {
                            String[] groups = getGroups(
                                    "\\s+at\\s([\\w\\.\\$]+)\\.([\\w]+)\\((.*\\.java)\\:(\\d+)\\)", line);
                            html.append(colorize(colorKeyword, Strings.repeat("&nbsp", 5) + "at&nbsp"));

                            if (groups[0].startsWith("org.shaf")) {
                                html.append(colorize(colorClassInt, (groups[0])));
                            } else {
                                html.append(colorize(colorClassExt, (groups[0])));
                            }
                            html.append(colorize(colorKeyword, "."));
                            html.append(colorize(colorMethod, groups[1]));
                            html.append(colorize(colorKeyword, "("));
                            html.append(colorize(colorSource, groups[2]));
                            html.append(colorize(colorKeyword, ":"));
                            html.append(colorize(colorLineNumber, groups[3]));
                            html.append(colorize(colorKeyword, ")"));

                        }
                    } else {
                        html.append("<b>");
                        String[] groups = getGroups("([\\w\\.\\$]+)(\\:.*)?", line);
                        if (groups[0].startsWith("org.shaf")) {
                            html.append(colorize(colorClassInt, groups[0]));
                        } else {
                            html.append(colorize(colorClassExt, groups[0]));
                        }
                        if (groups.length > 1) {
                            html.append(colorize(colorMessage, groups[1]));
                        }
                        html.append("</b>");
                    }
                }
            } catch (Exception exc) {
                LOG.error("Failed to decorate a part of exception trace: \"" + line + "\".", exc);

                html.append(line);
            }
            html.append("<br/>");
        }
        return html.toString();
    }

    /**
     * Transforms the plain text into HTML representation.
     * 
     * @param source
     *            the plain text to decorate.
     * @return the HTML representation of the plain text.
     */
    public static final String text2html(final String source) {
        StringBuilder html = new StringBuilder();

        String text = HtmlEscapers.htmlEscaper().escape(source);
        for (String line : text.split("\n")) {
            html.append(line.replace(" ", "&nbsp;"));
            html.append("<br/>");
        }
        return html.toString();
    }
}