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