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:
nhotta%netscape.com 1999-10-18 22:33:14 +00:00
Родитель db7dfb14bc
Коммит df06979b80
9 изменённых файлов: 537 добавлений и 262 удалений

Просмотреть файл

@ -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 &quot, &amp, &lt, &gt
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:&nbsp;
entity.2=161:&iexcl;
entity.3=162:&cent;
entity.4=163:&pound;
entity.5=164:&curren;
entity.6=165:&yen;
entity.7=166:&brvbar;
entity.8=167:&sect;
entity.9=168:&uml;
entity.10=169:&copy;
entity.11=170:&ordf;
entity.12=171:&laquo;
entity.13=172:&not;
entity.14=173:&shy;
entity.15=174:&reg;
entity.16=175:&macr;
entity.17=176:&deg;
entity.18=177:&plusmn;
entity.19=178:&sup2;
entity.20=179:&sup3;
entity.21=180:&acute;
entity.22=181:&micro;
entity.23=182:&para;
entity.24=183:&middot;
entity.25=184:&cedil;
entity.26=185:&sup1;
entity.27=186:&ordm;
entity.28=187:&raquo;
entity.29=188:&frac14;
entity.30=189:&frac12;
entity.31=190:&frac34;
entity.32=191:&iquest;
entity.33=192:&Agrave;
entity.34=193:&Aacute;
entity.35=194:&Acirc;
entity.36=195:&Atilde;
entity.37=196:&Auml;
entity.38=197:&Aring;
entity.39=198:&AElig;
entity.40=199:&Ccedil;
entity.41=200:&Egrave;
entity.42=201:&Eacite;
entity.43=202:&Ecirc;
entity.44=203:&Euml;
entity.45=204:&Igrave;
entity.46=205:&Iactue;
entity.47=206:&Icirc;
entity.48=207:&Iuml;
entity.49=208:&ETH;
entity.50=209:&NTilde;
entity.51=210:&Ograve;
entity.52=211:&Oacute;
entity.53=212:&Ocirc;
entity.54=213:&Otilde;
entity.55=214:&Ouml;
entity.56=215:&times;
entity.57=216:&Oslash;
entity.58=217:&Ugrave;
entity.59=218:&Uacute;
entity.60=219:&Ucirc;
entity.61=220:&Uuml;
entity.62=221:&Yacute;
entity.63=222:&THORN;
entity.64=223:&szlig;
entity.65=224:&agrave;
entity.66=225:&aacute;
entity.67=226:&acirc;
entity.68=227:&atilde;
entity.69=228:&auml;
entity.70=229:&aring;
entity.71=230:&aelig;
entity.72=231:&ccedil;
entity.73=232:&egrave;
entity.74=233:&eacute;
entity.75=234:&ecirc;
entity.76=235:&euml;
entity.77=236:&igrave;
entity.78=237:&iacute;
entity.79=238:&icirc;
entity.80=239:&iuml;
entity.81=240:&eth;
entity.82=241:&ntilde;
entity.83=242:&ograve;
entity.84=243:&oacute;
entity.85=244:&ocirc;
entity.86=245:&otilde;
entity.87=246:&ouml;
entity.88=247:&divide;
entity.89=248:&oslash;
entity.90=249:&ugrave;
entity.91=250:&uacute;
entity.92=251:&ucirc;
entity.93=252:&uuml;
entity.94=253:&yacute;
entity.95=254:&thorn;
entity.96=255:&yuml;
entity.list.name=html40Latin1
entity.160=&nbsp;
entity.161=&iexcl;
entity.162=&cent;
entity.163=&pound;
entity.164=&curren;
entity.165=&yen;
entity.166=&brvbar;
entity.167=&sect;
entity.168=&uml;
entity.169=&copy;
entity.170=&ordf;
entity.171=&laquo;
entity.172=&not;
entity.173=&shy;
entity.174=&reg;
entity.175=&macr;
entity.176=&deg;
entity.177=&plusmn;
entity.178=&sup2;
entity.179=&sup3;
entity.180=&acute;
entity.181=&micro;
entity.182=&para;
entity.183=&middot;
entity.184=&cedil;
entity.185=&sup1;
entity.186=&ordm;
entity.187=&raquo;
entity.188=&frac14;
entity.189=&frac12;
entity.190=&frac34;
entity.191=&iquest;
entity.192=&Agrave;
entity.193=&Aacute;
entity.194=&Acirc;
entity.195=&Atilde;
entity.196=&Auml;
entity.197=&Aring;
entity.198=&AElig;
entity.199=&Ccedil;
entity.200=&Egrave;
entity.201=&Eacite;
entity.202=&Ecirc;
entity.203=&Euml;
entity.204=&Igrave;
entity.205=&Iactue;
entity.206=&Icirc;
entity.207=&Iuml;
entity.208=&ETH;
entity.209=&NTilde;
entity.210=&Ograve;
entity.211=&Oacute;
entity.212=&Ocirc;
entity.213=&Otilde;
entity.214=&Ouml;
entity.215=&times;
entity.216=&Oslash;
entity.217=&Ugrave;
entity.218=&Uacute;
entity.219=&Ucirc;
entity.220=&Uuml;
entity.221=&Yacute;
entity.222=&THORN;
entity.223=&szlig;
entity.224=&agrave;
entity.225=&aacute;
entity.226=&acirc;
entity.227=&atilde;
entity.228=&auml;
entity.229=&aring;
entity.230=&aelig;
entity.231=&ccedil;
entity.232=&egrave;
entity.233=&eacute;
entity.234=&ecirc;
entity.235=&euml;
entity.236=&igrave;
entity.237=&iacute;
entity.238=&icirc;
entity.239=&iuml;
entity.240=&eth;
entity.241=&ntilde;
entity.242=&ograve;
entity.243=&oacute;
entity.244=&ocirc;
entity.245=&otilde;
entity.246=&ouml;
entity.247=&divide;
entity.248=&oslash;
entity.249=&ugrave;
entity.250=&uacute;
entity.251=&ucirc;
entity.252=&uuml;
entity.253=&yacute;
entity.254=&thorn;
entity.255=&yuml;

Просмотреть файл

@ -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