summaryrefslogtreecommitdiff
path: root/storage/connect/domdoc.cpp
blob: ba8eb829abde08bd3718bfcc5eefa9390550cff3 (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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/******************************************************************/
/*  Implementation of XML document processing using MS DOM        */
/*  Author: Olivier Bertrand                2007 - 2013           */          
/******************************************************************/
#include "my_global.h"
#include <stdio.h>
#if defined(__WIN__)
//#include <windows.h>
#if   defined(MSX2)
#import "msxml2.dll"  //Does not exist on Vista
#elif defined(MSX3)
#import "msxml3.dll"  //Causes error C2872: DOMNodeType: ambiguous symbol  ??
#elif defined(MSX4)
#import "msxml4.dll"  //Causes error C2872: DOMNodeType: ambiguous symbol  ??
#elif defined(MSX6)
#pragma warning(suppress : 4192)
#import "msxml6.dll"  //Causes error C2872: DOMNodeType: ambiguous symbol  ??
#else       // MSX4
#error MSX? is not defined
#endif     // MSX
using namespace MSXML2;
#else
#error This is a Windows implementation only
#endif

#define NODE_TYPE_LIST

#include "global.h"
#include "plgdbsem.h"
#include "xobject.h"
#include "domdoc.h"

inline bool TestHr(PGLOBAL g, HRESULT hr)
  {
  if FAILED(hr) {
    sprintf(g->Message, "%s, hr=%d", MSG(COM_ERROR), hr);
    return true;
  } else
    return false;

  } // end of TestHr

/******************************************************************/
/*  Return a DOMDOC as a XMLDOC.                                  */
/******************************************************************/
PXDOC GetDomDoc(PGLOBAL g, char *nsl, char *nsdf, 
                                      char *enc, PFBLOCK fp)
  {
  return (PXDOC) new(g) DOMDOC(nsl, nsdf, enc, fp);
  }  // end of GetDomDoc

/***********************************************************************/
/*  Close a loaded DOM XML file.                                       */
/***********************************************************************/
void CloseXMLFile(PGLOBAL g, PFBLOCK fp, bool all)
  {
  PXBLOCK xp = (PXBLOCK)fp;

  if (xp && xp->Count > 1 && !all) {
    xp->Count--;
  } else if (xp && xp->Count > 0) {
    try {
      if (xp->Docp)
        xp->Docp->Release();

    } catch(_com_error e)  {
			char *p = _com_util::ConvertBSTRToString(e.Description());
      sprintf(g->Message, "%s %s", MSG(COM_ERROR), p);
			delete[] p;
    } catch(...) {}

    CoUninitialize();
    xp->Count = 0;
  }  // endif

  } // end of CloseXMLFile

/* ------------------------ class DOMDOC ------------------------ */

/******************************************************************/
/*  DOMDOC constructor.                                           */
/******************************************************************/
DOMDOC::DOMDOC(char *nsl, char *nsdf, char *enc, PFBLOCK fp)
      : XMLDOCUMENT(nsl, nsdf, enc)
  {
  assert (!fp || fp->Type == TYPE_FB_XML);
  Docp = (fp) ? ((PXBLOCK)fp)->Docp : NULL;
  Nlist = NULL;
  Hr = 0;
  } // end of DOMDOC constructor

/******************************************************************/
/*  Initialize XML parser and check library compatibility.        */
/******************************************************************/
bool DOMDOC::Initialize(PGLOBAL g, PCSZ entry, bool zipped)
{
	if (zipped && InitZip(g, entry))
		return true;

	if (TestHr(g, CoInitialize(NULL)))
    return true;

  if (TestHr(g, Docp.CreateInstance("msxml2.domdocument")))
    return true;

  return MakeNSlist(g);
} // end of Initialize

/******************************************************************/
/* Parse the XML file and construct node tree in memory.          */
/******************************************************************/
bool DOMDOC::ParseFile(PGLOBAL g, char *fn)
{
	bool b;

  Docp->async = false;

	if (zip) {
		// Parse an in memory document
		char *xdoc = GetMemDoc(g, fn);

		// This is not equivalent to load for UTF8 characters
		// It is why get node content is not the same
  	b = (xdoc) ? (bool)Docp->loadXML((_bstr_t)xdoc) : false;
	} else
		// Load the document
		b = (bool)Docp->load((_bstr_t)fn);

	if (!b)
    return true;

  return false;
} // end of ParseFile

/******************************************************************/
/* Create or reuse an Xblock for this document.                   */
/******************************************************************/
PFBLOCK DOMDOC::LinkXblock(PGLOBAL g, MODE m, int rc, char *fn)
  {
  PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
  PXBLOCK xp = (PXBLOCK)PlugSubAlloc(g, NULL, sizeof(XBLOCK));

  memset(xp, 0, sizeof(XBLOCK));
  xp->Next = (PXBLOCK)dup->Openlist;
  dup->Openlist = (PFBLOCK)xp;
  xp->Type = TYPE_FB_XML;
  xp->Fname = (LPCSTR)PlugSubAlloc(g, NULL, strlen(fn) + 1);
  strcpy((char*)xp->Fname, fn);
  xp->Count = 1;
  xp->Length = (m == MODE_READ) ? 1 : 0;
  xp->Docp = Docp;
  xp->Retcode = rc;

  // Return xp as a fp 
  return (PFBLOCK)xp;
  } // end of LinkXblock

/******************************************************************/
/* Create the XML node.                                           */
/******************************************************************/
bool DOMDOC::NewDoc(PGLOBAL g, PCSZ ver)
  {
  char buf[64];
  MSXML2::IXMLDOMProcessingInstructionPtr pip;

  sprintf(buf, "version=\"%s\" encoding=\"%s\"", ver, Encoding);
  pip = Docp->createProcessingInstruction("xml", buf);
  return(TestHr(g, Docp->appendChild(pip)));
  } // end of NewDoc

/******************************************************************/
/* Add a comment to the document node.                            */
/******************************************************************/
void DOMDOC::AddComment(PGLOBAL g, char *com)
  {
  TestHr(g, Docp->appendChild(Docp->createComment(com)));
  } // end of AddComment

/******************************************************************/
/* Return the node class of the root of the document.             */
/******************************************************************/
PXNODE DOMDOC::GetRoot(PGLOBAL g)
  {
  MSXML2::IXMLDOMElementPtr root = Docp->documentElement;

  if (root == NULL)
    return NULL;

  return new(g) DOMNODE(this, root); 
  } // end of GetRoot

/******************************************************************/
/* Create a new root element and return its class node.           */
/******************************************************************/
PXNODE DOMDOC::NewRoot(PGLOBAL g, char *name)
  {
  MSXML2::IXMLDOMElementPtr ep = Docp->createElement(name);

  if (ep == NULL || TestHr(g, Docp->appendChild(ep)))
    return NULL;

  return new(g) DOMNODE(this, ep); 
  } // end of NewRoot

/******************************************************************/
/* Return a void DOMNODE node class.                              */
/******************************************************************/
PXNODE DOMDOC::NewPnode(PGLOBAL g, char *name)
  {
  MSXML2::IXMLDOMElementPtr root = NULL;

  if (name)
    if ((root = Docp->createElement(name)) == NULL)
      return NULL;

  return new(g) DOMNODE(this, root);
  } // end of NewPnode

/******************************************************************/
/* Return a void DOMATTR node class.                              */
/******************************************************************/
PXATTR DOMDOC::NewPattr(PGLOBAL g)
  {
  return new(g) DOMATTR(this, NULL);
  } // end of NewPattr

/******************************************************************/
/* Return a void DOMATTR node class.                              */
/******************************************************************/
PXLIST DOMDOC::NewPlist(PGLOBAL g)
  {
  return new(g) DOMNODELIST(this, NULL);
  } // end of NewPlist

/******************************************************************/
/* Dump the node tree to a new XML file.                          */
/******************************************************************/
int DOMDOC::DumpDoc(PGLOBAL g, char *ofn)
  {
  int rc = 0;

  try {
    Docp->save(ofn);
  } catch(_com_error e)  {
    sprintf(g->Message, "%s: %s", MSG(COM_ERROR), 
            _com_util::ConvertBSTRToString(e.Description()));
    rc = -1;
  }  catch(...) {}

  return rc;
  } // end of Dump

/******************************************************************/
/* Free the document, cleanup the XML library, and                */
/* debug memory for regression tests.                             */
/******************************************************************/
void DOMDOC::CloseDoc(PGLOBAL g, PFBLOCK xp)
  {
  CloseXMLFile(g, xp, false);
	CloseZip();
  } // end of Close

/* ----------------------- class DOMNODE ------------------------ */

/******************************************************************/
/*  DOMNODE constructor.                                          */
/******************************************************************/
DOMNODE::DOMNODE(PXDOC dp, MSXML2::IXMLDOMNodePtr np) : XMLNODE(dp)
  {
  Docp = ((PDOMDOC)dp)->Docp;
  Nodep = np;
  Ws = NULL;
  Len = 0;
	Zip = (bool)dp->zip;
  } // end of DOMNODE constructor

/******************************************************************/
/*  Return the node name.                                         */
/******************************************************************/
char *DOMNODE::GetName(PGLOBAL g)
  {
  if (!WideCharToMultiByte(CP_ACP, 0, Nodep->nodeName, -1,
                           Name, sizeof(Name), NULL, NULL)) {
    strcpy(g->Message, MSG(NAME_CONV_ERR));
    return NULL;
    } // endif

  return Name;
  }  // end of GetName

/******************************************************************/
/*  Return the node class of next sibling of the node.            */
/******************************************************************/
PXNODE DOMNODE::GetNext(PGLOBAL g)
  {
  if (Nodep->nextSibling == NULL)
    Next = NULL;
  else // if (!Next)
    Next = new(g) DOMNODE(Doc, Nodep->nextSibling);

  return Next;
  } // end of GetNext

/******************************************************************/
/*  Return the node class of first children of the node.          */
/******************************************************************/
PXNODE DOMNODE::GetChild(PGLOBAL g)
  {
  if (Nodep->firstChild == NULL)
    Children = NULL;
  else // if (!Children)
    Children = new(g) DOMNODE(Doc, Nodep->firstChild);

  return Children;
  } // end of GetChild

/******************************************************************/
/*  Return the content of a node and subnodes.                    */
/******************************************************************/
RCODE DOMNODE::GetContent(PGLOBAL g, char *buf, int len)
  {
  RCODE rc = RC_OK;

  // Nodep can be null for a missing HTML table column
  if (Nodep) {
		if (Zip) {
			strcpy(buf, Nodep->text);
		} else if (!WideCharToMultiByte(CP_UTF8, 0, Nodep->text, -1,
                             buf, len, NULL, NULL)) {
      DWORD lsr = GetLastError();

      switch (lsr) {
        case 0:
        case ERROR_INSUFFICIENT_BUFFER:      // 122L
          sprintf(g->Message, "Truncated %s content", GetName(g));
          rc = RC_INFO;
          break;
        case ERROR_NO_UNICODE_TRANSLATION:   // 1113L
          sprintf(g->Message, "Invalid character(s) in %s content",
                              GetName(g));
          rc = RC_INFO;
          break;
        default:
          sprintf(g->Message, "System error getting %s content",
                              GetName(g));
          rc = RC_FX;
          break;
        } // endswitch

      } // endif

  } else
    *buf = '\0';

  return rc;
  } // end of GetContent

/******************************************************************/
/*  Set the text content of an attribute.                         */
/******************************************************************/
bool DOMNODE::SetContent(PGLOBAL g, char *txtp, int len)
  {
  bool rc;
  BSTR val;

  if (len > Len || !Ws) {
    Ws = (WCHAR*)PlugSubAlloc(g, NULL, (len + 1) * 2);
    Len = len;
    } // endif len

  if (!MultiByteToWideChar(CP_UTF8, 0, txtp, strlen(txtp) + 1,
                                       Ws, Len + 1)) {
    sprintf(g->Message, MSG(WS_CONV_ERR), txtp);
    return true;
    } // endif

  val = SysAllocString(Ws);
  rc = TestHr(g, Nodep->put_text(val));
  SysFreeString(val);
  return rc;
  } // end of SetContent

/******************************************************************/
/*  Return a clone of this node.                                  */
/******************************************************************/
PXNODE DOMNODE::Clone(PGLOBAL g, PXNODE np)
  {
  if (np) {
    ((PDOMNODE)np)->Nodep = Nodep;
    return np;
  } else
    return new(g) DOMNODE(Doc, Nodep);

  } // end of Clone

/******************************************************************/
/*  Return the list of all or matching children that are elements.*/
/******************************************************************/
PXLIST DOMNODE::GetChildElements(PGLOBAL g, char *xp, PXLIST lp)
  {
  MSXML2::IXMLDOMNodeListPtr dnlp;

  if (xp) {
    if (Nodep->nodeType == MSXML2::NODE_ELEMENT) {
      MSXML2::IXMLDOMElementPtr ep = Nodep;
      dnlp = ep->getElementsByTagName(xp);
    } else
      return NULL;

  } else
    dnlp = Nodep->childNodes;

  if (lp) {
    ((PDOMLIST)lp)->Listp = dnlp;
    return lp;
  } else
    return new(g) DOMNODELIST(Doc, dnlp);

  } // end of GetChildElements

/******************************************************************/
/*  Return the list of nodes verifying the passed Xapth.          */
/******************************************************************/
PXLIST DOMNODE::SelectNodes(PGLOBAL g, char *xp, PXLIST lp)
  {
  MSXML2::IXMLDOMNodeListPtr dnlp = Nodep->selectNodes(xp);

  if (lp) {
    ((PDOMLIST)lp)->Listp = dnlp;
    return lp;
  } else
    return new(g) DOMNODELIST(Doc, dnlp);

  } // end of SelectNodes

/******************************************************************/
/*  Return the first node verifying the passed Xapth.             */
/******************************************************************/
PXNODE DOMNODE::SelectSingleNode(PGLOBAL g, char *xp, PXNODE np)
  {
  try {
    MSXML2::IXMLDOMNodePtr dnp = Nodep->selectSingleNode(xp);

    if (dnp) {
      if (np) {
        ((PDOMNODE)np)->Nodep = dnp;
        return np;
      } else
        return new(g) DOMNODE(Doc, dnp);

      } // endif dnp

  } catch(_com_error e) {
    sprintf(g->Message, "%s: %s", MSG(COM_ERROR), 
            _com_util::ConvertBSTRToString(e.Description()));
  } catch(...) {}

  return NULL;
  } // end of SelectSingleNode

/******************************************************************/
/*  Return the node attribute with the specified name.            */
/******************************************************************/
PXATTR DOMNODE::GetAttribute(PGLOBAL g, char *name, PXATTR ap)
  {
  MSXML2::IXMLDOMElementPtr ep;
  MSXML2::IXMLDOMNamedNodeMapPtr nmp;
  MSXML2::IXMLDOMAttributePtr atp;

  if (name) {
    ep = Nodep;
    atp = ep->getAttributeNode(name);
    nmp = NULL;
  } else {
    nmp = Nodep->Getattributes();
    atp = nmp->Getitem(0);
  } // endif name

  if (atp) {
    if (ap) {
      ((PDOMATTR)ap)->Atrp = atp;
      ((PDOMATTR)ap)->Nmp = nmp;
      ((PDOMATTR)ap)->K = 0;
      return ap;
    } else
      return new(g) DOMATTR(Doc, atp, nmp);

  } else
    return NULL;

  } // end of GetAttribute

/******************************************************************/
/*  Add a new element child node to this node and return it.      */
/******************************************************************/
PXNODE DOMNODE::AddChildNode(PGLOBAL g, PCSZ name, PXNODE np)
  {
  const char *p, *pn;
//  char *p, *pn, *epf, *pf = NULL;
  MSXML2::IXMLDOMNodePtr ep;
//  _bstr_t   uri((wchar_t*)NULL);

#if 0
  // Is a prefix specified ?
  if ((p = strchr(name, ':'))) {
    pf = BufAlloc(g, name, p - name);

    // Is it the pseudo default prefix
    if (Doc->DefNs && !strcmp(pf, Doc->DefNs)) {
      name = p + 1;              // Suppress it from name
      pf = NULL;                // No real prefix
      } // endif DefNs

    } // endif p

  // Look for matching namespace URI in context
  for (ep = Nodep; ep; ep = ep->parentNode) {
    epf = (_bstr_t)ep->prefix;

    if ((!pf && !epf) || (pf && epf && !strcmp(pf, epf))) {
      uri = Nodep->namespaceURI;
      break;
      } // endif

    } // endfor ep

  if ((wchar_t*)uri == NULL) {
    if (!pf)
      pf = Doc->DefNs;

    // Look for the namespace URI corresponding to this node
    if (pf)
      for (PNS nsp = Doc->Namespaces; nsp; nsp = nsp->Next)
        if (!strcmp(pf, nsp->Prefix)) {
          uri = nsp->Uri;
          break;
          } // endfor nsp

    } // endif pns
#endif // 0

  // If name has the format m[n] only m is taken as node name
  if ((p = strchr(name, '[')))
    pn = BufAlloc(g, name, (int)(p - name));
  else
    pn = name;

  // Construct the element node with eventual namespace
//  ep = Docp->createNode(_variant_t("Element"), pn, uri);
  ep = Docp->createElement(pn);

  _bstr_t pfx = ep->prefix;
  _bstr_t uri = ep->namespaceURI;

  if (ep == NULL || TestHr(g, Nodep->appendChild(ep)))
    return NULL;

  if (np)
    ((PDOMNODE)np)->Nodep = ep;
  else
    np = new(g) DOMNODE(Doc, ep);

  return NewChild(np);
  } // end of AddChildNode

/******************************************************************/
/*  Add a new property to this node and return it.                */
/******************************************************************/
PXATTR DOMNODE::AddProperty(PGLOBAL g, char *name, PXATTR ap)
  {
  MSXML2::IXMLDOMAttributePtr atp = Docp->createAttribute(name);

  if (atp) {
    MSXML2::IXMLDOMElementPtr ep = Nodep;
    ep->setAttributeNode(atp);

    if (ap) {
      ((PDOMATTR)ap)->Atrp = atp;
      return ap;
    } else
      return new(g) DOMATTR(Doc, atp);

  } else
    return NULL;

  } // end of AddProperty

/******************************************************************/
/*  Add a new text node to this node.                             */
/******************************************************************/
void DOMNODE::AddText(PGLOBAL g, PCSZ txtp)
  {
  MSXML2::IXMLDOMTextPtr tp= Docp->createTextNode((_bstr_t)txtp);

  if (tp != NULL)
    TestHr(g, Nodep->appendChild(tp));

  } // end of AddText

/******************************************************************/
/*  Remove a child node from this node.                           */
/******************************************************************/
void DOMNODE::DeleteChild(PGLOBAL g, PXNODE dnp)
  {
  TestHr(g, Nodep->removeChild(((PDOMNODE)dnp)->Nodep));
//  ((PDOMNODE)dnp)->Nodep->Release();  bad idea, causes a crash
  Delete(dnp);
  } // end of DeleteChild

/* --------------------- class DOMNODELIST ---------------------- */

/******************************************************************/
/*  DOMNODELIST constructor.                                      */
/******************************************************************/
DOMNODELIST::DOMNODELIST(PXDOC dp, MSXML2::IXMLDOMNodeListPtr lp)
           : XMLNODELIST(dp)
  {
  Listp = lp;
  } // end of DOMNODELIST constructor

/******************************************************************/
/*  Return the nth element of the list.                           */
/******************************************************************/
PXNODE DOMNODELIST::GetItem(PGLOBAL g, int n, PXNODE np)
  {
  if (Listp == NULL || Listp->length <= n)
    return NULL;

  if (np) {
    ((PDOMNODE)np)->Nodep = Listp->item[n];
    return np;
  } else
    return new(g) DOMNODE(Doc, Listp->item[n]);

  }  // end of GetItem

/******************************************************************/
/*  Reset the pointer on the deleted item.                        */
/******************************************************************/
bool DOMNODELIST::DropItem(PGLOBAL g, int n)
{
	if (Listp == NULL || Listp->length < n)
		return true;

//Listp->item[n] = NULL;  La propriété n'a pas de méthode 'set'
  return false;
}  // end of DeleteItem

/* ----------------------- class DOMATTR ------------------------ */

/******************************************************************/
/*  DOMATTR constructor.                                          */
/******************************************************************/
DOMATTR::DOMATTR(PXDOC dp, MSXML2::IXMLDOMAttributePtr ap,
                           MSXML2::IXMLDOMNamedNodeMapPtr nmp)
        : XMLATTRIBUTE(dp)
  {
  Atrp = ap;
  Nmp = nmp;
  Ws = NULL;
  Len = 0;
  K = 0;
  }  // end of DOMATTR constructor

/******************************************************************/
/*  Return the attribute name.                                    */
/******************************************************************/
char *DOMATTR::GetName(PGLOBAL g)
  {
  if (!WideCharToMultiByte(CP_ACP, 0, Atrp->nodeName, -1,
                           Name, sizeof(Name), NULL, NULL)) {
    strcpy(g->Message, MSG(NAME_CONV_ERR));
    return NULL;
    } // endif

  return Name;
  }  // end of GetName

/******************************************************************/
/*  Return the next attribute node.                               */
/*  This funtion is implemented as needed by XMLColumns.          */
/******************************************************************/
PXATTR DOMATTR::GetNext(PGLOBAL g)
  {
  if (!Nmp)
    return NULL;

  if (++K >= Nmp->Getlength()) {
    Nmp->reset();
    Nmp = NULL;
    K = 0;
    return NULL;
    } // endif K

  Atrp = Nmp->Getitem(K);
  return this;
  } // end of GetNext

/******************************************************************/
/*  Return the content of a node and subnodes.                    */
/******************************************************************/
RCODE DOMATTR::GetText(PGLOBAL g, char *buf, int len)
  {
  RCODE rc = RC_OK;

  if (!WideCharToMultiByte(CP_UTF8, 0, Atrp->text, -1,
                           buf, len, NULL, NULL)) {
    DWORD lsr = GetLastError();

    switch (lsr) {
      case 0:
      case ERROR_INSUFFICIENT_BUFFER:      // 122L
        sprintf(g->Message, "Truncated %s content", GetName(g));
        rc = RC_INFO;
        break;
      case ERROR_NO_UNICODE_TRANSLATION:   // 1113L
        sprintf(g->Message, "Invalid character(s) in %s content",
                            GetName(g));
        rc = RC_INFO;
        break;
      default:
        sprintf(g->Message, "System error getting %s content",
                            GetName(g));
        rc = RC_FX;
        break;
      } // endswitch

    } // endif

  return rc;
  } // end of GetText

/******************************************************************/
/*  Set the text content of an attribute.                         */
/******************************************************************/
bool DOMATTR::SetText(PGLOBAL g, char *txtp, int len)
  {
  bool rc;
  BSTR val;

  if (len > Len || !Ws) {
    Ws = (WCHAR*)PlugSubAlloc(g, NULL, (len + 1) * 2);
    Len = len;
    } // endif len

  if (!MultiByteToWideChar(CP_UTF8, 0, txtp, strlen(txtp) + 1,
                                       Ws, Len + 1)) {
    sprintf(g->Message, MSG(WS_CONV_ERR), txtp);
    return true;
    } // endif

  val = SysAllocString(Ws);
  rc = TestHr(g, Atrp->put_text(val));
  SysFreeString(val);
  return rc;
  } // end of SetText