summaryrefslogtreecommitdiff
path: root/TAO/tao/Codeset_Manager.cpp
blob: 9bdb67d0b9e19986628a5502ae36b6ab8082ba60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// $Id$

#include "tao/Codeset_Manager.h"
#include "tao/TAO_Server_Request.h"
#include "tao/operation_details.h"
#include "tao/Transport.h"
#include "tao/Profile.h"
#include "tao/Codeset_Translator_Factory.h"
#include "tao/SystemException.h"
#include "tao/UTF16_BOM_Factory.h"
#include "tao/debug.h"

#include "ace/Dynamic_Service.h"
#include "ace/Codeset_Registry.h"
#include "ace/OS_NS_string.h"


ACE_RCSID (tao,
           Codeset_Manager,
           "$Id$")


// These numbers are assigned by the OpenGroup, a database is
// available at
//
// ftp://ftp.opengroup.org/pub/code_set_registry/
//
#define TAO_CODESET_ID_ISO8859_1 0x00010001U
#define TAO_CODESET_ID_UNICODE   0x00010109U

// These are the default codesets that TAO declares, of course they
// will be different on each platform, once the complete support for
// character sets is implemented
#define TAO_DEFAULT_CHAR_CODESET_ID  TAO_CODESET_ID_ISO8859_1
#define TAO_DEFAULT_WCHAR_CODESET_ID TAO_CODESET_ID_UNICODE

// ****************************************************************

/// NCS for char is defaulted to ISO 8859-1:1987; Latin Alphabet No. 1
CONV_FRAME::CodeSetId
TAO_Codeset_Manager::default_char_codeset = TAO_DEFAULT_CHAR_CODESET_ID;
/// NCS for wchar is not defaulted by the CORBA specification, but a default
/// may be set here if desired
CONV_FRAME::CodeSetId
TAO_Codeset_Manager::default_wchar_codeset = TAO_DEFAULT_WCHAR_CODESET_ID;

TAO_Codeset_Manager::TAO_Codeset_Manager ()
  : codeset_info_ (),
    char_factories_ (),
    wchar_factories_ (),
    utf16_bom_translator_ (0)
{
  this->codeset_info_.ForCharData.native_code_set =
    TAO_Codeset_Manager::default_char_codeset;
  this->codeset_info_.ForWcharData.native_code_set =
    TAO_Codeset_Manager::default_wchar_codeset;
}

TAO_Codeset_Manager::~TAO_Codeset_Manager ()
{
  // Cleanup the character map
  TAO_CodesetFactorySetItor cf_end = this->char_factories_.end ();
  TAO_CodesetFactorySetItor cf_iter = this->char_factories_.begin ();

  for (; cf_iter != cf_end; ++cf_iter)
    {
      delete *cf_iter;
    }

  this->char_factories_.reset ();

  // Cleanup the wide character map
  cf_end = this->wchar_factories_.end ();
  cf_iter = this->wchar_factories_.begin ();

  for (;cf_iter != cf_end; ++cf_iter)
    {
      delete *cf_iter;
    }

  this->wchar_factories_.reset ();

  delete this->utf16_bom_translator_;
}

void
TAO_Codeset_Manager::set_codeset (TAO_Tagged_Components& tc) const
{
  tc.set_code_sets (this->codeset_info_);
}

void
TAO_Codeset_Manager::set_tcs (TAO_Profile &theProfile,
                              TAO_Transport &trans)
{
  /// If tcs is already set on the transport then donot process,
  /// use existing transport as CDR have translators set.
  if (trans.is_tcs_set ())
    {
      if(TAO_debug_level > 2)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::set_tcs, ")
                      ACE_TEXT ("transport already set\n")));
        }

      return;
    }

  TAO_Tagged_Components& theTaggedComp = theProfile.tagged_components ();

  CONV_FRAME::CodeSetComponentInfo remote;

  /// Get the codeset component
  if (theTaggedComp.get_code_sets(remote) == 0 )
    {
      if (TAO_debug_level > 2)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::set_tcs, ")
                      ACE_TEXT ("No codeset component in profile\n")));
        }

      remote.ForCharData.native_code_set =
        TAO_Codeset_Manager::default_char_codeset;
      remote.ForWcharData.native_code_set =
        TAO_Codeset_Manager::default_wchar_codeset;
    }
   else
     {
       CONV_FRAME::CodeSetId tcs =
         computeTCS (remote.ForCharData,
                     this->codeset_info_.ForCharData);
       if (TAO_debug_level > 2)
         ACE_DEBUG ((LM_DEBUG,
                     ACE_TEXT("TAO (%P|%t) - Codeset_Manager::set_tcs, ")
                     ACE_TEXT("setting char translator (%08x)\n"),
                     tcs));
       trans.char_translator(this->get_char_trans (tcs));

       tcs = computeTCS (remote.ForWcharData,
                         this->codeset_info_.ForWcharData);

       if (TAO_debug_level > 2)
         ACE_DEBUG ((LM_DEBUG,
                     ACE_TEXT("TAO (%P|%t) - Codeset_Manager::set_tcs, ")
                     ACE_TEXT("setting wchar translator (%08x)\n"),
                     tcs));
       trans.wchar_translator(this->get_wchar_trans (tcs));
     }
}

