• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007 Google Inc.
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 com.tonicsystems.jarjar;
18 
19 import static java.nio.charset.StandardCharsets.UTF_8;
20 
21 import com.android.jarjar.StripAnnotation;
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.Reader;
26 import java.nio.file.Files;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 
31 class RulesFileParser {
RulesFileParser()32   private RulesFileParser() {}
33 
parse(File file)34   public static List<PatternElement> parse(File file) throws IOException {
35     return parse(Files.newBufferedReader(file.toPath(), UTF_8));
36   }
37 
parse(String value)38   public static List<PatternElement> parse(String value) throws IOException {
39     return parse(new java.io.StringReader(value));
40   }
41 
parse(Reader r)42   private static List<PatternElement> parse(Reader r) throws IOException {
43     try {
44       List<PatternElement> patterns = new ArrayList<>();
45       BufferedReader br = new BufferedReader(r);
46       int c = 1;
47       String line;
48       while ((line = br.readLine()) != null) {
49         line = stripComment(line);
50         if (line.isEmpty()) {
51           continue;
52         }
53         String[] parts = line.split("\\s+");
54         if (parts.length < 2) {
55           error(c, parts);
56         }
57         String type = parts[0];
58         PatternElement element = null;
59         switch (type) {
60           case "rule":
61             if (parts.length < 3) {
62               error(c, parts);
63             }
64             Rule rule = new Rule();
65             rule.setResult(parts[2]);
66             element = rule;
67             break;
68           case "zap":
69             element = new Zap();
70             break;
71           case "keep":
72             element = new Keep();
73             break;
74           // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
75           case "strip-annotation":
76             element = new StripAnnotation();
77             break;
78           // ANDROID-END: b/222743634 Strip annotations from system module stubs
79           default:
80             error(c, parts);
81         }
82         element.setPattern(parts[1]);
83         patterns.add(element);
84         c++;
85       }
86       return patterns;
87     } finally {
88       r.close();
89     }
90   }
91 
stripComment(String in)92   private static String stripComment(String in) {
93     int p = in.indexOf("#");
94     return p < 0 ? in : in.substring(0, p);
95   }
96 
error(int line, String[] parts)97   private static void error(int line, String[] parts) {
98     throw new IllegalArgumentException("Error on line " + line + ": " + Arrays.asList(parts));
99   }
100 }
101