Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/ElementsTrimmer.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.SdkConstants.ANDROID_URI;
20
21 import com.android.SdkConstants;
22 import com.android.annotations.NonNull;
23 import com.android.xml.AndroidManifest;
24
25 import org.w3c.dom.Attr;
26
27 import java.util.Map;
28 import java.util.NavigableMap;
29 import java.util.TreeMap;
30
31 /**
32 * Trims the document from unwanted, repeated elements.
33 */
34 public class ElementsTrimmer {
35
36 /**
37 * Trims unwanted, duplicated elements from the merged document.
38 * <p>
39 * Current trimmed elements are :
40 * <ul>
41 * <li>uses-features with glEsVersion key
42 * <ul>
43 * <li>The highest 1.x version element will be kept regardless of 'required' flag value</li>
44 * <li>If the above element is present and has a 'false' required flag, there can be at most
45 * one element of a lesser version with 'required' attribute set to true.</li>
46 * <li>The highest 2.x or superior element will be kept regardless of 'required' flag value
47 * </li>
48 * <li>If the above element is present and has a 'false' required flag, there can be at
49 * most one element of a lesser version (but higher than 2.0) with a 'required' attribute
50 * set to true.</li>
51 * </ul>
52 * </li>
53 * </ul>
54 *
55 * @param xmlDocument the xml document to trim.
56 * @param mergingReport the report to log errors and actions.
57 */
58 public static void trim(
59 @NonNull XmlDocument xmlDocument,
60 @NonNull MergingReport.Builder mergingReport) {
61
62 // I sort the glEsVersion declaration by value.
63 NavigableMap<Integer, XmlElement> glEsVersionDeclarations = new TreeMap<Integer, XmlElement>();
64
65 for (XmlElement childElement : xmlDocument.getRootNode().getMergeableElements()) {
66 if (childElement.getType().equals(ManifestModel.NodeTypes.USES_FEATURE)) {
67 Integer value = getGlEsVersion(childElement);
68 if (value != null) {
69 glEsVersionDeclarations.put(value, childElement);
70 }
71 }
72 }
73
74 // now eliminate all unwanted declarations, revert the sorted map, so we get the
75 // higher elements first.
76 glEsVersionDeclarations = glEsVersionDeclarations.descendingMap();
77 boolean doneWithAboveTwoTrue = false;
78 boolean doneWithAboveTwoFalse = false;
79 boolean doneWithBelowTwoTrue = false;
80 boolean doneWithBelowTwoFalse = false;
81 for (Map.Entry<Integer, XmlElement> glEsVersionDeclaration :
82 glEsVersionDeclarations.entrySet()) {
83
84 boolean removeElement;
85
86 Attr requiredAttribute = glEsVersionDeclaration.getValue().getXml().getAttributeNodeNS(
87 ANDROID_URI, AndroidManifest.ATTRIBUTE_REQUIRED);
88
89 boolean isRequired = requiredAttribute == null ||
90 Boolean.parseBoolean(requiredAttribute.getValue());
91
92 if (glEsVersionDeclaration.getKey() < 0x20000) {
93 // version one.
94 removeElement = (doneWithBelowTwoFalse && doneWithBelowTwoTrue)
95 || (isRequired && doneWithBelowTwoTrue)
96 || (!isRequired && doneWithBelowTwoFalse);
97
98 if (!removeElement) {
99 doneWithBelowTwoFalse = true;
100 doneWithBelowTwoTrue = isRequired;
101 }
102 } else {
103 // version two or above.
104 removeElement = (doneWithAboveTwoFalse && doneWithAboveTwoTrue)
105 || (isRequired && doneWithAboveTwoTrue)
106 || (!isRequired && doneWithAboveTwoFalse);
107
108 if (!removeElement) {
109 doneWithAboveTwoFalse = true;
110 doneWithAboveTwoTrue = isRequired;
111 }
112 }
113 if (removeElement) {
114 // if the node only contains glEsVersion, then remove the entire node,
115 // if it also contains android:name, just remove the glEsVersion attribute
116 if (glEsVersionDeclaration.getValue().getXml().getAttributeNodeNS(ANDROID_URI,
117 SdkConstants.ATTR_NAME) != null) {
118 glEsVersionDeclaration.getValue().getXml().removeAttributeNS(ANDROID_URI,
119 AndroidManifest.ATTRIBUTE_GLESVERSION);
120 mergingReport.getActionRecorder().recordAttributeAction(
121 glEsVersionDeclaration.getValue().getAttribute(XmlNode.fromXmlName(
122 "android:" + AndroidManifest.ATTRIBUTE_GLESVERSION)).get(),
123 Actions.ActionType.REJECTED,
124 null /* attributeOperationType */);
125 } else {
126 xmlDocument.getRootNode().getXml().removeChild(
127 glEsVersionDeclaration.getValue().getXml());
128 mergingReport.getActionRecorder().recordNodeAction(
129 glEsVersionDeclaration.getValue(),
130 Actions.ActionType.REJECTED);
131
132 }
133 }
134
135 }
136
137 }
138
139 private static Integer getGlEsVersion(@NonNull XmlElement xmlElement) {
140 Attr glEsVersion = xmlElement.getXml()
141 .getAttributeNodeNS(ANDROID_URI, AndroidManifest.ATTRIBUTE_GLESVERSION);
142 if (glEsVersion == null) {
143 return null;
144 }
145 return getHexValue(glEsVersion);
146 }
147
148 private static Integer getHexValue(@NonNull Attr attribute) {
149 return Integer.decode(attribute.getValue());
150 }
151 }
152
153