1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "nsEntityConverter.h"
40
#include "nsIProperties.h"
41
#include "nsIServiceManager.h"
42
#include "nsIComponentManager.h"
43
#include "nsReadableUtils.h"
45
#include "nsLiteralString.h"
46
#include "nsXPIDLString.h"
50
// implementation methods
52
nsEntityConverter::nsEntityConverter()
58
nsEntityConverter::~nsEntityConverter()
60
if (NULL != mVersionList) delete [] mVersionList;
64
nsEntityConverter::LoadVersionPropertyFile()
66
NS_NAMED_LITERAL_CSTRING(url, "resource://gre/res/entityTables/htmlEntityVersions.properties");
68
nsCOMPtr<nsIStringBundleService> bundleService =
69
do_CreateInstance(NS_STRINGBUNDLE_CONTRACTID, &rv);
71
if (NS_FAILED(rv)) return rv;
73
nsCOMPtr<nsIStringBundle> entities;
74
rv = bundleService->CreateBundle(url.get(), getter_AddRefs(entities));
75
if (NS_FAILED(rv)) return rv;
81
rv = entities->GetStringFromName(NS_LITERAL_STRING("length").get(),
82
getter_Copies(value));
83
NS_ASSERTION(NS_SUCCEEDED(rv),"nsEntityConverter: malformed entity table\n");
84
if (NS_FAILED(rv)) return rv;
86
mVersionListLength = nsAutoString(value).ToInteger(&result);
87
NS_ASSERTION(32 >= mVersionListLength,"nsEntityConverter: malformed entity table\n");
88
if (32 < mVersionListLength) return NS_ERROR_FAILURE;
90
mVersionList = new nsEntityVersionList[mVersionListLength];
91
if (!mVersionList) return NS_ERROR_OUT_OF_MEMORY;
93
for (PRUint32 i = 0; i < mVersionListLength && NS_SUCCEEDED(rv); i++) {
95
key.AppendInt(i+1, 10);
96
rv = entities->GetStringFromName(key.get(), getter_Copies(value));
97
PRUint32 len = value.Length();
98
if (kVERSION_STRING_LEN < len) return NS_ERROR_UNEXPECTED;
100
memcpy(mVersionList[i].mEntityListName, value.get(), len*sizeof(PRUnichar));
101
mVersionList[i].mEntityListName[len] = 0;
102
mVersionList[i].mVersion = (1 << i);
108
already_AddRefed<nsIStringBundle>
109
nsEntityConverter::LoadEntityBundle(PRUint32 version)
111
nsCAutoString url(NS_LITERAL_CSTRING("resource://gre/res/entityTables/"));
112
const PRUnichar *versionName = NULL;
115
nsCOMPtr<nsIStringBundleService> bundleService =
116
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
117
if (NS_FAILED(rv)) return NULL;
119
versionName = GetVersionName(version);
120
if (NULL == versionName) return NULL;
122
// all property file names are ASCII, like "html40Latin1" so this is safe
123
LossyAppendUTF16toASCII(versionName, url);
124
url.Append(".properties");
126
nsIStringBundle* bundle;
127
rv = bundleService->CreateBundle(url.get(), &bundle);
128
if (NS_FAILED(rv)) return NULL;
130
// does this addref right?
135
nsEntityConverter:: GetVersionName(PRUint32 versionNumber)
137
for (PRUint32 i = 0; i < mVersionListLength; i++) {
138
if (versionNumber == mVersionList[i].mVersion)
139
return mVersionList[i].mEntityListName;
146
nsEntityConverter:: GetVersionBundleInstance(PRUint32 versionNumber)
148
if (NULL == mVersionList) {
149
// load the property file which contains available version names
150
// and generate a list of version/name pair
151
nsresult rv = LoadVersionPropertyFile();
152
if (NS_FAILED(rv)) return NULL;
156
for (i = 0; i < mVersionListLength; i++) {
157
if (versionNumber == mVersionList[i].mVersion) {
158
if (!mVersionList[i].mEntities)
160
// load the property file
161
mVersionList[i].mEntities = LoadEntityBundle(versionNumber);
162
NS_ASSERTION(mVersionList[i].mEntities, "LoadEntityBundle failed");
164
return mVersionList[i].mEntities.get();
173
// nsISupports methods
175
NS_IMPL_ISUPPORTS1(nsEntityConverter,nsIEntityConverter)
179
// nsIEntityConverter
182
nsEntityConverter::ConvertToEntity(PRUnichar character, PRUint32 entityVersion, char **_retval)
184
NS_ASSERTION(_retval, "null ptr- _retval");
185
if(nsnull == _retval)
186
return NS_ERROR_NULL_POINTER;
189
for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
190
if (0 == (entityVersion & mask))
192
nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
193
NS_ASSERTION(entities, "Cannot get the property file");
195
if (NULL == entities)
198
nsAutoString key(NS_LITERAL_STRING("entity."));
199
key.AppendInt(character,10);
202
nsresult rv = entities->GetStringFromName(key.get(), getter_Copies(value));
203
if (NS_SUCCEEDED(rv)) {
204
*_retval = ToNewCString(value);
205
if(nsnull == *_retval)
206
return NS_ERROR_OUT_OF_MEMORY;
211
return NS_ERROR_ILLEGAL_VALUE;
215
nsEntityConverter::ConvertToEntities(const PRUnichar *inString, PRUint32 entityVersion, PRUnichar **_retval)
217
NS_ASSERTION(inString, "null ptr- inString");
218
NS_ASSERTION(_retval, "null ptr- _retval");
219
if((nsnull == inString) || (nsnull == _retval))
220
return NS_ERROR_NULL_POINTER;
223
const PRUnichar *entity = NULL;
226
// per character look for the entity
227
PRUint32 len = nsCRT::strlen(inString);
228
for (PRUint32 i = 0; i < len; i++) {
229
nsAutoString key(NS_LITERAL_STRING("entity."));
230
key.AppendInt(inString[i],10);
235
for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
236
if (0 == (entityVersion & mask))
238
nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
239
NS_ASSERTION(entities, "Cannot get the property file");
241
if (NULL == entities)
244
nsresult rv = entities->GetStringFromName(key.get(),
245
getter_Copies(value));
246
if (NS_SUCCEEDED(rv)) {
247
entity = value.get();
251
if (NULL != entity) {
252
outString.Append(entity);
255
outString.Append(&inString[i], 1);
259
*_retval = ToNewUnicode(outString);
260
if (NULL == *_retval)
261
return NS_ERROR_OUT_OF_MEMORY;
268
nsresult NS_NewEntityConverter(nsISupports** oResult)
271
return NS_ERROR_NULL_POINTER;
272
*oResult = new nsEntityConverter();
275
return (*oResult) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;