diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index 9e0b6aa..f71763e 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -605,4 +605,4 @@ namespace OHOS::Xml { curNode = curNode->next; } } -} // namespace \ No newline at end of file +} // namespace OHOS::Xml diff --git a/convertxml/js_convertxml.h b/convertxml/js_convertxml.h index ca3e71f..e4d6056 100755 --- a/convertxml/js_convertxml.h +++ b/convertxml/js_convertxml.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_CCRUNTIME_CONVERT_XML_CLASS_H -#define FOUNDATION_ACE_CCRUNTIME_CONVERT_XML_CLASS_H +#ifndef CONVERTXML_JS_CONVERTXML_H_ +#define CONVERTXML_JS_CONVERTXML_H_ #include #include @@ -70,18 +70,48 @@ namespace OHOS::Xml { class ConvertXml { public: + /** + * To convert XML text to JavaScript object. + * + * @param env NAPI environment parameters. + */ explicit ConvertXml(napi_env env); + + /** + * The destructor of the ConvertXml. + */ virtual ~ConvertXml() {} + + /** + * To convert XML text to JavaScript object. + * + * @param strXml A string of XML text. + */ + napi_value Convert(std::string strXml); + + /** + * Converts the string of js to string of C++. + * + * @param napi_StrValue JS layer incoming stringing. + * @param result The C++ layer accepts stringing. + */ + napi_status DealNapiStrValue(const napi_value napi_StrValue, std::string &result) const; + + /** + * Handles user input of optional information + * + * @param napiObj Get the option parameter from js to the napi layer + */ + void DealOptions(const napi_value napiObj); + + private: void SetAttributes(xmlNodePtr curNode, const napi_value &elementsObject) const; void SetXmlElementType(xmlNodePtr curNode, const napi_value &elementsObject, bool &bFlag) const; void SetNodeInfo(xmlNodePtr curNode, const napi_value &elementsObject) const; void SetEndInfo(xmlNodePtr curNode, const napi_value &elementsObject, bool &bFlag) const; void GetXMLInfo(xmlNodePtr curNode, const napi_value &object, int flag = 0); - napi_value Convert(std::string strXml); std::string GetNodeType(const xmlElementType enumType) const; - napi_status DealNapiStrValue(const napi_value napi_StrValue, std::string &result) const; void SetKeyValue(const napi_value &object, const std::string strKey, const std::string strValue) const; - void DealOptions(const napi_value napiObj); std::string Trim(std::string strXmltrim) const; void GetPrevNodeList(xmlNodePtr curNode); void DealSpaces(const napi_value napiObj); @@ -93,7 +123,7 @@ namespace OHOS::Xml { void DealComplex(std::string &strXml, const napi_value &object) const; void Replace(std::string &str, const std::string src, const std::string dst) const; void DealCDataInfo(bool bCData, xmlNodePtr &curNode) const; - private: + napi_env env_; SpaceType spaceType_; int32_t iSpace_; @@ -102,5 +132,5 @@ namespace OHOS::Xml { std::vector prevObj_; XmlInfo xmlInfo_; }; -} // namespace -#endif \ No newline at end of file +} // namespace OHOS::Xml +#endif // CONVERTXML_JS_CONVERTXML_H_ diff --git a/convertxml/native_module_convertxml.cpp b/convertxml/native_module_convertxml.cpp index 69f4e6e..4706b06 100755 --- a/convertxml/native_module_convertxml.cpp +++ b/convertxml/native_module_convertxml.cpp @@ -32,8 +32,8 @@ namespace OHOS::Xml { auto objectInfo = new ConvertXml(env); napi_wrap( env, thisVar, objectInfo, - [](napi_env env_, void *data, void *hint) { - auto obj = (ConvertXml*)data; + [](napi_env environment, void *data, void *hint) { + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -94,11 +94,11 @@ namespace OHOS::Xml { .nm_filename = nullptr, .nm_register_func = ConvertXmlInit, .nm_modname = "ConvertXML", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = { 0 }, }; - extern "C" __attribute__ ((constructor)) void RegisterModule() + extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&convertXmlModule); } @@ -123,4 +123,4 @@ namespace OHOS::Xml { *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; } } -} // namespace +} // namespace OHOS::Xml diff --git a/test_uri/unittest/test.h b/test_uri/unittest/test.h index 466fd55..f31436f 100755 --- a/test_uri/unittest/test.h +++ b/test_uri/unittest/test.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H -#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H +#ifndef TEST_URI_UNITTEST_TEST_H_ +#define TEST_URI_UNITTEST_TEST_H_ #include "native_engine.h" @@ -30,4 +30,4 @@ protected: NativeEngine *engine_; }; -#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H */ \ No newline at end of file +#endif // TEST_URI_UNITTEST_TEST_H_ diff --git a/test_xml/unittest/test.h b/test_xml/unittest/test.h index 466fd55..8f09b8c 100644 --- a/test_xml/unittest/test.h +++ b/test_xml/unittest/test.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H -#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H +#ifndef TEST_XML_UNITTEST_TEST_H_ +#define TEST_XML_UNITTEST_TEST_H_ #include "native_engine.h" @@ -30,4 +30,4 @@ protected: NativeEngine *engine_; }; -#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H */ \ No newline at end of file +#endif // TEST_XML_UNITTEST_TEST_H_ diff --git a/uri/js_uri.cpp b/uri/js_uri.cpp index b88078f..b5cff10 100755 --- a/uri/js_uri.cpp +++ b/uri/js_uri.cpp @@ -119,7 +119,7 @@ namespace OHOS::Uri { if (!errStr_.empty()) { return; } - } else if (data_.find('/') == 0) { + } else if (data_[0] == '/') { uriData_.path = data_; uriData_.SchemeSpecificPart = data_ + uriData_.query; data_ = ""; @@ -531,4 +531,4 @@ namespace OHOS::Uri { } return uriData_.fragment; } -} // namespace +} // namespace OHOS::Uri diff --git a/uri/js_uri.h b/uri/js_uri.h index ca350dc..a7d3b4e 100755 --- a/uri/js_uri.h +++ b/uri/js_uri.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMPILERUNTIME_JS_API_URI_H -#define COMPILERUNTIME_JS_API_URI_H +#ifndef URI_JS_URI_H_ +#define URI_JS_URI_H_ #include #include @@ -40,24 +40,91 @@ namespace OHOS::Uri { class Uri { public: + /** + * URI constructor, which is used to instantiate a URI object. + * + * @param env NAPI environment parameters. + * @param input Constructs a URI by parsing a given string. + */ Uri(napi_env env, const std::string input); + + /** + * The destructor of the Uri. + */ virtual ~Uri() {} + /** + * Tests whether this URI is equivalent to other URI objects. + * + * @param other URI object to be compared + */ bool Equals(const Uri other) const; + + /** + * Indicates whether this URI is an absolute URI. + */ bool IsAbsolute() const; + /** + * Determine whether parsing failed. + */ std::string IsFailed() const; + + /** + * Returns the serialized URI as a string. + */ std::string ToString() const; + + /** + * Normalize the path of this URI. + */ std::string Normalize() const; + + /** + * Gets the protocol part of the URI. + */ std::string GetScheme() const; + + /** + * Gets the decoding permission component part of this URI. + */ std::string GetAuthority() const; + + /** + * Gets the decoding scheme-specific part of the URI. + */ std::string GetSsp() const; + + /** + * Obtains the user information part of the URI. + */ std::string GetUserinfo() const; + + /** + * Gets the hostname portion of the URI without a port. + */ std::string GetHost() const; + + /** + * Gets the hostname portion of the URI without a port. + */ std::string GetPort() const; + + /** + * Gets the path portion of the URI. + */ std::string GetPath() const; + + /** + * Gets the query portion of the URI. + */ std::string GetQuery() const; + + /** + * Gets the fragment part of the URI. + */ std::string GetFragment() const; + private: void PreliminaryWork() const; void AnalysisUri(); @@ -75,6 +142,7 @@ namespace OHOS::Uri { bool AnalysisIPV4(); std::string Split(std::string path) const; + private: UriData uriData_; std::string data_; @@ -82,5 +150,5 @@ namespace OHOS::Uri { std::string errStr_; napi_env env_ = nullptr; }; -} // namespace -#endif /* COMPILERUNTIME_JS_API_URI_H */ \ No newline at end of file +} // namespace OHOS::Uri +#endif // URI_JS_URI_H_ diff --git a/uri/native_module_uri.cpp b/uri/native_module_uri.cpp index dabf872..db934cc 100755 --- a/uri/native_module_uri.cpp +++ b/uri/native_module_uri.cpp @@ -32,31 +32,27 @@ namespace OHOS::Uri { size_t argc = 1; napi_value argv[1] = { 0 }; Uri *object = nullptr; - std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); napi_valuetype valuetype; NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); if (valuetype == napi_string) { - char *type = nullptr; + std::string type = ""; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; - delete[] type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - object = new Uri(env, input); + type.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], type.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; + } + object = new Uri(env, type); } else { napi_throw_error(env, nullptr, "parameter type is error"); } NAPI_CALL(env, napi_wrap(env, thisVar, object, - [](napi_env env_, void *data, void *hint) { + [](napi_env environment, void *data, void *hint) { auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; @@ -287,7 +283,7 @@ namespace OHOS::Uri { .nm_filename = nullptr, .nm_register_func = UriInit, .nm_modname = "uri", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = {0}, }; extern "C" __attribute__((constructor)) void RegisterModule() @@ -314,4 +310,4 @@ namespace OHOS::Uri { *buflen = _binary_uri_abc_end - _binary_uri_abc_start; } } -} // namespace +} // namespace OHOS::Uri diff --git a/url/js_url.cpp b/url/js_url.cpp index 2bd3b00..05ec19a 100755 --- a/url/js_url.cpp +++ b/url/js_url.cpp @@ -220,9 +220,9 @@ namespace OHOS::Url { } if (userAndPasswd.find(':') != std::string::npos) { - size_t pos_ = userAndPasswd.find(':'); - std::string user = userAndPasswd.substr(0, pos_); - std::string keyWord = userAndPasswd.substr(pos_ + 1); + size_t position = userAndPasswd.find(':'); + std::string user = userAndPasswd.substr(0, position); + std::string keyWord = userAndPasswd.substr(position + 1); if (!user.empty()) { username = user; flags.set(static_cast(BitsetStatusFlag::BIT2)); @@ -849,9 +849,10 @@ namespace OHOS::Url { } size_t i = 0; bool special = false; + std::string strHost = ""; if (hostandpath.find('/') != std::string::npos) { i = hostandpath.find('/'); - std::string strHost = hostandpath.substr(0, i); + strHost = hostandpath.substr(0, i); std::string strPath = hostandpath.substr(i + 1); if (strHost.find('@') != std::string::npos) { AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); @@ -873,14 +874,14 @@ namespace OHOS::Url { AnalysisHost(strHost, urlinfo.host, flags, special); AnalysisPath(strPath, urlinfo.path, flags, special); } else { - std::string strHost_ = hostandpath; - AnalyStrHost(strHost_, urlinfo, flags); - AnalyHostPath(strHost_, flags, urlinfo); - AnalysisHost(strHost_, urlinfo.host, flags, special); + strHost = hostandpath; + AnalyStrHost(strHost, urlinfo, flags); + AnalyHostPath(strHost, flags, urlinfo); + AnalysisHost(strHost, urlinfo.host, flags, special); } } else if (input[1] == '/') { - std::string strPath_ = input.substr(1); - AnalysisPath(strPath_, urlinfo.path, flags, false); + std::string strOfPath = input.substr(1); + AnalysisPath(strOfPath, urlinfo.path, flags, false); } else { AnalyInfoPath(flags, urlinfo, input); } @@ -972,17 +973,18 @@ namespace OHOS::Url { void AnalysisInput(std::string& input, UrlData& urlData, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { + size_t pos = 0; if (input.find('#') != std::string::npos) { - size_t pos = input.find('#'); + pos = input.find('#'); std::string fragment = input.substr(pos); AnalysisFragment(fragment, urlData.fragment, flags); input = input.substr(0, pos); } if (input.find('?') != std::string::npos) { - size_t pos_ = input.find('?'); - std::string query = input.substr(pos_); + pos = input.find('?'); + std::string query = input.substr(pos); AnalysisQuery(query, urlData.query, flags); - input = input.substr(0, pos_); + input = input.substr(0, pos); } bool special = (flags.test(static_cast(BitsetStatusFlag::BIT1)) ? true : false); AnalysisPath(input, urlData.path, flags, special); @@ -1055,10 +1057,10 @@ namespace OHOS::Url { input = input.substr(0, pos); } if (input.find('?') != std::string::npos) { - size_t pos_ = input.find('?'); - std::string query = input.substr(pos_); + size_t position = input.find('?'); + std::string query = input.substr(position); AnalysisQuery(query, urlData.query, flags); - input = input.substr(0, pos_); + input = input.substr(0, position); } std::string str = input.substr(pos); if (urlData.scheme == "file:") { @@ -1659,8 +1661,8 @@ namespace OHOS::Url { } reviseChar[0x20] = "+"; // 0x20:ASCII value of spaces const size_t lenStr = searchParams.size(); + napi_value result = nullptr; if (lenStr == 0) { - napi_value result = nullptr; napi_create_string_utf8(env, output.c_str(), output.size(), &result); return result; } @@ -1675,9 +1677,8 @@ namespace OHOS::Url { output += +"&" + strKey + "=" + strValue; } } - napi_value result_ = nullptr; - napi_create_string_utf8(env, output.c_str(), output.size(), &result_); - return result_; + napi_create_string_utf8(env, output.c_str(), output.size(), &result); + return result; } void URLSearchParams::HandleIllegalChar(std::wstring& inputStr, std::wstring::const_iterator it) { @@ -1865,19 +1866,16 @@ namespace OHOS::Url { napi_value URLSearchParams::IsHas(napi_value name) const { - char *buffer = nullptr; size_t bufferSize = 0; + if (napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get name size"); + return nullptr; + } std::string buf = ""; - napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; - if (memset_s(buffer, bufferSize + 1, 0, bufferSize + 1) != EOK) { - HILOG_ERROR("type memset error"); - delete [] buffer; - return nullptr; - } - napi_get_value_string_utf8(env, name, buffer, bufferSize + 1, &bufferSize); - buf = buffer; + buf.resize(bufferSize); + if (napi_get_value_string_utf8(env, name, buf.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get name value"); + return nullptr; } bool flag = false; napi_value result = nullptr; @@ -1886,11 +1884,9 @@ namespace OHOS::Url { if (searchParams[i] == buf) { flag = true; napi_get_boolean(env, flag, &result); - delete []buffer; return result; } } - delete []buffer; napi_get_boolean(env, flag, &result); return result; } @@ -2015,4 +2011,4 @@ namespace OHOS::Url { } return arr; } -} // namespace +} // namespace OHOS::Url diff --git a/url/js_url.h b/url/js_url.h index b89fb53..be591d7 100755 --- a/url/js_url.h +++ b/url/js_url.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef COMPILERUNTIME_JS_API_URL_H -#define COMPILERUNTIME_JS_API_URL_H +#ifndef URL_JS_URL_H_ +#define URL_JS_URL_H_ #include #include @@ -55,34 +55,162 @@ namespace OHOS::Url { class URL { public: + /** + * URI constructor, which is used to instantiate a URI object. + * + * @param env NAPI environment parameters. + * @param input Constructs a URI by parsing a given string. + */ URL(napi_env env, const std::string& input); + + /** + * URI constructor, which is used to instantiate a URI object. + * + * @param env NAPI environment parameters. + * @param input Constructs a URI by parsing a given string. + * @param base The input parameter is a character string. + */ URL(napi_env env, const std::string& input, const std::string& base); + + /** + * URI constructor, which is used to instantiate a URI object. + * + * @param env NAPI environment parameters. + * @param input Constructs a URI by parsing a given string. + * @param base The input parameter is the URL object. + */ URL(napi_env env, const std::string& input, const URL& base); + /** + * Gets the host name portion of the URL��not include the port. + */ napi_value GetHostname() const; + + /** + * Sets the host name portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetHostname(const std::string& input); + + /** + * Sets the username name portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetUsername(const std::string& input); + + /** + * Sets the password portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetPassword(const std::string& input); + + /** + * Sets the scheme portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetScheme(const std::string& input); + + /** + * Sets the fragment portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetFragment(const std::string& input); + + /** + * Sets the search portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetSearch(const std::string& input); + + /** + * Sets the host portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetHost(const std::string& input); + + /** + * Sets the port portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetPort(const std::string& input); + + /** + * Sets the href portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetHref(const std::string& input); + + /** + * Sets the path portion of the URL��not include the port. + * + * @param input Constructs a URI by parsing a given string. + */ void SetPath(const std::string& input); + /** + * Gets the search portion of the URL��not include the port. + */ napi_value GetSearch() const; + + /** + * Gets the username portion of the URL��not include the port. + */ napi_value GetUsername() const; + + /** + * Gets the password portion of the URL��not include the port. + */ napi_value GetPassword() const; + + /** + * Gets the fragment portion of the URL��not include the port. + */ napi_value GetFragment() const; + + /** + * Gets the scheme portion of the URL��not include the port. + */ napi_value GetScheme() const; + + /** + * Gets the path portion of the URL��not include the port. + */ napi_value GetPath() const; + + /** + * Gets the port portion of the URL��not include the port. + */ napi_value GetPort() const; + + /** + * Judge whether it's on or off. + */ napi_value GetOnOrOff() const; + + /** + * Judge whether it's Ipv6. + */ napi_value GetIsIpv6() const; + + /** + * Gets the host name portion of the URL��not include the port. + */ napi_value GetHost() const; + /** + * The destructor of the url + */ virtual ~URL() {} + private: UrlData urlData_; std::bitset(BitsetStatusFlag::BIT_STATUS_11)> flags_; @@ -92,27 +220,115 @@ namespace OHOS::Url { class URLSearchParams { public: + /** + * A parameterized constructor used to create an URLSearchParams instance. + * + * @param env NAPI environment parameters. + */ explicit URLSearchParams(napi_env env); + + /** + * Virtual destructor of URLSearchParams + */ virtual ~URLSearchParams() {} + + /** + * Returns a Boolean that indicates whether a parameter with the specified name exists. + * + * @param name Specifies the name of a key-value pair. + */ napi_value IsHas(napi_value name) const; + + /** + * Returns the first value associated to the given search parameter. + * + * @param buffer Returns the first value associated to the given search parameter. + */ napi_value Get(napi_value buffer); + + /** + * Returns all key-value pairs associated with a given search parameter as an array. + * + * @param buffer Specifies the name of a key value. + */ napi_value GetAll(napi_value buffer); + + /** + * Appends a specified key/value pair as a new search parameter. + * + * @param buffer Key name of the search parameter to be inserted. + * @param temp Values of search parameters to be inserted. + */ void Append(napi_value buffer, napi_value temp); + + /** + * Deletes the given search parameter and its associated value,from the list of all search parameters. + * + * @param buffer Name of the key-value pair to be deleted. + */ void Delete(napi_value buffer); + + /** + * Returns an ES6 iterator. Each item of the iterator is a JavaScript Array. + */ napi_value Entries() const; + + /** + * Sets the value associated with a given search parameter to the + * given value. If there were several matching values, this method + * deletes the others. If the search parameter doesn't exist, this + * method creates it. + * + * @param name Key name of the parameter to be set. + * @param value Indicates the parameter value to be set. + */ void Set(napi_value name, napi_value value); + + /** + * Sort all key/value pairs contained in this object in place and return undefined. + */ void Sort(); + + /** + * Returns a query string suitable for use in a URL. + */ napi_value ToString(); + + /** + * Returns an iterator allowing to go through all keys contained in this object. + */ napi_value IterByKeys(); + + /** + * Returns an iterator allowing to go through all values contained in this object. + */ napi_value IterByValues(); + + /** + * Sets the string array of searchParams. + * + * @param input String array. + */ void SetArray(std::vector input); + + /** + * Gets the string array of searchParams. + */ napi_value GetArray() const; + + /** + * This function will decode the string and put the parsed key and value values into the + * vector container of urlsearchparams class according to the rules. + * + * @param Stringpar The input parameter of urlsearchparams is string. + */ std::vector StringParmas(std::string Stringpar); + private: std::string ToUSVString(std::string inputStr); void HandleIllegalChar(std::wstring& inputStr, std::wstring::const_iterator it); std::vector searchParams; napi_env env; }; -} // namespace -#endif /* COMPILERUNTIME_JS_API_URL_H */ +} // namespace OHOS::Url +#endif // URL_JS_URL_H_ diff --git a/url/native_module_url.cpp b/url/native_module_url.cpp index 42d3440..9b779f3 100755 --- a/url/native_module_url.cpp +++ b/url/native_module_url.cpp @@ -104,8 +104,8 @@ namespace OHOS::Url { } napi_wrap( env, thisVar, object, - [](napi_env env_, void *data, void *hint) { - auto obj = (URL*)data; + [](napi_env environment, void *data, void *hint) { + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -231,22 +231,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -263,22 +256,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -295,22 +281,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -327,22 +306,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -359,22 +331,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -391,22 +356,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -423,22 +381,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -455,22 +406,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -487,22 +431,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -519,22 +456,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - char *type = nullptr; size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; } - if (type != nullptr) { - delete[] type; - type = nullptr; + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } URL *murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); @@ -552,8 +482,8 @@ namespace OHOS::Url { auto object = new URLSearchParams(env); napi_wrap( env, thisVar, object, - [](napi_env env_, void *data, void *hint) { - auto obj = (URLSearchParams*)data; + [](napi_env environment, void *data, void *hint) { + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -574,20 +504,19 @@ namespace OHOS::Url { size_t arraySize = 0; napi_value napiStr = nullptr; for (size_t i = 0; i < length; i++) { - char *cstr = nullptr; napi_get_element(env, argv[0], i, &napiStr); - napi_get_value_string_utf8(env, napiStr, nullptr, 0, &arraySize); + if (napi_get_value_string_utf8(env, napiStr, nullptr, 0, &arraySize) != napi_ok) { + HILOG_ERROR("can not get napiStr size"); + return nullptr; + } if (arraySize > 0) { - cstr = new char[arraySize + 1]; - if (memset_s(cstr, arraySize + 1, 0, arraySize + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] cstr; - return nullptr; + std::string cstr = ""; + cstr.resize(arraySize); + if (napi_get_value_string_utf8(env, napiStr, cstr.data(), arraySize + 1, &arraySize) != napi_ok) { + HILOG_ERROR("can not get name value"); + return nullptr; } - napi_get_value_string_utf8(env, napiStr, cstr, arraySize + 1, &arraySize); vec.push_back(cstr); - delete []cstr; - cstr = nullptr; } else { vec.push_back(""); } @@ -862,19 +791,15 @@ namespace OHOS::Url { size_t argc = 1; std::string input = ""; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - char *type = nullptr; size_t typelen = 0; - napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen); - if (typelen > 0) { - type = new char[typelen + 1]; - if (memset_s(type, typelen + 1, 0, typelen + 1) != 0) { - HILOG_ERROR("type memset error"); - delete [] type; - return nullptr; - } - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - input = type; - delete[] type; + if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] size"); + return nullptr; + } + input.resize(typelen); + if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { + HILOG_ERROR("can not get argv[0] value"); + return nullptr; } std::vector seachParasmsString; seachParasmsString = StringParsing(input); @@ -961,7 +886,7 @@ namespace OHOS::Url { .nm_filename = nullptr, .nm_register_func = Init, .nm_modname = "url", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = {0}, }; extern "C" __attribute__((constructor)) void RegisterModule() diff --git a/xml/js_xml.cpp b/xml/js_xml.cpp index 726587b..1945031 100644 --- a/xml/js_xml.cpp +++ b/xml/js_xml.cpp @@ -86,8 +86,10 @@ namespace OHOS::xml { type = "isDecl"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetDeclaration memcpy_s failed"); } } } @@ -106,8 +108,10 @@ namespace OHOS::xml { type = "isNsp"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetNamespace memcpy_s failed"); } } } @@ -141,8 +145,10 @@ namespace OHOS::xml { elementStack.push_back(""); size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("StartElement memcpy_s failed"); } } } @@ -160,23 +166,28 @@ namespace OHOS::xml { type = "isAttri"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetAttributes memcpy_s failed"); } } } void XmlSerializer::EndElement() { out_ = ""; + size_t iLenTemp = 0; if (type == "isStart" || type == "isAttri") { SplicNsp(); out_.append("/>"); type = "isEndTag"; --depth_; - size_t iLenTemp = out_.length(); + iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("StartElement memcpy_s failed"); } } return; @@ -195,10 +206,12 @@ namespace OHOS::xml { elementStack[depth_ * 3 + 1] = ""; // 3: number of args type = "isEndTag"; out_.append(">"); - size_t iLenTemp_ = out_.length(); - if (iLength_ > iPos_ + iLenTemp_ - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp_)) { - iPos_ += iLenTemp_; + iLenTemp = out_.length(); + if (iLength_ > iPos_ + iLenTemp - 1) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { + iPos_ += iLenTemp; + } else { + HILOG_ERROR("EndElement memcpy_s failed"); } } } @@ -218,8 +231,10 @@ namespace OHOS::xml { type = "isAddEmpElem"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("AddEmptyElement memcpy_s failed"); } } } @@ -234,8 +249,10 @@ namespace OHOS::xml { type = "isText"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetText memcpy_s failed"); } } } @@ -253,8 +270,10 @@ namespace OHOS::xml { type = "isCom"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetComment memcpy_s failed"); } } } @@ -273,8 +292,10 @@ namespace OHOS::xml { type = "isCData"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetCData memcpy_s failed"); } } } @@ -292,8 +313,10 @@ namespace OHOS::xml { type = "isDocType"; size_t iLenTemp = out_.length(); if (iLength_ > iPos_ + iLenTemp - 1) { - if (!memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp)) { + if (memcpy_s(pStart_ + iPos_, iLength_ - iPos_, out_.c_str(), iLenTemp) == EOK) { iPos_ += iLenTemp; + } else { + HILOG_ERROR("SetDocType memcpy_s failed"); } } } @@ -966,9 +989,10 @@ namespace OHOS::xml { bool XmlPullParser::ParseNsp() { bool any = false; + size_t cut = 0; for (size_t i = 0; i < (attriCount_ << 2); i += 4) { // 2 and 4: number of args std::string attrName = attributes[i + 2]; // 2: number of args - size_t cut = attrName.find(':'); + cut = attrName.find(':'); std::string prefix; if (cut != std::string::npos) { prefix = attrName.substr(0, cut); @@ -988,13 +1012,13 @@ namespace OHOS::xml { if (any) { ParseNspFunction(); } - size_t cut_ = name_.find(':'); - if (cut_ == 0) { + cut = name_.find(':'); + if (cut == 0) { xmlPullParserError_ = "illegal tag name: " + name_; } - if (cut_ != std::string::npos) { - prefix_ = name_.substr(0, cut_); - name_ = name_.substr(cut_ + 1); + if (cut != std::string::npos) { + prefix_ = name_.substr(0, cut); + name_ = name_.substr(cut + 1); } namespace_ = GetNamespace(prefix_); if (namespace_ == "") { @@ -1822,5 +1846,5 @@ namespace OHOS::xml { } return result; } -} // namespace +} // namespace OHOS::Xml diff --git a/xml/js_xml.h b/xml/js_xml.h index 467aab7..ba6cf5a 100644 --- a/xml/js_xml.h +++ b/xml/js_xml.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_CCRUNTIME_XML_CLASS_H -#define FOUNDATION_ACE_CCRUNTIME_XML_CLASS_H +#ifndef XML_JS_XML_H_ +#define XML_JS_XML_H_ #include #include @@ -28,24 +28,120 @@ namespace OHOS::xml { class XmlSerializer { public: + /** + * Constructor for XmlSerializer. + * + * @param pStart is the pointer. + * @param bufferLengthis the length of the ArrayBuffer or + * DataView memory used to receive the written xml information. + */ XmlSerializer(char *pStart, size_t bufferLength, std::string encoding = "utf-8") :pStart_(pStart), iLength_(bufferLength), encoding_(encoding) {}; + + /** + * XmlSerializer destructor. + */ ~XmlSerializer() {} + + /** + * 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(std::string name, 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(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, std::string nsTemp); + + /** + * Write the comment property. + * + * @param comment The parameter is the comment content of the current element. + */ void SetComment(std::string comment); + + /** + * Write CDATA attributes. + * + * @param comment The parameter is the content of the CDATA attribute. + */ void SetCData(std::string data); + + /** + * Write CDATA attributes. + * + * @param comment The parameter is the content of the CDATA attribute. + */ void SetText(std::string text); + + /** + * Write DocType property. + * + * @param text The parameter is the content of the DocType property. + */ void SetDocType(std::string text); + + /** + * write an escape. + * + * @param s The parameter is the passed in escaped string. + */ void WriteEscaped(std::string s); + + /** + * SplicNsp functio. + */ void SplicNsp(); + + /** + * NextItem function. + */ void NextItem(); + + /** + * Throw exception function. + */ std::string XmlSerializerError(); + + /** + * 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. + */ static napi_status DealNapiStrValue(napi_env env, const napi_value napiStr, std::string &result); + private: char *pStart_; size_t iPos_ = 0; @@ -92,15 +188,74 @@ namespace OHOS::xml { public: class ParseInfo { public: + /** + * Get the current depth of the element. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is the current depth of the returned element. + */ static napi_value GetDepth(napi_env env, napi_callback_info info); + + /** + * Get the current column number, starting at 1. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to return the current line number. + */ static napi_value GetColumnNumber(napi_env env, napi_callback_info info); + + /** + * Get the current line number, starting at 1. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to return the current column number. + */ static napi_value GetLineNumber(napi_env env, napi_callback_info info); + + /** + * Get the number of attributes of the current start tag. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is the number of attributes of the current start tag. + */ static napi_value GetAttributeCount(napi_env env, napi_callback_info info); + + /** + * Get the current element name. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to return the current element name. + */ static napi_value GetName(napi_env env, napi_callback_info info); + + /** + * Get the namespace of the current element. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is the namespace that returns the current element. + */ static napi_value GetNamespace(napi_env env, napi_callback_info info); + + /** + * Get the current element prefix. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to return the current element prefix. + */ static napi_value GetPrefix(napi_env env, napi_callback_info info); + + /** + * Get the text content of the current event. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to return the text content of the current event. + */ static napi_value GetText(napi_env env, napi_callback_info info); + + /** + * Check whether the current element is an empty element. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to returns true The current element is an empty element. + */ static napi_value IsEmptyElementTag(napi_env env, napi_callback_info info); + + /** + * Determines whether the current text event contains only space characters. + * @param env The parameter is NAPI environment variables. + * @param info The parameter is to returns true, the current text event contains only space characters. + */ static napi_value IsWhitespace(napi_env env, napi_callback_info info); }; struct TagText { @@ -216,6 +371,7 @@ namespace OHOS::xml { void MakeStrUpper(std::string &src) const; TagEnum DealLtGroup(); void DealWhiteSpace(unsigned char c); + private: napi_env env_; bool bDoctype_ = false; @@ -261,5 +417,5 @@ namespace OHOS::xml { bool bKeepNsAttri = false; bool bDocDecl = false; }; -} // namespace -#endif /* FOUNDATION_ACE_CCRUNTIME_XML_CLASS_H */ \ No newline at end of file +} // namespace OHOS::Xml +#endif // XML_JS_XML_H_ diff --git a/xml/native_module_xml.cpp b/xml/native_module_xml.cpp index 6ab824a..22f86b1 100644 --- a/xml/native_module_xml.cpp +++ b/xml/native_module_xml.cpp @@ -64,8 +64,8 @@ namespace OHOS::xml { } napi_wrap( env, thisVar, object, - [](napi_env env, void *data, void *hint) { - auto obj = (XmlSerializer*)data; + [](napi_env environment, void *data, void *hint) { + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -101,7 +101,7 @@ namespace OHOS::xml { napi_get_dataview_info(env, args[0], &len, &data, &arraybuffer, &offPos); } if (data) { - std::string strEnd = (char*)data; + std::string strEnd = reinterpret_cast(data); if (argc == 1) { object = new XmlPullParser(env, strEnd, "utf-8"); } else if (argc == 2) { // 2:When the input parameter is set to 2 @@ -114,8 +114,8 @@ namespace OHOS::xml { } napi_wrap( env, thisVar, object, - [](napi_env env, void *data, void *hint) { - auto obj = (XmlPullParser*)data; + [](napi_env env_, void *data, void *hint) { + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -459,11 +459,11 @@ namespace OHOS::xml { .nm_filename = nullptr, .nm_register_func = Init, .nm_modname = "xml", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = {0}, }; extern "C" __attribute__((constructor)) void RegisterModule() { napi_module_register(&xmlModule); } -} // namespace +} // namespace OHOS::Xml