1 /* 2 * Copyright (c) 2023 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 "adapter/preview/external/ability/context.h" 17 18 #include <fstream> 19 #include <iostream> 20 #include <sstream> 21 22 #include "adapter/preview/external/ability/fa/fa_context.h" 23 #include "adapter/preview/external/ability/stage/stage_context.h" 24 25 namespace OHOS::Ace { 26 namespace { 27 constexpr char MODULE_JSON[] = "module.json"; 28 constexpr char CONFIG_JSON[] = "config.json"; 29 #ifdef WINDOWS_PLATFORM 30 constexpr char DELIMITER[] = "\\"; 31 #else 32 constexpr char DELIMITER[] = "/"; 33 #endif 34 } // namespace 35 CreateContext(bool isStage,const std::string & rootDir)36RefPtr<Context> Context::CreateContext(bool isStage, const std::string& rootDir) 37 { 38 std::string filename = rootDir + DELIMITER + (isStage ? MODULE_JSON : CONFIG_JSON); 39 std::string contents; 40 std::ostringstream formatMessage; 41 std::ifstream fin(filename, std::ios::in); 42 if (!fin) { 43 LOGW("Unable to open input file: %{public}s", filename.c_str()); 44 return nullptr; 45 } 46 while (fin >> contents) { 47 formatMessage << contents; 48 } 49 contents = formatMessage.str(); 50 fin.close(); 51 RefPtr<Context> context; 52 if (isStage) { 53 context = AceType::MakeRefPtr<StageContext>(); 54 } else { 55 context = AceType::MakeRefPtr<FaContext>(); 56 } 57 context->Parse(contents); 58 return context; 59 } 60 } // namespace OHOS::Ace 61