зеркало из https://github.com/mozilla/gecko-dev.git
Fix for 67576; /r=valeski; /sr=brendan@mozilla.org
This commit is contained in:
Родитель
886075680d
Коммит
2333d6b171
|
@ -27,10 +27,12 @@
|
|||
|
||||
#include "pratom.h"
|
||||
#include "nsClassicCharDetDll.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
#include "nsISupports.h"
|
||||
#include "nsIComponentManager.h"
|
||||
#include "nsIFactory.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsIStringCharsetDetector.h"
|
||||
|
@ -41,112 +43,26 @@ static NS_DEFINE_CID(kJAClassicDetectorCID, NS_JA_CLASSIC_DETECTOR_CID);
|
|||
static NS_DEFINE_CID(kJAClassicStringDetectorCID, NS_JA_CLASSIC_STRING_DETECTOR_CID);
|
||||
static NS_DEFINE_CID(kKOClassicDetectorCID, NS_KO_CLASSIC_DETECTOR_CID);
|
||||
static NS_DEFINE_CID(kKOClassicStringDetectorCID, NS_KO_CLASSIC_STRING_DETECTOR_CID);
|
||||
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
||||
static NS_DEFINE_IID(kFactoryIID, NS_IFACTORY_IID);
|
||||
|
||||
PRInt32 g_InstanceCount = 0;
|
||||
PRInt32 g_LockCount = 0;
|
||||
|
||||
|
||||
extern "C" NS_EXPORT nsresult NSGetFactory(nsISupports* aServMgr,
|
||||
const nsCID &aClass,
|
||||
const char *aClassName,
|
||||
const char *aContractID,
|
||||
nsIFactory **aFactory)
|
||||
{
|
||||
if (aFactory == NULL) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJACharsetClassicDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJAStringCharsetClassicDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsKOCharsetClassicDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsKOStringCharsetClassicDetector);
|
||||
|
||||
nsIFactory *factory = nsnull;
|
||||
if (aClass.Equals(kJAClassicDetectorCID)) {
|
||||
;
|
||||
//bug#13844 disable this until find out the reason of the freeze
|
||||
//factory = NEW_JA_CLASSICDETECTOR_FACTORY();
|
||||
} else if (aClass.Equals(kJAClassicStringDetectorCID)) {
|
||||
factory = NEW_JA_STRING_CLASSICDETECTOR_FACTORY();
|
||||
#if 0
|
||||
} else if (aClass.Equals(kKOClassicDetectorCID)) {
|
||||
factory = NEW_KO_CLASSICDETECTOR_FACTORY();
|
||||
} else if (aClass.Equals(kKOClassicStringDetectorCID)) {
|
||||
factory = NEW_KO_STRING_CLASSICDETECTOR_FACTORY();
|
||||
#endif
|
||||
}
|
||||
static nsModuleComponentInfo components[] = {
|
||||
{ "Classic JA Charset Detector", NS_JA_CLASSIC_DETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "jaclassic", nsJACharsetClassicDetectorConstructor},
|
||||
{ "Classic JA String Charset Detector", NS_JA_CLASSIC_DETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "jaclassic", nsJAStringCharsetClassicDetectorConstructor},
|
||||
{ "Classic KO Charset Detector", NS_KO_CLASSIC_DETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "koclassic", nsKOCharsetClassicDetectorConstructor},
|
||||
{ "Classic KO String Charset Detector", NS_KO_CLASSIC_STRING_DETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "koclassic", nsKOStringCharsetClassicDetectorConstructor}
|
||||
};
|
||||
|
||||
if(nsnull != factory) {
|
||||
nsresult res = factory->QueryInterface(kFactoryIID, (void **) aFactory);
|
||||
if (NS_FAILED(res)) {
|
||||
*aFactory = NULL;
|
||||
delete factory;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
extern "C" NS_EXPORT PRBool NSCanUnload(nsISupports* aServMgr) {
|
||||
return PRBool(g_InstanceCount == 0 && g_LockCount == 0);
|
||||
}
|
||||
extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* aServMgr, const char *path)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsIComponentManager* compMgr;
|
||||
rv = servMgr->GetService(kComponentManagerCID,
|
||||
NS_GET_IID(nsIComponentManager),
|
||||
(nsISupports**)&compMgr);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = compMgr->RegisterComponent(kJAClassicDetectorCID,
|
||||
"Classic JA Charset Detector",
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "jaclassic",
|
||||
path,
|
||||
PR_TRUE, PR_TRUE);
|
||||
|
||||
rv = compMgr->RegisterComponent(kJAClassicStringDetectorCID,
|
||||
"Classic JA String Charset Detector",
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "jaclassic",
|
||||
path,
|
||||
PR_TRUE, PR_TRUE);
|
||||
|
||||
rv = compMgr->RegisterComponent(kKOClassicDetectorCID,
|
||||
"Classic KO Charset Detector",
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "koclassic",
|
||||
path,
|
||||
PR_TRUE, PR_TRUE);
|
||||
|
||||
rv = compMgr->RegisterComponent(kKOClassicStringDetectorCID,
|
||||
"Classic KO String Charset Detector",
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "koclassic",
|
||||
path,
|
||||
PR_TRUE, PR_TRUE);
|
||||
|
||||
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, const char *path)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsIComponentManager* compMgr;
|
||||
rv = servMgr->GetService(kComponentManagerCID,
|
||||
NS_GET_IID(nsIComponentManager),
|
||||
(nsISupports**)&compMgr);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = compMgr->UnregisterComponent(kJAClassicDetectorCID, path);
|
||||
rv = compMgr->UnregisterComponent(kJAClassicStringDetectorCID, path);
|
||||
rv = compMgr->UnregisterComponent(kKOClassicDetectorCID, path);
|
||||
rv = compMgr->UnregisterComponent(kKOClassicStringDetectorCID, path);
|
||||
|
||||
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
|
||||
return rv;
|
||||
}
|
||||
NS_IMPL_NSGETMODULE("nsCharDetModuleClassic", components);
|
||||
|
||||
|
|
|
@ -197,26 +197,6 @@ static nsresult JA_AutoCharsetDetectBuffer(const char* aBuffer, const PRInt32 aL
|
|||
}
|
||||
|
||||
//==========================================================
|
||||
|
||||
|
||||
class nsClassicDetector :
|
||||
public nsICharsetDetector // Implement the interface
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
nsClassicDetector(const char* language);
|
||||
virtual ~nsClassicDetector();
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool* oDontFeedMe);
|
||||
NS_IMETHOD Done();
|
||||
|
||||
private:
|
||||
nsICharsetDetectionObserver* mObserver;
|
||||
char mCharset[65];
|
||||
char mLanguage[32];
|
||||
};
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsClassicDetector, NS_GET_IID(nsICharsetDetector));
|
||||
|
||||
//----------------------------------------------------------
|
||||
|
@ -230,7 +210,6 @@ nsClassicDetector::nsClassicDetector(const char* language)
|
|||
//----------------------------------------------------------
|
||||
nsClassicDetector::~nsClassicDetector()
|
||||
{
|
||||
NS_IF_RELEASE(mObserver);
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
//----------------------------------------------------------
|
||||
|
@ -241,7 +220,6 @@ NS_IMETHODIMP nsClassicDetector::Init(
|
|||
if(nsnull == aObserver)
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
|
||||
NS_IF_ADDREF(aObserver);
|
||||
mObserver = aObserver;
|
||||
|
||||
return NS_OK;
|
||||
|
@ -275,21 +253,6 @@ NS_IMETHODIMP nsClassicDetector::Done()
|
|||
}
|
||||
|
||||
//==========================================================
|
||||
class nsClassicStringDetector :
|
||||
public nsIStringCharsetDetector // Implement the interface
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
nsClassicStringDetector(const char* language);
|
||||
virtual ~nsClassicStringDetector();
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset,
|
||||
nsDetectionConfident &oConfident);
|
||||
protected:
|
||||
char mCharset[65];
|
||||
char mLanguage[32];
|
||||
};
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsClassicStringDetector, NS_GET_IID(nsIStringCharsetDetector));
|
||||
|
||||
|
@ -323,76 +286,3 @@ NS_IMETHODIMP nsClassicStringDetector::DoIt(const char* aBuf, PRUint32 aLen,
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
//==========================================================
|
||||
class nsClassicDetectorFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsClassicDetectorFactory(const char* language, PRBool stringBase) {
|
||||
NS_INIT_REFCNT();
|
||||
PL_strcpy(mLanguage, language);
|
||||
mStringBase = stringBase;
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~nsClassicDetectorFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
private:
|
||||
char mLanguage[32];
|
||||
PRBool mStringBase;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
NS_DEFINE_IID( kIFactoryIID, NS_IFACTORY_IID);
|
||||
NS_IMPL_ISUPPORTS( nsClassicDetectorFactory , kIFactoryIID);
|
||||
|
||||
NS_IMETHODIMP nsClassicDetectorFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsISupports *inst = nsnull;
|
||||
if (mStringBase)
|
||||
inst = (nsISupports *) new nsClassicStringDetector(mLanguage);
|
||||
else
|
||||
inst = (nsISupports *) new nsClassicDetector(mLanguage);
|
||||
if(NULL == inst) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
NS_ADDREF(inst); // Stabilize
|
||||
nsresult res =inst->QueryInterface(aIID, aResult);
|
||||
NS_RELEASE(inst); // Destabilize and avoid leaks. Avoid calling delete <interface pointer>
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsClassicDetectorFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//==========================================================
|
||||
nsIFactory* NEW_JA_CLASSICDETECTOR_FACTORY() {
|
||||
return new nsClassicDetectorFactory("ja", PR_FALSE);
|
||||
}
|
||||
nsIFactory* NEW_JA_STRING_CLASSICDETECTOR_FACTORY() {
|
||||
return new nsClassicDetectorFactory("ja", PR_TRUE);
|
||||
}
|
||||
nsIFactory* NEW_KO_CLASSICDETECTOR_FACTORY() {
|
||||
return new nsClassicDetectorFactory("ko", PR_FALSE);
|
||||
}
|
||||
nsIFactory* NEW_KO_STRING_CLASSICDETECTOR_FACTORY() {
|
||||
return new nsClassicDetectorFactory("ko", PR_TRUE);
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#ifndef nsClassicDetectors_h__
|
||||
#define nsClassicDetectors_h__
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIFactory.h"
|
||||
|
||||
// {1D2394A0-542A-11d3-914D-006008A6EDF6}
|
||||
|
@ -41,9 +42,67 @@
|
|||
#define NS_KO_CLASSIC_STRING_DETECTOR_CID \
|
||||
{ 0x1d2394a3, 0x542a, 0x11d3, { 0x91, 0x4d, 0x0, 0x60, 0x8, 0xa6, 0xed, 0xf6 } }
|
||||
|
||||
nsIFactory* NEW_JA_CLASSICDETECTOR_FACTORY();
|
||||
nsIFactory* NEW_JA_STRING_CLASSICDETECTOR_FACTORY();
|
||||
nsIFactory* NEW_KO_CLASSICDETECTOR_FACTORY();
|
||||
nsIFactory* NEW_KO_STRING_CLASSICDETECTOR_FACTORY();
|
||||
class nsClassicDetector :
|
||||
public nsICharsetDetector // Implement the interface
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
nsClassicDetector(const char* language);
|
||||
virtual ~nsClassicDetector();
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool* oDontFeedMe);
|
||||
NS_IMETHOD Done();
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsICharsetDetectionObserver> mObserver;
|
||||
char mCharset[65];
|
||||
char mLanguage[32];
|
||||
};
|
||||
|
||||
|
||||
class nsClassicStringDetector :
|
||||
public nsIStringCharsetDetector // Implement the interface
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
nsClassicStringDetector(const char* language);
|
||||
virtual ~nsClassicStringDetector();
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset,
|
||||
nsDetectionConfident &oConfident);
|
||||
protected:
|
||||
char mCharset[65];
|
||||
char mLanguage[32];
|
||||
};
|
||||
|
||||
class nsJACharsetClassicDetector : public nsClassicDetector
|
||||
{
|
||||
public:
|
||||
nsJACharsetClassicDetector()
|
||||
: nsClassicDetector("ja") {};
|
||||
};
|
||||
|
||||
class nsJAStringCharsetClassicDetector : public nsClassicStringDetector
|
||||
{
|
||||
public:
|
||||
nsJAStringCharsetClassicDetector()
|
||||
: nsClassicStringDetector("ja") {};
|
||||
};
|
||||
|
||||
class nsKOCharsetClassicDetector : public nsClassicDetector
|
||||
{
|
||||
public:
|
||||
nsKOCharsetClassicDetector()
|
||||
: nsClassicDetector("ko") {};
|
||||
};
|
||||
|
||||
class nsKOStringCharsetClassicDetector : public nsClassicStringDetector
|
||||
{
|
||||
public:
|
||||
nsKOStringCharsetClassicDetector()
|
||||
: nsClassicStringDetector("ko") {};
|
||||
};
|
||||
|
||||
#endif /* nsClassicDetectors_h__ */
|
||||
|
|
|
@ -24,6 +24,11 @@
|
|||
#define nsCharDetDll_h__
|
||||
|
||||
#include "prtypes.h"
|
||||
#include "nsIFactory.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
#include "nsIStringCharsetDetector.h"
|
||||
#include "nsICharsetDetectionAdaptor.h"
|
||||
|
||||
extern "C" PRInt32 g_InstanceCount;
|
||||
extern "C" PRInt32 g_LockCount;
|
||||
|
|
|
@ -35,20 +35,37 @@
|
|||
#include "nsIComponentManager.h"
|
||||
#include "nsIFactory.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsIWebShellServices.h"
|
||||
#include "nsIStringCharsetDetector.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsMetaCharsetObserver.h"
|
||||
#include "nsXMLEncodingObserver.h"
|
||||
#include "nsDetectionAdaptor.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsIStringCharsetDetector.h"
|
||||
|
||||
#include "nsDocumentCharsetInfo.h"
|
||||
#include "nsCyrillicDetector.h"
|
||||
#include "nsPSMDetectors.h"
|
||||
#include "nsMetaCharsetCID.h"
|
||||
#include "nsXMLEncodingCID.h"
|
||||
#include "nsCharsetDetectionAdaptorCID.h"
|
||||
#include "nsDocumentCharsetInfo.h"
|
||||
#include "nsDocumentCharsetInfoCID.h"
|
||||
|
||||
#include "nsPSMDetectors.h"
|
||||
PRInt32 g_InstanceCount = 0;
|
||||
PRInt32 g_LockCount = 0;
|
||||
|
||||
//#define INCLUDE_DBGDETECTOR
|
||||
#ifdef INCLUDE_DBGDETECTOR
|
||||
// for debuging only
|
||||
#include "nsDebugDetector.h"
|
||||
NS_DEFINE_CID(k1stBlkDbgDetectorCID, NS_1STBLKDBG_DETECTOR_CID);
|
||||
NS_DEFINE_CID(k2ndBlkDbgDetectorCID, NS_2NDBLKDBG_DETECTOR_CID);
|
||||
NS_DEFINE_CID(klastBlkDbgDetectorCID, NS_LASTBLKDBG_DETECTOR_CID);
|
||||
#endif /* INCLUDE_DBGDETECTOR */
|
||||
|
||||
NS_DEFINE_CID(kMetaCharsetCID, NS_META_CHARSET_CID);
|
||||
NS_DEFINE_CID(kXMLEncodingCID, NS_XML_ENCODING_CID);
|
||||
NS_DEFINE_CID(kCharsetDetectionAdaptorCID, NS_CHARSET_DETECTION_ADAPTOR_CID);
|
||||
NS_DEFINE_CID(kDocumentCharsetInfoCID, NS_DOCUMENTCHARSETINFO_CID);
|
||||
NS_DEFINE_CID(kJAPSMDetectorCID, NS_JA_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kJAStringPSMDetectorCID, NS_JA_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kKOPSMDetectorCID, NS_KO_PSMDETECTOR_CID);
|
||||
|
@ -61,271 +78,47 @@ NS_DEFINE_CID(kZHPSMDetectorCID, NS_ZH_PSMDETECTOR_CID);
|
|||
NS_DEFINE_CID(kZHStringPSMDetectorCID, NS_ZH_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kCJKPSMDetectorCID, NS_CJK_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kCJKStringPSMDetectorCID, NS_CJK_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kRUProbDetectorCID, NS_RU_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kUKProbDetectorCID, NS_UK_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kRUStringProbDetectorCID, NS_RU_STRING_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kUKStringProbDetectorCID, NS_UK_STRING_PROBDETECTOR_CID);
|
||||
|
||||
#include "nsCyrillicDetector.h"
|
||||
NS_DEFINE_CID(kRUProbDetectorCID, NS_RU_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kRUStringProbDetectorCID, NS_RU_STRING_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kUKProbDetectorCID, NS_UK_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kUKStringProbDetectorCID, NS_UK_STRING_PROBDETECTOR_CID);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMetaCharsetObserver);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDocumentCharsetInfo);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsXMLEncodingObserver);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsDetectionAdaptor);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJAPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsJAStringPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsKOPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsKOStringPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsZHTWPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsZHTWStringPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsZHCNPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsZHCNStringPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsZHPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsZHStringPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCJKPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsCJKStringPSMDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsRUProbDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsUKProbDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsRUStringProbDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsUKStringProbDetector);
|
||||
|
||||
|
||||
//#define INCLUDE_DBGDETECTOR
|
||||
#ifdef INCLUDE_DBGDETECTOR
|
||||
// for debuging only
|
||||
#include "nsDebugDetector.h"
|
||||
NS_DEFINE_CID(k1stBlkDbgDetectorCID, NS_1STBLKDBG_DETECTOR_CID);
|
||||
NS_DEFINE_CID(k2ndBlkDbgDetectorCID, NS_2NDBLKDBG_DETECTOR_CID);
|
||||
NS_DEFINE_CID(klastBlkDbgDetectorCID, NS_LASTBLKDBG_DETECTOR_CID);
|
||||
#endif /* INCLUDE_DBGDETECTOR */
|
||||
|
||||
NS_DEFINE_IID(kFactoryIID, NS_IFACTORY_IID);
|
||||
NS_DEFINE_CID(kMetaCharsetCID, NS_META_CHARSET_CID);
|
||||
NS_DEFINE_CID(kXMLEncodingCID, NS_XML_ENCODING_CID);
|
||||
NS_DEFINE_CID(kCharsetDetectionAdaptorCID, NS_CHARSET_DETECTION_ADAPTOR_CID);
|
||||
NS_DEFINE_CID(kDocumentCharsetInfoCID, NS_DOCUMENTCHARSETINFO_CID);
|
||||
|
||||
PRInt32 g_InstanceCount = 0;
|
||||
PRInt32 g_LockCount = 0;
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
// Module implementation for the CharDet library
|
||||
class nsCharDetModule : public nsIModule
|
||||
{
|
||||
public:
|
||||
nsCharDetModule();
|
||||
virtual ~nsCharDetModule();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_DECL_NSIMODULE
|
||||
|
||||
protected:
|
||||
nsresult Initialize();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
PRBool mInitialized;
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
nsCharDetModule::nsCharDetModule()
|
||||
: mInitialized(PR_FALSE)
|
||||
{
|
||||
NS_INIT_ISUPPORTS();
|
||||
}
|
||||
|
||||
nsCharDetModule::~nsCharDetModule()
|
||||
{
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsCharDetModule, nsIModule)
|
||||
|
||||
// Perform our one-time intialization for this module
|
||||
nsresult
|
||||
nsCharDetModule::Initialize()
|
||||
{
|
||||
if (mInitialized) {
|
||||
return NS_OK;
|
||||
}
|
||||
mInitialized = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Shutdown this module, releasing all of the module resources
|
||||
void
|
||||
nsCharDetModule::Shutdown()
|
||||
{
|
||||
}
|
||||
|
||||
// Create a factory object for creating instances of aClass.
|
||||
NS_IMETHODIMP
|
||||
nsCharDetModule::GetClassObject(nsIComponentManager *aCompMgr,
|
||||
const nsCID& aClass,
|
||||
const nsIID& aIID,
|
||||
void** r_classObj)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
// Defensive programming: Initialize *r_classObj in case of error below
|
||||
if (!r_classObj) {
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
}
|
||||
*r_classObj = NULL;
|
||||
|
||||
// Do one-time-only initialization if necessary
|
||||
if (!mInitialized) {
|
||||
rv = Initialize();
|
||||
if (NS_FAILED(rv)) {
|
||||
// Initialization failed! yikes!
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
// Choose the appropriate factory, based on the desired instance
|
||||
// class type (aClass).
|
||||
nsIFactory* factory = nsnull;
|
||||
if (aClass.Equals(kMetaCharsetCID)) {
|
||||
factory = NEW_META_CHARSET_OBSERVER_FACTORY();
|
||||
} else if (aClass.Equals(kDocumentCharsetInfoCID)) {
|
||||
factory = NEW_DOCUMENT_CHARSET_INFO_FACTORY();
|
||||
} else if (aClass.Equals(kXMLEncodingCID)) {
|
||||
factory = NEW_XML_ENCODING_OBSERVER_FACTORY();
|
||||
} else if (aClass.Equals(kCharsetDetectionAdaptorCID)) {
|
||||
factory = NEW_DETECTION_ADAPTOR_FACTORY();
|
||||
} else if (aClass.Equals(kJAPSMDetectorCID) ||
|
||||
aClass.Equals(kJAStringPSMDetectorCID) ||
|
||||
aClass.Equals(kKOPSMDetectorCID) ||
|
||||
aClass.Equals(kKOStringPSMDetectorCID) ||
|
||||
aClass.Equals(kZHCNPSMDetectorCID) ||
|
||||
aClass.Equals(kZHCNStringPSMDetectorCID) ||
|
||||
aClass.Equals(kZHTWPSMDetectorCID) ||
|
||||
aClass.Equals(kZHTWStringPSMDetectorCID) ||
|
||||
aClass.Equals(kZHPSMDetectorCID) ||
|
||||
aClass.Equals(kZHStringPSMDetectorCID) ||
|
||||
aClass.Equals(kCJKPSMDetectorCID) ||
|
||||
aClass.Equals(kCJKStringPSMDetectorCID)
|
||||
)
|
||||
{
|
||||
factory = NEW_PSMDETECTOR_FACTORY(aClass);
|
||||
} else if (aClass.Equals(kRUProbDetectorCID) ||
|
||||
aClass.Equals(kRUStringProbDetectorCID) ||
|
||||
aClass.Equals(kUKProbDetectorCID) ||
|
||||
aClass.Equals(kUKStringProbDetectorCID)
|
||||
)
|
||||
{
|
||||
factory = NEW_PROBDETECTOR_FACTORY(aClass);
|
||||
#ifdef INCLUDE_DBGDETECTOR
|
||||
} else if (aClass.Equals(k1stBlkDbgDetectorCID)) {
|
||||
factory = NEW_1STBLKDBG_DETECTOR_FACTORY();
|
||||
} else if (aClass.Equals(k2ndBlkDbgDetectorCID)) {
|
||||
factory = NEW_2NDBLKDBG_DETECTOR_FACTORY();
|
||||
} else if (aClass.Equals(klastBlkDbgDetectorCID)) {
|
||||
factory = NEW_LASTBLKDBG_DETECTOR_FACTORY();
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(ns1stBlkDbgDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(ns2ndBlkDbgDetector);
|
||||
NS_GENERIC_FACTORY_CONSTRUCTOR(nsLastBlkDbgDetector);
|
||||
#endif /* INCLUDE_DBGDETECTOR */
|
||||
}
|
||||
else {
|
||||
rv = NS_ERROR_FACTORY_NOT_REGISTERED;
|
||||
#ifdef DEBUG
|
||||
char* cs = aClass.ToString();
|
||||
printf("+++ nsCharDetModule: unable to create factory for %s\n", cs);
|
||||
nsCRT::free(cs);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (factory) {
|
||||
rv = factory->QueryInterface(aIID, r_classObj);
|
||||
if (NS_FAILED(rv)) {
|
||||
delete factory; // XXX only works if virtual dtors were used!
|
||||
}
|
||||
}
|
||||
else
|
||||
rv = NS_ERROR_FACTORY_NOT_LOADED;
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
struct Components {
|
||||
const char* mDescription;
|
||||
const nsID* mCID;
|
||||
const char* mContractID;
|
||||
};
|
||||
|
||||
// The list of components we register
|
||||
static Components gComponents[] = {
|
||||
{ "Meta Charset", &kMetaCharsetCID,
|
||||
NS_META_CHARSET_CONTRACTID, },
|
||||
{ "Document Charset Info", &kDocumentCharsetInfoCID,
|
||||
NS_DOCUMENTCHARSETINFO_CONTRACTID, },
|
||||
{ "XML Encoding", &kXMLEncodingCID,
|
||||
NS_XML_ENCODING_CONTRACTID, },
|
||||
{ "Charset Detection Adaptor", &kCharsetDetectionAdaptorCID,
|
||||
NS_CHARSET_DETECTION_ADAPTOR_CONTRACTID, },
|
||||
{ "PSM based Japanese Charset Detector", &kJAPSMDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ja_parallel_state_machine", },
|
||||
{ "PSM based Japanese String Charset Detector", &kJAStringPSMDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ja_parallel_state_machine", },
|
||||
{ "PSM based Korean Charset Detector", &kKOPSMDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ko_parallel_state_machine", },
|
||||
{ "PSM based Korean String Charset Detector", &kKOStringPSMDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ko_parallel_state_machine", },
|
||||
{ "PSM based Traditional Chinese Charset Detector", &kZHTWPSMDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "zhtw_parallel_state_machine", },
|
||||
{ "PSM based Traditional Chinese String Charset Detector",
|
||||
&kZHTWStringPSMDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "zhtw_parallel_state_machine", },
|
||||
{ "PSM based Simplified Chinese Charset Detector", &kZHCNPSMDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "zhcn_parallel_state_machine", },
|
||||
{ "PSM based Simplified Chinese String Charset Detector",
|
||||
&kZHCNStringPSMDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "zhcn_parallel_state_machine", },
|
||||
{ "PSM based Chinese Charset Detector", &kZHPSMDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "zh_parallel_state_machine", },
|
||||
{ "PSM based Chinese String Charset Detector", &kZHStringPSMDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "zh_parallel_state_machine", },
|
||||
{ "PSM based CJK Charset Detector", &kCJKPSMDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "cjk_parallel_state_machine", },
|
||||
{ "PSM based CJK String Charset Detector", &kCJKStringPSMDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "cjk_parallel_state_machine", },
|
||||
{ "Probability based Russian Charset Detector", &kRUProbDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ruprob", },
|
||||
{ "Probability based Ukrainian Charset Detector", &kUKProbDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ukprob", },
|
||||
{ "Probability based Russian String Charset Detector",
|
||||
&kRUStringProbDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ruprob", },
|
||||
{ "Probability based Ukrainian String Charset Detector",
|
||||
&kUKStringProbDetectorCID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ukprob", },
|
||||
#ifdef INCLUDE_DBGDETECTOR
|
||||
{ "Debuging Detector 1st block", &k1stBlkDbgDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "1stblkdbg", },
|
||||
{ "Debuging Detector 2nd block", &k2ndBlkDbgDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "2ndblkdbg", },
|
||||
{ "Debuging Detector last block", &klastBlkDbgDetectorCID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "lastblkdbg", },
|
||||
#endif /* INCLUDE_DBGDETECTOR */
|
||||
};
|
||||
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsCharDetModule::RegisterSelf(nsIComponentManager *aCompMgr,
|
||||
nsIFile* aPath,
|
||||
const char* registryLocation,
|
||||
const char* componentType)
|
||||
static NS_METHOD nsDetectionAdaptorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("*** Registering CharDet components\n");
|
||||
#endif
|
||||
|
||||
Components* cp = gComponents;
|
||||
Components* end = cp + NUM_COMPONENTS;
|
||||
while (cp < end) {
|
||||
rv = aCompMgr->RegisterComponentSpec(*cp->mCID, cp->mDescription,
|
||||
cp->mContractID, aPath, PR_TRUE,
|
||||
PR_TRUE);
|
||||
if (NS_FAILED(rv)) {
|
||||
#ifdef DEBUG
|
||||
printf("nsCharDetModule: unable to register %s component => %x\n",
|
||||
cp->mDescription, rv);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
|
||||
// get the registry
|
||||
nsRegistryKey key;
|
||||
nsIRegistry* registry;
|
||||
rv = nsServiceManager::GetService(NS_REGISTRY_CONTRACTID,
|
||||
NS_GET_IID(nsIRegistry),
|
||||
(nsISupports**)®istry);
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
@ -334,7 +127,7 @@ nsCharDetModule::RegisterSelf(nsIComponentManager *aCompMgr,
|
|||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
goto done;
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
|
@ -343,122 +136,318 @@ nsCharDetModule::RegisterSelf(nsIComponentManager *aCompMgr,
|
|||
rv = registry-> SetStringUTF8(key, "type", "off");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Off");
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsJAPSMDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "ja_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "ja_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Japanese");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsKOPSMDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "ko_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "ko_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Korean");
|
||||
}
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "zhtw_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "zhtw_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Traditional Chinese");
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsZHTWPSMDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "zhcn_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "zhtw_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Simplified Chinese");
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "zh_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "zh_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Chinese");
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "zhtw_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "zhtw_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Traditional Chinese");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsZHCNPSMDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "cjk_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "cjk_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "East Asian");
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "zhcn_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "zhtw_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Simplified Chinese");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsZHPSMDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "zh_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "zh_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Chinese");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsCJKPSMDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "cjk_parallel_state_machine" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "cjk_parallel_state_machine");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "East Asian");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsRUProbDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "ruprob" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "ruprob");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Russian");
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
static NS_METHOD nsUKProbDetectorRegistrationProc(nsIComponentManager *aCompMgr,
|
||||
nsIFile *aPath,
|
||||
const char *registryLocation,
|
||||
const char *componentType,
|
||||
const nsModuleComponentInfo *info)
|
||||
{
|
||||
nsRegistryKey key;
|
||||
nsresult rv = NS_OK;
|
||||
nsCOMPtr<nsIRegistry> registry = do_GetService(NS_REGISTRY_CONTRACTID, &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// open the registry
|
||||
rv = registry->OpenWellKnownRegistry(
|
||||
nsIRegistry::ApplicationComponentRegistry);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = registry -> AddSubtree(nsIRegistry::Common,
|
||||
NS_CHARSET_DETECTOR_REG_BASE "ukprob" ,&key);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = registry-> SetStringUTF8(key, "type", "ukprob");
|
||||
rv = registry-> SetStringUTF8(key, "defaultEnglishText", "Ukrainian");
|
||||
}
|
||||
|
||||
done:
|
||||
if(nsnull != registry) {
|
||||
nsServiceManager::ReleaseService(NS_REGISTRY_CONTRACTID, registry);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsCharDetModule::UnregisterSelf(nsIComponentManager* aCompMgr,
|
||||
nsIFile* aPath,
|
||||
const char* registryLocation)
|
||||
// Component Table
|
||||
static nsModuleComponentInfo components[] =
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("*** Unregistering CharDet components\n");
|
||||
#endif
|
||||
Components* cp = gComponents;
|
||||
Components* end = cp + NUM_COMPONENTS;
|
||||
while (cp < end) {
|
||||
nsresult rv = aCompMgr->UnregisterComponentSpec(*cp->mCID, aPath);
|
||||
if (NS_FAILED(rv)) {
|
||||
#ifdef DEBUG
|
||||
printf("nsCharDetModule: unable to unregister %s component => %x\n",
|
||||
cp->mDescription, rv);
|
||||
#endif
|
||||
}
|
||||
cp++;
|
||||
}
|
||||
{ "Meta Charset", NS_META_CHARSET_CID,
|
||||
NS_META_CHARSET_CONTRACTID, nsMetaCharsetObserverConstructor,
|
||||
NULL, NULL},
|
||||
{ "Document Charset Info", NS_DOCUMENTCHARSETINFO_CID,
|
||||
NS_DOCUMENTCHARSETINFO_CONTRACTID, nsDocumentCharsetInfoConstructor,
|
||||
NULL, NULL},
|
||||
{ "XML Encoding", NS_XML_ENCODING_CID,
|
||||
NS_XML_ENCODING_CONTRACTID, nsXMLEncodingObserverConstructor,
|
||||
NULL, NULL},
|
||||
{ "Charset Detection Adaptor", NS_CHARSET_DETECTION_ADAPTOR_CID,
|
||||
NS_CHARSET_DETECTION_ADAPTOR_CONTRACTID, nsDetectionAdaptorConstructor,
|
||||
nsDetectionAdaptorRegistrationProc, NULL},
|
||||
{ "PSM based Japanese Charset Detector", NS_JA_PSMDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ja_parallel_state_machine", nsJAPSMDetectorConstructor,
|
||||
nsJAPSMDetectorRegistrationProc, NULL},
|
||||
{ "PSM based Japanese String Charset Detector", NS_JA_STRING_PSMDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ja_parallel_state_machine", nsJAStringPSMDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "PSM based Korean Charset Detector", NS_KO_PSMDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ko_parallel_state_machine", nsKOPSMDetectorConstructor,
|
||||
nsKOPSMDetectorRegistrationProc, NULL},
|
||||
{ "PSM based Korean String Charset Detector", NS_KO_STRING_PSMDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ko_parallel_state_machine", nsKOStringPSMDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "PSM based Traditional Chinese Charset Detector", NS_ZHTW_PSMDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "zhtw_parallel_state_machine", nsZHTWPSMDetectorConstructor,
|
||||
nsZHTWPSMDetectorRegistrationProc, NULL},
|
||||
{ "PSM based Traditional Chinese String Charset Detector", NS_ZHTW_STRING_PSMDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "zhtw_parallel_state_machine", nsZHTWStringPSMDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "PSM based Simplified Chinese Charset Detector", NS_ZHCN_PSMDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "zhcn_parallel_state_machine", nsZHCNPSMDetectorConstructor,
|
||||
nsZHCNPSMDetectorRegistrationProc, NULL},
|
||||
{ "PSM based Simplified Chinese String Charset Detector", NS_ZHCN_STRING_PSMDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "zhcn_parallel_state_machine", nsZHCNStringPSMDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "PSM based Chinese Charset Detector", NS_ZH_PSMDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "zh_parallel_state_machine", nsZHPSMDetectorConstructor,
|
||||
nsZHPSMDetectorRegistrationProc, NULL},
|
||||
{ "PSM based Chinese String Charset Detector", NS_ZH_STRING_PSMDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "zh_parallel_state_machine", nsZHStringPSMDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "PSM based CJK Charset Detector", NS_CJK_PSMDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "cjk_parallel_state_machine", nsCJKPSMDetectorConstructor,
|
||||
nsCJKPSMDetectorRegistrationProc, NULL},
|
||||
{ "PSM based CJK String Charset Detector", NS_CJK_STRING_PSMDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "cjk_parallel_state_machine", nsCJKStringPSMDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "Probability based Russian Charset Detector", NS_RU_PROBDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ruprob", nsRUProbDetectorConstructor,
|
||||
nsRUProbDetectorRegistrationProc, NULL},
|
||||
{ "Probability based Ukrainian Charset Detector", NS_UK_PROBDETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "ukprob", nsUKProbDetectorConstructor,
|
||||
nsUKProbDetectorRegistrationProc, NULL},
|
||||
{ "Probability based Russian String Charset Detector", NS_RU_STRING_PROBDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ruprob", nsRUStringProbDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "Probability based Ukrainian String Charset Detector", NS_UK_STRING_PROBDETECTOR_CID,
|
||||
NS_STRCDETECTOR_CONTRACTID_BASE "ukprob", nsUKStringProbDetectorConstructor,
|
||||
NULL, NULL}
|
||||
#ifdef INCLUDE_DBGDETECTOR
|
||||
,
|
||||
{ "Debuging Detector 1st block", NS_1STBLKDBG_DETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "1stblkdbg", ns1stBlkDbgDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "Debuging Detector 2nd block", NS_2NDBLKDBG_DETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "2ndblkdbg", ns2ndBlkDbgDetectorConstructor,
|
||||
NULL, NULL},
|
||||
{ "Debuging Detector Last block", NS_LASTBLKDBG_DETECTOR_CID,
|
||||
NS_CHARSET_DETECTOR_CONTRACTID_BASE "lastblkdbg", nsLastBlkDbgDetectorConstructor,
|
||||
NULL, NULL}
|
||||
#endif /* INCLUDE_DBGDETECTOR */
|
||||
};
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
NS_IMPL_NSGETMODULE("nsCharDetModule", components)
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsCharDetModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
|
||||
{
|
||||
if (!okToUnload) {
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
}
|
||||
*okToUnload = (g_InstanceCount == 0) && (g_LockCount == 0);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
static nsCharDetModule *gModule = NULL;
|
||||
|
||||
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
|
||||
nsIFile* location,
|
||||
nsIModule** return_cobj)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
NS_ASSERTION(return_cobj, "Null argument");
|
||||
NS_ASSERTION(gModule == NULL, "nsCharDetModule: Module already created.");
|
||||
|
||||
// Create an initialize the layout module instance
|
||||
nsCharDetModule *m = new nsCharDetModule();
|
||||
if (!m) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
// Increase refcnt and store away nsIModule interface to m in return_cobj
|
||||
rv = m->QueryInterface(NS_GET_IID(nsIModule), (void**)return_cobj);
|
||||
if (NS_FAILED(rv)) {
|
||||
delete m;
|
||||
m = nsnull;
|
||||
}
|
||||
gModule = m; // WARNING: Weak Reference
|
||||
return rv;
|
||||
}
|
||||
|
|
|
@ -22,85 +22,19 @@
|
|||
*/
|
||||
#include "nscore.h"
|
||||
#include "nsCyrillicProb.h"
|
||||
#include "nsCyrillicClass.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "nsCyrillicDetector.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsISupports.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsIStringCharsetDetector.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
|
||||
#include "nsCharDetDll.h"
|
||||
//---- for XPCOM
|
||||
#include "nsIFactory.h"
|
||||
#include "nsISupports.h"
|
||||
#include "nsCharDetDll.h"
|
||||
#include "pratom.h"
|
||||
// temp fix for XPCOM should be remove after alechf fix the xpcom one
|
||||
#define MY_NS_IMPL_QUERY_INTERFACE(_class,_classiiddef,_interface) \
|
||||
NS_IMETHODIMP _class::QueryInterface(REFNSIID aIID, void** aInstancePtr) \
|
||||
{ \
|
||||
if (NULL == aInstancePtr) { \
|
||||
return NS_ERROR_NULL_POINTER; \
|
||||
} \
|
||||
\
|
||||
*aInstancePtr = NULL; \
|
||||
\
|
||||
static NS_DEFINE_IID(kClassIID, _classiiddef); \
|
||||
if (aIID.Equals(kClassIID)) { \
|
||||
*aInstancePtr = (void*) ((_interface*)this); \
|
||||
NS_ADDREF_THIS(); \
|
||||
return NS_OK; \
|
||||
} \
|
||||
if (aIID.Equals(NS_GET_IID(nsISupports))) { \
|
||||
*aInstancePtr = (void*) ((nsISupports*)this); \
|
||||
NS_ADDREF_THIS(); \
|
||||
return NS_OK; \
|
||||
} \
|
||||
return NS_NOINTERFACE; \
|
||||
}
|
||||
#define MY_NS_IMPL_ISUPPORTS(_class,_classiiddef,_interface) \
|
||||
NS_IMPL_ADDREF(_class) \
|
||||
NS_IMPL_RELEASE(_class) \
|
||||
MY_NS_IMPL_QUERY_INTERFACE(_class,_classiiddef,_interface)
|
||||
#include "nsCyrillicDetector.h"
|
||||
|
||||
NS_DEFINE_CID(kRUProbDetectorCID, NS_RU_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kUKProbDetectorCID, NS_UK_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kRUStringProbDetectorCID, NS_RU_STRING_PROBDETECTOR_CID);
|
||||
NS_DEFINE_CID(kUKStringProbDetectorCID, NS_UK_STRING_PROBDETECTOR_CID);
|
||||
//----------------------------------------------------------------------
|
||||
// Interface nsISupports [implementation]
|
||||
NS_IMPL_ISUPPORTS1(nsCyrXPCOMDetector, nsICharsetDetector);
|
||||
NS_IMPL_ISUPPORTS1(nsCyrXPCOMStringDetector, nsIStringCharsetDetector);
|
||||
|
||||
#define NUM_CYR_CHARSET 5
|
||||
|
||||
class nsCyrillicDetector {
|
||||
public:
|
||||
nsCyrillicDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
const char **aCharsets)
|
||||
{
|
||||
mItems = aItems;
|
||||
mCyrillicClass = aCyrillicClass;
|
||||
mCharsets = aCharsets;
|
||||
for(PRUint8 i=0;i<mItems;i++)
|
||||
mProb[i] = mLastCls[i] =0;
|
||||
mDone = PR_FALSE;
|
||||
};
|
||||
virtual ~nsCyrillicDetector() {};
|
||||
virtual void HandleData(const char* aBuf, PRUint32 aLen);
|
||||
virtual void DataEnd();
|
||||
protected:
|
||||
virtual void Report(const char* aCharset) = 0;
|
||||
|
||||
private:
|
||||
PRUint8 mItems;
|
||||
PRUint8 ** mCyrillicClass;
|
||||
const char** mCharsets;
|
||||
PRUint32 mProb[NUM_CYR_CHARSET];
|
||||
PRUint8 mLastCls[NUM_CYR_CHARSET];
|
||||
PRBool mDone;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
void nsCyrillicDetector::HandleData(const char* aBuf, PRUint32 aLen)
|
||||
{
|
||||
PRUint8 cls;
|
||||
|
@ -110,7 +44,7 @@ void nsCyrillicDetector::HandleData(const char* aBuf, PRUint32 aLen)
|
|||
return;
|
||||
for(i=0, b=aBuf;i<aLen;i++,b++)
|
||||
{
|
||||
for(PRUint8 j=0;j<mItems;j++)
|
||||
for(PRUintn j=0;j<mItems;j++)
|
||||
{
|
||||
if( 0x80 & *b)
|
||||
cls = mCyrillicClass[j][(*b) & 0x7F];
|
||||
|
@ -124,6 +58,7 @@ void nsCyrillicDetector::HandleData(const char* aBuf, PRUint32 aLen)
|
|||
// We now only based on the first block we receive
|
||||
DataEnd();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
#define THRESHOLD_RATIO 1.5f
|
||||
void nsCyrillicDetector::DataEnd()
|
||||
|
@ -151,25 +86,7 @@ void nsCyrillicDetector::DataEnd()
|
|||
this->Report(mCharsets[maxIdx]);
|
||||
mDone = PR_TRUE;
|
||||
}
|
||||
//=====================================================================
|
||||
class nsCyrXPCOMDetector :
|
||||
public nsCyrillicDetector,
|
||||
public nsICharsetDetector
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsCyrXPCOMDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
const char **aCharsets);
|
||||
virtual ~nsCyrXPCOMDetector();
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool *oDontFeedMe);
|
||||
NS_IMETHOD Done();
|
||||
protected:
|
||||
virtual void Report(const char* aCharset);
|
||||
private:
|
||||
nsICharsetDetectionObserver* mObserver;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
nsCyrXPCOMDetector:: nsCyrXPCOMDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
|
@ -180,14 +97,13 @@ nsCyrXPCOMDetector:: nsCyrXPCOMDetector(PRUint8 aItems,
|
|||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
mObserver = nsnull;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
nsCyrXPCOMDetector::~nsCyrXPCOMDetector()
|
||||
{
|
||||
NS_IF_RELEASE(mObserver);
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
//---------------------------------------------------------------------
|
||||
MY_NS_IMPL_ISUPPORTS(nsCyrXPCOMDetector, NS_GET_IID(nsICharsetDetector), nsICharsetDetector)
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsCyrXPCOMDetector::Init(
|
||||
nsICharsetDetectionObserver* aObserver)
|
||||
|
@ -196,10 +112,10 @@ NS_IMETHODIMP nsCyrXPCOMDetector::Init(
|
|||
if(nsnull == aObserver)
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
|
||||
NS_IF_ADDREF(aObserver);
|
||||
mObserver = aObserver;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
NS_IMETHODIMP nsCyrXPCOMDetector::DoIt(
|
||||
const char* aBuf, PRUint32 aLen, PRBool* oDontFeedMe)
|
||||
|
@ -213,6 +129,7 @@ NS_IMETHODIMP nsCyrXPCOMDetector::DoIt(
|
|||
*oDontFeedMe = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
NS_IMETHODIMP nsCyrXPCOMDetector::Done()
|
||||
{
|
||||
|
@ -220,6 +137,7 @@ NS_IMETHODIMP nsCyrXPCOMDetector::Done()
|
|||
this->DataEnd();
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
void nsCyrXPCOMDetector::Report(const char* aCharset)
|
||||
{
|
||||
|
@ -227,26 +145,6 @@ void nsCyrXPCOMDetector::Report(const char* aCharset)
|
|||
mObserver->Notify(aCharset, eBestAnswer);
|
||||
}
|
||||
|
||||
|
||||
//=====================================================================
|
||||
class nsCyrXPCOMStringDetector :
|
||||
public nsCyrillicDetector,
|
||||
public nsIStringCharsetDetector
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsCyrXPCOMStringDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
const char **aCharsets);
|
||||
virtual ~nsCyrXPCOMStringDetector();
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset, nsDetectionConfident &oConf);
|
||||
protected:
|
||||
virtual void Report(const char* aCharset);
|
||||
private:
|
||||
nsICharsetDetectionObserver* mObserver;
|
||||
const char* mResult;
|
||||
};
|
||||
//---------------------------------------------------------------------
|
||||
nsCyrXPCOMStringDetector:: nsCyrXPCOMStringDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
|
@ -256,19 +154,19 @@ nsCyrXPCOMStringDetector:: nsCyrXPCOMStringDetector(PRUint8 aItems,
|
|||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
nsCyrXPCOMStringDetector::~nsCyrXPCOMStringDetector()
|
||||
{
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
//---------------------------------------------------------------------
|
||||
MY_NS_IMPL_ISUPPORTS(nsCyrXPCOMStringDetector,
|
||||
NS_GET_IID(nsIStringCharsetDetector), nsIStringCharsetDetector)
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
void nsCyrXPCOMStringDetector::Report(const char *aCharset)
|
||||
{
|
||||
mResult = aCharset;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsCyrXPCOMStringDetector::DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset, nsDetectionConfident &oConf)
|
||||
|
@ -280,107 +178,5 @@ NS_IMETHODIMP nsCyrXPCOMStringDetector::DoIt(const char* aBuf, PRUint32 aLen,
|
|||
oConf = eBestAnswer;
|
||||
return NS_OK;
|
||||
}
|
||||
//=====================================================================
|
||||
static PRUint8 *gCyrillicCls[5] =
|
||||
{
|
||||
CP1251Map,
|
||||
KOI8Map,
|
||||
ISO88595Map,
|
||||
MacCyrillicMap,
|
||||
IBM866Map
|
||||
};
|
||||
static const char* gRussian[5] = {
|
||||
"windows-1251",
|
||||
"KOI8-R",
|
||||
"ISO-8859-5",
|
||||
"x-mac-cyrillic",
|
||||
"IBM866"
|
||||
};
|
||||
static const char* gUkrainian[5] = {
|
||||
"windows-1251",
|
||||
"KOI8-U",
|
||||
"ISO-8859-5",
|
||||
"x-mac-ukrainian",
|
||||
"IBM866"
|
||||
};
|
||||
//=====================================================================
|
||||
|
||||
class MyFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
MyFactory(const nsCID& aCID) {
|
||||
NS_INIT_REFCNT();
|
||||
mCID = aCID;
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~MyFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
private:
|
||||
nsCID mCID;
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
NS_IMPL_ISUPPORTS1( MyFactory , nsIFactory)
|
||||
|
||||
NS_IMETHODIMP MyFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsCyrXPCOMDetector *inst1 = nsnull;
|
||||
nsCyrXPCOMStringDetector *inst2 = nsnull;
|
||||
|
||||
if (mCID.Equals(kRUProbDetectorCID)) {
|
||||
inst1 = new nsCyrXPCOMDetector(5, gCyrillicCls, gRussian);
|
||||
} else if (mCID.Equals(kRUStringProbDetectorCID)) {
|
||||
inst2 = new nsCyrXPCOMStringDetector(5, gCyrillicCls, gRussian);
|
||||
} else if (mCID.Equals(kUKProbDetectorCID)) {
|
||||
inst1 = new nsCyrXPCOMDetector(5, gCyrillicCls, gUkrainian);
|
||||
} else if (mCID.Equals(kUKStringProbDetectorCID)) {
|
||||
inst2 = new nsCyrXPCOMStringDetector(5, gCyrillicCls, gUkrainian);
|
||||
}
|
||||
if((NULL == inst1) && (NULL == inst2)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res = NS_OK;
|
||||
if(inst1)
|
||||
res =inst1->QueryInterface(aIID, aResult);
|
||||
else
|
||||
res =inst2->QueryInterface(aIID, aResult);
|
||||
if(NS_FAILED(res)) {
|
||||
if(inst1)
|
||||
delete inst1;
|
||||
if(inst2)
|
||||
delete inst2;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP MyFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
nsIFactory* NEW_PROBDETECTOR_FACTORY(const nsCID& aClass) {
|
||||
return new MyFactory(aClass);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -22,8 +22,8 @@
|
|||
#ifndef nsCyrillicDetector_h__
|
||||
#define nsCyrillicDetector_h__
|
||||
|
||||
#include "nsCyrillicClass.h"
|
||||
|
||||
#include "nsIFactory.h"
|
||||
|
||||
|
||||
|
||||
|
@ -40,11 +40,131 @@
|
|||
#define NS_RU_STRING_PROBDETECTOR_CID \
|
||||
{ 0x2002f783, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
|
||||
|
||||
|
||||
// {2002F784-3960-11d3-B3C3-00805F8A6670}
|
||||
#define NS_UK_STRING_PROBDETECTOR_CID \
|
||||
{ 0x2002f784, 0x3960, 0x11d3, { 0xb3, 0xc3, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
|
||||
|
||||
nsIFactory* NEW_PROBDETECTOR_FACTORY(const nsCID& aClass);
|
||||
static PRUint8 *gCyrillicCls[5] =
|
||||
{
|
||||
CP1251Map,
|
||||
KOI8Map,
|
||||
ISO88595Map,
|
||||
MacCyrillicMap,
|
||||
IBM866Map
|
||||
};
|
||||
|
||||
static const char * gRussian[5] = {
|
||||
"windows-1251",
|
||||
"KOI8-R",
|
||||
"ISO-8859-5",
|
||||
"x-mac-cyrillic",
|
||||
"IBM866"
|
||||
};
|
||||
|
||||
static const char * gUkrainian[5] = {
|
||||
"windows-1251",
|
||||
"KOI8-U",
|
||||
"ISO-8859-5",
|
||||
"x-mac-ukrainian",
|
||||
"IBM866"
|
||||
};
|
||||
|
||||
#define NUM_CYR_CHARSET 5
|
||||
|
||||
class nsCyrillicDetector
|
||||
{
|
||||
public:
|
||||
nsCyrillicDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
const char **aCharsets) {
|
||||
mItems = aItems;
|
||||
mCyrillicClass = aCyrillicClass;
|
||||
mCharsets = aCharsets;
|
||||
for(PRUintn i=0;i<mItems;i++)
|
||||
mProb[i] = mLastCls[i] =0;
|
||||
mDone = PR_FALSE;
|
||||
};
|
||||
virtual ~nsCyrillicDetector() {};
|
||||
virtual void HandleData(const char* aBuf, PRUint32 aLen);
|
||||
virtual void DataEnd();
|
||||
protected:
|
||||
virtual void Report(const char* aCharset) = 0;
|
||||
|
||||
private:
|
||||
PRUint8 mItems;
|
||||
PRUint8 ** mCyrillicClass;
|
||||
const char** mCharsets;
|
||||
PRUint32 mProb[NUM_CYR_CHARSET];
|
||||
PRUint8 mLastCls[NUM_CYR_CHARSET];
|
||||
PRBool mDone;
|
||||
};
|
||||
|
||||
class nsCyrXPCOMDetector :
|
||||
public nsCyrillicDetector,
|
||||
public nsICharsetDetector
|
||||
{
|
||||
public:
|
||||
// nsISupports interface
|
||||
NS_DECL_ISUPPORTS
|
||||
nsCyrXPCOMDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
const char **aCharsets);
|
||||
virtual ~nsCyrXPCOMDetector();
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool *oDontFeedMe);
|
||||
NS_IMETHOD Done();
|
||||
protected:
|
||||
virtual void Report(const char* aCharset);
|
||||
private:
|
||||
nsCOMPtr<nsICharsetDetectionObserver> mObserver;
|
||||
};
|
||||
|
||||
class nsCyrXPCOMStringDetector :
|
||||
public nsCyrillicDetector,
|
||||
public nsIStringCharsetDetector
|
||||
{
|
||||
public:
|
||||
// nsISupports interface
|
||||
NS_DECL_ISUPPORTS
|
||||
nsCyrXPCOMStringDetector(PRUint8 aItems,
|
||||
PRUint8 ** aCyrillicClass,
|
||||
const char **aCharsets);
|
||||
virtual ~nsCyrXPCOMStringDetector();
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset, nsDetectionConfident &oConf);
|
||||
protected:
|
||||
virtual void Report(const char* aCharset);
|
||||
private:
|
||||
nsCOMPtr<nsICharsetDetectionObserver> mObserver;
|
||||
const char* mResult;
|
||||
};
|
||||
|
||||
class nsRUProbDetector : public nsCyrXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsRUProbDetector()
|
||||
: nsCyrXPCOMDetector(5, gCyrillicCls, gRussian) {};
|
||||
};
|
||||
|
||||
class nsRUStringProbDetector : public nsCyrXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsRUStringProbDetector()
|
||||
: nsCyrXPCOMStringDetector(5, gCyrillicCls, gRussian) {};
|
||||
};
|
||||
|
||||
class nsUKProbDetector : public nsCyrXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsUKProbDetector()
|
||||
: nsCyrXPCOMDetector(5, gCyrillicCls, gUkrainian) {};
|
||||
};
|
||||
|
||||
class nsUKStringProbDetector : public nsCyrXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsUKStringProbDetector()
|
||||
: nsCyrXPCOMStringDetector(5, gCyrillicCls, gUkrainian) {};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -22,8 +22,6 @@
|
|||
*/
|
||||
|
||||
#include "nsDebugDetector.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
#include "pratom.h"
|
||||
#include "nsCharDetDll.h"
|
||||
|
||||
|
@ -31,38 +29,6 @@
|
|||
#define REPORT_CHARSET "ISO-8859-7"
|
||||
#define REPORT_CONFIDENT eSureAnswer
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// bad coding patern, do not copy, just for this hacky debug code
|
||||
typedef enum {
|
||||
k1stBlk,
|
||||
k2ndBlk,
|
||||
klastBlk
|
||||
} nsDebugDetectorSel;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
class nsDebugDetector : public nsICharsetDetector {
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsDebugDetector(nsDebugDetectorSel aSel);
|
||||
virtual ~nsDebugDetector();
|
||||
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
|
||||
NS_IMETHOD DoIt(const char* aBytesArray, PRUint32 aLen, PRBool* oDontFeedMe);
|
||||
|
||||
NS_IMETHOD Done();
|
||||
protected:
|
||||
|
||||
virtual void Report();
|
||||
|
||||
private:
|
||||
PRInt32 mBlks;
|
||||
nsDebugDetectorSel mSel;
|
||||
nsICharsetDetectionObserver* mObserver;
|
||||
PRBool mStop;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
nsDebugDetector::nsDebugDetector( nsDebugDetectorSel aSel)
|
||||
{
|
||||
|
@ -76,7 +42,6 @@ nsDebugDetector::nsDebugDetector( nsDebugDetectorSel aSel)
|
|||
//--------------------------------------------------------------------
|
||||
nsDebugDetector::~nsDebugDetector()
|
||||
{
|
||||
NS_IF_RELEASE(mObserver);
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
//--------------------------------------------------------------------
|
||||
|
@ -86,7 +51,6 @@ NS_IMETHODIMP nsDebugDetector::Init(nsICharsetDetectionObserver* aObserver)
|
|||
if(nsnull == aObserver)
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
|
||||
NS_IF_ADDREF(aObserver);
|
||||
mObserver = aObserver;
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -131,74 +95,3 @@ void nsDebugDetector::Report()
|
|||
|
||||
NS_IMPL_ISUPPORTS1(nsDebugDetector, nsICharsetDetector)
|
||||
|
||||
//--------------------------------------------------------------
|
||||
class nsDebugDetectorFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsDebugDetectorFactory(nsDebugDetectorSel aSel) {
|
||||
NS_INIT_REFCNT();
|
||||
mSel = aSel;
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~nsDebugDetectorFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
private:
|
||||
// bad coding patern, do not copy, just for this hacky debug code
|
||||
nsDebugDetectorSel mSel;
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
NS_IMPL_ISUPPORTS1( nsDebugDetectorFactory , nsIFactory);
|
||||
|
||||
NS_IMETHODIMP nsDebugDetectorFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsDebugDetector *inst = new nsDebugDetector(mSel);
|
||||
|
||||
|
||||
if(NULL == inst) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res =inst->QueryInterface(aIID, aResult);
|
||||
if(NS_FAILED(res)) {
|
||||
delete inst;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsDebugDetectorFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsIFactory* NEW_1STBLKDBG_DETECTOR_FACTORY() {
|
||||
// bad coding patern, do not copy, just for this hacky debug code
|
||||
return new nsDebugDetectorFactory(k1stBlk);
|
||||
}
|
||||
nsIFactory* NEW_2NDBLKDBG_DETECTOR_FACTORY() {
|
||||
// bad coding patern, do not copy, just for this hacky debug code
|
||||
return new nsDebugDetectorFactory(k2ndBlk);
|
||||
}
|
||||
nsIFactory* NEW_LASTBLKDBG_DETECTOR_FACTORY() {
|
||||
// bad coding patern, do not copy, just for this hacky debug code
|
||||
return new nsDebugDetectorFactory(klastBlk);
|
||||
}
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#define nsDebugDetector_h__
|
||||
|
||||
#include "nsIFactory.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
|
||||
|
||||
// {12BB8F18-2389-11d3-B3BF-00805F8A6670}
|
||||
|
@ -39,8 +40,53 @@
|
|||
|
||||
|
||||
|
||||
nsIFactory* NEW_1STBLKDBG_DETECTOR_FACTORY();
|
||||
nsIFactory* NEW_2NDBLKDBG_DETECTOR_FACTORY();
|
||||
nsIFactory* NEW_LASTBLKDBG_DETECTOR_FACTORY();
|
||||
typedef enum {
|
||||
k1stBlk,
|
||||
k2ndBlk,
|
||||
klastBlk
|
||||
} nsDebugDetectorSel;
|
||||
|
||||
class nsDebugDetector : public nsICharsetDetector
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsDebugDetector(nsDebugDetectorSel aSel);
|
||||
virtual ~nsDebugDetector();
|
||||
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
|
||||
NS_IMETHOD DoIt(const char* aBytesArray, PRUint32 aLen, PRBool* oDontFeedMe);
|
||||
|
||||
NS_IMETHOD Done();
|
||||
|
||||
protected:
|
||||
|
||||
virtual void Report();
|
||||
|
||||
private:
|
||||
PRInt32 mBlks;
|
||||
nsDebugDetectorSel mSel;
|
||||
nsICharsetDetectionObserver* mObserver;
|
||||
PRBool mStop;
|
||||
};
|
||||
|
||||
class ns1stBlkDbgDetector : public nsDebugDetector
|
||||
{
|
||||
public:
|
||||
ns1stBlkDbgDetector () : nsDebugDetector(k1stBlk) {} ;
|
||||
};
|
||||
|
||||
class ns2ndBlkDbgDetector : public nsDebugDetector
|
||||
{
|
||||
public:
|
||||
ns2ndBlkDbgDetector () : nsDebugDetector(k2ndBlk) {} ;
|
||||
};
|
||||
|
||||
class nsLastBlkDbgDetector : public nsDebugDetector
|
||||
{
|
||||
public:
|
||||
nsLastBlkDbgDetector () : nsDebugDetector(klastBlk) {} ;
|
||||
};
|
||||
|
||||
#endif // nsDebugDetector_h__
|
||||
|
|
|
@ -24,69 +24,15 @@
|
|||
#define IMPL_NS_IPARSERFILTER
|
||||
|
||||
#include "nsString.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
#include "nsICharsetDetectionAdaptor.h"
|
||||
#include "nsDetectionAdaptor.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsIWebShellServices.h"
|
||||
#include "plstr.h"
|
||||
#include "pratom.h"
|
||||
#include "nsCharDetDll.h"
|
||||
#ifdef IMPL_NS_IPARSERFILTER
|
||||
#include "nsIParserFilter.h"
|
||||
static NS_DEFINE_IID(kIParserFilterIID, NS_IPARSERFILTER_IID);
|
||||
|
||||
#endif /* IMPL_NS_IPARSERFILTER */
|
||||
#include "nsIParser.h"
|
||||
#include "nsIDocument.h"
|
||||
#include "nsDetectionAdaptor.h"
|
||||
|
||||
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
||||
|
||||
class CToken;
|
||||
|
||||
//--------------------------------------------------------------
|
||||
class nsMyObserver : public nsICharsetDetectionObserver
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsMyObserver( void )
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(& g_InstanceCount);
|
||||
mWebShellSvc = nsnull;
|
||||
mNotifyByReload = PR_FALSE;
|
||||
mWeakRefDocument = nsnull;
|
||||
mWeakRefParser = nsnull;
|
||||
}
|
||||
virtual ~nsMyObserver( void )
|
||||
{
|
||||
NS_IF_RELEASE(mWebShellSvc);
|
||||
PR_AtomicDecrement(& g_InstanceCount);
|
||||
// do not release nor delete mWeakRefDocument
|
||||
// do not release nor delete mWeakRefParser
|
||||
}
|
||||
|
||||
|
||||
// Methods to support nsICharsetDetectionAdaptor
|
||||
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc,
|
||||
nsIDocument* aDocument,
|
||||
nsIParser* aParser,
|
||||
const PRUnichar* aCharset,
|
||||
const char* aCommand);
|
||||
|
||||
// Methods to support nsICharsetDetectionObserver
|
||||
NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf);
|
||||
void SetNotifyByReload(PRBool aByReload) { mNotifyByReload = aByReload; };
|
||||
private:
|
||||
nsIWebShellServices* mWebShellSvc;
|
||||
PRBool mNotifyByReload;
|
||||
nsIDocument* mWeakRefDocument;
|
||||
nsIParser* mWeakRefParser;
|
||||
nsAutoString mCharset;
|
||||
nsCAutoString mCommand;
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsMyObserver::Notify(
|
||||
const char* aCharset, nsDetectionConfident aConf)
|
||||
|
@ -130,7 +76,6 @@ NS_IMETHODIMP nsMyObserver::Init( nsIWebShellServices* aWebShellSvc,
|
|||
}
|
||||
if(nsnull != aWebShellSvc)
|
||||
{
|
||||
NS_IF_ADDREF(aWebShellSvc);
|
||||
mWebShellSvc = aWebShellSvc;
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -138,43 +83,7 @@ NS_IMETHODIMP nsMyObserver::Init( nsIWebShellServices* aWebShellSvc,
|
|||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMPL_ISUPPORTS1 ( nsMyObserver ,nsICharsetDetectionObserver);
|
||||
//--------------------------------------------------------------
|
||||
|
||||
class nsDetectionAdaptor :
|
||||
#ifdef IMPL_NS_IPARSERFILTER
|
||||
public nsIParserFilter,
|
||||
#endif /* IMPL_NS_IPARSERFILTER */
|
||||
public nsICharsetDetectionAdaptor
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsDetectionAdaptor( void );
|
||||
virtual ~nsDetectionAdaptor( void );
|
||||
|
||||
// Methods to support nsICharsetDetectionAdaptor
|
||||
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector,
|
||||
nsIDocument* aDocument,
|
||||
nsIParser* aParser,
|
||||
const PRUnichar* aCharset,
|
||||
const char* aCommand=nsnull);
|
||||
|
||||
// Methode to suppor nsIParserFilter
|
||||
NS_IMETHOD RawBuffer(const char * buffer, PRUint32 * buffer_length) ;
|
||||
NS_IMETHOD Finish();
|
||||
|
||||
// really don't care the following two, only because they are defined
|
||||
// in nsIParserFilter.h
|
||||
NS_IMETHOD WillAddToken(CToken & token) { return NS_OK; };
|
||||
NS_IMETHOD ProcessTokens( void ) {return NS_OK;};
|
||||
|
||||
private:
|
||||
nsICharsetDetector* mDetector;
|
||||
PRBool mDontFeedToDetector;
|
||||
nsMyObserver* mObserver;
|
||||
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
nsDetectionAdaptor::nsDetectionAdaptor( void )
|
||||
{
|
||||
|
@ -187,14 +96,14 @@ nsDetectionAdaptor::nsDetectionAdaptor( void )
|
|||
//--------------------------------------------------------------
|
||||
nsDetectionAdaptor::~nsDetectionAdaptor()
|
||||
{
|
||||
NS_IF_RELEASE(mDetector);
|
||||
NS_IF_RELEASE(mObserver);
|
||||
PR_AtomicDecrement(& g_InstanceCount);
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMPL_ADDREF ( nsDetectionAdaptor );
|
||||
NS_IMPL_RELEASE ( nsDetectionAdaptor );
|
||||
//----------------------------------------------------------------------
|
||||
// here: can't use NS_IMPL_QUERYINTERFACE due to the #ifdef
|
||||
//----------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsDetectionAdaptor::QueryInterface(REFNSIID aIID, void**aInstancePtr)
|
||||
{
|
||||
|
||||
|
@ -232,36 +141,30 @@ NS_IMETHODIMP nsDetectionAdaptor::Init(
|
|||
{
|
||||
if((nsnull != aWebShellSvc) && (nsnull != aDetector) && (nsnull != aCharset))
|
||||
{
|
||||
nsICharsetDetectionObserver* aObserver = nsnull;
|
||||
nsresult rv = NS_OK;
|
||||
mObserver = new nsMyObserver(); // weak ref to it, release by charset detector
|
||||
if(nsnull == mObserver)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
NS_IF_ADDREF(mObserver);
|
||||
nsICharsetDetectionObserver* aObserver = nsnull;
|
||||
nsresult rv = NS_OK;
|
||||
mObserver = new nsMyObserver(); // weak ref to it, release by charset detector
|
||||
if(nsnull == mObserver)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
rv = mObserver->QueryInterface(NS_GET_IID(nsICharsetDetectionObserver),
|
||||
(void**) &aObserver);
|
||||
|
||||
rv = mObserver->QueryInterface(NS_GET_IID(nsICharsetDetectionObserver),
|
||||
(void**) &aObserver);
|
||||
|
||||
if(NS_SUCCEEDED(rv)) {
|
||||
rv = mObserver->Init(aWebShellSvc, aDocument,
|
||||
aParser, aCharset, aCommand);
|
||||
if(NS_SUCCEEDED(rv)) {
|
||||
rv = mObserver->Init(aWebShellSvc, aDocument,
|
||||
aParser, aCharset, aCommand);
|
||||
if(NS_SUCCEEDED(rv)) {
|
||||
rv = aDetector->Init(aObserver);
|
||||
}
|
||||
rv = aDetector->Init(aObserver);
|
||||
}
|
||||
|
||||
NS_IF_RELEASE(aObserver);
|
||||
if(NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
if(NS_FAILED(rv))
|
||||
return rv;
|
||||
mDetector = aDetector;
|
||||
|
||||
NS_IF_ADDREF(aDetector);
|
||||
mDetector = aDetector;
|
||||
|
||||
mDontFeedToDetector = PR_FALSE;
|
||||
return NS_OK;
|
||||
} else {
|
||||
NS_IF_RELEASE(mObserver);
|
||||
}
|
||||
mDontFeedToDetector = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
@ -286,67 +189,6 @@ NS_IMETHODIMP nsDetectionAdaptor::Finish()
|
|||
nsresult rv = NS_OK;
|
||||
rv = mDetector->Done();
|
||||
|
||||
NS_IF_RELEASE(mDetector); // need this line so no cycle reference
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
class nsDetectionAdaptorFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsDetectionAdaptorFactory() {
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~nsDetectionAdaptorFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
NS_IMPL_ISUPPORTS1( nsDetectionAdaptorFactory , nsIFactory);
|
||||
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsDetectionAdaptorFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsDetectionAdaptor *inst = new nsDetectionAdaptor();
|
||||
|
||||
|
||||
if(NULL == inst) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res =inst->QueryInterface(aIID, aResult);
|
||||
if(NS_FAILED(res)) {
|
||||
delete inst;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsDetectionAdaptorFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//==============================================================
|
||||
nsIFactory* NEW_DETECTION_ADAPTOR_FACTORY()
|
||||
{
|
||||
return new nsDetectionAdaptorFactory();
|
||||
}
|
||||
|
|
|
@ -22,9 +22,96 @@
|
|||
#ifndef nsDetectionAdaptor_h__
|
||||
#define nsDetectionAdaptor_h__
|
||||
|
||||
#include "nsIFactory.h"
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIDocument.h"
|
||||
#include "nsIWebShellServices.h"
|
||||
|
||||
#ifdef IMPL_NS_IPARSERFILTER
|
||||
#include "nsIParserFilter.h"
|
||||
static NS_DEFINE_IID(kIParserFilterIID, NS_IPARSERFILTER_IID);
|
||||
|
||||
#endif /* IMPL_NS_IPARSERFILTER */
|
||||
|
||||
class CToken;
|
||||
extern "C" PRInt32 g_InstanceCount;
|
||||
extern "C" PRInt32 g_LockCount;
|
||||
|
||||
//--------------------------------------------------------------
|
||||
class nsMyObserver : public nsICharsetDetectionObserver
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsMyObserver( void )
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(& g_InstanceCount);
|
||||
mWebShellSvc = nsnull;
|
||||
mNotifyByReload = PR_FALSE;
|
||||
mWeakRefDocument = nsnull;
|
||||
mWeakRefParser = nsnull;
|
||||
}
|
||||
virtual ~nsMyObserver( void )
|
||||
{
|
||||
PR_AtomicDecrement(& g_InstanceCount);
|
||||
// do not release nor delete mWeakRefDocument
|
||||
// do not release nor delete mWeakRefParser
|
||||
}
|
||||
|
||||
|
||||
nsIFactory* NEW_DETECTION_ADAPTOR_FACTORY();
|
||||
// Methods to support nsICharsetDetectionAdaptor
|
||||
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc,
|
||||
nsIDocument* aDocument,
|
||||
nsIParser* aParser,
|
||||
const PRUnichar* aCharset,
|
||||
const char* aCommand);
|
||||
|
||||
// Methods to support nsICharsetDetectionObserver
|
||||
NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf);
|
||||
void SetNotifyByReload(PRBool aByReload) { mNotifyByReload = aByReload; };
|
||||
private:
|
||||
nsCOMPtr<nsIWebShellServices> mWebShellSvc;
|
||||
PRBool mNotifyByReload;
|
||||
nsCOMPtr<nsIDocument> mWeakRefDocument;
|
||||
nsCOMPtr<nsIParser> mWeakRefParser;
|
||||
nsAutoString mCharset;
|
||||
nsCAutoString mCommand;
|
||||
};
|
||||
|
||||
class nsDetectionAdaptor :
|
||||
#ifdef IMPL_NS_IPARSERFILTER
|
||||
public nsIParserFilter,
|
||||
#endif /* IMPL_NS_IPARSERFILTER */
|
||||
public nsICharsetDetectionAdaptor
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsDetectionAdaptor( void );
|
||||
virtual ~nsDetectionAdaptor( void );
|
||||
|
||||
// Methods to support nsICharsetDetectionAdaptor
|
||||
NS_IMETHOD Init(nsIWebShellServices* aWebShellSvc, nsICharsetDetector *aDetector,
|
||||
nsIDocument* aDocument,
|
||||
nsIParser* aParser,
|
||||
const PRUnichar* aCharset,
|
||||
const char* aCommand=nsnull);
|
||||
|
||||
// Methode to suppor nsIParserFilter
|
||||
NS_IMETHOD RawBuffer(const char * buffer, PRUint32 * buffer_length) ;
|
||||
NS_IMETHOD Finish();
|
||||
|
||||
// really don't care the following two, only because they are defined
|
||||
// in nsIParserFilter.h
|
||||
NS_IMETHOD WillAddToken(CToken & token) { return NS_OK; };
|
||||
NS_IMETHOD ProcessTokens( void ) {return NS_OK;};
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsICharsetDetector> mDetector;
|
||||
PRBool mDontFeedToDetector;
|
||||
nsMyObserver* mObserver;
|
||||
};
|
||||
|
||||
#endif /* nsDetectionAdaptor_h__ */
|
||||
|
|
|
@ -31,48 +31,6 @@
|
|||
|
||||
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
||||
|
||||
class nsDocumentCharsetInfo : public nsIDocumentCharsetInfo
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
|
||||
nsDocumentCharsetInfo ();
|
||||
virtual ~nsDocumentCharsetInfo ();
|
||||
|
||||
NS_IMETHOD SetForcedCharset(nsIAtom * aCharset);
|
||||
NS_IMETHOD GetForcedCharset(nsIAtom ** aResult);
|
||||
|
||||
NS_IMETHOD SetForcedDetector(PRBool aForced);
|
||||
NS_IMETHOD GetForcedDetector(PRBool * aResult);
|
||||
|
||||
NS_IMETHOD SetParentCharset(nsIAtom * aCharset);
|
||||
NS_IMETHOD GetParentCharset(nsIAtom ** aResult);
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsIAtom> mForcedCharset;
|
||||
nsCOMPtr<nsIAtom> mParentCharset;
|
||||
};
|
||||
|
||||
class nsDocumentCharsetInfoFactory : public nsIFactory
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
|
||||
nsDocumentCharsetInfoFactory() {
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
virtual ~nsDocumentCharsetInfoFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
};
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDocumentCharsetInfo, nsIDocumentCharsetInfo);
|
||||
|
||||
nsDocumentCharsetInfo::nsDocumentCharsetInfo()
|
||||
|
@ -124,44 +82,4 @@ NS_IMETHODIMP nsDocumentCharsetInfo::GetParentCharset(nsIAtom ** aResult)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsDocumentCharsetInfoFactory, nsIFactory)
|
||||
|
||||
NS_IMETHODIMP nsDocumentCharsetInfoFactory::CreateInstance(
|
||||
nsISupports* aDelegate,
|
||||
const nsIID &aIID,
|
||||
void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsDocumentCharsetInfo * inst = new nsDocumentCharsetInfo();
|
||||
|
||||
|
||||
if(NULL == inst) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res =inst->QueryInterface(aIID, aResult);
|
||||
if(NS_FAILED(res)) {
|
||||
delete inst;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsDocumentCharsetInfoFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsIFactory * NEW_DOCUMENT_CHARSET_INFO_FACTORY()
|
||||
{
|
||||
return new nsDocumentCharsetInfoFactory();
|
||||
}
|
||||
|
|
|
@ -26,6 +26,18 @@
|
|||
#include "nsIFactory.h"
|
||||
#include "nsIDocumentCharsetInfo.h"
|
||||
|
||||
nsIFactory * NEW_DOCUMENT_CHARSET_INFO_FACTORY();
|
||||
class nsDocumentCharsetInfo : public nsIDocumentCharsetInfo
|
||||
{
|
||||
public:
|
||||
nsDocumentCharsetInfo ();
|
||||
virtual ~nsDocumentCharsetInfo ();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSIDOCUMENTCHARSETINFO
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsIAtom> mForcedCharset;
|
||||
nsCOMPtr<nsIAtom> mParentCharset;
|
||||
};
|
||||
|
||||
#endif // nsDocumentCharsetInfo_h__
|
||||
|
|
|
@ -37,74 +37,17 @@
|
|||
|
||||
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Class declaration for the class
|
||||
//
|
||||
//==========================================================================
|
||||
class nsMetaCharsetObserver: public nsIElementObserver,
|
||||
public nsIObserver,
|
||||
public nsObserverBase,
|
||||
public nsIMetaCharsetService,
|
||||
public nsSupportsWeakReference {
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
|
||||
nsMetaCharsetObserver();
|
||||
virtual ~nsMetaCharsetObserver();
|
||||
|
||||
/* methode for nsIElementObserver */
|
||||
/*
|
||||
* This method return the tag which the observer care about
|
||||
*/
|
||||
NS_IMETHOD_(const char*)GetTagNameAt(PRUint32 aTagIndex);
|
||||
|
||||
/*
|
||||
* Subject call observer when the parser hit the tag
|
||||
* @param aDocumentID- ID of the document
|
||||
* @param aTag- the tag
|
||||
* @param numOfAttributes - number of attributes
|
||||
* @param nameArray - array of name.
|
||||
* @param valueArray - array of value
|
||||
*/
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, eHTMLTags aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, const PRUnichar* aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
|
||||
NS_IMETHOD Notify(nsISupports* aDocumentID, const PRUnichar* aTag, const nsStringArray* keys, const nsStringArray* values);
|
||||
|
||||
/* methode for nsIObserver */
|
||||
NS_DECL_NSIOBSERVER
|
||||
|
||||
/* methode for nsIMetaCharsetService */
|
||||
NS_IMETHOD Start();
|
||||
NS_IMETHOD End();
|
||||
|
||||
private:
|
||||
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
|
||||
NS_IMETHOD Notify(nsISupports* aDocumentID, const nsStringArray* keys, const nsStringArray* values);
|
||||
|
||||
nsICharsetAlias *mAlias;
|
||||
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
nsMetaCharsetObserver::nsMetaCharsetObserver()
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
bMetaCharsetObserverStarted = PR_FALSE;
|
||||
PR_AtomicIncrement(& g_InstanceCount);
|
||||
nsresult res;
|
||||
mAlias = nsnull;
|
||||
NS_WITH_SERVICE(nsICharsetAlias, calias, kCharsetAliasCID, &res);
|
||||
if(NS_SUCCEEDED(res)) {
|
||||
mAlias = calias;
|
||||
NS_ADDREF(mAlias);
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
|
@ -112,20 +55,22 @@ nsMetaCharsetObserver::~nsMetaCharsetObserver()
|
|||
{
|
||||
// should we release mAlias
|
||||
PR_AtomicDecrement(& g_InstanceCount);
|
||||
NS_IF_RELEASE(mAlias);
|
||||
if (bMetaCharsetObserverStarted == PR_TRUE) {
|
||||
// call to end the ObserverService
|
||||
End();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
NS_IMPL_ADDREF ( nsMetaCharsetObserver );
|
||||
NS_IMPL_RELEASE ( nsMetaCharsetObserver );
|
||||
|
||||
NS_INTERFACE_MAP_BEGIN(nsMetaCharsetObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIElementObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIMetaCharsetService)
|
||||
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
||||
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIElementObserver)
|
||||
NS_INTERFACE_MAP_END
|
||||
// Use the new scheme
|
||||
NS_IMPL_QUERY_INTERFACE4(nsMetaCharsetObserver,
|
||||
nsIElementObserver,
|
||||
nsIObserver,
|
||||
nsIMetaCharsetService,
|
||||
nsISupportsWeakReference);
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
NS_IMETHODIMP_(const char*) nsMetaCharsetObserver::GetTagNameAt(PRUint32 aTagIndex)
|
||||
|
@ -320,81 +265,38 @@ NS_IMETHODIMP nsMetaCharsetObserver::Start()
|
|||
{
|
||||
nsresult res = NS_OK;
|
||||
|
||||
if (bMetaCharsetObserverStarted == PR_FALSE) {
|
||||
bMetaCharsetObserverStarted = PR_TRUE;
|
||||
|
||||
// get the observer service
|
||||
NS_WITH_SERVICE(nsIObserverService, anObserverService, NS_OBSERVERSERVICE_CONTRACTID, &res);
|
||||
if (NS_FAILED(res)) return res;
|
||||
|
||||
nsAutoString htmlTopic; htmlTopic.AssignWithConversion(kHTMLTextContentType);
|
||||
return anObserverService->AddObserver(this, htmlTopic.GetUnicode());
|
||||
nsAutoString htmlTopic; htmlTopic.AssignWithConversion(kHTMLTextContentType);
|
||||
|
||||
// add self to ObserverService
|
||||
res = anObserverService->AddObserver(this, htmlTopic.GetUnicode());
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsMetaCharsetObserver::End()
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
if (bMetaCharsetObserverStarted == PR_TRUE) {
|
||||
bMetaCharsetObserverStarted = PR_FALSE;
|
||||
|
||||
NS_WITH_SERVICE(nsIObserverService, anObserverService, NS_OBSERVERSERVICE_CONTRACTID, &res);
|
||||
if (NS_FAILED(res)) return res;
|
||||
|
||||
nsAutoString htmlTopic; htmlTopic.AssignWithConversion(kHTMLTextContentType);
|
||||
return anObserverService->RemoveObserver(this, htmlTopic.GetUnicode());
|
||||
res = anObserverService->RemoveObserver(this, htmlTopic.GetUnicode());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
//==========================================================================
|
||||
|
||||
class nsMetaCharsetObserverFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsMetaCharsetObserverFactory() {
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~nsMetaCharsetObserverFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
NS_IMPL_ISUPPORTS1( nsMetaCharsetObserverFactory , nsIFactory);
|
||||
|
||||
NS_IMETHODIMP nsMetaCharsetObserverFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsMetaCharsetObserver *inst = new nsMetaCharsetObserver();
|
||||
|
||||
|
||||
if(NULL == inst) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res =inst->QueryInterface(aIID, aResult);
|
||||
if(NS_FAILED(res)) {
|
||||
delete inst;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsMetaCharsetObserverFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//==============================================================
|
||||
nsIFactory* NEW_META_CHARSET_OBSERVER_FACTORY()
|
||||
{
|
||||
return new nsMetaCharsetObserverFactory();
|
||||
}
|
||||
|
|
|
@ -23,6 +23,66 @@
|
|||
#define nsMetaCharsetObserverFactory_h__
|
||||
|
||||
#include "nsIFactory.h"
|
||||
nsIFactory* NEW_META_CHARSET_OBSERVER_FACTORY();
|
||||
#include "nsIMetaCharsetService.h"
|
||||
#include "nsIElementObserver.h"
|
||||
#include "nsIObserver.h"
|
||||
#include "nsObserverBase.h"
|
||||
#include "nsWeakReference.h"
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Class declaration for the class
|
||||
//
|
||||
//==========================================================================
|
||||
class nsMetaCharsetObserver: public nsIElementObserver,
|
||||
public nsIObserver,
|
||||
public nsObserverBase,
|
||||
public nsIMetaCharsetService,
|
||||
public nsSupportsWeakReference {
|
||||
public:
|
||||
nsMetaCharsetObserver();
|
||||
virtual ~nsMetaCharsetObserver();
|
||||
|
||||
/* methode for nsIElementObserver */
|
||||
/*
|
||||
* This method return the tag which the observer care about
|
||||
*/
|
||||
NS_IMETHOD_(const char*)GetTagNameAt(PRUint32 aTagIndex);
|
||||
|
||||
/*
|
||||
* Subject call observer when the parser hit the tag
|
||||
* @param aDocumentID- ID of the document
|
||||
* @param aTag- the tag
|
||||
* @param numOfAttributes - number of attributes
|
||||
* @param nameArray - array of name.
|
||||
* @param valueArray - array of value
|
||||
*/
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, eHTMLTags aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, const PRUnichar* aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
|
||||
NS_IMETHOD Notify(nsISupports* aDocumentID, const PRUnichar* aTag, const nsStringArray* keys, const nsStringArray* values);
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
/* methode for nsIObserver */
|
||||
NS_DECL_NSIOBSERVER
|
||||
|
||||
/* methode for nsIMetaCharsetService */
|
||||
NS_IMETHOD Start();
|
||||
NS_IMETHOD End();
|
||||
|
||||
private:
|
||||
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
|
||||
NS_IMETHOD Notify(nsISupports* aDocumentID, const nsStringArray* keys, const nsStringArray* values);
|
||||
|
||||
nsCOMPtr<nsICharsetAlias> mAlias;
|
||||
|
||||
PRBool bMetaCharsetObserverStarted;
|
||||
};
|
||||
|
||||
#endif // nsMetaCharsetObserverFactory_h__
|
||||
|
|
|
@ -23,137 +23,15 @@
|
|||
|
||||
|
||||
#include <math.h>
|
||||
#include "nsVerifier.h"
|
||||
//---- for verifiers
|
||||
#include "nsSJISVerifier.h"
|
||||
#include "nsEUCJPVerifier.h"
|
||||
#include "nsCP1252Verifier.h"
|
||||
#include "nsUTF8Verifier.h"
|
||||
#include "nsISO2022JPVerifier.h"
|
||||
#include "nsISO2022KRVerifier.h"
|
||||
#include "nsISO2022CNVerifier.h"
|
||||
#include "nsHZVerifier.h"
|
||||
#include "nsUCS2BEVerifier.h"
|
||||
#include "nsUCS2LEVerifier.h"
|
||||
#include "nsBIG5Verifier.h"
|
||||
#include "nsGB2312Verifier.h"
|
||||
#include "nsEUCTWVerifier.h"
|
||||
#include "nsEUCKRVerifier.h"
|
||||
//---- end verifiers
|
||||
#include <stdio.h>
|
||||
//---- for XPCOM
|
||||
#include "nsIFactory.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsISupports.h"
|
||||
#include "nsCharDetDll.h"
|
||||
#include "pratom.h"
|
||||
// temp fix for XPCOM should be remove after alechf fix the xpcom one
|
||||
#define MY_NS_IMPL_QUERY_INTERFACE(_class,_classiiddef,_interface) \
|
||||
NS_IMETHODIMP _class::QueryInterface(REFNSIID aIID, void** aInstancePtr) \
|
||||
{ \
|
||||
if (NULL == aInstancePtr) { \
|
||||
return NS_ERROR_NULL_POINTER; \
|
||||
} \
|
||||
\
|
||||
*aInstancePtr = NULL; \
|
||||
\
|
||||
static NS_DEFINE_IID(kClassIID, _classiiddef); \
|
||||
if (aIID.Equals(kClassIID)) { \
|
||||
*aInstancePtr = (void*) ((_interface*)this); \
|
||||
NS_ADDREF_THIS(); \
|
||||
return NS_OK; \
|
||||
} \
|
||||
if (aIID.Equals(NS_GET_IID(nsISupports))) { \
|
||||
*aInstancePtr = (void*) ((nsISupports*)this); \
|
||||
NS_ADDREF_THIS(); \
|
||||
return NS_OK; \
|
||||
} \
|
||||
return NS_NOINTERFACE; \
|
||||
}
|
||||
#define MY_NS_IMPL_ISUPPORTS(_class,_classiiddef,_interface) \
|
||||
NS_IMPL_ADDREF(_class) \
|
||||
NS_IMPL_RELEASE(_class) \
|
||||
MY_NS_IMPL_QUERY_INTERFACE(_class,_classiiddef,_interface)
|
||||
//---- end XPCOM
|
||||
//---- for XPCOM based detector interfaces
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
#include "nsIStringCharsetDetector.h"
|
||||
#include "nsPSMDetectors.h"
|
||||
|
||||
NS_DEFINE_CID(kJAPSMDetectorCID, NS_JA_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kJAStringPSMDetectorCID, NS_JA_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kKOPSMDetectorCID, NS_KO_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kKOStringPSMDetectorCID, NS_KO_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kZHCNPSMDetectorCID, NS_ZHCN_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kZHCNStringPSMDetectorCID, NS_ZHCN_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kZHTWPSMDetectorCID, NS_ZHTW_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kZHTWStringPSMDetectorCID, NS_ZHTW_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kZHPSMDetectorCID, NS_ZH_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kZHStringPSMDetectorCID, NS_ZH_STRING_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kCJKPSMDetectorCID, NS_CJK_PSMDETECTOR_CID);
|
||||
NS_DEFINE_CID(kCJKStringPSMDetectorCID, NS_CJK_STRING_PSMDETECTOR_CID);
|
||||
//---- end XPCOM based detector interfaces
|
||||
|
||||
|
||||
#define DETECTOR_DEBUG
|
||||
typedef struct {
|
||||
float mFirstByteFreq[94];
|
||||
float mFirstByteStdDev;
|
||||
float mFirstByteMean;
|
||||
float mFirstByteWeight;
|
||||
float mSecoundByteFreq[94];
|
||||
float mSecoundByteStdDev;
|
||||
float mSecoundByteMean;
|
||||
float mSecoundByteWeight;
|
||||
} nsEUCStatistics;
|
||||
|
||||
static nsEUCStatistics gBig5Statistics =
|
||||
#include "Big5Statistics.h"
|
||||
// end of UECTWStatistics.h include
|
||||
|
||||
static nsEUCStatistics gEUCTWStatistics =
|
||||
#include "EUCTWStatistics.h"
|
||||
// end of UECTWStatistics.h include
|
||||
|
||||
static nsEUCStatistics gGB2312Statistics =
|
||||
#include "GB2312Statistics.h"
|
||||
// end of GB2312Statistics.h include
|
||||
|
||||
static nsEUCStatistics gEUCJPStatistics =
|
||||
#include "EUCJPStatistics.h"
|
||||
// end of EUCJPStatistics.h include
|
||||
|
||||
static nsEUCStatistics gEUCKRStatistics =
|
||||
#include "EUCKRStatistics.h"
|
||||
// end of EUCKRStatistics.h include
|
||||
|
||||
class nsEUCSampler {
|
||||
public:
|
||||
nsEUCSampler() {
|
||||
mTotal =0;
|
||||
mThreshold = 200;
|
||||
mState = 0;
|
||||
PRInt32 i;
|
||||
for(i=0;i<94;i++)
|
||||
mFirstByteCnt[i] = mSecondByteCnt[i]=0;
|
||||
}
|
||||
PRBool EnoughData() { return mTotal > mThreshold; }
|
||||
PRBool GetSomeData() { return mTotal > 1; }
|
||||
PRBool Sample(const char* aIn, PRUint32 aLen);
|
||||
void CalFreq();
|
||||
float GetScore(const float* aFirstByteFreq, float aFirstByteWeight,
|
||||
const float* aSecondByteFreq, float aSecondByteWeight);
|
||||
float GetScore(const float* array1, const float* array2);
|
||||
private:
|
||||
PRUint32 mTotal;
|
||||
PRUint32 mThreshold;
|
||||
PRInt8 mState;
|
||||
PRUint32 mFirstByteCnt[94];
|
||||
PRUint32 mSecondByteCnt[94];
|
||||
float mFirstByteFreq[94];
|
||||
float mSecondByteFreq[94];
|
||||
|
||||
};
|
||||
PRBool nsEUCSampler::Sample(const char* aIn, PRUint32 aLen)
|
||||
{
|
||||
if(mState == 1)
|
||||
|
@ -227,55 +105,10 @@ void nsEUCSampler::CalFreq()
|
|||
mSecondByteFreq[i] = (float)mSecondByteCnt[i] / (float)mTotal;
|
||||
}
|
||||
}
|
||||
/*
|
||||
In the current design, we know the following combination of verifiers
|
||||
are not good-
|
||||
|
||||
1. Two or more of the following verifier in one detector:
|
||||
nsEUCJPVerifer
|
||||
nsGB2312Verifier
|
||||
nsEUCKRVerifer
|
||||
nsEUCTWVerifier
|
||||
nsBIG5Verifer
|
||||
|
||||
*/
|
||||
|
||||
//==========================================================
|
||||
|
||||
#define MAX_VERIFIERS 16
|
||||
class nsPSMDetector {
|
||||
public :
|
||||
nsPSMDetector(PRUint8 aItems, nsVerifier** aVerifierSet, nsEUCStatistics** aStatisticsSet);
|
||||
virtual ~nsPSMDetector() {};
|
||||
|
||||
virtual PRBool HandleData(const char* aBuf, PRUint32 aLen);
|
||||
virtual void DataEnd();
|
||||
|
||||
protected:
|
||||
virtual void Report(const char* charset) = 0;
|
||||
|
||||
PRUint8 mItems;
|
||||
PRUint8 mClassItems;
|
||||
PRUint8 mState[MAX_VERIFIERS];
|
||||
PRUint8 mItemIdx[MAX_VERIFIERS];
|
||||
nsVerifier** mVerifier;
|
||||
nsEUCStatistics** mStatisticsData;
|
||||
PRBool mDone;
|
||||
|
||||
PRBool mRunSampler;
|
||||
PRBool mClassRunSampler;
|
||||
protected:
|
||||
void Reset();
|
||||
void Sample(const char* aBuf, PRUint32 aLen, PRBool aLastChance=PR_FALSE);
|
||||
private:
|
||||
#ifdef DETECTOR_DEBUG
|
||||
PRUint32 mDbgTest;
|
||||
PRUint32 mDbgLen;
|
||||
#endif
|
||||
nsEUCSampler mSampler;
|
||||
|
||||
};
|
||||
//----------------------------------------------------------
|
||||
NS_IMPL_ISUPPORTS1(nsXPCOMDetector, nsICharsetDetector);
|
||||
NS_IMPL_ISUPPORTS1(nsXPCOMStringDetector, nsIStringCharsetDetector);
|
||||
nsPSMDetector::nsPSMDetector(PRUint8 aItems, nsVerifier** aVerifierSet, nsEUCStatistics** aStatisticsSet)
|
||||
{
|
||||
mClassRunSampler = (nsnull != aStatisticsSet);
|
||||
|
@ -474,144 +307,6 @@ void nsPSMDetector::Sample(const char* aBuf, PRUint32 aLen, PRBool aLastChance)
|
|||
} // if (eucNum == nonUCS2Num)
|
||||
} // if(mRunSampler)
|
||||
}
|
||||
//==========================================================
|
||||
/*
|
||||
This class won't detect x-euc-tw for now. It can only
|
||||
tell a Big5 document is not x-euc-tw , but cannot tell
|
||||
a x-euc-tw docuement is not Big5 unless we hit characters
|
||||
defined in CNS 11643 plane 2.
|
||||
|
||||
May need improvement ....
|
||||
*/
|
||||
|
||||
#define ZHTW_DETECTOR_NUM_VERIFIERS 7
|
||||
static nsVerifier *gZhTwVerifierSet[ZHTW_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsBIG5Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsEUCTWVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
static nsEUCStatistics *gZhTwStatisticsSet[ZHTW_DETECTOR_NUM_VERIFIERS] = {
|
||||
nsnull,
|
||||
&gBig5Statistics,
|
||||
nsnull,
|
||||
&gEUCTWStatistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull
|
||||
};
|
||||
//==========================================================
|
||||
#define KO_DETECTOR_NUM_VERIFIERS 6
|
||||
static nsVerifier *gKoVerifierSet[KO_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsEUCKRVerifier,
|
||||
&nsISO2022KRVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
//==========================================================
|
||||
#define ZHCN_DETECTOR_NUM_VERIFIERS 7
|
||||
static nsVerifier *gZhCnVerifierSet[ZHCN_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsGB2312Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsHZVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
//==========================================================
|
||||
#define JA_DETECTOR_NUM_VERIFIERS 7
|
||||
static nsVerifier *gJaVerifierSet[JA_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsSJISVerifier,
|
||||
&nsEUCJPVerifier,
|
||||
&nsISO2022JPVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
//==========================================================
|
||||
#define ZH_DETECTOR_NUM_VERIFIERS 9
|
||||
static nsVerifier *gZhVerifierSet[ZH_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsGB2312Verifier,
|
||||
&nsBIG5Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsHZVerifier,
|
||||
&nsEUCTWVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
static nsEUCStatistics *gZhStatisticsSet[ZH_DETECTOR_NUM_VERIFIERS] = {
|
||||
nsnull,
|
||||
&gGB2312Statistics,
|
||||
&gBig5Statistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
&gEUCTWStatistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull
|
||||
};
|
||||
//==========================================================
|
||||
#define CJK_DETECTOR_NUM_VERIFIERS 14
|
||||
static nsVerifier *gCJKVerifierSet[CJK_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsSJISVerifier,
|
||||
&nsEUCJPVerifier,
|
||||
&nsISO2022JPVerifier,
|
||||
&nsEUCKRVerifier,
|
||||
&nsISO2022KRVerifier,
|
||||
&nsBIG5Verifier,
|
||||
&nsEUCTWVerifier,
|
||||
&nsGB2312Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsHZVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
static nsEUCStatistics *gCJKStatisticsSet[CJK_DETECTOR_NUM_VERIFIERS] = {
|
||||
nsnull,
|
||||
nsnull,
|
||||
&gEUCJPStatistics,
|
||||
nsnull,
|
||||
&gEUCKRStatistics,
|
||||
nsnull,
|
||||
&gBig5Statistics,
|
||||
&gEUCTWStatistics,
|
||||
&gGB2312Statistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull
|
||||
};
|
||||
//==========================================================
|
||||
class nsXPCOMDetector :
|
||||
private nsPSMDetector,
|
||||
public nsICharsetDetector // Implement the interface
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsXPCOMDetector(PRUint8 aItems, nsVerifier** aVer, nsEUCStatistics** aStatisticsSet);
|
||||
virtual ~nsXPCOMDetector();
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool* oDontFeedMe);
|
||||
NS_IMETHOD Done();
|
||||
|
||||
protected:
|
||||
virtual void Report(const char* charset);
|
||||
|
||||
private:
|
||||
nsICharsetDetectionObserver* mObserver;
|
||||
};
|
||||
//----------------------------------------------------------
|
||||
nsXPCOMDetector::nsXPCOMDetector(PRUint8 aItems, nsVerifier **aVer, nsEUCStatistics** aStatisticsSet)
|
||||
: nsPSMDetector( aItems, aVer, aStatisticsSet)
|
||||
|
@ -623,12 +318,9 @@ nsXPCOMDetector::nsXPCOMDetector(PRUint8 aItems, nsVerifier **aVer, nsEUCStatist
|
|||
//----------------------------------------------------------
|
||||
nsXPCOMDetector::~nsXPCOMDetector()
|
||||
{
|
||||
NS_IF_RELEASE(mObserver);
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
//----------------------------------------------------------
|
||||
MY_NS_IMPL_ISUPPORTS(nsXPCOMDetector,NS_GET_IID(nsICharsetDetector), nsICharsetDetector)
|
||||
//----------------------------------------------------------
|
||||
NS_IMETHODIMP nsXPCOMDetector::Init(
|
||||
nsICharsetDetectionObserver* aObserver)
|
||||
{
|
||||
|
@ -636,7 +328,6 @@ NS_IMETHODIMP nsXPCOMDetector::Init(
|
|||
if(nsnull == aObserver)
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
|
||||
NS_IF_ADDREF(aObserver);
|
||||
mObserver = aObserver;
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -665,23 +356,6 @@ void nsXPCOMDetector::Report(const char* charset)
|
|||
{
|
||||
mObserver->Notify(charset, eSureAnswer);
|
||||
}
|
||||
//==========================================================
|
||||
class nsXPCOMStringDetector :
|
||||
private nsPSMDetector,
|
||||
public nsIStringCharsetDetector // Implement the interface
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsXPCOMStringDetector(PRUint8 aItems, nsVerifier** aVer, nsEUCStatistics** aStatisticsSet);
|
||||
virtual ~nsXPCOMStringDetector();
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset,
|
||||
nsDetectionConfident &oConfident);
|
||||
protected:
|
||||
virtual void Report(const char* charset);
|
||||
private:
|
||||
const char* mResult;
|
||||
};
|
||||
//----------------------------------------------------------
|
||||
nsXPCOMStringDetector::nsXPCOMStringDetector(PRUint8 aItems, nsVerifier** aVer, nsEUCStatistics** aStatisticsSet)
|
||||
: nsPSMDetector( aItems, aVer, aStatisticsSet)
|
||||
|
@ -695,8 +369,6 @@ nsXPCOMStringDetector::~nsXPCOMStringDetector()
|
|||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
//----------------------------------------------------------
|
||||
MY_NS_IMPL_ISUPPORTS(nsXPCOMStringDetector,NS_GET_IID(nsIStringCharsetDetector), nsIStringCharsetDetector)
|
||||
//----------------------------------------------------------
|
||||
void nsXPCOMStringDetector::Report(const char* charset)
|
||||
{
|
||||
mResult = charset;
|
||||
|
@ -730,102 +402,4 @@ NS_IMETHODIMP nsXPCOMStringDetector::DoIt(const char* aBuf, PRUint32 aLen,
|
|||
this->Reset();
|
||||
return NS_OK;
|
||||
}
|
||||
//==========================================================
|
||||
|
||||
class nsXPCOMDetectorFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsXPCOMDetectorFactory(const nsCID& aCID) {
|
||||
NS_INIT_REFCNT();
|
||||
mCID = aCID;
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~nsXPCOMDetectorFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
private:
|
||||
nsCID mCID;
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
NS_IMPL_ISUPPORTS1( nsXPCOMDetectorFactory , nsIFactory);
|
||||
|
||||
NS_IMETHODIMP nsXPCOMDetectorFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsXPCOMDetector *inst1 = nsnull;
|
||||
nsXPCOMStringDetector *inst2 = nsnull;
|
||||
|
||||
if (mCID.Equals(kJAPSMDetectorCID)) {
|
||||
inst1 = new nsXPCOMDetector(JA_DETECTOR_NUM_VERIFIERS, gJaVerifierSet, nsnull);
|
||||
} else if (mCID.Equals(kKOPSMDetectorCID)) {
|
||||
inst1 = new nsXPCOMDetector(KO_DETECTOR_NUM_VERIFIERS, gKoVerifierSet, nsnull);
|
||||
} else if (mCID.Equals(kZHCNPSMDetectorCID)) {
|
||||
inst1 = new nsXPCOMDetector(ZHCN_DETECTOR_NUM_VERIFIERS, gZhCnVerifierSet, nsnull);
|
||||
} else if (mCID.Equals(kZHTWPSMDetectorCID)) {
|
||||
inst1 = new nsXPCOMDetector(ZHTW_DETECTOR_NUM_VERIFIERS, gZhTwVerifierSet, gZhTwStatisticsSet);
|
||||
} else if (mCID.Equals(kZHPSMDetectorCID)) {
|
||||
inst1 = new nsXPCOMDetector(ZH_DETECTOR_NUM_VERIFIERS, gZhVerifierSet, gZhStatisticsSet);
|
||||
} else if (mCID.Equals(kCJKPSMDetectorCID)) {
|
||||
inst1 = new nsXPCOMDetector(CJK_DETECTOR_NUM_VERIFIERS, gCJKVerifierSet, gCJKStatisticsSet);
|
||||
} else if (mCID.Equals(kJAStringPSMDetectorCID)) {
|
||||
inst2 = new nsXPCOMStringDetector(JA_DETECTOR_NUM_VERIFIERS - 3, gJaVerifierSet, nsnull);
|
||||
} else if (mCID.Equals(kKOStringPSMDetectorCID)) {
|
||||
inst2 = new nsXPCOMStringDetector(KO_DETECTOR_NUM_VERIFIERS - 3, gKoVerifierSet, nsnull);
|
||||
} else if (mCID.Equals(kZHCNStringPSMDetectorCID)) {
|
||||
inst2 = new nsXPCOMStringDetector(ZHCN_DETECTOR_NUM_VERIFIERS - 3, gZhCnVerifierSet, nsnull);
|
||||
} else if (mCID.Equals(kZHTWStringPSMDetectorCID)) {
|
||||
inst2 = new nsXPCOMStringDetector(ZHTW_DETECTOR_NUM_VERIFIERS - 3, gZhTwVerifierSet, gZhTwStatisticsSet);
|
||||
} else if (mCID.Equals(kZHStringPSMDetectorCID)) {
|
||||
inst2 = new nsXPCOMStringDetector(ZH_DETECTOR_NUM_VERIFIERS - 3, gZhVerifierSet, gZhStatisticsSet);
|
||||
} else if (mCID.Equals(kCJKStringPSMDetectorCID)) {
|
||||
inst2 = new nsXPCOMStringDetector(CJK_DETECTOR_NUM_VERIFIERS - 3, gCJKVerifierSet, gCJKStatisticsSet);
|
||||
}
|
||||
if((NULL == inst1) && (NULL == inst2)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res = NS_OK;
|
||||
|
||||
if(inst1)
|
||||
res =inst1->QueryInterface(aIID, aResult);
|
||||
else
|
||||
res =inst2->QueryInterface(aIID, aResult);
|
||||
|
||||
if(NS_FAILED(res)) {
|
||||
if(inst1)
|
||||
delete inst1;
|
||||
if(inst2)
|
||||
delete inst2;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsXPCOMDetectorFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
nsIFactory* NEW_PSMDETECTOR_FACTORY(const nsCID& aClass) {
|
||||
return new nsXPCOMDetectorFactory(aClass);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,27 @@
|
|||
#define nsPSMDetectors_h__
|
||||
|
||||
#include "nsIFactory.h"
|
||||
#include "nsVerifier.h"
|
||||
//---- for verifiers
|
||||
#include "nsSJISVerifier.h"
|
||||
#include "nsEUCJPVerifier.h"
|
||||
#include "nsCP1252Verifier.h"
|
||||
#include "nsUTF8Verifier.h"
|
||||
#include "nsISO2022JPVerifier.h"
|
||||
#include "nsISO2022KRVerifier.h"
|
||||
#include "nsISO2022CNVerifier.h"
|
||||
#include "nsHZVerifier.h"
|
||||
#include "nsUCS2BEVerifier.h"
|
||||
#include "nsUCS2LEVerifier.h"
|
||||
#include "nsBIG5Verifier.h"
|
||||
#include "nsGB2312Verifier.h"
|
||||
#include "nsEUCTWVerifier.h"
|
||||
#include "nsEUCKRVerifier.h"
|
||||
//---- end verifiers
|
||||
|
||||
#define DETECTOR_DEBUG
|
||||
|
||||
#define MAX_VERIFIERS 16
|
||||
|
||||
// {12BB8F1B-2389-11d3-B3BF-00805F8A6670}
|
||||
#define NS_JA_PSMDETECTOR_CID \
|
||||
|
@ -76,11 +96,363 @@
|
|||
#define NS_CJK_PSMDETECTOR_CID \
|
||||
{ 0xfcacef24, 0x2b40, 0x11d3, { 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70 } }
|
||||
|
||||
typedef struct {
|
||||
float mFirstByteFreq[94];
|
||||
float mFirstByteStdDev;
|
||||
float mFirstByteMean;
|
||||
float mFirstByteWeight;
|
||||
float mSecoundByteFreq[94];
|
||||
float mSecoundByteStdDev;
|
||||
float mSecoundByteMean;
|
||||
float mSecoundByteWeight;
|
||||
} nsEUCStatistics;
|
||||
|
||||
static nsEUCStatistics gBig5Statistics =
|
||||
#include "Big5Statistics.h"
|
||||
// end of UECTWStatistics.h include
|
||||
|
||||
static nsEUCStatistics gEUCTWStatistics =
|
||||
#include "EUCTWStatistics.h"
|
||||
// end of UECTWStatistics.h include
|
||||
|
||||
static nsEUCStatistics gGB2312Statistics =
|
||||
#include "GB2312Statistics.h"
|
||||
// end of GB2312Statistics.h include
|
||||
|
||||
static nsEUCStatistics gEUCJPStatistics =
|
||||
#include "EUCJPStatistics.h"
|
||||
// end of EUCJPStatistics.h include
|
||||
|
||||
nsIFactory* NEW_PSMDETECTOR_FACTORY(const nsCID& aClass);
|
||||
static nsEUCStatistics gEUCKRStatistics =
|
||||
#include "EUCKRStatistics.h"
|
||||
// end of EUCKRStatistics.h include
|
||||
|
||||
//==========================================================
|
||||
/*
|
||||
This class won't detect x-euc-tw for now. It can only
|
||||
tell a Big5 document is not x-euc-tw , but cannot tell
|
||||
a x-euc-tw docuement is not Big5 unless we hit characters
|
||||
defined in CNS 11643 plane 2.
|
||||
|
||||
May need improvement ....
|
||||
*/
|
||||
|
||||
#define ZHTW_DETECTOR_NUM_VERIFIERS 7
|
||||
|
||||
static nsVerifier *gZhTwVerifierSet[ZHTW_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsBIG5Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsEUCTWVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
|
||||
static nsEUCStatistics *gZhTwStatisticsSet[ZHTW_DETECTOR_NUM_VERIFIERS] = {
|
||||
nsnull,
|
||||
&gBig5Statistics,
|
||||
nsnull,
|
||||
&gEUCTWStatistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull
|
||||
};
|
||||
|
||||
//==========================================================
|
||||
#define KO_DETECTOR_NUM_VERIFIERS 6
|
||||
|
||||
static nsVerifier *gKoVerifierSet[KO_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsEUCKRVerifier,
|
||||
&nsISO2022KRVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
|
||||
//==========================================================
|
||||
#define ZHCN_DETECTOR_NUM_VERIFIERS 7
|
||||
|
||||
static nsVerifier *gZhCnVerifierSet[ZHCN_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsGB2312Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsHZVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
|
||||
//==========================================================
|
||||
#define JA_DETECTOR_NUM_VERIFIERS 7
|
||||
|
||||
static nsVerifier *gJaVerifierSet[JA_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsSJISVerifier,
|
||||
&nsEUCJPVerifier,
|
||||
&nsISO2022JPVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
|
||||
//==========================================================
|
||||
#define ZH_DETECTOR_NUM_VERIFIERS 9
|
||||
|
||||
static nsVerifier *gZhVerifierSet[ZH_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsGB2312Verifier,
|
||||
&nsBIG5Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsHZVerifier,
|
||||
&nsEUCTWVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
|
||||
static nsEUCStatistics *gZhStatisticsSet[ZH_DETECTOR_NUM_VERIFIERS] = {
|
||||
nsnull,
|
||||
&gGB2312Statistics,
|
||||
&gBig5Statistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
&gEUCTWStatistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull
|
||||
};
|
||||
|
||||
//==========================================================
|
||||
#define CJK_DETECTOR_NUM_VERIFIERS 14
|
||||
|
||||
static nsVerifier *gCJKVerifierSet[CJK_DETECTOR_NUM_VERIFIERS] = {
|
||||
&nsUTF8Verifier,
|
||||
&nsSJISVerifier,
|
||||
&nsEUCJPVerifier,
|
||||
&nsISO2022JPVerifier,
|
||||
&nsEUCKRVerifier,
|
||||
&nsISO2022KRVerifier,
|
||||
&nsBIG5Verifier,
|
||||
&nsEUCTWVerifier,
|
||||
&nsGB2312Verifier,
|
||||
&nsISO2022CNVerifier,
|
||||
&nsHZVerifier,
|
||||
&nsCP1252Verifier,
|
||||
&nsUCS2BEVerifier,
|
||||
&nsUCS2LEVerifier
|
||||
};
|
||||
|
||||
static nsEUCStatistics *gCJKStatisticsSet[CJK_DETECTOR_NUM_VERIFIERS] = {
|
||||
nsnull,
|
||||
nsnull,
|
||||
&gEUCJPStatistics,
|
||||
nsnull,
|
||||
&gEUCKRStatistics,
|
||||
nsnull,
|
||||
&gBig5Statistics,
|
||||
&gEUCTWStatistics,
|
||||
&gGB2312Statistics,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull,
|
||||
nsnull
|
||||
};
|
||||
|
||||
class nsEUCSampler {
|
||||
public:
|
||||
nsEUCSampler() {
|
||||
mTotal =0;
|
||||
mThreshold = 200;
|
||||
mState = 0;
|
||||
PRInt32 i;
|
||||
for(i=0;i<94;i++)
|
||||
mFirstByteCnt[i] = mSecondByteCnt[i]=0;
|
||||
}
|
||||
PRBool EnoughData() { return mTotal > mThreshold; }
|
||||
PRBool GetSomeData() { return mTotal > 1; }
|
||||
PRBool Sample(const char* aIn, PRUint32 aLen);
|
||||
void CalFreq();
|
||||
float GetScore(const float* aFirstByteFreq, float aFirstByteWeight,
|
||||
const float* aSecondByteFreq, float aSecondByteWeight);
|
||||
float GetScore(const float* array1, const float* array2);
|
||||
private:
|
||||
PRUint32 mTotal;
|
||||
PRUint32 mThreshold;
|
||||
PRInt8 mState;
|
||||
PRUint32 mFirstByteCnt[94];
|
||||
PRUint32 mSecondByteCnt[94];
|
||||
float mFirstByteFreq[94];
|
||||
float mSecondByteFreq[94];
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
In the current design, we know the following combination of verifiers
|
||||
are not good-
|
||||
|
||||
1. Two or more of the following verifier in one detector:
|
||||
nsEUCJPVerifer
|
||||
nsGB2312Verifier
|
||||
nsEUCKRVerifer
|
||||
nsEUCTWVerifier
|
||||
nsBIG5Verifer
|
||||
|
||||
*/
|
||||
//----------------------------------------------------------
|
||||
class nsPSMDetector {
|
||||
public :
|
||||
nsPSMDetector(PRUint8 aItems, nsVerifier** aVerifierSet, nsEUCStatistics** aStatisticsSet);
|
||||
virtual ~nsPSMDetector() {};
|
||||
|
||||
virtual PRBool HandleData(const char* aBuf, PRUint32 aLen);
|
||||
virtual void DataEnd();
|
||||
|
||||
protected:
|
||||
virtual void Report(const char* charset) = 0;
|
||||
|
||||
PRUint8 mItems;
|
||||
PRUint8 mClassItems;
|
||||
PRUint8 mState[MAX_VERIFIERS];
|
||||
PRUint8 mItemIdx[MAX_VERIFIERS];
|
||||
nsVerifier** mVerifier;
|
||||
nsEUCStatistics** mStatisticsData;
|
||||
PRBool mDone;
|
||||
|
||||
PRBool mRunSampler;
|
||||
PRBool mClassRunSampler;
|
||||
protected:
|
||||
void Reset();
|
||||
void Sample(const char* aBuf, PRUint32 aLen, PRBool aLastChance=PR_FALSE);
|
||||
private:
|
||||
#ifdef DETECTOR_DEBUG
|
||||
PRUint32 mDbgTest;
|
||||
PRUint32 mDbgLen;
|
||||
#endif
|
||||
nsEUCSampler mSampler;
|
||||
|
||||
};
|
||||
|
||||
class nsXPCOMDetector :
|
||||
private nsPSMDetector,
|
||||
public nsICharsetDetector // Implement the interface
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsXPCOMDetector(PRUint8 aItems, nsVerifier** aVer, nsEUCStatistics** aStatisticsSet);
|
||||
virtual ~nsXPCOMDetector();
|
||||
NS_IMETHOD Init(nsICharsetDetectionObserver* aObserver);
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen, PRBool* oDontFeedMe);
|
||||
NS_IMETHOD Done();
|
||||
|
||||
protected:
|
||||
virtual void Report(const char* charset);
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsICharsetDetectionObserver> mObserver;
|
||||
};
|
||||
|
||||
class nsXPCOMStringDetector :
|
||||
private nsPSMDetector,
|
||||
public nsIStringCharsetDetector // Implement the interface
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsXPCOMStringDetector(PRUint8 aItems, nsVerifier** aVer, nsEUCStatistics** aStatisticsSet);
|
||||
virtual ~nsXPCOMStringDetector();
|
||||
NS_IMETHOD DoIt(const char* aBuf, PRUint32 aLen,
|
||||
const char** oCharset,
|
||||
nsDetectionConfident &oConfident);
|
||||
protected:
|
||||
virtual void Report(const char* charset);
|
||||
private:
|
||||
const char* mResult;
|
||||
};
|
||||
|
||||
class nsJAPSMDetector : public nsXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsJAPSMDetector()
|
||||
: nsXPCOMDetector(JA_DETECTOR_NUM_VERIFIERS, gJaVerifierSet, nsnull) {};
|
||||
};
|
||||
|
||||
class nsJAStringPSMDetector : public nsXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsJAStringPSMDetector()
|
||||
: nsXPCOMStringDetector(JA_DETECTOR_NUM_VERIFIERS - 3, gJaVerifierSet, nsnull) {};
|
||||
};
|
||||
|
||||
class nsKOPSMDetector : public nsXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsKOPSMDetector()
|
||||
: nsXPCOMDetector(KO_DETECTOR_NUM_VERIFIERS, gKoVerifierSet, nsnull){};
|
||||
};
|
||||
|
||||
class nsKOStringPSMDetector : public nsXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsKOStringPSMDetector()
|
||||
: nsXPCOMStringDetector(KO_DETECTOR_NUM_VERIFIERS - 3, gKoVerifierSet, nsnull) {};
|
||||
};
|
||||
|
||||
class nsZHTWPSMDetector : public nsXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsZHTWPSMDetector()
|
||||
: nsXPCOMDetector(ZHTW_DETECTOR_NUM_VERIFIERS, gZhTwVerifierSet, gZhTwStatisticsSet) {};
|
||||
};
|
||||
|
||||
class nsZHTWStringPSMDetector : public nsXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsZHTWStringPSMDetector()
|
||||
: nsXPCOMStringDetector(ZHTW_DETECTOR_NUM_VERIFIERS - 3, gZhTwVerifierSet, gZhTwStatisticsSet) {};
|
||||
};
|
||||
|
||||
class nsZHCNPSMDetector : public nsXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsZHCNPSMDetector()
|
||||
: nsXPCOMDetector(ZHCN_DETECTOR_NUM_VERIFIERS, gZhCnVerifierSet, nsnull) {};
|
||||
};
|
||||
|
||||
class nsZHCNStringPSMDetector : public nsXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsZHCNStringPSMDetector()
|
||||
: nsXPCOMStringDetector(ZHCN_DETECTOR_NUM_VERIFIERS - 3, gZhCnVerifierSet, nsnull) {};
|
||||
};
|
||||
|
||||
class nsZHPSMDetector : public nsXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsZHPSMDetector()
|
||||
: nsXPCOMDetector(ZH_DETECTOR_NUM_VERIFIERS, gZhVerifierSet, gZhStatisticsSet) {};
|
||||
};
|
||||
|
||||
class nsZHStringPSMDetector : public nsXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsZHStringPSMDetector()
|
||||
: nsXPCOMStringDetector(ZH_DETECTOR_NUM_VERIFIERS - 3, gZhVerifierSet, gZhStatisticsSet) {};
|
||||
};
|
||||
|
||||
class nsCJKPSMDetector : public nsXPCOMDetector
|
||||
{
|
||||
public:
|
||||
nsCJKPSMDetector()
|
||||
: nsXPCOMDetector(CJK_DETECTOR_NUM_VERIFIERS, gCJKVerifierSet, gCJKStatisticsSet) {};
|
||||
};
|
||||
|
||||
class nsCJKStringPSMDetector : public nsXPCOMStringDetector
|
||||
{
|
||||
public:
|
||||
nsCJKStringPSMDetector()
|
||||
: nsXPCOMStringDetector(CJK_DETECTOR_NUM_VERIFIERS - 3, gCJKVerifierSet, gCJKStatisticsSet) {};
|
||||
};
|
||||
|
||||
#endif // nsPSMDetectors_h__
|
||||
|
|
|
@ -37,81 +37,34 @@
|
|||
|
||||
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// Class declaration for the class
|
||||
//
|
||||
//==========================================================================
|
||||
class nsXMLEncodingObserver: public nsIElementObserver,
|
||||
public nsIObserver,
|
||||
public nsObserverBase,
|
||||
public nsIXMLEncodingService,
|
||||
public nsSupportsWeakReference {
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
|
||||
nsXMLEncodingObserver();
|
||||
virtual ~nsXMLEncodingObserver();
|
||||
|
||||
/* methode for nsIElementObserver */
|
||||
/*
|
||||
* This method return the tag which the observer care about
|
||||
*/
|
||||
NS_IMETHOD_(const char*)GetTagNameAt(PRUint32 aTagIndex);
|
||||
|
||||
/*
|
||||
* Subject call observer when the parser hit the tag
|
||||
* @param aDocumentID- ID of the document
|
||||
* @param aTag- the tag
|
||||
* @param numOfAttributes - number of attributes
|
||||
* @param nameArray - array of name.
|
||||
* @param valueArray - array of value
|
||||
*/
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, eHTMLTags aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, const PRUnichar* aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
NS_IMETHOD Notify(nsISupports* aDocumentID, const PRUnichar* aTag, const nsStringArray* keys, const nsStringArray* values)
|
||||
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
||||
|
||||
/* methode for nsIObserver */
|
||||
NS_DECL_NSIOBSERVER
|
||||
|
||||
/* methode for nsIXMLEncodingService */
|
||||
NS_IMETHOD Start();
|
||||
NS_IMETHOD End();
|
||||
private:
|
||||
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
nsXMLEncodingObserver::nsXMLEncodingObserver()
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(& g_InstanceCount);
|
||||
bXMLEncodingObserverStarted = PR_FALSE;
|
||||
}
|
||||
//-------------------------------------------------------------------------
|
||||
nsXMLEncodingObserver::~nsXMLEncodingObserver()
|
||||
{
|
||||
PR_AtomicDecrement(& g_InstanceCount);
|
||||
|
||||
// call to end the ObserverService
|
||||
if (bXMLEncodingObserverStarted == PR_TRUE) {
|
||||
End();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
NS_IMPL_ADDREF ( nsXMLEncodingObserver );
|
||||
NS_IMPL_RELEASE ( nsXMLEncodingObserver );
|
||||
|
||||
NS_INTERFACE_MAP_BEGIN(nsXMLEncodingObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIElementObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIXMLEncodingService)
|
||||
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
||||
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIElementObserver)
|
||||
NS_INTERFACE_MAP_END
|
||||
// Use the new scheme
|
||||
NS_IMPL_QUERY_INTERFACE4(nsXMLEncodingObserver,
|
||||
nsIElementObserver,
|
||||
nsIObserver,
|
||||
nsIXMLEncodingService,
|
||||
nsISupportsWeakReference);
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
NS_IMETHODIMP_(const char*) nsXMLEncodingObserver::GetTagNameAt(PRUint32 aTagIndex)
|
||||
|
@ -205,11 +158,7 @@ NS_IMETHODIMP nsXMLEncodingObserver::Notify(
|
|||
{
|
||||
if(! encoding.Equals(currentCharset))
|
||||
{
|
||||
nsICharsetAlias* calias = nsnull;
|
||||
res = nsServiceManager::GetService(
|
||||
kCharsetAliasCID,
|
||||
NS_GET_IID(nsICharsetAlias),
|
||||
(nsISupports**) &calias);
|
||||
nsCOMPtr<nsICharsetAlias> calias = do_GetService(kCharsetAliasCID, &res);
|
||||
if(NS_SUCCEEDED(res) && (nsnull != calias) )
|
||||
{
|
||||
PRBool same = PR_FALSE;
|
||||
|
@ -228,7 +177,6 @@ NS_IMETHODIMP nsXMLEncodingObserver::Notify(
|
|||
}
|
||||
} // if check for GetPreferred
|
||||
} // if check res for Equals
|
||||
nsServiceManager::ReleaseService(kCharsetAliasCID, calias);
|
||||
} // if check res for GetService
|
||||
} // if Equals
|
||||
} // if
|
||||
|
@ -246,19 +194,19 @@ NS_IMETHODIMP nsXMLEncodingObserver::Observe(nsISupports*, const PRUnichar*, con
|
|||
NS_IMETHODIMP nsXMLEncodingObserver::Start()
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
nsAutoString xmlTopic; xmlTopic.AssignWithConversion("xmlparser");
|
||||
nsIObserverService* anObserverService = nsnull;
|
||||
|
||||
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_CONTRACTID,
|
||||
NS_GET_IID(nsIObserverService),
|
||||
(nsISupports**) &anObserverService);
|
||||
if (bXMLEncodingObserverStarted == PR_TRUE)
|
||||
return res;
|
||||
|
||||
nsAutoString xmlTopic; xmlTopic.AssignWithConversion("xmlparser");
|
||||
|
||||
nsCOMPtr<nsIObserverService> anObserverService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &res);
|
||||
if(NS_FAILED(res))
|
||||
goto done;
|
||||
|
||||
res = anObserverService->AddObserver(this, xmlTopic.GetUnicode());
|
||||
|
||||
nsServiceManager::ReleaseService(NS_OBSERVERSERVICE_CONTRACTID,
|
||||
anObserverService);
|
||||
bXMLEncodingObserverStarted = PR_TRUE;
|
||||
done:
|
||||
return res;
|
||||
}
|
||||
|
@ -266,80 +214,19 @@ done:
|
|||
NS_IMETHODIMP nsXMLEncodingObserver::End()
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
nsAutoString xmlTopic; xmlTopic.AssignWithConversion("xmlparser");
|
||||
nsIObserverService* anObserverService = nsnull;
|
||||
|
||||
if (bXMLEncodingObserverStarted == PR_FALSE)
|
||||
return res;
|
||||
|
||||
res = nsServiceManager::GetService(NS_OBSERVERSERVICE_CONTRACTID,
|
||||
NS_GET_IID(nsIObserverService),
|
||||
(nsISupports**) &anObserverService);
|
||||
nsAutoString xmlTopic; xmlTopic.AssignWithConversion("xmlparser");
|
||||
nsCOMPtr<nsIObserverService> anObserverService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &res);
|
||||
if(NS_FAILED(res))
|
||||
goto done;
|
||||
|
||||
res = anObserverService->RemoveObserver(this, xmlTopic.GetUnicode());
|
||||
|
||||
nsServiceManager::ReleaseService(NS_OBSERVERSERVICE_CONTRACTID,
|
||||
anObserverService);
|
||||
bXMLEncodingObserverStarted = PR_FALSE;
|
||||
done:
|
||||
return res;
|
||||
}
|
||||
//==========================================================================
|
||||
|
||||
class nsXMLEncodingObserverFactory : public nsIFactory {
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
public:
|
||||
nsXMLEncodingObserverFactory() {
|
||||
NS_INIT_REFCNT();
|
||||
PR_AtomicIncrement(&g_InstanceCount);
|
||||
}
|
||||
virtual ~nsXMLEncodingObserverFactory() {
|
||||
PR_AtomicDecrement(&g_InstanceCount);
|
||||
}
|
||||
|
||||
NS_IMETHOD CreateInstance(nsISupports* aDelegate, const nsIID& aIID, void** aResult);
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------
|
||||
|
||||
NS_IMPL_ISUPPORTS1( nsXMLEncodingObserverFactory , nsIFactory);
|
||||
|
||||
NS_IMETHODIMP nsXMLEncodingObserverFactory::CreateInstance(
|
||||
nsISupports* aDelegate, const nsIID &aIID, void** aResult)
|
||||
{
|
||||
if(NULL == aResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if(NULL != aDelegate)
|
||||
return NS_ERROR_NO_AGGREGATION;
|
||||
|
||||
*aResult = NULL;
|
||||
|
||||
nsXMLEncodingObserver *inst = new nsXMLEncodingObserver();
|
||||
|
||||
|
||||
if(NULL == inst) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
nsresult res =inst->QueryInterface(aIID, aResult);
|
||||
if(NS_FAILED(res)) {
|
||||
delete inst;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
NS_IMETHODIMP nsXMLEncodingObserverFactory::LockFactory(PRBool aLock)
|
||||
{
|
||||
if(aLock)
|
||||
PR_AtomicIncrement( &g_LockCount );
|
||||
else
|
||||
PR_AtomicDecrement( &g_LockCount );
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//==============================================================
|
||||
nsIFactory* NEW_XML_ENCODING_OBSERVER_FACTORY()
|
||||
{
|
||||
return new nsXMLEncodingObserverFactory();
|
||||
}
|
||||
|
|
|
@ -23,6 +23,57 @@
|
|||
#define nsXMLEncodingObserverFactory_h__
|
||||
|
||||
#include "nsIFactory.h"
|
||||
nsIFactory* NEW_XML_ENCODING_OBSERVER_FACTORY();
|
||||
#include "nsIXMLEncodingService.h"
|
||||
#include "nsIElementObserver.h"
|
||||
#include "nsIObserver.h"
|
||||
#include "nsIObserverService.h"
|
||||
#include "nsObserverBase.h"
|
||||
#include "nsWeakReference.h"
|
||||
|
||||
class nsXMLEncodingObserver: public nsIElementObserver,
|
||||
public nsIObserver,
|
||||
public nsObserverBase,
|
||||
public nsIXMLEncodingService,
|
||||
public nsSupportsWeakReference {
|
||||
public:
|
||||
nsXMLEncodingObserver();
|
||||
virtual ~nsXMLEncodingObserver();
|
||||
|
||||
/* methode for nsIElementObserver */
|
||||
/*
|
||||
* This method return the tag which the observer care about
|
||||
*/
|
||||
NS_IMETHOD_(const char*)GetTagNameAt(PRUint32 aTagIndex);
|
||||
|
||||
/*
|
||||
* Subject call observer when the parser hit the tag
|
||||
* @param aDocumentID- ID of the document
|
||||
* @param aTag- the tag
|
||||
* @param numOfAttributes - number of attributes
|
||||
* @param nameArray - array of name.
|
||||
* @param valueArray - array of value
|
||||
*/
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, eHTMLTags aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, const PRUnichar* aTag, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
NS_IMETHOD Notify(nsISupports* aDocumentID, const PRUnichar* aTag, const nsStringArray* keys, const nsStringArray* values)
|
||||
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
/* methode for nsIObserver */
|
||||
NS_DECL_NSIOBSERVER
|
||||
|
||||
/* methode for nsIXMLEncodingService */
|
||||
NS_IMETHOD Start();
|
||||
NS_IMETHOD End();
|
||||
|
||||
private:
|
||||
NS_IMETHOD Notify(PRUint32 aDocumentID, PRUint32 numOfAttributes,
|
||||
const PRUnichar* nameArray[], const PRUnichar* valueArray[]);
|
||||
|
||||
PRBool bXMLEncodingObserverStarted;
|
||||
};
|
||||
|
||||
#endif // nsXMLEncodingObserverFactory_h__
|
||||
|
|
|
@ -92,10 +92,7 @@ extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* aServMgr, const char *
|
|||
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsIComponentManager* compMgr;
|
||||
rv = servMgr->GetService(kComponentManagerCID,
|
||||
NS_GET_IID(nsIComponentManager),
|
||||
(nsISupports**)&compMgr);
|
||||
nsCOMPtr<nsIComponentManager> compMgr = do_GetService(kComponentManagerCID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = compMgr->RegisterComponent(kJANativeDetectorCID,
|
||||
|
@ -121,8 +118,6 @@ extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* aServMgr, const char *
|
|||
NS_STRCDETECTOR_CONTRACTID_BASE "koms",
|
||||
path,
|
||||
PR_TRUE, PR_TRUE);
|
||||
|
||||
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
@ -133,10 +128,7 @@ extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, const char
|
|||
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsIComponentManager* compMgr;
|
||||
rv = servMgr->GetService(kComponentManagerCID,
|
||||
NS_GET_IID(nsIComponentManager),
|
||||
(nsISupports**)&compMgr);
|
||||
nsCOMPtr<nsIComponentManager> compMgr = do_GetService(kComponentManagerCID, &rv);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = compMgr->UnregisterComponent(kJANativeDetectorCID, path);
|
||||
|
@ -144,7 +136,6 @@ extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, const char
|
|||
rv = compMgr->UnregisterComponent(kKONativeDetectorCID, path);
|
||||
rv = compMgr->UnregisterComponent(kKONativeStringDetectorCID, path);
|
||||
|
||||
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
|
|
@ -138,7 +138,6 @@ NS_IMETHODIMP nsNativeDetector::Init(
|
|||
if(nsnull == aObserver)
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
|
||||
NS_IF_ADDREF(aObserver);
|
||||
mObserver = aObserver;
|
||||
|
||||
HRESULT hr = CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER,
|
||||
|
|
|
@ -136,10 +136,11 @@ void genCyrillicClass(const char* name, const char* charset)
|
|||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
nsresult res = nsServiceManager::GetService(kCharsetConverterManagerCID,
|
||||
NS_GET_IID(nsICharsetConverterManager),
|
||||
(nsISupports**) & gCCM);
|
||||
if(NS_FAILED(res) && (nsnull != gCCM))
|
||||
nsresult res = nsnull;
|
||||
|
||||
nsCOMPtr<nsICharsetConverterManager> gCCM = do_GetService(kCharsetConverterManagerCID, &res);
|
||||
|
||||
if(NS_FAILED(res) && (nsnull != gCCM))
|
||||
{
|
||||
printf("cannot locate CharsetConverterManager\n");
|
||||
return(-1);
|
||||
|
@ -163,6 +164,5 @@ int main(int argc, char** argv) {
|
|||
genCyrillicClass("MacCyrillic", "x-mac-cyrillic");
|
||||
footer();
|
||||
NS_IF_RELEASE(gKOI8REncoder);
|
||||
nsServiceManager::ReleaseService(kCharsetConverterManagerCID, gCCM);
|
||||
return(0);
|
||||
};
|
||||
|
|
Загрузка…
Ссылка в новой задаче