-
Notifications
You must be signed in to change notification settings - Fork 1
/
docObj.c
1955 lines (1817 loc) · 64.8 KB
/
docObj.c
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* docObj.c --
*
* This module manages libxml2 xmlDocPtr Tcl objects.
*
* Copyright (c) 2005-2009 by Explain.
* http://www.explain.com.au/
* Copyright (c) 2003-2004 Zveno Pty Ltd
* http://www.zveno.com/
*
* See the file "LICENSE" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* $Id: docObj.c,v 1.8.2.1 2005/12/28 06:49:51 balls Exp $
*/
#include <tclxml-libxml2/tclxml-libxml2.h>
#include <libxml/xmlerror.h>
#include <string.h>
#define TCL_DOES_STUBS \
(TCL_MAJOR_VERSION > 8 || TCL_MAJOR_VERSION == 8 && (TCL_MINOR_VERSION > 1 || \
(TCL_MINOR_VERSION == 1 && TCL_RELEASE_LEVEL == TCL_FINAL_RELEASE)))
#undef TCL_STORAGE_CLASS
#define TCL_STORAGE_CLASS DLLEXPORT
/*
* Basic list for tracking Tcl_Obj's for a document.
*/
typedef struct ObjList {
Tcl_Obj *objPtr;
struct ObjList *next;
} ObjList;
/*
* Prototypes for procedures defined later in this file:
*/
static void DestroyTclDoc _ANSI_ARGS_((TclXML_libxml2_Document *tDocPtr));
Tcl_FreeInternalRepProc TclXMLlibxml2_DocFree;
Tcl_DupInternalRepProc TclXMLlibxml2_DocDup;
Tcl_UpdateStringProc TclXMLlibxml2_DocUpdate;
Tcl_SetFromAnyProc TclXMLlibxml2_DocSetFromAny;
Tcl_ObjType TclXMLlibxml2_DocObjType = {
"libxml2-doc",
TclXMLlibxml2_DocFree,
TclXMLlibxml2_DocDup,
TclXMLlibxml2_DocUpdate,
TclXMLlibxml2_DocSetFromAny
};
typedef struct ThreadSpecificData {
int initialized;
/*
* Hash table for mapping string rep to doc structure.
*/
Tcl_HashTable *documents;
int docCntr;
/*
* Hash table for tracking doc objects.
*/
Tcl_HashTable *docByPtr;
/*
* Structured error handling
*/
TclXML_ErrorInfo *errorInfoPtr;
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* libxml2 is mostly thread-safe, but just-in-case use a mutex to control access.
*/
TCL_DECLARE_MUTEX(libxml2)
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_InitDocObj --
*
* Initialise this module.
*
* Results:
* Returns success code
*
* Side effects:
* Memory may be allocated
*
*----------------------------------------------------------------------------
*/
int
TclXML_libxml2_InitDocObj(interp)
Tcl_Interp *interp;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
tsdPtr->initialized = 1;
tsdPtr->documents = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(tsdPtr->documents, TCL_STRING_KEYS);
tsdPtr->docByPtr = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(tsdPtr->docByPtr, TCL_ONE_WORD_KEYS);
tsdPtr->docCntr = 0;
/*
* Setup an error handler that stores structured error info
*/
tsdPtr->errorInfoPtr = (TclXML_ErrorInfo *) Tcl_Alloc(sizeof(TclXML_ErrorInfo));
tsdPtr->errorInfoPtr->interp = interp;
tsdPtr->errorInfoPtr->listPtr = NULL;
tsdPtr->errorInfoPtr->nodeHandlerProc = NULL;
xmlSetStructuredErrorFunc((void *) tsdPtr->errorInfoPtr, TclXML_libxml2_ErrorHandler);
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_NewDocObj --
*
* Creates a new xmlDocPtr and wraps it in a Tcl_Obj.
*
* Results:
* Returns a *TclObj
*
* Side effects:
* Objects allocated.
*
*----------------------------------------------------------------------------
*/
Tcl_Obj *
TclXML_libxml2_NewDocObj(interp)
Tcl_Interp *interp;
{
xmlDocPtr new;
Tcl_MutexLock(&libxml2);
new = xmlNewDoc((const xmlChar *) "1.0");
Tcl_MutexUnlock(&libxml2);
if (!new) {
Tcl_SetResult(interp, "unable to create document", NULL);
return NULL;
}
return TclXML_libxml2_CreateObjFromDoc(new);
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_CreateObjFromDoc --
*
* Create a Tcl_Obj to wrap a xmlDocPtr.
*
* Results:
* Returns Tcl_Obj*.
*
* Side effects:
* Allocates object.
*
*----------------------------------------------------------------------------
*/
Tcl_Obj *
ImportDoc (docPtr, tDocPtrPtr)
xmlDocPtr docPtr;
TclXML_libxml2_Document **tDocPtrPtr;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
TclXML_libxml2_Document *tDocPtr;
Tcl_HashEntry *entryPtr;
Tcl_Obj *objPtr;
ObjList *listPtr;
/*
* This xmlDocPtr may already have been wrapped by a Tcl object.
* If so, return an already existing wrapper.
* If not, create a new wrapper.
*/
entryPtr = Tcl_FindHashEntry(tsdPtr->docByPtr, (ClientData) docPtr);
if (entryPtr) {
tDocPtr = (TclXML_libxml2_Document *) Tcl_GetHashValue(entryPtr);
if (tDocPtr->objs) {
/* The first object is sufficient */
listPtr = (ObjList *) tDocPtr->objs;
objPtr = listPtr->objPtr;
} else {
/* Create a new Tcl_Obj to refer to existing structure */
objPtr = Tcl_NewObj();
listPtr = (ObjList *) Tcl_Alloc(sizeof(ObjList));
listPtr->objPtr = objPtr;
listPtr->next = NULL;
tDocPtr->objs = (void *) listPtr;
objPtr->length = strlen(tDocPtr->token);
objPtr->bytes = Tcl_Alloc(objPtr->length + 1);
strcpy(objPtr->bytes, tDocPtr->token);
objPtr->internalRep.twoPtrValue.ptr1 = (void *) tDocPtr;
objPtr->internalRep.twoPtrValue.ptr2 = NULL;
objPtr->typePtr = &TclXMLlibxml2_DocObjType;
}
} else {
int new;
objPtr = Tcl_NewObj();
tDocPtr = (TclXML_libxml2_Document *) Tcl_Alloc(sizeof(TclXML_libxml2_Document));
tDocPtr->docPtr = docPtr;
tDocPtr->token = Tcl_Alloc(20);
sprintf(tDocPtr->token, "doc%d", tsdPtr->docCntr++);
tDocPtr->keep = TCLXML_LIBXML2_DOCUMENT_IMPLICIT;
tDocPtr->dom = NULL;
tDocPtr->domfree = NULL;
tDocPtr->apphook = NULL;
tDocPtr->appfree = NULL;
listPtr = (ObjList *) Tcl_Alloc(sizeof(ObjList));
listPtr->objPtr = objPtr;
listPtr->next = NULL;
tDocPtr->objs = (void *) listPtr;
entryPtr = Tcl_CreateHashEntry(tsdPtr->documents, tDocPtr->token, &new);
Tcl_SetHashValue(entryPtr, (ClientData) tDocPtr);
entryPtr = Tcl_CreateHashEntry(tsdPtr->docByPtr, (ClientData) docPtr, &new);
Tcl_SetHashValue(entryPtr, (ClientData) tDocPtr);
objPtr->length = strlen(tDocPtr->token);
objPtr->bytes = Tcl_Alloc(objPtr->length + 1);
strcpy(objPtr->bytes, tDocPtr->token);
objPtr->internalRep.twoPtrValue.ptr1 = (void *) tDocPtr;
objPtr->internalRep.twoPtrValue.ptr2 = NULL;
objPtr->typePtr = &TclXMLlibxml2_DocObjType;
}
/* Bug fix #1032660. David Welton. */
Tcl_IncrRefCount(objPtr);
if (tDocPtrPtr != NULL) {
*tDocPtrPtr = tDocPtr;
}
return objPtr;
}
Tcl_Obj *
TclXML_libxml2_CreateObjFromDoc (docPtr)
xmlDocPtr docPtr;
{
return ImportDoc(docPtr, NULL);
}
TclXML_libxml2_Document *
TclXML_libxml2_NewDoc (docPtr)
xmlDocPtr docPtr;
{
Tcl_Obj *objPtr;
TclXML_libxml2_Document *tDocPtr;
objPtr = ImportDoc(docPtr, &tDocPtr);
return tDocPtr;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_GetDocFromObj --
*
* Retrieve the xmlDocPtr from a Tcl object.
*
* Results:
* Returns success code.
*
* Side effects:
* May set internal rep of object.
*
*----------------------------------------------------------------------------
*/
int
TclXML_libxml2_GetDocFromObj (interp, objPtr, docPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
xmlDocPtr *docPtr;
{
TclXML_libxml2_Document *tDocPtr;
if (TclXML_libxml2_GetTclDocFromObj(interp, objPtr, &tDocPtr) != TCL_OK) {
return TCL_ERROR;
}
*docPtr = tDocPtr->docPtr;
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_GetTclDocFromNode --
*
* Retrieve a pointer to the TclXML Doc structure from a xmlNodePtr.
*
* Results:
* Returns success code.
*
* Side effects:
* Sets pointer
*
*----------------------------------------------------------------------------
*/
int
TclXML_libxml2_GetTclDocFromNode (interp, nodePtr, tDocPtrPtr)
Tcl_Interp *interp;
xmlNodePtr nodePtr;
TclXML_libxml2_Document **tDocPtrPtr;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
Tcl_HashEntry *entryPtr;
entryPtr = Tcl_FindHashEntry(tsdPtr->docByPtr, (ClientData) nodePtr->doc);
if (!entryPtr) {
/* We haven't seen this doc before - probably a RVT */
*tDocPtrPtr = TclXML_libxml2_NewDoc(nodePtr->doc);
return TCL_OK;
}
*tDocPtrPtr = (TclXML_libxml2_Document *) Tcl_GetHashValue(entryPtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_GetTclDocFromObj --
*
* Retrieve the TclXML_libxml2_Document from a Tcl object.
*
* Results:
* Returns success code.
*
* Side effects:
* May set internal rep of object.
*
*----------------------------------------------------------------------------
*/
int
TclXML_libxml2_GetTclDocFromObj (interp, objPtr, tDocPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
TclXML_libxml2_Document **tDocPtr;
{
if (objPtr->typePtr == &TclXMLlibxml2_DocObjType) {
*tDocPtr = (TclXML_libxml2_Document *) objPtr->internalRep.twoPtrValue.ptr1;
} else if (TclXMLlibxml2_DocSetFromAny(interp, objPtr) == TCL_OK) {
*tDocPtr = (TclXML_libxml2_Document *) objPtr->internalRep.twoPtrValue.ptr1;
} else {
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_GetTclDocFromDoc --
*
* Retrieve the TclXML_libxml2_Document from a xmlDocPtr.
*
* Results:
* Returns success code.
*
* Side effects:
* Sets pointer.
*
*----------------------------------------------------------------------------
*/
int
TclXML_libxml2_GetTclDocFromDoc (interp, docPtr, tDocPtrPtr)
Tcl_Interp *interp;
xmlDocPtr docPtr;
TclXML_libxml2_Document **tDocPtrPtr;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
Tcl_HashEntry *entryPtr;
entryPtr = Tcl_FindHashEntry(tsdPtr->docByPtr, (ClientData) docPtr);
if (!entryPtr) {
*tDocPtrPtr = NULL;
Tcl_SetResult(interp, "document not known", NULL);
return TCL_ERROR;
}
*tDocPtrPtr = (TclXML_libxml2_Document *) Tcl_GetHashValue(entryPtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_DestroyDocument --
*
* Manage destruction of a document.
* The trick here is to make sure that all Tcl_Obj's
* that reference this document have their internal rep
* invalidated.
*
* Results:
* None.
*
* Side effects:
* Memory deallocated, object internal reps changed.
*
*----------------------------------------------------------------------------
*/
void
TclXML_libxml2_DestroyDocument (tDocPtr)
TclXML_libxml2_Document *tDocPtr;
{
ObjList *listPtr = (ObjList *) tDocPtr->objs;
ObjList *next;
/*
* Invalidate the internal representation of all Tcl_Obj's
* that refer to this document.
*/
while (listPtr) {
next = listPtr->next;
TclXMLlibxml2_DocFree(listPtr->objPtr);
listPtr = next;
}
if (tDocPtr->keep == TCLXML_LIBXML2_DOCUMENT_KEEP) {
DestroyTclDoc(tDocPtr);
}
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_DocKeep --
*
* Changes how the document's destruction is handled.
*
* Results:
* None.
*
* Side effects:
* Changes document configuration.
*
*----------------------------------------------------------------------------
*/
void
TclXML_libxml2_DocKeep(objPtr, keep)
Tcl_Obj *objPtr;
TclXML_libxml2_DocumentHandling keep;
{
TclXML_libxml2_Document *tDocPtr;
if (TclXML_libxml2_GetTclDocFromObj(NULL, objPtr, &tDocPtr) != TCL_OK) {
return;
}
tDocPtr->keep = keep;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_GetBaseURIFromDoc --
*
* Returns the base URI of a document.
*
* Results:
* Returns Tcl object.
*
*----------------------------------------------------------------------------
*/
Tcl_Obj *
TclXML_libxml2_GetBaseURIFromDoc(docPtr)
xmlDocPtr docPtr;
{
return Tcl_NewStringObj((char *) docPtr->URL, -1);
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_SetBaseURI --
*
* Sets the base URI of a document.
*
* Results:
* Returns success code.
*
* Side effects:
* Changes the xml document's URL.
*
*----------------------------------------------------------------------------
*/
int
TclXML_libxml2_SetBaseURI(interp, docPtr, uriObj)
Tcl_Interp *interp;
xmlDocPtr docPtr;
Tcl_Obj *uriObj;
{
char *url;
int len;
if (docPtr == NULL) {
Tcl_SetResult(interp, "no document", NULL);
return TCL_ERROR;
}
if (uriObj == NULL) {
Tcl_SetResult(interp, "no URL", NULL);
return TCL_ERROR;
}
if (docPtr->URL) {
xmlFree((xmlChar *) docPtr->URL);
}
url = Tcl_GetStringFromObj(uriObj, &len);
docPtr->URL = xmlCharStrndup(url, len);
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXMLlibxml2_DocSetFromAny --
*
* Finds the xmlDocPtr wrapper for a Tcl object.
*
* Results:
* Returns success code.
*
* Side effects:
* Changes the Tcl_Obj's internal rep.
*
*----------------------------------------------------------------------------
*/
int
TclXMLlibxml2_DocSetFromAny(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
Tcl_HashEntry *entryPtr;
TclXML_libxml2_Document *tDocPtr;
ObjList *listPtr;
entryPtr = Tcl_FindHashEntry(tsdPtr->documents, Tcl_GetStringFromObj(objPtr, NULL));
if (entryPtr) {
if (objPtr->typePtr != NULL && objPtr->typePtr->freeIntRepProc != NULL) {
objPtr->typePtr->freeIntRepProc(objPtr);
}
objPtr->internalRep.twoPtrValue.ptr1 = Tcl_GetHashValue(entryPtr);
objPtr->typePtr = &TclXMLlibxml2_DocObjType;
tDocPtr = (TclXML_libxml2_Document *) objPtr->internalRep.twoPtrValue.ptr1;
/*
* Add this object to the Tcl_Obj list.
* NB. There should be no duplicates.
*/
listPtr = (ObjList *) tDocPtr->objs;
if (listPtr == NULL) {
listPtr = (ObjList *) Tcl_Alloc(sizeof(ObjList));
listPtr->objPtr = objPtr;
listPtr->next = NULL;
tDocPtr->objs = listPtr;
} else {
ObjList *newPtr;
newPtr = (ObjList *) Tcl_Alloc(sizeof(ObjList));
newPtr->objPtr = objPtr;
newPtr->next = listPtr;
tDocPtr->objs = (void *) newPtr;
}
/* SANITY CHECK NEEDED: no duplicates in the list */
} else {
if (interp) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "token \"", Tcl_GetStringFromObj(objPtr, NULL), "\" is not a libxml2 document", NULL);
}
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXMLlibxml2_DocUpdate --
*
* Finds the token for a xmlDocPtr wrapper.
*
* Results:
* None.
*
* Side effects:
* Changes the Tcl_Obj's string rep.
*
*----------------------------------------------------------------------------
*/
void
TclXMLlibxml2_DocUpdate(objPtr)
Tcl_Obj *objPtr;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
Tcl_HashEntry *entryPtr;
entryPtr = Tcl_FindHashEntry(tsdPtr->docByPtr, objPtr->internalRep.twoPtrValue.ptr1);
Tcl_InvalidateStringRep(objPtr);
if (entryPtr != NULL) {
TclXML_libxml2_Document *tDocPtr = (TclXML_libxml2_Document *) Tcl_GetHashValue(entryPtr);
objPtr->length = strlen(tDocPtr->token);
objPtr->bytes = Tcl_Alloc(objPtr->length + 1);
strcpy(objPtr->bytes, tDocPtr->token);
}
}
/*
*----------------------------------------------------------------------------
*
* TclXMLlibxml2_DocDup --
*
* Duplicates the Tcl wrapper.
* NB. This does *not* copy the document itself - it simply creates
* another reference to the same document.
*
* Results:
* None.
*
* Side effects:
* Changes the target Tcl_Obj.
*
*----------------------------------------------------------------------------
*/
void
TclXMLlibxml2_DocDup(srcPtr, dstPtr)
Tcl_Obj *srcPtr;
Tcl_Obj *dstPtr;
{
TclXML_libxml2_Document *tDocPtr;
ObjList *listPtr;
if (dstPtr->typePtr != NULL && dstPtr->typePtr->freeIntRepProc != NULL) {
dstPtr->typePtr->freeIntRepProc(dstPtr);
}
tDocPtr = (TclXML_libxml2_Document *) srcPtr->internalRep.twoPtrValue.ptr1;
listPtr = (ObjList *) Tcl_Alloc(sizeof(ObjList));
listPtr->objPtr = dstPtr;
listPtr->next = ((ObjList *) tDocPtr->objs)->next;
tDocPtr->objs = listPtr;
Tcl_InvalidateStringRep(dstPtr);
dstPtr->internalRep.twoPtrValue.ptr1 = srcPtr->internalRep.twoPtrValue.ptr1;
dstPtr->internalRep.twoPtrValue.ptr2 = NULL;
dstPtr->typePtr = srcPtr->typePtr;
}
/*
*----------------------------------------------------------------------------
*
* TclXMLlibxml2_DocFree --
*
* Removes a Tcl wrapper to a libxml2 document.
*
* Results:
* None.
*
* Side effects:
* May free the document.
*
*----------------------------------------------------------------------------
*/
void
TclXMLlibxml2_DocFree(objPtr)
Tcl_Obj *objPtr;
{
TclXML_libxml2_Document *tDocPtr = (TclXML_libxml2_Document *) objPtr->internalRep.twoPtrValue.ptr1;
ObjList *listPtr = tDocPtr->objs;
ObjList *prevPtr = NULL;
while (listPtr) {
if (listPtr->objPtr == objPtr) {
break;
}
prevPtr = listPtr;
listPtr = listPtr->next;
}
if (listPtr == NULL) {
/* internal error */
} else if (prevPtr == NULL) {
tDocPtr->objs = listPtr->next;
} else {
prevPtr->next = listPtr->next;
}
Tcl_Free((char *) listPtr);
if (tDocPtr->objs == NULL && tDocPtr->keep == TCLXML_LIBXML2_DOCUMENT_IMPLICIT) {
DestroyTclDoc(tDocPtr);
}
objPtr->internalRep.twoPtrValue.ptr1 = NULL;
objPtr->internalRep.twoPtrValue.ptr2 = NULL;
objPtr->typePtr = NULL;
}
/*
*----------------------------------------------------------------------------
*
* DestroyTclDoc --
*
* Destroy the Tcl wrapper for a document.
*
* Results:
* None.
*
* Side effects:
* Free memory.
*
*----------------------------------------------------------------------------
*/
void
DestroyTclDoc(tDocPtr)
TclXML_libxml2_Document *tDocPtr;
{
ThreadSpecificData *tsdPtr = Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
Tcl_HashEntry *entryPtr;
if (tDocPtr->domfree) {
(tDocPtr->domfree)(tDocPtr->dom);
}
if (tDocPtr->appfree) {
(tDocPtr->appfree)(tDocPtr->dom);
}
entryPtr = Tcl_FindHashEntry(tsdPtr->documents, tDocPtr->token);
if (entryPtr) {
Tcl_DeleteHashEntry(entryPtr);
} else {
/* Internal error */
}
entryPtr = Tcl_FindHashEntry(tsdPtr->docByPtr, (ClientData) tDocPtr->docPtr);
if (entryPtr) {
Tcl_DeleteHashEntry(entryPtr);
} else {
/* Internal error */
}
Tcl_MutexLock(&libxml2);
xmlFreeDoc(tDocPtr->docPtr);
Tcl_MutexUnlock(&libxml2);
Tcl_Free(tDocPtr->token);
Tcl_Free((char *) tDocPtr);
}
/*
*----------------------------------------------------------------------------
*
* TclXML_libxml2_ErrorHandler --
*
* Handler for structured error reports
*
* Results:
* None.
*
* Side effects:
* Creates a Tcl_Obj to store the error information.
*
*----------------------------------------------------------------------------
*/
static Tcl_Obj *
ErrorDomainToString(domain)
int domain;
{
switch ((xmlErrorDomain) domain) {
case XML_FROM_NONE:
return Tcl_NewStringObj("none", -1);
case XML_FROM_PARSER:
return Tcl_NewStringObj("parser", -1);
case XML_FROM_TREE:
return Tcl_NewStringObj("tree", -1);
case XML_FROM_NAMESPACE:
return Tcl_NewStringObj("namespace", -1);
case XML_FROM_DTD:
return Tcl_NewStringObj("dtd-validation", -1);
case XML_FROM_HTML:
return Tcl_NewStringObj("html-parser", -1);
case XML_FROM_MEMORY:
return Tcl_NewStringObj("memory", -1);
case XML_FROM_OUTPUT:
return Tcl_NewStringObj("output", -1);
case XML_FROM_IO:
return Tcl_NewStringObj("io", -1);
case XML_FROM_FTP:
return Tcl_NewStringObj("ftp", -1);
case XML_FROM_HTTP:
return Tcl_NewStringObj("http", -1);
case XML_FROM_XINCLUDE:
return Tcl_NewStringObj("XInclude", -1);
case XML_FROM_XPOINTER:
return Tcl_NewStringObj("XPointer", -1);
case XML_FROM_REGEXP:
return Tcl_NewStringObj("regexp", -1);
case XML_FROM_DATATYPE:
return Tcl_NewStringObj("schemas-datatype", -1);
case XML_FROM_SCHEMASP:
return Tcl_NewStringObj("schemas-parser", -1);
case XML_FROM_SCHEMASV:
return Tcl_NewStringObj("schemas-validation", -1);
case XML_FROM_RELAXNGP:
return Tcl_NewStringObj("relaxng-parser", -1);
case XML_FROM_RELAXNGV:
return Tcl_NewStringObj("relaxng-validation", -1);
case XML_FROM_CATALOG:
return Tcl_NewStringObj("catalog", -1);
case XML_FROM_C14N:
return Tcl_NewStringObj("canonicalization", -1);
case XML_FROM_XSLT:
return Tcl_NewStringObj("xslt", -1);
default:
return Tcl_NewObj();
}
}
static Tcl_Obj *
ErrorLevelToString(level)
xmlErrorLevel level;
{
switch (level) {
case XML_ERR_WARNING:
return Tcl_NewStringObj("warning", -1);
case XML_ERR_ERROR:
return Tcl_NewStringObj("error", -1);
case XML_ERR_FATAL:
return Tcl_NewStringObj("fatal", -1);
case XML_ERR_NONE:
default:
return Tcl_NewStringObj("none", -1);
}
}
static Tcl_Obj *
ErrorCodeToString(code)
int code;
{
switch ((xmlParserErrors) code) {
case XML_ERR_OK:
return Tcl_NewObj();
case XML_ERR_INTERNAL_ERROR:
return Tcl_NewStringObj("internal-error", -1);
case XML_ERR_NO_MEMORY:
return Tcl_NewStringObj("no-memory", -1);
case XML_ERR_DOCUMENT_START:
return Tcl_NewStringObj("document-start", -1);
case XML_ERR_DOCUMENT_EMPTY:
return Tcl_NewStringObj("document-empty", -1);
case XML_ERR_DOCUMENT_END:
return Tcl_NewStringObj("document-end", -1);
case XML_ERR_INVALID_HEX_CHARREF:
return Tcl_NewStringObj("invalid-hex-character-reference", -1);
case XML_ERR_INVALID_DEC_CHARREF:
return Tcl_NewStringObj("invalid-decimal-character-reference", -1);
case XML_ERR_INVALID_CHARREF:
return Tcl_NewStringObj("invalid-character-reference", -1);
case XML_ERR_INVALID_CHAR:
return Tcl_NewStringObj("invalid-character", -1);
case XML_ERR_CHARREF_AT_EOF:
return Tcl_NewStringObj("character-reference-at-eof", -1);
case XML_ERR_CHARREF_IN_PROLOG:
return Tcl_NewStringObj("character-reference-in-prolog", -1);
case XML_ERR_CHARREF_IN_EPILOG:
return Tcl_NewStringObj("character-reference-in-epilog", -1);
case XML_ERR_CHARREF_IN_DTD:
return Tcl_NewStringObj("character-reference-in-dtd", -1);
case XML_ERR_ENTITYREF_AT_EOF:
return Tcl_NewStringObj("entity-reference-at-eof", -1);
case XML_ERR_ENTITYREF_IN_PROLOG:
return Tcl_NewStringObj("entity-reference-in-prolog", -1);
case XML_ERR_ENTITYREF_IN_EPILOG:
return Tcl_NewStringObj("entity-reference-in-epilog", -1);
case XML_ERR_ENTITYREF_IN_DTD:
return Tcl_NewStringObj("entity-reference-in-dtd", -1);
case XML_ERR_PEREF_AT_EOF:
return Tcl_NewStringObj("parameter-entity-reference-at-eof", -1);
case XML_ERR_PEREF_IN_PROLOG:
return Tcl_NewStringObj("parameter-entity-reference-in-prolog", -1);
case XML_ERR_PEREF_IN_EPILOG:
return Tcl_NewStringObj("parameter-entity-reference-in-epilog", -1);
case XML_ERR_PEREF_IN_INT_SUBSET:
return Tcl_NewStringObj("parameter-entity-reference-in-internal-subset", -1);
case XML_ERR_ENTITYREF_NO_NAME:
return Tcl_NewStringObj("entity-reference-no-name", -1);
case XML_ERR_ENTITYREF_SEMICOL_MISSING:
return Tcl_NewStringObj("entity-reference-semicolon-missing", -1);
case XML_ERR_PEREF_NO_NAME:
return Tcl_NewStringObj("parameter-entity-reference-no-name", -1);
case XML_ERR_PEREF_SEMICOL_MISSING:
return Tcl_NewStringObj("parameter-entity-reference-semicolon-missing", -1);
case XML_ERR_UNDECLARED_ENTITY:
return Tcl_NewStringObj("undeclared-entity", -1);
case XML_WAR_UNDECLARED_ENTITY:
return Tcl_NewStringObj("undeclared-entity", -1);
case XML_ERR_UNPARSED_ENTITY:
return Tcl_NewStringObj("unparsed-entity", -1);
case XML_ERR_ENTITY_IS_EXTERNAL:
return Tcl_NewStringObj("entity-is-external", -1);
case XML_ERR_ENTITY_IS_PARAMETER:
return Tcl_NewStringObj("entity-is-parameter", -1);
case XML_ERR_UNKNOWN_ENCODING:
return Tcl_NewStringObj("unknown-encoding", -1);
case XML_ERR_UNSUPPORTED_ENCODING:
return Tcl_NewStringObj("unsupported-encoding", -1);
case XML_ERR_STRING_NOT_STARTED:
return Tcl_NewStringObj("string-not-started", -1);
case XML_ERR_STRING_NOT_CLOSED:
return Tcl_NewStringObj("string-not-closed", -1);
case XML_ERR_NS_DECL_ERROR:
return Tcl_NewStringObj("namespace-declaration-error", -1);
case XML_ERR_ENTITY_NOT_STARTED:
return Tcl_NewStringObj("entity-not-started", -1);
case XML_ERR_ENTITY_NOT_FINISHED:
return Tcl_NewStringObj("entity-not-finished", -1);
case XML_ERR_LT_IN_ATTRIBUTE:
return Tcl_NewStringObj("less-than-character-in-attribute", -1);
case XML_ERR_ATTRIBUTE_NOT_STARTED:
return Tcl_NewStringObj("attribute-not-started", -1);
case XML_ERR_ATTRIBUTE_NOT_FINISHED:
return Tcl_NewStringObj("attribute-not-finished", -1);
case XML_ERR_ATTRIBUTE_WITHOUT_VALUE:
return Tcl_NewStringObj("attribute-without-value", -1);
case XML_ERR_ATTRIBUTE_REDEFINED:
return Tcl_NewStringObj("attribute-redefined", -1);
case XML_ERR_LITERAL_NOT_STARTED:
return Tcl_NewStringObj("literal-not-started", -1);
case XML_ERR_LITERAL_NOT_FINISHED:
return Tcl_NewStringObj("literal-not-finished", -1);
case XML_ERR_COMMENT_NOT_FINISHED:
return Tcl_NewStringObj("comment-not-finished", -1);
case XML_ERR_PI_NOT_STARTED:
return Tcl_NewStringObj("processing-instruction-not-started", -1);
case XML_ERR_PI_NOT_FINISHED:
return Tcl_NewStringObj("processing-instruction-not-finished", -1);
case XML_ERR_NOTATION_NOT_STARTED:
return Tcl_NewStringObj("notation-not-started", -1);
case XML_ERR_NOTATION_NOT_FINISHED:
return Tcl_NewStringObj("notation-not-finished", -1);
case XML_ERR_ATTLIST_NOT_STARTED:
return Tcl_NewStringObj("attribute-list-not-started", -1);
case XML_ERR_ATTLIST_NOT_FINISHED:
return Tcl_NewStringObj("attribute-list-not-finished", -1);