刘光辉
6 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
package jnpf.elnService.template.docx;
 
import org.springframework.util.StringUtils;
 
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
 
public final class DocxControlConfigMatcher {
 
    private static final String DROPDOWN = "DROPDOWN";
    private static final String DATE = "DATE";
 
    private DocxControlConfigMatcher() {
    }
 
    /**
     * OOXML does not reliably expose field placeholders, default text, default checkbox state,
     * or checkbox presentation metadata. Dropdown items and date format currently cross the
     * field-config and DOCX evidence boundary.
     */
    public static boolean matches(String controlType, Map<String, Object> fieldConfig,
            Map<String, Object> docxConfig) {
        if (DROPDOWN.equalsIgnoreCase(controlType)) {
            Map<String, String> expected = fieldOptions(fieldConfig);
            Map<String, String> observed = docxOptions(docxConfig);
            return expected != null && observed != null && expected.equals(observed);
        }
        if (DATE.equalsIgnoreCase(controlType)) {
            String expected = fieldConfig == null ? null : text(fieldConfig.get("dateFormat"));
            String observed = docxConfig == null ? null : text(docxConfig.get("dateFormat"));
            return StringUtils.hasText(expected) && expected.equals(observed);
        }
        return true;
    }
 
    private static Map<String, String> fieldOptions(Map<String, Object> config) {
        if (config == null || !(config.get("options") instanceof Collection)) {
            return null;
        }
        Map<String, String> result = new LinkedHashMap<>();
        for (Object option : (Collection<?>) config.get("options")) {
            if (!(option instanceof Map)) {
                return null;
            }
            Map<?, ?> values = (Map<?, ?>) option;
            String value = text(values.get("value"));
            String display = text(values.get("display"));
            if (!StringUtils.hasText(value) || !StringUtils.hasText(display)
                    || result.containsKey(value)) {
                return null;
            }
            result.put(value, display);
        }
        return result;
    }
 
    private static Map<String, String> docxOptions(Map<String, Object> config) {
        if (config == null) {
            return new LinkedHashMap<>();
        }
        if (config.get("options") instanceof Collection) {
            return fieldOptions(config);
        }
        Map<String, String> result = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : config.entrySet()) {
            String value = text(entry.getKey());
            String display = text(entry.getValue());
            if (!StringUtils.hasText(value) || !StringUtils.hasText(display)
                    || result.containsKey(value)) {
                return null;
            }
            result.put(value, display);
        }
        return result;
    }
 
    private static String text(Object value) {
        return value == null ? null : String.valueOf(value).trim();
    }
}