-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcldom-libxml2.c
7483 lines (6331 loc) · 193 KB
/
tcldom-libxml2.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
/* tcldom-libxml2.c --
*
* A Tcl wrapper for libxml's node tree API,
* conformant to the TclDOM API.
*
* Copyright (c) 2005-2009 by Explain.
* http://www.explain.com.au/
* Copyright (c) 2001-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: tcldomlibxml2.c,v 1.2 2005/05/24 21:09:56 balls Exp $
*/
#include <tcldom/tcldom.h>
#include <tcldom-libxml2/tcldom-libxml2.h>
#include <tclxml-libxml2/docObj.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlIO.h>
#include <libxml/HTMLtree.h>
#include <libxml/globals.h>
#include <libxml/xinclude.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlschemas.h>
#include <libxml/xmlschemastypes.h>
#include <libxml/relaxng.h>
#include <libxml/xmlsave.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
/*
* Manage lists of Tcl_Obj's
*/
typedef struct ObjList {
Tcl_Obj *objPtr;
struct ObjList *next;
} ObjList;
/*
* Forward declarations for private functions.
*/
static void FreeDocument _ANSI_ARGS_((ClientData clientData));
static TclDOM_libxml2_Document * GetDOMDocument _ANSI_ARGS_((Tcl_Interp *interp,
TclXML_libxml2_Document *tDocPtr));
static void TclDOM_libxml2_DestroyNode _ANSI_ARGS_((Tcl_Interp *interp, TclDOM_libxml2_Node *tNodePtr));
static void TclDOM_libxml2_InvalidateNode _ANSI_ARGS_((TclDOM_libxml2_Node *tNodePtr));
static char * TclDOMLiveNodeListNode _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp,
char *name1,
char *name2,
int flags));
static char * TclDOMLiveNodeListDoc _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp,
char *name1,
char *name2,
int flags));
static char * TclDOMLiveNamedNodeMap _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp,
char *name1,
char *name2,
int flags));
static int TclDOMSetLiveNodeListNode _ANSI_ARGS_((Tcl_Interp *interp,
char *varname,
xmlNodePtr nodePtr));
static int TclDOMSetLiveNodeListDoc _ANSI_ARGS_((Tcl_Interp *interp,
char *varname,
xmlDocPtr docPtr));
static int TclDOMSetLiveNamedNodeMap _ANSI_ARGS_((Tcl_Interp *interp,
char *varname,
xmlNodePtr nodePtr));
/*
* Forward declarations of commands
*/
static int TclDOMDOMImplementationCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMDocumentCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static void DocumentNodeCmdDelete _ANSI_ARGS_((ClientData clientdata));
static int TclDOMNodeCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static void TclDOMNodeCommandDelete _ANSI_ARGS_((ClientData clientdata));
static int TclDOMElementCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMEventCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static void TclDOMEventCommandDelete _ANSI_ARGS_((ClientData clientdata));
static Tcl_Obj * TclDOM_libxml2_NewEventObj _ANSI_ARGS_((Tcl_Interp *interp,
xmlDocPtr docPtr,
enum TclDOM_EventTypes type,
Tcl_Obj *typeObjPtr));
/*
* Functions that implement the TclDOM_Implementation interface
*/
static int TclDOM_HasFeatureCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMCreateCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMDestroyCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMParseCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMSerializeCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMSelectNodeCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMIsNodeCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMAdoptCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
/*
* Additional features
*/
static int TclDOMXIncludeCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMPrefix2NSCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static int TclDOMTrimCommand _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]));
static void TrimDocument _ANSI_ARGS_((Tcl_Interp *interp, xmlDocPtr docPtr));
static int AdoptDocument _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *objPtr));
static int DocumentCget _ANSI_ARGS_((Tcl_Interp *interp,
xmlDocPtr docPtr,
Tcl_Obj *CONST objPtr));
static int DocumentConfigure _ANSI_ARGS_((Tcl_Interp *interp,
xmlDocPtr docPtr,
int objc,
Tcl_Obj *CONST objv[]));
static int NodeCget _ANSI_ARGS_((Tcl_Interp *interp,
xmlDocPtr docPtr,
xmlNodePtr nodePtr,
Tcl_Obj *CONST objPtr));
static int NodeConfigure _ANSI_ARGS_((Tcl_Interp *interp,
xmlNodePtr nodePtr,
int objc,
Tcl_Obj *CONST objPtr[]));
static int ElementCget _ANSI_ARGS_((Tcl_Interp *interp,
xmlNodePtr nodePtr,
Tcl_Obj *CONST objPtr));
static int TclDOM_NodeAppendChild _ANSI_ARGS_((Tcl_Interp *interp,
xmlNodePtr nodePtr,
xmlNodePtr newPtr));
static int TclDOM_NodeInsertBefore _ANSI_ARGS_((Tcl_Interp *interp,
xmlNodePtr refPtr,
xmlNodePtr newPtr));
static void PostMutationEvents _ANSI_ARGS_((Tcl_Interp *interp,
TclXML_libxml2_Document *tDocPtr,
xmlNodePtr nodePtr,
xmlNodePtr refPtr,
xmlNodePtr newPtr,
xmlNodePtr oldParent,
xmlNodePtr newParent));
static int DTDValidate _ANSI_ARGS_((Tcl_Interp *interp,
TclDOM_libxml2_Document *domDocPtr));
static int SchemaCompile _ANSI_ARGS_((Tcl_Interp *interp,
TclDOM_libxml2_Document *domDocPtr));
static int SchemaValidate _ANSI_ARGS_((Tcl_Interp *interp,
TclDOM_libxml2_Document *domDocPtr,
xmlDocPtr instancePtr));
static int RelaxNGCompile _ANSI_ARGS_((Tcl_Interp *interp,
TclDOM_libxml2_Document *domDocPtr));
static int RelaxNGValidate _ANSI_ARGS_((Tcl_Interp *interp,
TclDOM_libxml2_Document *domDocPtr,
xmlDocPtr instance));
static void NodeAddObjRef _ANSI_ARGS_((TclDOM_libxml2_Node *tNodePtr,
Tcl_Obj *objPtr));
#if 0
static void DumpNode _ANSI_ARGS_((TclDOM_libxml2_Node *tNodePtr));
#endif
/*
* Other utilities
*/
static Tcl_Obj * GetPath _ANSI_ARGS_((Tcl_Interp *interp,
xmlNodePtr nodePtr));
/*
* MS VC++ oddities
*/
#ifdef WIN32
#if !defined (__CYGWIN__)
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#endif /* __CYGWIN__ */
#endif /* WIN32 */
/*
* Nodes as Tcl Objects (overloaded to also support event nodes).
*/
Tcl_FreeInternalRepProc NodeTypeFree;
Tcl_DupInternalRepProc NodeTypeDup;
Tcl_UpdateStringProc NodeTypeUpdate;
Tcl_SetFromAnyProc NodeTypeSetFromAny;
Tcl_ObjType NodeObjType = {
"libxml2-node",
NodeTypeFree,
NodeTypeDup,
NodeTypeUpdate,
NodeTypeSetFromAny
};
/*
* For additional checks when creating nodes.
* These are setup at initialisation-time, but thereafter are read-only.
*/
static Tcl_Obj *checkName;
static Tcl_Obj *checkQName;
/*
* libxml2 is mostly thread-safe, but there are issues with error callbacks
*/
TCL_DECLARE_MUTEX(libxml2)
/*
* Statically include the definitions of option tables:
* Due to linking problems on Windows, using MS VC++.
*/
#include "tcldom.c"
/*
*----------------------------------------------------------------------------
*
* Tcldom_libxml2_Init --
*
* Initialisation routine for module.
* This is no longer loaded as a separate module.
*
* Results:
* None.
*
* Side effects:
* Creates commands in the interpreter,
*
*----------------------------------------------------------------------------
*/
int
Tcldom_libxml2_Init (interp)
Tcl_Interp *interp; /* Interpreter to initialise */
{
Tcl_MutexLock(&libxml2);
xmlXPathInit();
Tcl_MutexUnlock(&libxml2);
/*
* Provide a handler for nodes for structured error reporting
*/
TclXML_libxml2_SetErrorNodeFunc(interp,
(TclXML_ErrorNodeHandlerProc *) TclDOM_libxml2_CreateObjFromNode);
/*
* For each of the standard commands, register the command
* in both the ::dom and ::dom::libxml2 Tcl namespaces -
* they are equivalent.
*/
Tcl_CreateObjCommand(interp, "dom::libxml2::DOMImplementation",
TclDOMDOMImplementationCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::DOMImplementation",
TclDOMDOMImplementationCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::hasfeature",
TclDOM_HasFeatureCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::hasfeature",
TclDOM_HasFeatureCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::document",
TclDOMDocumentCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::document",
TclDOMDocumentCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::node",
TclDOMNodeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::node",
TclDOMNodeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::create",
TclDOMCreateCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::create",
TclDOMCreateCommand, NULL, NULL);
/*
* Implemented in Tcl (for the moment)
Tcl_CreateObjCommand(interp, "dom::libxml2::parse",
TclDOMParseCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::parse",
TclDOMParseCommand, NULL, NULL);
*/
Tcl_CreateObjCommand(interp, "dom::libxml2::adoptdocument",
TclDOMAdoptCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::serialize",
TclDOMSerializeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::serialize",
TclDOMSerializeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::selectnode",
TclDOMSelectNodeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::selectNode",
TclDOMSelectNodeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::isNode",
TclDOMIsNodeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::isNode",
TclDOMIsNodeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::element",
TclDOMElementCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::element",
TclDOMElementCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::event",
TclDOMEventCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::event",
TclDOMEventCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::xinclude",
TclDOMXIncludeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::xinclude",
TclDOMXIncludeCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::prefix2namespaceURI",
TclDOMPrefix2NSCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::prefix2namespaceURI",
TclDOMPrefix2NSCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::destroy",
TclDOMDestroyCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::destroy",
TclDOMDestroyCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::libxml2::trim",
TclDOMTrimCommand, NULL, NULL);
Tcl_CreateObjCommand(interp, "dom::trim",
TclDOMTrimCommand, NULL, NULL);
/* Setup name checking REs */
checkName = Tcl_NewStringObj("^", -1);
Tcl_AppendObjToObj(checkName, Tcl_GetVar2Ex(interp, "::xml::Name", NULL, 0));
Tcl_AppendToObj(checkName, "$", -1);
Tcl_IncrRefCount(checkName);
checkQName = Tcl_NewStringObj("^", -1);
Tcl_AppendObjToObj(checkQName, Tcl_GetVar2Ex(interp, "::xml::QName", NULL, 0));
Tcl_AppendToObj(checkQName, "$", -1);
Tcl_IncrRefCount(checkQName);
TclDOM_SetVars(interp);
Tcl_RegisterObjType(&NodeObjType);
return TCL_OK;
}
/*
* DOM is safe, since it is merely an in-memory representation of the document tree.
* However, XInclude is not safe. This is still OK because XInclude uses the external
* entity mechanism to load remote documents and TclXML/libxml2 intercepts those calls.
*/
int
Tcldom_libxml2_SafeInit (interp)
Tcl_Interp *interp; /* Interpreter to initialise */
{
return Tcldom_libxml2_Init(interp);
}
#if 0
void
DumpDocNodeTable(domDocPtr)
TclDOM_libxml2_Document *domDocPtr;
{
return;
/*
TclDOM_libxml2_Node *tNodePtr;
Tcl_HashEntry *entryPtr;
Tcl_HashSearch search;
sprintf(dbgbuf, " Nodes in doc \"%s\":\n", domDocPtr->tDocPtr->token);
Tcl_WriteChars(stderrChan, dbgbuf, -1);
for (entryPtr = Tcl_FirstHashEntry(domDocPtr->nodes, &search);
entryPtr;
entryPtr = Tcl_NextHashEntry(&search)) {
tNodePtr = (TclDOM_libxml2_Node *) Tcl_GetHashValue(entryPtr);
sprintf(dbgbuf, " Hash entry \"%s\" (x%x)\n", Tcl_GetHashKey(domDocPtr->nodes, entryPtr), tNodePtr);
Tcl_WriteChars(stderrChan, dbgbuf, -1);
sprintf(dbgbuf, " Node \"%s\"\n", tNodePtr->token);
Tcl_WriteChars(stderrChan, dbgbuf, -1);
}
*/
}
#endif
/*
*----------------------------------------------------------------------------
*
* TclDOM_HasFeatureCommand --
*
* Implements dom::libxml2::hasfeature command
*
* Results:
* Returns boolean.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
int
TclDOM_HasFeatureCommand (dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
if (objc != 3) {
Tcl_WrongNumArgs(interp, 0, objv, "hasfeature feature version");
return TCL_ERROR;
}
if (Tcl_RegExpMatchObj(interp, objv[1], Tcl_NewStringObj("create|destroy|parse|query|serialize|trim|Events|UIEvents|isNode", -1)) == 1) {
if (Tcl_StringMatch(Tcl_GetStringFromObj(objv[2], NULL), "1.0") == 1) {
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(1));
} else {
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(0));
}
} else {
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(0));
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclDOMCreateCommand --
*
* Implements dom::libxml2::create command
*
* Results:
* Creates a new document.
*
* Side effects:
* Allocates memory.
*
*----------------------------------------------------------------------------
*/
int
TclDOMCreateCommand (dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
Tcl_Obj *objPtr;
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
objPtr = TclXML_libxml2_NewDocObj(interp);
if (!objPtr) {
return TCL_ERROR;
}
TclXML_libxml2_DocKeep(objPtr, TCLXML_LIBXML2_DOCUMENT_KEEP);
if (AdoptDocument(interp, objPtr) != TCL_OK) {
return TCL_ERROR;
}
return TCL_OK;
}
int
AdoptDocument(interp, objPtr)
Tcl_Interp *interp;
Tcl_Obj *objPtr;
{
TclXML_libxml2_Document *tDocPtr;
TclDOM_libxml2_Document *domDocPtr;
/*
* Claim this object so the document will not be destroyed
* underneath us.
*/
Tcl_IncrRefCount(objPtr);
if (TclXML_libxml2_GetTclDocFromObj(interp, objPtr, &tDocPtr) != TCL_OK) {
return TCL_ERROR;
}
domDocPtr = (TclDOM_libxml2_Document *) Tcl_Alloc(sizeof(TclDOM_libxml2_Document));
domDocPtr->interp = interp;
domDocPtr->tDocPtr = tDocPtr;
domDocPtr->objPtr = objPtr;
domDocPtr->schema = NULL;
domDocPtr->relaxng = NULL;
domDocPtr->nodes = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(domDocPtr->nodes, TCL_STRING_KEYS);
domDocPtr->nodeCntr = 0;
domDocPtr->captureListeners = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(domDocPtr->captureListeners, TCL_ONE_WORD_KEYS);
domDocPtr->bubbleListeners = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(domDocPtr->bubbleListeners, TCL_ONE_WORD_KEYS);
memset(domDocPtr->listening, 0, TCLDOM_NUM_EVENT_TYPES * sizeof(int));
/*
* When the document is eventually destroyed,
* make sure all memory is freed.
*/
tDocPtr->dom = (ClientData) domDocPtr;
tDocPtr->domfree = FreeDocument;
/*
* Create a Tcl namespace for this document
*/
Tcl_VarEval(interp, "namespace eval ::dom::", tDocPtr->token, " {}\n", NULL);
/*
* Create a DOM command to control the document.
*/
domDocPtr->cmd = Tcl_CreateObjCommand(interp, tDocPtr->token, TclDOMDocumentCommand, (ClientData) domDocPtr, DocumentNodeCmdDelete);
Tcl_SetObjResult(interp, objPtr);
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclDOM_libxml2_CreateObjFromDoc --
*
* Wrapper for TclXML_libxml2_CreateObjFromDoc
*
* Results:
* Returns Tcl_Obj.
*
* Side effects:
* Allocates memory.
*
*----------------------------------------------------------------------------
*/
Tcl_Obj *
TclDOM_libxml2_CreateObjFromDoc (interp, docPtr)
Tcl_Interp *interp;
xmlDocPtr docPtr;
{
Tcl_Obj *newPtr;
newPtr = TclXML_libxml2_CreateObjFromDoc(docPtr);
if (AdoptDocument(interp, newPtr) != TCL_OK) {
Tcl_DecrRefCount(newPtr);
return NULL;
}
return newPtr;
}
/*
*----------------------------------------------------------------------------
*
* TclDOMDestroyCommand --
*
* Implements dom::libxml2::destroy command
*
* Results:
* Frees document or node.
*
* Side effects:
* Deallocates memory.
*
*----------------------------------------------------------------------------
*/
int
TclDOMDestroyCommand (dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
TclXML_libxml2_Document *tDocPtr;
TclDOM_libxml2_Node *tNodePtr;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "token");
return TCL_ERROR;
}
if (TclXML_libxml2_GetTclDocFromObj(interp, objv[1], &tDocPtr) == TCL_OK) {
TclDOM_libxml2_Document *domDocPtr = GetDOMDocument(interp, tDocPtr);
if (domDocPtr == NULL) {
/* This is an error! */
TclXML_libxml2_DestroyDocument(tDocPtr);
} else {
Tcl_DeleteCommandFromToken(interp, domDocPtr->cmd);
}
} else if (TclDOM_libxml2_GetTclNodeFromObj(interp, objv[1], &tNodePtr) == TCL_OK) {
TclDOM_libxml2_DestroyNode(interp, tNodePtr);
} else if (TclDOM_libxml2_GetTclEventFromObj(interp, objv[1], &tNodePtr) == TCL_OK) {
TclDOM_libxml2_DestroyNode(interp, tNodePtr);
} else {
Tcl_SetResult(interp, "not a DOM node", NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* DocumentNodeCmdDelete --
*
* Invoked when a DOM document's command is deleted.
*
* Results:
* Frees document.
*
* Side effects:
* Deallocates memory.
*
*----------------------------------------------------------------------------
*/
void
DocumentNodeCmdDelete (clientData)
ClientData clientData;
{
TclDOM_libxml2_Document *domDocPtr = (TclDOM_libxml2_Document *) clientData;
#ifndef WIN32
TclXML_libxml2_DestroyDocument(domDocPtr->tDocPtr);
#endif /* not WIN32 */
#ifdef WIN32
/*
* Workaround bug in TclXML/libxml2.
* This will, of course, leak memory.
*/
/* FreeDocument((ClientData) domDocPtr); */
#endif /* WIN32 */
}
/*
*----------------------------------------------------------------------------
*
* FreeDocument --
*
* Frees resources associated with a document.
*
* Results:
* None.
*
* Side effects:
* Deallocates memory.
*
*----------------------------------------------------------------------------
*/
#ifdef WIN32
/*
* Using Tcl internal functions appears to cause linking problems
* when using MS VC++, so avoid the problem by invoking a script instead.
*/
void DeleteNamespace (interp, ns)
Tcl_Interp *interp;
char *ns;
{
Tcl_Obj *cmdPtr = Tcl_NewObj();
Tcl_AppendStringsToObj(cmdPtr, "namespace delete ", ns, NULL);
Tcl_EvalObjEx(interp, cmdPtr, TCL_EVAL_GLOBAL);
Tcl_DecrRefCount(cmdPtr);
}
#else /* not WIN32 */
/*
* Internal Tcl functions
*/
#if (TCL_MAJOR_VERSION < 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION < 5))
/*
* SRB: 2005-12-29: This should use #include <tclInt.h>, but private sources may not be available.
*/
EXTERN Tcl_Namespace * Tcl_FindNamespace _ANSI_ARGS_((Tcl_Interp * interp,
CONST char * name,
Tcl_Namespace * contextNsPtr,
int flags));
EXTERN void Tcl_DeleteNamespace _ANSI_ARGS_((Tcl_Namespace * nsPtr));
#endif /* Tcl < 8.5 */
void DeleteNamespace (interp, ns)
Tcl_Interp *interp;
char *ns;
{
Tcl_Namespace *namespacePtr;
namespacePtr = Tcl_FindNamespace(interp, ns,
(Tcl_Namespace *) NULL, 0);
if (namespacePtr) {
Tcl_DeleteNamespace(namespacePtr);
} /* else internal error */
}
#endif /* WIN32 */
void
FreeDocument (clientData)
ClientData clientData;
{
TclDOM_libxml2_Document *domDocPtr = (TclDOM_libxml2_Document *) clientData;
char buf[1024];
snprintf(buf, 1023, "::dom::%s", domDocPtr->tDocPtr->token);
DeleteNamespace(domDocPtr->interp, buf);
/*
* Deleting the namespace deletes all of the node commands,
* which in turn invalidates the node references.
* So no need to do it again here.
*
entry = Tcl_FirstHashEntry(domDocPtr->nodes, &search);
while (entry) {
tNodePtr = (TclDOM_libxml2_Node *) Tcl_GetHashValue(entry);
TclDOM_libxml2_InvalidateNode(tNodePtr);
entry = Tcl_NextHashEntry(&search);
}
*/
Tcl_DeleteHashTable(domDocPtr->nodes);
Tcl_Free((char *) domDocPtr->nodes);
if (domDocPtr->schema) {
Tcl_MutexLock(&libxml2);
/* This also frees the copy of the document used by the schema context */
xmlSchemaFree(domDocPtr->schema);
Tcl_MutexUnlock(&libxml2);
}
if (domDocPtr->relaxng) {
Tcl_MutexLock(&libxml2);
/* This also frees the copy of the document used by the schema context */
xmlRelaxNGFree(domDocPtr->relaxng);
Tcl_MutexUnlock(&libxml2);
}
Tcl_Free((char *) domDocPtr->captureListeners);
Tcl_Free((char *) domDocPtr->bubbleListeners);
/* Workaround win32 destroy bug, see above */
#ifndef WIN32
Tcl_DecrRefCount(domDocPtr->objPtr);
#endif /* not WIN32 */
Tcl_Free((char *) domDocPtr);
}
/*
*----------------------------------------------------------------------------
*
* GetDOMDocument --
*
* Retrieves the DOM document structure associated with a libxml2 document.
* libxslt synthesizes documents, so it is often the case that a node
* must be processed that has not had its document "adopted".
*
* Results:
* Returns pointer to DOM structure.
*
* Side effects:
* Document is "adopted" if necessary.
*
*----------------------------------------------------------------------------
*/
TclDOM_libxml2_Document *
GetDOMDocument(interp, tDocPtr)
Tcl_Interp *interp;
TclXML_libxml2_Document *tDocPtr;
{
if (tDocPtr->dom != NULL) {
return (TclDOM_libxml2_Document *) tDocPtr->dom;
} else if (interp == NULL) {
return NULL;
} else {
Tcl_Obj *objPtr;
objPtr = TclXML_libxml2_CreateObjFromDoc(tDocPtr->docPtr);
if (AdoptDocument(interp, objPtr) != TCL_OK) {
Tcl_DecrRefCount(objPtr);
return NULL;
} else {
return (TclDOM_libxml2_Document *) tDocPtr->dom;
}
}
}
/*
*----------------------------------------------------------------------------
*
* TclDOMParseCommand --
*
* Implements dom::libxml2::parse command
*
* Not implemented here at present - calls Tcl script
*
* Results:
* Depends on method.
*
* Side effects:
* Depends on method.
*
*----------------------------------------------------------------------------
*/
int
TclDOMParseCommand (dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
/* Tcl_Obj *objPtr; */
Tcl_Obj **newobjv;
int i;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "xml ?args ...?");
return TCL_ERROR;
}
newobjv = (Tcl_Obj **) Tcl_Alloc((objc + 1) * sizeof(Tcl_Obj *));
newobjv[0] = Tcl_NewStringObj("::dom::libxml2::parse", -1);
for (i = 1; i < objc; i++) {
newobjv[i] = objv[i];
}
newobjv[i] = NULL;
return Tcl_EvalObjv(interp, objc, newobjv, 0);
/*
if (TclXML_CreateParser(interp, objc, objv) != TCL_OK) {
return TCL_ERROR;
}
parserObj = Tcl_GetObjResult(interp);
if (TclXML_Parse(interp, parserObj, objc, objv) != TCL_OK) {
return TCL_ERROR;
}
if (TclXML_Get(interp, parserObj, "document") != TCL_OK) {
return TCL_ERROR;
}
Tcl_SetObjResult(interp, objPtr);
*/
return TCL_OK;
}
int
TclDOMAdoptCommand (dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "doc");
return TCL_ERROR;
}
return AdoptDocument(interp, objv[1]);
}
/*
*----------------------------------------------------------------------------
*
* TclDOMSerializeCommand --
*
* Implements dom::libxml2::serialize command
*
* Results:
* Depends on method.
*
* Side effects:
* Depends on method.
*
*----------------------------------------------------------------------------
*/
int
TclDOMSerializeCommand (dummy, interp, objc, objv)
ClientData dummy;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
xmlDocPtr docPtr;
xmlNodePtr nodePtr;
xmlBufferPtr bufptr = NULL;
xmlSaveCtxtPtr savectxtptr = NULL;
xmlChar *result = NULL;
Tcl_Obj *encodingPtr = NULL;
int option, method = TCLDOM_SERIALIZE_METHOD_XML, indent = 0, len = 0, omitXMLDeclaration = 0, saveoptions = 0;
char *buf, *encoding;
Tcl_Encoding tclencoding;
Tcl_DString *serialized;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "node ?option value ...?");
return TCL_ERROR;
}
if (TclXML_libxml2_GetDocFromObj(interp, objv[1], &docPtr) != TCL_OK) {
if (TclDOM_libxml2_GetNodeFromObj(interp, objv[1], &nodePtr) == TCL_OK) {
/* Serialize just the node */
Tcl_SetResult(interp, "not yet implemented - serialize whole document", NULL);
return TCL_ERROR;
} else {
Tcl_SetResult(interp, "not a libxml2 node", NULL);
return TCL_ERROR;
}
}
if (objc > 2) {
objc -= 2;
objv += 2;
while (objc) {
if (objc == 1) {
Tcl_Obj *msgPtr;