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 return nullptr; 62 } else { 63 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); 64 NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument typr. String expected."); 65 object->DealNapiStrValue(env, args[0], strXml); 66 } 67 if (args[1] != nullptr) { 68 object->DealOptions(env, args[1]); 69 } 70 napi_value result = object->Convert(env, strXml); 71 return result; 72 } 73 74 ConvertXmlInit(napi_env env,napi_value exports)75 static napi_value ConvertXmlInit(napi_env env, napi_value exports) 76 { 77 const char *convertXmlClassName = "ConvertXml"; 78 napi_value convertXmlClass = nullptr; 79 napi_property_descriptor convertXmlDesc[] = { 80 DECLARE_NAPI_FUNCTION("convert", Convert) 81 }; 82 NAPI_CALL(env, napi_define_class(env, convertXmlClassName, strlen(convertXmlClassName), ConvertXmlConstructor, 83 nullptr, sizeof(convertXmlDesc) / sizeof(convertXmlDesc[0]), convertXmlDesc, 84 &convertXmlClass)); 85 napi_property_descriptor desc[] = { 86 DECLARE_NAPI_PROPERTY("ConvertXml", convertXmlClass) 87 }; 88 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 89 return exports; 90 } 91 92 static napi_module convertXmlModule = { 93 .nm_version = 1, 94 .nm_flags = 0, 95 .nm_filename = nullptr, 96 .nm_register_func = ConvertXmlInit, 97 .nm_modname = "convertxml", 98 .nm_priv = reinterpret_cast<void*>(0), 99 .reserved = { 0 }, 100 }; 101 RegisterModule()102 extern "C" __attribute__((constructor)) void RegisterModule() 103 { 104 napi_module_register(&convertXmlModule); 105 } 106 107 extern "C" NAPI_convertxml_GetJSCode(const char ** buf,int * bufLen)108 __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char **buf, int *bufLen) 109 { 110 if (buf != nullptr) { 111 *buf = _binary_js_convertxml_js_start; 112 } 113 if (bufLen != nullptr) { 114 *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start; 115 } 116 } 117 extern "C" NAPI_convertxml_GetABCCode(const char ** buf,int * buflen)118 __attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen) 119 { 120 if (buf != nullptr) { 121 *buf = _binary_convertxml_abc_start; 122 } 123 if (buflen != nullptr) { 124 *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; 125 } 126 } 127 } // namespace OHOS::Xml 128