All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.liyifeng.html.HtmlUtil Maven / Gradle / Ivy

The newest version!
package org.liyifeng.html;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyifeng
 * 2016年7月1日 上午10:23:43
 */
public class HtmlUtil {

    private static  Table collectionToTable(Table table, Collection objectList) {

        Tr tr;
        Tr headTr = new Tr();
        List fields = null;
        // 用于排除没有值的字段
        HashSet hasValueFieldSet = getFieldsHasValue(objectList);

        for (Object obj : objectList) {
            tr = new Tr();
            // 设置表头
            if (fields == null) {
                fields = getAllFields(obj, hasValueFieldSet);
                if (obj instanceof ITableSort) {
                    //字段排序
                    fields = sort((ITableSort) obj, fields);
                }
            }

            for (Field field : fields) {
                TableTag tableTag = field.getAnnotation(TableTag.class);
                if (tableTag != null) {
                    if (headTr != null) {
                        Td td = new Td(tableTag.value());
                        if (tableTag.width() != -1) {
                            td.width(tableTag.width());
                        }
                        headTr.add(td);
                    }
                    try {
                        field.setAccessible(true);
                        tr.add(new Td(field.get(obj)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            if (headTr != null) {
                table.head(headTr);
                headTr = null;
            }
            table.tr(tr);
        }
        return table;
    }

    /**
     *
     * @param obj
     * @param fields
     * @return
     */
    private static List sort(ITableSort obj, List fields) {
        // 对字段进行排序
        Map map = fields.stream().collect(Collectors.toMap(Field::getName, Field -> Field));
        List sortFieldList = obj.fieldSortList();
        List result = new ArrayList<>();
        if (sortFieldList != null && sortFieldList.size() > 0) {
            for (String fieldName : sortFieldList) {
                if(map.containsKey(fieldName)){
                    result.add(map.get(fieldName));
                    map.remove(fieldName);
                }
            }
        }

        result.addAll(map.values());
        return result;
    }

    /**
     *
     * @param objectList
     * @param 
     * @return
     */
    private static  HashSet getFieldsHasValue(Collection objectList) {
        HashSet newSet = new HashSet<>();
        if (objectList != null && objectList.size() > 0) {
            for (E e : objectList) {
                List fields = getAllFields(e, null);
                for (Field field : fields) {
                    field.setAccessible(true);
                    try {
                        Object val = field.get(e);
                        if (val != null) {
                            newSet.add(field.getName());
                        }
                    } catch (IllegalAccessException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }

        return newSet;
    }

    /**
     * 获取class的字段,包括父类的属性
     *
     * @param object
     * @param expectedFieldSet 期望的字段集合,null:返回所有
     * @return
     */
    private static List getAllFields(Object object, Set expectedFieldSet) {
        Class clazz = object.getClass();
        List fieldList = new ArrayList<>();
        List resultList = new ArrayList<>();
        while (clazz != null) {
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        if (expectedFieldSet != null) {
            fieldList.forEach(f -> {
                if (expectedFieldSet.contains(f.getName())) {
                    resultList.add(f);
                }
            });
        } else {
            resultList.addAll(fieldList);
        }

        return resultList;
    }

    /**
     * 集合转换成默认css样式的table对象
* @param objectList 数据集合 * @param 集合中对象的字段必须有{@link TableTag} 自定义注解,用于定义表头,不带TableTag注解的字段不会出现在输出表格中 * @return table */ public static Table toDefaultTable(Collection objectList) { return collectionToTable(Table.table(), objectList); } /** * 集合转换成无css样式的table对象 * @param objectList 数据集合 * @param 集合中对象的字段必须有{@link TableTag} 自定义注解,用于定义表头,不带TableTag注解的字段不会出现在输出表格中 * @return 由集合中对象组装成的table标签对象 */ public static Table toTable(Collection objectList) { return collectionToTable(new Table(), objectList); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy