Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/ToolsInstructionsCleaner.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.MergingReport.Result.ERROR;
20
21 import com.android.SdkConstants;
22 import com.android.annotations.NonNull;
23 import com.android.annotations.Nullable;
24 import com.android.annotations.concurrency.Immutable;
25 import com.android.utils.ILogger;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.ImmutableList;
28
29 import org.w3c.dom.Element;
30 import org.w3c.dom.NamedNodeMap;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * Removes all "tools:" statements from the resulting xml.
39 *
40 * All attributes belonging to the {@link com.android.SdkConstants#ANDROID_URI} namespace will be
41 * removed. If an element contained a "tools:node=\"remove\"" attribute, the element will be
42 * deleted.
43 */
44 @Immutable
45 public class ToolsInstructionsCleaner {
46
47 private static final String REMOVE_OPERATION_XML_MAME =
48 NodeOperationType.REMOVE.toCamelCaseName();
49 private static final String REMOVE_ALL_OPERATION_XML_MAME =
50 NodeOperationType.REMOVE_ALL.toCamelCaseName();
51
52 /**
53 * Cleans all attributes belonging to the {@link com.android.SdkConstants#TOOLS_URI} namespace.
54 *
55 * @param document the xml document to clean
56 * @param logger logger to use in case of errors and warnings.
57 * @return the cleaned document or null if an error occurred.
58 */
59 @Nullable
60 public static XmlDocument cleanToolsReferences(
61 @NonNull ManifestMerger2.MergeType mergeType,
62 @NonNull XmlDocument document,
63 @NonNull ILogger logger) {
64
65 Preconditions.checkNotNull(document);
66 Preconditions.checkNotNull(logger);
67 MergingReport.Result result = cleanToolsReferences(
68 mergeType,
69 document.getRootNode().getXml(),
70 logger);
71 return result == MergingReport.Result.SUCCESS
72 ? document.reparse()
73 : null;
74 }
75
76 @NonNull
77 private static MergingReport.Result cleanToolsReferences(
78 @NonNull ManifestMerger2.MergeType mergeType,
79 @NonNull Element element,
80 @NonNull ILogger logger) {
81
82 NamedNodeMap namedNodeMap = element.getAttributes();
83 if (namedNodeMap != null) {
84 // make a copy of the original list of attributes as we will remove some during this
85 // process.
86 List<Node> attributes = new ArrayList<Node>();
87 for (int i = 0; i < namedNodeMap.getLength(); i++) {
88 attributes.add(namedNodeMap.item(i));
89 }
90 for (Node attribute : attributes) {
91 if (SdkConstants.TOOLS_URI.equals(attribute.getNamespaceURI())) {
92 // we need to special case when the element contained tools:node="remove"
93 // since it also needs to be deleted unless it had a selector.
94 // if this is tools:node="removeAll", we always delete the element whether or
95 // not there is a tools:selector.
96 boolean hasSelector = namedNodeMap.getNamedItemNS(
97 SdkConstants.TOOLS_URI, "selector") != null;
98 if (attribute.getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)
99 && (attribute.getNodeValue().equals(REMOVE_ALL_OPERATION_XML_MAME)
100 || (attribute.getNodeValue().equals(REMOVE_OPERATION_XML_MAME))
101 && !hasSelector)) {
102
103 if (element.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
104 logger.error(null /* Throwable */,
105 String.format(
106 "tools:node=\"%1$s\" not allowed on top level %2$s element",
107 attribute.getNodeValue(),
108 XmlNode.unwrapName(element)));
109 return ERROR;
110 } else {
111 element.getParentNode().removeChild(element);
112 }
113 } else {
114 // anything else, we just clean the attribute unless we are merging for
115 // libraries.
116 if (mergeType != ManifestMerger2.MergeType.LIBRARY) {
117 element.removeAttributeNS(
118 attribute.getNamespaceURI(), attribute.getLocalName());
119 }
120 }
121 }
122 // this could also be the xmlns:tools declaration.
123 if (attribute.getNodeName().startsWith(SdkConstants.XMLNS_PREFIX)
124 && SdkConstants.TOOLS_URI.equals(attribute.getNodeValue())
125 && mergeType != ManifestMerger2.MergeType.LIBRARY) {
126 element.removeAttribute(attribute.getNodeName());
127 }
128 }
129 }
130 // make a copy of the element children since we will be removing some during
131 // this process, we don't want side effects.
132 NodeList childNodes = element.getChildNodes();
133 ImmutableList.Builder<Element> childElements = ImmutableList.builder();
134 for (int i = 0; i < childNodes.getLength(); i++) {
135 Node node = childNodes.item(i);
136 if (node.getNodeType() == Node.ELEMENT_NODE) {
137 childElements.add((Element) node);
138 }
139 }
140 for (Element childElement : childElements.build()) {
141 if (cleanToolsReferences(mergeType, childElement, logger) == ERROR) {
142 return ERROR;
143 }
144 }
145 return MergingReport.Result.SUCCESS;
146 }
147
148 }