Initial revision
[TestXSLT.git] / libxml2 / SAX.c
1 /*
2  * SAX.c : Default SAX handler to build a tree.
3  *
4  * See Copyright for the status of this software.
5  *
6  * Daniel Veillard <daniel@veillard.com>
7  */
8
9
10 #define IN_LIBXML
11 #include "libxml.h"
12 #include <stdlib.h>
13 #include <string.h>
14 #include <libxml/xmlmemory.h>
15 #include <libxml/tree.h>
16 #include <libxml/parser.h>
17 #include <libxml/parserInternals.h>
18 #include <libxml/valid.h>
19 #include <libxml/entities.h>
20 #include <libxml/xmlerror.h>
21 #include <libxml/debugXML.h>
22 #include <libxml/xmlIO.h>
23 #include <libxml/SAX.h>
24 #include <libxml/uri.h>
25 #include <libxml/valid.h>
26 #include <libxml/HTMLtree.h>
27 #include <libxml/globals.h>
28
29 /* #define DEBUG_SAX */
30 /* #define DEBUG_SAX_TREE */
31
32 /**
33  * getPublicId:
34  * @ctx: the user data (XML parser context)
35  *
36  * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
37  *
38  * Returns a xmlChar *
39  */
40 const xmlChar *
41 getPublicId(void *ctx ATTRIBUTE_UNUSED)
42 {
43     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
44     return(NULL);
45 }
46
47 /**
48  * getSystemId:
49  * @ctx: the user data (XML parser context)
50  *
51  * Provides the system ID, basically URL or filename e.g.
52  * http://www.sgmlsource.com/dtds/memo.dtd
53  *
54  * Returns a xmlChar *
55  */
56 const xmlChar *
57 getSystemId(void *ctx)
58 {
59     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
60     return((const xmlChar *) ctxt->input->filename); 
61 }
62
63 /**
64  * getLineNumber:
65  * @ctx: the user data (XML parser context)
66  *
67  * Provide the line number of the current parsing point.
68  *
69  * Returns an int
70  */
71 int
72 getLineNumber(void *ctx)
73 {
74     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
75     return(ctxt->input->line);
76 }
77
78 /**
79  * getColumnNumber:
80  * @ctx: the user data (XML parser context)
81  *
82  * Provide the column number of the current parsing point.
83  *
84  * Returns an int
85  */
86 int
87 getColumnNumber(void *ctx)
88 {
89     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
90     return(ctxt->input->col);
91 }
92
93 /**
94  * isStandalone:
95  * @ctx: the user data (XML parser context)
96  *
97  * Is this document tagged standalone ?
98  *
99  * Returns 1 if true
100  */
101 int
102 isStandalone(void *ctx)
103 {
104     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
105     return(ctxt->myDoc->standalone == 1);
106 }
107
108 /**
109  * hasInternalSubset:
110  * @ctx: the user data (XML parser context)
111  *
112  * Does this document has an internal subset
113  *
114  * Returns 1 if true
115  */
116 int
117 hasInternalSubset(void *ctx)
118 {
119     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
120     return(ctxt->myDoc->intSubset != NULL);
121 }
122
123 /**
124  * hasExternalSubset:
125  * @ctx: the user data (XML parser context)
126  *
127  * Does this document has an external subset
128  *
129  * Returns 1 if true
130  */
131 int
132 hasExternalSubset(void *ctx)
133 {
134     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
135     return(ctxt->myDoc->extSubset != NULL);
136 }
137
138 /**
139  * internalSubset:
140  * @ctx:  the user data (XML parser context)
141  * @name:  the root element name
142  * @ExternalID:  the external ID
143  * @SystemID:  the SYSTEM ID (e.g. filename or URL)
144  *
145  * Callback on internal subset declaration.
146  */
147 void
148 internalSubset(void *ctx, const xmlChar *name,
149                const xmlChar *ExternalID, const xmlChar *SystemID)
150 {
151     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
152     xmlDtdPtr dtd;
153 #ifdef DEBUG_SAX
154     xmlGenericError(xmlGenericErrorContext,
155             "SAX.internalSubset(%s, %s, %s)\n",
156             name, ExternalID, SystemID);
157 #endif
158
159     if (ctxt->myDoc == NULL)
160         return;
161     dtd = xmlGetIntSubset(ctxt->myDoc);
162     if (dtd != NULL) {
163         if (ctxt->html)
164             return;
165         xmlUnlinkNode((xmlNodePtr) dtd);
166         xmlFreeDtd(dtd);
167         ctxt->myDoc->intSubset = NULL;
168     }
169     ctxt->myDoc->intSubset = 
170         xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
171 }
172
173 /**
174  * externalSubset:
175  * @ctx: the user data (XML parser context)
176  * @name:  the root element name
177  * @ExternalID:  the external ID
178  * @SystemID:  the SYSTEM ID (e.g. filename or URL)
179  *
180  * Callback on external subset declaration.
181  */
182 void
183 externalSubset(void *ctx, const xmlChar *name,
184                const xmlChar *ExternalID, const xmlChar *SystemID)
185 {
186     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
187 #ifdef DEBUG_SAX
188     xmlGenericError(xmlGenericErrorContext,
189             "SAX.externalSubset(%s, %s, %s)\n",
190             name, ExternalID, SystemID);
191 #endif
192     if (((ExternalID != NULL) || (SystemID != NULL)) &&
193         (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
194          (ctxt->wellFormed && ctxt->myDoc))) {
195         /*
196          * Try to fetch and parse the external subset.
197          */
198         xmlParserInputPtr oldinput;
199         int oldinputNr;
200         int oldinputMax;
201         xmlParserInputPtr *oldinputTab;
202         xmlParserInputPtr input = NULL;
203         xmlCharEncoding enc;
204         int oldcharset;
205
206         /*
207          * Ask the Entity resolver to load the damn thing
208          */
209         if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
210             input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
211                                                 SystemID);
212         if (input == NULL) {
213             return;
214         }
215
216         xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
217
218         /*
219          * make sure we won't destroy the main document context
220          */
221         oldinput = ctxt->input;
222         oldinputNr = ctxt->inputNr;
223         oldinputMax = ctxt->inputMax;
224         oldinputTab = ctxt->inputTab;
225         oldcharset = ctxt->charset;
226
227         ctxt->inputTab = (xmlParserInputPtr *)
228                          xmlMalloc(5 * sizeof(xmlParserInputPtr));
229         if (ctxt->inputTab == NULL) {
230             ctxt->errNo = XML_ERR_NO_MEMORY;
231             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
232                 ctxt->sax->error(ctxt->userData, 
233                      "externalSubset: out of memory\n");
234             ctxt->errNo = XML_ERR_NO_MEMORY;
235             ctxt->input = oldinput;
236             ctxt->inputNr = oldinputNr;
237             ctxt->inputMax = oldinputMax;
238             ctxt->inputTab = oldinputTab;
239             ctxt->charset = oldcharset;
240             return;
241         }
242         ctxt->inputNr = 0;
243         ctxt->inputMax = 5;
244         ctxt->input = NULL;
245         xmlPushInput(ctxt, input);
246
247         /*
248          * On the fly encoding conversion if needed
249          */
250         enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
251         xmlSwitchEncoding(ctxt, enc);
252
253         if (input->filename == NULL)
254             input->filename = (char *) xmlStrdup(SystemID);
255         input->line = 1;
256         input->col = 1;
257         input->base = ctxt->input->cur;
258         input->cur = ctxt->input->cur;
259         input->free = NULL;
260
261         /*
262          * let's parse that entity knowing it's an external subset.
263          */
264         xmlParseExternalSubset(ctxt, ExternalID, SystemID);
265
266         /*
267          * Free up the external entities
268          */
269
270         while (ctxt->inputNr > 1)
271             xmlPopInput(ctxt);
272         xmlFreeInputStream(ctxt->input);
273         xmlFree(ctxt->inputTab);
274
275         /*
276          * Restore the parsing context of the main entity
277          */
278         ctxt->input = oldinput;
279         ctxt->inputNr = oldinputNr;
280         ctxt->inputMax = oldinputMax;
281         ctxt->inputTab = oldinputTab;
282         ctxt->charset = oldcharset;
283         /* ctxt->wellFormed = oldwellFormed; */
284     }
285 }
286
287 /**
288  * resolveEntity:
289  * @ctx: the user data (XML parser context)
290  * @publicId: The public ID of the entity
291  * @systemId: The system ID of the entity
292  *
293  * The entity loader, to control the loading of external entities,
294  * the application can either:
295  *    - override this resolveEntity() callback in the SAX block
296  *    - or better use the xmlSetExternalEntityLoader() function to
297  *      set up it's own entity resolution routine
298  *
299  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
300  */
301 xmlParserInputPtr
302 resolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
303 {
304     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
305     xmlParserInputPtr ret;
306     xmlChar *URI;
307     const char *base = NULL;
308
309     if (ctxt->input != NULL)
310         base = ctxt->input->filename;
311     if (base == NULL)
312         base = ctxt->directory;
313
314     URI = xmlBuildURI(systemId, (const xmlChar *) base);
315
316 #ifdef DEBUG_SAX
317     xmlGenericError(xmlGenericErrorContext,
318             "SAX.resolveEntity(%s, %s)\n", publicId, systemId);
319 #endif
320
321     ret = xmlLoadExternalEntity((const char *) URI,
322                                 (const char *) publicId, ctxt);
323     if (URI != NULL)
324         xmlFree(URI);
325     return(ret);
326 }
327
328 /**
329  * getEntity:
330  * @ctx: the user data (XML parser context)
331  * @name: The entity name
332  *
333  * Get an entity by name
334  *
335  * Returns the xmlEntityPtr if found.
336  */
337 xmlEntityPtr
338 getEntity(void *ctx, const xmlChar *name)
339 {
340     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
341     xmlEntityPtr ret = NULL;
342
343 #ifdef DEBUG_SAX
344     xmlGenericError(xmlGenericErrorContext,
345             "SAX.getEntity(%s)\n", name);
346 #endif
347
348     if (ctxt->inSubset == 0) {
349         ret = xmlGetPredefinedEntity(name);
350         if (ret != NULL)
351             return(ret);
352     }
353     if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
354         if (ctxt->inSubset == 2) {
355             ctxt->myDoc->standalone = 0;
356             ret = xmlGetDocEntity(ctxt->myDoc, name);
357             ctxt->myDoc->standalone = 1;
358         } else {
359             ret = xmlGetDocEntity(ctxt->myDoc, name);
360             if (ret == NULL) {
361                 ctxt->myDoc->standalone = 0;
362                 ret = xmlGetDocEntity(ctxt->myDoc, name);
363                 if (ret != NULL) {
364                     if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
365                         ctxt->sax->error(ctxt, 
366                  "Entity(%s) document marked standalone but require external subset\n",
367                                          name);
368                     ctxt->valid = 0;
369                     ctxt->wellFormed = 0;
370                 }
371                 ctxt->myDoc->standalone = 1;
372             }
373         }
374     } else {
375         ret = xmlGetDocEntity(ctxt->myDoc, name);
376     }
377     if ((ret != NULL) &&
378         ((ctxt->validate) || (ctxt->replaceEntities)) &&
379         (ret->children == NULL) &&
380         (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
381         /*
382          * for validation purposes we really need to fetch and
383          * parse the external entity
384          */
385         xmlNodePtr children;
386
387         xmlParseCtxtExternalEntity(ctxt, ret->URI, ret->ExternalID, &children);
388         xmlAddChildList((xmlNodePtr) ret, children);
389         ret->owner = 1;
390     }
391     return(ret);
392 }
393
394 /**
395  * getParameterEntity:
396  * @ctx: the user data (XML parser context)
397  * @name: The entity name
398  *
399  * Get a parameter entity by name
400  *
401  * Returns the xmlEntityPtr if found.
402  */
403 xmlEntityPtr
404 getParameterEntity(void *ctx, const xmlChar *name)
405 {
406     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
407     xmlEntityPtr ret;
408
409 #ifdef DEBUG_SAX
410     xmlGenericError(xmlGenericErrorContext,
411             "SAX.getParameterEntity(%s)\n", name);
412 #endif
413
414     ret = xmlGetParameterEntity(ctxt->myDoc, name);
415     return(ret);
416 }
417
418
419 /**
420  * entityDecl:
421  * @ctx: the user data (XML parser context)
422  * @name:  the entity name 
423  * @type:  the entity type 
424  * @publicId: The public ID of the entity
425  * @systemId: The system ID of the entity
426  * @content: the entity value (without processing).
427  *
428  * An entity definition has been parsed
429  */
430 void
431 entityDecl(void *ctx, const xmlChar *name, int type,
432           const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
433 {
434     xmlEntityPtr ent;
435     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
436
437 #ifdef DEBUG_SAX
438     xmlGenericError(xmlGenericErrorContext,
439             "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
440             name, type, publicId, systemId, content);
441 #endif
442     if (ctxt->inSubset == 1) {
443         ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
444                               systemId, content);
445         if ((ent == NULL) && (ctxt->pedantic) &&
446             (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
447             ctxt->sax->warning(ctxt, 
448              "Entity(%s) already defined in the internal subset\n", name);
449         if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
450             xmlChar *URI;
451             const char *base = NULL;
452
453             if (ctxt->input != NULL)
454                 base = ctxt->input->filename;
455             if (base == NULL)
456                 base = ctxt->directory;
457         
458             URI = xmlBuildURI(systemId, (const xmlChar *) base);
459             ent->URI = URI;
460         }
461     } else if (ctxt->inSubset == 2) {
462         ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
463                               systemId, content);
464         if ((ent == NULL) && (ctxt->pedantic) &&
465             (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
466             ctxt->sax->warning(ctxt, 
467              "Entity(%s) already defined in the external subset\n", name);
468         if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
469             xmlChar *URI;
470             const char *base = NULL;
471
472             if (ctxt->input != NULL)
473                 base = ctxt->input->filename;
474             if (base == NULL)
475                 base = ctxt->directory;
476         
477             URI = xmlBuildURI(systemId, (const xmlChar *) base);
478             ent->URI = URI;
479         }
480     } else {
481         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
482             ctxt->sax->error(ctxt, 
483              "SAX.entityDecl(%s) called while not in subset\n", name);
484     }
485 }
486
487 /**
488  * attributeDecl:
489  * @ctx: the user data (XML parser context)
490  * @elem:  the name of the element
491  * @fullname:  the attribute name 
492  * @type:  the attribute type 
493  * @def:  the type of default value
494  * @defaultValue: the attribute default value
495  * @tree:  the tree of enumerated value set
496  *
497  * An attribute definition has been parsed
498  */
499 void
500 attributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
501               int type, int def, const xmlChar *defaultValue,
502               xmlEnumerationPtr tree)
503 {
504     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
505     xmlAttributePtr attr;
506     xmlChar *name = NULL, *prefix = NULL;
507
508 #ifdef DEBUG_SAX
509     xmlGenericError(xmlGenericErrorContext,
510             "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
511             elem, fullname, type, def, defaultValue);
512 #endif
513     name = xmlSplitQName(ctxt, fullname, &prefix);
514     ctxt->vctxt.valid = 1;
515     if (ctxt->inSubset == 1)
516         attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
517                name, prefix, (xmlAttributeType) type,
518                (xmlAttributeDefault) def, defaultValue, tree);
519     else if (ctxt->inSubset == 2)
520         attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
521            name, prefix, (xmlAttributeType) type, 
522            (xmlAttributeDefault) def, defaultValue, tree);
523     else {
524         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
525             ctxt->sax->error(ctxt, 
526              "SAX.attributeDecl(%s) called while not in subset\n", name);
527         return;
528     }
529     if (ctxt->vctxt.valid == 0)
530         ctxt->valid = 0;
531     if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
532         (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
533         ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
534                                                 attr);
535     if (prefix != NULL)
536         xmlFree(prefix);
537     if (name != NULL)
538         xmlFree(name);
539 }
540
541 /**
542  * elementDecl:
543  * @ctx: the user data (XML parser context)
544  * @name:  the element name 
545  * @type:  the element type 
546  * @content: the element value tree
547  *
548  * An element definition has been parsed
549  */
550 void
551 elementDecl(void *ctx, const xmlChar * name, int type,
552             xmlElementContentPtr content)
553 {
554     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
555     xmlElementPtr elem = NULL;
556
557 #ifdef DEBUG_SAX
558     xmlGenericError(xmlGenericErrorContext,
559                     "SAX.elementDecl(%s, %d, ...)\n", name, type);
560 #endif
561
562     if (ctxt->inSubset == 1)
563         elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
564                                  name, (xmlElementTypeVal) type, content);
565     else if (ctxt->inSubset == 2)
566         elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
567                                  name, (xmlElementTypeVal) type, content);
568     else {
569         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
570             ctxt->sax->error(ctxt,
571                              "SAX.elementDecl(%s) called while not in subset\n",
572                              name);
573         return;
574     }
575     if (elem == NULL)
576         ctxt->valid = 0;
577     if (ctxt->validate && ctxt->wellFormed &&
578         ctxt->myDoc && ctxt->myDoc->intSubset)
579         ctxt->valid &=
580             xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
581 }
582
583 /**
584  * notationDecl:
585  * @ctx: the user data (XML parser context)
586  * @name: The name of the notation
587  * @publicId: The public ID of the entity
588  * @systemId: The system ID of the entity
589  *
590  * What to do when a notation declaration has been parsed.
591  */
592 void
593 notationDecl(void *ctx, const xmlChar *name,
594              const xmlChar *publicId, const xmlChar *systemId)
595 {
596     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
597     xmlNotationPtr nota = NULL;
598
599 #ifdef DEBUG_SAX
600     xmlGenericError(xmlGenericErrorContext,
601             "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
602 #endif
603
604     if ((publicId == NULL) && (systemId == NULL)) {
605         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
606             ctxt->sax->error(ctxt, 
607              "SAX.notationDecl(%s) externalID or PublicID missing\n", name);
608         ctxt->valid = 0;
609         ctxt->wellFormed = 0;
610         return;
611     } else if (ctxt->inSubset == 1)
612         nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
613                               publicId, systemId);
614     else if (ctxt->inSubset == 2)
615         nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
616                               publicId, systemId);
617     else {
618         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
619             ctxt->sax->error(ctxt, 
620              "SAX.notationDecl(%s) called while not in subset\n", name);
621         return;
622     }
623     if (nota == NULL) ctxt->valid = 0;
624     if (ctxt->validate && ctxt->wellFormed &&
625         ctxt->myDoc && ctxt->myDoc->intSubset)
626         ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
627                                                nota);
628 }
629
630 /**
631  * unparsedEntityDecl:
632  * @ctx: the user data (XML parser context)
633  * @name: The name of the entity
634  * @publicId: The public ID of the entity
635  * @systemId: The system ID of the entity
636  * @notationName: the name of the notation
637  *
638  * What to do when an unparsed entity declaration is parsed
639  */
640 void
641 unparsedEntityDecl(void *ctx, const xmlChar *name,
642                    const xmlChar *publicId, const xmlChar *systemId,
643                    const xmlChar *notationName)
644 {
645     xmlEntityPtr ent;
646     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
647 #ifdef DEBUG_SAX
648     xmlGenericError(xmlGenericErrorContext,
649             "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
650             name, publicId, systemId, notationName);
651 #endif
652 #if 0
653     Done in xmlValidateDtdFinal now.
654     if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc) {
655         int ret;
656         ret = xmlValidateNotationUse(&ctxt->vctxt, ctxt->myDoc,
657                                               notationName);
658         if (ret == 0) {
659             ctxt->wellFormed = 0;
660             ctxt->valid = 0;
661         }
662     }
663 #endif
664     if (ctxt->inSubset == 1) {
665         ent = xmlAddDocEntity(ctxt->myDoc, name,
666                         XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
667                         publicId, systemId, notationName);
668         if ((ent == NULL) && (ctxt->pedantic) &&
669             (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
670             ctxt->sax->warning(ctxt, 
671              "Entity(%s) already defined in the internal subset\n", name);
672         if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
673             xmlChar *URI;
674             const char *base = NULL;
675
676             if (ctxt->input != NULL)
677                 base = ctxt->input->filename;
678             if (base == NULL)
679                 base = ctxt->directory;
680         
681             URI = xmlBuildURI(systemId, (const xmlChar *) base);
682             ent->URI = URI;
683         }
684     } else if (ctxt->inSubset == 2) {
685         ent = xmlAddDtdEntity(ctxt->myDoc, name,
686                         XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
687                         publicId, systemId, notationName);
688         if ((ent == NULL) && (ctxt->pedantic) &&
689             (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
690             ctxt->sax->warning(ctxt, 
691              "Entity(%s) already defined in the external subset\n", name);
692         if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
693             xmlChar *URI;
694             const char *base = NULL;
695
696             if (ctxt->input != NULL)
697                 base = ctxt->input->filename;
698             if (base == NULL)
699                 base = ctxt->directory;
700         
701             URI = xmlBuildURI(systemId, (const xmlChar *) base);
702             ent->URI = URI;
703         }
704     } else {
705         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
706             ctxt->sax->error(ctxt, 
707              "SAX.unparsedEntityDecl(%s) called while not in subset\n", name);
708     }
709 }
710
711 /**
712  * setDocumentLocator:
713  * @ctx: the user data (XML parser context)
714  * @loc: A SAX Locator
715  *
716  * Receive the document locator at startup, actually xmlDefaultSAXLocator
717  * Everything is available on the context, so this is useless in our case.
718  */
719 void
720 setDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
721 {
722     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
723 #ifdef DEBUG_SAX
724     xmlGenericError(xmlGenericErrorContext,
725             "SAX.setDocumentLocator()\n");
726 #endif
727 }
728
729 /**
730  * startDocument:
731  * @ctx: the user data (XML parser context)
732  *
733  * called when the document start being processed.
734  */
735 void
736 startDocument(void *ctx)
737 {
738     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
739     xmlDocPtr doc;
740
741 #ifdef DEBUG_SAX
742     xmlGenericError(xmlGenericErrorContext,
743             "SAX.startDocument()\n");
744 #endif
745     if (ctxt->html) {
746         if (ctxt->myDoc == NULL)
747 #ifdef LIBXML_HTML_ENABLED
748             ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
749 #else
750         xmlGenericError(xmlGenericErrorContext,
751                 "libxml2 built without HTML support\n");
752 #endif
753     } else {
754         doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
755         if (doc != NULL) {
756             if (ctxt->encoding != NULL)
757                 doc->encoding = xmlStrdup(ctxt->encoding);
758             else
759                 doc->encoding = NULL;
760             doc->standalone = ctxt->standalone;
761         }
762     }
763     if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
764         (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
765         ctxt->myDoc->URL = xmlCanonicPath((const xmlChar *) ctxt->input->filename);
766         if (ctxt->myDoc->URL == NULL)
767             ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
768     }
769 }
770
771 /**
772  * endDocument:
773  * @ctx: the user data (XML parser context)
774  *
775  * called when the document end has been detected.
776  */
777 void
778 endDocument(void *ctx)
779 {
780     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
781 #ifdef DEBUG_SAX
782     xmlGenericError(xmlGenericErrorContext,
783             "SAX.endDocument()\n");
784 #endif
785     if (ctxt->validate && ctxt->wellFormed &&
786         ctxt->myDoc && ctxt->myDoc->intSubset)
787         ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
788
789     /*
790      * Grab the encoding if it was added on-the-fly
791      */
792     if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
793         (ctxt->myDoc->encoding == NULL)) {
794         ctxt->myDoc->encoding = ctxt->encoding;
795         ctxt->encoding = NULL;
796     }
797     if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
798         (ctxt->myDoc->encoding == NULL)) {
799         ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
800     }
801     if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
802         (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
803         ctxt->myDoc->charset = ctxt->charset;
804     }
805 }
806
807 /**
808  * my_attribute:
809  * @ctx: the user data (XML parser context)
810  * @fullname:  The attribute name, including namespace prefix
811  * @value:  The attribute value
812  * @prefix: the prefix on the element node
813  *
814  * Handle an attribute that has been read by the parser.
815  * The default handling is to convert the attribute into an
816  * DOM subtree and past it in a new xmlAttr element added to
817  * the element.
818  */
819 static void
820 my_attribute(void *ctx, const xmlChar *fullname, const xmlChar *value,
821              const xmlChar *prefix)
822 {
823     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
824     xmlAttrPtr ret;
825     xmlChar *name;
826     xmlChar *ns;
827     xmlChar *nval;
828     xmlNsPtr namespace;
829
830 /****************
831 #ifdef DEBUG_SAX
832     xmlGenericError(xmlGenericErrorContext,
833     "SAX.attribute(%s, %s)\n", fullname, value);
834 #endif
835  ****************/
836     /*
837      * Split the full name into a namespace prefix and the tag name
838      */
839     name = xmlSplitQName(ctxt, fullname, &ns);
840
841     /*
842      * Do the last stage of the attribute normalization
843      * Needed for HTML too:
844      *   http://www.w3.org/TR/html4/types.html#h-6.2
845      */
846     ctxt->vctxt.valid = 1;
847     nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
848                                            ctxt->myDoc, ctxt->node,
849                                            fullname, value);
850     if (ctxt->vctxt.valid != 1) {
851         ctxt->valid = 0;
852     }
853     if (nval != NULL)
854         value = nval;
855
856     /*
857      * Check whether it's a namespace definition
858      */
859     if ((!ctxt->html) && (ns == NULL) &&
860         (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
861         (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
862         xmlNsPtr nsret;
863
864         if (value[0] != 0) {
865             xmlURIPtr uri;
866
867             uri = xmlParseURI((const char *)value);
868             if (uri == NULL) {
869                 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
870                     ctxt->sax->warning(ctxt->userData, 
871                          "nmlns: %s not a valid URI\n", value);
872             } else {
873                 if (uri->scheme == NULL) {
874                     if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
875                         ctxt->sax->warning(ctxt->userData, 
876                              "xmlns: URI %s is not absolute\n", value);
877                 }
878                 xmlFreeURI(uri);
879             }
880         }
881
882         /* a default namespace definition */
883         nsret = xmlNewNs(ctxt->node, value, NULL);
884
885         /*
886          * Validate also for namespace decls, they are attributes from
887          * an XML-1.0 perspective
888          */
889         if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
890             ctxt->myDoc && ctxt->myDoc->intSubset)
891             ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
892                                            ctxt->node, prefix, nsret, value);
893         if (name != NULL) 
894             xmlFree(name);
895         if (nval != NULL)
896             xmlFree(nval);
897         return;
898     }
899     if ((!ctxt->html) &&
900         (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
901         (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
902         xmlNsPtr nsret;
903
904         if (value[0] == 0) {
905             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
906                 ctxt->sax->error(ctxt->userData, 
907                      "Empty namespace name for prefix %s\n", name);
908         }
909         if ((ctxt->pedantic != 0) && (value[0] != 0)) {
910             xmlURIPtr uri;
911
912             uri = xmlParseURI((const char *)value);
913             if (uri == NULL) {
914                 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
915                     ctxt->sax->warning(ctxt->userData, 
916                          "xmlns:%s: %s not a valid URI\n", name, value);
917             } else {
918                 if (uri->scheme == NULL) {
919                     if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
920                         ctxt->sax->warning(ctxt->userData, 
921                            "xmlns:%s: URI %s is not absolute\n", name, value);
922                 }
923                 xmlFreeURI(uri);
924             }
925         }
926
927         /* a standard namespace definition */
928         nsret = xmlNewNs(ctxt->node, value, name);
929         xmlFree(ns);
930         /*
931          * Validate also for namespace decls, they are attributes from
932          * an XML-1.0 perspective
933          */
934         if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
935             ctxt->myDoc && ctxt->myDoc->intSubset)
936             ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
937                                            ctxt->node, name, nsret, value);
938         if (name != NULL) 
939             xmlFree(name);
940         if (nval != NULL)
941             xmlFree(nval);
942         return;
943     }
944
945     if (ns != NULL) {
946         xmlAttrPtr prop;
947         namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
948
949         prop = ctxt->node->properties;
950         while (prop != NULL) {
951             if (prop->ns != NULL) {
952                 if ((xmlStrEqual(name, prop->name)) &&
953                     ((namespace == prop->ns) ||
954                      (xmlStrEqual(namespace->href, prop->ns->href)))) {
955                     ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
956                     if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
957                         ctxt->sax->error(ctxt->userData,
958                                 "Attribute %s in %s redefined\n",
959                                          name, namespace->href);
960                     ctxt->wellFormed = 0;
961                     if (ctxt->recovery == 0) ctxt->disableSAX = 1;
962                     goto error;
963                 }
964             }
965             prop = prop->next;
966         }
967     } else {
968         namespace = NULL;
969     }
970
971     /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
972     ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
973
974     if (ret != NULL) {
975         if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
976             xmlNodePtr tmp;
977
978             ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
979             tmp = ret->children;
980             while (tmp != NULL) {
981                 tmp->parent = (xmlNodePtr) ret;
982                 if (tmp->next == NULL)
983                     ret->last = tmp;
984                 tmp = tmp->next;
985             }
986         } else if (value != NULL) {
987             ret->children = xmlNewDocText(ctxt->myDoc, value);
988             ret->last = ret->children;
989             if (ret->children != NULL)
990                 ret->children->parent = (xmlNodePtr) ret;
991         }
992     }
993
994     if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
995         ctxt->myDoc && ctxt->myDoc->intSubset) {
996         
997         /*
998          * If we don't substitute entities, the validation should be
999          * done on a value with replaced entities anyway.
1000          */
1001         if (!ctxt->replaceEntities) {
1002             xmlChar *val;
1003
1004             ctxt->depth++;
1005             val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1006                                           0,0,0);
1007             ctxt->depth--;
1008             
1009             if (val == NULL)
1010                 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1011                                 ctxt->myDoc, ctxt->node, ret, value);
1012             else {
1013                 xmlChar *nvalnorm;
1014
1015                 /*
1016                  * Do the last stage of the attribute normalization
1017                  * It need to be done twice ... it's an extra burden related
1018                  * to the ability to keep references in attributes
1019                  */
1020                 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1021                                             ctxt->node, fullname, val);
1022                 if (nvalnorm != NULL) {
1023                     xmlFree(val);
1024                     val = nvalnorm;
1025                 }
1026
1027                 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1028                                 ctxt->myDoc, ctxt->node, ret, val);
1029                 xmlFree(val);
1030             }
1031         } else {
1032             ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1033                                                ctxt->node, ret, value);
1034         }
1035     } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1036                ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) {
1037         /*
1038          * when validating, the ID registration is done at the attribute
1039          * validation level. Otherwise we have to do specific handling here.
1040          */
1041         if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1042             xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1043         else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1044             xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1045     }
1046
1047 error:
1048     if (nval != NULL)
1049         xmlFree(nval);
1050     if (ns != NULL) 
1051         xmlFree(ns);
1052 }
1053
1054 /**
1055  * attribute:
1056  * @ctx: the user data (XML parser context)
1057  * @fullname:  The attribute name, including namespace prefix
1058  * @value:  The attribute value
1059  *
1060  * Handle an attribute that has been read by the parser.
1061  * The default handling is to convert the attribute into an
1062  * DOM subtree and past it in a new xmlAttr element added to
1063  * the element.
1064  */
1065 void
1066 attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1067 {
1068     my_attribute(ctx, fullname, value, NULL);
1069 }
1070
1071 /*
1072  * xmlCheckDefaultedAttributes:
1073  *
1074  * Check defaulted attributes from the DTD
1075  */
1076 static void
1077 xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1078         const xmlChar *prefix, const xmlChar **atts) {
1079     xmlElementPtr elemDecl;
1080     const xmlChar *att;
1081     int internal = 1;
1082     int i;
1083
1084     elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1085     if (elemDecl == NULL) {
1086         elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1087         internal = 0;
1088     }
1089
1090 process_external_subset:
1091
1092     if (elemDecl != NULL) {
1093         xmlAttributePtr attr = elemDecl->attributes;
1094         /*
1095          * Check against defaulted attributes from the external subset
1096          * if the document is stamped as standalone
1097          */
1098         if ((ctxt->myDoc->standalone == 1) &&
1099             (ctxt->myDoc->extSubset != NULL) &&
1100             (ctxt->validate)) {
1101             while (attr != NULL) {
1102                 if ((attr->defaultValue != NULL) &&
1103                     (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1104                                         attr->elem, attr->name,
1105                                         attr->prefix) == attr) &&
1106                     (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1107                                         attr->elem, attr->name,
1108                                         attr->prefix) == NULL)) {
1109                     xmlChar *fulln;
1110
1111                     if (attr->prefix != NULL) {
1112                         fulln = xmlStrdup(attr->prefix);
1113                         fulln = xmlStrcat(fulln, BAD_CAST ":");
1114                         fulln = xmlStrcat(fulln, attr->name);
1115                     } else {
1116                         fulln = xmlStrdup(attr->name);
1117                     }
1118
1119                     /*
1120                      * Check that the attribute is not declared in the
1121                      * serialization
1122                      */
1123                     att = NULL;
1124                     if (atts != NULL) {
1125                         i = 0;
1126                         att = atts[i];
1127                         while (att != NULL) {
1128                             if (xmlStrEqual(att, fulln))
1129                                 break;
1130                             i += 2;
1131                             att = atts[i];
1132                         }
1133                     }
1134                     if (att == NULL) {
1135                         if (ctxt->vctxt.error != NULL)
1136                             ctxt->vctxt.error(ctxt->vctxt.userData,
1137       "standalone: attribute %s on %s defaulted from external subset\n",
1138                                               fulln, attr->elem);
1139                         ctxt->valid = 0;
1140                     }
1141                 }
1142                 attr = attr->nexth;
1143             }
1144         }
1145
1146         /*
1147          * Actually insert defaulted values when needed
1148          */
1149         attr = elemDecl->attributes;
1150         while (attr != NULL) {
1151             /*
1152              * Make sure that attributes redefinition occuring in the
1153              * internal subset are not overriden by definitions in the
1154              * external subset.
1155              */
1156             if (attr->defaultValue != NULL) {
1157                 /*
1158                  * the element should be instantiated in the tree if:
1159                  *  - this is a namespace prefix
1160                  *  - the user required for completion in the tree
1161                  *    like XSLT
1162                  *  - there isn't already an attribute definition 
1163                  *    in the internal subset overriding it.
1164                  */
1165                 if (((attr->prefix != NULL) &&
1166                      (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1167                     ((attr->prefix == NULL) &&
1168                      (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1169                     (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1170                     xmlAttributePtr tst;
1171
1172                     tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1173                                              attr->elem, attr->name,
1174                                              attr->prefix);
1175                     if ((tst == attr) || (tst == NULL)) {
1176                         xmlChar *fulln;
1177
1178                         if (attr->prefix != NULL) {
1179                             fulln = xmlStrdup(attr->prefix);
1180                             fulln = xmlStrcat(fulln, BAD_CAST ":");
1181                             fulln = xmlStrcat(fulln, attr->name);
1182                         } else {
1183                             fulln = xmlStrdup(attr->name);
1184                         }
1185
1186                         /*
1187                          * Check that the attribute is not declared in the
1188                          * serialization
1189                          */
1190                         att = NULL;
1191                         if (atts != NULL) {
1192                             i = 0;
1193                             att = atts[i];
1194                             while (att != NULL) {
1195                                 if (xmlStrEqual(att, fulln))
1196                                     break;
1197                                 i += 2;
1198                                 att = atts[i];
1199                             }
1200                         }
1201                         if (att == NULL) {
1202                             attribute(ctxt, fulln, attr->defaultValue);
1203                         }
1204                         xmlFree(fulln);
1205                     }
1206                 }
1207             }
1208             attr = attr->nexth;
1209         }
1210         if (internal == 1) {
1211             elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1212                                              name, prefix);
1213             internal = 0;
1214             goto process_external_subset;
1215         }
1216     }
1217 }
1218
1219 /**
1220  * startElement:
1221  * @ctx: the user data (XML parser context)
1222  * @fullname:  The element name, including namespace prefix
1223  * @atts:  An array of name/value attributes pairs, NULL terminated
1224  *
1225  * called when an opening tag has been processed.
1226  */
1227 void
1228 startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1229 {
1230     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1231     xmlNodePtr ret;
1232     xmlNodePtr parent = ctxt->node;
1233     xmlNsPtr ns;
1234     xmlChar *name;
1235     xmlChar *prefix;
1236     const xmlChar *att;
1237     const xmlChar *value;
1238     int i;
1239
1240 #ifdef DEBUG_SAX
1241     xmlGenericError(xmlGenericErrorContext,
1242             "SAX.startElement(%s)\n", fullname);
1243 #endif
1244
1245     /*
1246      * First check on validity:
1247      */
1248     if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) && 
1249         ((ctxt->myDoc->intSubset == NULL) ||
1250          ((ctxt->myDoc->intSubset->notations == NULL) && 
1251           (ctxt->myDoc->intSubset->elements == NULL) &&
1252           (ctxt->myDoc->intSubset->attributes == NULL) && 
1253           (ctxt->myDoc->intSubset->entities == NULL)))) {
1254         if (ctxt->vctxt.error != NULL) {
1255             ctxt->vctxt.error(ctxt->vctxt.userData,
1256               "Validation failed: no DTD found !\n");
1257         }
1258         ctxt->validate = 0;
1259         ctxt->valid = 0;
1260         ctxt->errNo = XML_ERR_NO_DTD;
1261     }
1262        
1263
1264     /*
1265      * Split the full name into a namespace prefix and the tag name
1266      */
1267     name = xmlSplitQName(ctxt, fullname, &prefix);
1268
1269
1270     /*
1271      * Note : the namespace resolution is deferred until the end of the
1272      *        attributes parsing, since local namespace can be defined as
1273      *        an attribute at this level.
1274      */
1275     ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1276     if (ret == NULL) return;
1277     if (ctxt->myDoc->children == NULL) {
1278 #ifdef DEBUG_SAX_TREE
1279         xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1280 #endif
1281         xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1282     } else if (parent == NULL) {
1283         parent = ctxt->myDoc->children;
1284     }
1285     ctxt->nodemem = -1;
1286     if (ctxt->linenumbers) {
1287         if (ctxt->input != NULL)
1288             ret->content = (void *) (long) ctxt->input->line;
1289     }
1290
1291     /*
1292      * We are parsing a new node.
1293      */
1294 #ifdef DEBUG_SAX_TREE
1295     xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1296 #endif
1297     nodePush(ctxt, ret);
1298
1299     /*
1300      * Link the child element
1301      */
1302     if (parent != NULL) {
1303         if (parent->type == XML_ELEMENT_NODE) {
1304 #ifdef DEBUG_SAX_TREE
1305             xmlGenericError(xmlGenericErrorContext,
1306                     "adding child %s to %s\n", name, parent->name);
1307 #endif
1308             xmlAddChild(parent, ret);
1309         } else {
1310 #ifdef DEBUG_SAX_TREE
1311             xmlGenericError(xmlGenericErrorContext,
1312                     "adding sibling %s to ", name);
1313             xmlDebugDumpOneNode(stderr, parent, 0);
1314 #endif
1315             xmlAddSibling(parent, ret);
1316         }
1317     }
1318
1319     /*
1320      * Insert all the defaulted attributes from the DTD especially namespaces
1321      */
1322     if ((!ctxt->html) &&
1323         ((ctxt->myDoc->intSubset != NULL) ||
1324          (ctxt->myDoc->extSubset != NULL))) {
1325         xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1326     }
1327
1328     /*
1329      * process all the attributes whose name start with "xmlns"
1330      */
1331     if (atts != NULL) {
1332         i = 0;
1333         att = atts[i++];
1334         value = atts[i++];
1335         if (!ctxt->html) {
1336             while ((att != NULL) && (value != NULL)) {
1337                 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1338                     (att[3] == 'n') && (att[4] == 's'))
1339                     my_attribute(ctxt, att, value, prefix);
1340
1341                 att = atts[i++];
1342                 value = atts[i++];
1343             }
1344         }
1345     }
1346
1347     /*
1348      * Search the namespace, note that since the attributes have been
1349      * processed, the local namespaces are available.
1350      */
1351     ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1352     if ((ns == NULL) && (parent != NULL))
1353         ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1354     if ((prefix != NULL) && (ns == NULL)) {
1355         ns = xmlNewNs(ret, NULL, prefix);
1356         if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1357             ctxt->sax->warning(ctxt->userData, 
1358                  "Namespace prefix %s is not defined\n", prefix);
1359     }
1360
1361     /*
1362      * set the namespace node, making sure that if the default namspace
1363      * is unbound on a parent we simply kee it NULL
1364      */
1365     if ((ns != NULL) && (ns->href != NULL) &&
1366         ((ns->href[0] != 0) || (ns->prefix != NULL)))
1367         xmlSetNs(ret, ns);
1368
1369     /*
1370      * process all the other attributes
1371      */
1372     if (atts != NULL) {
1373         i = 0;
1374         att = atts[i++];
1375         value = atts[i++];
1376         if (ctxt->html) {
1377             while (att != NULL) {
1378                 attribute(ctxt, att, value);
1379                 att = atts[i++];
1380                 value = atts[i++];
1381             }
1382         } else {
1383             while ((att != NULL) && (value != NULL)) {
1384                 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1385                     (att[3] != 'n') || (att[4] != 's'))
1386                     attribute(ctxt, att, value);
1387
1388                 /*
1389                  * Next ones
1390                  */
1391                 att = atts[i++];
1392                 value = atts[i++];
1393             }
1394         }
1395     }
1396
1397     /*
1398      * If it's the Document root, finish the DTD validation and
1399      * check the document root element for validity
1400      */
1401     if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
1402         int chk;
1403
1404         chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1405         if (chk <= 0)
1406             ctxt->valid = 0;
1407         if (chk < 0)
1408             ctxt->wellFormed = 0;
1409         ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1410         ctxt->vctxt.finishDtd = 1;
1411     }
1412
1413     if (prefix != NULL)
1414         xmlFree(prefix);
1415
1416 }
1417
1418 /**
1419  * endElement:
1420  * @ctx: the user data (XML parser context)
1421  * @name:  The element name
1422  *
1423  * called when the end of an element has been detected.
1424  */
1425 void
1426 endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1427 {
1428     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1429     xmlParserNodeInfo node_info;
1430     xmlNodePtr cur = ctxt->node;
1431
1432 #ifdef DEBUG_SAX
1433     if (name == NULL)
1434         xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1435     else
1436         xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1437 #endif
1438     
1439     /* Capture end position and add node */
1440     if (cur != NULL && ctxt->record_info) {
1441       node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1442       node_info.end_line = ctxt->input->line;
1443       node_info.node = cur;
1444       xmlParserAddNodeInfo(ctxt, &node_info);
1445     }
1446     ctxt->nodemem = -1;
1447
1448     if (ctxt->validate && ctxt->wellFormed &&
1449         ctxt->myDoc && ctxt->myDoc->intSubset)
1450         ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1451                                              cur);
1452
1453     
1454     /*
1455      * end of parsing of this node.
1456      */
1457 #ifdef DEBUG_SAX_TREE
1458     xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1459 #endif
1460     nodePop(ctxt);
1461 }
1462
1463 /**
1464  * reference:
1465  * @ctx: the user data (XML parser context)
1466  * @name:  The entity name
1467  *
1468  * called when an entity reference is detected. 
1469  */
1470 void
1471 reference(void *ctx, const xmlChar *name)
1472 {
1473     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1474     xmlNodePtr ret;
1475
1476 #ifdef DEBUG_SAX
1477     xmlGenericError(xmlGenericErrorContext,
1478             "SAX.reference(%s)\n", name);
1479 #endif
1480     if (name[0] == '#')
1481         ret = xmlNewCharRef(ctxt->myDoc, name);
1482     else
1483         ret = xmlNewReference(ctxt->myDoc, name);
1484 #ifdef DEBUG_SAX_TREE
1485     xmlGenericError(xmlGenericErrorContext,
1486             "add reference %s to %s \n", name, ctxt->node->name);
1487 #endif
1488     xmlAddChild(ctxt->node, ret);
1489 }
1490
1491 /**
1492  * characters:
1493  * @ctx: the user data (XML parser context)
1494  * @ch:  a xmlChar string
1495  * @len: the number of xmlChar
1496  *
1497  * receiving some chars from the parser.
1498  */
1499 void
1500 characters(void *ctx, const xmlChar *ch, int len)
1501 {
1502     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1503     xmlNodePtr lastChild;
1504
1505 #ifdef DEBUG_SAX
1506     xmlGenericError(xmlGenericErrorContext,
1507             "SAX.characters(%.30s, %d)\n", ch, len);
1508 #endif
1509     /*
1510      * Handle the data if any. If there is no child
1511      * add it as content, otherwise if the last child is text,
1512      * concatenate it, else create a new node of type text.
1513      */
1514
1515     if (ctxt->node == NULL) {
1516 #ifdef DEBUG_SAX_TREE
1517         xmlGenericError(xmlGenericErrorContext,
1518                 "add chars: ctxt->node == NULL !\n");
1519 #endif
1520         return;
1521     }
1522     lastChild = xmlGetLastChild(ctxt->node);
1523 #ifdef DEBUG_SAX_TREE
1524     xmlGenericError(xmlGenericErrorContext,
1525             "add chars to %s \n", ctxt->node->name);
1526 #endif
1527
1528     /*
1529      * Here we needed an accelerator mechanism in case of very large
1530      * elements. Use an attribute in the structure !!!
1531      */
1532     if (lastChild == NULL) {
1533         /* first node, first time */
1534         xmlNodeAddContentLen(ctxt->node, ch, len);
1535         if (ctxt->node->children != NULL) {
1536             ctxt->nodelen = len;
1537             ctxt->nodemem = len + 1;
1538         }
1539     } else {
1540         int coalesceText = (lastChild != NULL) &&
1541             (lastChild->type == XML_TEXT_NODE) &&
1542             (lastChild->name == xmlStringText);
1543         if ((coalesceText) && (ctxt->nodemem != 0)) {
1544             /*
1545              * The whole point of maintaining nodelen and nodemem,
1546              * xmlTextConcat is too costly, i.e. compute length,
1547              * reallocate a new buffer, move data, append ch. Here
1548              * We try to minimaze realloc() uses and avoid copying
1549              * and recomputing length over and over.
1550              */
1551             if (ctxt->nodelen + len >= ctxt->nodemem) {
1552                 xmlChar *newbuf;
1553                 int size;
1554
1555                 size = ctxt->nodemem + len;
1556                 size *= 2;
1557                 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1558                 if (newbuf == NULL) {
1559                     if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1560                         ctxt->sax->error(ctxt->userData, 
1561                              "SAX.characters(): out of memory\n");
1562                     return;
1563                 }
1564                 ctxt->nodemem = size;
1565                 lastChild->content = newbuf;
1566             }
1567             memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1568             ctxt->nodelen += len;
1569             lastChild->content[ctxt->nodelen] = 0;
1570         } else if (coalesceText) {
1571             xmlTextConcat(lastChild, ch, len);
1572             if (ctxt->node->children != NULL) {
1573                 ctxt->nodelen = xmlStrlen(lastChild->content);
1574                 ctxt->nodemem = ctxt->nodelen + 1;
1575             }
1576         } else {
1577             /* Mixed content, first time */
1578             lastChild = xmlNewTextLen(ch, len);
1579             xmlAddChild(ctxt->node, lastChild);
1580             if (ctxt->node->children != NULL) {
1581                 ctxt->nodelen = len;
1582                 ctxt->nodemem = len + 1;
1583             }
1584         }
1585     }
1586 }
1587
1588 /**
1589  * ignorableWhitespace:
1590  * @ctx: the user data (XML parser context)
1591  * @ch:  a xmlChar string
1592  * @len: the number of xmlChar
1593  *
1594  * receiving some ignorable whitespaces from the parser.
1595  * UNUSED: by default the DOM building will use characters
1596  */
1597 void
1598 ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
1599 {
1600     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1601 #ifdef DEBUG_SAX
1602     xmlGenericError(xmlGenericErrorContext,
1603             "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1604 #endif
1605 }
1606
1607 /**
1608  * processingInstruction:
1609  * @ctx: the user data (XML parser context)
1610  * @target:  the target name
1611  * @data: the PI data's
1612  *
1613  * A processing instruction has been parsed.
1614  */
1615 void
1616 processingInstruction(void *ctx, const xmlChar *target,
1617                       const xmlChar *data)
1618 {
1619     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1620     xmlNodePtr ret;
1621     xmlNodePtr parent = ctxt->node;
1622
1623 #ifdef DEBUG_SAX
1624     xmlGenericError(xmlGenericErrorContext,
1625             "SAX.processingInstruction(%s, %s)\n", target, data);
1626 #endif
1627
1628     ret = xmlNewPI(target, data);
1629     if (ret == NULL) return;
1630     parent = ctxt->node;
1631
1632     if (ctxt->inSubset == 1) {
1633         xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1634         return;
1635     } else if (ctxt->inSubset == 2) {
1636         xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1637         return;
1638     }
1639     if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1640 #ifdef DEBUG_SAX_TREE
1641             xmlGenericError(xmlGenericErrorContext,
1642                     "Setting PI %s as root\n", target);
1643 #endif
1644         xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1645         return;
1646     }
1647     if (parent->type == XML_ELEMENT_NODE) {
1648 #ifdef DEBUG_SAX_TREE
1649         xmlGenericError(xmlGenericErrorContext,
1650                 "adding PI %s child to %s\n", target, parent->name);
1651 #endif
1652         xmlAddChild(parent, ret);
1653     } else {
1654 #ifdef DEBUG_SAX_TREE
1655         xmlGenericError(xmlGenericErrorContext,
1656                 "adding PI %s sibling to ", target);
1657         xmlDebugDumpOneNode(stderr, parent, 0);
1658 #endif
1659         xmlAddSibling(parent, ret);
1660     }
1661 }
1662
1663 /**
1664  * globalNamespace:
1665  * @ctx: the user data (XML parser context)
1666  * @href:  the namespace associated URN
1667  * @prefix: the namespace prefix
1668  *
1669  * An old global namespace has been parsed.
1670  */
1671 void
1672 globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1673 {
1674     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1675 #ifdef DEBUG_SAX
1676     xmlGenericError(xmlGenericErrorContext,
1677             "SAX.globalNamespace(%s, %s)\n", href, prefix);
1678 #endif
1679     xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1680 }
1681
1682 /**
1683  * setNamespace:
1684  * @ctx: the user data (XML parser context)
1685  * @name:  the namespace prefix
1686  *
1687  * Set the current element namespace.
1688  */
1689
1690 void
1691 setNamespace(void *ctx, const xmlChar *name)
1692 {
1693     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1694     xmlNsPtr ns;
1695     xmlNodePtr parent;
1696
1697 #ifdef DEBUG_SAX
1698     xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1699 #endif
1700     ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1701     if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1702         if (ctxt->nodeNr >= 2) {
1703             parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1704             if (parent != NULL)
1705                 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1706         }
1707     }
1708     xmlSetNs(ctxt->node, ns);
1709 }
1710
1711 /**
1712  * getNamespace:
1713  * @ctx: the user data (XML parser context)
1714  *
1715  * Get the current element namespace.
1716  *
1717  * Returns the xmlNsPtr or NULL if none
1718  */
1719
1720 xmlNsPtr
1721 getNamespace(void *ctx)
1722 {
1723     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1724     xmlNsPtr ret;
1725
1726 #ifdef DEBUG_SAX
1727     xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1728 #endif
1729     ret = ctxt->node->ns;
1730     return(ret);
1731 }
1732
1733 /**
1734  * checkNamespace:
1735  * @ctx: the user data (XML parser context)
1736  * @namespace: the namespace to check against
1737  *
1738  * Check that the current element namespace is the same as the
1739  * one read upon parsing.
1740  *
1741  * Returns 1 if true 0 otherwise
1742  */
1743
1744 int
1745 checkNamespace(void *ctx, xmlChar *namespace)
1746 {
1747     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1748     xmlNodePtr cur = ctxt->node;
1749
1750 #ifdef DEBUG_SAX
1751     xmlGenericError(xmlGenericErrorContext,
1752             "SAX.checkNamespace(%s)\n", namespace);
1753 #endif
1754
1755     /*
1756      * Check that the Name in the ETag is the same as in the STag.
1757      */
1758     if (namespace == NULL) {
1759         if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1760             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1761                 ctxt->sax->error(ctxt, 
1762                  "End tags for %s don't hold the namespace %s\n",
1763                                  cur->name, cur->ns->prefix);
1764             ctxt->wellFormed = 0;
1765         }
1766     } else {
1767         if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1768             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1769                 ctxt->sax->error(ctxt, 
1770                  "End tags %s holds a prefix %s not used by the open tag\n",
1771                                  cur->name, namespace);
1772             ctxt->wellFormed = 0;
1773         } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1774             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1775                 ctxt->sax->error(ctxt, 
1776     "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1777                                  cur->name, cur->ns->prefix, namespace);
1778             ctxt->wellFormed = 0;
1779         } else
1780             return(1);
1781     }
1782     return(0);
1783 }
1784
1785 /**
1786  * namespaceDecl:
1787  * @ctx: the user data (XML parser context)
1788  * @href:  the namespace associated URN
1789  * @prefix: the namespace prefix
1790  *
1791  * A namespace has been parsed.
1792  */
1793 void
1794 namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1795 {
1796     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1797 #ifdef DEBUG_SAX
1798     if (prefix == NULL)
1799         xmlGenericError(xmlGenericErrorContext,
1800                 "SAX.namespaceDecl(%s, NULL)\n", href);
1801     else
1802         xmlGenericError(xmlGenericErrorContext,
1803                 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1804 #endif
1805     xmlNewNs(ctxt->node, href, prefix);
1806 }
1807
1808 /**
1809  * comment:
1810  * @ctx: the user data (XML parser context)
1811  * @value:  the comment content
1812  *
1813  * A comment has been parsed.
1814  */
1815 void
1816 comment(void *ctx, const xmlChar *value)
1817 {
1818     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1819     xmlNodePtr ret;
1820     xmlNodePtr parent = ctxt->node;
1821
1822 #ifdef DEBUG_SAX
1823     xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1824 #endif
1825     ret = xmlNewDocComment(ctxt->myDoc, value);
1826     if (ret == NULL) return;
1827
1828     if (ctxt->inSubset == 1) {
1829         xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1830         return;
1831     } else if (ctxt->inSubset == 2) {
1832         xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1833         return;
1834     }
1835     if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1836 #ifdef DEBUG_SAX_TREE
1837             xmlGenericError(xmlGenericErrorContext,
1838                     "Setting comment as root\n");
1839 #endif
1840         xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1841         return;
1842     }
1843     if (parent->type == XML_ELEMENT_NODE) {
1844 #ifdef DEBUG_SAX_TREE
1845         xmlGenericError(xmlGenericErrorContext,
1846                 "adding comment child to %s\n", parent->name);
1847 #endif
1848         xmlAddChild(parent, ret);
1849     } else {
1850 #ifdef DEBUG_SAX_TREE
1851         xmlGenericError(xmlGenericErrorContext,
1852                 "adding comment sibling to ");
1853         xmlDebugDumpOneNode(stderr, parent, 0);
1854 #endif
1855         xmlAddSibling(parent, ret);
1856     }
1857 }
1858
1859 /**
1860  * cdataBlock:
1861  * @ctx: the user data (XML parser context)
1862  * @value:  The pcdata content
1863  * @len:  the block length
1864  *
1865  * called when a pcdata block has been parsed
1866  */
1867 void
1868 cdataBlock(void *ctx, const xmlChar *value, int len)
1869 {
1870     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1871     xmlNodePtr ret, lastChild;
1872
1873 #ifdef DEBUG_SAX
1874     xmlGenericError(xmlGenericErrorContext,
1875             "SAX.pcdata(%.10s, %d)\n", value, len);
1876 #endif
1877     lastChild = xmlGetLastChild(ctxt->node);
1878 #ifdef DEBUG_SAX_TREE
1879     xmlGenericError(xmlGenericErrorContext,
1880             "add chars to %s \n", ctxt->node->name);
1881 #endif
1882     if ((lastChild != NULL) &&
1883         (lastChild->type == XML_CDATA_SECTION_NODE)) {
1884         xmlTextConcat(lastChild, value, len);
1885     } else {
1886         ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1887         xmlAddChild(ctxt->node, ret);
1888     }
1889 }
1890
1891 /**
1892  * initxmlDefaultSAXHandler:
1893  * @hdlr:  the SAX handler
1894  * @warning:  flag if non-zero sets the handler warning procedure
1895  *
1896  * Initialize the default XML SAX handler
1897  */
1898 void
1899 initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1900 {
1901     if(hdlr->initialized == 1)
1902         return;
1903
1904     hdlr->internalSubset = internalSubset;
1905     hdlr->externalSubset = externalSubset;
1906     hdlr->isStandalone = isStandalone;
1907     hdlr->hasInternalSubset = hasInternalSubset;
1908     hdlr->hasExternalSubset = hasExternalSubset;
1909     hdlr->resolveEntity = resolveEntity;
1910     hdlr->getEntity = getEntity;
1911     hdlr->getParameterEntity = getParameterEntity;
1912     hdlr->entityDecl = entityDecl;
1913     hdlr->attributeDecl = attributeDecl;
1914     hdlr->elementDecl = elementDecl;
1915     hdlr->notationDecl = notationDecl;
1916     hdlr->unparsedEntityDecl = unparsedEntityDecl;
1917     hdlr->setDocumentLocator = setDocumentLocator;
1918     hdlr->startDocument = startDocument;
1919     hdlr->endDocument = endDocument;
1920     hdlr->startElement = startElement;
1921     hdlr->endElement = endElement;
1922     hdlr->reference = reference;
1923     hdlr->characters = characters;
1924     hdlr->cdataBlock = cdataBlock;
1925     hdlr->ignorableWhitespace = characters;
1926     hdlr->processingInstruction = processingInstruction;
1927     hdlr->comment = comment;
1928     /* if (xmlGetWarningsDefaultValue == 0) */
1929     if (warning == 0)
1930         hdlr->warning = NULL;
1931     else
1932         hdlr->warning = xmlParserWarning;
1933     hdlr->error = xmlParserError;
1934     hdlr->fatalError = xmlParserError;
1935
1936     hdlr->initialized = 1;
1937 }
1938
1939 /**
1940  * xmlDefaultSAXHandlerInit:
1941  *
1942  * Initialize the default SAX handler
1943  */
1944 void
1945 xmlDefaultSAXHandlerInit(void)
1946 {
1947     initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
1948 }
1949
1950 #ifdef LIBXML_HTML_ENABLED
1951
1952 /**
1953  * inithtmlDefaultSAXHandler:
1954  * @hdlr:  the SAX handler
1955  *
1956  * Initialize the default HTML SAX handler
1957  */
1958 void
1959 inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1960 {
1961     if(hdlr->initialized == 1)
1962         return;
1963
1964     hdlr->internalSubset = internalSubset;
1965     hdlr->externalSubset = NULL;
1966     hdlr->isStandalone = NULL;
1967     hdlr->hasInternalSubset = NULL;
1968     hdlr->hasExternalSubset = NULL;
1969     hdlr->resolveEntity = NULL;
1970     hdlr->getEntity = getEntity;
1971     hdlr->getParameterEntity = NULL;
1972     hdlr->entityDecl = NULL;
1973     hdlr->attributeDecl = NULL;
1974     hdlr->elementDecl = NULL;
1975     hdlr->notationDecl = NULL;
1976     hdlr->unparsedEntityDecl = NULL;
1977     hdlr->setDocumentLocator = setDocumentLocator;
1978     hdlr->startDocument = startDocument;
1979     hdlr->endDocument = endDocument;
1980     hdlr->startElement = startElement;
1981     hdlr->endElement = endElement;
1982     hdlr->reference = NULL;
1983     hdlr->characters = characters;
1984     hdlr->cdataBlock = cdataBlock;
1985     hdlr->ignorableWhitespace = ignorableWhitespace;
1986     hdlr->processingInstruction = NULL;
1987     hdlr->comment = comment;
1988     hdlr->warning = xmlParserWarning;
1989     hdlr->error = xmlParserError;
1990     hdlr->fatalError = xmlParserError;
1991
1992     hdlr->initialized = 1;
1993 }
1994
1995 /**
1996  * htmlDefaultSAXHandlerInit:
1997  *
1998  * Initialize the default SAX handler
1999  */
2000 void
2001 htmlDefaultSAXHandlerInit(void)
2002 {
2003         inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
2004 }
2005
2006 #endif /* LIBXML_HTML_ENABLED */
2007
2008 #ifdef LIBXML_DOCB_ENABLED
2009
2010 /**
2011  * initdocbDefaultSAXHandler:
2012  * @hdlr:  the SAX handler
2013  *
2014  * Initialize the default DocBook SAX handler
2015  */
2016 void
2017 initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2018 {
2019     if(hdlr->initialized == 1)
2020         return;
2021
2022     hdlr->internalSubset = internalSubset;
2023     hdlr->externalSubset = NULL;
2024     hdlr->isStandalone = isStandalone;
2025     hdlr->hasInternalSubset = hasInternalSubset;
2026     hdlr->hasExternalSubset = hasExternalSubset;
2027     hdlr->resolveEntity = resolveEntity;
2028     hdlr->getEntity = getEntity;
2029     hdlr->getParameterEntity = NULL;
2030     hdlr->entityDecl = entityDecl;
2031     hdlr->attributeDecl = NULL;
2032     hdlr->elementDecl = NULL;
2033     hdlr->notationDecl = NULL;
2034     hdlr->unparsedEntityDecl = NULL;
2035     hdlr->setDocumentLocator = setDocumentLocator;
2036     hdlr->startDocument = startDocument;
2037     hdlr->endDocument = endDocument;
2038     hdlr->startElement = startElement;
2039     hdlr->endElement = endElement;
2040     hdlr->reference = reference;
2041     hdlr->characters = characters;
2042     hdlr->cdataBlock = NULL;
2043     hdlr->ignorableWhitespace = ignorableWhitespace;
2044     hdlr->processingInstruction = NULL;
2045     hdlr->comment = comment;
2046     hdlr->warning = xmlParserWarning;
2047     hdlr->error = xmlParserError;
2048     hdlr->fatalError = xmlParserError;
2049
2050     hdlr->initialized = 1;
2051 }
2052
2053 /**
2054  * docbDefaultSAXHandlerInit:
2055  *
2056  * Initialize the default SAX handler
2057  */
2058 void
2059 docbDefaultSAXHandlerInit(void)
2060 {
2061     initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
2062 }
2063
2064 #endif /* LIBXML_DOCB_ENABLED */