void
TAO_Codeset_Manager::process_service_context (TAO_ServerRequest &request)
{
  if (request.transport()->is_tcs_set ())
    {
      return;
    }

  // Get the service Context from an object of TAO_ServerRequest
  // and set the TCS values on the Transport
  TAO_Service_Context &service_cntx = request.request_service_context ();
  IOP::ServiceContext context;
  context.context_id = IOP::CodeSets;

  CONV_FRAME::CodeSetId tcs_c = TAO_Codeset_Manager::default_char_codeset;
  CONV_FRAME::CodeSetId tcs_w = TAO_Codeset_Manager::default_wchar_codeset;
  if (service_cntx.get_context(context))
    {
      // Convert the Service Context to Codeset Context
      const char *buffer =
        reinterpret_cast<const char*> (context.context_data.get_buffer ());

      TAO_InputCDR cdr (buffer,context.context_data.length ());
      CORBA::Boolean byte_order;

      if (cdr >> TAO_InputCDR::to_boolean (byte_order))
        {
          cdr.reset_byte_order (static_cast<int> (byte_order));
          cdr >> tcs_c;
          cdr >> tcs_w;
        }
    }
  else
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT("TAO (%P|%t) - Codeset_Manager::process_service_context ")
                    ACE_TEXT("no codeset context in request, inferring TAO backwards compatibility\n")));
    }
  if (TAO_debug_level > 2)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                  ACE_TEXT ("process_service_context, ")
                  ACE_TEXT ("using tcsc = %08x, tcsw = %08x\n"),
                  tcs_c,tcs_w));
    }

  request.transport()->char_translator(this->get_char_trans (tcs_c));
  request.transport()->wchar_translator(this->get_wchar_trans (tcs_w));
}

void
TAO_Codeset_Manager::generate_service_context (TAO_Operation_Details &opd,
                                               TAO_Transport &trans)
{
  TAO_Service_Context &service_cntx = opd.request_service_context ();
  CONV_FRAME::CodeSetContext codeset_cntx;

  // Generating codeset context
  // Assuming the TCS values from Transport will be defaulted
  TAO_Codeset_Translator_Factory *tf = trans.char_translator ();

  codeset_cntx.char_data =
    tf ? tf->tcs () : this->codeset_info_.ForCharData.native_code_set;

  tf = trans.wchar_translator ();

  codeset_cntx.wchar_data =
    tf ? tf->tcs () : this->codeset_info_.ForWcharData.native_code_set;

  if (TAO_debug_level > 2)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                  ACE_TEXT ("generate_service_context, ")
                  ACE_TEXT ("using tcs_c = %08x, tcs_w = %08x\n"),
                  codeset_cntx.char_data,
                  codeset_cntx.wchar_data));
    }

  TAO_OutputCDR codeset_cdr;
  codeset_cdr << TAO_OutputCDR::from_boolean (TAO_ENCAP_BYTE_ORDER);
  codeset_cdr << codeset_cntx;

  service_cntx.set_context (IOP::CodeSets,codeset_cdr);
}

/// Checks whether the NCS is a part of CCS
int
TAO_Codeset_Manager::isElementOf (CONV_FRAME::CodeSetId id,
                                  CONV_FRAME::CodeSetComponent &cs_comp)
{
  for (CORBA::ULong i = 0L;
       i < cs_comp.conversion_code_sets.length ();
       ++i )
    {
      if (id == cs_comp.conversion_code_sets[i])
        return 1;
    }

  return 0;
}

