|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2005, The Codehaus |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * this software and associated documentation files (the "Software"), to deal in |
| 8 | + * the Software without restriction, including without limitation the rights to |
| 9 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | + * of the Software, and to permit persons to whom the Software is furnished to do |
| 11 | + * so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package org.codehaus.plexus.compiler.eclipse; |
| 25 | + |
| 26 | +import java.io.BufferedReader; |
| 27 | +import java.io.File; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.InputStreamReader; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.LinkedList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.regex.Matcher; |
| 35 | +import java.util.regex.Pattern; |
| 36 | + |
| 37 | +import org.codehaus.plexus.compiler.CompilerMessage; |
| 38 | +import org.codehaus.plexus.compiler.CompilerMessage.Kind; |
| 39 | + |
| 40 | +/** |
| 41 | + * Parser for non-XML Eclipse Compiler output. |
| 42 | + * |
| 43 | + * @author <a href="mailto:[email protected]">Jason Faust</a> |
| 44 | + * @since 2.14.0 |
| 45 | + */ |
| 46 | +public class EcjTextLogParser implements EcjLogParser { |
| 47 | + |
| 48 | + private static final String TEN_DASH = "----------"; |
| 49 | + private static final String PATTERN_LEVEL_FILE = "^\\d+\\.\\s+(\\w+)\\s+in\\s+(.*)\\s+\\(at line (\\d+)\\)$"; |
| 50 | + |
| 51 | + private Pattern levelFile = Pattern.compile(PATTERN_LEVEL_FILE); |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<CompilerMessage> parse(File logFile, boolean errorsAsWarnings) throws Exception { |
| 55 | + List<CompilerMessage> ret = new ArrayList<>(); |
| 56 | + try (BufferedReader in = |
| 57 | + new BufferedReader(new InputStreamReader(new FileInputStream(logFile), StandardCharsets.UTF_8))) { |
| 58 | + String line; |
| 59 | + List<String> buffer = new LinkedList<>(); |
| 60 | + boolean header = true; |
| 61 | + |
| 62 | + while ((line = in.readLine()) != null) { |
| 63 | + if (header) { |
| 64 | + if (!line.startsWith(TEN_DASH)) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + header = false; |
| 68 | + } |
| 69 | + if (line.startsWith(TEN_DASH)) { |
| 70 | + if (!buffer.isEmpty()) { |
| 71 | + ret.add(parse(buffer, errorsAsWarnings)); |
| 72 | + } |
| 73 | + buffer.clear(); |
| 74 | + } else { |
| 75 | + buffer.add(line); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return ret; |
| 80 | + } |
| 81 | + |
| 82 | + private CompilerMessage parse(List<String> buffer, boolean errorsAsWarnings) { |
| 83 | + |
| 84 | + Kind kind = null; |
| 85 | + String file = null; |
| 86 | + int lineNum = 0; |
| 87 | + int startCol = 0; |
| 88 | + int endCol = 0; |
| 89 | + String message = null; |
| 90 | + |
| 91 | + // First line, kind, file, lineNum |
| 92 | + if (buffer.size() > 1) { |
| 93 | + String str = buffer.get(0); |
| 94 | + Matcher matcher = levelFile.matcher(str); |
| 95 | + if (matcher.find()) { |
| 96 | + file = matcher.group(2); |
| 97 | + kind = decodeKind(matcher.group(1), errorsAsWarnings); |
| 98 | + lineNum = Integer.parseInt(matcher.group(3)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Last line, message |
| 103 | + if (buffer.size() >= 2) { |
| 104 | + String str = buffer.get(buffer.size() - 1); |
| 105 | + message = str.trim(); |
| 106 | + } |
| 107 | + |
| 108 | + // 2nd to last line, columns |
| 109 | + if (buffer.size() >= 3) { |
| 110 | + String str = buffer.get(buffer.size() - 2); |
| 111 | + startCol = str.indexOf('^'); |
| 112 | + endCol = str.lastIndexOf('^'); |
| 113 | + } |
| 114 | + |
| 115 | + return new CompilerMessage(file, kind, lineNum, startCol, lineNum, endCol, message); |
| 116 | + } |
| 117 | + |
| 118 | + private Kind decodeKind(String str, boolean errorsAsWarnings) { |
| 119 | + if (str.equals("ERROR")) { |
| 120 | + return errorsAsWarnings ? Kind.WARNING : Kind.ERROR; |
| 121 | + } |
| 122 | + if (str.equals("INFO")) { |
| 123 | + return Kind.NOTE; |
| 124 | + } |
| 125 | + return Kind.WARNING; |
| 126 | + } |
| 127 | +} |
0 commit comments