Escaping and canceling the escape of html special characters in java

Keywords: Database Java Attribute Spring

1, Requirement description

1. The data from the front end is stored in the database, which needs to be escaped after the background

When the user fills in the data, sometimes some html special characters (such as trademark symbol) will be passed in. At this time, we need to escape the special characters from the front end, and then store them in the database. Otherwise, the special characters in the database will become question marks, which cannot be displayed on the page normally

2. Find out the data from the database, transfer it to the front page for display, and cancel the escape processing after the background

2, Code implementation

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.HtmlUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by shucheng on 2018-07-16 Forenoon 0:11.
 * Html Tool class
 **/
public class HtmlUtil {

    /**
     * De escapes all string properties of objects contained in the list
     * @param list
     * @param <T>
     * @return
     */
    public static <T> List<T> listHtmlUnEscape(List<T> list) {
        return listHtmlTextHandle(list, 0);
    }

    /**
     * Add escape to all string properties of objects contained in the list
     * @param list
     * @param <T>
     * @return
     */
    public static <T> List<T> listHtmlEscape(List<T> list) {
        return listHtmlTextHandle(list, 1);
    }

    /**
     * De escapes all string properties in the object
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T objectHtmlUnEscape(T t) {
        return objectHtmlTextHandle(t, 0);
    }

    /**
     * Add escape to all string properties in the object
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T objectHtmlEscape(T t) {
        return objectHtmlTextHandle(t, 1);
    }

    /**
     * list Batch html escape of object attribute values in
     * @param list list to be escaped
     * @param option 0 Cancel escape 1 escape
     * @param <T>
     * @return
     */
    public static <T> List<T> listHtmlTextHandle(List<T> list, int option) {
        for (int i = 0; i < list.size(); i++) {
            T t = list.get(i);
            list.remove(i); // Remove the original object first
            t = 0 == option ? objectHtmlUnEscape(t) : objectHtmlEscape(t);
            list.add(i, t); // Then add the object after de escaping to the corresponding position of list
        }
        return list;
    }

    /**
     * Batch html escape of object attribute value
     * @param t Objects that need to be escaped
     * @param option 0 Cancel escape 1 escape
     * @param <T>
     * @return
     */
    public static <T> T objectHtmlTextHandle(T t, int option) {
        Class clazz = t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            Class type = field.getType();
            if (type.equals(String.class)) {
                field.setAccessible(true);
                try {
                    String filedValue = (String) field.get(t);
                    // Cancel escape (0), escape (1)
                    filedValue = 0 == option ? htmlUnescape(filedValue) : htmlEscape(filedValue);
                    field.set(t, filedValue);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return t;
    }

    /**
     * Spring HtmlUtils for escape
     */
    public static String htmlEscape(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        } else {
            return HtmlUtils.htmlEscape(str);
        }
    }

    /**
     * Spring HTML utils for restore
     */
    public static String htmlUnescape(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        } else {
            return HtmlUtils.htmlUnescape(str);
        }
    }
}

 

Posted by cueball2000uk on Thu, 13 Feb 2020 14:01:19 -0800