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 "tools/ets_error.h" 18 #include "tools/log.h" 19 #include "js_convertxml.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 { 27 using namespace OHOS::Tools; 28 static const int32_t ERROR_CODE = 401; // 401 : the parameter type is incorrect 29 ConvertXmlConstructor(napi_env env,napi_callback_info info)30 static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info) 31 { 32 napi_value thisVar = nullptr; 33 void *data = nullptr; 34 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); 35 auto objectInfo = new ConvertXml(env); 36 napi_status status = napi_wrap(env, thisVar, objectInfo, 37 [](napi_env environment, void *data, void *hint) { 38 auto obj = reinterpret_cast<ConvertXml*>(data); 39 if (obj != nullptr) { 40 delete obj; 41 obj = nullptr; 42 } 43 }, nullptr, nullptr); 44 if (status != napi_ok && objectInfo != nullptr) { 45 HILOG_ERROR("ConvertXmlConstructor:: napi_wrap failed"); 46 delete objectInfo; 47 objectInfo = nullptr; 48 } 49 return thisVar; 50 } 51 Convert(napi_env env,napi_callback_info info)52 static napi_value Convert(napi_env env, napi_callback_info info) 53 { 54 napi_value thisVar = nullptr; 55 size_t requireMaxArgc = 2; // 2:MaxArgc 56 size_t requireMinArgc = 1; 57 size_t argc = 2; // 2:The number of parameters is 2 58 napi_value args[2] = { nullptr }; 59 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); 60 NAPI_ASSERT(env, argc <= requireMaxArgc, "Wrong number of arguments(Over)"); 61 NAPI_ASSERT(env, argc >= requireMinArgc, "Wrong number of arguments(Less)"); 62 std::string strXml; 63 napi_valuetype valuetype; 64 ConvertXml *object = nullptr; 65 NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object))); 66 67 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype)); 68 NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type: string expected."); 69 object->DealNapiStrValue(env, args[0], strXml); 70 if (argc > 1) { 71 object->DealOptions(env, args[1], true); 72 } 73 napi_value result = object->Convert(env, strXml, true); 74 return result; 75 } 76 FastConvert(napi_env env,napi_callback_info info)77 static napi_value FastConvert(napi_env env, napi_callback_info info) 78 { 79 napi_value thisVar = nullptr; 80 size_t argc = 2; // 2:The number of parameters 81 napi_value args[2] = { nullptr }; // 2:The number of parameters 82 napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); 83 std::string strXml; 84 ConvertXml *convertxml = nullptr; 85 napi_unwrap(env, thisVar, reinterpret_cast<void**>(&convertxml)); 86 if (convertxml == nullptr) { 87 ErrorHelper::ThrowError(env, ERROR_CODE, "Parameter error. Parameter verification failed."); 88 return nullptr; 89 } 90 convertxml->DealNapiStrValue(env, args[0], strXml); 91 if (argc > 1) { 92 convertxml->DealOptions(env, args[1], false); 93 } 94 return convertxml->Convert(env, strXml, false); 95 } 96 ConvertXmlInit(napi_env env,napi_value exports)97 napi_value ConvertXmlInit(napi_env env, napi_value exports) 98 { 99 const char *convertXmlClassName = "ConvertXml"; 100 napi_value convertXmlClass = nullptr; 101 napi_property_descriptor convertXmlDesc[] = { 102 DECLARE_NAPI_FUNCTION("convert", Convert), 103 DECLARE_NAPI_FUNCTION("fastConvertToJSObject", FastConvert) 104 }; 105 NAPI_CALL(env, napi_define_class(env, convertXmlClassName, strlen(convertXmlClassName), ConvertXmlConstructor, 106 nullptr, sizeof(convertXmlDesc) / sizeof(convertXmlDesc[0]), convertXmlDesc, 107 &convertXmlClass)); 108 napi_property_descriptor desc[] = { 109 DECLARE_NAPI_PROPERTY("ConvertXml", convertXmlClass) 110 }; 111 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 112 return exports; 113 } 114 115 extern "C" NAPI_convertxml_GetJSCode(const char ** buf,int * bufLen)116 __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char **buf, int *bufLen) 117 { 118 if (buf != nullptr) { 119 *buf = _binary_js_convertxml_js_start; 120 } 121 if (bufLen != nullptr) { 122 *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start; 123 } 124 } 125 extern "C" NAPI_convertxml_GetABCCode(const char ** buf,int * buflen)126 __attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen) 127 { 128 if (buf != nullptr) { 129 *buf = _binary_convertxml_abc_start; 130 } 131 if (buflen != nullptr) { 132 *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; 133 } 134 } 135 136 static napi_module_with_js convertXmlModule = { 137 .nm_version = 1, 138 .nm_flags = 0, 139 .nm_filename = nullptr, 140 .nm_register_func = ConvertXmlInit, 141 .nm_modname = "convertxml", 142 .nm_priv = reinterpret_cast<void*>(0), 143 .nm_get_abc_code = NAPI_convertxml_GetABCCode, 144 .nm_get_js_code = NAPI_convertxml_GetJSCode, 145 }; 146 ConvertXMLRegisterModule(void)147 extern "C" __attribute__((constructor)) void ConvertXMLRegisterModule(void) 148 { 149 napi_module_with_js_register(&convertXmlModule); 150 } 151 } // namespace OHOS::Xml 152