| | |
| | | package org.springblade.mdm.program.service.programannotation; |
| | | |
| | | import com.qiniu.util.IOUtils; |
| | | 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 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; |
| | | //@Autowired |
| | | //private ProgramAnnotationService programAnnotationService; |
| | | private String controlSystem; |
| | | |
| | | |
| | | @Override |
| | | public InputStream setAnnotation(AnnotationData annoData, InputStream inputStream) throws IOException { |
| | | List<DictBiz> annoDicts = programAnnotationService.getAnnotionDictList(); |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.toByteArray(inputStream)); |
| | | public InputStream putAnnotation(AnnotationData annoData, InputStream inputStream) throws IOException { |
| | | InputStream insAfter = putFilenameAnnotation(annoData.getFilename(),inputStream); |
| | | |
| | | String line = FileContentUtil.readLineAt(bais, annotationProperties.getProgramNameLineIndex()); |
| | | bais.reset(); |
| | | boolean isAnnotation = AnnotationUtil.isAnnotation(line,controlSystem,annoDicts); |
| | | InputStream insAfter; |
| | | 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 proNameLine = AnnotationUtil.generateAnnotation(FilenameUtils.removeExtension(annoData.getFilename()),getControlSystem(),annoDicts); |
| | | if(isAnnotation){ |
| | | insAfter = FileContentUtil.replaceAtLine(bais, annotationProperties.getProgramNameLineIndex(),proNameLine); |
| | | 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{ |
| | | insAfter = FileContentUtil.insertLine(bais, annotationProperties.getProgramNameLineIndex(),proNameLine); |
| | | //非注释,插入状态 |
| | | insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation); |
| | | } |
| | | return insAfterProgramName; |
| | | } |
| | | |
| | | return super.setAnnotation(annoData, insAfter); |
| | | |
| | | /** |
| | | * 是否为当前程序的注释 |
| | | * @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 |