diff --git a/js_api_module/xml/BUILD.gn b/js_api_module/xml/BUILD.gn index d6853bd442ec027a84a2a780bfe49a2c3f84f5f0..ad9e7c3a98fad46770de33c7f6a93217c079e021 100755 --- a/js_api_module/xml/BUILD.gn +++ b/js_api_module/xml/BUILD.gn @@ -73,7 +73,6 @@ gen_obj("xml_abc") { xml_sources = [ "js_xml.cpp", - "js_xml_dynamic.cpp", "native_module_xml.cpp", ] diff --git a/js_api_module/xml/js_xml_dynamic.cpp b/js_api_module/xml/js_xml_dynamic.cpp deleted file mode 100644 index a5e65dea265ba59e83372cb5b9325eb505d1dab4..0000000000000000000000000000000000000000 --- a/js_api_module/xml/js_xml_dynamic.cpp +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "js_xml_dynamic.h" -#include "securec.h" -#include "tools/ets_error.h" - -using namespace OHOS::Tools; -namespace OHOS::xml { - napi_status XmlDynamicSerializer::DealNapiStrValue(napi_env env, const napi_value napiStr, std::string &result) - { - std::string buffer = ""; - size_t bufferSize = 0; - napi_status status = napi_get_value_string_utf8(env, napiStr, nullptr, -1, &bufferSize); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: can not get buffer size"); - return status; - } - buffer.reserve(bufferSize + 1); - buffer.resize(bufferSize); - if (bufferSize > 0) { - status = napi_get_value_string_utf8(env, napiStr, buffer.data(), bufferSize + 1, &bufferSize); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: can not get buffer value"); - return status; - } - } - if (buffer.data() != nullptr) { - result = buffer; - } - return status; - } - - void XmlDynamicSerializer::SplicNsp() - { - elementStack_[depth_ * 3] = elementStack_[(depth_ - 1) * 3]; // 3: number of args - elementStack_[depth_ * 3 + 1] = elementStack_[(depth_ - 1) * 3 + 1]; // 3: number of args - if (multNsp_[depth_ - 1].size() == 0) { - return; - } - if (type_ == "isAttri" || type_ == "isStart") { - for (int i = 0; i < curNspNum; ++i) { - out_.append(" xmlns:"); - out_.append(multNsp_[depth_ - 1][i * 2]); // 2: number of args - out_.append("=\""); - out_.append(multNsp_[depth_ - 1][i * 2 + 1]); // 2: number of args - out_.append("\""); - } - multNsp_[depth_ - 1].clear(); - curNspNum = 0; - } - } - - void XmlDynamicSerializer::NextItem() - { - out_.append("\r\n"); - for (size_t i = 0; i < depth_; i++) { - out_.append(" "); - } - } - - std::string XmlDynamicSerializer::Replace(std::string str, const std::string &subStr, const std::string &repStr) - { - size_t iPos = 0; - size_t subLen = subStr.length(); - size_t step = repStr.length(); - while ((iPos = str.find(subStr, iPos)) != std::string::npos) { - str = str.substr(0, iPos) + repStr + str.substr(iPos + subLen); - iPos += step; - } - return str; - } - - void XmlDynamicSerializer::BufferCopy() - { - if (strXml_.length() + out_.length() <= MAX_XML_LENGTH) { - strXml_.append(out_); - } else { - errorCode_ = ErrorCodeEnum::BUFFER_OVERFLOW; - xmlSerializerError_ = "The length has exceeded the upper limit"; - ErrorHelper::ThrowError(env_, errorCode_, xmlSerializerError_.c_str()); - } - } - - void XmlDynamicSerializer::SetDeclaration() - { - if (strXml_.length() > 0) { - xmlSerializerError_ = "illegal position for xml"; - errorCode_ = ErrorCodeEnum::ILLEGAL_POSITION; - ErrorHelper::ThrowError(env_, errorCode_, xmlSerializerError_.c_str()); - return; - } - out_ = ""; - out_.append(""); - type_ = "isDecl"; - BufferCopy(); - } - - void XmlDynamicSerializer::SetNamespace(std::string prefix, const std::string &nsTemp) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - elementStack_[depth_ * 3] = prefix; // 3: number of args - elementStack_[depth_ * 3 + 1] = nsTemp; // 3: number of args - multNsp_[depth_][curNspNum * 2] = elementStack_[depth_ * 3]; // 3: number of args 2: number of args - multNsp_[depth_][curNspNum * 2 + 1] = elementStack_[depth_ * 3 + 1]; // 3: number of args 2: number of args - ++curNspNum; - type_ = "isNsp"; - BufferCopy(); - } - - void XmlDynamicSerializer::StartElement(const std::string &name) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - if (type_ != "") { - NextItem(); - } - elementStack_[depth_ * 3 + 2] = name; // 3: number of args 2: number of args - out_.append("<"); - if (elementStack_[depth_ * 3] != "") { // 3: number of args - out_.append(elementStack_[depth_ * 3]); // 3: number of args - out_.append(":"); - } else if (depth_ != 0) { - if (elementStack_[(depth_ - 1) * 3] != "") { // 3: number of args - elementStack_[depth_ * 3] = elementStack_[(depth_ - 1) * 3]; // 3: number of args - out_.append(elementStack_[depth_ * 3]); // 3: number of args - out_.append(":"); - } - } - out_.append(elementStack_[depth_ * 3 + 2]); // 3: number of args 2: number of args - type_ = "isStart"; - ++depth_; - elementNum_++; - elementStack_.push_back(""); - elementStack_.push_back(""); - elementStack_.push_back(""); - BufferCopy(); - } - - void XmlDynamicSerializer::SetAttributes(const std::string &name, const std::string &value) - { - out_ = ""; - if (type_ != "isStart" && type_ != "isAttri") { - xmlSerializerError_ = "illegal position for xml"; - errorCode_ = ErrorCodeEnum::ILLEGAL_POSITION; - ErrorHelper::ThrowError(env_, errorCode_, xmlSerializerError_.c_str()); - return; - } - out_.append(" "); - out_.append(name); - out_.append("=\""); - WriteEscaped(value); - out_.append("\""); - type_ = "isAttri"; - BufferCopy(); - } - - void XmlDynamicSerializer::EndElement() - { - if (elementNum_ < 1) { - xmlSerializerError_ = "There is no match between the startElement and the endElement"; - errorCode_ = ErrorCodeEnum::NO_ELEMENT_MATCH; - ErrorHelper::ThrowError(env_, errorCode_, xmlSerializerError_.c_str()); - return; - } - elementNum_--; - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append("/>"); - type_ = "isEndTag"; - --depth_; - BufferCopy(); - return; - } - --depth_; - if (type_ != "isText") { - NextItem(); - } - out_.append(""); - BufferCopy(); - } - - void XmlDynamicSerializer::AddEmptyElement(std::string name) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - if (type_ != "") { - NextItem(); - } - out_.append("<"); - out_.append(name); - out_.append("/>"); - type_ = "isAddEmpElem"; - BufferCopy(); - } - - void XmlDynamicSerializer::SetText(const std::string &text) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - WriteEscaped(text); - type_ = "isText"; - BufferCopy(); - } - - void XmlDynamicSerializer::SetComment(const std::string &comment) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - if (type_ != "") { - NextItem(); - } - out_ += ""; - type_ = "isCom"; - BufferCopy(); - } - - void XmlDynamicSerializer::SetCData(std::string data) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - if (type_ != "") { - NextItem(); - } - data = Replace(data, "]]>", "]]]]>"); - out_ += ""; - type_ = "isCData"; - BufferCopy(); - } - - void XmlDynamicSerializer::SetDocType(const std::string &text) - { - out_ = ""; - if (type_ == "isStart" || type_ == "isAttri") { - SplicNsp(); - out_.append(">"); - } - if (type_ != "") { - NextItem(); - } - out_ += ""; - type_ = "isDocType"; - BufferCopy(); - } - - void XmlDynamicSerializer::WriteEscaped(std::string s) - { - size_t len = s.length(); - for (size_t i = 0; i < len; ++i) { - char c = s[i]; - switch (c) { - case '\'': - out_.append("'"); - break; - case '\"': - out_.append("""); - break; - case '&': - out_.append("&"); - break; - case '>': - out_.append(">"); - break; - case '<': - out_.append("<"); - break; - default: - out_ += c; - } - } - } - - size_t XmlDynamicSerializer::GetXmlBufferLength() - { - return strXml_.length(); - } - - bool XmlDynamicSerializer::GetXmlBuffer(void *data, uint32_t length) - { - if (data == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: GetXmlBuffer data is NULL"); - return false; - } - if (memcpy_s(data, length, reinterpret_cast(strXml_.data()), strXml_.length()) != EOK) { - HILOG_ERROR("XmlDynamicSerializer:: GetXmlBuffer copy xml buffer error"); - return false; - } - return true; - } -} // namespace OHOS::Xml \ No newline at end of file diff --git a/js_api_module/xml/js_xml_dynamic.h b/js_api_module/xml/js_xml_dynamic.h deleted file mode 100644 index 8b81f7414ec27f1dbf1c484f7353a02cb384197d..0000000000000000000000000000000000000000 --- a/js_api_module/xml/js_xml_dynamic.h +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef XML_JS_XML_DYNAMIC_H -#define XML_JS_XML_DYNAMIC_H - -#include -#include -#include -#include -#include -#include "napi/native_api.h" -#include "napi/native_node_api.h" -#include "native_engine/native_engine.h" -#include "tools/log.h" - -namespace OHOS::xml { - constexpr uint32_t MAX_XML_LENGTH = 100000; - constexpr uint32_t INIT_XML_LENGTH = 8 * 1024; - enum ErrorCodeEnum { - BUFFER_OVERFLOW = 10200062, - ILLEGAL_POSITION = 10200063, - NO_ELEMENT_MATCH = 10200065 - }; - - class XmlDynamicSerializer { - public: - - /** - * Constructor for XmlDynamicSerializer. - * - * @param encoding Is the encoding format of XML serializer. - */ - explicit XmlDynamicSerializer(napi_env env, - const std::string &encoding = "utf-8") :env_(env), encoding_(encoding) - { - strXml_.reserve(INIT_XML_LENGTH); - } - - /** - * XmlDynamicSerializer destructor. - */ - ~XmlDynamicSerializer() {} - - /** - * Set the Attributes method. - * - * @param name The parameter is the key value of the property. - * @param value The parameter is the value of the property. - */ - void SetAttributes(const std::string &name, const std::string &value); - - /** - * Writes an empty element. - * - * @param name The parameter is the element name of the empty element. - */ - void AddEmptyElement(std::string name); - - /** - * Set the Declaration method. - * - */ - void SetDeclaration(); - - /** - * Writes the element start tag with the given name. - * - * @param name The parameter is the element name of the current element. - */ - void StartElement(const std::string &name); - - /** - * Write element end tag. - * - */ - void EndElement(); - - /** - * The namespace into which the current element tag is written. - * - * @param prefix The parameter is the prefix of the current element and its children. - * @param nsTemp The parameter is the namespace of the current element and its children. - */ - void SetNamespace(std::string prefix, const std::string &nsTemp); - - /** - * Write the comment property. - * - * @param comment The parameter is the comment content of the current element. - */ - void SetComment(const std::string &comment); - - /** - * Write CDATA attributes. - * - * @param data The parameter is the content of the CDATA attribute. - */ - void SetCData(std::string data); - - /** - * Write text attributes. - * - * @param text The parameter is the content between the elements. - */ - void SetText(const std::string &text); - - /** - * Write DocType property. - * - * @param text The parameter is the content of the DocType property. - */ - void SetDocType(const std::string &text); - - /** - * Write an escape. - * - * @param s The parameter is the passed in escaped string. - */ - void WriteEscaped(std::string s); - - /** - * Set namespace splice. - */ - void SplicNsp(); - - /** - * Set next item. - */ - void NextItem(); - - /** - * Get XML serializer buffer length. - * - * @return XML serializer buffer length. - */ - size_t GetXmlBufferLength(); - - /** - * Get XML serializer buffer. - * - * @param data The parameter is the point of XML serializer buffer - * @param length The parameter is the length of XML serializer buffer. - * @return If the funtion copy buffer failed return false, else return true. - */ - bool GetXmlBuffer(void* data, uint32_t length); - - /** - * Process the value of the string passed by napi. - * - * @param env The parameter is NAPI environment variables. - * @param napiStr The parameter is pass parameters. - * @param result The parameter is return the processed value. - * @return Native API status. - */ - static napi_status DealNapiStrValue(napi_env env, const napi_value napiStr, std::string &result); - - friend class XmlTest; - - private: - void BufferCopy(); - std::string Replace(std::string str, const std::string &subStr, const std::string &repStr); - int32_t curNspNum {0}; - int32_t elementNum_ {0}; - size_t depth_ {0}; - ErrorCodeEnum errorCode_; - napi_env env_ {nullptr}; - std::string xmlSerializerError_ {""}; - std::string encoding_ {""}; - std::string out_ {""}; - std::string strXml_ {""}; - std::string type_ {""}; - std::vector elementStack_ = { "", "", ""}; - std::map> multNsp_; - }; -} // namespace OHOS::Xml -#endif // XML_JS_XML_DYNAMIC_H \ No newline at end of file diff --git a/js_api_module/xml/native_module_xml.cpp b/js_api_module/xml/native_module_xml.cpp index b5c92f6ae603bb3494bf28ac3d19479538016855..0bebd5de2b64779b0aa51de4290b00cb31f93d13 100644 --- a/js_api_module/xml/native_module_xml.cpp +++ b/js_api_module/xml/native_module_xml.cpp @@ -15,7 +15,6 @@ #include "native_module_xml.h" #include "js_xml.h" -#include "js_xml_dynamic.h" #include "tools/ets_error.h" extern const char _binary_js_xml_js_start[]; @@ -26,8 +25,6 @@ extern const char _binary_xml_abc_end[]; namespace OHOS::xml { using namespace OHOS::Tools; static const int32_t ERROR_CODE = 401; // 401 : the parameter type is incorrect -const int32_t ARGC_ONE = 1; // 1 : number of args -const int32_t ARGC_TWO = 2; // 2 : number of args static napi_value XmlSerializerConstructor(napi_env env, napi_callback_info info) { @@ -386,362 +383,6 @@ const int32_t ARGC_TWO = 2; // 2 : number of args return exports; } - static napi_value XmlDynamicSerializerConstructor(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - void *data = nullptr; - XmlDynamicSerializer *object = nullptr; - size_t argc = ARGC_ONE; - napi_value args[1] = { nullptr }; // 1:The number of parameters is 1 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, &data); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - std::string encoding; - status = XmlDynamicSerializer::DealNapiStrValue(env, args[0], encoding); - if (status == napi_ok) { - object = new (std::nothrow) XmlDynamicSerializer(env, encoding); - if (object == nullptr) { - HILOG_ERROR("XmlDynamicSerializerConstructor:: memory allocation failed, object is nullptr"); - return nullptr; - } - } - status = napi_wrap(env, thisVar, object, - [](napi_env environment, void *data, void *hint) { - auto obj = reinterpret_cast(data); - if (obj != nullptr) { - delete obj; - obj = nullptr; - } - }, nullptr, nullptr); - if (status != napi_ok && object != nullptr) { - HILOG_ERROR("XmlDynamicSerializerConstructor::napi_wrap failed"); - delete object; - object = nullptr; - } - return thisVar; - } - - static napi_value SetAttributesDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_TWO; - napi_value args[2] = { nullptr }; // 2:The number of parameters is 2 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string name; - std::string value; - status = XmlSerializer::DealNapiStrValue(env, args[0], name); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - status = XmlSerializer::DealNapiStrValue(env, args[1], value); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->SetAttributes(name, value); - return nullptr; - } - - static napi_value AddEmptyElementDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_ONE; - napi_value args[1] = { 0 }; // 1:The number of parameters is 1 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string name; - status = object->DealNapiStrValue(env, args[0], name); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->AddEmptyElement(name); - return nullptr; - } - - static napi_value SetDeclarationDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - object->SetDeclaration(); - return nullptr; - } - - static napi_value StartElementDynamic(napi_env env, napi_callback_info info) - { - size_t argc = ARGC_ONE; - napi_value args[1] = { nullptr }; // 1:The number of parameters is 1 - napi_value thisVar = nullptr; - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string name; - status = object->DealNapiStrValue(env, args[0], name); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->StartElement(name); - return nullptr; - } - - static napi_value EndElementDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - object->EndElement(); - return nullptr; - } - - static napi_value SetNamespaceDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_TWO; - napi_value args[2] = { nullptr }; // 2:The number of parameters is 2 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string prefix; - std::string nsTemp; - status = XmlDynamicSerializer::DealNapiStrValue(env, args[0], prefix); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - status = XmlDynamicSerializer::DealNapiStrValue(env, args[1], nsTemp); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->SetNamespace(prefix, nsTemp); - return nullptr; - } - - static napi_value SetCommentDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_ONE; - napi_value args[1] = { nullptr }; // 1:The number of parameters is 1 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string comment; - status = object->DealNapiStrValue(env, args[0], comment); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->SetComment(comment); - return nullptr; - } - - static napi_value SetCDataDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_ONE; - napi_value args[1] = { nullptr }; // 1:The number of parameters is 1 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string data; - status = XmlDynamicSerializer::DealNapiStrValue(env, args[0], data); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->SetCData(data); - return nullptr; - } - - static napi_value SetTextDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_ONE; - napi_value args[1] = { nullptr }; // 1:The number of parameters is 1 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string text; - status = XmlDynamicSerializer::DealNapiStrValue(env, args[0], text); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->SetText(text); - return nullptr; - } - - static napi_value SetDocTypeDynamic(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - size_t argc = ARGC_ONE; - napi_value args[1] = { nullptr }; // 1:The number of parameters is 1 - napi_status status = napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - std::string text; - status = XmlDynamicSerializer::DealNapiStrValue(env, args[0], text); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: get string from native failed!"); - return nullptr; - } - object->SetDocType(text); - return nullptr; - } - - static napi_value GetOutput(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: napi_get_cb_info failed!"); - return nullptr; - } - XmlDynamicSerializer *object = nullptr; - status = napi_unwrap(env, thisVar, reinterpret_cast(&object)); - if (status != napi_ok || object == nullptr) { - HILOG_ERROR("XmlDynamicSerializer:: napi_unwrap failed!"); - return nullptr; - } - napi_value arrBuffer = nullptr; - void* arrBufferPtr = nullptr; - size_t arrBufferSize = object->GetXmlBufferLength(); - status = napi_create_arraybuffer(env, arrBufferSize, &arrBufferPtr, &arrBuffer); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: create arraybuffer failed!"); - return nullptr; - } - if (arrBufferSize == 0) { - return arrBuffer; - } - bool result = object->GetXmlBuffer(arrBufferPtr, arrBufferSize); - if (!result) { - return nullptr; - } - return arrBuffer; - } - - napi_value XmlDynamicSerializerInit(napi_env env, napi_value exports) - { - const char *xmlSerializerClass = "XmlDynamicSerializer"; - napi_value xmlClass = nullptr; - napi_property_descriptor xmlDesc[] = { - DECLARE_NAPI_FUNCTION("setAttributes", SetAttributesDynamic), - DECLARE_NAPI_FUNCTION("addEmptyElement", AddEmptyElementDynamic), - DECLARE_NAPI_FUNCTION("setDeclaration", SetDeclarationDynamic), - DECLARE_NAPI_FUNCTION("startElement", StartElementDynamic), - DECLARE_NAPI_FUNCTION("endElement", EndElementDynamic), - DECLARE_NAPI_FUNCTION("setNamespace", SetNamespaceDynamic), - DECLARE_NAPI_FUNCTION("setComment", SetCommentDynamic), - DECLARE_NAPI_FUNCTION("setCDATA", SetCDataDynamic), - DECLARE_NAPI_FUNCTION("setText", SetTextDynamic), - DECLARE_NAPI_FUNCTION("setDocType", SetDocTypeDynamic), - DECLARE_NAPI_FUNCTION("getOutput", GetOutput) - }; - napi_status status = napi_define_class(env, xmlSerializerClass, strlen(xmlSerializerClass), - XmlDynamicSerializerConstructor, nullptr, - sizeof(xmlDesc) / sizeof(xmlDesc[0]), xmlDesc, &xmlClass); - if (status != napi_ok) { - HILOG_ERROR("XmlDynamicSerializer:: XmlDynamicSerializer init failed!"); - return nullptr; - } - napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("XmlDynamicSerializer", xmlClass) - }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - return exports; - } - static napi_value Parse(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -818,7 +459,6 @@ const int32_t ARGC_TWO = 2; // 2 : number of args { XmlSerializerInit(env, exports); XmlPullParserInit(env, exports); - XmlDynamicSerializerInit(env, exports); return exports; } diff --git a/js_api_module/xml/native_module_xml.h b/js_api_module/xml/native_module_xml.h index 423feef51aa39a47c2b4f07c3d4175d2c81713ea..5757bc19ec817fceeed6cd55d9db39d49c508dbe 100644 --- a/js_api_module/xml/native_module_xml.h +++ b/js_api_module/xml/native_module_xml.h @@ -21,6 +21,5 @@ namespace OHOS::xml { napi_value XmlSerializerInit(napi_env env, napi_value exports); napi_value XmlPullParserInit(napi_env env, napi_value exports); -napi_value XmlDynamicSerializerInit(napi_env env, napi_value exports); } #endif // NATIVE_MODULE_XML_H \ No newline at end of file diff --git a/js_api_module/xml/src/js_xml.ts b/js_api_module/xml/src/js_xml.ts index 3866079d9267907c400f5613f9f50e5136864e2d..0effe2dc8e991a24b31a91f5102eacab5af24467 100644 --- a/js_api_module/xml/src/js_xml.ts +++ b/js_api_module/xml/src/js_xml.ts @@ -35,38 +35,18 @@ interface NativeXMLSerializer { XmlSerializerError(): string; } -interface NativeXMLDynamicSerializer { - new(strEncoding?: string): NativeXMLDynamicSerializer; - setAttributes(name: string, value: string): void; - addEmptyElement(name: string): void; - setDeclaration(): void; - startElement(name: string): void; - endElement(): void; - setNamespace(prefix: string, namespace: string): void; - setComment(text: string): void; - setCDATA(text: string): void; - setText(text: string): void; - setDocType(text: string): void; - getOutput(): ArrayBuffer | undefined; -} - interface Xml { XmlSerializer: NativeXMLSerializer; XmlPullParser: NativeXmlPullParser; - XmlDynamicSerializer: NativeXMLDynamicSerializer; } - -const ARGUMENT_LENGTH_ONE = 1; const ARGUMENT_LENGTH_TWO = 2; const TypeErrorCode = 401; -const EmptyErrorCode = 10200064; -const EncodingErrorCode = 10200066; class BusinessError extends Error { code: number; - constructor(msg: string, errCode?: number) { + constructor(msg: string) { super(msg); this.name = 'BusinessError'; - this.code = errCode ?? TypeErrorCode; + this.code = TypeErrorCode; } } @@ -235,124 +215,6 @@ class XmlSerializer { } } -class XmlDynamicSerializer { - xmlSerializerClass: NativeXMLDynamicSerializer; - constructor(encoding?: string) { - let input: string = 'utf-8'; - if (arguments.length === ARGUMENT_LENGTH_ONE) { - if (typeof encoding !== 'string') { - throw new BusinessError(`Parameter error.The type of ${encoding} must be string`); - } - if (encoding.toLowerCase() !== 'utf-8') { - throw new BusinessError('Parameter error.Just support utf-8', EncodingErrorCode); - } - } - this.xmlSerializerClass = new XML.XmlDynamicSerializer(input); - } - - getOutput(): ArrayBuffer { - let result = this.xmlSerializerClass.getOutput(); - if (result === undefined) { - return new ArrayBuffer(0); - } - return result; - } - - setAttributes(name: string, value: string): void { - if (typeof name !== 'string') { - throw new BusinessError(`Parameter error.The type of ${name} must be string`); - } - if (name.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - if (typeof value !== 'string') { - throw new BusinessError(`Parameter error.The type of ${value} must be string`); - } - this.xmlSerializerClass.setAttributes(name, value); - } - - addEmptyElement(name: string): void { - if (typeof name !== 'string') { - throw new BusinessError(`Parameter error.The type of ${name} must be string`); - } - if (name.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.addEmptyElement(name); - } - - setDeclaration(): void { - this.xmlSerializerClass.setDeclaration(); - } - - startElement(name: string): void { - if (typeof name !== 'string') { - throw new BusinessError(`Parameter error.The type of ${name} must be string`); - } - if (name.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.startElement(name); - } - - endElement(): void { - this.xmlSerializerClass.endElement(); - } - - setNamespace(prefix: string, ns: string): void { - if (typeof prefix !== 'string') { - throw new BusinessError(`Parameter error.The type of ${prefix} must be string`); - } - if (typeof ns !== 'string') { - throw new BusinessError(`Parameter error.The type of ${ns} must be string`); - } - if (prefix.length === 0 || ns.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.setNamespace(prefix, ns); - } - - setComment(text: string): void { - if (typeof text !== 'string') { - throw new BusinessError(`Parameter error.The type of ${text} must be string`); - } - if (text.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.setComment(text); - } - - setCdata(text: string): void { - if (typeof text !== 'string') { - throw new BusinessError(`Parameter error.The type of ${text} must be string`); - } - if (text.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.setCDATA(text); - } - - setText(text: string): void { - if (typeof text !== 'string') { - throw new BusinessError(`Parameter error.The type of ${text} must be string`); - } - if (text.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.setText(text); - } - - setDocType(text: string): void { - if (typeof text !== 'string') { - throw new BusinessError(`Parameter error.The type of ${text} must be string`); - } - if (text.length === 0) { - throw new BusinessError('Parameter error. Parameter cannot be empty', EmptyErrorCode); - } - this.xmlSerializerClass.setDocType(text); - } -} - class XmlPullParser { xmlPullParserClass: NativeXmlPullParser; constructor(obj: object, inputStr: string) { @@ -417,6 +279,5 @@ enum EventType { export default { XmlSerializer: XmlSerializer, XmlPullParser: XmlPullParser, - XmlDynamicSerializer: XmlDynamicSerializer, EventType, }; diff --git a/js_api_module/xml/test/test_xml.cpp b/js_api_module/xml/test/test_xml.cpp index 9810476dbcfe9f2a778ce5db60f192aeeed8d269..897d635bb81f2e3d66a46c578dad1971c5d4a683 100644 --- a/js_api_module/xml/test/test_xml.cpp +++ b/js_api_module/xml/test/test_xml.cpp @@ -14,7 +14,6 @@ */ #include "test_xml.h" -#include "js_xml_dynamic.h" #include "test.h" #include "napi/native_api.h" @@ -24,7 +23,6 @@ #include "native_module_xml.h" #include "securec.h" #include "tools/log.h" -#include using namespace OHOS::xml; @@ -3205,207 +3203,4 @@ HWTEST_F(NativeEngineTest, SkipCharFunction001, testing::ext::TestSize.Level0) OHOS::xml::XmlTest testXml; int output = testXml.SkipCharFunction(env, xml, expected); ASSERT_EQ(output, 63); -} - -/* @tc.name: StartElementDynamicTest001 - * @tc.desc: Test whether write a elemnet start tag with the given name successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, StartElementDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.StartElement("note1"); - xmlSerializer.StartElement("note2"); - xmlSerializer.EndElement(); - xmlSerializer.StartElement("note3"); - xmlSerializer.EndElement(); - xmlSerializer.EndElement(); - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), "\r\n \r\n \r\n"); -} - -/* @tc.name: SetAttributesDynamicTest001 - * @tc.desc: Test whether write an attribute successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetAttributesDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.StartElement("note"); - xmlSerializer.SetAttributes("importance1", "high1"); - xmlSerializer.SetAttributes("importance2", "high2"); - xmlSerializer.SetAttributes("importance3", "high3"); - xmlSerializer.SetAttributes("importance4", "high4"); - xmlSerializer.SetAttributes("importance5", "high5"); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - std::string strPrior = ""; - std::string strEnd = strPrior + strBack; - ASSERT_STREQ(pBuffer.get(), strEnd.c_str()); -} - -/* @tc.name: AddEmptyElementDynamicTest001 - * @tc.desc: Test whether add an empty element successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, AddEmptyElementDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.StartElement("note"); - xmlSerializer.AddEmptyElement("c"); - xmlSerializer.AddEmptyElement("d"); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), "\r\n \r\n \r\n"); -} - -/* @tc.name: SetDeclarationDynamicTest001 - * @tc.desc: Test whether write xml declaration with encoding successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetDeclarationDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.SetDeclaration(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), ""); -} - -/* @tc.name: EndElementDynamicTest001 - * @tc.desc: Test whether write end tag of the element successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, EndElementDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.StartElement("note2"); - xmlSerializer.SetAttributes("importance", "high"); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), ""); -} - -/* @tc.name: SetNamespaceDynamicTest001 - * @tc.desc: Test whether write the namespace of the current element tag successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetNamespaceDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.SetDeclaration(); - xmlSerializer.SetNamespace("h", "http://www.w3.org/TR/html4/"); - xmlSerializer.StartElement("note1"); - xmlSerializer.StartElement("note2"); - xmlSerializer.EndElement(); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - std::string strPrior = "\r\n"; - std::string strBack = "\r\n \r\n"; - std::string strEnd = strPrior + strBack; - ASSERT_STREQ(pBuffer.get(), strEnd.c_str()); -} - -/* @tc.name: SetCommentDynamicTest001 - * @tc.desc: Test write the comment successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetCommentDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.SetComment("Hello, World!"); - xmlSerializer.StartElement("note"); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), "\r\n"); -} - -/* @tc.name: SetCDATADynamicTest001 - * @tc.desc: Test whether Writes the CDATA successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetCDATADynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.SetCData("]]>"); - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), "]]>"); -} - -/* @tc.name: SetTextDynamicTest001 - * @tc.desc: Test whether Writes the text successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetTextDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.StartElement("note"); - xmlSerializer.SetAttributes("importance", "high"); - xmlSerializer.SetText("Happy5"); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), "Happy5"); -} - -/* @tc.name: SetDocTypeDynamicTest001 - * @tc.desc: Test whether rites the DOCTYPE successfully. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, SetDocTypeDynamicTest001, testing::ext::TestSize.Level0) -{ - napi_env env = (napi_env)engine_; - OHOS::xml::XmlDynamicSerializer xmlSerializer(env); - xmlSerializer.StartElement("note"); - xmlSerializer.SetDocType("root SYSTEM \"http://www.test.org/test.dtd\""); - xmlSerializer.EndElement(); - - size_t size = xmlSerializer.GetXmlBufferLength() + 1; // 1: buffer size add one. - auto pBuffer = std::make_unique(size); - memset_s(pBuffer.get(), size, 0, size); - xmlSerializer.GetXmlBuffer(pBuffer.get(), size); - ASSERT_STREQ(pBuffer.get(), - "\r\n \r\n"); } \ No newline at end of file