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/log.h"
18#include "js_convertxml.h"
19
20extern const char _binary_js_convertxml_js_start[];
21extern const char _binary_js_convertxml_js_end[];
22extern const char _binary_convertxml_abc_start[];
23extern const char _binary_convertxml_abc_end[];
24
25namespace OHOS::Xml {
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 (std::nothrow) ConvertXml(env);
32        if (objectInfo == nullptr) {
33            HILOG_ERROR("ConvertXmlConstructor::objectInfo is nullptr");
34            return nullptr;
35        }
36        napi_wrap(
37            env, thisVar, objectInfo,
38            [](napi_env environment, void *data, void *hint) {
39                auto obj = reinterpret_cast<ConvertXml*>(data);
40                if (obj != nullptr) {
41                    delete obj;
42                }
43            },
44            nullptr, nullptr);
45        return thisVar;
46    }
47
48    static napi_value Convert(napi_env env, napi_callback_info info)
49    {
50        napi_value thisVar = nullptr;
51        size_t requireMaxArgc = 2; // 2:MaxArgc
52        size_t requireMinArgc = 1;
53        size_t argc = 2; // 2:The number of parameters is 2
54        napi_value args[2] = { nullptr };
55        NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
56        NAPI_ASSERT(env, argc <= requireMaxArgc, "Wrong number of arguments(Over)");
57        NAPI_ASSERT(env, argc >= requireMinArgc, "Wrong number of arguments(Less)");
58        std::string strXml;
59        napi_valuetype valuetype;
60        ConvertXml *object = nullptr;
61        NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
62
63        NAPI_CALL(env, napi_typeof(env, args[0], &valuetype));
64        NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type: string expected.");
65        object->DealNapiStrValue(env, args[0], strXml);
66
67        if (argc > 1) {
68            object->DealOptions(env, args[1]);
69        }
70        napi_value result = object->Convert(env, strXml);
71        return result;
72    }
73
74    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    extern "C"
92    __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char **buf, int *bufLen)
93    {
94        if (buf != nullptr) {
95            *buf = _binary_js_convertxml_js_start;
96        }
97        if (bufLen != nullptr) {
98            *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start;
99        }
100    }
101    extern "C"
102    __attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char** buf, int* buflen)
103    {
104        if (buf != nullptr) {
105            *buf = _binary_convertxml_abc_start;
106        }
107        if (buflen != nullptr) {
108            *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start;
109        }
110    }
111
112    static napi_module_with_js convertXmlModule = {
113        .nm_version = 1,
114        .nm_flags = 0,
115        .nm_filename = nullptr,
116        .nm_register_func = ConvertXmlInit,
117        .nm_modname = "convertxml",
118        .nm_priv = reinterpret_cast<void*>(0),
119        .nm_get_abc_code = NAPI_convertxml_GetABCCode,
120        .nm_get_js_code = NAPI_convertxml_GetJSCode,
121    };
122
123    extern "C" __attribute__((constructor)) void ConvertXMLRegisterModule(void)
124    {
125        napi_module_with_js_register(&convertXmlModule);
126    }
127} // namespace OHOS::Xml
128