| 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.Record.Severity.ERROR; |
| 20 |
import static com.android.manifmerger.MergingReport.Record.Severity.WARNING; |
| 21 |
import static com.android.manifmerger.XmlNode.NodeKey; |
| 22 |
|
| 23 |
import com.android.SdkConstants; |
| 24 |
import com.android.annotations.NonNull; |
| 25 |
import com.android.xml.AndroidManifest; |
| 26 |
import com.google.common.base.Joiner; |
| 27 |
import com.google.common.base.Optional; |
| 28 |
import com.google.common.base.Preconditions; |
| 29 |
import com.google.common.base.Strings; |
| 30 |
import com.google.common.collect.ImmutableList; |
| 31 |
|
| 32 |
import org.w3c.dom.Attr; |
| 33 |
import org.w3c.dom.NamedNodeMap; |
| 34 |
import org.w3c.dom.Node; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.HashMap; |
| 38 |
import java.util.List; |
| 39 |
import java.util.Map; |
| 40 |
|
| 41 |
/** |
| 42 |
* Validates a loaded {@link XmlDocument} and check for potential inconsistencies in the model due |
| 43 |
* to user error or omission. |
| 44 |
* |
| 45 |
* This is implemented as a separate class so it can be invoked by tools independently from the |
| 46 |
* merging process. |
| 47 |
* |
| 48 |
* This validator will check the state of the loaded xml document before any merging activity is |
| 49 |
* attempted. It verifies things like a "tools:replace="foo" attribute has a "android:foo" |
| 50 |
* attribute also declared on the same element (since we want to replace its value). |
| 51 |
*/ |
| 52 |
public class PreValidator { |
| 53 |
|
| 54 |
private PreValidator(){ |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Validates a loaded {@link com.android.manifmerger.XmlDocument} and return a status of the |
| 59 |
* merging model. |
| 60 |
* |
| 61 |
* Will return one the following status : |
| 62 |
* <ul> |
| 63 |
* <li>{@link com.android.manifmerger.MergingReport.Result#SUCCESS} : the merging model is |
| 64 |
* correct, merging should be attempted</li> |
| 65 |
* <li>{@link com.android.manifmerger.MergingReport.Result#WARNING} : the merging model |
| 66 |
* contains non fatal error, user should be notified, merging can be attempted</li> |
| 67 |
* <li>{@link com.android.manifmerger.MergingReport.Result#ERROR} : the merging model |
| 68 |
* contains errors, user must be notified, merging should not be attempted</li> |
| 69 |
* </ul> |
| 70 |
* |
| 71 |
* A successful validation does not mean that the merging will be successful, it only means |
| 72 |
* that the {@link com.android.SdkConstants#TOOLS_URI} instructions are correct and consistent. |
| 73 |
* |
| 74 |
* @param mergingReport report to log warnings and errors. |
| 75 |
* @param xmlDocument the loaded xml part. |
| 76 |
* @return one the {@link com.android.manifmerger.MergingReport.Result} value. |
| 77 |
*/ |
| 78 |
@NonNull |
| 79 |
public static MergingReport.Result validate( |
| 80 |
@NonNull MergingReport.Builder mergingReport, |
| 81 |
@NonNull XmlDocument xmlDocument) { |
| 82 |
|
| 83 |
validateManifestAttribute( |
| 84 |
mergingReport, xmlDocument.getRootNode(), xmlDocument.getFileType()); |
| 85 |
return validate(mergingReport, xmlDocument.getRootNode()); |
| 86 |
} |
| 87 |
|
| 88 |
@NonNull |
| 89 |
private static MergingReport.Result validate(@NonNull MergingReport.Builder mergingReport, |
| 90 |
@NonNull XmlElement xmlElement) { |
| 91 |
|
| 92 |
validateAttributeInstructions(mergingReport, xmlElement); |
| 93 |
|
| 94 |
validateAndroidAttributes(mergingReport, xmlElement); |
| 95 |
|
| 96 |
checkSelectorPresence(mergingReport, xmlElement); |
| 97 |
|
| 98 |
// create a temporary hash map of children indexed by key to ensure key uniqueness. |
| 99 |
Map<NodeKey, XmlElement> childrenKeys = new HashMap<NodeKey, XmlElement>(); |
| 100 |
for (XmlElement childElement : xmlElement.getMergeableElements()) { |
| 101 |
|
| 102 |
// if this element is tagged with 'tools:node=removeAll', ensure it has no other |
| 103 |
// attributes. |
| 104 |
if (childElement.getOperationType() == NodeOperationType.REMOVE_ALL) { |
| 105 |
validateRemoveAllOperation(mergingReport, childElement); |
| 106 |
} else { |
| 107 |
if (checkKeyPresence(mergingReport, childElement)) { |
| 108 |
XmlElement twin = childrenKeys.get(childElement.getId()); |
| 109 |
if (twin != null && !childElement.getType().areMultipleDeclarationAllowed()) { |
| 110 |
// we have 2 elements with the same identity, if they are equals, |
| 111 |
// issue a warning, if not, issue an error. |
| 112 |
String message = String.format( |
| 113 |
"Element %1$s at %2$s duplicated with element declared at %3$s", |
| 114 |
childElement.getId(), |
| 115 |
childElement.printPosition(), |
| 116 |
childrenKeys.get(childElement.getId()).printPosition()); |
| 117 |
if (twin.compareTo(childElement).isPresent()) { |
| 118 |
childElement.addMessage(mergingReport, ERROR, message); |
| 119 |
} else { |
| 120 |
childElement.addMessage(mergingReport, WARNING, message); |
| 121 |
} |
| 122 |
} |
| 123 |
childrenKeys.put(childElement.getId(), childElement); |
| 124 |
} |
| 125 |
validate(mergingReport, childElement); |
| 126 |
} |
| 127 |
} |
| 128 |
return mergingReport.hasErrors() |
| 129 |
? MergingReport.Result.ERROR : MergingReport.Result.SUCCESS; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Validate an xml declaration with 'tools:node="removeAll" annotation. There should not |
| 134 |
* be any other attribute declaration on this element. |
| 135 |
*/ |
| 136 |
private static void validateRemoveAllOperation(@NonNull MergingReport.Builder mergingReport, |
| 137 |
@NonNull XmlElement element) { |
| 138 |
|
| 139 |
NamedNodeMap attributes = element.getXml().getAttributes(); |
| 140 |
if (attributes.getLength() > 1) { |
| 141 |
List<String> extraAttributeNames = new ArrayList<String>(); |
| 142 |
for (int i = 0; i < attributes.getLength(); i++) { |
| 143 |
Node item = attributes.item(i); |
| 144 |
if (!(SdkConstants.TOOLS_URI.equals(item.getNamespaceURI()) && |
| 145 |
NodeOperationType.NODE_LOCAL_NAME.equals(item.getLocalName()))) { |
| 146 |
extraAttributeNames.add(item.getNodeName()); |
| 147 |
} |
| 148 |
} |
| 149 |
String message = String.format( |
| 150 |
"Element %1$s at %2$s annotated with 'tools:node=\"removeAll\"' cannot " |
| 151 |
+ "have other attributes : %3$s", |
| 152 |
element.getId(), |
| 153 |
element.printPosition(), |
| 154 |
Joiner.on(',').join(extraAttributeNames) |
| 155 |
); |
| 156 |
element.addMessage(mergingReport, ERROR, message); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
private static void checkSelectorPresence(@NonNull MergingReport.Builder mergingReport, |
| 161 |
@NonNull XmlElement element) { |
| 162 |
|
| 163 |
Attr selectorAttribute = |
| 164 |
element.getXml().getAttributeNodeNS(SdkConstants.TOOLS_URI, Selector.SELECTOR_LOCAL_NAME); |
| 165 |
if (selectorAttribute!=null && !element.supportsSelector()) { |
| 166 |
String message = String.format( |
| 167 |
"Unsupported tools:selector=\"%1$s\" found on node %2$s at %3$s", |
| 168 |
selectorAttribute.getValue(), |
| 169 |
element.getId(), |
| 170 |
element.printPosition()); |
| 171 |
element.addMessage(mergingReport, ERROR, message); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
private static void validateManifestAttribute( |
| 176 |
@NonNull MergingReport.Builder mergingReport, @NonNull XmlElement manifest, XmlDocument.Type fileType) { |
| 177 |
Attr attributeNode = manifest.getXml().getAttributeNode(AndroidManifest.ATTRIBUTE_PACKAGE); |
| 178 |
// it's ok for an overlay to not have a package name, it's not ok for a main manifest |
| 179 |
// and it's a warning for a library. |
| 180 |
if (attributeNode == null && fileType != XmlDocument.Type.OVERLAY) { |
| 181 |
manifest.addMessage(mergingReport, |
| 182 |
fileType == XmlDocument.Type.MAIN ? ERROR : WARNING, |
| 183 |
String.format( |
| 184 |
"Missing 'package' declaration in manifest at %1$s", |
| 185 |
manifest.printPosition())); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Checks that an element which is supposed to have a key does have one. |
| 191 |
* @param mergingReport report to log warnings and errors. |
| 192 |
* @param xmlElement xml element to check for key presence. |
| 193 |
* @return true if the element has a valid key or false it does not need one or it is invalid. |
| 194 |
*/ |
| 195 |
private static boolean checkKeyPresence( |
| 196 |
@NonNull MergingReport.Builder mergingReport, |
| 197 |
@NonNull XmlElement xmlElement) { |
| 198 |
ManifestModel.NodeKeyResolver nodeKeyResolver = xmlElement.getType().getNodeKeyResolver(); |
| 199 |
ImmutableList<String> keyAttributesNames = nodeKeyResolver.getKeyAttributesNames(); |
| 200 |
if (keyAttributesNames.isEmpty()) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
if (Strings.isNullOrEmpty(xmlElement.getKey())) { |
| 204 |
// we should have a key but we don't. |
| 205 |
String message = keyAttributesNames.size() > 1 |
| 206 |
? String.format( |
| 207 |
"Missing one of the key attributes '%1$s' on element %2$s at %3$s", |
| 208 |
Joiner.on(',').join(keyAttributesNames), |
| 209 |
xmlElement.getId(), |
| 210 |
xmlElement.printPosition()) |
| 211 |
: String.format( |
| 212 |
"Missing '%1$s' key attribute on element %2$s at %3$s", |
| 213 |
keyAttributesNames.get(0), |
| 214 |
xmlElement.getId(), |
| 215 |
xmlElement.printPosition()); |
| 216 |
xmlElement.addMessage(mergingReport, ERROR, message); |
| 217 |
return false; |
| 218 |
} |
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Validate attributes part of the {@link com.android.SdkConstants#ANDROID_URI} |
| 224 |
* @param mergingReport report to log warnings and errors. |
| 225 |
* @param xmlElement xml element to check its attributes. |
| 226 |
*/ |
| 227 |
private static void validateAndroidAttributes(@NonNull MergingReport.Builder mergingReport, |
| 228 |
@NonNull XmlElement xmlElement) { |
| 229 |
for (XmlAttribute xmlAttribute : xmlElement.getAttributes()) { |
| 230 |
AttributeModel model = xmlAttribute.getModel(); |
| 231 |
if (model != null && model.getOnReadValidator() != null) { |
| 232 |
model.getOnReadValidator().validates( |
| 233 |
mergingReport, xmlAttribute, xmlAttribute.getValue()); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Validates attributes part of the {@link com.android.SdkConstants#TOOLS_URI} |
| 240 |
* @param mergingReport report to log warnings and errors. |
| 241 |
* @param xmlElement xml element to check its attributes. |
| 242 |
*/ |
| 243 |
private static void validateAttributeInstructions( |
| 244 |
@NonNull MergingReport.Builder mergingReport, |
| 245 |
@NonNull XmlElement xmlElement) { |
| 246 |
|
| 247 |
for (Map.Entry<XmlNode.NodeName, AttributeOperationType> attributeOperationTypeEntry : |
| 248 |
xmlElement.getAttributeOperations()) { |
| 249 |
|
| 250 |
Optional<XmlAttribute> attribute = xmlElement |
| 251 |
.getAttribute(attributeOperationTypeEntry.getKey()); |
| 252 |
switch(attributeOperationTypeEntry.getValue()) { |
| 253 |
case STRICT: |
| 254 |
break; |
| 255 |
case REMOVE: |
| 256 |
// check we are not provided a new value. |
| 257 |
if (attribute.isPresent()) { |
| 258 |
// Add one to startLine so the first line is displayed as 1. |
| 259 |
xmlElement.addMessage(mergingReport, ERROR, String.format( |
| 260 |
"tools:remove specified at line:%d for attribute %s, but " |
| 261 |
+ "attribute also declared at line:%d, " |
| 262 |
+ "do you want to use tools:replace instead ?", |
| 263 |
xmlElement.getPosition().getStartLine() + 1, |
| 264 |
attributeOperationTypeEntry.getKey(), |
| 265 |
attribute.get().getPosition().getStartLine() + 1 |
| 266 |
)); |
| 267 |
} |
| 268 |
break; |
| 269 |
case REPLACE: |
| 270 |
// check we are provided a new value |
| 271 |
if (!attribute.isPresent()) { |
| 272 |
// Add one to startLine so the first line is displayed as 1. |
| 273 |
xmlElement.addMessage(mergingReport, ERROR, String.format( |
| 274 |
"tools:replace specified at line:%d for attribute %s, but " |
| 275 |
+ "no new value specified", |
| 276 |
xmlElement.getPosition().getStartLine() + 1, |
| 277 |
attributeOperationTypeEntry.getKey() |
| 278 |
)); |
| 279 |
} |
| 280 |
break; |
| 281 |
default: |
| 282 |
throw new IllegalStateException("Unhandled AttributeOperationType " + |
| 283 |
attributeOperationTypeEntry.getValue()); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |