2001-09-26 00:47:52 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
#include "nsEntityConverter.h"
|
2002-06-23 16:05:35 +00:00
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsString.h"
|
2010-05-14 09:24:41 +00:00
|
|
|
#include "mozilla/Services.h"
|
2013-09-27 16:45:04 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsCRT.h"
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// implementation methods
|
|
|
|
//
|
2012-08-24 07:49:00 +00:00
|
|
|
nsEntityConverter::nsEntityConverter() :
|
|
|
|
mVersionList(nullptr),
|
|
|
|
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()
|
|
|
|
{
|
2012-08-24 07:49:00 +00:00
|
|
|
if (mVersionList)
|
|
|
|
delete [] mVersionList;
|
1999-10-18 22:33:14 +00:00
|
|
|
}
|
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");
|
2002-06-23 16:05:35 +00:00
|
|
|
|
2010-05-14 09:24:41 +00:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
if (!bundleService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-06-23 16:05:35 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> entities;
|
2010-05-14 09:24:41 +00:00
|
|
|
nsresult rv = bundleService->CreateBundle(url.get(), getter_AddRefs(entities));
|
2002-06-23 16:05:35 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-07-27 13:59:29 +00:00
|
|
|
nsresult result;
|
2002-06-23 16:05:35 +00:00
|
|
|
|
|
|
|
nsAutoString key;
|
|
|
|
nsXPIDLString value;
|
2013-12-13 01:50:01 +00:00
|
|
|
rv = entities->GetStringFromName(MOZ_UTF16("length"),
|
2002-06-23 16:05:35 +00:00
|
|
|
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;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t 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));
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = value.Length();
|
2002-06-23 16:05:35 +00:00
|
|
|
if (kVERSION_STRING_LEN < len) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
memcpy(mVersionList[i].mEntityListName, value.get(), len*sizeof(char16_t));
|
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>
|
2012-08-22 15:56:38 +00:00
|
|
|
nsEntityConverter::LoadEntityBundle(uint32_t version)
|
1999-10-18 22:33:14 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString url(NS_LITERAL_CSTRING("resource://gre/res/entityTables/"));
|
2002-06-23 16:05:35 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
2012-08-24 07:49:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *versionName = GetVersionName(version);
|
2012-08-24 07:49:00 +00:00
|
|
|
NS_ENSURE_TRUE(versionName, nullptr);
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
// all property file names are ASCII, like "html40Latin1" so this is safe
|
2003-11-01 10:57:41 +00:00
|
|
|
LossyAppendUTF16toASCII(versionName, url);
|
2014-05-22 03:48:51 +00:00
|
|
|
url.AppendLiteral(".properties");
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2013-04-28 11:52:10 +00:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
rv = bundleService->CreateBundle(url.get(), getter_AddRefs(bundle));
|
2012-08-24 07:49:00 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2002-06-23 16:05:35 +00:00
|
|
|
|
2013-04-28 11:52:10 +00:00
|
|
|
return bundle.forget();
|
1999-08-20 23:57:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsEntityConverter:: GetVersionName(uint32_t versionNumber)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mVersionListLength; i++) {
|
1999-10-18 22:33:14 +00:00
|
|
|
if (versionNumber == mVersionList[i].mVersion)
|
|
|
|
return mVersionList[i].mEntityListName;
|
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
2012-08-24 07:49:00 +00:00
|
|
|
return nullptr;
|
1999-08-20 23:57:51 +00:00
|
|
|
}
|
|
|
|
|
2002-06-23 16:05:35 +00:00
|
|
|
nsIStringBundle*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsEntityConverter:: GetVersionBundleInstance(uint32_t versionNumber)
|
1999-08-20 23:57:51 +00:00
|
|
|
{
|
2012-08-24 07:49:00 +00:00
|
|
|
if (!mVersionList) {
|
1999-10-18 22:33:14 +00:00
|
|
|
// load the property file which contains available version names
|
|
|
|
// and generate a list of version/name pair
|
2012-08-24 07:49:00 +00:00
|
|
|
if (NS_FAILED(LoadVersionPropertyFile()))
|
|
|
|
return nullptr;
|
1999-10-18 22:33:14 +00:00
|
|
|
}
|
1999-08-20 23:57:51 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t i;
|
1999-10-18 22:33:14 +00:00
|
|
|
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
|
|
|
|
2012-08-24 07:49:00 +00:00
|
|
|
return nullptr;
|
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
|
|
|
|
//
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsEntityConverter,nsIEntityConverter)
|
1999-08-20 23:57:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsIEntityConverter
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 15:02:17 +00:00
|
|
|
nsEntityConverter::ConvertToEntity(char16_t character, uint32_t entityVersion, char **_retval)
|
2005-04-02 18:44:01 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
return ConvertUTF32ToEntity((uint32_t)character, entityVersion, _retval);
|
2005-04-02 18:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsEntityConverter::ConvertUTF32ToEntity(uint32_t character, uint32_t 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");
|
2012-07-30 14:20:58 +00:00
|
|
|
if(nullptr == _retval)
|
1999-10-18 22:33:14 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2012-08-24 07:49:00 +00:00
|
|
|
*_retval = nullptr;
|
1999-08-20 23:57:51 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
1999-10-18 22:33:14 +00:00
|
|
|
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
|
|
|
|
2012-08-24 07:49:00 +00:00
|
|
|
if (!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);
|
2012-07-30 14:20:58 +00:00
|
|
|
if(nullptr == *_retval)
|
1999-10-18 22:33:14 +00:00
|
|
|
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
|
2014-01-04 15:02:17 +00:00
|
|
|
nsEntityConverter::ConvertToEntities(const char16_t *inString, uint32_t entityVersion, char16_t **_retval)
|
1999-10-18 22:33:14 +00:00
|
|
|
{
|
2012-08-24 07:49:00 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(inString);
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
*_retval = nullptr;
|
1999-10-18 22:33:14 +00:00
|
|
|
|
2000-04-03 06:01:54 +00:00
|
|
|
nsString outString;
|
1999-10-18 22:33:14 +00:00
|
|
|
|
|
|
|
// per character look for the entity
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = NS_strlen(inString);
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
2002-06-23 16:05:35 +00:00
|
|
|
nsAutoString key(NS_LITERAL_STRING("entity."));
|
2006-12-21 07:03:23 +00:00
|
|
|
if (NS_IS_HIGH_SURROGATE(inString[i]) &&
|
2005-04-02 18:44:01 +00:00
|
|
|
i + 2 < len &&
|
2006-12-21 07:03:23 +00:00
|
|
|
NS_IS_LOW_SURROGATE(inString[i + 1])) {
|
2006-03-19 07:08:11 +00:00
|
|
|
key.AppendInt(SURROGATE_TO_UCS4(inString[i], inString[i+1]), 10);
|
|
|
|
++i;
|
2005-04-02 18:44:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
key.AppendInt(inString[i],10);
|
|
|
|
}
|
2002-06-23 16:05:35 +00:00
|
|
|
|
|
|
|
nsXPIDLString value;
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *entity = nullptr;
|
2012-08-24 07:49:00 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
1999-10-18 22:33:14 +00:00
|
|
|
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
|
|
|
|
2012-08-24 07:49:00 +00:00
|
|
|
if (!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;
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 07:49:00 +00:00
|
|
|
if (entity) {
|
1999-10-18 22:33:14 +00:00
|
|
|
outString.Append(entity);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outString.Append(&inString[i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-29 08:28:41 +00:00
|
|
|
*_retval = ToNewUnicode(outString);
|
2012-08-24 07:49:00 +00:00
|
|
|
if (!*_retval)
|
1999-10-18 22:33:14 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|