summaryrefslogtreecommitdiff
path: root/src/main/com/tonicsystems/jarjar/Wildcard.java
blob: c5d0c206ba5d4c522f357b9da8c968997835a9c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonicsystems.jarjar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Wildcard {
  private static final Pattern DSTAR = Pattern.compile("\\*\\*");
  private static final Pattern STAR = Pattern.compile("\\*");
  private static final Pattern ESTAR = Pattern.compile("\\+\\??\\)\\Z");
  private static final Pattern DOLLAR = Pattern.compile("\\$");
  // Apart from stars and dollar signs, wildcards are plain-text full matches
  private static final Pattern PLAIN_TEXT_PREFIX_PATTERN = Pattern.compile("^[^*$]*");

  private final Pattern pattern;
  private final String plainTextPrefix;
  private final int ruleIndex;
  private final int count;
  private final ArrayList<Object> parts = new ArrayList<>(16); // kept for debugging
  private final String[] strings;
  private final int[] refs;

  public Wildcard(String pattern, String result, int ruleIndex) {
    if (pattern.equals("**")) {
      throw new IllegalArgumentException("'**' is not a valid pattern");
    }
    if (!checkIdentifierChars(pattern, "/*-")) {
      throw new IllegalArgumentException("Not a valid package pattern: " + pattern);
    }
    if (pattern.contains("***")) {
      throw new IllegalArgumentException("The sequence '***' is invalid in a package pattern");
    }

    String regex = pattern;
    regex = replaceAllLiteral(DSTAR, regex, "(.+?)");
    regex = replaceAllLiteral(STAR, regex, "([^/]+)");
    regex = replaceAllLiteral(ESTAR, regex, "*)");
    regex = replaceAllLiteral(DOLLAR, regex, "\\$");
    Matcher prefixMatcher = PLAIN_TEXT_PREFIX_PATTERN.matcher(pattern);
    // prefixMatcher will always match, but may match an empty string
    if (!prefixMatcher.find()) {
      throw new IllegalArgumentException(PLAIN_TEXT_PREFIX_PATTERN + " not found in " + pattern);
    }
    this.plainTextPrefix = prefixMatcher.group();
    this.ruleIndex = ruleIndex;
    this.pattern = Pattern.compile("\\A" + regex + "\\Z");
    this.count = this.pattern.matcher("foo").groupCount();

    // TODO: check for illegal characters
    char[] chars = result.toCharArray();
    int max = 0;
    for (int i = 0, mark = 0, state = 0, len = chars.length; i < len + 1; i++) {
      char ch = (i == len) ? '@' : chars[i];
      if (state == 0) {
        if (ch == '@') {
          parts.add(new String(chars, mark, i - mark));
          mark = i + 1;
          state = 1;
        }
      } else {
        switch (ch) {
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            break;
          default:
            if (i == mark) {
              throw new IllegalArgumentException("Backslash not followed by a digit");
            }
            int n = Integer.parseInt(new String(chars, mark, i - mark));
            if (n > max) {
              max = n;
            }
            parts.add(Integer.valueOf(n));
            mark = i--;
            state = 0;
        }
      }
    }
    int size = parts.size();
    strings = new String[size];
    refs = new int[size];
    Arrays.fill(refs, -1);
    for (int i = 0; i < size; i++) {
      Object v = parts.get(i);
      if (v instanceof String) {
        strings[i] = ((String) v).replace('.', '/');
      } else {
        refs[i] = ((Integer) v).intValue();
      }
    }
    if (count < max) {
      throw new IllegalArgumentException(
          "Result includes impossible placeholder \"@" + max + "\": " + result);
    }
    // System.err.println(this);
  }

  public String getPlainTextPrefix() {
    return plainTextPrefix;
  }

  public int getRuleIndex() {
    return ruleIndex;
  }

  public boolean matches(String value) {
    return getMatcher(value) != null;
  }

  public String replace(String value) {
    Matcher matcher = getMatcher(value);
    if (matcher != null) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < strings.length; i++) {
        sb.append((refs[i] >= 0) ? matcher.group(refs[i]) : strings[i]);
      }
      return sb.toString();
    }
    return null;
  }

  private Matcher getMatcher(String value) {
    Matcher matcher = pattern.matcher(value);
    if (matcher.matches() && checkIdentifierChars(value, "/-")) {
      return matcher;
    }
    return null;
  }

  private static boolean checkIdentifierChars(String expr, String extra) {
    // package-info violates the spec for Java Identifiers.
    // Nevertheless, expressions that end with this string are still legal.
    // See 7.4.1.1 of the Java language spec for discussion.
    if (expr.endsWith("package-info")) {
      expr = expr.substring(0, expr.length() - "package-info".length());
    }
    // Android-changed: also include module-info
    if (expr.endsWith("module-info")) {
      expr = expr.substring(0, expr.length() - "module-info".length());
    }
    for (int i = 0, len = expr.length(); i < len; i++) {
      char c = expr.charAt(i);
      if (extra.indexOf(c) >= 0) {
        continue;
      }
      if (!Character.isJavaIdentifierPart(c)) {
        return false;
      }
    }
    return true;
  }

  private static String replaceAllLiteral(Pattern pattern, String value, String replace) {
    replace = replace.replaceAll("([$\\\\])", "\\\\$0");
    return pattern.matcher(value).replaceAll(replace);
  }

  @Override
  public String toString() {
    return "Wildcard{pattern=" + pattern + ",parts=" + parts + "}";
  }
}