зеркало из https://github.com/mozilla/pjs.git
Changed interface and implementation of nsIEntityConverter,
more entity support (before Latin1 only), added a string based interface. Bug fix 8865, r=ftang.
This commit is contained in:
Родитель
db7dfb14bc
Коммит
df06979b80
|
@ -27,6 +27,14 @@
|
|||
[scriptable, uuid(D14C7111-55E0-11d3-91D9-00105AA3F7DC)]
|
||||
interface nsIEntityConverter : nsISupports
|
||||
{
|
||||
const unsigned long entityNone = 0;
|
||||
const unsigned long html40Latin1 = 1;
|
||||
const unsigned long html40Symbols = 2;
|
||||
const unsigned long html40Special = 4; // excludes ", &, <, >
|
||||
const unsigned long html32 = html40Latin1;
|
||||
const unsigned long html40 = html40Latin1+html40Symbols+html40Special;
|
||||
|
||||
wstring ConvertToEntity(in wchar character);
|
||||
string ConvertToEntity(in wchar character, in unsigned long entityVersion);
|
||||
|
||||
wstring ConvertToEntities(in wstring inString, in unsigned long entityVersion);
|
||||
};
|
|
@ -27,88 +27,154 @@
|
|||
// guids
|
||||
//
|
||||
NS_DEFINE_IID(kIEntityConverterIID,NS_IENTITYCONVERTER_IID);
|
||||
NS_DEFINE_IID(kIFactoryIID,NS_IFACTORY_IID);
|
||||
NS_DEFINE_IID(kIPersistentPropertiesIID,NS_IPERSISTENTPROPERTIES_IID);
|
||||
|
||||
//
|
||||
// implementation methods
|
||||
//
|
||||
nsEntityConverter::nsEntityConverter()
|
||||
: mEntityList(nsnull),
|
||||
mEntityListLength(0)
|
||||
: mVersionList(NULL),
|
||||
mVersionListLength(0)
|
||||
{
|
||||
NS_INIT_REFCNT();
|
||||
nsIPersistentProperties* pEntities = nsnull;
|
||||
nsIURI* url = nsnull;
|
||||
nsIInputStream* in = nsnull;
|
||||
nsresult res;
|
||||
nsString aUrl("resource:/res/entityTables/html40Latin1.properties");
|
||||
|
||||
res = NS_NewURI(&url,aUrl,nsnull);
|
||||
if (NS_FAILED(res)) return;
|
||||
|
||||
res = NS_OpenURI(&in,url);
|
||||
NS_RELEASE(url);
|
||||
if (NS_FAILED(res)) return;
|
||||
|
||||
res = nsComponentManager::CreateInstance(NS_PERSISTENTPROPERTIES_PROGID,nsnull,
|
||||
kIPersistentPropertiesIID,
|
||||
(void**)&pEntities);
|
||||
if(NS_SUCCEEDED(res) && in) {
|
||||
res = pEntities->Load(in);
|
||||
LoadEntityProperties(pEntities);
|
||||
pEntities->Release();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
nsEntityConverter::~nsEntityConverter()
|
||||
{
|
||||
|
||||
if (NULL != mVersionList) delete [] mVersionList;
|
||||
}
|
||||
|
||||
void
|
||||
nsEntityConverter::LoadEntityProperties(nsIPersistentProperties* pEntities)
|
||||
NS_IMETHODIMP
|
||||
nsEntityConverter::LoadVersionPropertyFile()
|
||||
{
|
||||
nsString key, value, temp;
|
||||
PRInt32 i;
|
||||
PRInt32 result;
|
||||
nsString aUrl("resource:/res/entityTables/htmlEntityVersions.properties");
|
||||
nsIPersistentProperties* entityProperties = NULL;
|
||||
nsIURI* url = NULL;
|
||||
nsIInputStream* in = NULL;
|
||||
nsresult rv;
|
||||
|
||||
rv = NS_NewURI(&url,aUrl,NULL);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
key="entity.list.name";
|
||||
nsresult rv = pEntities->GetStringProperty(key,mEntityListName);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv),"nsEntityConverter: malformed entity table\n");
|
||||
if (NS_FAILED(rv)) return;
|
||||
rv = NS_OpenURI(&in,url);
|
||||
NS_RELEASE(url);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
key="entity.list.length";
|
||||
rv = pEntities->GetStringProperty(key,value);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv),"nsEntityConverter: malformed entity table\n");
|
||||
if (NS_FAILED(rv)) return;
|
||||
mEntityListLength = value.ToInteger(&result);
|
||||
rv = nsComponentManager::CreateInstance(NS_PERSISTENTPROPERTIES_PROGID,NULL,
|
||||
kIPersistentPropertiesIID,
|
||||
(void**)&entityProperties);
|
||||
if(NS_SUCCEEDED(rv) && in) {
|
||||
rv = entityProperties->Load(in);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
nsAutoString key("length"), value;
|
||||
PRInt32 result;
|
||||
|
||||
//
|
||||
// allocate the table
|
||||
//
|
||||
mEntityList = new nsEntityEntry[mEntityListLength];
|
||||
if (!mEntityList) return;
|
||||
rv = entityProperties->GetStringProperty(key,value);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv),"nsEntityConverter: malformed entity table\n");
|
||||
if (NS_FAILED(rv)) goto done;
|
||||
mVersionListLength = value.ToInteger(&result);
|
||||
NS_ASSERTION(32 >= mVersionListLength,"nsEntityConverter: malformed entity table\n");
|
||||
if (32 < mVersionListLength) goto done;
|
||||
mVersionList = new nsEntityVersionList[mVersionListLength];
|
||||
if (NULL == mVersionList) {rv = NS_ERROR_OUT_OF_MEMORY; goto done;}
|
||||
|
||||
for(i=1;i<=mEntityListLength;i++)
|
||||
{
|
||||
key="entity.";
|
||||
key.Append(i,10);
|
||||
rv = pEntities->GetStringProperty(key,value);
|
||||
if (NS_FAILED(rv)) return;
|
||||
|
||||
PRInt32 offset = value.Find(":");
|
||||
value.Left(temp,offset);
|
||||
mEntityList[i-1].mChar = (PRUnichar)temp.ToInteger(&result);
|
||||
|
||||
value.Mid(temp,offset+1,-1);
|
||||
mEntityList[i-1].mEntityValue = temp.ToNewUnicode();
|
||||
|
||||
for (PRUint32 i = 0; i < mVersionListLength && NS_SUCCEEDED(rv); i++) {
|
||||
key.SetString("");
|
||||
key.Append(i+1, 10);
|
||||
rv = entityProperties->GetStringProperty(key, value);
|
||||
PRUint32 len = value.Length();
|
||||
if (kVERSION_STRING_LEN < len) {rv = NS_ERROR_OUT_OF_MEMORY; goto done;}
|
||||
nsCRT::memcpy(mVersionList[i].mEntityListName, value.GetUnicode(), len*sizeof(PRUnichar));
|
||||
mVersionList[i].mEntityListName[len] = 0;
|
||||
mVersionList[i].mVersion = (1 << i);
|
||||
mVersionList[i].mEntityProperties = NULL;
|
||||
}
|
||||
}
|
||||
done:
|
||||
NS_IF_RELEASE(in);
|
||||
NS_IF_RELEASE(entityProperties);
|
||||
}
|
||||
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsIPersistentProperties*
|
||||
nsEntityConverter::LoadEntityPropertyFile(PRInt32 version)
|
||||
{
|
||||
nsString aUrl("resource:/res/entityTables/");
|
||||
nsIPersistentProperties* entityProperties = NULL;
|
||||
nsIURI* url = NULL;
|
||||
nsIInputStream* in = NULL;
|
||||
const PRUnichar *versionName = NULL;
|
||||
nsresult rv;
|
||||
|
||||
versionName = GetVersionName(version);
|
||||
if (NULL == versionName) return NULL;
|
||||
|
||||
aUrl.Append(versionName);
|
||||
aUrl.Append(".properties");
|
||||
|
||||
rv = NS_NewURI(&url,aUrl,NULL);
|
||||
if (NS_FAILED(rv)) return NULL;
|
||||
|
||||
rv = NS_OpenURI(&in,url);
|
||||
NS_RELEASE(url);
|
||||
if (NS_FAILED(rv)) return NULL;
|
||||
|
||||
rv = nsComponentManager::CreateInstance(NS_PERSISTENTPROPERTIES_PROGID,NULL,
|
||||
kIPersistentPropertiesIID,
|
||||
(void**)&entityProperties);
|
||||
if(NS_SUCCEEDED(rv) && in) {
|
||||
rv = entityProperties->Load(in);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
NS_IF_RELEASE(in);
|
||||
return entityProperties;
|
||||
}
|
||||
}
|
||||
NS_IF_RELEASE(in);
|
||||
NS_IF_RELEASE(entityProperties);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const PRUnichar*
|
||||
nsEntityConverter:: GetVersionName(PRUint32 versionNumber)
|
||||
{
|
||||
for (PRUint32 i = 0; i < mVersionListLength; i++) {
|
||||
if (versionNumber == mVersionList[i].mVersion)
|
||||
return mVersionList[i].mEntityListName;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
nsIPersistentProperties*
|
||||
nsEntityConverter:: GetVersionPropertyInst(PRUint32 versionNumber)
|
||||
{
|
||||
if (NULL == mVersionList) {
|
||||
// load the property file which contains available version names
|
||||
// and generate a list of version/name pair
|
||||
nsresult rv = LoadVersionPropertyFile();
|
||||
if (NS_FAILED(rv)) return NULL;
|
||||
}
|
||||
|
||||
PRUint32 i;
|
||||
for (i = 0; i < mVersionListLength; i++) {
|
||||
if (versionNumber == mVersionList[i].mVersion) {
|
||||
if (NULL == mVersionList[i].mEntityProperties)
|
||||
{ // not loaded
|
||||
// load the property file
|
||||
mVersionList[i].mEntityProperties = LoadEntityPropertyFile(versionNumber);
|
||||
NS_ASSERTION(mVersionList[i].mEntityProperties, "LoadEntityPropertyFile failed");
|
||||
}
|
||||
return mVersionList[i].mEntityProperties;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// nsISupports methods
|
||||
//
|
||||
|
@ -119,22 +185,84 @@ NS_IMPL_ISUPPORTS(nsEntityConverter,kIEntityConverterIID)
|
|||
// nsIEntityConverter
|
||||
//
|
||||
NS_IMETHODIMP
|
||||
nsEntityConverter::ConvertToEntity(PRUnichar character, PRUnichar **_retval)
|
||||
nsEntityConverter::ConvertToEntity(PRUnichar character, PRUint32 entityVersion, char **_retval)
|
||||
{
|
||||
PRInt32 i;
|
||||
NS_ASSERTION(_retval, "null ptr- _retval");
|
||||
if(nsnull == _retval)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
*_retval = NULL;
|
||||
|
||||
for(i=0;i<mEntityListLength;i++)
|
||||
{
|
||||
if (mEntityList[i].mChar==character) {
|
||||
nsString entity = mEntityList[i].mEntityValue;
|
||||
*_retval = entity.ToNewUnicode();
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
||||
if (0 == (entityVersion & mask))
|
||||
continue;
|
||||
nsIPersistentProperties* entityProperties = GetVersionPropertyInst(entityVersion & mask);
|
||||
NS_ASSERTION(entityProperties, "Cannot get the property file");
|
||||
|
||||
if (NULL == entityProperties)
|
||||
continue;
|
||||
|
||||
nsAutoString key("entity."), value;
|
||||
key.Append(character,10);
|
||||
nsresult rv = entityProperties->GetStringProperty(key, value);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
*_retval = value.ToNewCString();
|
||||
if(nsnull == *_retval)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
else
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsEntityConverter::ConvertToEntities(const PRUnichar *inString, PRUint32 entityVersion, PRUnichar **_retval)
|
||||
{
|
||||
NS_ASSERTION(inString, "null ptr- inString");
|
||||
NS_ASSERTION(_retval, "null ptr- _retval");
|
||||
if((nsnull == inString) || (nsnull == _retval))
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
*_retval = NULL;
|
||||
|
||||
const PRUnichar *entity = NULL;
|
||||
nsString outString("");
|
||||
|
||||
// per character look for the entity
|
||||
PRUint32 len = nsCRT::strlen(inString);
|
||||
for (PRUint32 i = 0; i < len; i++) {
|
||||
nsAutoString key("entity."), value;
|
||||
key.Append(inString[i],10);
|
||||
entity = NULL;
|
||||
for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
||||
if (0 == (entityVersion & mask))
|
||||
continue;
|
||||
nsIPersistentProperties* entityProperties = GetVersionPropertyInst(entityVersion & mask);
|
||||
NS_ASSERTION(entityProperties, "Cannot get the property file");
|
||||
|
||||
if (NULL == entityProperties)
|
||||
continue;
|
||||
|
||||
nsresult rv = entityProperties->GetStringProperty(key, value);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
entity = value.GetUnicode();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (NULL != entity) {
|
||||
outString.Append(entity);
|
||||
}
|
||||
else {
|
||||
outString.Append(&inString[i], 1);
|
||||
}
|
||||
}
|
||||
|
||||
*_retval = outString.ToNewUnicode();
|
||||
if (NULL == *_retval)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
nsresult NS_NewEntityConverter(nsISupports** oResult)
|
||||
|
|
|
@ -20,13 +20,19 @@
|
|||
#include "nsIFactory.h"
|
||||
#include "nsIProperties.h"
|
||||
|
||||
|
||||
nsresult NS_NewEntityConverter(nsISupports** oResult);
|
||||
|
||||
class nsEntityEntry
|
||||
#define kVERSION_STRING_LEN 128
|
||||
|
||||
class nsEntityVersionList
|
||||
{
|
||||
public:
|
||||
PRUnichar mChar;
|
||||
PRUnichar* mEntityValue;
|
||||
nsEntityVersionList() : mEntityProperties(NULL) {}
|
||||
~nsEntityVersionList() {NS_IF_RELEASE(mEntityProperties);}
|
||||
PRUint32 mVersion;
|
||||
PRUnichar mEntityListName[kVERSION_STRING_LEN+1];
|
||||
nsIPersistentProperties *mEntityProperties;
|
||||
};
|
||||
|
||||
class nsEntityConverter: public nsIEntityConverter
|
||||
|
@ -47,16 +53,25 @@ public:
|
|||
//
|
||||
// nsIEntityConverter
|
||||
//
|
||||
NS_IMETHOD ConvertToEntity(PRUnichar character, PRUnichar **_retval);
|
||||
NS_IMETHOD ConvertToEntity(PRUnichar character, PRUint32 entityVersion, char **_retval);
|
||||
|
||||
NS_IMETHOD ConvertToEntities(const PRUnichar *inString, PRUint32 entityVersion, PRUnichar **_retval);
|
||||
|
||||
protected:
|
||||
|
||||
void LoadEntityProperties(nsIPersistentProperties* entityProperties);
|
||||
nsString mEntityListName;
|
||||
nsEntityEntry *mEntityList;
|
||||
PRInt32 mEntityListLength;
|
||||
// load a version property file and generate a version list (number/name pair)
|
||||
NS_IMETHOD LoadVersionPropertyFile();
|
||||
|
||||
// map version number to version string
|
||||
const PRUnichar* GetVersionName(PRUint32 versionNumber);
|
||||
|
||||
// map version number to nsIPersistentProperties
|
||||
nsIPersistentProperties* GetVersionPropertyInst(PRUint32 versionNumber);
|
||||
|
||||
// load a properies file
|
||||
nsIPersistentProperties* LoadEntityPropertyFile(PRInt32 version);
|
||||
|
||||
|
||||
nsEntityVersionList *mVersionList; // array of version number/name pairs
|
||||
PRUint32 mVersionListLength; // number of supported versions
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -104,8 +104,9 @@ nsSaveAsCharset::Convert(const PRUnichar *inString, char **_retval)
|
|||
if (NULL == mEntityConverter) return NS_ERROR_FAILURE;
|
||||
PRUnichar *entity = NULL;
|
||||
// do the entity conversion first
|
||||
rv = DoEntityConversion(inString, &entity);
|
||||
if(NS_SUCCEEDED(rv) && NULL != entity) {
|
||||
rv = mEntityConverter->ConvertToEntities(inString, mEntityVersion, &entity);
|
||||
if(NS_SUCCEEDED(rv)) {
|
||||
if (NULL == entity) return NS_ERROR_OUT_OF_MEMORY;
|
||||
rv = DoCharsetConversion(entity, _retval);
|
||||
nsAllocator::Free(entity);
|
||||
}
|
||||
|
@ -228,61 +229,6 @@ nsSaveAsCharset::DoCharsetConversion(const PRUnichar *inString, char **outString
|
|||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSaveAsCharset::DoEntityConversion(const PRUnichar *inString, PRUnichar **outString)
|
||||
{
|
||||
NS_ASSERTION(outString, "invalid input");
|
||||
if(nsnull == outString)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
*outString = NULL;
|
||||
|
||||
nsresult rv;
|
||||
|
||||
nsString aString(inString), tempString("");
|
||||
|
||||
for (PRInt32 i = 0; i < aString.Length(); i++) {
|
||||
PRUnichar *entity = NULL;
|
||||
rv = mEntityConverter->ConvertToEntity(inString[i], &entity);
|
||||
if (NS_SUCCEEDED(rv) && NULL != entity) {
|
||||
tempString.Append(entity);
|
||||
nsAllocator::Free(entity);
|
||||
}
|
||||
else {
|
||||
tempString.Append(inString[i]);
|
||||
}
|
||||
}
|
||||
*outString = tempString.ToNewUnicode();
|
||||
if(nsnull == outString)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
else
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSaveAsCharset::DoEntityConversion(PRUnichar inCharacter, char *outString, PRInt32 bufferLength)
|
||||
{
|
||||
NS_ASSERTION(outString, "invalid input");
|
||||
if(nsnull == outString)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
*outString = NULL;
|
||||
|
||||
PRUnichar *entity = NULL;
|
||||
nsresult rv;
|
||||
|
||||
rv = mEntityConverter->ConvertToEntity(inCharacter, &entity);
|
||||
if (NS_SUCCEEDED(rv) && NULL != entity) {
|
||||
nsString temp(entity);
|
||||
nsAllocator::Free(entity);
|
||||
|
||||
if (NULL == temp.ToCString(outString, bufferLength))
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSaveAsCharset::DoConversionFallBack(PRUnichar inCharacter, char *outString, PRInt32 bufferLength)
|
||||
{
|
||||
|
@ -298,8 +244,16 @@ nsSaveAsCharset::DoConversionFallBack(PRUnichar inCharacter, char *outString, PR
|
|||
return NS_OK;
|
||||
}
|
||||
if (attr_EntityAfterCharsetConv == MASK_ENTITY(mAttribute)) {
|
||||
rv = DoEntityConversion(inCharacter, outString, bufferLength);
|
||||
if (NS_SUCCEEDED(rv)) return rv;
|
||||
char *entity = NULL;
|
||||
rv = mEntityConverter->ConvertToEntity(inCharacter, mEntityVersion, &entity);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
if (NULL == entity || (PRInt32)nsCRT::strlen(entity) > bufferLength) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
PL_strcpy(outString, entity);
|
||||
nsAllocator::Free(entity);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
switch (MASK_FALLBACK(mAttribute)) {
|
||||
|
@ -341,6 +295,7 @@ NS_NewSaveAsCharset(nsISupports **inst)
|
|||
if(nsnull == inst )
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
*inst = (nsISupports *) new nsSaveAsCharset;
|
||||
if (NULL == *inst) return NS_ERROR_OUT_OF_MEMORY;
|
||||
return NS_OK;
|
||||
if(*inst)
|
||||
NS_ADDREF(*inst);
|
||||
return (*inst) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
|
|
@ -55,10 +55,6 @@ protected:
|
|||
|
||||
NS_IMETHOD DoCharsetConversion(const PRUnichar *inString, char **outString);
|
||||
|
||||
NS_IMETHOD DoEntityConversion(const PRUnichar *inString, PRUnichar **outString);
|
||||
|
||||
NS_IMETHOD DoEntityConversion(PRUnichar inCharacter, char *outString, PRInt32 bufferLength);
|
||||
|
||||
NS_IMETHOD DoConversionFallBack(PRUnichar inCharacter, char *outString, PRInt32 bufferLength);
|
||||
|
||||
// do the fallback, reallocate the buffer if necessary
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "nsTextTransformFactory.h"
|
||||
#include "nsICaseConversion.h"
|
||||
#include "nsEntityConverter.h"
|
||||
#include "nsSaveAsCharset.h"
|
||||
#include "nsUUDll.h"
|
||||
|
||||
PRInt32 g_InstanceCount = 0;
|
||||
|
@ -32,6 +33,7 @@ PRInt32 g_LockCount = 0;
|
|||
static NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
|
||||
static NS_DEFINE_CID(kHankakuToZenkakuCID, NS_HANKAKUTOZENKAKU_CID);
|
||||
static NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID);
|
||||
static NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID);
|
||||
|
||||
// Module implementation
|
||||
class nsUcharUtilModule : public nsIModule
|
||||
|
@ -53,6 +55,7 @@ protected:
|
|||
nsCOMPtr<nsIGenericFactory> mHankakuToZenkakuFactory;
|
||||
nsCOMPtr<nsIGenericFactory> mUnicharUtilFactory;
|
||||
nsCOMPtr<nsIGenericFactory> mEntityConverterFactory;
|
||||
nsCOMPtr<nsIGenericFactory> mSaveAsCharsetFactory;
|
||||
};
|
||||
|
||||
|
||||
|
@ -90,6 +93,7 @@ CreateNew##_name(nsISupports* aOuter, REFNSIID aIID, void **aResult) \
|
|||
MAKE_CTOR(CaseConversion)
|
||||
MAKE_CTOR(HankakuToZenkaku)
|
||||
MAKE_CTOR(EntityConverter)
|
||||
MAKE_CTOR(SaveAsCharset)
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
|
@ -127,6 +131,7 @@ nsUcharUtilModule::Shutdown()
|
|||
mHankakuToZenkakuFactory = nsnull;
|
||||
mUnicharUtilFactory = nsnull;
|
||||
mEntityConverterFactory = nsnull;
|
||||
mSaveAsCharsetFactory = nsnull;
|
||||
}
|
||||
|
||||
// Create a factory object for creating instances of aClass.
|
||||
|
@ -172,6 +177,14 @@ nsUcharUtilModule::GetClassObject(nsIComponentManager *aCompMgr,
|
|||
fact = mEntityConverterFactory;
|
||||
}
|
||||
else
|
||||
if (aClass.Equals(kSaveAsCharsetCID)) {
|
||||
if (!mSaveAsCharsetFactory) {
|
||||
rv = NS_NewGenericFactory(getter_AddRefs(mSaveAsCharsetFactory),
|
||||
CreateNewSaveAsCharset);
|
||||
}
|
||||
fact = mSaveAsCharsetFactory;
|
||||
}
|
||||
else
|
||||
if (aClass.Equals(kHankakuToZenkakuCID)) {
|
||||
if(!mHankakuToZenkakuFactory) {
|
||||
rv = NS_NewGenericFactory(getter_AddRefs(mHankakuToZenkakuFactory),
|
||||
|
@ -209,6 +222,8 @@ static Components gComponents[] = {
|
|||
NS_UNICHARUTIL_PROGID, },
|
||||
{ "Unicode To Entity Converter", &kEntityConverterCID,
|
||||
NS_ENTITYCONVERTER_PROGID, },
|
||||
{ "Unicode To Charset Converter", &kSaveAsCharsetCID,
|
||||
NS_SAVEASCHARSET_PROGID, },
|
||||
{ "Japanese Hankaku To Zenkaku", &kHankakuToZenkakuCID,
|
||||
NS_HANKAKUTOZENKAKU_PROGID, },
|
||||
};
|
||||
|
|
|
@ -14,104 +14,103 @@
|
|||
## Copyright (C) 1999 Netscape Communications Corporation. All Rights
|
||||
## Reserved.
|
||||
##
|
||||
entity.list.name=latin1
|
||||
entity.list.length=96
|
||||
entity.1=160:
|
||||
entity.2=161:¡
|
||||
entity.3=162:¢
|
||||
entity.4=163:£
|
||||
entity.5=164:¤
|
||||
entity.6=165:¥
|
||||
entity.7=166:¦
|
||||
entity.8=167:§
|
||||
entity.9=168:¨
|
||||
entity.10=169:©
|
||||
entity.11=170:ª
|
||||
entity.12=171:«
|
||||
entity.13=172:¬
|
||||
entity.14=173:­
|
||||
entity.15=174:®
|
||||
entity.16=175:¯
|
||||
entity.17=176:°
|
||||
entity.18=177:±
|
||||
entity.19=178:²
|
||||
entity.20=179:³
|
||||
entity.21=180:´
|
||||
entity.22=181:µ
|
||||
entity.23=182:¶
|
||||
entity.24=183:·
|
||||
entity.25=184:¸
|
||||
entity.26=185:¹
|
||||
entity.27=186:º
|
||||
entity.28=187:»
|
||||
entity.29=188:¼
|
||||
entity.30=189:½
|
||||
entity.31=190:¾
|
||||
entity.32=191:¿
|
||||
entity.33=192:À
|
||||
entity.34=193:Á
|
||||
entity.35=194:Â
|
||||
entity.36=195:Ã
|
||||
entity.37=196:Ä
|
||||
entity.38=197:Å
|
||||
entity.39=198:Æ
|
||||
entity.40=199:Ç
|
||||
entity.41=200:È
|
||||
entity.42=201:&Eacite;
|
||||
entity.43=202:Ê
|
||||
entity.44=203:Ë
|
||||
entity.45=204:Ì
|
||||
entity.46=205:&Iactue;
|
||||
entity.47=206:Î
|
||||
entity.48=207:Ï
|
||||
entity.49=208:Ð
|
||||
entity.50=209:&NTilde;
|
||||
entity.51=210:Ò
|
||||
entity.52=211:Ó
|
||||
entity.53=212:Ô
|
||||
entity.54=213:Õ
|
||||
entity.55=214:Ö
|
||||
entity.56=215:×
|
||||
entity.57=216:Ø
|
||||
entity.58=217:Ù
|
||||
entity.59=218:Ú
|
||||
entity.60=219:Û
|
||||
entity.61=220:Ü
|
||||
entity.62=221:Ý
|
||||
entity.63=222:Þ
|
||||
entity.64=223:ß
|
||||
entity.65=224:à
|
||||
entity.66=225:á
|
||||
entity.67=226:â
|
||||
entity.68=227:ã
|
||||
entity.69=228:ä
|
||||
entity.70=229:å
|
||||
entity.71=230:æ
|
||||
entity.72=231:ç
|
||||
entity.73=232:è
|
||||
entity.74=233:é
|
||||
entity.75=234:ê
|
||||
entity.76=235:ë
|
||||
entity.77=236:ì
|
||||
entity.78=237:í
|
||||
entity.79=238:î
|
||||
entity.80=239:ï
|
||||
entity.81=240:ð
|
||||
entity.82=241:ñ
|
||||
entity.83=242:ò
|
||||
entity.84=243:ó
|
||||
entity.85=244:ô
|
||||
entity.86=245:õ
|
||||
entity.87=246:ö
|
||||
entity.88=247:÷
|
||||
entity.89=248:ø
|
||||
entity.90=249:ù
|
||||
entity.91=250:ú
|
||||
entity.92=251:û
|
||||
entity.93=252:ü
|
||||
entity.94=253:ý
|
||||
entity.95=254:þ
|
||||
entity.96=255:ÿ
|
||||
entity.list.name=html40Latin1
|
||||
entity.160=
|
||||
entity.161=¡
|
||||
entity.162=¢
|
||||
entity.163=£
|
||||
entity.164=¤
|
||||
entity.165=¥
|
||||
entity.166=¦
|
||||
entity.167=§
|
||||
entity.168=¨
|
||||
entity.169=©
|
||||
entity.170=ª
|
||||
entity.171=«
|
||||
entity.172=¬
|
||||
entity.173=­
|
||||
entity.174=®
|
||||
entity.175=¯
|
||||
entity.176=°
|
||||
entity.177=±
|
||||
entity.178=²
|
||||
entity.179=³
|
||||
entity.180=´
|
||||
entity.181=µ
|
||||
entity.182=¶
|
||||
entity.183=·
|
||||
entity.184=¸
|
||||
entity.185=¹
|
||||
entity.186=º
|
||||
entity.187=»
|
||||
entity.188=¼
|
||||
entity.189=½
|
||||
entity.190=¾
|
||||
entity.191=¿
|
||||
entity.192=À
|
||||
entity.193=Á
|
||||
entity.194=Â
|
||||
entity.195=Ã
|
||||
entity.196=Ä
|
||||
entity.197=Å
|
||||
entity.198=Æ
|
||||
entity.199=Ç
|
||||
entity.200=È
|
||||
entity.201=&Eacite;
|
||||
entity.202=Ê
|
||||
entity.203=Ë
|
||||
entity.204=Ì
|
||||
entity.205=&Iactue;
|
||||
entity.206=Î
|
||||
entity.207=Ï
|
||||
entity.208=Ð
|
||||
entity.209=&NTilde;
|
||||
entity.210=Ò
|
||||
entity.211=Ó
|
||||
entity.212=Ô
|
||||
entity.213=Õ
|
||||
entity.214=Ö
|
||||
entity.215=×
|
||||
entity.216=Ø
|
||||
entity.217=Ù
|
||||
entity.218=Ú
|
||||
entity.219=Û
|
||||
entity.220=Ü
|
||||
entity.221=Ý
|
||||
entity.222=Þ
|
||||
entity.223=ß
|
||||
entity.224=à
|
||||
entity.225=á
|
||||
entity.226=â
|
||||
entity.227=ã
|
||||
entity.228=ä
|
||||
entity.229=å
|
||||
entity.230=æ
|
||||
entity.231=ç
|
||||
entity.232=è
|
||||
entity.233=é
|
||||
entity.234=ê
|
||||
entity.235=ë
|
||||
entity.236=ì
|
||||
entity.237=í
|
||||
entity.238=î
|
||||
entity.239=ï
|
||||
entity.240=ð
|
||||
entity.241=ñ
|
||||
entity.242=ò
|
||||
entity.243=ó
|
||||
entity.244=ô
|
||||
entity.245=õ
|
||||
entity.246=ö
|
||||
entity.247=÷
|
||||
entity.248=ø
|
||||
entity.249=ù
|
||||
entity.250=ú
|
||||
entity.251=û
|
||||
entity.252=ü
|
||||
entity.253=ý
|
||||
entity.254=þ
|
||||
entity.255=ÿ
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -20,10 +20,20 @@
|
|||
#include "nsIServiceManager.h"
|
||||
#include "nsIComponentManager.h"
|
||||
#include "nsICaseConversion.h"
|
||||
#include "nsIEntityConverter.h"
|
||||
#include "nsISaveAsCharset.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
#include "nsUnicharUtilCIID.h"
|
||||
#include "nsIProperties.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsNeckoUtil.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
|
||||
NS_DEFINE_IID(kCaseConversionIID, NS_ICASECONVERSION_IID);
|
||||
NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID);
|
||||
NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID);
|
||||
NS_DEFINE_IID(kIPersistentPropertiesIID,NS_IPERSISTENTPROPERTIES_IID);
|
||||
|
||||
#if defined(XP_UNIX) || defined(XP_BEOS)
|
||||
#define UNICHARUTIL_DLL_NAME "libunicharutil"MOZ_DLL_SUFFIX
|
||||
|
@ -344,6 +354,151 @@ void TestCaseConversion()
|
|||
|
||||
}
|
||||
|
||||
static void TestEntityConversion(PRUint32 version)
|
||||
{
|
||||
cout << "==============================\n";
|
||||
cout << "Start nsIEntityConverter Test \n";
|
||||
cout << "==============================\n";
|
||||
|
||||
PRInt32 i;
|
||||
nsString inString;
|
||||
PRUnichar uChar;
|
||||
nsresult res;
|
||||
|
||||
|
||||
inString.SetString("\xA0\xA1\xA2\xA3");
|
||||
uChar = (PRUnichar) 8364; //euro
|
||||
inString.Append(&uChar, 1);
|
||||
uChar = (PRUnichar) 9830; //
|
||||
inString.Append(&uChar, 1);
|
||||
|
||||
nsCOMPtr <nsIEntityConverter> entityConv;
|
||||
res = nsComponentManager::CreateInstance(kEntityConverterCID, NULL, nsIEntityConverter::GetIID(), getter_AddRefs(entityConv));
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n"; return;}
|
||||
if (NULL == entityConv) {cout << "\tFailed!! instant null\n"; return;}
|
||||
|
||||
|
||||
// convert char by char
|
||||
for (i = 0; i < inString.Length(); i++) {
|
||||
char *entity = NULL;
|
||||
res = entityConv->ConvertToEntity(inString[i], version, &entity);
|
||||
if (NS_SUCCEEDED(res) && NULL != entity) {
|
||||
cout << inString[i] << " " << entity << "\n";
|
||||
nsAllocator::Free(entity);
|
||||
}
|
||||
}
|
||||
|
||||
// convert at once as a string
|
||||
PRUnichar *entities;
|
||||
res = entityConv->ConvertToEntities(inString.GetUnicode(), version, &entities);
|
||||
if (NS_SUCCEEDED(res) && NULL != entities) {
|
||||
for (i = 0; i < (PRInt32) nsCRT::strlen(entities); i++) {
|
||||
cout << (char) entities[i];
|
||||
if (';' == (char) entities[i])
|
||||
cout << "\n";
|
||||
}
|
||||
nsAllocator::Free(entities);
|
||||
}
|
||||
|
||||
cout << "==============================\n";
|
||||
cout << "Finish nsIEntityConverter Test \n";
|
||||
cout << "==============================\n\n";
|
||||
}
|
||||
|
||||
static void TestSaveAsCharset()
|
||||
{
|
||||
cout << "==============================\n";
|
||||
cout << "Start nsISaveAsCharset Test \n";
|
||||
cout << "==============================\n";
|
||||
|
||||
nsresult res;
|
||||
|
||||
nsString inString("\x61\x62\x80\xA0\x63");
|
||||
char *outString;
|
||||
|
||||
// first, dump input string
|
||||
for (PRInt32 i = 0; i < inString.Length(); i++) {
|
||||
cout << inString[i] << " ";
|
||||
}
|
||||
cout << "\n";
|
||||
|
||||
nsCOMPtr <nsISaveAsCharset> saveAsCharset;
|
||||
res = nsComponentManager::CreateInstance(kSaveAsCharsetCID, NULL, nsISaveAsCharset::GetIID(), getter_AddRefs(saveAsCharset));
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == saveAsCharset) {cout << "\tFailed!! instant null\n";}
|
||||
|
||||
cout << "ISO-8859-1 " << "attr_plainTextDefault " << "entityNone " << "\n";
|
||||
res = saveAsCharset->Init("ISO-8859-1",
|
||||
nsISaveAsCharset::attr_plainTextDefault,
|
||||
nsIEntityConverter::entityNone);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
res = saveAsCharset->Convert(inString.GetUnicode(), &outString);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << outString << "\n"; nsAllocator::Free(outString);}
|
||||
|
||||
cout << "ISO-2022-JP " << "attr_plainTextDefault " << "entityNone " << "\n";
|
||||
res = saveAsCharset->Init("ISO-2022-JP",
|
||||
nsISaveAsCharset::attr_plainTextDefault,
|
||||
nsIEntityConverter::entityNone);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
res = saveAsCharset->Convert(inString.GetUnicode(), &outString);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << outString << "\n"; nsAllocator::Free(outString);}
|
||||
if (NS_ERROR_UENC_NOMAPPING == res) {
|
||||
outString = inString.ToNewUTF8String();
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << "Fall back to UTF-8: " << outString << "\n"; nsAllocator::Free(outString);}
|
||||
}
|
||||
|
||||
cout << "ISO-2022-JP " << "attr_FallbackQuestionMark " << "entityNone " << "\n";
|
||||
res = saveAsCharset->Init("ISO-2022-JP",
|
||||
nsISaveAsCharset::attr_FallbackQuestionMark,
|
||||
nsIEntityConverter::entityNone);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
res = saveAsCharset->Convert(inString.GetUnicode(), &outString);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << outString << "\n"; nsAllocator::Free(outString);}
|
||||
|
||||
cout << "ISO-2022-JP " << "attr_FallbackEscapeU " << "entityNone " << "\n";
|
||||
res = saveAsCharset->Init("ISO-2022-JP",
|
||||
nsISaveAsCharset::attr_FallbackEscapeU,
|
||||
nsIEntityConverter::entityNone);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
res = saveAsCharset->Convert(inString.GetUnicode(), &outString);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << outString << "\n"; nsAllocator::Free(outString);}
|
||||
|
||||
cout << "ISO-8859-1 " << "attr_htmlTextDefault " << "html40Latin1 " << "\n";
|
||||
res = saveAsCharset->Init("ISO-8859-1",
|
||||
nsISaveAsCharset::attr_htmlTextDefault,
|
||||
nsIEntityConverter::html40Latin1);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
res = saveAsCharset->Convert(inString.GetUnicode(), &outString);
|
||||
if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << outString << "\n"; nsAllocator::Free(outString);}
|
||||
|
||||
cout << "ISO-8859-1 " << "attr_FallbackHexNCR+attr_EntityAfterCharsetConv " << "html40Latin1 " << "\n";
|
||||
res = saveAsCharset->Init("ISO-8859-1",
|
||||
nsISaveAsCharset::attr_FallbackHexNCR +
|
||||
nsISaveAsCharset::attr_EntityAfterCharsetConv,
|
||||
nsIEntityConverter::html40Latin1);
|
||||
if (NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
res = saveAsCharset->Convert(inString.GetUnicode(), &outString);
|
||||
if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {cout << "\tFailed!! return value != NS_OK\n";}
|
||||
if (NULL == outString) {cout << "\tFailed!! output null\n";}
|
||||
else {cout << outString << "\n"; nsAllocator::Free(outString);}
|
||||
|
||||
|
||||
cout << "==============================\n";
|
||||
cout << "Finish nsISaveAsCharset Test \n";
|
||||
cout << "==============================\n\n";
|
||||
}
|
||||
|
||||
void RegisterFactories()
|
||||
{
|
||||
nsresult res;
|
||||
|
@ -367,6 +522,14 @@ int main(int argc, char** argv) {
|
|||
|
||||
TestCaseConversion();
|
||||
|
||||
// --------------------------------------------
|
||||
|
||||
TestEntityConversion(nsIEntityConverter::html40);
|
||||
|
||||
// --------------------------------------------
|
||||
|
||||
TestSaveAsCharset();
|
||||
|
||||
// --------------------------------------------
|
||||
cout << "Finish All The Test Cases\n";
|
||||
nsresult res = NS_OK;
|
||||
|
|
|
@ -297,23 +297,19 @@ nsresult nsMsgI18NConvertToEntity(const nsString& inString, nsString* outString)
|
|||
nsresult res;
|
||||
|
||||
outString->SetString("");
|
||||
|
||||
NS_WITH_SERVICE(nsIEntityConverter, entityConv, kEntityConverterCID, &res);
|
||||
nsCOMPtr <nsIEntityConverter> entityConv;
|
||||
res = nsComponentManager::CreateInstance(kEntityConverterCID, NULL,
|
||||
nsIEntityConverter::GetIID(), getter_AddRefs(entityConv));
|
||||
if(NS_SUCCEEDED(res) && (NULL != entityConv)) {
|
||||
for (PRInt32 i = 0; i < inString.Length(); i++) {
|
||||
PRUnichar *entity = NULL;
|
||||
res = entityConv->ConvertToEntity(inString[i], &entity);
|
||||
if (NS_SUCCEEDED(res) && NULL != entity) {
|
||||
outString->Append(entity);
|
||||
nsAllocator::Free(entity);
|
||||
}
|
||||
else {
|
||||
outString->Append(inString[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK; // ignore error since the converter returns error for non entities
|
||||
PRUnichar *entities = NULL;
|
||||
res = entityConv->ConvertToEntities(inString.GetUnicode(), nsIEntityConverter::html40Latin1, &entities);
|
||||
if (NS_SUCCEEDED(res) && (NULL != entities)) {
|
||||
outString->SetString(entities);
|
||||
nsAllocator::Free(entities);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// RICHIE - not sure about this one?? need to see what it did in the old
|
||||
|
|
Загрузка…
Ссылка в новой задаче