/** * Saves the {@link com.android.manifmerger.XmlDocument} to a file in UTF-8 encoding. * @param xmlDocument xml document to save. * @param out file to save to. */ private void save(XmlDocument xmlDocument, File out) { try { Files.write(xmlDocument.prettyPrint(), out, Charsets.UTF_8); } catch(IOException e) { throw new RuntimeException(e); } }
@NonNull public String blame(@NonNull XmlDocument xmlDocument) throws IOException, SAXException, ParserConfigurationException { ImmutableMultimap<Integer, Record> resultingSourceMapping = getResultingSourceMapping(xmlDocument); LineReader lineReader = new LineReader( new StringReader(xmlDocument.prettyPrint())); StringBuilder actualMappings = new StringBuilder(); String line; int count = 0; while ((line = lineReader.readLine()) != null) { actualMappings.append(count + 1).append(line).append("\n"); if (resultingSourceMapping.containsKey(count)) { for (Record record : resultingSourceMapping.get(count)) { actualMappings.append(count + 1).append("-->") .append(record.getActionLocation().toString()) .append("\n"); } } count++; } return actualMappings.toString(); }
public ImmutableMultimap<Integer, Record> getResultingSourceMapping(@NonNull XmlDocument xmlDocument) throws ParserConfigurationException, SAXException, IOException { SourceFile inMemory = SourceFile.UNKNOWN; XmlDocument loadedWithLineNumbers = XmlLoader.load( xmlDocument.getSelectors(), xmlDocument.getSystemPropertyResolver(), inMemory, xmlDocument.prettyPrint(), XmlDocument.Type.MAIN, Optional.<String>absent() /* mainManifestPackageName */); ImmutableMultimap.Builder<Integer, Record> mappingBuilder = ImmutableMultimap.builder(); for (XmlElement xmlElement : loadedWithLineNumbers.getRootNode().getMergeableElements()) { parse(xmlElement, mappingBuilder); } return mappingBuilder.build(); }
private Optional<XmlDocument> merge( @NonNull Optional<XmlDocument> xmlDocument, @NonNull LoadedManifestInfo lowerPriorityDocument, @NonNull MergingReport.Builder mergingReportBuilder) throws MergeFailureException { MergingReport.Result validationResult = PreValidator .validate(mergingReportBuilder, lowerPriorityDocument.getXmlDocument()); if (validationResult == MergingReport.Result.ERROR) { mergingReportBuilder.addMessage( lowerPriorityDocument.getXmlDocument().getSourceFile(), MergingReport.Record.Severity.ERROR, "Validation failed, exiting"); return Optional.absent(); } Optional<XmlDocument> result; if (xmlDocument.isPresent()) { result = xmlDocument.get().merge( lowerPriorityDocument.getXmlDocument(), mergingReportBuilder); } else { mergingReportBuilder.getActionRecorder().recordDefaultNodeAction( lowerPriorityDocument.getXmlDocument().getRootNode()); result = Optional.of(lowerPriorityDocument.getXmlDocument()); } // if requested, dump each intermediary merging stage into the report. if (mOptionalFeatures.contains(Invoker.Feature.KEEP_INTERMEDIARY_STAGES) && result.isPresent()) { mergingReportBuilder.addMergingStage(result.get().prettyPrint()); } return result; }
if (!mOptionalFeatures.contains(Invoker.Feature.SKIP_XML_STRING)) { mergingReport.setMergedDocument( MergingReport.MergedManifestKind.MERGED, document.prettyPrint()); mergingReport.setMergedDocument( MergingReport.MergedManifestKind.MERGED, addTestOnlyAttribute(document).prettyPrint()); mergingReport.setMergedDocument( MergingReport.MergedManifestKind.AAPT_SAFE, document.prettyPrint()); mergingReport.setMergedDocument( MergingReport.MergedManifestKind.INSTANT_RUN, instantRunReplacement(document).prettyPrint());