~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/unicharutil/src/nsEntityConverter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsEntityConverter.h"
 
40
#include "nsIProperties.h"
 
41
#include "nsIServiceManager.h"
 
42
#include "nsIComponentManager.h"
 
43
#include "nsReadableUtils.h"
 
44
#include "nsCRT.h"
 
45
#include "nsLiteralString.h"
 
46
#include "nsXPIDLString.h"
 
47
#include "nsString.h"
 
48
 
 
49
//
 
50
// implementation methods
 
51
//
 
52
nsEntityConverter::nsEntityConverter()
 
53
:       mVersionList(NULL),
 
54
  mVersionListLength(0)
 
55
{
 
56
}
 
57
 
 
58
nsEntityConverter::~nsEntityConverter()
 
59
{
 
60
  if (NULL != mVersionList) delete [] mVersionList;
 
61
}
 
62
 
 
63
NS_IMETHODIMP 
 
64
nsEntityConverter::LoadVersionPropertyFile()
 
65
{
 
66
    NS_NAMED_LITERAL_CSTRING(url, "resource://gre/res/entityTables/htmlEntityVersions.properties");
 
67
        nsresult rv;
 
68
    nsCOMPtr<nsIStringBundleService> bundleService =
 
69
        do_CreateInstance(NS_STRINGBUNDLE_CONTRACTID, &rv);
 
70
 
 
71
    if (NS_FAILED(rv)) return rv;
 
72
    
 
73
    nsCOMPtr<nsIStringBundle> entities;
 
74
    rv = bundleService->CreateBundle(url.get(), getter_AddRefs(entities));
 
75
    if (NS_FAILED(rv)) return rv;
 
76
    
 
77
    PRInt32     result;
 
78
 
 
79
    nsAutoString key;
 
80
    nsXPIDLString value;
 
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;
 
85
      
 
86
    mVersionListLength = nsAutoString(value).ToInteger(&result);
 
87
    NS_ASSERTION(32 >= mVersionListLength,"nsEntityConverter: malformed entity table\n");
 
88
    if (32 < mVersionListLength) return NS_ERROR_FAILURE;
 
89
    
 
90
    mVersionList = new nsEntityVersionList[mVersionListLength];
 
91
    if (!mVersionList) return NS_ERROR_OUT_OF_MEMORY;
 
92
 
 
93
    for (PRUint32 i = 0; i < mVersionListLength && NS_SUCCEEDED(rv); i++) {
 
94
        key.SetLength(0);
 
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;
 
99
        
 
100
        memcpy(mVersionList[i].mEntityListName, value.get(), len*sizeof(PRUnichar));
 
101
        mVersionList[i].mEntityListName[len] = 0;
 
102
        mVersionList[i].mVersion = (1 << i);
 
103
    }
 
104
 
 
105
    return NS_OK;
 
106
}
 
107
 
 
108
already_AddRefed<nsIStringBundle>
 
109
nsEntityConverter::LoadEntityBundle(PRUint32 version)
 
110
{
 
111
  nsCAutoString url(NS_LITERAL_CSTRING("resource://gre/res/entityTables/"));
 
112
  const PRUnichar *versionName = NULL;
 
113
  nsresult rv;
 
114
 
 
115
  nsCOMPtr<nsIStringBundleService> bundleService =
 
116
      do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
 
117
  if (NS_FAILED(rv)) return NULL;
 
118
  
 
119
  versionName = GetVersionName(version);
 
120
  if (NULL == versionName) return NULL;
 
121
 
 
122
  // all property file names are ASCII, like "html40Latin1" so this is safe
 
123
  LossyAppendUTF16toASCII(versionName, url);
 
124
  url.Append(".properties");
 
125
 
 
126
  nsIStringBundle* bundle;
 
127
  rv = bundleService->CreateBundle(url.get(), &bundle);
 
128
  if (NS_FAILED(rv)) return NULL;
 
129
  
 
130
  // does this addref right?
 
131
  return bundle;
 
132
}
 
133
 
 
134
const PRUnichar*
 
135
nsEntityConverter:: GetVersionName(PRUint32 versionNumber)
 
136
{
 
137
  for (PRUint32 i = 0; i < mVersionListLength; i++) {
 
138
    if (versionNumber == mVersionList[i].mVersion)
 
139
      return mVersionList[i].mEntityListName;
 
140
  }
 
141
 
 
142
  return NULL;
 
143
}
 
