Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/PostValidator.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 static com.android.manifmerger.Actions.ActionType;
20
21 import com.android.SdkConstants;
22 import com.android.annotations.NonNull;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25
26 import org.w3c.dom.Node;
27
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33 * Validator that runs post merging activities and verifies that all "tools:" instructions
34 * triggered an action by the merging tool.
35 * <p>
36 *
37 * This is primarily to catch situations like a user entered a tools:remove="foo" directory on one
38 * of its elements and that particular attribute was never removed during the merges possibly
39 * indicating an unforeseen change of configuration.
40 * <p>
41 *
42 * Most of the output from this validation should be warnings.
43 */
44 public class PostValidator {
45
46 /**
47 * Post validation of the merged document. This will essentially check that all merging
48 * instructions were applied at least once.
49 *
50 * @param xmlDocument merged document to check.
51 * @param mergingReport report for errors and warnings.
52 */
53 public static void validate(
54 @NonNull XmlDocument xmlDocument,
55 @NonNull MergingReport.Builder mergingReport) {
56
57 Preconditions.checkNotNull(xmlDocument);
58 Preconditions.checkNotNull(mergingReport);
59 enforceAndroidNamespaceDeclaration(xmlDocument);
60 reOrderElements(xmlDocument.getRootNode());
61 validate(xmlDocument.getRootNode(),
62 mergingReport.getActionRecorder().build(),
63 mergingReport);
64 }
65
66 /**
67 * Enforces {@link com.android.SdkConstants#ANDROID_URI} declaration in the top level element.
68 * It is possible that the original manifest file did not contain any attribute declaration,
69 * therefore not requiring a xmlns: declaration. Yet the implicit elements handling may have
70 * added attributes requiring the namespace declaration.
71 */
72 private static void enforceAndroidNamespaceDeclaration(@NonNull XmlDocument xmlDocument) {
73 XmlElement manifest = xmlDocument.getRootNode();
74 for (XmlAttribute xmlAttribute : manifest.getAttributes()) {
75 if (xmlAttribute.getXml().getName().startsWith(SdkConstants.XMLNS) &&
76 SdkConstants.ANDROID_URI.equals(xmlAttribute.getValue())) {
77 return;
78 }
79 }
80 // if we are here, we did not find the namespace declaration, add it.
81 manifest.getXml().setAttribute(SdkConstants.XMLNS + ":" + "android",
82 SdkConstants.ANDROID_URI);
83 }
84
85 /**
86 * Reorder child elements :
87 * <li>
88 * <ul> <application> is moved last in the list of children
89 * of the <manifest> element.
90 * <ul> uses-sdk is moved first in the list of children of the <manifest> element </ul>
91 * </li>
92 * @param xmlElement the root element of the manifest document.
93 */
94 private static void reOrderElements(@NonNull XmlElement xmlElement) {
95
96 reOrderApplication(xmlElement);
97 reOrderUsesSdk(xmlElement);
98 }
99
100 /**
101 * Reorder application element
102 *
103 * @param xmlElement the root element of the manifest document.
104 */
105 private static void reOrderApplication(@NonNull XmlElement xmlElement) {
106
107 // look up application element.
108 Optional<XmlElement> element = xmlElement
109 .getNodeByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null);
110 if (!element.isPresent()) {
111 return;
112 }
113 XmlElement applicationElement = element.get();
114
115 List<Node> comments = XmlElement.getLeadingComments(applicationElement.getXml());
116
117 // move the application's comments if any.
118 for (Node comment : comments) {
119 xmlElement.getXml().removeChild(comment);
120 xmlElement.getXml().appendChild(comment);
121 }
122 // remove the application element and add it back, it will be automatically placed last.
123 xmlElement.getXml().removeChild(applicationElement.getXml());
124 xmlElement.getXml().appendChild(applicationElement.getXml());
125 }
126
127 /**
128 * Reorder uses-sdk element
129 *
130 * @param xmlElement the root element of the manifest document.
131 */
132 private static void reOrderUsesSdk(@NonNull XmlElement xmlElement) {
133
134 // look up application element.
135 Optional<XmlElement> element = xmlElement
136 .getNodeByTypeAndKey(ManifestModel.NodeTypes.USES_SDK, null);
137 if (!element.isPresent()) {
138 return;
139 }
140
141 XmlElement usesSdk = element.get();
142 Node firstChild = xmlElement.getXml().getFirstChild();
143 // already the first element ?
144 if (firstChild == usesSdk.getXml()) {
145 return;
146 }
147
148 List<Node> comments = XmlElement.getLeadingComments(usesSdk.getXml());
149
150 // move the application's comments if any.
151 for (Node comment : comments) {
152 xmlElement.getXml().removeChild(comment);
153 xmlElement.getXml().insertBefore(comment, firstChild);
154 }
155 // remove the application element and add it back, it will be automatically placed last.
156 xmlElement.getXml().removeChild(usesSdk.getXml());
157 xmlElement.getXml().insertBefore(usesSdk.getXml(), firstChild);
158 }
159
160 /**
161 * Validate an xml element and recursively its children elements, ensuring that all merging
162 * instructions were applied.
163 *
164 * @param xmlElement xml element to validate.
165 * @param actions the actions recorded during the merging activities.
166 * @param mergingReport report for errors and warnings.
167 * instructions were applied once or {@link MergingReport.Result#WARNING} otherwise.
168 */
169 private static void validate(
170 @NonNull XmlElement xmlElement,
171 @NonNull Actions actions,
172 @NonNull MergingReport.Builder mergingReport) {
173
174 NodeOperationType operationType = xmlElement.getOperationType();
175 switch (operationType) {
176 case REPLACE:
177 // we should find at least one rejected twin.
178 if (!isNodeOperationPresent(xmlElement, actions, ActionType.REJECTED)) {
179 xmlElement.addMessage(mergingReport, MergingReport.Record.Severity.WARNING,
180 String.format(
181 "%1$s was tagged at %2$s:%3$d to replace another declaration "
182 + "but no other declaration present",
183 xmlElement.getId(),
184 xmlElement.getDocument().getSourceFile().print(true),
185 xmlElement.getPosition().getStartLine() + 1
186 ));
187 }
188 break;
189 case REMOVE:
190 case REMOVE_ALL:
191 // we should find at least one rejected twin.
192 if (!isNodeOperationPresent(xmlElement, actions, ActionType.REJECTED)) {
193 xmlElement.addMessage(mergingReport, MergingReport.Record.Severity.WARNING,
194 String.format(
195 "%1$s was tagged at %2$s:%3$d to remove other declarations "
196 + "but no other declaration present",
197 xmlElement.getId(),
198 xmlElement.getDocument().getSourceFile().print(true),
199 xmlElement.getPosition().getStartLine() + 1
200 ));
201 }
202 break;
203 }
204 validateAttributes(xmlElement, actions, mergingReport);
205 validateAndroidAttributes(xmlElement, mergingReport);
206 for (XmlElement child : xmlElement.getMergeableElements()) {
207 validate(child, actions, mergingReport);
208 }
209 }
210
211
212 /**
213 * Verifies that all merging attributes on a passed xml element were applied.
214 */
215 private static void validateAttributes(
216 @NonNull XmlElement xmlElement,
217 @NonNull Actions actions,
218 @NonNull MergingReport.Builder mergingReport) {
219
220 @NonNull Collection<Map.Entry<XmlNode.NodeName, AttributeOperationType>> attributeOperations
221 = xmlElement.getAttributeOperations();
222 for (Map.Entry<XmlNode.NodeName, AttributeOperationType> attributeOperation :
223 attributeOperations) {
224 switch (attributeOperation.getValue()) {
225 case REMOVE:
226 if (!isAttributeOperationPresent(
227 xmlElement, attributeOperation, actions, ActionType.REJECTED)) {
228 xmlElement.addMessage(mergingReport, MergingReport.Record.Severity.WARNING,
229 String.format(
230 "%1$s@%2$s was tagged at %3$s:%4$d to remove other"
231 + " declarations but no other declaration present",
232 xmlElement.getId(),
233 attributeOperation.getKey(),
234 xmlElement.getDocument().getSourceFile().print(true),
235 xmlElement.getPosition().getStartLine() + 1
236 ));
237 }
238 break;
239 case REPLACE:
240 if (!isAttributeOperationPresent(
241 xmlElement, attributeOperation, actions, ActionType.REJECTED)) {
242 xmlElement.addMessage(mergingReport, MergingReport.Record.Severity.WARNING,
243 String.format(
244 "%1$s@%2$s was tagged at %3$s:%4$d to replace other"
245 + " declarations but no other declaration present",
246 xmlElement.getId(),
247 attributeOperation.getKey(),
248 xmlElement.getDocument().getSourceFile().print(true),
249 xmlElement.getPosition().getStartLine() + 1
250 ));
251 }
252 break;
253 }
254 }
255
256 }
257
258 /**
259 * Check in our list of applied actions that a particular
260 * {@link com.android.manifmerger.Actions.ActionType} action was recorded on the passed element.
261 * @return true if it was applied, false otherwise.
262 */
263 private static boolean isNodeOperationPresent(@NonNull XmlElement xmlElement,
264 @NonNull Actions actions,
265 ActionType action) {
266
267 for (Actions.NodeRecord nodeRecord : actions.getNodeRecords(xmlElement.getId())) {
268 if (nodeRecord.getActionType() == action) {
269 return true;
270 }
271 }
272 return false;
273 }
274
275 /**
276 * Check in our list of attribute actions that a particular
277 * {@link com.android.manifmerger.Actions.ActionType} action was recorded on the passed element.
278 * @return true if it was applied, false otherwise.
279 */
280 private static boolean isAttributeOperationPresent(@NonNull XmlElement xmlElement,
281 @NonNull Map.Entry<XmlNode.NodeName, AttributeOperationType> attributeOperation,
282 @NonNull Actions actions,
283 ActionType action) {
284
285 for (Actions.AttributeRecord attributeRecord : actions.getAttributeRecords(
286 xmlElement.getId(), attributeOperation.getKey())) {
287 if (attributeRecord.getActionType() == action) {
288 return true;
289 }
290 }
291 return false;
292 }
293
294 /**
295 * Validates all {@link com.android.manifmerger.XmlElement} attributes belonging to the
296 * {@link com.android.SdkConstants#ANDROID_URI} namespace.
297 *
298 * @param xmlElement xml element to check the attributes from.
299 * @param mergingReport report for errors and warnings.
300 */
301 private static void validateAndroidAttributes(@NonNull XmlElement xmlElement,
302 @NonNull MergingReport.Builder mergingReport) {
303
304 for (XmlAttribute xmlAttribute : xmlElement.getAttributes()) {
305 if (xmlAttribute.getModel() != null) {
306 AttributeModel.Validator onWriteValidator = xmlAttribute.getModel()
307 .getOnWriteValidator();
308 if (onWriteValidator != null) {
309 onWriteValidator.validates(
310 mergingReport, xmlAttribute, xmlAttribute.getValue());
311 }
312 }
313 }
314 }
315 }