1 /* 2 * Copyright (c) 2022 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 "utils/log.h" 17 #include "js_convertxml.h" 18 #include "napi/native_api.h" 19 #include "napi/native_node_api.h" 20 21 extern const char _binary_js_convertxml_js_start[]; 22 extern const char _binary_js_convertxml_js_end[]; 23 extern const char _binary_convertxml_abc_start[]; 24 extern const char _binary_convertxml_abc_end[]; 25 26 namespace OHOS::Xml { ConvertXmlConstructor(napi_env env,napi_callback_info info)27 static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info) 28 { 29 napi_value thisVar = nullptr; 30 void *data = nullptr; 31 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); 32 auto objectInfo = new ConvertXml(); 33 napi_wrap( 34 env, thisVar, objectInfo, 35 [](napi_env environment, void *data, void *hint) { 36 auto obj = reinterpret_cast<ConvertXml*>(data); 37 if (obj != nullptr) { 38 delete obj; 39 } 40 }, 41 nullptr, nullptr); 42 return thisVar; 43 } 44 Convert(napi_env env,napi_callback_info info)45 static napi_value Convert(napi_env env, napi_callback_info info) 46 { 47 napi_value thisVar = nullptr; 48 size_t requireMaxArgc = 2; // 2:MaxArgc 49 size_t requireMinArgc = 1; 50 size_t argc = 2; // 2:The number of parameters is 2 51 napi_value args[2] = {nullptr}; 52 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); 53 NAPI_ASSERT(env, argc <= requireMaxArgc, "Wrong number of arguments(Over)"); 54 NAPI_ASSERT(env, argc >= requireMinArgc, "Wrong number of arguments(Less)"); 55 std::string strXml; 56 napi_valuetype valuetype; 57 ConvertXml *object = nullptr; 58 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object))); 59 if (args[0] == nullptr) { 60 NAPI_CALL(env, napi_throw_error(env, "", "parameter is empty")); 61 } else { 62 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); 63 NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument typr. String expected."); 64 object->DealNapiStrValue(env, args[0], strXml); 65 } 66 if (args[1] != nullptr) { 67 object->DealOptions(env, args[1]); 68 } 69 napi_value result = object->Convert(env, strXml); 70 return result; 71 } 72 73 ConvertXmlInit(napi_env env,napi_value exports)74 static napi_value ConvertXmlInit(napi_env env, napi_value exports) 75 { 76 const char *convertXmlClassName = "ConvertXml"; 77 napi_value convertXmlClass = nullptr; 78 napi_property_descriptor convertXmlDesc[] = { 79 DECLARE_NAPI_FUNCTION("convert", Convert) 80 }; 81 NAPI_CALL(env, napi_define_class(env, convertXmlClassName, strlen(convertXmlClassName), ConvertXmlConstructor, 82 nullptr, sizeof(convertXmlDesc) / sizeof(convertXmlDesc[0]), convertXmlDesc, 83 &convertXmlClass)); 84 napi_property_descriptor desc[] = { 85 DECLARE_NAPI_PROPERTY("ConvertXml", convertXmlClass) 86 }; 87 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 88 return exports; 89 } 90 91 static napi_module convertXmlModule = { 92 .nm_version = 1, 93 .nm_flags = 0, 94 .nm_filename = nullptr, 95 .nm_register_func = ConvertXmlInit, 96 .nm_modname = "convertxml", 97 .nm_priv = reinterpret_cast<void*>(0), 98 .reserved = { 0 }, 99 }; 100 RegisterModule()101 extern "C" __attribute__((constructor)) void RegisterModule() 102 { 103 napi_module_register(&convertXmlModule); 104 } 105 106 extern "C" NAPI_convertxml_GetJSCode(const char ** buf,int * bufLen)107 __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char **buf, int *bufLen) 108 { 109 if (buf != nullptr) { 110 *buf = _binary_js_convertxml_js_start; 111 } 112 if (bufLen != nullptr) { 113 *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start; 114 } 115 } 116 extern "C" NAPI_convertxml_GetABCCode(const char ** buf,int * buflen)117 __attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen) 118 { 119 if (buf != nullptr) { 120 *buf = _binary_convertxml_abc_start; 121 } 122 if (buflen != nullptr) { 123 *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; 124 } 125 } 126 } // namespace OHOS::Xml 127