144
 
 
145
nsIStringBundle*
 
146
nsEntityConverter:: GetVersionBundleInstance(PRUint32 versionNumber)
 
147
{
 
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;
 
153
  }
 
154
 
 
155
  PRUint32 i;
 
156
  for (i = 0; i < mVersionListLength; i++) {
 
157
    if (versionNumber == mVersionList[i].mVersion) {
 
158
      if (!mVersionList[i].mEntities)
 
159
      { // not loaded
 
160
        // load the property file
 
161
        mVersionList[i].mEntities = LoadEntityBundle(versionNumber);
 
162
        NS_ASSERTION(mVersionList[i].mEntities, "LoadEntityBundle failed");
 
163
      }
 
164
      return mVersionList[i].mEntities.get();
 
165
    }
 
166
  }
 
167
 
 
168
  return NULL;
 
169
}
 
170
 
 
171
 
 
172
//
 
173
// nsISupports methods
 
174
//
 
175
NS_IMPL_ISUPPORTS1(nsEntityConverter,nsIEntityConverter)
 
176
 
 
177
 
 
178
//
 
179
// nsIEntityConverter
 
180
//
 
181
NS_IMETHODIMP
 
182
nsEntityConverter::ConvertToEntity(PRUnichar character, PRUint32 entityVersion, char **_retval)
 
183
{
 
184
  NS_ASSERTION(_retval, "null ptr- _retval");
 
185
  if(nsnull == _retval)
 
186
    return NS_ERROR_NULL_POINTER;
 
187
  *_retval = NULL;
 
188
 
 
189
  for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
 
190
    if (0 == (entityVersion & mask)) 
 
191
      continue;
 
192
    nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
 
193
    NS_ASSERTION(entities, "Cannot get the property file");
 
194
 
 
195
    if (NULL == entities) 
 
196
      continue;
 
197
 
 
198
    nsAutoString key(NS_LITERAL_STRING("entity."));
 
199
    key.AppendInt(character,10);
 
200
 
 
201
    nsXPIDLString value;
 
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;
 
207
      else
 
208
        return NS_OK;
 
209
    }
 
210
  }
 
211
        return NS_ERROR_ILLEGAL_VALUE;
 
212
}
 
213
 
 
214
NS_IMETHODIMP
 
215
nsEntityConverter::ConvertToEntities(const PRUnichar *inString, PRUint32 entityVersion, PRUnichar **_retval)
 
216
{
 
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;
 
221
  *_retval = NULL;
 
222
 
 
223
  const PRUnichar *entity = NULL;
 
224
  nsString outString;
 
225
 
 
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);
 
231
    
 
232
    nsXPIDLString value;
 
233
    
 
234
    entity = NULL;
 
235
    for (PRUint32 mask = 1, mask2 = 0xFFFFFFFFL; (0!=(entityVersion & mask2)); mask<<=1, mask2<<=1) {
 
236
      if (0 == (entityVersion & mask)) 
 
237
         continue;
 
238
      nsIStringBundle* entities = GetVersionBundleInstance(entityVersion & mask);
 
239
      NS_ASSERTION(entities, "Cannot get the property file");
 
240
 
 
241
      if (NULL == entities) 
 
242
          continue;
 
243
 
 
244
      nsresult rv = entities->GetStringFromName(key.get(),
 
245
                                                getter_Copies(value));
 
246
      if (NS_SUCCEEDED(rv)) {
 
247
        entity = value.get();
 
248
        break;
 
249
      }
 
250
    }
 
251
    if (NULL != entity) {
 
252
      outString.Append(entity);
 
253
    }
 
254
    else {
 
255
      outString.Append(&inString[i], 1);
 
256
    }
 
257
  }
 
258
 
 
259
  *_retval = ToNewUnicode(outString);
 
260
  if (NULL == *_retval) 
 
261
    return NS_ERROR_OUT_OF_MEMORY;
 
262
 
 
263
  return NS_OK;
 
264
}
 
265
 
 
266
 
 
267
 
 
268
nsresult NS_NewEntityConverter(nsISupports** oResult)
 
269
{
 
270
   if(!oResult)
 
271
      return NS_ERROR_NULL_POINTER;
 
272
   *oResult = new nsEntityConverter();
 
273
   if(*oResult)
 
274
      NS_ADDREF(*oResult);
 
275
   return (*oResult) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 
276
}