diff --git a/frameworks/intl/include/date_time_format.h b/frameworks/intl/include/date_time_format.h index 129027c1728f49c2aa9c97882475b77858611a32..07f5686a5d472aa16ca2f22631c270e70987bf79 100644 --- a/frameworks/intl/include/date_time_format.h +++ b/frameworks/intl/include/date_time_format.h @@ -59,6 +59,9 @@ public: std::string GetLocaleMatcher() const; std::string GetFormatMatcher() const; std::string GetFractionalSecondDigits() const; + static std::unique_ptr CreateInstance(const std::vector &localeTags, + std::map &configs); + private: std::string localeTag; std::string dateStyle; @@ -135,6 +138,7 @@ private: void FixPatternPartTwo(); void removeAmPmChar(); int64_t GetArrayValue(int64_t *dateArray, size_t index, size_t size); + bool CheckInitSuccess(); }; } // namespace I18n } // namespace Global diff --git a/frameworks/intl/include/i18n_timezone.h b/frameworks/intl/include/i18n_timezone.h index ca769e0c0f6d9281ffb68b48dac8224a6fa34434..d1e31ce1e14a38eeddc45181ac5fc06ecf6b607f 100644 --- a/frameworks/intl/include/i18n_timezone.h +++ b/frameworks/intl/include/i18n_timezone.h @@ -32,8 +32,10 @@ public: std::string GetDisplayName(bool isDST); std::string GetDisplayName(std::string localeStr); std::string GetDisplayName(std::string localeStr, bool isDST); + static std::unique_ptr CreateInstance(std::string zoneID); private: + icu::TimeZone* GetTimeZone(); icu::TimeZone *timezone; }; } // namespace I18n diff --git a/frameworks/intl/include/locale_config.h b/frameworks/intl/include/locale_config.h index 2c0c48b7d838b0d7e98e949fdd6e77a604412b2e..c695cb6a06da9aeeb800b4692d4b1777bbc152a4 100644 --- a/frameworks/intl/include/locale_config.h +++ b/frameworks/intl/include/locale_config.h @@ -46,6 +46,7 @@ public: static std::string GetValidLocale(const std::string &localeTag); static bool Is24HourClock(); static bool Set24HourClock(bool option); + static bool CheckPermission(); private: static bool IsValidLanguage(const std::string &language); @@ -96,7 +97,6 @@ private: static std::set validHcTag; static bool listsInitialized; static bool InitializeLists(); - static bool CheckPermission(); }; } // namespace I18n } // namespace Global diff --git a/frameworks/intl/include/phone_number_format.h b/frameworks/intl/include/phone_number_format.h index 60ebce358ab83f3371eff99be13746b22bc6d054..676fa90a4df96324719e964e655fee3e9ca4f0e6 100644 --- a/frameworks/intl/include/phone_number_format.h +++ b/frameworks/intl/include/phone_number_format.h @@ -31,8 +31,11 @@ public: virtual ~PhoneNumberFormat(); bool isValidPhoneNumber(const std::string &number) const; std::string format(const std::string &number) const; + static std::unique_ptr CreateInstance(const std::string &countryTag, + const std::map &options); private: + PhoneNumberUtil* GetPhoneNumberUtil(); PhoneNumberUtil *util; std::string country; PhoneNumberUtil::PhoneNumberFormat phoneNumberFormat; diff --git a/frameworks/intl/src/collator.cpp b/frameworks/intl/src/collator.cpp index b3b5fc9b6152cc38590aeb908e8644913a939878..2f1b4824da0f9b74b55cd4417120403394cd5b41 100644 --- a/frameworks/intl/src/collator.cpp +++ b/frameworks/intl/src/collator.cpp @@ -92,12 +92,17 @@ bool Collator::IsValidCollation(std::string &collation, UErrorCode &status) std::unique_ptr enumeration( icu::Collator::getKeywordValuesForLocale("collation", icu::Locale(locale.getBaseName()), false, status)); int length; - const char *validCollations = enumeration->next(&length, status); + const char *validCollations = nullptr; + if (enumeration != nullptr) { + validCollations = enumeration->next(&length, status); + } while (validCollations != nullptr) { if (!strcmp(validCollations, currentCollation)) { return true; } - validCollations = enumeration->next(&length, status); + if (enumeration != nullptr) { + validCollations = enumeration->next(&length, status); + } } } return false; diff --git a/frameworks/intl/src/date_time_format.cpp b/frameworks/intl/src/date_time_format.cpp index 3929550b77aa01131e4ea60bc472b264f7497d35..79df7efb059755ad004b12d4006b9f592d41c243 100644 --- a/frameworks/intl/src/date_time_format.cpp +++ b/frameworks/intl/src/date_time_format.cpp @@ -77,6 +77,24 @@ DateTimeFormat::~DateTimeFormat() } } +bool DateTimeFormat::CheckInitSuccess() +{ + if (dateIntvFormat == nullptr || calendar == nullptr || dateFormat == nullptr || localeInfo == nullptr) { + return false; + } + return true; +} + +std::unique_ptr DateTimeFormat::CreateInstance(const std::vector &localeTags, + std::map &configs) +{ + std::unique_ptr dateTimeFormat = std::make_unique(localeTags, configs); + if (!dateTimeFormat->CheckInitSuccess()) { + return nullptr; + } + return dateTimeFormat; +} + void DateTimeFormat::InitWithLocale(const std::string &curLocale, std::map &configs) { UErrorCode status = U_ZERO_ERROR; @@ -228,10 +246,10 @@ void DateTimeFormat::InitDateFormat(UErrorCode &status) if (!dateStyle.empty() || !timeStyle.empty()) { DateFormat::EStyle dateStyleValue = DateFormat::EStyle::kNone; DateFormat::EStyle timeStyleValue = DateFormat::EStyle::kNone; - if (!dateStyle.empty()) { + if (!dateStyle.empty() && dateTimeStyle.count(dateStyle) > 0) { dateStyleValue = dateTimeStyle[dateStyle]; } - if (!timeStyle.empty()) { + if (!timeStyle.empty() && dateTimeStyle.count(timeStyle) > 0) { timeStyleValue = dateTimeStyle[timeStyle]; } dateFormat = DateFormat::createDateTimeInstance(dateStyleValue, timeStyleValue, locale); @@ -495,6 +513,9 @@ std::string DateTimeFormat::FormatRange(int64_t *fromDate, size_t fromDateSize, minute = GetArrayValue(toDate, MINUTE_INDEX, toDateSize); second = GetArrayValue(toDate, SECOND_INDEX, toDateSize); auto toCalendar = std::unique_ptr(Calendar::createInstance(locale, status)); + if (toCalendar == nullptr) { + return nullptr; + } toCalendar->clear(); toCalendar->set(year, month, day, hour, minute, second); if (!timeZone.empty()) { diff --git a/frameworks/intl/src/i18n_timezone.cpp b/frameworks/intl/src/i18n_timezone.cpp index 24ee048c52eb02f9b34b13c74ee9841b0920e9b5..5bb50a357609de5728b01e16b133ce1771c3a602 100644 --- a/frameworks/intl/src/i18n_timezone.cpp +++ b/frameworks/intl/src/i18n_timezone.cpp @@ -39,6 +39,20 @@ I18nTimeZone::~I18nTimeZone() } } +icu::TimeZone* I18nTimeZone::GetTimeZone() +{ + return timezone; +} + +std::unique_ptr I18nTimeZone::CreateInstance(std::string zoneID) +{ + std::unique_ptr i18nTimeZone = std::make_unique(zoneID); + if (i18nTimeZone->GetTimeZone() == nullptr) { + return nullptr; + } + return i18nTimeZone; +} + int32_t I18nTimeZone::GetOffset(double date) { int32_t rawOffset = 0; diff --git a/frameworks/intl/src/number_format.cpp b/frameworks/intl/src/number_format.cpp index 3ad769a3bbd9a8fc858a30e798b1c0259875f1cc..adee381b0242140eaedd1ff9a0539c4545098ed0 100644 --- a/frameworks/intl/src/number_format.cpp +++ b/frameworks/intl/src/number_format.cpp @@ -175,7 +175,9 @@ void NumberFormat::ParseConfigs(std::map &configs) unit = configs["unit"]; if (configs.count("unitDisplay") > 0) { unitDisplayString = configs["unitDisplay"]; - unitDisplay = unitStyle[unitDisplayString]; + if (unitStyle.count(unitDisplayString) > 0) { + unitDisplay = unitStyle[unitDisplayString]; + } } if (configs.count("unitUsage") > 0) { unitUsage = configs["unitUsage"]; @@ -185,7 +187,8 @@ void NumberFormat::ParseConfigs(std::map &configs) currency = configs["currency"]; if (configs.count("currencySign") > 0) { currencySign = configs["currencySign"]; - if (configs["currencySign"] != "accounting" && !signDisplayString.empty()) { + if (configs["currencySign"] != "accounting" && !signDisplayString.empty() && + signAccountingStyle.count(signDisplayString) > 0) { signDisplay = signAccountingStyle[signDisplayString]; } } diff --git a/frameworks/intl/src/phone_number_format.cpp b/frameworks/intl/src/phone_number_format.cpp index 21706e7fb7860b4ff69e59f9fee3d409f0f8ac75..e1c5084f087c0026c43725e3e81385a0cfe14fb9 100644 --- a/frameworks/intl/src/phone_number_format.cpp +++ b/frameworks/intl/src/phone_number_format.cpp @@ -51,6 +51,21 @@ PhoneNumberFormat::~PhoneNumberFormat() { } +std::unique_ptr PhoneNumberFormat::CreateInstance(const std::string &countryTag, + const std::map &options) +{ + std::unique_ptr phoneNumberFormat = std::make_unique(countryTag, options); + if (phoneNumberFormat->GetPhoneNumberUtil() == nullptr) { + return nullptr; + } + return phoneNumberFormat; +} + +PhoneNumberUtil* PhoneNumberFormat::GetPhoneNumberUtil() +{ + return util; +} + bool PhoneNumberFormat::isValidPhoneNumber(const std::string &number) const { i18n::phonenumbers::PhoneNumber phoneNumber; diff --git a/frameworks/intl/src/preferred_language.cpp b/frameworks/intl/src/preferred_language.cpp index f03ae4fa987750c794d75bec5441dd9da1c00d39..11c508442f073e7fcf93ee140fc9b51c68771d88 100644 --- a/frameworks/intl/src/preferred_language.cpp +++ b/frameworks/intl/src/preferred_language.cpp @@ -66,6 +66,9 @@ bool PreferredLanguage::AddPreferredLanguageNonExist(std::vector &p bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int index) { + if (!LocaleConfig::CheckPermission()) { + return false; + } if (!IsValidTag(language)) { return false; } @@ -111,6 +114,9 @@ bool PreferredLanguage::AddPreferredLanguage(const std::string &language, int in bool PreferredLanguage::RemovePreferredLanguage(int index) { + if (!LocaleConfig::CheckPermission()) { + return false; + } std::vector preferredLanguageList = GetPreferredLanguageList(); int idx = index; if (index < 0) { diff --git a/interfaces/js/kits/src/i18n_addon.cpp b/interfaces/js/kits/src/i18n_addon.cpp index 9fbcb17ecdf35ed92639dbb08a16693bfe172c64..45c6f09969950cbc5040a5bd11fc7422ffcb9f1d 100644 --- a/interfaces/js/kits/src/i18n_addon.cpp +++ b/interfaces/js/kits/src/i18n_addon.cpp @@ -958,7 +958,7 @@ bool I18nAddon::InitPhoneNumberFormatContext(napi_env env, napi_callback_info in return false; } env_ = env; - phonenumberfmt_ = std::make_unique(country, options); + phonenumberfmt_ = PhoneNumberFormat::CreateInstance(country, options); return phonenumberfmt_ != nullptr; } @@ -2456,7 +2456,7 @@ napi_value I18nAddon::I18nTimeZoneConstructor(napi_env env, napi_callback_info i HiLog::Error(LABEL, "Wrap II18nAddon failed"); return nullptr; } - obj->timezone_ = std::make_unique(zoneID); + obj->timezone_ = I18nTimeZone::CreateInstance(zoneID); if (!obj->timezone_) { HiLog::Error(LABEL, "Wrap TimeZone failed"); return nullptr; diff --git a/interfaces/js/kits/src/intl_addon.cpp b/interfaces/js/kits/src/intl_addon.cpp index 01005d6b2fc1f807d8c17a322c6b464c282cdb70..31195e8b5d9c9221b28c7331f20210f650d40175 100644 --- a/interfaces/js/kits/src/intl_addon.cpp +++ b/interfaces/js/kits/src/intl_addon.cpp @@ -370,7 +370,6 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - std::vector localeTags; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -389,31 +388,26 @@ napi_value IntlAddon::DateTimeFormatConstructor(napi_env env, napi_callback_info } } } - std::map map = {}; if (argv[1] != nullptr) { GetDateOptionValues(env, argv[1], map); } - std::unique_ptr obj = nullptr; obj = std::make_unique(); if (!obj) { HiLog::Error(LABEL, "Create IntlAddon failed"); return nullptr; } - status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), IntlAddon::Destructor, nullptr, &obj->wrapper_); if (status != napi_ok) { HiLog::Error(LABEL, "Wrap IntlAddon failed"); return nullptr; } - if (!obj->InitDateTimeFormatContext(env, info, localeTags, map)) { HiLog::Error(LABEL, "Init DateTimeFormat failed"); return nullptr; } - obj.release(); return thisVar; } @@ -428,7 +422,7 @@ bool IntlAddon::InitDateTimeFormatContext(napi_env env, napi_callback_info info, return false; } env_ = env; - datefmt_ = std::make_unique(localeTags, map); + datefmt_ = DateTimeFormat::CreateInstance(localeTags, map); return datefmt_ != nullptr; } @@ -538,6 +532,10 @@ napi_value IntlAddon::FormatDateTimeRange(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (argv[0] == nullptr || argv[1] == nullptr) { + HiLog::Error(LABEL, "Parameter wrong"); + return nullptr; + } int64_t firstYear = GetYear(env, argv, 0); int64_t firstMonth = GetMonth(env, argv, 0); int64_t firstDay = GetDay(env, argv, 0);