shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.walker.openocr.table;
 
import com.walker.infrastructure.utils.StringUtils;
import com.walker.openocr.Constants;
import com.walker.openocr.MultipleLine;
import com.walker.openocr.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.List;
import java.util.Map;
 
public abstract class AbstractTableConfigLoader implements TableConfigLoader{
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    private static final String NAME_TABLE_TITLE = "table_title";
    private static final String NAME_TABLE_TYPE_KEYS = "table_type_keys";
    private static final String NAME_TABLE_TYPE_KEYS_NOT = "table_type_keys_not";
    private static final String NAME_TABLE_SETTING = "table_setting";
    private static final String NAME_REMOVE_COLUMNS = "remove_columns";
 
    private static final String KEY_CELL_TOLERANCE = "cell_tolerance";
    private static final String KEY_TITLE_TOLERANCE = "title_tolerance";
    private static final String KEY_MULTI_LINE_TOLERANCE = "multi_line_tolerance";
    private static final String KEY_CELL_NAME = "name";
    private static final String KEY_MULTI_LINE = "multi_line";
    private static final String KEY_NONE_CESS = "none_cell";
    private static final String KEY_END_FLAG = "end_flag";
    private static final String KEY_ALIGN = "align";
    private static final String KEY_DATA_TYPE = "data_type";
    private static final String KEY_ORDER_NUM = "order_num";
    private static final String KEY_FULL_ROW = "full_row";
    private static final String KEY_MIN_VALUE_SIZE = "min_value_size";
 
 
    @Override
    public TableConfig load(Object option) {
        List<String> configContent = this.doLoadContent(option);
        if(configContent == null || configContent.size() == 0){
            logger.error("未加载到配置项内容:" + option);
            return null;
        }
 
        TableConfig tableConfig = new TableConfig();
 
        try {
            String[] keyValue = null;
            for (String line : configContent) {
                // 忽略注释行
                if (line.startsWith(Constants.VALUE_SHARP)) {
                    continue;
                }
                keyValue = line.split(Constants.EQUALS);
                if (keyValue.length != 2) {
//                    throw new IllegalArgumentException("解析表格配置行错误,等号左右必须都有值:" + line);
                    if(keyValue.length == 1){
                        logger.warn("该属性没有配置任何值:" + line);
                        continue;
                    }
                    throw new IllegalArgumentException("解析表格配置行错误,等号左右必须都有值,配置不正确:" + line);
                }
 
                logger.debug("加载key:" + keyValue[0] + ", value=" + keyValue[1]);
 
                if (keyValue[0].equals(NAME_TABLE_TYPE_KEYS)) {
                    this.processTableTypeKeys(keyValue[1], tableConfig);
 
                } else if(keyValue[0].equals(NAME_TABLE_TYPE_KEYS_NOT)){
                    // 2023-11-01 添加表格标题可排除的关键词
                    this.processTableTypeKeysNot(keyValue[1], tableConfig);
 
                } else if (keyValue[0].equals(NAME_TABLE_SETTING)) {
                    this.processTableSetting(keyValue[1], tableConfig);
 
                } else if (keyValue[0].equals(NAME_REMOVE_COLUMNS)) {
                    this.processRemoveColumns(keyValue[1], tableConfig);
 
                } else if (keyValue[0].equals(NAME_TABLE_TITLE)) {
                    tableConfig.setTableTitle(keyValue[1]);
 
                } else {
                    this.processCellConfig(keyValue[0], keyValue[1], tableConfig);
                }
            }
            logger.info("加载完成 TableConfig:" + option);
        } catch (Exception ex){
            logger.error("解析表格配置出现错误:" + option, ex);
        }
        return tableConfig;
    }
 
    private void processCellConfig(String key, String value, TableConfig tableConfig) throws Exception{
        Map<String, Object> map = JsonUtils.jsonStringToObject(value, Map.class);
        if(map == null || map.size() == 0){
            return;
        }
        CellConfigItem item = new CellConfigItem();
        item.setId(key);
 
        Object name = map.get(KEY_CELL_NAME);
        if(name != null){
            item.setName(name.toString());
        }
        Object orderNum = map.get(KEY_ORDER_NUM);
        if(orderNum != null){
            item.setOrderNum(Integer.parseInt(orderNum.toString()));
        }
        Object align = map.get(KEY_ALIGN);
        if(align != null){
            item.setAlign(align.toString());
        }
        Object multipleLine = map.get(KEY_MULTI_LINE);
        if(multipleLine != null){
            item.setMultipleLine(MultipleLine.getType(multipleLine.toString()));
        }
        Object dataType = map.get(KEY_DATA_TYPE);
        if(dataType != null){
            item.setDataType(dataType.toString());
        }
        Object noneCell = map.get(KEY_NONE_CESS);
        if(noneCell != null){
            item.setNoneCell(Boolean.parseBoolean(noneCell.toString()));
        }
        Object endFlag = map.get(KEY_END_FLAG);
        if(endFlag != null){
            item.setEndFlag(endFlag.toString());
        }
        Object fullRow = map.get(KEY_FULL_ROW);
        if(fullRow != null){
            item.setFullRow(Boolean.parseBoolean(fullRow.toString()));
        }
        Object minValueSize = map.get(KEY_MIN_VALUE_SIZE);
        if(minValueSize != null){
            item.setMinValueSize(Integer.parseInt(minValueSize.toString()));
        }
        tableConfig.addCellConfig(item);
    }
 
    private void processRemoveColumns(String text, TableConfig tableConfig){
        String[] names = text.split(Constants.VALUE_SPLIT);
        tableConfig.setRemoveColumnsName(names);
    }
 
    private void processTableSetting(String text, TableConfig tableConfig) throws Exception{
        Map<String, Object> map = JsonUtils.jsonStringToObject(text, Map.class);
        if(map == null || map.size() == 0){
            throw new IllegalArgumentException("未找到表格配置项:" + NAME_TABLE_SETTING);
        }
        Object cellTolerance = map.get(KEY_CELL_TOLERANCE);
        if(cellTolerance != null){
            tableConfig.setCellTolerance(Integer.parseInt(cellTolerance.toString()));
        }
        Object titleTolerance = map.get(KEY_TITLE_TOLERANCE);
        if(titleTolerance != null){
            tableConfig.setTitleTolerance(Float.parseFloat(titleTolerance.toString()));
        }
        Object multiLineTolerance = map.get(KEY_MULTI_LINE_TOLERANCE);
        if(multiLineTolerance != null){
            tableConfig.setMultiLineTolerance(Integer.parseInt(multiLineTolerance.toString()));
        }
    }
 
    private void processTableTypeKeys(String text, TableConfig tableConfig){
        tableConfig.setTableTypeKeys(text);
    }
 
    private void processTableTypeKeysNot(String text, TableConfig tableConfig){
        if(StringUtils.isNotEmpty(text)){
            List<String> list = StringUtils.asList(text.split(StringUtils.DEFAULT_SPLIT_SEPARATOR));
            tableConfig.setTableTitleNotKey(list);
        }
    }
 
    protected abstract List<String> doLoadContent(Object option);
}