/// Find the Intersection of Client and Server CCS's
CONV_FRAME::CodeSetId
TAO_Codeset_Manager::intersectionOf (CONV_FRAME::CodeSetComponent &cs_comp1,
                                     CONV_FRAME::CodeSetComponent &cs_comp2)
{
  for(CORBA::ULong index = 0L;
       index < cs_comp1.conversion_code_sets.length();
       ++index )
    {
      if (this->isElementOf(cs_comp1.conversion_code_sets[index], cs_comp2))
        {
          return cs_comp1.conversion_code_sets[index];
        }
    }

  return 0;
}

int
TAO_Codeset_Manager::isCompatible(CONV_FRAME::CodeSetId cs1,
                                  CONV_FRAME::CodeSetId cs2 )
{
  // Call the is_compatible method of ACE_Codeset_Registry
  return ACE_Codeset_Registry::is_compatible(cs1,cs2);
}

/// returns the TCS for Char / Wchar
CONV_FRAME::CodeSetId
TAO_Codeset_Manager::computeTCS (CONV_FRAME::CodeSetComponent &remote,
                                 CONV_FRAME::CodeSetComponent &local )
{
  if (remote.native_code_set == local.native_code_set)
    {
      return local.native_code_set;
    }

  if (this->isElementOf (remote.native_code_set, local))
    {
      return remote.native_code_set;
    }

  if (this->isElementOf (local.native_code_set, remote))
    {
      return local.native_code_set;
    }

  CONV_FRAME::CodeSetId tcs;

  if ((tcs = this->intersectionOf (remote, local)) == 0)
    {
      if (isCompatible (local.native_code_set, remote.native_code_set))
        {
          return remote.native_code_set;
        }
      else
        {
          ACE_DECLARE_NEW_CORBA_ENV;
          ACE_THROW_RETURN(CORBA::CODESET_INCOMPATIBLE (), 0);
        }
    }

  return tcs;
}

void
TAO_Codeset_Manager::set_ncs_c (CONV_FRAME::CodeSetId ncs)
{
  this->codeset_info_.ForCharData.native_code_set = ncs;
}

void
TAO_Codeset_Manager::set_ncs_w (CONV_FRAME::CodeSetId ncs,int mb)
{
  this->codeset_info_.ForWcharData.native_code_set = ncs;
  ACE_OutputCDR::wchar_maxbytes(mb);
}

int
TAO_Codeset_Manager::add_char_translator (const char *name)
{
  TAO_Codeset_Item *item = 0;
  ACE_NEW_RETURN (item, TAO_Codeset_Item (name), -1);

  if (this->char_factories_.insert (item) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                         ACE_TEXT ("add_char_translator, ")
                         ACE_TEXT ("Unable to add Codeset ")
                         ACE_TEXT ("factories for %s: %m\n"),
                         ACE_TEXT_CHAR_TO_TCHAR (name)),
                        -1);
    }

  return 0;
}

int
TAO_Codeset_Manager::add_wchar_translator (const char *name)
{
  TAO_Codeset_Item *item = 0;
  ACE_NEW_RETURN (item, TAO_Codeset_Item (name), -1);

  if (this->wchar_factories_.insert (item) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                         ACE_TEXT ("add_wchar_translator, ")
                         ACE_TEXT ("Unable to add Codeset ")
                         ACE_TEXT ("factories for %s: %m\n"),
                         ACE_TEXT_CHAR_TO_TCHAR (name)),
                        -1);
    }

  return 0;
}

void
TAO_Codeset_Manager::configure_codeset_factories()
{
  if (init_codeset_factories_i (this->char_factories_,
                                this->codeset_info_.ForCharData) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                  ACE_TEXT ("configure_codeset_factories, failed to init ")
                  ACE_TEXT ("char codeset factories\n")));
    }

  if (init_codeset_factories_i (this->wchar_factories_,
                                this->codeset_info_.ForWcharData) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                  ACE_TEXT ("configure_codeset_factories, failed to init ")
                  ACE_TEXT ("wchar codeset factories\n")));
    }

  if (this->codeset_info_.ForWcharData.native_code_set == 0)
    {
      ACE_OutputCDR::wchar_maxbytes(0); // disallow wchar when no ncs_w set
    }
}

