| 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.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
import com.android.annotations.VisibleForTesting; |
| 22 |
import com.android.annotations.concurrency.Immutable; |
| 23 |
import com.android.ide.common.blame.MessageJsonSerializer; |
| 24 |
import com.android.ide.common.blame.SourceFile; |
| 25 |
import com.android.ide.common.blame.SourceFilePosition; |
| 26 |
import com.android.utils.ILogger; |
| 27 |
import com.google.common.base.Objects; |
| 28 |
import com.google.common.base.Optional; |
| 29 |
import com.google.common.base.Preconditions; |
| 30 |
import com.google.common.collect.ImmutableList; |
| 31 |
import com.google.common.collect.ImmutableMap; |
| 32 |
import com.google.common.collect.ImmutableMultimap; |
| 33 |
import com.google.common.io.LineReader; |
| 34 |
import com.google.gson.Gson; |
| 35 |
import com.google.gson.GsonBuilder; |
| 36 |
import com.google.gson.JsonDeserializationContext; |
| 37 |
import com.google.gson.JsonDeserializer; |
| 38 |
import com.google.gson.JsonElement; |
| 39 |
import com.google.gson.JsonParseException; |
| 40 |
|
| 41 |
import org.xml.sax.SAXException; |
| 42 |
|
| 43 |
import java.io.FileWriter; |
| 44 |
import java.io.IOException; |
| 45 |
import java.io.InputStream; |
| 46 |
import java.io.InputStreamReader; |
| 47 |
import java.io.StringReader; |
| 48 |
import java.lang.reflect.Type; |
| 49 |
import java.util.ArrayList; |
| 50 |
import java.util.HashMap; |
| 51 |
import java.util.List; |
| 52 |
import java.util.Map; |
| 53 |
import java.util.Set; |
| 54 |
|
| 55 |
import javax.xml.parsers.ParserConfigurationException; |
| 56 |
|
| 57 |
/** |
| 58 |
* Contains all actions taken during a merging invocation. |
| 59 |
*/ |
| 60 |
@Immutable |
| 61 |
public class Actions { |
| 62 |
|
| 63 |
// TODO: i18n |
| 64 |
@VisibleForTesting |
| 65 |
static final String HEADER = "-- Merging decision tree log ---\n"; |
| 66 |
|
| 67 |
// defines all the records for the merging tool activity, indexed by element name+key. |
| 68 |
// iterator should be ordered by the key insertion order. |
| 69 |
private final Map<XmlNode.NodeKey, DecisionTreeRecord> mRecords; |
| 70 |
|
| 71 |
public Actions(Map<XmlNode.NodeKey, DecisionTreeRecord> records) { |
| 72 |
mRecords = records; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns a {@link com.google.common.collect.ImmutableSet} of all the element's keys that have |
| 77 |
* at least one {@link NodeRecord}. |
| 78 |
*/ |
| 79 |
@NonNull |
| 80 |
public Set<XmlNode.NodeKey> getNodeKeys() { |
| 81 |
return mRecords.keySet(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns an {@link ImmutableList} of {@link NodeRecord} for the element identified with the |
| 86 |
* passed key. |
| 87 |
*/ |
| 88 |
@NonNull |
| 89 |
public ImmutableList<NodeRecord> getNodeRecords(XmlNode.NodeKey key) { |
| 90 |
return mRecords.containsKey(key) |
| 91 |
? mRecords.get(key).getNodeRecords() |
| 92 |
: ImmutableList.<NodeRecord>of(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns a {@link ImmutableList} of all attributes names that have at least one record for |
| 97 |
* the element identified with the passed key. |
| 98 |
*/ |
| 99 |
@NonNull |
| 100 |
public ImmutableList<XmlNode.NodeName> getRecordedAttributeNames(XmlNode.NodeKey nodeKey) { |
| 101 |
DecisionTreeRecord decisionTreeRecord = mRecords.get(nodeKey); |
| 102 |
if (decisionTreeRecord == null) { |
| 103 |
return ImmutableList.of(); |
| 104 |
} |
| 105 |
return decisionTreeRecord.getAttributesRecords().keySet().asList(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the {@link com.google.common.collect.ImmutableList} of {@link AttributeRecord} for |
| 110 |
* the attribute identified by attributeName of the element identified by elementKey. |
| 111 |
*/ |
| 112 |
@NonNull |
| 113 |
public ImmutableList<AttributeRecord> getAttributeRecords(XmlNode.NodeKey elementKey, |
| 114 |
XmlNode.NodeName attributeName) { |
| 115 |
|
| 116 |
DecisionTreeRecord decisionTreeRecord = mRecords.get(elementKey); |
| 117 |
if (decisionTreeRecord == null) { |
| 118 |
return ImmutableList.of(); |
| 119 |
} |
| 120 |
return decisionTreeRecord.getAttributeRecords(attributeName); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Initial dump of the merging tool actions, need to be refined and spec'ed out properly. |
| 125 |
* @param logger logger to log to at INFO level. |
| 126 |
*/ |
| 127 |
void log(@NonNull ILogger logger) { |
| 128 |
logger.verbose(getLogs()); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Dump merging tool actions to a text file. |
| 133 |
* @param fileWriter the file to write all actions into. |
| 134 |
* @throws IOException |
| 135 |
*/ |
| 136 |
void log(@NonNull FileWriter fileWriter) throws IOException { |
| 137 |
fileWriter.append(getLogs()); |
| 138 |
} |
| 139 |
|
| 140 |
private String getLogs() { |
| 141 |
StringBuilder stringBuilder = new StringBuilder(); |
| 142 |
stringBuilder.append(HEADER); |
| 143 |
for (Map.Entry<XmlNode.NodeKey, Actions.DecisionTreeRecord> record : mRecords.entrySet()) { |
| 144 |
stringBuilder.append(record.getKey()).append("\n"); |
| 145 |
for (Actions.NodeRecord nodeRecord : record.getValue().getNodeRecords()) { |
| 146 |
nodeRecord.print(stringBuilder); |
| 147 |
stringBuilder.append('\n'); |
| 148 |
} |
| 149 |
for (Map.Entry<XmlNode.NodeName, List<Actions.AttributeRecord>> attributeRecords : |
| 150 |
record.getValue().mAttributeRecords.entrySet()) { |
| 151 |
stringBuilder.append('\t').append(attributeRecords.getKey()).append('\n'); |
| 152 |
for (Actions.AttributeRecord attributeRecord : attributeRecords.getValue()) { |
| 153 |
stringBuilder.append("\t\t"); |
| 154 |
attributeRecord.print(stringBuilder); |
| 155 |
stringBuilder.append('\n'); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
return stringBuilder.toString(); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Defines all possible actions taken from the merging tool for an xml element or attribute. |
| 164 |
*/ |
| 165 |
public enum ActionType { |
| 166 |
/** |
| 167 |
* The element was added into the resulting merged manifest. |
| 168 |
*/ |
| 169 |
ADDED, |
| 170 |
/** |
| 171 |
* The element was injected from the merger invocation parameters. |
| 172 |
*/ |
| 173 |
INJECTED, |
| 174 |
/** |
| 175 |
* The element was merged with another element into the resulting merged manifest. |
| 176 |
*/ |
| 177 |
MERGED, |
| 178 |
/** |
| 179 |
* The element was rejected. |
| 180 |
*/ |
| 181 |
REJECTED, |
| 182 |
/** |
| 183 |
* The implied element was added was added when importing a library that expected the |
| 184 |
* element to be present by default while targeted SDK requires its declaration. |
| 185 |
*/ |
| 186 |
IMPLIED, |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Defines an abstract record contain common metadata for elements and attributes actions. |
| 191 |
*/ |
| 192 |
public abstract static class Record { |
| 193 |
|
| 194 |
@NonNull protected final ActionType mActionType; |
| 195 |
@NonNull protected final SourceFilePosition mActionLocation; |
| 196 |
@NonNull protected final XmlNode.NodeKey mTargetId; |
| 197 |
@Nullable protected final String mReason; |
| 198 |
|
| 199 |
private Record(@NonNull ActionType actionType, |
| 200 |
@NonNull SourceFilePosition actionLocation, |
| 201 |
@NonNull XmlNode.NodeKey targetId, |
| 202 |
@Nullable String reason) { |
| 203 |
mActionType = Preconditions.checkNotNull(actionType); |
| 204 |
mActionLocation = Preconditions.checkNotNull(actionLocation); |
| 205 |
mTargetId = Preconditions.checkNotNull(targetId); |
| 206 |
mReason = reason; |
| 207 |
} |
| 208 |
|
| 209 |
@NonNull |
| 210 |
public ActionType getActionType() { |
| 211 |
return mActionType; |
| 212 |
} |
| 213 |
|
| 214 |
@NonNull |
| 215 |
public SourceFilePosition getActionLocation() { |
| 216 |
return mActionLocation; |
| 217 |
} |
| 218 |
|
| 219 |
@NonNull |
| 220 |
public XmlNode.NodeKey getTargetId() { |
| 221 |
return mTargetId; |
| 222 |
} |
| 223 |
|
| 224 |
@Nullable |
| 225 |
public String getReason() { |
| 226 |
return mReason; |
| 227 |
} |
| 228 |
|
| 229 |
public void print(@NonNull StringBuilder stringBuilder) { |
| 230 |
stringBuilder.append(mActionType) |
| 231 |
.append(" from ") |
| 232 |
.append(mActionLocation); |
| 233 |
if (mReason != null) { |
| 234 |
stringBuilder.append(" reason: ") |
| 235 |
.append(mReason); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Defines a merging tool action for an xml element. |
| 242 |
*/ |
| 243 |
public static class NodeRecord extends Record { |
| 244 |
|
| 245 |
@NonNull |
| 246 |
private final NodeOperationType mNodeOperationType; |
| 247 |
|
| 248 |
NodeRecord(@NonNull ActionType actionType, |
| 249 |
@NonNull SourceFilePosition actionLocation, |
| 250 |
@NonNull XmlNode.NodeKey targetId, |
| 251 |
@Nullable String reason, |
| 252 |
@NonNull NodeOperationType nodeOperationType) { |
| 253 |
super(actionType, actionLocation, targetId, reason); |
| 254 |
this.mNodeOperationType = Preconditions.checkNotNull(nodeOperationType); |
| 255 |
} |
| 256 |
|
| 257 |
@NonNull |
| 258 |
@Override |
| 259 |
public String toString() { |
| 260 |
return "Id=" + mTargetId.toString() + " actionType=" + getActionType() |
| 261 |
+ " location=" + getActionLocation() |
| 262 |
+ " opType=" + mNodeOperationType; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Defines a merging tool action for an xml attribute |
| 268 |
*/ |
| 269 |
public static class AttributeRecord extends Record { |
| 270 |
|
| 271 |
// first in wins which should be fine, the first |
| 272 |
// operation type will be the highest priority one |
| 273 |
@Nullable |
| 274 |
private final AttributeOperationType mOperationType; |
| 275 |
|
| 276 |
AttributeRecord( |
| 277 |
@NonNull ActionType actionType, |
| 278 |
@NonNull SourceFilePosition actionLocation, |
| 279 |
@NonNull XmlNode.NodeKey targetId, |
| 280 |
@Nullable String reason, |
| 281 |
@Nullable AttributeOperationType operationType) { |
| 282 |
super(actionType, actionLocation, targetId, reason); |
| 283 |
this.mOperationType = operationType; |
| 284 |
} |
| 285 |
|
| 286 |
@Nullable |
| 287 |
public AttributeOperationType getOperationType() { |
| 288 |
return mOperationType; |
| 289 |
} |
| 290 |
|
| 291 |
@NonNull |
| 292 |
@Override |
| 293 |
public String toString() { |
| 294 |
return Objects.toStringHelper(this).add("Id", mTargetId) |
| 295 |
.add("actionType=",getActionType()) |
| 296 |
.add("location", getActionLocation()) |
| 297 |
.add("opType", getOperationType()).toString(); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
@NonNull |
| 302 |
public String persist() throws IOException { |
| 303 |
//noinspection SpellCheckingInspection |
| 304 |
GsonBuilder gson = new GsonBuilder().setPrettyPrinting(); |
| 305 |
gson.enableComplexMapKeySerialization(); |
| 306 |
MessageJsonSerializer.registerTypeAdapters(gson); |
| 307 |
return gson.create().toJson(this); |
| 308 |
} |
| 309 |
|
| 310 |
@Nullable |
| 311 |
public static Actions load(@NonNull InputStream inputStream) throws IOException { |
| 312 |
|
| 313 |
return getGsonParser().fromJson(new InputStreamReader(inputStream), Actions.class); |
| 314 |
} |
| 315 |
|
| 316 |
private static class NodeNameDeserializer implements JsonDeserializer<XmlNode.NodeName> { |
| 317 |
|
| 318 |
@Override |
| 319 |
public XmlNode.NodeName deserialize(@NonNull JsonElement json, Type typeOfT, |
| 320 |
@NonNull JsonDeserializationContext context) throws JsonParseException { |
| 321 |
if (json.getAsJsonObject().get("mNamespaceURI") != null) { |
| 322 |
return context.deserialize(json, XmlNode.NamespaceAwareName.class); |
| 323 |
} else { |
| 324 |
return context.deserialize(json, XmlNode.Name.class); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
@Nullable |
| 330 |
public static Actions load(String xml) { |
| 331 |
return getGsonParser().fromJson(xml, Actions.class); |
| 332 |
} |
| 333 |
|
| 334 |
@SuppressWarnings("SpellCheckingInspection") |
| 335 |
@NonNull |
| 336 |
private static Gson getGsonParser() { |
| 337 |
GsonBuilder gsonBuilder = new GsonBuilder(); |
| 338 |
gsonBuilder.enableComplexMapKeySerialization(); |
| 339 |
gsonBuilder.registerTypeAdapter(XmlNode.NodeName.class, new NodeNameDeserializer()); |
| 340 |
MessageJsonSerializer.registerTypeAdapters(gsonBuilder); |
| 341 |
return gsonBuilder.create(); |
| 342 |
} |
| 343 |
|
| 344 |
public ImmutableMultimap<Integer, Record> getResultingSourceMapping(@NonNull XmlDocument xmlDocument) |
| 345 |
throws ParserConfigurationException, SAXException, IOException { |
| 346 |
|
| 347 |
SourceFile inMemory = SourceFile.UNKNOWN; |
| 348 |
|
| 349 |
XmlDocument loadedWithLineNumbers = XmlLoader.load( |
| 350 |
xmlDocument.getSelectors(), |
| 351 |
xmlDocument.getSystemPropertyResolver(), |
| 352 |
inMemory, |
| 353 |
xmlDocument.prettyPrint(), |
| 354 |
XmlDocument.Type.MAIN, |
| 355 |
Optional.<String>absent() /* mainManifestPackageName */); |
| 356 |
|
| 357 |
ImmutableMultimap.Builder<Integer, Record> mappingBuilder = ImmutableMultimap.builder(); |
| 358 |
for (XmlElement xmlElement : loadedWithLineNumbers.getRootNode().getMergeableElements()) { |
| 359 |
parse(xmlElement, mappingBuilder); |
| 360 |
} |
| 361 |
return mappingBuilder.build(); |
| 362 |
} |
| 363 |
|
| 364 |
private void parse(@NonNull XmlElement element, |
| 365 |
@NonNull ImmutableMultimap.Builder<Integer, Record> mappings) { |
| 366 |
DecisionTreeRecord decisionTreeRecord = mRecords.get(element.getId()); |
| 367 |
if (decisionTreeRecord != null) { |
| 368 |
Actions.NodeRecord nodeRecord = findNodeRecord(decisionTreeRecord); |
| 369 |
if (nodeRecord != null) { |
| 370 |
mappings.put(element.getPosition().getStartLine(), nodeRecord); |
| 371 |
} |
| 372 |
for (XmlAttribute xmlAttribute : element.getAttributes()) { |
| 373 |
Actions.AttributeRecord attributeRecord = findAttributeRecord(decisionTreeRecord, |
| 374 |
xmlAttribute); |
| 375 |
if (attributeRecord != null) { |
| 376 |
mappings.put(xmlAttribute.getPosition().getStartLine(), attributeRecord); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
for (XmlElement xmlElement : element.getMergeableElements()) { |
| 381 |
parse(xmlElement, mappings); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
@NonNull |
| 386 |
public String blame(@NonNull XmlDocument xmlDocument) |
| 387 |
throws IOException, SAXException, ParserConfigurationException { |
| 388 |
|
| 389 |
ImmutableMultimap<Integer, Record> resultingSourceMapping = |
| 390 |
getResultingSourceMapping(xmlDocument); |
| 391 |
LineReader lineReader = new LineReader( |
| 392 |
new StringReader(xmlDocument.prettyPrint())); |
| 393 |
|
| 394 |
StringBuilder actualMappings = new StringBuilder(); |
| 395 |
String line; |
| 396 |
int count = 0; |
| 397 |
while ((line = lineReader.readLine()) != null) { |
| 398 |
actualMappings.append(count + 1).append(line).append("\n"); |
| 399 |
if (resultingSourceMapping.containsKey(count)) { |
| 400 |
for (Record record : resultingSourceMapping.get(count)) { |
| 401 |
actualMappings.append(count + 1).append("-->") |
| 402 |
.append(record.getActionLocation().toString()) |
| 403 |
.append("\n"); |
| 404 |
} |
| 405 |
} |
| 406 |
count++; |
| 407 |
} |
| 408 |
return actualMappings.toString(); |
| 409 |
} |
| 410 |
|
| 411 |
@Nullable |
| 412 |
private static Actions.NodeRecord findNodeRecord(@NonNull DecisionTreeRecord decisionTreeRecord) { |
| 413 |
for (Actions.NodeRecord nodeRecord : decisionTreeRecord.getNodeRecords()) { |
| 414 |
if (nodeRecord.getActionType() == Actions.ActionType.ADDED) { |
| 415 |
return nodeRecord; |
| 416 |
} |
| 417 |
} |
| 418 |
return null; |
| 419 |
} |
| 420 |
|
| 421 |
@Nullable |
| 422 |
private static Actions.AttributeRecord findAttributeRecord( |
| 423 |
@NonNull DecisionTreeRecord decisionTreeRecord, |
| 424 |
@NonNull XmlAttribute xmlAttribute) { |
| 425 |
for (Actions.AttributeRecord attributeRecord : decisionTreeRecord |
| 426 |
.getAttributeRecords(xmlAttribute.getName())) { |
| 427 |
if (attributeRecord.getActionType() == Actions.ActionType.ADDED) { |
| 428 |
return attributeRecord; |
| 429 |
} |
| 430 |
} |
| 431 |
return null; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Internal structure on how {@link com.android.manifmerger.Actions.Record}s are kept for an |
| 436 |
* xml element. |
| 437 |
* |
| 438 |
* Each xml element should have an associated DecisionTreeRecord which keeps a list of |
| 439 |
* {@link com.android.manifmerger.Actions.NodeRecord} for all the node actions related |
| 440 |
* to this xml element. |
| 441 |
* |
| 442 |
* It will also contain a map indexed by attribute name on all the attribute actions related |
| 443 |
* to that particular attribute within the xml element. |
| 444 |
* |
| 445 |
*/ |
| 446 |
static class DecisionTreeRecord { |
| 447 |
// all other occurrences of the nodes decisions, in order of decisions. |
| 448 |
private final List<NodeRecord> mNodeRecords = new ArrayList<NodeRecord>(); |
| 449 |
|
| 450 |
// all attributes decisions indexed by attribute name. |
| 451 |
@NonNull |
| 452 |
final Map<XmlNode.NodeName, List<AttributeRecord>> mAttributeRecords = |
| 453 |
new HashMap<XmlNode.NodeName, List<AttributeRecord>>(); |
| 454 |
|
| 455 |
@NonNull |
| 456 |
ImmutableList<NodeRecord> getNodeRecords() { |
| 457 |
return ImmutableList.copyOf(mNodeRecords); |
| 458 |
} |
| 459 |
|
| 460 |
@NonNull |
| 461 |
ImmutableMap<XmlNode.NodeName, List<AttributeRecord>> getAttributesRecords() { |
| 462 |
return ImmutableMap.copyOf(mAttributeRecords); |
| 463 |
} |
| 464 |
|
| 465 |
DecisionTreeRecord() { |
| 466 |
} |
| 467 |
|
| 468 |
void addNodeRecord(@NonNull NodeRecord nodeRecord) { |
| 469 |
mNodeRecords.add(Preconditions.checkNotNull(nodeRecord)); |
| 470 |
} |
| 471 |
|
| 472 |
@NonNull |
| 473 |
ImmutableList<AttributeRecord> getAttributeRecords(XmlNode.NodeName attributeName) { |
| 474 |
List<AttributeRecord> attributeRecords = mAttributeRecords.get(attributeName); |
| 475 |
return attributeRecords == null |
| 476 |
? ImmutableList.<AttributeRecord>of() |
| 477 |
: ImmutableList.copyOf(attributeRecords); |
| 478 |
} |
| 479 |
} |
| 480 |
} |