зеркало из https://github.com/mozilla/pjs.git
bug 76944 - lazy loading of string bundles.
While I'm there, do massive cleanup of string usage to avoid excess copying also remove ns*AcceptLang on recommendation of tao r=tao, sr=dveditz
This commit is contained in:
Родитель
99c6b46d02
Коммит
50cf47c2a4
|
@ -1,3 +0,0 @@
|
|||
nsIStringBundle.idl
|
||||
nsIAcceptLang.idl
|
||||
|
|
@ -28,7 +28,7 @@ include $(DEPTH)/config/autoconf.mk
|
|||
|
||||
MODULE = intl
|
||||
|
||||
XPIDLSRCS = nsIStringBundle.idl nsIAcceptLang.idl
|
||||
XPIDLSRCS = nsIStringBundle.idl
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
#!nmake
|
||||
#
|
||||
# The contents of this file are subject to the Netscape Public
|
||||
# License Version 1.1 (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.mozilla.org/NPL/
|
||||
#
|
||||
# Software distributed under the License is distributed on an "AS
|
||||
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
# implied. See the License for the specific language governing
|
||||
# rights and limitations under the License.
|
||||
#
|
||||
# The Original Code is mozilla.org code.
|
||||
#
|
||||
# The Initial Developer of the Original Code is Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 1998 Netscape Communications Corporation. All
|
||||
# Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
|
||||
DEPTH=..\..\..
|
||||
|
||||
|
||||
|
||||
MODULE = intl
|
||||
|
||||
XPIDLSRCS = \
|
||||
.\nsIStringBundle.idl \
|
||||
.\nsIAcceptLang.idl \
|
||||
$(NULL)
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
|
@ -53,7 +53,7 @@
|
|||
[scriptable, uuid(383F6C16-2797-11d4-BA1C-001083344DE7)]
|
||||
interface nsIAcceptLang : nsISupports
|
||||
{
|
||||
wstring getAcceptLangFromLocale([const] in wstring aLocale);
|
||||
wstring getLocaleFromAcceptLang([const] in wstring aName);
|
||||
wstring acceptLang2List([const] in wstring aName, [const] in wstring aList);
|
||||
wstring getAcceptLangFromLocale(in wstring aLocale);
|
||||
wstring getLocaleFromAcceptLang(in wstring aName);
|
||||
wstring acceptLang2List(in wstring aName, in wstring aList);
|
||||
};
|
||||
|
|
|
@ -37,7 +37,6 @@
|
|||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "nsISupports.idl"
|
||||
#include "nsIEnumerator.idl"
|
||||
#include "nsISimpleEnumerator.idl"
|
||||
|
||||
%{C++
|
||||
|
@ -76,20 +75,11 @@ interface nsIStringBundle : nsISupports
|
|||
wstring formatStringFromName(in wstring aName,
|
||||
[array, size_is(length)] in wstring params,
|
||||
in unsigned long length);
|
||||
/*
|
||||
Don't use - nsIEnumerator and its subinterfaces have been deprecated
|
||||
*/
|
||||
[noscript] nsIBidirectionalEnumerator GetEnumeration();
|
||||
|
||||
/*
|
||||
Implements nsISimpleEnumerator, replaces nsIEnumerator
|
||||
*/
|
||||
nsISimpleEnumerator getSimpleEnumeration();
|
||||
|
||||
/* stream loaded
|
||||
*/
|
||||
attribute boolean loaded;
|
||||
|
||||
};
|
||||
|
||||
[scriptable, uuid(D85A17C0-AA7C-11d2-9B8C-00805F8A16D9)]
|
||||
|
|
|
@ -34,10 +34,9 @@ MODULE_NAME = nsStringBundleModule
|
|||
REQUIRES = xpcom \
|
||||
string \
|
||||
necko \
|
||||
locale \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = nsStringBundle.cpp nsAcceptLang.cpp
|
||||
CPPSRCS = nsStringBundle.cpp
|
||||
|
||||
EXTRA_DSO_LDOPTS =\
|
||||
$(MOZ_NECKO_UTIL_LIBS) \
|
||||
|
|
|
@ -25,7 +25,6 @@ MODULE=intl
|
|||
REQUIRES = xpcom \
|
||||
string \
|
||||
necko \
|
||||
locale \
|
||||
$(NULL)
|
||||
include <$(DEPTH)\config\config.mak>
|
||||
|
||||
|
@ -33,12 +32,10 @@ LIBRARY_NAME=strres
|
|||
|
||||
CPPSRCS = \
|
||||
nsStringBundle.cpp \
|
||||
nsAcceptLang.cpp \
|
||||
$(NULL)
|
||||
|
||||
CPP_OBJS = \
|
||||
.\$(OBJDIR)\nsStringBundle.obj \
|
||||
.\$(OBJDIR)\nsAcceptLang.obj \
|
||||
$(NULL)
|
||||
|
||||
LIBRARY_NAME=strres
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
#include "nsIServiceManager.h"
|
||||
#include "nsID.h"
|
||||
#include "nsString.h"
|
||||
#include "nsReadableUtils.h"
|
||||
#include "nsIStringBundle.h"
|
||||
#include "nsAcceptLang.h"
|
||||
|
||||
|
@ -52,19 +53,6 @@ static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
|
|||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/* util func */
|
||||
static PRUnichar *copyUnicode(const nsString str) {
|
||||
|
||||
PRInt32 len = str.Length()+1;
|
||||
PRUnichar *retval = (PRUnichar *) PR_Calloc(len, sizeof(PRUnichar));
|
||||
retval = (PRUnichar *) memcpy(retval, str.get(), sizeof(PRUnichar)*len);
|
||||
retval[len-1] = '\0';
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsAcceptLang, nsIAcceptLang)
|
||||
|
||||
nsAcceptLang::nsAcceptLang()
|
||||
|
@ -80,11 +68,11 @@ nsAcceptLang::~nsAcceptLang()
|
|||
|
||||
// NS_IMPL_THREADSAFE_ISUPPORTS1(nsAcceptLang, nsIAcceptLang)
|
||||
|
||||
/* wstring getAcceptLangFromLocale ([const] in wstring aLocale); */
|
||||
/* wstring getAcceptLangFromLocale (in wstring aLocale); */
|
||||
NS_IMETHODIMP
|
||||
nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_retval)
|
||||
{
|
||||
nsString lc_name(aLocale);
|
||||
nsDependentString lc_name(aLocale);
|
||||
if (lc_name.Length() <=0) {
|
||||
#ifdef DEBUG
|
||||
printf("nsAcceptLang::GetAcceptLangFromLocale: aLocale is empty!");
|
||||
|
@ -101,26 +89,25 @@ nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_ret
|
|||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
nsIStringBundle *bundle = nsnull;
|
||||
nsCOMPtr<nsIStringBundle> bundle;
|
||||
#if 1
|
||||
res = sBundleService->CreateBundle("resource:/res/language.properties",
|
||||
&bundle);
|
||||
getter_AddRefs(bundle));
|
||||
#else
|
||||
res = sBundleService->CreateBundle("chrome://global/locale/languageNames.properties",
|
||||
&bundle);
|
||||
getter_AddRefs(bundle));
|
||||
#endif
|
||||
PRUnichar *ptrv = nsnull;
|
||||
nsString lc_tmp(aLocale);
|
||||
nsAutoString lc_tmp(aLocale);
|
||||
NS_NAMED_LITERAL_STRING(sAccept, ".accept");
|
||||
NS_NAMED_LITERAL_STRING(sTrue, "true");
|
||||
|
||||
lc_tmp.ToLowerCase();
|
||||
lc_tmp.Append(sAccept);
|
||||
if (NS_OK == (res = bundle->GetStringFromName(lc_tmp.get(), &ptrv))) {
|
||||
nsString tmp(ptrv);
|
||||
if (tmp.Equals(sTrue)) {
|
||||
if (sTrue.Equals(ptrv)) {
|
||||
// valid name already
|
||||
*_retval = copyUnicode(lc_name);
|
||||
*_retval = ToNewUnicode(lc_name);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@ -128,8 +115,8 @@ nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_ret
|
|||
/* not in languageNames.properties; lang only?
|
||||
*/
|
||||
PRInt32 dash = lc_tmp.FindCharInSet("-");
|
||||
nsString lang;
|
||||
nsString country;
|
||||
nsAutoString lang;
|
||||
nsAutoString country;
|
||||
if (dash > 0) {
|
||||
/* lang-country
|
||||
*/
|
||||
|
@ -147,12 +134,11 @@ nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_ret
|
|||
}
|
||||
|
||||
// lang always in lower case; don't convert
|
||||
*_retval = copyUnicode(lang);
|
||||
*_retval = ToNewUnicode(lang);
|
||||
lang.Append(sAccept);
|
||||
if (NS_OK == (res = bundle->GetStringFromName(lang.get(), &ptrv))) {
|
||||
|
||||
nsString tmp(ptrv);
|
||||
if (tmp.Equals(sTrue)) {
|
||||
if (sTrue.Equals(ptrv)) {
|
||||
/* lang is accepted */
|
||||
return res;
|
||||
}
|
||||
|
@ -163,7 +149,7 @@ nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_ret
|
|||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
/* wstring getLocaleFromAcceptLang ([const] in wstring aName); */
|
||||
/* wstring getLocaleFromAcceptLang (in wstring aName); */
|
||||
NS_IMETHODIMP
|
||||
nsAcceptLang::GetLocaleFromAcceptLang(const PRUnichar *aName, PRUnichar **_retval)
|
||||
{
|
||||
|
@ -175,7 +161,7 @@ nsAcceptLang::GetLocaleFromAcceptLang(const PRUnichar *aName, PRUnichar **_retva
|
|||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
nsString acceptLang(aName);
|
||||
nsAutoString acceptLang(aName);
|
||||
|
||||
/* TODO: need to parse accept lang since "en; q=0.3, ja,...
|
||||
*/
|
||||
|
@ -185,7 +171,7 @@ nsAcceptLang::GetLocaleFromAcceptLang(const PRUnichar *aName, PRUnichar **_retva
|
|||
PRInt32 dash = acceptLang.FindCharInSet("-");
|
||||
if (dash > 0) {
|
||||
/* lang-country already */
|
||||
*_retval = copyUnicode(acceptLang);
|
||||
*_retval = ToNewUnicode(acceptLang);
|
||||
return res;
|
||||
}
|
||||
/* lang only
|
||||
|
@ -208,8 +194,7 @@ nsAcceptLang::GetLocaleFromAcceptLang(const PRUnichar *aName, PRUnichar **_retva
|
|||
if (NS_OK == (res = bundle->GetStringFromName(acceptLang.get(), &ptrv))) {
|
||||
|
||||
// valid name already
|
||||
nsString lc_name(ptrv);
|
||||
*_retval = copyUnicode(lc_name);
|
||||
*_retval = ToNewUnicode(nsDependentString(ptrv));
|
||||
}
|
||||
else {
|
||||
/* shall we use system locale instead ? */
|
||||
|
@ -222,7 +207,7 @@ nsAcceptLang::GetLocaleFromAcceptLang(const PRUnichar *aName, PRUnichar **_retva
|
|||
return res;
|
||||
}
|
||||
|
||||
/* wstring acceptLang2List ([const] in wstring aName, [const] in wstring aList); */
|
||||
/* wstring acceptLang2List (in wstring aName, in wstring aList); */
|
||||
NS_IMETHODIMP
|
||||
nsAcceptLang::AcceptLang2List(const PRUnichar *aName, const PRUnichar *aList, PRUnichar **_retval)
|
||||
{
|
||||
|
|
|
@ -44,7 +44,6 @@
|
|||
#include "nsIPersistentProperties2.h"
|
||||
#include "nsIStringBundle.h"
|
||||
#include "nscore.h"
|
||||
#include "nsILocale.h"
|
||||
#include "nsMemory.h"
|
||||
#include "plstr.h"
|
||||
#include "nsNetUtil.h"
|
||||
|
@ -69,11 +68,11 @@
|
|||
#include "nsTextFormatter.h"
|
||||
#include "nsIErrorService.h"
|
||||
|
||||
#include "nsAcceptLang.h" // for nsIAcceptLang
|
||||
|
||||
// for async loading
|
||||
#ifdef ASYNC_LOADING
|
||||
#include "nsIBinaryInputStream.h"
|
||||
#include "nsIStringStream.h"
|
||||
#endif
|
||||
|
||||
// eventQ
|
||||
#include "nsIEventQueueService.h"
|
||||
|
@ -87,20 +86,22 @@ static NS_DEFINE_CID(kErrorServiceCID, NS_ERRORSERVICE_CID);
|
|||
|
||||
NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
|
||||
|
||||
class nsStringBundle : public nsIStringBundle,
|
||||
public nsIStreamLoaderObserver
|
||||
class nsStringBundle : public nsIStringBundle
|
||||
#ifdef ASYNC_LOADING
|
||||
, public nsIStreamLoaderObserver
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
// init version
|
||||
nsStringBundle();
|
||||
nsresult Init(const char* aURLSpec);
|
||||
nsresult InitSyncStream(const char* aURLSpec);
|
||||
nsStringBundle(const char* aURLSpec);
|
||||
nsresult LoadProperties();
|
||||
virtual ~nsStringBundle();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSISTRINGBUNDLE
|
||||
#ifdef ASYNC_LOADING
|
||||
NS_DECL_NSISTREAMLOADEROBSERVER
|
||||
#endif
|
||||
|
||||
nsCOMPtr<nsIPersistentProperties> mProps;
|
||||
|
||||
|
@ -108,68 +109,30 @@ protected:
|
|||
//
|
||||
// functional decomposition of the funitions repeatively called
|
||||
//
|
||||
nsresult GetStringFromID(PRInt32 aID, nsString& aResult);
|
||||
nsresult GetStringFromName(const nsAReadableString& aName, nsString& aResult);
|
||||
nsresult GetStringFromID(PRInt32 aID, nsAString& aResult);
|
||||
nsresult GetStringFromName(const nsAReadableString& aName, nsAString& aResult);
|
||||
|
||||
private:
|
||||
nsCString mPropertiesURL;
|
||||
PRBool mAttemptedLoad;
|
||||
PRBool mLoaded;
|
||||
PRPackedBool mAttemptedLoad;
|
||||
PRPackedBool mLoaded;
|
||||
|
||||
public:
|
||||
static nsresult GetLangCountry(nsILocale* aLocale, nsString& lang, nsString& country);
|
||||
|
||||
static nsresult FormatString(const PRUnichar *formatStr,
|
||||
const PRUnichar **aParams, PRUint32 aLength,
|
||||
PRUnichar **aResult);
|
||||
};
|
||||
|
||||
nsresult
|
||||
nsStringBundle::InitSyncStream(const char* aURLSpec)
|
||||
{
|
||||
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// plan A: don't fallback; use aURLSpec: xxx.pro -> xxx.pro
|
||||
|
||||
#ifdef DEBUG_tao_
|
||||
printf("\n** nsStringBundle::InitSyncStream: %s\n", aURLSpec?s:"null");
|
||||
#endif
|
||||
|
||||
nsCOMPtr<nsIURI> uri;
|
||||
rv = NS_NewURI(getter_AddRefs(uri), aURLSpec);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsCOMPtr<nsIInputStream> in;
|
||||
rv = NS_OpenURI(getter_AddRefs(in), uri);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv) && in, "Error in OpenBlockingStream");
|
||||
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && in, NS_ERROR_FAILURE);
|
||||
|
||||
mProps = do_CreateInstance(kPersistentPropertiesCID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
mAttemptedLoad = mLoaded = PR_TRUE;
|
||||
return mProps->Load(in);
|
||||
}
|
||||
|
||||
nsStringBundle::~nsStringBundle()
|
||||
{
|
||||
}
|
||||
|
||||
nsStringBundle::nsStringBundle() :
|
||||
nsStringBundle::nsStringBundle(const char* aURLSpec) :
|
||||
mPropertiesURL(aURLSpec),
|
||||
mAttemptedLoad(PR_FALSE),
|
||||
mLoaded(PR_FALSE)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStringBundle::Init(const char* aURLSpec)
|
||||
{
|
||||
mPropertiesURL = aURLSpec;
|
||||
return NS_OK;
|
||||
NS_INIT_REFCNT();
|
||||
}
|
||||
|
||||
nsresult
|
||||
|
@ -188,11 +151,17 @@ nsStringBundle::LoadProperties()
|
|||
|
||||
mAttemptedLoad = PR_TRUE;
|
||||
|
||||
//
|
||||
// use eventloop instead
|
||||
//
|
||||
// create an event queue for this thread.
|
||||
nsresult rv;
|
||||
|
||||
// to be turned on once we figure out how to ensure we're loading on
|
||||
// the main thread
|
||||
#ifdef ASYNC_LOADING
|
||||
|
||||
// load the string bundle asynchronously on another thread,
|
||||
// but make sure we block this thread so that LoadProperties() is
|
||||
// synchronous
|
||||
|
||||
// create an event queue for this thread.
|
||||
nsCOMPtr<nsIEventQueueService> service = do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
|
@ -209,25 +178,49 @@ nsStringBundle::LoadProperties()
|
|||
rv = NS_NewStreamLoader(getter_AddRefs(loader),
|
||||
uri,
|
||||
this /*the load observer*/);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "\n-->nsStringBundle::Init: NS_NewStreamLoader failed...\n");
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "\n-->nsStringBundle::Init: NS_NewStreamLoader failed...\n");
|
||||
|
||||
// process events until we're finished.
|
||||
PLEvent *event;
|
||||
while (!mLoaded) {
|
||||
rv = currentThreadQ->WaitForEvent(&event);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "\n-->nsStringBundle::Init: currentThreadQ->WaitForEvent failed...\n");
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
// process events until we're finished.
|
||||
PLEvent *event;
|
||||
while (!mLoaded) {
|
||||
rv = currentThreadQ->WaitForEvent(&event);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "\n-->nsStringBundle::Init: currentThreadQ->WaitForEvent failed...\n");
|
||||
if (NS_FAILED(rv)) break;
|
||||
|
||||
rv = currentThreadQ->HandleEvent(event);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "\n-->nsStringBundle::Init: currentThreadQ->HandleEvent failed...\n");
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
rv = currentThreadQ->HandleEvent(event);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "\n-->nsStringBundle::Init: currentThreadQ->HandleEvent failed...\n");
|
||||
if (NS_FAILED(rv)) break;
|
||||
}
|
||||
|
||||
rv = service->PopThreadEventQueue(currentThreadQ);
|
||||
rv = service->PopThreadEventQueue(currentThreadQ);
|
||||
|
||||
#else
|
||||
|
||||
// do it synchronously
|
||||
nsCOMPtr<nsIURI> uri;
|
||||
rv = NS_NewURI(getter_AddRefs(uri), mPropertiesURL);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsCOMPtr<nsIInputStream> in;
|
||||
rv = NS_OpenURI(getter_AddRefs(in), uri);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv) && in, "Error in OpenBlockingStream");
|
||||
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && in, NS_ERROR_FAILURE);
|
||||
|
||||
mProps = do_CreateInstance(kPersistentPropertiesCID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
mAttemptedLoad = mLoaded = PR_TRUE;
|
||||
rv = mProps->Load(in);
|
||||
|
||||
mLoaded = NS_SUCCEEDED(rv);
|
||||
#endif
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
#ifdef ASYNC_LOADING
|
||||
NS_IMETHODIMP
|
||||
nsStringBundle::OnStreamComplete(nsIStreamLoader* aLoader,
|
||||
nsISupports* context,
|
||||
|
@ -293,8 +286,10 @@ nsStringBundle::OnStreamComplete(nsIStreamLoader* aLoader,
|
|||
return rv;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
nsresult
|
||||
nsStringBundle::GetStringFromID(PRInt32 aID, nsString& aResult)
|
||||
nsStringBundle::GetStringFromID(PRInt32 aID, nsAString& aResult)
|
||||
{
|
||||
nsAutoCMonitor(this);
|
||||
nsAutoString name;
|
||||
|
@ -313,11 +308,11 @@ nsStringBundle::GetStringFromID(PRInt32 aID, nsString& aResult)
|
|||
|
||||
nsresult
|
||||
nsStringBundle::GetStringFromName(const nsAReadableString& aName,
|
||||
nsString& aResult)
|
||||
nsAString& aResult)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
rv = mProps->GetStringProperty(nsAutoString(aName), aResult);
|
||||
rv = mProps->GetStringProperty(aName, aResult);
|
||||
#ifdef DEBUG_tao_
|
||||
char *s = ToNewCString(aResult),
|
||||
*ss = ToNewCString(aName);
|
||||
|
@ -359,8 +354,13 @@ nsStringBundle::FormatStringFromName(const PRUnichar *aName,
|
|||
}
|
||||
|
||||
|
||||
#ifdef ASYNC_LOADING
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS2(nsStringBundle,
|
||||
nsIStringBundle, nsIStreamLoaderObserver)
|
||||
#else
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS1(nsStringBundle,
|
||||
nsIStringBundle)
|
||||
#endif
|
||||
|
||||
/* void GetStringFromID (in long aID, out wstring aResult); */
|
||||
NS_IMETHODIMP
|
||||
|
@ -374,18 +374,12 @@ nsStringBundle::GetStringFromID(PRInt32 aID, PRUnichar **aResult)
|
|||
nsAutoString tmpstr;
|
||||
|
||||
nsresult ret = GetStringFromID(aID, tmpstr);
|
||||
PRInt32 len = tmpstr.Length()+1;
|
||||
if (NS_FAILED(ret) || !len) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
*aResult = (PRUnichar *) PR_Calloc(len, sizeof(PRUnichar));
|
||||
*aResult = (PRUnichar *) memcpy(*aResult, tmpstr.get(), sizeof(PRUnichar)*len);
|
||||
(*aResult)[len-1] = '\0';
|
||||
if (NS_SUCCEEDED(ret))
|
||||
*aResult = ToNewUnicode(tmpstr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* void GetStringFromName ([const] in wstring aName, out wstring aResult); */
|
||||
/* void GetStringFromName (in wstring aName, out wstring aResult); */
|
||||
NS_IMETHODIMP
|
||||
nsStringBundle::GetStringFromName(const PRUnichar *aName, PRUnichar **aResult)
|
||||
{
|
||||
|
@ -396,33 +390,13 @@ nsStringBundle::GetStringFromName(const PRUnichar *aName, PRUnichar **aResult)
|
|||
nsAutoCMonitor(this);
|
||||
*aResult = nsnull;
|
||||
nsAutoString tmpstr;
|
||||
nsAutoString nameStr(aName);
|
||||
rv = GetStringFromName(nameStr, tmpstr);
|
||||
PRInt32 len = tmpstr.Length()+1;
|
||||
if (NS_FAILED(rv) || !len) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
*aResult = (PRUnichar *) PR_Calloc(len, sizeof(PRUnichar));
|
||||
*aResult = (PRUnichar *) memcpy(*aResult, tmpstr.get(), sizeof(PRUnichar)*len);
|
||||
(*aResult)[len-1] = '\0';
|
||||
rv = GetStringFromName(nsDependentString(aName), tmpstr);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
*aResult = ToNewUnicode(tmpstr);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringBundle::GetEnumeration(nsIBidirectionalEnumerator** elements)
|
||||
{
|
||||
if (!mProps)
|
||||
return NS_OK;
|
||||
|
||||
nsAutoCMonitor(this);
|
||||
if (!elements)
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
|
||||
return mProps->EnumerateProperties(elements);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringBundle::GetSimpleEnumeration(nsISimpleEnumerator** elements)
|
||||
{
|
||||
|
@ -432,49 +406,6 @@ nsStringBundle::GetSimpleEnumeration(nsISimpleEnumerator** elements)
|
|||
return mProps->SimpleEnumerateProperties(elements);
|
||||
}
|
||||
|
||||
/* attribute boolean loaded; */
|
||||
NS_IMETHODIMP nsStringBundle::GetLoaded(PRBool *aLoaded)
|
||||
{
|
||||
*aLoaded = mLoaded;
|
||||
return NS_OK;
|
||||
}
|
||||
NS_IMETHODIMP nsStringBundle::SetLoaded(PRBool aLoaded)
|
||||
{
|
||||
mLoaded = aLoaded;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStringBundle::GetLangCountry(nsILocale* aLocale, nsString& lang, nsString& country)
|
||||
{
|
||||
if (!aLocale) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
PRUnichar *lc_name_unichar;
|
||||
nsAutoString lc_name;
|
||||
nsAutoString category; category.AssignWithConversion("NSILOCALE_MESSAGES");
|
||||
nsresult result = aLocale->GetCategory(category.get(), &lc_name_unichar);
|
||||
lc_name.Assign(lc_name_unichar);
|
||||
nsMemory::Free(lc_name_unichar);
|
||||
|
||||
NS_ASSERTION(NS_SUCCEEDED(result),"nsStringBundle::GetLangCountry: locale.GetCategory failed");
|
||||
NS_ASSERTION(lc_name.Length()>0,"nsStringBundle::GetLangCountry: locale.GetCategory failed");
|
||||
|
||||
PRInt32 dash = lc_name.FindCharInSet("-");
|
||||
if (dash > 0) {
|
||||
/*
|
||||
*/
|
||||
PRInt32 count = 0;
|
||||
count = lc_name.Left(lang, dash);
|
||||
count = lc_name.Right(country, (lc_name.Length()-dash-1));
|
||||
}
|
||||
else
|
||||
lang = lc_name;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsStringBundle::FormatString(const PRUnichar *aFormatStr,
|
||||
const PRUnichar **aParams, PRUint32 aLength,
|
||||
|
@ -688,13 +619,6 @@ nsExtensibleStringBundle::FormatStringFromName(const PRUnichar *aName,
|
|||
return nsStringBundle::FormatString(formatStr, aParams, aLength, aResult);
|
||||
}
|
||||
|
||||
nsresult nsExtensibleStringBundle::GetEnumeration(nsIBidirectionalEnumerator ** aResult)
|
||||
{
|
||||
// XXX write me
|
||||
*aResult = NULL;
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
nsresult nsExtensibleStringBundle::GetSimpleEnumeration(nsISimpleEnumerator ** aResult)
|
||||
{
|
||||
// XXX write me
|
||||
|
@ -702,19 +626,6 @@ nsresult nsExtensibleStringBundle::GetSimpleEnumeration(nsISimpleEnumerator ** a
|
|||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
/* attribute boolean loaded; */
|
||||
NS_IMETHODIMP nsExtensibleStringBundle::GetLoaded(PRBool *aLoaded)
|
||||
{
|
||||
*aLoaded = mLoaded;
|
||||
return NS_OK;
|
||||
}
|
||||
NS_IMETHODIMP nsExtensibleStringBundle::SetLoaded(PRBool aLoaded)
|
||||
{
|
||||
mLoaded = aLoaded;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define MAX_CACHED_BUNDLES 16
|
||||
|
@ -741,7 +652,7 @@ public:
|
|||
NS_DECL_NSIOBSERVER
|
||||
|
||||
private:
|
||||
nsresult getStringBundle(const char *aUrl, PRBool async, nsIStringBundle** aResult);
|
||||
nsresult getStringBundle(const char *aUrl, nsIStringBundle** aResult);
|
||||
nsresult FormatWithBundle(nsIStringBundle* bundle, nsresult aStatus,
|
||||
PRUint32 argCount, PRUnichar** argArray,
|
||||
PRUnichar* *result);
|
||||
|
@ -759,7 +670,6 @@ private:
|
|||
|
||||
nsCOMPtr<nsIErrorService> mErrorService;
|
||||
|
||||
const char *mAsync; // temporary; remove after we settle w/ sync/async
|
||||
};
|
||||
|
||||
nsStringBundleService::nsStringBundleService() :
|
||||
|
@ -778,7 +688,6 @@ nsStringBundleService::nsStringBundleService() :
|
|||
mErrorService = do_GetService(kErrorServiceCID);
|
||||
NS_ASSERTION(mErrorService, "Couldn't get error service");
|
||||
|
||||
mAsync = PR_GetEnv("STRRES_ASYNC");
|
||||
}
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS3(nsStringBundleService,
|
||||
|
@ -841,10 +750,8 @@ nsStringBundleService::FlushBundles()
|
|||
|
||||
nsresult
|
||||
nsStringBundleService::getStringBundle(const char *aURLSpec,
|
||||
PRBool async,
|
||||
nsIStringBundle **aResult)
|
||||
{
|
||||
nsresult ret;
|
||||
nsCStringKey completeKey(aURLSpec);
|
||||
|
||||
bundleCacheEntry_t* cacheEntry =
|
||||
|
@ -859,21 +766,10 @@ nsStringBundleService::getStringBundle(const char *aURLSpec,
|
|||
} else {
|
||||
|
||||
// hasn't been cached, so insert it into the hash table
|
||||
nsStringBundle* bundle = new nsStringBundle();
|
||||
nsStringBundle* bundle = new nsStringBundle(aURLSpec);
|
||||
if (!bundle) return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_ADDREF(bundle);
|
||||
|
||||
if (async)
|
||||
ret = bundle->Init(aURLSpec);
|
||||
else
|
||||
ret = bundle->InitSyncStream(aURLSpec);
|
||||
|
||||
|
||||
if (NS_FAILED(ret)) {
|
||||
NS_RELEASE(bundle);
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
cacheEntry = insertIntoCache(bundle, &completeKey);
|
||||
NS_RELEASE(bundle); // cache should now be holding a ref
|
||||
// in the cacheEntry
|
||||
|
@ -947,20 +843,19 @@ nsStringBundleService::CreateBundle(const char* aURLSpec,
|
|||
#ifdef DEBUG_tao_
|
||||
printf("\n++ nsStringBundleService::CreateBundle ++\n");
|
||||
{
|
||||
nsAutoString aURLStr(aURLSpec);
|
||||
char *s = ToNewCString(aURLStr);
|
||||
printf("\n** nsStringBundleService::CreateBundle: %s\n", s?s:"null");
|
||||
printf("\n** nsStringBundleService::CreateBundle: %s\n",
|
||||
aURLSpec ? aURLSpec : "null");
|
||||
delete s;
|
||||
}
|
||||
#endif
|
||||
|
||||
return getStringBundle(aURLSpec, mAsync?PR_TRUE:PR_FALSE, aResult);
|
||||
return getStringBundle(aURLSpec,aResult);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsStringBundleService::CreateAsyncBundle(const char* aURLSpec, nsIStringBundle** aResult)
|
||||
{
|
||||
return getStringBundle(aURLSpec, PR_TRUE, aResult);
|
||||
return getStringBundle(aURLSpec, aResult);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
|
@ -1001,8 +896,8 @@ nsStringBundleService::FormatWithBundle(nsIStringBundle* bundle, nsresult aStatu
|
|||
|
||||
// first try looking up the error message with the string key:
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsAutoString name; name.AssignWithConversion(key);
|
||||
rv = bundle->FormatStringFromName(name.get(), (const PRUnichar**)argArray,
|
||||
rv = bundle->FormatStringFromName(NS_ConvertASCIItoUCS2(key).get(),
|
||||
(const PRUnichar**)argArray,
|
||||
argCount, result);
|
||||
}
|
||||
|
||||
|
@ -1046,7 +941,7 @@ nsStringBundleService::FormatStatusMessage(nsresult aStatus,
|
|||
}
|
||||
|
||||
// format the arguments:
|
||||
nsAutoString args(aStatusArg);
|
||||
const nsAutoString args(aStatusArg);
|
||||
argCount = args.CountChar(PRUnichar('\n')) + 1;
|
||||
NS_ENSURE_ARG(argCount <= 10); // enforce 10-parameter limit
|
||||
PRUnichar* argArray[10];
|
||||
|
@ -1078,13 +973,13 @@ nsStringBundleService::FormatStatusMessage(nsresult aStatus,
|
|||
rv = mErrorService->GetErrorStringBundle(NS_ERROR_GET_MODULE(aStatus),
|
||||
getter_Copies(stringBundleURL));
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = getStringBundle(stringBundleURL, mAsync?PR_TRUE:PR_FALSE, getter_AddRefs(bundle));
|
||||
rv = getStringBundle(stringBundleURL, getter_AddRefs(bundle));
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = FormatWithBundle(bundle, aStatus, argCount, argArray, result);
|
||||
}
|
||||
}
|
||||
if (NS_FAILED(rv)) {
|
||||
rv = getStringBundle(GLOBAL_PROPERTIES, mAsync?PR_TRUE:PR_FALSE, getter_AddRefs(bundle));
|
||||
rv = getStringBundle(GLOBAL_PROPERTIES, getter_AddRefs(bundle));
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = FormatWithBundle(bundle, aStatus, argCount, argArray, result);
|
||||
}
|
||||
|
@ -1102,12 +997,9 @@ done:
|
|||
|
||||
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsStringBundleService, Init)
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAcceptLang)
|
||||
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
{ "String Bundle", NS_STRINGBUNDLESERVICE_CID, NS_STRINGBUNDLE_CONTRACTID, nsStringBundleServiceConstructor},
|
||||
{ "Accept Language", NS_ACCEPTLANG_CID, NS_ACCEPTLANG_CONTRACTID, nsAcceptLangConstructor}
|
||||
};
|
||||
|
||||
NS_IMPL_NSGETMODULE(nsStringBundleModule, components)
|
||||
|
|
|
@ -42,7 +42,6 @@
|
|||
#include "nsReadableUtils.h"
|
||||
#include "nsIPersistentProperties2.h"
|
||||
#include "nsIStringBundle.h"
|
||||
#include "nsIAcceptLang.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include <iostream.h>
|
||||
|
||||
|
@ -83,9 +82,6 @@ static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
|||
static NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
|
||||
static NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID);
|
||||
|
||||
static NS_DEFINE_IID(kAcceptLangCID, NS_ACCEPTLANG_CID);
|
||||
static NS_DEFINE_IID(kIAcceptLangIID, NS_IACCEPTLANG_IID);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// let add some locale stuff
|
||||
|
@ -149,14 +145,6 @@ main(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
|
||||
nsIAcceptLang* Aservice = nsnull;
|
||||
ret = nsServiceManager::GetService(kAcceptLangCID,
|
||||
kIAcceptLangIID, (nsISupports**) &Aservice);
|
||||
if (NS_FAILED(ret)) {
|
||||
printf("cannot create AcceptLang service\n");
|
||||
return 1;
|
||||
}
|
||||
printf("\n ** created AcceptLang service\n");
|
||||
|
||||
nsIStringBundle* bundle = nsnull;
|
||||
|
||||
|
@ -195,58 +183,5 @@ main(int argc, char *argv[])
|
|||
value = ToNewCString(v);
|
||||
cout << "file=\"" << value << "\"" << endl;
|
||||
|
||||
nsIBidirectionalEnumerator* propEnum = nsnull;
|
||||
ret = bundle->GetEnumeration(&propEnum);
|
||||
if (NS_FAILED(ret)) {
|
||||
printf("cannot get enumeration\n");
|
||||
return 1;
|
||||
}
|
||||
ret = propEnum->First();
|
||||
if (NS_FAILED(ret))
|
||||
{
|
||||
printf("enumerator is empty\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
cout << endl << "Key" << "\t" << "Value" << endl;
|
||||
cout << "---" << "\t" << "-----" << endl;
|
||||
while (NS_SUCCEEDED(ret))
|
||||
{
|
||||
nsIPropertyElement* propElem = nsnull;
|
||||
ret = propEnum->CurrentItem((nsISupports**)&propElem);
|
||||
if (NS_FAILED(ret)) {
|
||||
printf("failed to get current item\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
PRUnichar *pKey = nsnull;
|
||||
PRUnichar *pVal = nsnull;
|
||||
|
||||
ret = propElem->GetKey(&pKey);
|
||||
if (NS_FAILED(ret)) {
|
||||
printf("failed to get current element's key\n");
|
||||
return 1;
|
||||
}
|
||||
ret = propElem->GetValue(&pVal);
|
||||
if (NS_FAILED(ret)) {
|
||||
printf("failed to get current element's value\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
nsAutoString keyAdjustedLengthBuff(pKey);
|
||||
nsAutoString valAdjustedLengthBuff(pVal);
|
||||
|
||||
char* keyCStr = ToNewCString(keyAdjustedLengthBuff);
|
||||
char* valCStr = ToNewCString(valAdjustedLengthBuff);
|
||||
if (keyCStr && valCStr)
|
||||
cout << keyCStr << "\t" << valCStr << endl;
|
||||
delete[] keyCStr;
|
||||
delete[] valCStr;
|
||||
delete[] pKey;
|
||||
delete[] pVal;
|
||||
|
||||
ret = propEnum->Next();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче