Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/PlaceholderHandler.java

raw
1 /*
2 * Copyright (C) 2014 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 com.android.manifmerger;
18
19 import com.android.SdkConstants;
20 import com.android.annotations.NonNull;
21 import com.android.annotations.Nullable;
22 import com.android.ide.common.blame.SourcePosition;
23
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 /**
28 * Replaces all placeholders of the form ${name} with a tool invocation provided value
29 */
30 public class PlaceholderHandler {
31
32 // interesting placeholders names that are documented to be automatically provided.
33 public static final String INSTRUMENTATION_RUNNER = "instrumentationRunner";
34 public static final String PACKAGE_NAME = "packageName";
35 public static final String APPLICATION_ID = "applicationId";
36
37 // regular expression to recognize placeholders like ${name}, potentially surrounded by a
38 // prefix and suffix string. this will split in 3 groups, the prefix, the placeholder name, and
39 // the suffix.
40 // If this pattern is modified, studio ManifestPlaceholderResolver.PLACEHOLDER_PATTERN must be also change
41 static final Pattern PATTERN = Pattern.compile("([^\\$]*)\\$\\{([^\\}]*)\\}(.*)");
42
43 /**
44 * Interface to provide a value for a placeholder key.
45 * @param <T> the key type
46 */
47 public interface KeyBasedValueResolver<T> {
48
49 /**
50 * Returns a placeholder value for the placeholder key or null if none exists.
51 */
52 @Nullable
53 String getValue(@NonNull T key);
54 }
55
56 /**
57 * Returns true if the passed string is a placeholder value, false otherwise.
58 */
59 public static boolean isPlaceHolder(@NonNull String string) {
60 return PATTERN.matcher(string).matches();
61 }
62
63 /**
64 * Visits a document's entire tree and check each attribute for a placeholder existence.
65 * If one is found, delegate to the provided {@link KeyBasedValueResolver} to provide a value
66 * for the placeholder.
67 * <p>
68 * If no value is provided, an error will be generated.
69 *
70 * @param xmlDocument the xml document to visit
71 * @param valueProvider the placeholder value provider.
72 * @param mergingReportBuilder to report errors and log actions.
73 */
74 public static void visit(
75 @NonNull ManifestMerger2.MergeType mergeType,
76 @NonNull XmlDocument xmlDocument,
77 @NonNull KeyBasedValueResolver<String> valueProvider,
78 @NonNull MergingReport.Builder mergingReportBuilder) {
79
80 visit(mergeType, xmlDocument.getRootNode(), valueProvider, mergingReportBuilder);
81 }
82
83 private static void visit(
84 @NonNull ManifestMerger2.MergeType mergeType,
85 @NonNull XmlElement xmlElement,
86 @NonNull KeyBasedValueResolver<String> valueProvider,
87 @NonNull MergingReport.Builder mergingReportBuilder) {
88
89 for (XmlAttribute xmlAttribute : xmlElement.getAttributes()) {
90
91 StringBuilder resultString = new StringBuilder();
92 String inputString = xmlAttribute.getValue();
93 Matcher matcher = PATTERN.matcher(inputString);
94 if (matcher.matches()) {
95 while (matcher.matches()) {
96 String placeholderValue = valueProvider.getValue(matcher.group(2));
97 // whatever precedes the placeholder key is added back to the string.
98 resultString.append(matcher.group(1));
99 if (placeholderValue == null) {
100 // if this is a library, ignore the failure
101 MergingReport.Record.Severity severity =
102 mergeType == ManifestMerger2.MergeType.LIBRARY
103 ? MergingReport.Record.Severity.INFO
104 : MergingReport.Record.Severity.ERROR;
105
106 xmlAttribute.addMessage(mergingReportBuilder, severity,
107 String.format(
108 "Attribute %1$s at %2$s requires a placeholder substitution"
109 + " but no value for <%3$s> is provided.",
110 xmlAttribute.getId(),
111 xmlAttribute.printPosition(),
112 matcher.group(2)
113 ));
114 // we add back the placeholder key, since this is not an error for libraries
115 resultString.append(SdkConstants.MANIFEST_PLACEHOLDER_PREFIX);
116 resultString.append(matcher.group(2));
117 resultString.append(SdkConstants.MANIFEST_PLACEHOLDER_SUFFIX);
118 } else {
119 // record the attribute set
120 mergingReportBuilder.getActionRecorder().recordAttributeAction(
121 xmlAttribute,
122 SourcePosition.UNKNOWN,
123 Actions.ActionType.INJECTED,
124 null /* attributeOperationType */);
125
126 // substitute the placeholder key with its value.
127 resultString.append(placeholderValue);
128 }
129 // the new input string is the tail of the previous match, as it may contain
130 // more placeholders to substitute.
131 inputString = matcher.group(3);
132 // reset the pattern matching with that new string to test for more placeholders
133 matcher = PATTERN.matcher(inputString);
134 }
135 // append the last remainder (without placeholders) in the result string.
136 resultString.append(inputString);
137 xmlAttribute.getXml().setValue(resultString.toString());
138 }
139 }
140 for (XmlElement childElement : xmlElement.getMergeableElements()) {
141 visit(mergeType, childElement, valueProvider, mergingReportBuilder);
142 }
143 }
144 }