/// Initialise the specific type codeset factories
int
TAO_Codeset_Manager::init_codeset_factories_i (
    TAO_CodesetFactorySet& factset,
    CONV_FRAME::CodeSetComponent& cs_comp
  )
{
  TAO_CodesetFactorySetItor end = factset.end ();
  TAO_CodesetFactorySetItor iter = factset.begin ();

  CONV_FRAME::CodeSetId ncs = cs_comp.native_code_set;
  cs_comp.conversion_code_sets.length (
    static_cast<CORBA::ULong> (factset.size()));
  CORBA::ULong index;

  for (index = 0; iter != end; ++iter)
    {
      const char *name = (*iter)->codeset_name ();
      TAO_Codeset_Translator_Factory *trans =
        ACE_Dynamic_Service<TAO_Codeset_Translator_Factory>::instance (name);

      if (trans == 0)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                      ACE_TEXT ("init_codeset_factories_i, Unable to load ")
                      ACE_TEXT ("CodeSet <%s>, %m\n"),
                      ACE_TEXT_CHAR_TO_TCHAR(name)));
          continue;
        }

      if (trans->ncs() == ncs)
        {
          (*iter)->factory (trans);
          cs_comp.conversion_code_sets[index++] = trans->tcs();

          if (TAO_debug_level > 2)
            {
              ACE_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("TAO (%P|%t) - Codeset_Manager::")
                          ACE_TEXT ("init_codeset_factories_i, Loaded CodeSet ")
                          ACE_TEXT ("<%s>, ncs = %08x tcs = %08x\n"),
                          ACE_TEXT_CHAR_TO_TCHAR(name),
                          trans->ncs(),trans->tcs()));
            }
        }
    }

  cs_comp.conversion_code_sets.length(index);
  return 0;
}

TAO_Codeset_Translator_Factory *
TAO_Codeset_Manager::get_char_trans (CONV_FRAME::CodeSetId tcs)
{
  if (this->codeset_info_.ForCharData.native_code_set == tcs)
    {
      if (tcs != ACE_CODESET_ID_ISO_UTF_16)
        return 0;
      else
        {
          if (this->utf16_bom_translator_ == 0)
            {
              ACE_NEW_RETURN (this->utf16_bom_translator_,
                              UTF16_BOM_Factory,
                              0);
              this->utf16_bom_translator_->init(0,0);
            }
          return this->utf16_bom_translator_;
        }
    }

  return this->get_translator_i (this->char_factories_,tcs);
}

TAO_Codeset_Translator_Factory *
TAO_Codeset_Manager::get_wchar_trans (CONV_FRAME::CodeSetId tcs)
{
  if (this->codeset_info_.ForWcharData.native_code_set == tcs)
    {
      return 0;
    }

  return this->get_translator_i (this->wchar_factories_,tcs);
}

TAO_Codeset_Translator_Factory *
TAO_Codeset_Manager::get_translator_i (TAO_CodesetFactorySet& factset,
                                       CONV_FRAME::CodeSetId tcs)
{
  const TAO_CodesetFactorySetItor end = factset.end ();

  for (TAO_CodesetFactorySetItor iter = factset.begin (); iter != end; ++iter)
    {
      TAO_Codeset_Translator_Factory *fact = (*iter)->factory ();

      if (fact && tcs == fact->tcs ())
        {
          return fact;
        }
    }

  return 0;
}


//---------------------------------------------------------------------
// Codeset Item
TAO_Codeset_Item::TAO_Codeset_Item (const char *name)
  :   name_ (0),
      factory_ (0)
{
  if (name)
    {
      name_ = ACE_OS::strdup(name);
    }
}

TAO_Codeset_Item::~TAO_Codeset_Item (void)
{
  ACE_OS::free (this->name_);
}

const char *
TAO_Codeset_Item::codeset_name (void)
{
  return this->name_;
}

TAO_Codeset_Translator_Factory *
TAO_Codeset_Item::factory (void)
{
  return this->factory_;
}

void
TAO_Codeset_Item::factory (TAO_Codeset_Translator_Factory *factory)
{
  this->factory_ = factory;
}

// End of Codeset Item Class



#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

template class ACE_Dynamic_Service<TAO_Codeset_Translator_Factory>;

template class ACE_Node<TAO_Codeset_Item*>;
template class ACE_Unbounded_Set<TAO_Codeset_Item*>;
template class ACE_Unbounded_Set_Iterator<TAO_Codeset_Item*>;

#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#pragma instantiate ACE_Dynamic_Service<TAO_Codeset_Translator_Factory>
#pragma instantiate ACE_Node<TAO_Codeset_Item*>
///
#pragma instantiate ACE_Unbounded_Set<TAO_Codeset_Item*>
#pragma instantiate ACE_Unbounded_Set_Iterator<TAO_Codeset_Item*>

#  endif