[77] | 1 | package com.framsticks.parsers; |
---|
| 2 | |
---|
| 3 | import com.framsticks.params.*; |
---|
[100] | 4 | import org.apache.logging.log4j.Logger; |
---|
| 5 | import org.apache.logging.log4j.LogManager; |
---|
[77] | 6 | |
---|
| 7 | import java.util.*; |
---|
| 8 | |
---|
| 9 | public class MultiParamLoader { |
---|
[87] | 10 | |
---|
| 11 | public interface StatusListener { |
---|
| 12 | void onStatusChange(); |
---|
| 13 | } |
---|
| 14 | |
---|
[100] | 15 | private final static Logger log = LogManager.getLogger(MultiParamLoader.class); |
---|
[77] | 16 | |
---|
| 17 | /** |
---|
| 18 | * The class which name was recently found in the file, to which execution |
---|
| 19 | * should be passed. |
---|
| 20 | */ |
---|
[100] | 21 | public Access getLastAccess() { |
---|
| 22 | return lastAccess; |
---|
[77] | 23 | } |
---|
| 24 | |
---|
| 25 | /** TODO does it support multilines? */ |
---|
| 26 | /** |
---|
| 27 | * Specifies the condition to break execution. |
---|
| 28 | */ |
---|
| 29 | public enum Status { |
---|
| 30 | None, Finished, BeforeObject, AfterObject, BeforeUnknown, OnComment, OnError, Loading |
---|
| 31 | } |
---|
| 32 | |
---|
[87] | 33 | protected final Map<Status, List<StatusListener>> listeners = new HashMap<>(); |
---|
| 34 | |
---|
[77] | 35 | /** |
---|
| 36 | * Specifies the action that should be taken inside loops. |
---|
| 37 | */ |
---|
| 38 | private enum LoopAction { |
---|
| 39 | Nothing, Break, Continue |
---|
| 40 | } |
---|
| 41 | |
---|
[100] | 42 | protected Access lastAccess; |
---|
[77] | 43 | |
---|
[88] | 44 | protected static final FramsClass emptyFramsClass = FramsClass.build().idAndName("<empty>").finish(); |
---|
[77] | 45 | /** |
---|
| 46 | * Empty Param representing unknown classes - used to omit unknown |
---|
| 47 | * objects in the file. |
---|
| 48 | */ |
---|
[100] | 49 | protected Access emptyParam = new PropertiesAccess(emptyFramsClass); |
---|
[77] | 50 | |
---|
| 51 | /** |
---|
| 52 | * Last comment found in the file. |
---|
| 53 | */ |
---|
| 54 | protected String lastComment; |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * Set of break conditions. |
---|
| 58 | */ |
---|
| 59 | private EnumSet<Status> breakConditions = EnumSet.of(Status.None); |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Status of current execution. |
---|
| 63 | */ |
---|
[86] | 64 | private Status status = Status.None; |
---|
[77] | 65 | |
---|
[86] | 66 | |
---|
[77] | 67 | /** |
---|
| 68 | * File from which data should be read. |
---|
| 69 | */ |
---|
[101] | 70 | private Source currentSource; |
---|
[77] | 71 | |
---|
[86] | 72 | protected String currentLine; |
---|
[77] | 73 | |
---|
[86] | 74 | |
---|
[77] | 75 | /** |
---|
| 76 | * All the files read by the loader (there could be many of them because of |
---|
| 77 | * '#|include'). |
---|
| 78 | */ |
---|
| 79 | private Stack<String> fileStack = new Stack<String>(); |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * A map that specifies connection between the getName of the file and the |
---|
| 83 | * actual reader. |
---|
| 84 | */ |
---|
[101] | 85 | private Map<String, Source> fileMap = new HashMap<String, Source>(); |
---|
[77] | 86 | |
---|
| 87 | /** |
---|
| 88 | * List of known classes. |
---|
| 89 | */ |
---|
[101] | 90 | protected AccessProvider accessProvider = new AccessStash(); |
---|
[77] | 91 | |
---|
| 92 | /** |
---|
| 93 | * Last unknown object found in the file. |
---|
| 94 | */ |
---|
| 95 | private String lastUnknownObjectName; |
---|
| 96 | |
---|
[86] | 97 | /** |
---|
| 98 | * @return the currentLine |
---|
| 99 | */ |
---|
| 100 | public String getCurrentLine() { |
---|
| 101 | return currentLine; |
---|
| 102 | } |
---|
| 103 | |
---|
[101] | 104 | /** |
---|
| 105 | * @return the accessProvider |
---|
| 106 | */ |
---|
| 107 | public AccessProvider getAccessProvider() { |
---|
| 108 | return accessProvider; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * @param accessProvider the accessProvider to set |
---|
| 113 | */ |
---|
| 114 | public void setAccessProvider(AccessProvider accessProvider) { |
---|
| 115 | this.accessProvider = accessProvider; |
---|
| 116 | } |
---|
| 117 | |
---|
[77] | 118 | public MultiParamLoader() { |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Starts reading the file. |
---|
| 124 | */ |
---|
[97] | 125 | public Status go() { |
---|
[86] | 126 | log.trace("go"); |
---|
[77] | 127 | |
---|
| 128 | while (!isFinished()) { |
---|
| 129 | // check if we are before some known or unknown object |
---|
| 130 | LoopAction loopAction = tryReadObject(); |
---|
| 131 | if (loopAction == LoopAction.Break) { |
---|
| 132 | break; |
---|
| 133 | } else if (loopAction == LoopAction.Continue) { |
---|
| 134 | continue; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // read data |
---|
[86] | 138 | currentLine = currentSource.readLine(); |
---|
[77] | 139 | |
---|
| 140 | // end of file |
---|
[86] | 141 | if (currentLine == null) { |
---|
[77] | 142 | if (!returnFromIncluded()) { |
---|
| 143 | finish(); |
---|
| 144 | break; |
---|
| 145 | } else { |
---|
| 146 | continue; |
---|
| 147 | } |
---|
| 148 | } |
---|
[100] | 149 | log.trace("read line: {}", currentLine); |
---|
[77] | 150 | |
---|
| 151 | // empty line |
---|
[86] | 152 | if (currentLine.length() == 0) { |
---|
[77] | 153 | continue; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // check if some file should be included |
---|
[86] | 157 | if (isIncludeLine(currentLine) == LoopAction.Continue) { |
---|
[77] | 158 | continue; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | // check if should break on comment |
---|
[86] | 162 | if (isCommentLine(currentLine) == LoopAction.Break) { |
---|
[77] | 163 | break; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // get class getName |
---|
[86] | 167 | LoopAction action = changeCurrentParamInterface(currentLine); |
---|
| 168 | if (action == LoopAction.Break) { |
---|
[77] | 169 | break; |
---|
| 170 | } |
---|
[86] | 171 | if (action == LoopAction.Continue) { |
---|
| 172 | continue; |
---|
| 173 | } |
---|
[97] | 174 | |
---|
[100] | 175 | // log.warn("unknown line: {}", currentLine); |
---|
[97] | 176 | changeStatus(Status.OnError); |
---|
| 177 | if (action == LoopAction.Break) { |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | if (action == LoopAction.Continue) { |
---|
| 181 | continue; |
---|
| 182 | } |
---|
[77] | 183 | } |
---|
| 184 | |
---|
| 185 | return status; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | * Checks whether the reader found a known or unknown object and execution |
---|
| 190 | * should be passed to it. |
---|
[86] | 191 | * @throws Exception |
---|
[77] | 192 | */ |
---|
[97] | 193 | private LoopAction tryReadObject() { |
---|
[100] | 194 | if (status == Status.BeforeObject || (status == Status.BeforeUnknown && lastAccess != null)) { |
---|
[77] | 195 | // found object - let it load data |
---|
[100] | 196 | if (lastAccess.getSelected() == null) { |
---|
| 197 | lastAccess.select(lastAccess.createAccessee()); |
---|
[86] | 198 | } |
---|
[100] | 199 | log.trace("loading into {}", lastAccess); |
---|
[101] | 200 | AccessOperations.load(lastAccess, currentSource); |
---|
[77] | 201 | |
---|
[87] | 202 | if (changeStatus(Status.AfterObject)) { |
---|
[77] | 203 | return LoopAction.Break; |
---|
| 204 | } |
---|
| 205 | return LoopAction.Continue; |
---|
| 206 | } else if (status == Status.BeforeUnknown) { |
---|
[100] | 207 | log.warn("omitting unknown object: {}", lastUnknownObjectName); |
---|
[77] | 208 | |
---|
| 209 | // found unknown object |
---|
[101] | 210 | AccessOperations.load(emptyParam, currentSource); |
---|
[87] | 211 | if (changeStatus(Status.AfterObject)) { |
---|
| 212 | return LoopAction.Break; |
---|
| 213 | } |
---|
[77] | 214 | return LoopAction.Continue; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | return LoopAction.Nothing; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | * Checks whether some additional file shouldn't be included. |
---|
| 222 | */ |
---|
[97] | 223 | private LoopAction isIncludeLine(String line) { |
---|
[77] | 224 | try { |
---|
| 225 | // found comment |
---|
| 226 | if (line.charAt(0) == '#') { |
---|
| 227 | // maybe we should include something |
---|
| 228 | if (line.substring(1, 8).equals("include")) { |
---|
| 229 | int beg = line.indexOf('\"'); |
---|
| 230 | if (beg == -1) { |
---|
[86] | 231 | log.info("Wanted to include some file, but the format is incorrect"); |
---|
[77] | 232 | return LoopAction.Continue; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | String includeFileName = line.substring(beg + 1); |
---|
| 236 | int end = includeFileName.indexOf('\"'); |
---|
| 237 | if (end == -1) { |
---|
[86] | 238 | log.info("Wanted to include some file, but the format is incorrect"); |
---|
[77] | 239 | return LoopAction.Continue; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | includeFileName = includeFileName.substring(0, end); |
---|
| 243 | |
---|
| 244 | include(includeFileName); |
---|
| 245 | |
---|
| 246 | return LoopAction.Continue; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | } catch (IndexOutOfBoundsException ex) { |
---|
| 250 | // value after # sign is shorter than expected 7 characters - do |
---|
| 251 | // nothing |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | return LoopAction.Nothing; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | /** |
---|
| 258 | * Checks whether execution shouldn't break on comment. |
---|
| 259 | */ |
---|
| 260 | private LoopAction isCommentLine(String line) { |
---|
[87] | 261 | if (line.charAt(0) == '#') { |
---|
[77] | 262 | lastComment = line; |
---|
[87] | 263 | if (changeStatus(Status.OnComment)) { |
---|
| 264 | // it's a simple comment - maybe we should break? |
---|
| 265 | return LoopAction.Break; |
---|
| 266 | } |
---|
[77] | 267 | } |
---|
| 268 | return LoopAction.Nothing; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | /** |
---|
| 272 | * Gets the getName of the class from line read from file. |
---|
| 273 | */ |
---|
| 274 | private LoopAction changeCurrentParamInterface(String line) { |
---|
| 275 | // found key - value line |
---|
| 276 | if (line.charAt(line.length() - 1) == ':') { |
---|
| 277 | String typeName = line.substring(0, line.length() - 1); |
---|
[101] | 278 | lastAccess = accessProvider.getAccess(typeName); |
---|
[77] | 279 | |
---|
[100] | 280 | if (lastAccess != null) { |
---|
[87] | 281 | if (changeStatus(Status.BeforeObject)) { |
---|
[77] | 282 | return LoopAction.Break; |
---|
[86] | 283 | } else { |
---|
| 284 | return LoopAction.Continue; |
---|
[77] | 285 | } |
---|
| 286 | } else { |
---|
| 287 | lastUnknownObjectName = typeName; |
---|
[87] | 288 | if (changeStatus(Status.BeforeUnknown)) { |
---|
[77] | 289 | return LoopAction.Break; |
---|
[86] | 290 | } else { |
---|
| 291 | return LoopAction.Continue; |
---|
[77] | 292 | } |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | return LoopAction.Nothing; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | /** |
---|
| 299 | * Adds another break condition. |
---|
| 300 | */ |
---|
| 301 | public void addBreakCondition(Status condition) { |
---|
| 302 | breakConditions.add(condition); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /** |
---|
| 306 | * Removes break condition. |
---|
| 307 | */ |
---|
| 308 | public void removeBreakCondition(Status condition) { |
---|
| 309 | breakConditions.remove(condition); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /** |
---|
| 313 | * Adds another class. |
---|
| 314 | */ |
---|
[100] | 315 | public void addAccess(Access access) { |
---|
[101] | 316 | accessProvider.addAccess(access); |
---|
[77] | 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * Checks whether execution is finished. |
---|
| 321 | */ |
---|
| 322 | private boolean isFinished() { |
---|
| 323 | return (status == Status.Finished); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | private void finish() { |
---|
[86] | 327 | log.trace("finishing"); |
---|
[77] | 328 | if (currentSource != null) { |
---|
| 329 | currentSource.close(); |
---|
| 330 | } |
---|
| 331 | |
---|
[87] | 332 | changeStatus(Status.Finished); |
---|
[77] | 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
| 336 | * Opens selected file. |
---|
| 337 | */ |
---|
| 338 | |
---|
[101] | 339 | public boolean setNewSource(Source source) { |
---|
[100] | 340 | log.debug("switching current source to {}...", source.getFilename()); |
---|
[77] | 341 | |
---|
| 342 | currentSource = source; |
---|
[87] | 343 | changeStatus(Status.Loading); |
---|
[77] | 344 | |
---|
| 345 | return true; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | /** |
---|
| 349 | * Includes specified file. |
---|
| 350 | */ |
---|
| 351 | private void include(String includeFilename) { |
---|
| 352 | |
---|
| 353 | includeFilename = currentSource.demangleInclude(includeFilename); |
---|
| 354 | |
---|
| 355 | if (includeFilename == null) { |
---|
| 356 | return; |
---|
| 357 | } |
---|
| 358 | // check if it is already included and break if it is |
---|
| 359 | if (isAlreadyIncluded(includeFilename)) { |
---|
[100] | 360 | log.debug("circular reference ignored ({})", includeFilename); |
---|
[77] | 361 | return; |
---|
| 362 | } |
---|
| 363 | |
---|
[100] | 364 | log.info("including file {}...", includeFilename); |
---|
[77] | 365 | |
---|
[101] | 366 | Source newSource = currentSource.openInclude(includeFilename); |
---|
[77] | 367 | if (newSource == null) { |
---|
| 368 | return; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | fileStack.add(currentSource.getFilename()); |
---|
| 372 | fileMap.put(currentSource.getFilename(), currentSource); |
---|
| 373 | setNewSource(newSource); |
---|
| 374 | |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
| 378 | * Checks whether selected file was already included. |
---|
| 379 | */ |
---|
| 380 | private boolean isAlreadyIncluded(String filename) { |
---|
| 381 | for (String file : fileStack) { |
---|
| 382 | if (filename.equals(file)) { |
---|
[100] | 383 | log.warn("file {} was already included", filename); |
---|
[77] | 384 | return true; |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | return false; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | /** |
---|
| 392 | * Returns from included file. |
---|
| 393 | */ |
---|
[97] | 394 | private boolean returnFromIncluded() { |
---|
[77] | 395 | if (fileStack.size() == 0) { |
---|
| 396 | return false; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | if (currentSource != null) { |
---|
| 400 | currentSource.close(); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | String filename = fileStack.pop(); |
---|
| 404 | currentSource = fileMap.get(filename); |
---|
| 405 | fileMap.remove(filename); |
---|
| 406 | |
---|
| 407 | return true; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | /** |
---|
| 411 | * Checks whether execution should break on selected condition. |
---|
| 412 | */ |
---|
[87] | 413 | private boolean changeStatus(Status status) { |
---|
[100] | 414 | log.trace("changing status: {} -> {}", this.status.toString(), status.toString()); |
---|
[87] | 415 | this.status = status; |
---|
| 416 | if (listeners.containsKey(status)) { |
---|
| 417 | for (StatusListener l : listeners.get(status)) { |
---|
| 418 | l.onStatusChange(); |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | return breakConditions.contains(status); |
---|
[77] | 422 | } |
---|
| 423 | |
---|
| 424 | public Object returnObject() { |
---|
[100] | 425 | assert lastAccess != null; |
---|
| 426 | Object result = lastAccess.getSelected(); |
---|
[77] | 427 | if (result == null) { |
---|
| 428 | return null; |
---|
| 429 | } |
---|
[100] | 430 | lastAccess.select(null); |
---|
[77] | 431 | return result; |
---|
| 432 | } |
---|
[87] | 433 | |
---|
| 434 | public void addListener(Status status, StatusListener listener) { |
---|
| 435 | if (!listeners.containsKey(status)) { |
---|
| 436 | listeners.put(status, new LinkedList<StatusListener>()); |
---|
| 437 | } |
---|
| 438 | listeners.get(status).add(listener); |
---|
| 439 | } |
---|
[100] | 440 | |
---|
[101] | 441 | public static List<Object> loadAll(Source source, Access access) { |
---|
[100] | 442 | final List<Object> result = new LinkedList<>(); |
---|
| 443 | |
---|
| 444 | final MultiParamLoader loader = new MultiParamLoader(); |
---|
| 445 | loader.setNewSource(source); |
---|
| 446 | loader.addAccess(access); |
---|
| 447 | loader.addListener(MultiParamLoader.Status.AfterObject, new StatusListener() { |
---|
| 448 | @Override |
---|
| 449 | public void onStatusChange() { |
---|
| 450 | result.add(loader.returnObject()); |
---|
| 451 | } |
---|
| 452 | }); |
---|
| 453 | loader.go(); |
---|
| 454 | return result; |
---|
| 455 | } |
---|
[77] | 456 | } |
---|