1 /* 2 * Copyright (c) 2025 Shenzhen Kaihong Digital. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package parse; 17 18 import antlr.ParseBaseListener; 19 import com.fasterxml.jackson.core.JsonProcessingException; 20 import com.fasterxml.jackson.databind.ObjectMapper; 21 import grammar.*; 22 import org.antlr.v4.runtime.CharStream; 23 import utils.BaseEvent; 24 import utils.BaseListener; 25 import utils.Constants; 26 27 import java.util.List; 28 import java.util.concurrent.CopyOnWriteArrayList; 29 30 /** 31 * <h3>类名:该类用于xxx</h3> 32 * description base of parse 33 * 34 * @author Administrator 35 * date 2025-02-28 36 * @version 1.0 37 * @since 2025-02-28 38 */ 39 public abstract class ParseBase { 40 41 /** 42 * 文件内容 43 */ 44 protected String fileContent; 45 46 /** 47 * 文件char stream 48 */ 49 protected CharStream fcStream; 50 51 /** 52 * 状态 53 */ 54 protected String status; 55 56 /** 57 * 进度消息 58 */ 59 protected String procMsg; 60 61 /** 62 * 进度数据 63 */ 64 protected int progress; 65 66 /** 67 * 总进度 68 */ 69 protected int totalProgress = Constants.HUNDRED_PERCENT; 70 71 /** 72 * 存储所有监听回调 73 */ 74 protected final List<BaseListener> listeners = new CopyOnWriteArrayList<>(); 75 76 /** 77 * 构造函数 78 */ ParseBase()79 public ParseBase() {} 80 81 /** 82 * 增加listener 83 * 84 * @param listener 监听器 85 */ addListener(BaseListener listener)86 public void addListener(BaseListener listener) { 87 listeners.add(listener); 88 } 89 90 /** 91 * send event 92 * 93 * @param status 状态 94 * @param msg 消息 95 * @param process 进度 96 */ sendEvent(String status, String msg, int process)97 protected void sendEvent(String status, String msg, int process) { 98 this.procMsg = msg; 99 this.status = status; 100 this.progress = process; 101 doNotify(status, msg, process); 102 } 103 104 /** 105 * notify parse info 106 * 107 * @param status 状态 108 * @param msg 消息 109 * @param process 进度 110 */ doNotify(String status, String msg, int process)111 protected void doNotify(String status, String msg, int process) { 112 BaseEvent pcEvent = new BaseEvent(this); 113 pcEvent.setEventMsg("parsec complete"); 114 ParseTaskInfo pi = new ParseTaskInfo(status, msg, process, this.totalProgress); 115 ObjectMapper mapper = new ObjectMapper(); 116 try { 117 String jsonStr = mapper.writeValueAsString(pi); 118 pcEvent.setEventMsg(jsonStr); 119 } catch (JsonProcessingException e) { 120 System.out.println("json process error: " + e.getMessage()); 121 } 122 listeners.forEach(listener -> { 123 listener.onEvent(pcEvent); 124 }); 125 } 126 127 /** 128 * 根据文件名解析文件 129 * 130 * @param filePath 文件路径 131 */ parseFile(String filePath)132 public abstract void parseFile(String filePath); 133 134 /** 135 * 根据文件内容解析文件 136 * 137 * @param fileContent 文件内容 138 */ parseContent(String fileContent)139 public abstract void parseContent(String fileContent); 140 141 /** 142 * 根据文件char stream解析文件 143 * 144 * @param fileCStream 145 * 文件内容 146 * @return 解析结果 147 */ parseCStream(CharStream fileCStream)148 public abstract ParseObj parseCStream(CharStream fileCStream); 149 150 /** 151 * 生成 解析结果 152 * 153 * @param pbl 解析监听 154 * @return 解析结果 155 */ genParseResult(ParseBaseListener pbl)156 protected abstract ParseObj genParseResult(ParseBaseListener pbl); 157 158 /** 159 * 解析enum 160 * 161 * @param pi2 解析结果 162 * @return enum 163 */ parseEnum(ParseTaskInfo pi2)164 protected EnumObj[] parseEnum(ParseTaskInfo pi2) { 165 System.out.println("parse enum: " + pi2.toString()); 166 return new EnumObj[0]; 167 }; 168 169 /** 170 * 解析union 171 * 172 * @param pi2 解析任务 173 * @return union 174 */ parseUnion(ParseTaskInfo pi2)175 protected UnionObj[] parseUnion(ParseTaskInfo pi2) { 176 System.out.println("parse union: " + pi2.toString()); 177 return new UnionObj[0]; 178 } 179 180 /** 181 * 解析struct 182 * 183 * @return struct 184 */ parseStruct(ParseTaskInfo pi2)185 protected StructObj[] parseStruct(ParseTaskInfo pi2) { 186 System.out.println("parse struct: " + pi2.toString()); 187 return new StructObj[0]; 188 } 189 190 /** 191 * 解析class 192 * 193 * @return class 194 */ parseClass(ParseTaskInfo pi2)195 protected ClassObj[] parseClass(ParseTaskInfo pi2) { 196 System.out.println("parse class: " + pi2.toString()); 197 return new ClassObj[0]; 198 } 199 200 /** 201 * 解析func 202 * 203 * @return func 204 */ parseFunc(ParseTaskInfo pi2)205 protected FuncObj[] parseFunc(ParseTaskInfo pi2) { 206 System.out.println("parse function: " + pi2.toString()); 207 return new FuncObj[0]; 208 } 209 210 /** 211 * 解析type 212 * 213 * @param pi2 解析进程 214 * @return type 215 */ parseType(ParseTaskInfo pi2)216 protected TypeObj[] parseType(ParseTaskInfo pi2) { 217 System.out.println("parse type: " + pi2.toString()); 218 return new TypeObj[0]; 219 } 220 221 /** 222 * 接收解析结果 223 * 224 * @param pi2 解析结构 225 */ receive(ParseTaskInfo pi2)226 public void receive(ParseTaskInfo pi2) { 227 System.out.println("receive parse result: " + pi2.getJsonData()); 228 } 229 } 230