Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/MergingReport.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 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.SourceFile;
24 import com.android.ide.common.blame.SourceFilePosition;
25 import com.android.ide.common.blame.SourcePosition;
26 import com.android.utils.ILogger;
27 import com.google.common.base.CaseFormat;
28 import com.google.common.collect.ImmutableList;
29
30 import org.w3c.dom.Document;
31 import org.xml.sax.SAXException;
32
33 import java.io.IOException;
34 import java.util.EnumMap;
35 import java.util.Map;
36
37 import javax.xml.parsers.ParserConfigurationException;
38
39 /**
40 * Contains the result of 2 files merging.
41 *
42 * TODO: more work necessary, this is pretty raw as it stands.
43 */
44 @Immutable
45 public class MergingReport {
46
47 public enum MergedManifestKind {
48 /**
49 * Merged manifest file
50 */
51 MERGED,
52
53 /**
54 * Merged manifest file with Instant Run related decorations.
55 */
56 INSTANT_RUN,
57
58 /**
59 * Merged manifest file with unresolved placeholders encoded to be AAPT friendly.
60 */
61 AAPT_SAFE,
62
63 /**
64 * Blame file for merged manifest file.
65 */
66 BLAME
67 }
68
69 @NonNull
70 private final Map<MergedManifestKind, String> mMergedDocuments;
71 @NonNull
72 private final Map<MergedManifestKind, XmlDocument> mMergedXmlDocuments;
73 @NonNull
74 private final Result mResult;
75 // list of logging events, ordered by their recording time.
76 @NonNull
77 private final ImmutableList<Record> mRecords;
78 @NonNull
79 private final ImmutableList<String> mIntermediaryStages;
80 @NonNull
81 private final Actions mActions;
82
83 private MergingReport(@NonNull Map<MergedManifestKind, String> mergedDocuments,
84 @NonNull Map<MergedManifestKind, XmlDocument> mergedXmlDocuments,
85 @NonNull Result result,
86 @NonNull ImmutableList<Record> records,
87 @NonNull ImmutableList<String> intermediaryStages,
88 @NonNull Actions actions) {
89 mMergedDocuments = mergedDocuments;
90 mMergedXmlDocuments = mergedXmlDocuments;
91 mResult = result;
92 mRecords = records;
93 mIntermediaryStages = intermediaryStages;
94 mActions = actions;
95 }
96
97 /**
98 * dumps all logging records to a logger.
99 */
100 public void log(@NonNull ILogger logger) {
101 for (Record record : mRecords) {
102 switch(record.mSeverity) {
103 case WARNING:
104 logger.warning(record.toString());
105 break;
106 case ERROR:
107 logger.error(null /* throwable */, record.toString());
108 break;
109 case INFO:
110 logger.verbose(record.toString());
111 break;
112 default:
113 logger.error(null /* throwable */, "Unhandled record type " + record.mSeverity);
114 }
115 }
116 mActions.log(logger);
117
118 if (!mResult.isSuccess()) {
119 logger.warning("\nSee http://g.co/androidstudio/manifest-merger for more information"
120 + " about the manifest merger.\n");
121 }
122 }
123
124 @Nullable
125 public String getMergedDocument(@NonNull MergedManifestKind state) {
126 return mMergedDocuments.get(state);
127 }
128
129 @Nullable
130 public XmlDocument getMergedXmlDocument(@NonNull MergedManifestKind state) {
131 return mMergedXmlDocuments.get(state);
132 }
133
134 /**
135 * Returns all the merging intermediary stages if
136 * {@link com.android.manifmerger.ManifestMerger2.Invoker.Feature#KEEP_INTERMEDIARY_STAGES}
137 * is set.
138 */
139 @NonNull
140 public ImmutableList<String> getIntermediaryStages() {
141 return mIntermediaryStages;
142 }
143
144 /**
145 * Overall result of the merging process.
146 */
147 public enum Result {
148 SUCCESS,
149
150 WARNING,
151
152 ERROR;
153
154 public boolean isSuccess() {
155 return this == SUCCESS || this == WARNING;
156 }
157
158 public boolean isWarning() {
159 return this == WARNING;
160 }
161
162 public boolean isError() {
163 return this == ERROR;
164 }
165 }
166
167 @NonNull
168 public Result getResult() {
169 return mResult;
170 }
171
172 @NonNull
173 public ImmutableList<Record> getLoggingRecords() {
174 return mRecords;
175 }
176
177 @NonNull
178 public Actions getActions() {
179 return mActions;
180 }
181
182 @NonNull
183 public String getReportString() {
184 switch (mResult) {
185 case SUCCESS:
186 return "Manifest merger executed successfully";
187 case WARNING:
188 return mRecords.size() > 1
189 ? "Manifest merger exited with warnings, see logs"
190 : "Manifest merger warning : " + mRecords.get(0).mLog;
191 case ERROR:
192 return mRecords.size() > 1
193 ? "Manifest merger failed with multiple errors, see logs"
194 : "Manifest merger failed : " + mRecords.get(0).mLog;
195 default:
196 return "Manifest merger returned an invalid result " + mResult;
197 }
198 }
199
200 /**
201 * Log record. This is used to give users some information about what is happening and
202 * what might have gone wrong.
203 */
204 public static class Record {
205
206
207 public enum Severity {WARNING, ERROR, INFO }
208
209 @NonNull
210 private final Severity mSeverity;
211 @NonNull
212 private final String mLog;
213 @NonNull
214 private final SourceFilePosition mSourceLocation;
215
216 private Record(
217 @NonNull SourceFilePosition sourceLocation,
218 @NonNull Severity severity,
219 @NonNull String mLog) {
220 this.mSourceLocation = sourceLocation;
221 this.mSeverity = severity;
222 this.mLog = mLog;
223 }
224
225 @NonNull
226 public Severity getSeverity() {
227 return mSeverity;
228 }
229
230 @NonNull
231 public String getMessage() {
232 return mLog;
233 }
234
235 @NonNull
236 public SourceFilePosition getSourceLocation() {
237 return mSourceLocation;
238 }
239
240 @NonNull
241 @Override
242 public String toString() {
243 return mSourceLocation.toString() // needs short string.
244 + " "
245 + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, mSeverity.toString())
246 + ":\n\t"
247 + mLog;
248 }
249 }
250
251 /**
252 * This builder is used to accumulate logging, action recording and intermediary results as
253 * well as final result of the merging activity.
254 *
255 * Once the merging is finished, the {@link #build()} is called to return an immutable version
256 * of itself with all the logging, action recordings and xml files obtainable.
257 *
258 */
259 static class Builder {
260
261 private Map<MergedManifestKind, String> mergedDocuments =
262 new EnumMap<MergedManifestKind, String>(MergedManifestKind.class);
263 private Map<MergedManifestKind, XmlDocument> mergedXmlDocuments =
264 new EnumMap<MergedManifestKind, XmlDocument>(MergedManifestKind.class);
265
266
267 @NonNull
268 private ImmutableList.Builder<Record> mRecordBuilder = new ImmutableList.Builder<Record>();
269 @NonNull
270 private ImmutableList.Builder<String> mIntermediaryStages = new ImmutableList.Builder<String>();
271 private boolean mHasWarnings = false;
272 private boolean mHasErrors = false;
273 @NonNull
274 private ActionRecorder mActionRecorder = new ActionRecorder();
275 private final ILogger mLogger;
276
277 Builder(ILogger logger) {
278 mLogger = logger;
279 }
280
281 Builder setMergedDocument(@NonNull MergedManifestKind mergedManifestKind, @NonNull String mergedDocument) {
282 this.mergedDocuments.put(mergedManifestKind, mergedDocument);
283 return this;
284 }
285
286 Builder setMergedXmlDocument(@NonNull MergedManifestKind mergedManifestKind, @NonNull XmlDocument mergedDocument) {
287 this.mergedXmlDocuments.put(mergedManifestKind, mergedDocument);
288 return this;
289 }
290
291 @NonNull
292 @VisibleForTesting
293 Builder addMessage(@NonNull SourceFile sourceFile,
294 int line,
295 int column,
296 @NonNull Record.Severity severity,
297 @NonNull String message) {
298 // The line and column used are 1-based, but SourcePosition uses zero-based.
299 return addMessage(
300 new SourceFilePosition(sourceFile, new SourcePosition(line - 1, column -1, -1)),
301 severity,
302 message);
303 }
304
305 @NonNull
306 Builder addMessage(@NonNull SourceFile sourceFile,
307 @NonNull Record.Severity severity,
308 @NonNull String message) {
309 return addMessage(
310 new SourceFilePosition(sourceFile, SourcePosition.UNKNOWN),
311 severity,
312 message);
313 }
314
315 @NonNull
316 Builder addMessage(@NonNull SourceFilePosition sourceFilePosition,
317 @NonNull Record.Severity severity,
318 @NonNull String message) {
319 switch (severity) {
320 case ERROR:
321 mHasErrors = true;
322 break;
323 case WARNING:
324 mHasWarnings = true;
325 break;
326 }
327 mRecordBuilder.add(new Record(sourceFilePosition, severity, message));
328 return this;
329 }
330
331 @NonNull
332 Builder addMergingStage(@NonNull String xml) {
333 mIntermediaryStages.add(xml);
334 return this;
335 }
336
337 /**
338 * Returns true if some fatal errors were reported.
339 */
340 boolean hasErrors() {
341 return mHasErrors;
342 }
343
344 @NonNull
345 ActionRecorder getActionRecorder() {
346 return mActionRecorder;
347 }
348
349 @NonNull
350 MergingReport build() {
351 Result result = mHasErrors
352 ? Result.ERROR
353 : mHasWarnings
354 ? Result.WARNING
355 : Result.SUCCESS;
356
357 return new MergingReport(
358 mergedDocuments,
359 mergedXmlDocuments,
360 result,
361 mRecordBuilder.build(),
362 mIntermediaryStages.build(),
363 mActionRecorder.build());
364 }
365
366 public ILogger getLogger() {
367 return mLogger;
368 }
369
370 public String blame(XmlDocument document)
371 throws ParserConfigurationException, SAXException, IOException {
372 return mActionRecorder.build().blame(document);
373 }
374 }
375 }