14d6c458bSopenharmony_ci /*
24d6c458bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34d6c458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44d6c458bSopenharmony_ci * you may not use this file except in compliance with the License.
54d6c458bSopenharmony_ci * You may obtain a copy of the License at
64d6c458bSopenharmony_ci *
74d6c458bSopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
84d6c458bSopenharmony_ci *
94d6c458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104d6c458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114d6c458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124d6c458bSopenharmony_ci * See the License for the specific language governing permissions and
134d6c458bSopenharmony_ci * limitations under the License.
144d6c458bSopenharmony_ci */
154d6c458bSopenharmony_ci
164d6c458bSopenharmony_ci#include "native_module_convertxml.h"
174d6c458bSopenharmony_ci#include "tools/log.h"
184d6c458bSopenharmony_ci#include "js_convertxml.h"
194d6c458bSopenharmony_ci
204d6c458bSopenharmony_ciextern const char _binary_js_convertxml_js_start[];
214d6c458bSopenharmony_ciextern const char _binary_js_convertxml_js_end[];
224d6c458bSopenharmony_ciextern const char _binary_convertxml_abc_start[];
234d6c458bSopenharmony_ciextern const char _binary_convertxml_abc_end[];
244d6c458bSopenharmony_ci
254d6c458bSopenharmony_cinamespace OHOS::Xml {
264d6c458bSopenharmony_ci    static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info)
274d6c458bSopenharmony_ci    {
284d6c458bSopenharmony_ci        napi_value thisVar = nullptr;
294d6c458bSopenharmony_ci        void *data = nullptr;
304d6c458bSopenharmony_ci        napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
314d6c458bSopenharmony_ci        auto objectInfo = new (std::nothrow) ConvertXml(env);
324d6c458bSopenharmony_ci        if (objectInfo == nullptr) {
334d6c458bSopenharmony_ci            HILOG_ERROR("ConvertXmlConstructor::objectInfo is nullptr");
344d6c458bSopenharmony_ci            return nullptr;
354d6c458bSopenharmony_ci        }
364d6c458bSopenharmony_ci        napi_wrap(
374d6c458bSopenharmony_ci            env, thisVar, objectInfo,
384d6c458bSopenharmony_ci            [](napi_env environment, void *data, void *hint) {
394d6c458bSopenharmony_ci                auto obj = reinterpret_cast<ConvertXml*>(data);
404d6c458bSopenharmony_ci                if (obj != nullptr) {
414d6c458bSopenharmony_ci                    delete obj;
424d6c458bSopenharmony_ci                }
434d6c458bSopenharmony_ci            },
444d6c458bSopenharmony_ci            nullptr, nullptr);
454d6c458bSopenharmony_ci        return thisVar;
464d6c458bSopenharmony_ci    }
474d6c458bSopenharmony_ci
484d6c458bSopenharmony_ci    static napi_value Convert(napi_env env, napi_callback_info info)
494d6c458bSopenharmony_ci    {
504d6c458bSopenharmony_ci        napi_value thisVar = nullptr;
514d6c458bSopenharmony_ci        size_t requireMaxArgc = 2; // 2:MaxArgc
524d6c458bSopenharmony_ci        size_t requireMinArgc = 1;
534d6c458bSopenharmony_ci        size_t argc = 2; // 2:The number of parameters is 2
544d6c458bSopenharmony_ci        napi_value args[2] = { nullptr };
554d6c458bSopenharmony_ci        NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
564d6c458bSopenharmony_ci        NAPI_ASSERT(env, argc <= requireMaxArgc, "Wrong number of arguments(Over)");
574d6c458bSopenharmony_ci        NAPI_ASSERT(env, argc >= requireMinArgc, "Wrong number of arguments(Less)");
584d6c458bSopenharmony_ci        std::string strXml;
594d6c458bSopenharmony_ci        napi_valuetype valuetype;
604d6c458bSopenharmony_ci        ConvertXml *object = nullptr;
614d6c458bSopenharmony_ci        NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
624d6c458bSopenharmony_ci
634d6c458bSopenharmony_ci        NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
644d6c458bSopenharmony_ci        NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type: string expected.");
654d6c458bSopenharmony_ci        object->DealNapiStrValue(env, args[0], strXml);
664d6c458bSopenharmony_ci
674d6c458bSopenharmony_ci        if (argc > 1) {
684d6c458bSopenharmony_ci            object->DealOptions(env, args[1]);
694d6c458bSopenharmony_ci        }
704d6c458bSopenharmony_ci        napi_value result = object->Convert(env, strXml);
714d6c458bSopenharmony_ci        return result;
724d6c458bSopenharmony_ci    }
734d6c458bSopenharmony_ci
744d6c458bSopenharmony_ci    napi_value ConvertXmlInit(napi_env env, napi_value exports)
754d6c458bSopenharmony_ci    {
764d6c458bSopenharmony_ci        const char *convertXmlClassName = "ConvertXml";
774d6c458bSopenharmony_ci        napi_value convertXmlClass = nullptr;
784d6c458bSopenharmony_ci        napi_property_descriptor convertXmlDesc[] = {
794d6c458bSopenharmony_ci            DECLARE_NAPI_FUNCTION("convert", Convert)
804d6c458bSopenharmony_ci        };
814d6c458bSopenharmony_ci        NAPI_CALL(env, napi_define_class(env, convertXmlClassName, strlen(convertXmlClassName), ConvertXmlConstructor,
824d6c458bSopenharmony_ci                                         nullptr, sizeof(convertXmlDesc) / sizeof(convertXmlDesc[0]), convertXmlDesc,
834d6c458bSopenharmony_ci                                         &convertXmlClass));
844d6c458bSopenharmony_ci        napi_property_descriptor desc[] = {
854d6c458bSopenharmony_ci            DECLARE_NAPI_PROPERTY("ConvertXml", convertXmlClass)
864d6c458bSopenharmony_ci        };
874d6c458bSopenharmony_ci        NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
884d6c458bSopenharmony_ci        return exports;
894d6c458bSopenharmony_ci    }
904d6c458bSopenharmony_ci
914d6c458bSopenharmony_ci    extern "C"
924d6c458bSopenharmony_ci    __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char **buf, int *bufLen)
934d6c458bSopenharmony_ci    {
944d6c458bSopenharmony_ci        if (buf != nullptr) {
954d6c458bSopenharmony_ci            *buf = _binary_js_convertxml_js_start;
964d6c458bSopenharmony_ci        }
974d6c458bSopenharmony_ci        if (bufLen != nullptr) {
984d6c458bSopenharmony_ci            *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start;
994d6c458bSopenharmony_ci        }
1004d6c458bSopenharmony_ci    }
1014d6c458bSopenharmony_ci    extern "C"
1024d6c458bSopenharmony_ci    __attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen)
1034d6c458bSopenharmony_ci    {
1044d6c458bSopenharmony_ci        if (buf != nullptr) {
1054d6c458bSopenharmony_ci            *buf = _binary_convertxml_abc_start;
1064d6c458bSopenharmony_ci        }
1074d6c458bSopenharmony_ci        if (buflen != nullptr) {
1084d6c458bSopenharmony_ci            *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start;
1094d6c458bSopenharmony_ci        }
1104d6c458bSopenharmony_ci    }
1114d6c458bSopenharmony_ci
1124d6c458bSopenharmony_ci    static napi_module_with_js convertXmlModule = {
1134d6c458bSopenharmony_ci        .nm_version = 1,
1144d6c458bSopenharmony_ci        .nm_flags = 0,
1154d6c458bSopenharmony_ci        .nm_filename = nullptr,
1164d6c458bSopenharmony_ci        .nm_register_func = ConvertXmlInit,
1174d6c458bSopenharmony_ci        .nm_modname = "convertxml",
1184d6c458bSopenharmony_ci        .nm_priv = reinterpret_cast<void*>(0),
1194d6c458bSopenharmony_ci        .nm_get_abc_code = NAPI_convertxml_GetABCCode,
1204d6c458bSopenharmony_ci        .nm_get_js_code = NAPI_convertxml_GetJSCode,
1214d6c458bSopenharmony_ci    };
1224d6c458bSopenharmony_ci
1234d6c458bSopenharmony_ci    extern "C" __attribute__((constructor)) void ConvertXMLRegisterModule(void)
1244d6c458bSopenharmony_ci    {
1254d6c458bSopenharmony_ci        napi_module_with_js_register(&convertXmlModule);
1264d6c458bSopenharmony_ci    }
1274d6c458bSopenharmony_ci} // namespace OHOS::Xml
128