2001-09-26 00:47:52 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-20 23:57:51 +00:00
|
|
|
*
|
2001-09-26 00:47:52 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-08-20 23:57:51 +00:00
|
|
|
*
|
2001-09-26 00:47:52 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-08-20 23:57:51 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-26 00:47:52 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-26 00:47:52 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
#include "nsEntityConverter.h"
|
|
|
|
#include "nsIProperties.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2002-06-23 16:05:35 +00:00
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsString.h"
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// implementation methods
|
|
|
|
//
|
|
|
|
nsEntityConverter::nsEntityConverter()
|
1999-10-18 22:33:14 +00:00
|
|
|
: mVersionList(NULL),
|
|
|
|
mVersionListLength(0)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
1999-10-18 22:33:14 +00:00
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
nsEntityConverter::~nsEntityConverter()
|
|
|
|
{
|
|
|
|
if (NULL != mVersionList) delete [] mVersionList;
|
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEntityConverter::LoadVersionPropertyFile()
|
|
|
|
{
|
2003-09-13 17:55:56 +00:00
|
|
|
NS_NAMED_LITERAL_CSTRING(url, "resource://gre/res/entityTables/htmlEntityVersions.properties");
|
1999-10-18 22:33:14 +00:00
|
|
|
nsresult rv;
|
2002-06-23 16:05:35 +00:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_CreateInstance(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> entities;
|
|
|
|
rv = bundleService->CreateBundle(url.get(), getter_AddRefs(entities));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRInt32 result;
|
|
|
|
|
|
|
|
nsAutoString key;
|
|
|
|
nsXPIDLString value;
|
|
|
|
rv = entities->GetStringFromName(NS_LITERAL_STRING("length").get(),
|
|
|
|
getter_Copies(value));
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),"nsEntityConverter: malformed entity table\n");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mVersionListLength = nsAutoString(value).ToInteger(&result);
|
|
|
|
NS_ASSERTION(32 >= mVersionListLength,"nsEntityConverter: malformed entity table\n");
|
|
|
|
if (32 < mVersionListLength) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mVersionList = new nsEntityVersionList[mVersionListLength];
|
|
|
|
if (!mVersionList) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mVersionListLength && NS_SUCCEEDED(rv); i++) {
|
2000-04-03 06:01:54 +00:00
|
|
|
key.SetLength(0);
|
2000-04-15 10:56:54 +00:00
|
|
|
key.AppendInt(i+1, 10);
|
2002-06-23 16:05:35 +00:00
|
|
|
rv = entities->GetStringFromName(key.get(), getter_Copies(value));
|
1999-10-18 22:33:14 +00:00
|
|
|
PRUint32 len = value.Length();
|
2002-06-23 16:05:35 +00:00
|
|
|
if (kVERSION_STRING_LEN < len) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2002-01-12 03:18:55 +00:00
|
|
|
memcpy(mVersionList[i].mEntityListName, value.get(), len*sizeof(PRUnichar));
|
1999-10-18 22:33:14 +00:00
|
|
|
mVersionList[i].mEntityListName[len] = 0;
|
|
|
|
mVersionList[i].mVersion = (1 << i);
|
|
|
|
}
|
2002-06-23 16:05:35 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-10-18 22:33:14 +00:00
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
already_AddRefed<nsIStringBundle>
|
|
|
|
nsEntityConverter::LoadEntityBundle(PRUint32 version)
|
1999-10-18 22:33:14 +00:00
|
|
|
{
|
2003-09-13 17:55:56 +00:00
|
|
|
nsCAutoString url(NS_LITERAL_CSTRING("resource://gre/res/entityTables/"));
|
1999-10-18 22:33:14 +00:00
|
|
|
const PRUnichar *versionName = NULL;
|
2002-06-23 16:05:35 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return NULL;
|
1999-10-18 22:33:14 +00:00
|
|
|
|
|
|
|
versionName = GetVersionName(version);
|
|
|
|
if (NULL == versionName) return NULL;
|
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
// all property file names are ASCII, like "html40Latin1" so this is safe
|
|
|
|
url.Append(NS_LossyConvertUCS2toASCII(versionName) +
|
|
|
|
NS_LITERAL_CSTRING(".properties"));
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
nsIStringBundle* bundle;
|
|
|
|
rv = bundleService->CreateBundle(url.get(), &bundle);
|
|
|
|
if (NS_FAILED(rv)) return NULL;
|
|
|
|
|
|
|
|
// does this addref right?
|
|
|
|
return bundle;
|
1999-08-20 23:57:51 +00:00
|
|
|
}
|
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
const PRUnichar*
|
|
|
|
nsEntityConverter:: GetVersionName(PRUint32 versionNumber)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
1999-10-18 22:33:14 +00:00
|
|
|
for (PRUint32 i = 0; i < mVersionListLength; i++) {
|
|
|
|
if (versionNumber == mVersionList[i].mVersion)
|
|
|
|
return mVersionList[i].mEntityListName;
|
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
return NULL;
|
1999-08-20 23:57:51 +00:00
|
|
|
}
|
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
nsIStringBundle*
|
|
|
|
nsEntityConverter:: GetVersionBundleInstance(PRUint32 versionNumber)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
1999-10-18 22:33:14 +00:00
|
|
|
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;
|
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
PRUint32 i;
|
|
|
|
for (i = 0; i < mVersionListLength; i++) {
|
|
|
|
if (versionNumber == mVersionList[i].mVersion) {
|
2002-06-23 18:42:50 +00:00
|
|
|
if (!mVersionList[i].mEntities)
|
1999-10-18 22:33:14 +00:00
|
|
|
{ // not loaded
|
|
|
|
// load the property file
|
2002-06-23 16:05:35 +00:00
|
|
|
mVersionList[i].mEntities = LoadEntityBundle(versionNumber);
|
|
|
|
NS_ASSERTION(mVersionList[i].mEntities, "LoadEntityBundle failed");
|
1999-10-18 22:33:14 +00:00
|
|
|
}
|
2002-06-23 16:05:35 +00:00
|
|
|
return mVersionList[i].mEntities.get();
|
1999-10-18 22:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
return NULL;
|
1999-08-20 23:57:51 +00:00
|
|
|
}
|
1999-10-18 22:33:14 +00:00
|
|
|
|
|
|
|
|
1999-08-20 23:57:51 +00:00
|
|
|
//
|
|
|
|
// nsISupports methods
|
|
|
|
//
|
2000-11-17 08:06:12 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsEntityConverter,nsIEntityConverter)
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsIEntityConverter
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
1999-10-18 22:33:14 +00:00
|
|
|
nsEntityConverter::ConvertToEntity(PRUnichar character, PRUint32 entityVersion, char **_retval)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
1999-10-18 22:33:14 +00:00
|
|
|
NS_ASSERTION(_retval, "null ptr- _retval");
|
|
|
|
if(nsnull == _retval)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*_retval = NULL;
|
1999-08-20 23:57:51 +00:00
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
|
|
|
if (0 == (entityVersion & mask))
|
|
|
|
continue;
|
2002-06-23 16:05:35 +00:00
|
|
|
nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
|
|
|
|
NS_ASSERTION(entities, "Cannot get the property file");
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
if (NULL == entities)
|
1999-10-18 22:33:14 +00:00
|
|
|
continue;
|
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
nsAutoString key(NS_LITERAL_STRING("entity."));
|
|
|
|
key.AppendInt(character,10);
|
|
|
|
|
|
|
|
nsXPIDLString value;
|
|
|
|
nsresult rv = entities->GetStringFromName(key.get(), getter_Copies(value));
|
1999-10-18 22:33:14 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-09-29 08:28:41 +00:00
|
|
|
*_retval = ToNewCString(value);
|
1999-10-18 22:33:14 +00:00
|
|
|
if(nsnull == *_retval)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
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;
|
2000-04-03 06:01:54 +00:00
|
|
|
nsString outString;
|
1999-10-18 22:33:14 +00:00
|
|
|
|
|
|
|
// per character look for the entity
|
|
|
|
PRUint32 len = nsCRT::strlen(inString);
|
|
|
|
for (PRUint32 i = 0; i < len; i++) {
|
2002-06-23 16:05:35 +00:00
|
|
|
nsAutoString key(NS_LITERAL_STRING("entity."));
|
|
|
|
key.AppendInt(inString[i],10);
|
|
|
|
|
|
|
|
nsXPIDLString value;
|
|
|
|
|
1999-10-18 22:33:14 +00:00
|
|
|
entity = NULL;
|
|
|
|
for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
|
|
|
if (0 == (entityVersion & mask))
|
|
|
|
continue;
|
2002-06-23 16:05:35 +00:00
|
|
|
nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
|
|
|
|
NS_ASSERTION(entities, "Cannot get the property file");
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
if (NULL == entities)
|
1999-10-18 22:33:14 +00:00
|
|
|
continue;
|
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
nsresult rv = entities->GetStringFromName(key.get(),
|
|
|
|
getter_Copies(value));
|
1999-10-18 22:33:14 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-06-30 11:02:25 +00:00
|
|
|
entity = value.get();
|
1999-10-18 22:33:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NULL != entity) {
|
|
|
|
outString.Append(entity);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outString.Append(&inString[i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-29 08:28:41 +00:00
|
|
|
*_retval = ToNewUnicode(outString);
|
1999-10-18 22:33:14 +00:00
|
|
|
if (NULL == *_retval)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
|
1999-10-08 20:30:58 +00:00
|
|
|
nsresult NS_NewEntityConverter(nsISupports** oResult)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
1999-10-08 20:30:58 +00:00
|
|
|
if(!oResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*oResult = new nsEntityConverter();
|
|
|
|
if(*oResult)
|
|
|
|
NS_ADDREF(*oResult);
|
|
|
|
return (*oResult) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
1999-08-20 23:57:51 +00:00
|
|
|
}
|