|
| 1 | +/* |
| 2 | + * Copyright 2019 java-diff-utils. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.difflib.unifieddiff; |
| 17 | + |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.InputStreamReader; |
| 22 | +import java.io.Reader; |
| 23 | +import java.util.function.BiConsumer; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.Logger; |
| 26 | +import java.util.regex.MatchResult; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
| 29 | + |
| 30 | +/** |
| 31 | + * |
| 32 | + * @author Tobias Warneke (t.warneke@gmx.net) |
| 33 | + */ |
| 34 | +public final class UnifiedDiffParser { |
| 35 | + |
| 36 | + private final UnifiedDiffReader READER; |
| 37 | + private final UnifiedDiff data = new UnifiedDiff(); |
| 38 | + private final UnifiedDiffLine[] PARSER_RULES = new UnifiedDiffLine[]{ |
| 39 | + new UnifiedDiffLine("^\\s+", (m, l) -> LOG.info("normal " + l)), |
| 40 | + new UnifiedDiffLine(true, "^diff\\s", UnifiedDiffParser::processDiff), |
| 41 | + new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", UnifiedDiffParser::processIndex) |
| 42 | + }; |
| 43 | + private UnifiedDiffFile actualFile; |
| 44 | + |
| 45 | + UnifiedDiffParser(Reader reader) { |
| 46 | + this.READER = new UnifiedDiffReader(reader); |
| 47 | + } |
| 48 | + |
| 49 | + // schema = [[/^\s+/, normal], [/^diff\s/, start], [/^new file mode \d+$/, new_file], |
| 50 | + // [/^deleted file mode \d+$/, deleted_file], [/^index\s[\da-zA-Z]+\.\.[\da-zA-Z]+(\s(\d+))?$/, index], |
| 51 | + // [/^---\s/, from_file], [/^\+\+\+\s/, to_file], [/^@@\s+\-(\d+),?(\d+)?\s+\+(\d+),?(\d+)?\s@@/, chunk], |
| 52 | + // [/^-/, del], [/^\+/, add], [/^\\ No newline at end of file$/, eof]]; |
| 53 | + private UnifiedDiff parse() throws IOException { |
| 54 | + boolean header = true; |
| 55 | + String headerTxt = ""; |
| 56 | + while (READER.ready()) { |
| 57 | + String line = READER.readLine(); |
| 58 | + if (processLine(header, line) == false) { |
| 59 | + if (header) { |
| 60 | + headerTxt += line + "\n"; |
| 61 | + } else { |
| 62 | + break; |
| 63 | + } |
| 64 | + } else { |
| 65 | + header = false; |
| 66 | + data.setHeader(headerTxt); |
| 67 | + } |
| 68 | + } |
| 69 | + return data; |
| 70 | + } |
| 71 | + |
| 72 | + static String[] parseFileNames(String line) { |
| 73 | + String[] split = line.split(" "); |
| 74 | + return new String[]{ |
| 75 | + split[2].replaceAll("^a/", ""), |
| 76 | + split[3].replaceAll("^b/", "") |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + private static final Logger LOG = Logger.getLogger(UnifiedDiffParser.class.getName()); |
| 81 | + |
| 82 | + public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOException { |
| 83 | + UnifiedDiffParser parser = new UnifiedDiffParser(new BufferedReader(new InputStreamReader(stream))); |
| 84 | + return parser.parse(); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean processLine(boolean header, String line) { |
| 88 | + for (UnifiedDiffLine rule : PARSER_RULES) { |
| 89 | + if (header && rule.isStopsHeaderParsing() || !header) { |
| 90 | + if (rule.processLine(line)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + private void initFileIfNecessary() { |
| 99 | + if (actualFile == null) { |
| 100 | + actualFile = new UnifiedDiffFile(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void processDiff(MatchResult match, String line) { |
| 105 | + initFileIfNecessary(); |
| 106 | + LOG.log(Level.INFO, "start {0}", line); |
| 107 | + String[] fromTo = parseFileNames(READER.lastLine()); |
| 108 | + actualFile.setFromFile(fromTo[0]); |
| 109 | + actualFile.setToFile(fromTo[1]); |
| 110 | + } |
| 111 | + |
| 112 | + private void processIndex(MatchResult match, String line) { |
| 113 | + initFileIfNecessary(); |
| 114 | + LOG.log(Level.INFO, "index {0}", line); |
| 115 | + actualFile.setIndex(line.substring(6)); |
| 116 | + } |
| 117 | + |
| 118 | + class UnifiedDiffLine { |
| 119 | + |
| 120 | + private final Pattern pattern; |
| 121 | + private final BiConsumer<MatchResult, String> command; |
| 122 | + private final boolean stopsHeaderParsing; |
| 123 | + |
| 124 | + public UnifiedDiffLine(String pattern, BiConsumer<MatchResult, String> command) { |
| 125 | + this(false, pattern, command); |
| 126 | + } |
| 127 | + |
| 128 | + public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer<MatchResult, String> command) { |
| 129 | + this.pattern = Pattern.compile(pattern); |
| 130 | + this.command = command; |
| 131 | + this.stopsHeaderParsing = stopsHeaderParsing; |
| 132 | + } |
| 133 | + |
| 134 | + public boolean processLine(String line) { |
| 135 | + Matcher m = pattern.matcher(line); |
| 136 | + if (m.find()) { |
| 137 | + command.accept(m.toMatchResult(), line); |
| 138 | + return true; |
| 139 | + } else { |
| 140 | + return false; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public boolean isStopsHeaderParsing() { |
| 145 | + return stopsHeaderParsing; |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +class UnifiedDiffReader extends BufferedReader { |
| 151 | + |
| 152 | + private String lastLine; |
| 153 | + |
| 154 | + public UnifiedDiffReader(Reader reader) { |
| 155 | + super(reader); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public String readLine() throws IOException { |
| 160 | + lastLine = super.readLine(); |
| 161 | + return lastLine(); |
| 162 | + } |
| 163 | + |
| 164 | + String lastLine() { |
| 165 | + return lastLine; |
| 166 | + } |
| 167 | +} |
0 commit comments