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