1 /*
2  * Copyright (C) 2023 The Android Open Source Project
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 
17 package libcore.tools.analyzer.openjdk;
18 
19 import java.io.BufferedReader;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * It partially mirrors the ExpectedUpstreamFile class in
28  * libcore/tools/expected_upstream/common_util.py in Python.
29  *
30  * @see #readAllEntries to obtain the list of entries.
31  */
32 class ExpectedUpstreamFile {
33     private final Path mPath;
34 
ExpectedUpstreamFile()35     public ExpectedUpstreamFile() {
36         this.mPath = AndroidHostEnvUtil.getAndroidBuildTop().resolve("libcore/EXPECTED_UPSTREAM");
37     }
38 
readAllEntries()39     public List<ExpectedUpstreamEntry> readAllEntries() throws IOException {
40         List<ExpectedUpstreamEntry> result = new ArrayList<>();
41         try (BufferedReader in = new BufferedReader(new FileReader(mPath.toFile()))) {
42             String line;
43             StringBuilder commentLines = new StringBuilder();
44             while ((line = in.readLine()) != null) {
45                 line = line.strip();
46                 if (line.isEmpty()) {
47                     continue;
48                 } else if (line.startsWith("#")) {
49                     commentLines.append(line).append('\n');
50                     continue;
51                 }
52 
53                 ExpectedUpstreamEntry entry = parseLine(line, commentLines.toString());
54                 result.add(entry);
55                 commentLines = new StringBuilder();
56             }
57         }
58         return result;
59     }
60 
parseLine(String line, String commentLines)61     private static ExpectedUpstreamEntry parseLine(String line, String commentLines) {
62         String[] items = line.split(",");
63         int size = items.length;
64         if (size != 3) {
65             throw new IllegalStateException("The size must be 3, but is " + size + ". "
66                     + "The line is " + line);
67         }
68         return new ExpectedUpstreamEntry(items[0], items[1], items[2], commentLines);
69     }
70 
71     public static final class ExpectedUpstreamEntry {
72         public final String dstPath;
73         public final String gitRef;
74         public final String srcPath;
75         public final String commentLines;
76 
ExpectedUpstreamEntry(String dstPath, String gitRef, String srcPath, String commentLines)77         ExpectedUpstreamEntry(String dstPath, String gitRef, String srcPath,
78                 String commentLines) {
79             this.dstPath = dstPath;
80             this.gitRef = gitRef;
81             this.srcPath = srcPath;
82             this.commentLines = commentLines;
83         }
84     }
85 }
86