yangys
2025-09-29 4c7296d45efe849dc70a3b2e2240c905481a91c9
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
package org.springblade.mdm.program.service.programannotation;
 
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.system.pojo.entity.DictBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
@Component("defaultProcessor")
public class DefaultProcessor extends AbstractProcessor{
 
    //@Autowired
    //private ProgramAnnotationService programAnnotationService;
    private String controlSystem;
 
 
    @Override
    public InputStream putAnnotation(AnnotationData annoData, InputStream inputStream) throws IOException {
        InputStream insAfter = putFilenameAnnotation(annoData.getFilename(),inputStream);
 
        return super.putAnnotation(annoData, insAfter);
 
    }
 
    @Override
    public InputStream putFilenameAnnotation(String fileName, InputStream inputStream) throws IOException {
        List<DictBiz> annoDicts = getAnnotationDictList();
        ByteArrayInputStream byteStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
 
        String programNameLine = FileContentUtil.readLineAt(byteStream, annotationProperties.getProgramNameLineIndex());
        byteStream.reset();
        boolean isAnnotation = AnnotationUtil.isAnnotation(programNameLine,this.getControlSystem(),annoDicts);
        InputStream insAfterProgramName;
        //加入程序名注释行
        String programNameStr = genProgramNameText(FilenameUtils.removeExtension(fileName));
 
        String proNameAnnotation =  AnnotationUtil.generateAnnotation(programNameStr,getControlSystem(),annoDicts);
 
        if(isAnnotation){//是注释
            //提取注释内文字
            String programName = FilenameUtils.removeExtension(fileName);
            String progNameLineWithoutAnno = AnnotationUtil.removeAnnotation(this.getControlSystem(),programNameLine,annoDicts);
            if(!isCurrentProgramNameAnnotation(progNameLineWithoutAnno,programName)) {
                //注释不是程序名(工艺员自己写的注释),插入
                insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation);
            }else{
                //是程序名注释,不处理,返回原stream
                insAfterProgramName =  byteStream;
            }
        }else{
            //非注释,插入状态
            insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation);
        }
        return insAfterProgramName;
    }
 
    /**
     * 是否为当前程序的注释
     * @param lineTextWithoutAnno 注释行的文字
     * @param programName 程序名称
     * @return 是否时程序注释行,如果与当前程序名匹配则true
     */
    boolean isCurrentProgramNameAnnotation(String lineTextWithoutAnno,String programName){
        Pattern pattern = Pattern.compile("^\\((.+)\\)");
        Matcher matcher = pattern.matcher(lineTextWithoutAnno);
        if(matcher.find()){
            String findText = matcher.group(1);
            return findText.equals(programName);
        }else{
            return false;
        }
    }
    /**
     * 生成程序名成的行 加入 (pname=[程序名称])
     * @param proggramName
     * @return 报装后的程序名称
     */
    private String genProgramNameText(String proggramName) {
        return String.format("(%s)",proggramName);
    }
 
    @Override
    public void setControlSystem(String controlSystemDictVal) {
        this.controlSystem = controlSystemDictVal;
    }
 
    @Override
    public String getControlSystem() {
        return this.controlSystem;
    }
 
 
}