TableMerger.h revision 1ab598f46c3ff520a67f9d80194847741f3467ab
1/*
2 * Copyright (C) 2015 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#ifndef AAPT_TABLEMERGER_H
18#define AAPT_TABLEMERGER_H
19
20#include "ResourceTable.h"
21#include "ResourceValues.h"
22#include "util/Util.h"
23
24#include "process/IResourceTableConsumer.h"
25
26#include <queue>
27#include <set>
28
29namespace aapt {
30
31struct FileToMerge {
32    ResourceTable* srcTable;
33    std::u16string srcPath;
34    std::u16string dstPath;
35};
36
37/**
38 * TableMerger takes resource tables and merges all packages within the tables that have the same
39 * package ID.
40 *
41 * If a package has a different name, all the entries in that table have their names mangled
42 * to include the package name. This way there are no collisions. In order to do this correctly,
43 * the TableMerger needs to also mangle any FileReference paths. Once these are mangled,
44 * the original source path of the file, along with the new destination path is recorded in the
45 * queue returned from getFileMergeQueue().
46 *
47 * Once the merging is complete, a separate process can go collect the files from the various
48 * source APKs and either copy or process their XML and put them in the correct location in
49 * the final APK.
50 */
51class TableMerger {
52public:
53    TableMerger(IAaptContext* context, ResourceTable* outTable);
54
55    inline std::queue<FileToMerge>* getFileMergeQueue() {
56        return &mFilesToMerge;
57    }
58
59    inline const std::set<std::u16string>& getMergedPackages() const {
60        return mMergedPackages;
61    }
62
63    bool merge(const Source& src, ResourceTable* table);
64
65private:
66    IAaptContext* mContext;
67    ResourceTable* mMasterTable;
68    ResourceTablePackage* mMasterPackage;
69
70    std::set<std::u16string> mMergedPackages;
71    std::queue<FileToMerge> mFilesToMerge;
72
73    bool doMerge(const Source& src, ResourceTable* srcTable, ResourceTablePackage* srcPackage,
74                 const bool manglePackage);
75
76    std::unique_ptr<Value> cloneAndMangle(ResourceTable* table, const std::u16string& package,
77                                          Value* value);
78    std::unique_ptr<Value> clone(Value* value);
79};
80
81} // namespace aapt
82
83#endif /* AAPT_TABLEMERGER_H */
84