| 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.SourceFile; |
| 23 |
import com.android.ide.common.blame.SourceFilePosition; |
| 24 |
import com.android.ide.common.blame.SourcePosition; |
| 25 |
import com.google.common.base.Joiner; |
| 26 |
import com.google.common.base.Optional; |
| 27 |
import com.google.common.base.Preconditions; |
| 28 |
import com.google.common.base.Splitter; |
| 29 |
import com.google.common.collect.ImmutableSet; |
| 30 |
|
| 31 |
import org.w3c.dom.Attr; |
| 32 |
|
| 33 |
/** |
| 34 |
* Defines an XML attribute inside a {@link XmlElement}. |
| 35 |
* |
| 36 |
* Basically a facade object on {@link Attr} objects with some added features like automatic |
| 37 |
* namespace handling, manifest merger friendly identifiers and smart replacement of shortened |
| 38 |
* full qualified class names using manifest node's package setting from the the owning Android's |
| 39 |
* document. |
| 40 |
*/ |
| 41 |
public class XmlAttribute extends XmlNode { |
| 42 |
|
| 43 |
@NonNull |
| 44 |
private final XmlElement mOwnerElement; |
| 45 |
@NonNull |
| 46 |
private final Attr mXml; |
| 47 |
@Nullable |
| 48 |
private final AttributeModel mAttributeModel; |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates a new facade object to a {@link Attr} xml attribute in a |
| 52 |
* {@link XmlElement}. |
| 53 |
* |
| 54 |
* @param ownerElement the xml node object owning this attribute. |
| 55 |
* @param xml the xml definition of the attribute. |
| 56 |
*/ |
| 57 |
public XmlAttribute( |
| 58 |
@NonNull XmlElement ownerElement, |
| 59 |
@NonNull Attr xml, |
| 60 |
@Nullable AttributeModel attributeModel) { |
| 61 |
this.mOwnerElement = Preconditions.checkNotNull(ownerElement); |
| 62 |
this.mXml = Preconditions.checkNotNull(xml); |
| 63 |
this.mAttributeModel = attributeModel; |
| 64 |
if (mAttributeModel != null && mAttributeModel.isPackageDependent()) { |
| 65 |
String value = mXml.getValue(); |
| 66 |
if (value == null || value.isEmpty()) return; |
| 67 |
// placeholders are never expanded. |
| 68 |
if (!PlaceholderHandler.isPlaceHolder(value)) { |
| 69 |
String pkg = mOwnerElement.getDocument().getPackageNameForAttributeExpansion(); |
| 70 |
// We know it's a shortened FQCN if it starts with a dot |
| 71 |
// or does not contain any dot. |
| 72 |
if (value.indexOf('.') == -1 || value.charAt(0) == '.') { |
| 73 |
if (value.charAt(0) == '.') { |
| 74 |
value = pkg + value; |
| 75 |
} else { |
| 76 |
value = pkg + '.' + value; |
| 77 |
} |
| 78 |
mXml.setValue(value); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the attribute's name, providing isolation from details like namespaces handling. |
| 86 |
*/ |
| 87 |
@NonNull |
| 88 |
@Override |
| 89 |
public NodeName getName() { |
| 90 |
return XmlNode.unwrapName(mXml); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the attribute's value |
| 95 |
*/ |
| 96 |
@NonNull |
| 97 |
public String getValue() { |
| 98 |
return mXml.getValue(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns a display friendly identification string that can be used in machine and user |
| 103 |
* readable messages. |
| 104 |
*/ |
| 105 |
@NonNull |
| 106 |
@Override |
| 107 |
public NodeKey getId() { |
| 108 |
// (Id of the parent element)@(my name) |
| 109 |
String myName = mXml.getNamespaceURI() == null ? mXml.getName() : mXml.getLocalName(); |
| 110 |
return new NodeKey(mOwnerElement.getId() + "@" + myName); |
| 111 |
} |
| 112 |
|
| 113 |
@NonNull |
| 114 |
@Override |
| 115 |
public SourcePosition getPosition() { |
| 116 |
try { |
| 117 |
return XmlDocument.getNodePosition(this); |
| 118 |
} catch(Exception e) { |
| 119 |
return SourcePosition.UNKNOWN; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@NonNull |
| 124 |
@Override |
| 125 |
public Attr getXml() { |
| 126 |
return mXml; |
| 127 |
} |
| 128 |
|
| 129 |
@Nullable |
| 130 |
public AttributeModel getModel() { |
| 131 |
return mAttributeModel; |
| 132 |
} |
| 133 |
|
| 134 |
@NonNull |
| 135 |
XmlElement getOwnerElement() { |
| 136 |
return mOwnerElement; |
| 137 |
} |
| 138 |
|
| 139 |
void mergeInHigherPriorityElement(@NonNull XmlElement higherPriorityElement, |
| 140 |
@NonNull MergingReport.Builder mergingReport) { |
| 141 |
|
| 142 |
// does the higher priority has the same attribute as myself ? |
| 143 |
Optional<XmlAttribute> higherPriorityAttributeOptional = |
| 144 |
higherPriorityElement.getAttribute(getName()); |
| 145 |
|
| 146 |
@NonNull AttributeOperationType attributeOperationType = |
| 147 |
higherPriorityElement.getAttributeOperationType(getName()); |
| 148 |
|
| 149 |
if (higherPriorityAttributeOptional.isPresent()) { |
| 150 |
|
| 151 |
XmlAttribute higherPriorityAttribute = higherPriorityAttributeOptional.get(); |
| 152 |
handleBothAttributePresent( |
| 153 |
mergingReport, higherPriorityAttribute, attributeOperationType); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
// it does not exist, verify if we are supposed to remove it. |
| 158 |
if (attributeOperationType == AttributeOperationType.REMOVE) { |
| 159 |
// record the fact the attribute was actively removed. |
| 160 |
mergingReport.getActionRecorder().recordAttributeAction( |
| 161 |
this, |
| 162 |
Actions.ActionType.REJECTED, |
| 163 |
AttributeOperationType.REMOVE); |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
// the node is not defined in the higher priority element, it's defined in this lower |
| 168 |
// priority element, we need to merge this lower priority attribute value with a potential |
| 169 |
// higher priority default value (implicitly set on the higher priority element). |
| 170 |
String mergedValue = mergeThisAndDefaultValue(mergingReport, higherPriorityElement); |
| 171 |
if (mergedValue == null) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
// ok merge it in the higher priority element. |
| 176 |
getName().addToNode(higherPriorityElement.getXml(), mergedValue); |
| 177 |
|
| 178 |
// and record the action. |
| 179 |
mergingReport.getActionRecorder().recordAttributeAction( |
| 180 |
this, |
| 181 |
Actions.ActionType.ADDED, |
| 182 |
getOwnerElement().getAttributeOperationType(getName())); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Handles merging of two attributes value explicitly declared in xml elements. |
| 187 |
* |
| 188 |
* @param report report to log errors and actions. |
| 189 |
* @param higherPriority higher priority attribute we should merge this attribute with. |
| 190 |
* @param operationType user operation type optionally requested by the user. |
| 191 |
*/ |
| 192 |
private void handleBothAttributePresent( |
| 193 |
@NonNull MergingReport.Builder report, |
| 194 |
@NonNull XmlAttribute higherPriority, |
| 195 |
AttributeOperationType operationType) { |
| 196 |
|
| 197 |
// handles tools: attribute separately. |
| 198 |
|
| 199 |
if (getXml().getNamespaceURI() != null |
| 200 |
&& getXml().getNamespaceURI().equals(SdkConstants.TOOLS_URI)) { |
| 201 |
handleBothToolsAttributePresent(higherPriority); |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
// the attribute is present on both elements, there are 2 possibilities : |
| 206 |
// 1. tools:replace was specified, replace the value. |
| 207 |
// 2. nothing was specified, the values should be equal or this is an error. |
| 208 |
if (operationType == AttributeOperationType.REPLACE) { |
| 209 |
// record the fact the lower priority attribute was rejected. |
| 210 |
report.getActionRecorder().recordAttributeAction( |
| 211 |
this, |
| 212 |
Actions.ActionType.REJECTED, |
| 213 |
AttributeOperationType.REPLACE); |
| 214 |
return; |
| 215 |
} |
| 216 |
// if the values are the same, then it's fine, otherwise flag the error. |
| 217 |
if (mAttributeModel != null) { |
| 218 |
String mergedValue = mAttributeModel.getMergingPolicy() |
| 219 |
.merge(higherPriority.getValue(), getValue()); |
| 220 |
if (mergedValue != null) { |
| 221 |
higherPriority.mXml.setValue(mergedValue); |
| 222 |
} else { |
| 223 |
addConflictingValueMessage(report, higherPriority); |
| 224 |
} |
| 225 |
return; |
| 226 |
} |
| 227 |
// no merging policy, for now revert on checking manually for equality. |
| 228 |
if (!getValue().equals(higherPriority.getValue())) { |
| 229 |
addConflictingValueMessage(report, higherPriority); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Handles tools: namespace attributes presence in both documents. |
| 235 |
* @param higherPriority the higherPriority attribute |
| 236 |
*/ |
| 237 |
private void handleBothToolsAttributePresent( |
| 238 |
@NonNull XmlAttribute higherPriority) { |
| 239 |
|
| 240 |
// do not merge tools:node attributes, the higher priority one wins. |
| 241 |
if (getName().getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)) { |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
// everything else should be merged, duplicates should be eliminated. |
| 246 |
@NonNull Splitter splitter = Splitter.on(','); |
| 247 |
@NonNull ImmutableSet.Builder<String> targetValues = ImmutableSet.builder(); |
| 248 |
targetValues.addAll(splitter.split(higherPriority.getValue())); |
| 249 |
targetValues.addAll(splitter.split(getValue())); |
| 250 |
higherPriority.getXml().setValue(Joiner.on(',').join(targetValues.build())); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Merge this attribute value (on a lower priority element) with a implicit default value |
| 255 |
* (implicitly declared on the implicitNode). |
| 256 |
* @param mergingReport report to log errors and actions. |
| 257 |
* @param implicitNode the lower priority node where the implicit attribute value resides. |
| 258 |
* @return the merged value that should be stored in the attribute or null if nothing should |
| 259 |
* be stored. |
| 260 |
*/ |
| 261 |
@Nullable |
| 262 |
private String mergeThisAndDefaultValue(@NonNull MergingReport.Builder mergingReport, |
| 263 |
@NonNull XmlElement implicitNode) { |
| 264 |
|
| 265 |
String mergedValue = getValue(); |
| 266 |
if (mAttributeModel == null || mAttributeModel.getDefaultValue() == null |
| 267 |
|| !mAttributeModel.getMergingPolicy().shouldMergeDefaultValues()) { |
| 268 |
return mergedValue; |
| 269 |
} |
| 270 |
String defaultValue = mAttributeModel.getDefaultValue(); |
| 271 |
if (defaultValue.equals(mergedValue)) { |
| 272 |
// even though the lower priority attribute is only declared and its value is the same |
| 273 |
// as the default value, ensure it gets added to the higher priority node. |
| 274 |
return mergedValue; |
| 275 |
} else { |
| 276 |
// ok, the default value and actual declaration are different, delegate to the |
| 277 |
// merging policy to figure out what value should be used if any. |
| 278 |
mergedValue = mAttributeModel.getMergingPolicy().merge(defaultValue, mergedValue); |
| 279 |
if (mergedValue == null) { |
| 280 |
addIllegalImplicitOverrideMessage(mergingReport, mAttributeModel, implicitNode); |
| 281 |
return null; |
| 282 |
} |
| 283 |
if (mergedValue.equals(defaultValue)) { |
| 284 |
// no need to forcefully add an attribute to the parent with its default value |
| 285 |
// since it was not declared to start with. |
| 286 |
return null; |
| 287 |
} |
| 288 |
} |
| 289 |
return mergedValue; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Merge this attribute value with a lower priority node attribute default value. |
| 294 |
* The attribute is not explicitly set on the implicitNode, yet it exist on this attribute |
| 295 |
* {@link com.android.manifmerger.XmlElement} higher priority owner. |
| 296 |
* |
| 297 |
* @param mergingReport report to log errors and actions. |
| 298 |
* @param implicitNode the lower priority node where the implicit attribute value resides. |
| 299 |
*/ |
| 300 |
void mergeWithLowerPriorityDefaultValue( |
| 301 |
@NonNull MergingReport.Builder mergingReport, @NonNull XmlElement implicitNode) { |
| 302 |
|
| 303 |
if (mAttributeModel == null || mAttributeModel.getDefaultValue() == null |
| 304 |
|| !mAttributeModel.getMergingPolicy().shouldMergeDefaultValues()) { |
| 305 |
return; |
| 306 |
} |
| 307 |
// if this value has been explicitly set to replace the implicit default value, just |
| 308 |
// log the action. |
| 309 |
if (mOwnerElement.getAttributeOperationType(getName()) == AttributeOperationType.REPLACE) { |
| 310 |
mergingReport.getActionRecorder().recordImplicitRejection(this, implicitNode); |
| 311 |
return; |
| 312 |
} |
| 313 |
@Nullable String mergedValue = mAttributeModel.getMergingPolicy().merge( |
| 314 |
getValue(), mAttributeModel.getDefaultValue()); |
| 315 |
if (mergedValue == null) { |
| 316 |
addIllegalImplicitOverrideMessage(mergingReport, mAttributeModel, implicitNode); |
| 317 |
} else { |
| 318 |
getXml().setValue(mergedValue); |
| 319 |
mergingReport.getActionRecorder().recordAttributeAction( |
| 320 |
this, |
| 321 |
Actions.ActionType.MERGED, |
| 322 |
null /* attributeOperationType */); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
private void addIllegalImplicitOverrideMessage( |
| 327 |
@NonNull MergingReport.Builder mergingReport, |
| 328 |
@NonNull AttributeModel attributeModel, |
| 329 |
@NonNull XmlElement implicitNode) { |
| 330 |
String error = String.format("Attribute %1$s value=(%2$s) at %3$s" |
| 331 |
+ " cannot override implicit default value=(%4$s) at %5$s", |
| 332 |
getId(), |
| 333 |
getValue(), |
| 334 |
printPosition(), |
| 335 |
attributeModel.getDefaultValue(), |
| 336 |
implicitNode.printPosition()); |
| 337 |
addMessage(mergingReport, MergingReport.Record.Severity.ERROR, error); |
| 338 |
} |
| 339 |
|
| 340 |
private void addConflictingValueMessage( |
| 341 |
@NonNull MergingReport.Builder report, |
| 342 |
@NonNull XmlAttribute higherPriority) { |
| 343 |
|
| 344 |
Actions.AttributeRecord attributeRecord = report.getActionRecorder() |
| 345 |
.getAttributeCreationRecord(higherPriority); |
| 346 |
|
| 347 |
String error; |
| 348 |
if (getOwnerElement().getType().getMergeType() == MergeType.MERGE_CHILDREN_ONLY) { |
| 349 |
error = String.format( |
| 350 |
"Attribute %1$s value=(%2$s) from %3$s\n" |
| 351 |
+ "\tis also present at %4$s value=(%5$s).\n" |
| 352 |
+ "\tAttributes of <%6$s> elements are not merged.", |
| 353 |
higherPriority.getId(), |
| 354 |
higherPriority.getValue(), |
| 355 |
attributeRecord != null |
| 356 |
? attributeRecord.getActionLocation().print(true /*shortFormat*/) |
| 357 |
: "(unknown)", |
| 358 |
printPosition(), |
| 359 |
getValue(), |
| 360 |
getOwnerElement().getType().toXmlName()); |
| 361 |
} else { |
| 362 |
error = String.format( |
| 363 |
"Attribute %1$s value=(%2$s) from %3$s\n" |
| 364 |
+ "\tis also present at %4$s value=(%5$s).\n" |
| 365 |
+ "\tSuggestion: add 'tools:replace=\"%6$s\"' to <%7$s> element " |
| 366 |
+ "at %8$s to override.", |
| 367 |
higherPriority.getId(), |
| 368 |
higherPriority.getValue(), |
| 369 |
attributeRecord != null |
| 370 |
? attributeRecord.getActionLocation().print(true /*shortFormat*/) |
| 371 |
: "(unknown)", |
| 372 |
printPosition(), |
| 373 |
getValue(), |
| 374 |
mXml.getName(), |
| 375 |
getOwnerElement().getType().toXmlName(), |
| 376 |
higherPriority.getOwnerElement().printPosition()); |
| 377 |
} |
| 378 |
higherPriority.addMessage(report, |
| 379 |
attributeRecord != null |
| 380 |
? attributeRecord.getActionLocation().getPosition() |
| 381 |
: SourcePosition.UNKNOWN, |
| 382 |
MergingReport.Record.Severity.ERROR, error); |
| 383 |
} |
| 384 |
|
| 385 |
void addMessage(@NonNull MergingReport.Builder report, |
| 386 |
@NonNull MergingReport.Record.Severity severity, |
| 387 |
@NonNull String message) { |
| 388 |
addMessage(report, getPosition(), severity, message); |
| 389 |
} |
| 390 |
|
| 391 |
void addMessage(@NonNull MergingReport.Builder report, |
| 392 |
@NonNull SourcePosition position, |
| 393 |
@NonNull MergingReport.Record.Severity severity, |
| 394 |
@NonNull String message) { |
| 395 |
report.addMessage( |
| 396 |
new SourceFilePosition(getOwnerElement().getDocument().getSourceFile(), position), |
| 397 |
severity, message); |
| 398 |
} |
| 399 |
|
| 400 |
@NonNull |
| 401 |
@Override |
| 402 |
public SourceFile getSourceFile() { |
| 403 |
return getOwnerElement().getSourceFile(); |
| 404 |
} |
| 405 |
} |