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 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; } }