2015-04-22 07:21:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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-21 03:57:51 +04:00
|
|
|
|
|
|
|
#include "nsEntityConverter.h"
|
2002-06-23 20:05:35 +04:00
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsString.h"
|
2010-05-14 13:24:41 +04:00
|
|
|
#include "mozilla/Services.h"
|
2013-09-27 20:45:04 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsCRT.h"
|
1999-08-21 03:57:51 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// implementation methods
|
|
|
|
//
|
2015-04-22 07:21:00 +03:00
|
|
|
nsEntityConverter::nsEntityConverter() { }
|
1999-08-21 03:57:51 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
nsEntityConverter::~nsEntityConverter() { }
|
1999-08-21 03:57:51 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
nsIStringBundle*
|
|
|
|
nsEntityConverter:: GetVersionBundleInstance(uint32_t versionNumber)
|
1999-10-19 02:33:14 +04:00
|
|
|
{
|
2015-04-22 07:21:00 +03:00
|
|
|
switch(versionNumber){
|
|
|
|
case nsIEntityConverter::html40Latin1:
|
|
|
|
if (!mHTML40Latin1Bundle) {
|
|
|
|
mHTML40Latin1Bundle = LoadEntityBundle(kHTML40LATIN1);
|
|
|
|
MOZ_ASSERT(mHTML40Latin1Bundle, "LoadEntityBundle failed");
|
1999-10-19 02:33:14 +04:00
|
|
|
}
|
2015-04-22 07:21:00 +03:00
|
|
|
return mHTML40Latin1Bundle;
|
|
|
|
case nsIEntityConverter::html40Symbols:
|
|
|
|
if (!mHTML40SymbolsBundle) {
|
|
|
|
mHTML40SymbolsBundle = LoadEntityBundle(kHTML40SYMBOLS);
|
|
|
|
MOZ_ASSERT(mHTML40SymbolsBundle, "LoadEntityBundle failed");
|
|
|
|
}
|
|
|
|
return mHTML40SymbolsBundle;
|
|
|
|
case nsIEntityConverter::html40Special:
|
|
|
|
if (!mHTML40SpecialBundle) {
|
|
|
|
mHTML40SpecialBundle = LoadEntityBundle(kHTML40SPECIAL);
|
|
|
|
MOZ_ASSERT(mHTML40SpecialBundle, "LoadEntityBundle failed");
|
|
|
|
}
|
|
|
|
return mHTML40SpecialBundle;
|
|
|
|
case nsIEntityConverter::mathml20:
|
|
|
|
if (!mMathML20Bundle) {
|
|
|
|
mMathML20Bundle = LoadEntityBundle(kMATHML20);
|
|
|
|
MOZ_ASSERT(mMathML20Bundle, "LoadEntityBundle failed");
|
|
|
|
}
|
|
|
|
return mMathML20Bundle;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
1999-10-19 02:33:14 +04:00
|
|
|
}
|
1999-08-21 03:57:51 +04:00
|
|
|
|
2002-06-23 20:05:35 +04:00
|
|
|
already_AddRefed<nsIStringBundle>
|
2015-04-22 07:21:00 +03:00
|
|
|
nsEntityConverter:: LoadEntityBundle(const char *fileName)
|
1999-10-19 02:33:14 +04:00
|
|
|
{
|
2015-04-22 07:21:00 +03:00
|
|
|
NS_ENSURE_TRUE(fileName, nullptr);
|
|
|
|
|
|
|
|
nsAutoCString url("resource://gre/res/entityTables/");
|
2002-06-23 20:05:35 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
2015-04-22 07:21:00 +03:00
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
2012-08-24 11:49:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
url.Append(fileName);
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2013-04-28 15:52:10 +04:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
rv = bundleService->CreateBundle(url.get(), getter_AddRefs(bundle));
|
2012-08-24 11:49:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
1999-08-21 03:57:51 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
return bundle.forget();
|
1999-08-21 03:57:51 +04:00
|
|
|
}
|
1999-10-19 02:33:14 +04:00
|
|
|
|
1999-08-21 03:57:51 +04:00
|
|
|
//
|
|
|
|
// nsISupports methods
|
|
|
|
//
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsEntityConverter,nsIEntityConverter)
|
1999-08-21 03:57:51 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// nsIEntityConverter
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsEntityConverter::ConvertToEntity(char16_t character, uint32_t entityVersion, char **_retval)
|
2015-04-22 07:21:00 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
return ConvertUTF32ToEntity((uint32_t)character, entityVersion, _retval);
|
2005-04-02 22:44:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsEntityConverter::ConvertUTF32ToEntity(uint32_t character, uint32_t entityVersion, char **_retval)
|
1999-08-21 03:57:51 +04:00
|
|
|
{
|
1999-10-19 02:33:14 +04:00
|
|
|
NS_ASSERTION(_retval, "null ptr- _retval");
|
2015-04-22 07:21:00 +03:00
|
|
|
if (nullptr == _retval) {
|
1999-10-19 02:33:14 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2015-04-22 07:21:00 +03:00
|
|
|
}
|
2012-08-24 11:49:00 +04:00
|
|
|
*_retval = nullptr;
|
1999-08-21 03:57:51 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
2015-04-22 07:21:00 +03:00
|
|
|
if (0 == (entityVersion & mask)) {
|
1999-10-19 02:33:14 +04:00
|
|
|
continue;
|
2015-04-22 07:21:00 +03:00
|
|
|
}
|
|
|
|
|
2002-06-23 20:05:35 +04:00
|
|
|
nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
|
2015-04-22 07:21:00 +03:00
|
|
|
NS_ASSERTION(entities, "Cannot get the entity");
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
if (!entities) {
|
1999-10-19 02:33:14 +04:00
|
|
|
continue;
|
2015-04-22 07:21:00 +03:00
|
|
|
}
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2002-06-23 20:05:35 +04:00
|
|
|
nsAutoString key(NS_LITERAL_STRING("entity."));
|
|
|
|
key.AppendInt(character,10);
|
|
|
|
|
|
|
|
nsXPIDLString value;
|
|
|
|
nsresult rv = entities->GetStringFromName(key.get(), getter_Copies(value));
|
1999-10-19 02:33:14 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-09-29 12:28:41 +04:00
|
|
|
*_retval = ToNewCString(value);
|
2015-04-22 07:21:00 +03:00
|
|
|
return NS_OK;
|
1999-10-19 02:33:14 +04:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 07:21:00 +03:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
1999-08-21 03:57:51 +04:00
|
|
|
}
|
|
|
|
|
1999-10-19 02:33:14 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsEntityConverter::ConvertToEntities(const char16_t *inString, uint32_t entityVersion, char16_t **_retval)
|
1999-10-19 02:33:14 +04:00
|
|
|
{
|
2012-08-24 11:49:00 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(inString);
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
|
|
|
*_retval = nullptr;
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2000-04-03 10:01:54 +04:00
|
|
|
nsString outString;
|
1999-10-19 02:33:14 +04:00
|
|
|
|
|
|
|
// per character look for the entity
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = NS_strlen(inString);
|
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
2002-06-23 20:05:35 +04:00
|
|
|
nsAutoString key(NS_LITERAL_STRING("entity."));
|
2015-04-22 07:21:00 +03:00
|
|
|
if (NS_IS_HIGH_SURROGATE(inString[i]) && i + 2 < len && NS_IS_LOW_SURROGATE(inString[i + 1])) {
|
2006-03-19 10:08:11 +03:00
|
|
|
key.AppendInt(SURROGATE_TO_UCS4(inString[i], inString[i+1]), 10);
|
|
|
|
++i;
|
2015-04-22 07:21:00 +03:00
|
|
|
} else {
|
2005-04-02 22:44:01 +04:00
|
|
|
key.AppendInt(inString[i],10);
|
|
|
|
}
|
2015-04-22 07:21:00 +03:00
|
|
|
|
2002-06-23 20:05:35 +04:00
|
|
|
nsXPIDLString value;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *entity = nullptr;
|
2012-08-24 11:49:00 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
|
2015-04-22 07:21:00 +03:00
|
|
|
if (0 == (entityVersion & mask)) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-06-23 20:05:35 +04:00
|
|
|
nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
|
|
|
|
NS_ASSERTION(entities, "Cannot get the property file");
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
if (!entities) {
|
|
|
|
continue;
|
|
|
|
}
|
1999-10-19 02:33:14 +04:00
|
|
|
|
2015-04-22 07:21:00 +03:00
|
|
|
nsresult rv = entities->GetStringFromName(key.get(), getter_Copies(value));
|
1999-10-19 02:33:14 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-06-30 15:02:25 +04:00
|
|
|
entity = value.get();
|
1999-10-19 02:33:14 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 11:49:00 +04:00
|
|
|
if (entity) {
|
1999-10-19 02:33:14 +04:00
|
|
|
outString.Append(entity);
|
2015-04-22 07:21:00 +03:00
|
|
|
} else {
|
1999-10-19 02:33:14 +04:00
|
|
|
outString.Append(&inString[i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-29 12:28:41 +04:00
|
|
|
*_retval = ToNewUnicode(outString);
|
1999-10-19 02:33:14 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|