ResourceBundlePtr ResourceBundle::getBundle()

in src/main/cpp/resourcebundle.cpp [30:118]


ResourceBundlePtr ResourceBundle::getBundle(const LogString& baseName,
	const Locale& locale)
{
	std::vector<LogString> bundlesNames;

	if (!locale.getVariant().empty())
	{
		bundlesNames.push_back(baseName + LOG4CXX_STR("_") +
			locale.getLanguage() + LOG4CXX_STR("_") +
			locale.getCountry() + LOG4CXX_STR("_") +
			locale.getVariant());
	}

	if (!locale.getCountry().empty())
	{
		bundlesNames.push_back(baseName + LOG4CXX_STR("_") +
			locale.getLanguage() + LOG4CXX_STR("_") +
			locale.getCountry());
	}

	if (!locale.getLanguage().empty())
	{
		bundlesNames.push_back(baseName + LOG4CXX_STR("_") +
			locale.getLanguage());
	}

	bundlesNames.push_back(baseName);

	PropertyResourceBundlePtr resourceBundle, previous;
	for (auto bundleName : bundlesNames)
	{
		PropertyResourceBundlePtr current;

		// Try loading a class which implements ResourceBundle
		try
		{
			const Class& classObj = Loader::loadClass(bundleName);
			ObjectPtr obj = ObjectPtr(classObj.newInstance());
			current = LOG4CXX_NS::cast<PropertyResourceBundle>(obj);
		}
		catch (ClassNotFoundException&)
		{
			current.reset();
		}

		// No class found, then try to create a PropertyResourceBundle from a file
		if (!current)
		{
			InputStreamPtr bundleStream =
				Loader::getResourceAsStream(
					bundleName + LOG4CXX_STR(".properties"));

			if (!bundleStream)
			{
				continue;
			}

			try
			{
				current = std::make_shared<PropertyResourceBundle>(bundleStream);
			}
			catch (Exception&)
			{
				throw;
			}
		}

		// Add the new resource bundle to the hierarchy
		if (!resourceBundle)
		{
			resourceBundle = current;
			previous = current;
		}
		else
		{
			previous->setParent(current);
			previous = current;
		}
	}

	// no resource bundle found at all, then throw exception
	if (!resourceBundle)
	{
		throw MissingResourceException(
			((LogString) LOG4CXX_STR("Missing resource bundle ")) + baseName);
	}

	return resourceBundle;
}