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