wuxw
2019-05-05 3e77432495dcbca5b2ba3b03e5f18246777fca75
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
package com.java110.web.core;
 
import org.springframework.util.StringUtils;
 
import java.io.*;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 静态资源文件加载器
 * Created by wuxw on 2019/3/18.
 */
public class VueComponentTemplate extends PackageScanner {
 
    /**
     * 默认扫描路径
     */
    public final static String DEFAULT_COMPONENT_PACKAGE_PATH = "components";
 
    /**
     * js 文件
     */
    public final static String COMPONENT_JS = "js";
 
    /**
     * css 文件
     */
    public final static String COMPONENT_CSS = "css";
 
    /**
     * html 文件
     */
    public final static String COMPONENT_HTML = "html";
 
 
    /**
     * HTML 文件缓存器
     */
    private final static Map<String, String> componentTemplate = new HashMap<>();
 
 
    /**
     * 初始化 组件信息
     */
    public static void initComponent(String scanPath) {
        VueComponentTemplate vueComponentTemplate = new VueComponentTemplate();
        vueComponentTemplate.packageScanner(scanPath, COMPONENT_JS);
        vueComponentTemplate.packageScanner(scanPath, COMPONENT_HTML);
        vueComponentTemplate.packageScanner(scanPath, COMPONENT_CSS);
    }
 
 
    /**
     * 根据组件编码查询模板
     *
     * @param componentCode
     * @return
     */
    public static String findTemplateByComponentCode(String componentCode) {
        if (componentTemplate.containsKey(componentCode)) {
            return componentTemplate.get(componentCode);
        }
 
        return null;
    }
 
 
    /**
     * 处理资源
     *
     * @param filePath
     */
    protected void handleResource(String filePath) {
        Reader reader = null;
        String sb = "";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
            reader = new InputStreamReader(inputStream, "UTF-8");
            int tempChar;
            StringBuffer b = new StringBuffer();
            while ((tempChar = reader.read()) != -1) {
                b.append((char) tempChar);
            }
            sb = b.toString();
            if (!StringUtils.isEmpty(sb)) {
                componentTemplate.put(filePath.substring(filePath.lastIndexOf(File.separator) + 1, filePath.length()), sb);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}