1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "file_manager.h"
17 #include<algorithm>
18 #include<iostream>
19 #include "factory_resource_compiler.h"
20 #include "file_entry.h"
21 #include "key_parser.h"
22 #include "reference_parser.h"
23 #include "resource_directory.h"
24 #include "resource_util.h"
25 #include "restool_errors.h"
26
27 #include "resource_module.h"
28
29 namespace OHOS {
30 namespace Global {
31 namespace Restool {
32 using namespace std;
ScanModules(const vector<string> & inputs,const string & output)33 uint32_t FileManager::ScanModules(const vector<string> &inputs, const string &output)
34 {
35 vector<string> sxmlFolders;
36 for (auto input : inputs) {
37 map<ResType, vector<DirectoryInfo>> resTypeOfDirs;
38 if (ScanModule(input, output, resTypeOfDirs) != RESTOOL_SUCCESS) {
39 return RESTOOL_ERROR;
40 }
41 FilterRefSolidXml(output, sxmlFolders, resTypeOfDirs);
42 }
43 return ParseReference(output, sxmlFolders);
44 }
45
ScanIncrement(const string & output)46 uint32_t FileManager::ScanIncrement(const string &output)
47 {
48 auto &incrementManager = IncrementManager::GetInstance();
49 items_ = incrementManager.GetResourceItems();
50 vector<string> sxmlFolders;
51 FilterRefSolidXml(output, sxmlFolders, incrementManager.GetScanDirs());
52 return ParseReference(output, sxmlFolders);
53 }
54
MergeResourceItem(const map<int32_t,vector<ResourceItem>> & resourceInfos)55 uint32_t FileManager::MergeResourceItem(const map<int32_t, vector<ResourceItem>> &resourceInfos)
56 {
57 return ResourceModule::MergeResourceItem(items_, resourceInfos);
58 }
59
60 // below private founction
ScanModule(const string & input,const string & output,map<ResType,vector<DirectoryInfo>> & resTypeOfDirs)61 uint32_t FileManager::ScanModule(const string &input, const string &output,
62 map<ResType, vector<DirectoryInfo>> &resTypeOfDirs)
63 {
64 ResourceModule resourceModule(input, output, moduleName_);
65 if (resourceModule.ScanResource() != RESTOOL_SUCCESS) {
66 return RESTOOL_ERROR;
67 }
68 resTypeOfDirs = resourceModule.GetScanDirectorys();
69 MergeResourceItem(resourceModule.GetOwner());
70 return RESTOOL_SUCCESS;
71 }
72
ParseReference(const string & output,const vector<string> & sxmlFolders)73 uint32_t FileManager::ParseReference(const string &output, const vector<string> &sxmlFolders)
74 {
75 ReferenceParser referenceParser;
76 if (referenceParser.ParseRefInSolidXml(sxmlFolders) != RESTOOL_SUCCESS ||
77 referenceParser.ParseRefInElement(items_) != RESTOOL_SUCCESS ||
78 referenceParser.ParseRefInProfile(output) != RESTOOL_SUCCESS) {
79 return RESTOOL_ERROR;
80 }
81 return RESTOOL_SUCCESS;
82 }
83
NeedParseReferenceInSolidXml(ResType resType) const84 bool FileManager::NeedParseReferenceInSolidXml(ResType resType) const
85 {
86 if (resType == ResType::LAYOUT || resType == ResType::ANIMATION || resType == ResType::GRAPHIC) {
87 return true;
88 }
89 return false;
90 }
91
FilterRefSolidXml(const string & output,vector<string> & outputPaths,const map<ResType,vector<DirectoryInfo>> & resTypeOfDirs) const92 void FileManager::FilterRefSolidXml(const string &output, vector<string> &outputPaths,
93 const map<ResType, vector<DirectoryInfo>> &resTypeOfDirs) const
94 {
95 for (const auto &iter : resTypeOfDirs) {
96 if (!NeedParseReferenceInSolidXml(iter.first)) {
97 continue;
98 }
99 for (const auto &resourceDir : iter.second) {
100 string outputPath = FileEntry::FilePath(output).Append(RESOURCES_DIR).Append(resourceDir.limitKey)
101 .Append(resourceDir.fileCluster).GetPath();
102 if (find(outputPaths.begin(), outputPaths.end(), outputPath) == outputPaths.end()) {
103 outputPaths.push_back(outputPath);
104 }
105 }
106 }
107 }
108 }
109 }
110 }
111