dbghelp: Properly fail on PDB files generated by MSVC compiler version 14.31.
[wine.git] / libs / xml2 / SAX2.c
blob031924652a465e3b28d964cf1af8b11f2bcc9675
1 /*
2 * SAX2.c : Default SAX2 handler to build a tree.
4 * See Copyright for the status of this software.
6 * Daniel Veillard <daniel@veillard.com>
7 */
10 #define IN_LIBXML
11 #include "libxml.h"
12 #include <stdlib.h>
13 #include <string.h>
14 #include <limits.h>
15 #include <stddef.h>
16 #include <libxml/xmlmemory.h>
17 #include <libxml/tree.h>
18 #include <libxml/parser.h>
19 #include <libxml/parserInternals.h>
20 #include <libxml/valid.h>
21 #include <libxml/entities.h>
22 #include <libxml/xmlerror.h>
23 #include <libxml/debugXML.h>
24 #include <libxml/xmlIO.h>
25 #include <libxml/SAX.h>
26 #include <libxml/uri.h>
27 #include <libxml/valid.h>
28 #include <libxml/HTMLtree.h>
29 #include <libxml/globals.h>
31 /* Define SIZE_T_MAX unless defined through <limits.h>. */
32 #ifndef SIZE_T_MAX
33 # define SIZE_T_MAX ((size_t)-1)
34 #endif /* !SIZE_T_MAX */
36 /* #define DEBUG_SAX2 */
37 /* #define DEBUG_SAX2_TREE */
39 /**
40 * TODO:
42 * macro to flag unimplemented blocks
43 * XML_CATALOG_PREFER user env to select between system/public preferred
44 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
45 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
46 *> values "system" and "public". I have made the default be "system" to
47 *> match yours.
49 #define TODO \
50 xmlGenericError(xmlGenericErrorContext, \
51 "Unimplemented block at %s:%d\n", \
52 __FILE__, __LINE__);
55 * xmlSAX2ErrMemory:
56 * @ctxt: an XML validation parser context
57 * @msg: a string to accompany the error message
59 static void LIBXML_ATTR_FORMAT(2,0)
60 xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
61 xmlStructuredErrorFunc schannel = NULL;
62 const char *str1 = "out of memory\n";
64 if (ctxt != NULL) {
65 ctxt->errNo = XML_ERR_NO_MEMORY;
66 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
67 schannel = ctxt->sax->serror;
68 __xmlRaiseError(schannel,
69 ctxt->vctxt.error, ctxt->vctxt.userData,
70 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
71 XML_ERR_ERROR, NULL, 0, (const char *) str1,
72 NULL, NULL, 0, 0,
73 msg, (const char *) str1, NULL);
74 ctxt->errNo = XML_ERR_NO_MEMORY;
75 ctxt->instate = XML_PARSER_EOF;
76 ctxt->disableSAX = 1;
77 } else {
78 __xmlRaiseError(schannel,
79 NULL, NULL,
80 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
81 XML_ERR_ERROR, NULL, 0, (const char *) str1,
82 NULL, NULL, 0, 0,
83 msg, (const char *) str1, NULL);
87 /**
88 * xmlValidError:
89 * @ctxt: an XML validation parser context
90 * @error: the error number
91 * @msg: the error message
92 * @str1: extra data
93 * @str2: extra data
95 * Handle a validation error
97 static void LIBXML_ATTR_FORMAT(3,0)
98 xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
99 const char *msg, const char *str1, const char *str2)
101 xmlStructuredErrorFunc schannel = NULL;
103 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
104 (ctxt->instate == XML_PARSER_EOF))
105 return;
106 if (ctxt != NULL) {
107 ctxt->errNo = error;
108 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
109 schannel = ctxt->sax->serror;
110 __xmlRaiseError(schannel,
111 ctxt->vctxt.error, ctxt->vctxt.userData,
112 ctxt, NULL, XML_FROM_DTD, error,
113 XML_ERR_ERROR, NULL, 0, (const char *) str1,
114 (const char *) str2, NULL, 0, 0,
115 msg, (const char *) str1, (const char *) str2);
116 ctxt->valid = 0;
117 } else {
118 __xmlRaiseError(schannel,
119 NULL, NULL,
120 ctxt, NULL, XML_FROM_DTD, error,
121 XML_ERR_ERROR, NULL, 0, (const char *) str1,
122 (const char *) str2, NULL, 0, 0,
123 msg, (const char *) str1, (const char *) str2);
128 * xmlFatalErrMsg:
129 * @ctxt: an XML parser context
130 * @error: the error number
131 * @msg: the error message
132 * @str1: an error string
133 * @str2: an error string
135 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
137 static void LIBXML_ATTR_FORMAT(3,0)
138 xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
139 const char *msg, const xmlChar *str1, const xmlChar *str2)
141 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
142 (ctxt->instate == XML_PARSER_EOF))
143 return;
144 if (ctxt != NULL)
145 ctxt->errNo = error;
146 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
147 XML_ERR_FATAL, NULL, 0,
148 (const char *) str1, (const char *) str2,
149 NULL, 0, 0, msg, str1, str2);
150 if (ctxt != NULL) {
151 ctxt->wellFormed = 0;
152 ctxt->valid = 0;
153 if (ctxt->recovery == 0)
154 ctxt->disableSAX = 1;
159 * xmlWarnMsg:
160 * @ctxt: an XML parser context
161 * @error: the error number
162 * @msg: the error message
163 * @str1: an error string
164 * @str2: an error string
166 * Handle a parser warning
168 static void LIBXML_ATTR_FORMAT(3,0)
169 xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
170 const char *msg, const xmlChar *str1)
172 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
173 (ctxt->instate == XML_PARSER_EOF))
174 return;
175 if (ctxt != NULL)
176 ctxt->errNo = error;
177 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
178 XML_ERR_WARNING, NULL, 0,
179 (const char *) str1, NULL,
180 NULL, 0, 0, msg, str1);
184 * xmlNsErrMsg:
185 * @ctxt: an XML parser context
186 * @error: the error number
187 * @msg: the error message
188 * @str1: an error string
189 * @str2: an error string
191 * Handle a namespace error
193 static void LIBXML_ATTR_FORMAT(3,0)
194 xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
195 const char *msg, const xmlChar *str1, const xmlChar *str2)
197 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
198 (ctxt->instate == XML_PARSER_EOF))
199 return;
200 if (ctxt != NULL)
201 ctxt->errNo = error;
202 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
203 XML_ERR_ERROR, NULL, 0,
204 (const char *) str1, (const char *) str2,
205 NULL, 0, 0, msg, str1, str2);
209 * xmlNsWarnMsg:
210 * @ctxt: an XML parser context
211 * @error: the error number
212 * @msg: the error message
213 * @str1: an error string
215 * Handle a namespace warning
217 static void LIBXML_ATTR_FORMAT(3,0)
218 xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
219 const char *msg, const xmlChar *str1, const xmlChar *str2)
221 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
222 (ctxt->instate == XML_PARSER_EOF))
223 return;
224 if (ctxt != NULL)
225 ctxt->errNo = error;
226 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
227 XML_ERR_WARNING, NULL, 0,
228 (const char *) str1, (const char *) str2,
229 NULL, 0, 0, msg, str1, str2);
233 * xmlSAX2GetPublicId:
234 * @ctx: the user data (XML parser context)
236 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
238 * Returns a xmlChar *
240 const xmlChar *
241 xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
243 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
244 return(NULL);
248 * xmlSAX2GetSystemId:
249 * @ctx: the user data (XML parser context)
251 * Provides the system ID, basically URL or filename e.g.
252 * http://www.sgmlsource.com/dtds/memo.dtd
254 * Returns a xmlChar *
256 const xmlChar *
257 xmlSAX2GetSystemId(void *ctx)
259 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
260 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
261 return((const xmlChar *) ctxt->input->filename);
265 * xmlSAX2GetLineNumber:
266 * @ctx: the user data (XML parser context)
268 * Provide the line number of the current parsing point.
270 * Returns an int
273 xmlSAX2GetLineNumber(void *ctx)
275 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
276 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
277 return(ctxt->input->line);
281 * xmlSAX2GetColumnNumber:
282 * @ctx: the user data (XML parser context)
284 * Provide the column number of the current parsing point.
286 * Returns an int
289 xmlSAX2GetColumnNumber(void *ctx)
291 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
292 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
293 return(ctxt->input->col);
297 * xmlSAX2IsStandalone:
298 * @ctx: the user data (XML parser context)
300 * Is this document tagged standalone ?
302 * Returns 1 if true
305 xmlSAX2IsStandalone(void *ctx)
307 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
308 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
309 return(ctxt->myDoc->standalone == 1);
313 * xmlSAX2HasInternalSubset:
314 * @ctx: the user data (XML parser context)
316 * Does this document has an internal subset
318 * Returns 1 if true
321 xmlSAX2HasInternalSubset(void *ctx)
323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
324 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
325 return(ctxt->myDoc->intSubset != NULL);
329 * xmlSAX2HasExternalSubset:
330 * @ctx: the user data (XML parser context)
332 * Does this document has an external subset
334 * Returns 1 if true
337 xmlSAX2HasExternalSubset(void *ctx)
339 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
340 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
341 return(ctxt->myDoc->extSubset != NULL);
345 * xmlSAX2InternalSubset:
346 * @ctx: the user data (XML parser context)
347 * @name: the root element name
348 * @ExternalID: the external ID
349 * @SystemID: the SYSTEM ID (e.g. filename or URL)
351 * Callback on internal subset declaration.
353 void
354 xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
355 const xmlChar *ExternalID, const xmlChar *SystemID)
357 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
358 xmlDtdPtr dtd;
359 if (ctx == NULL) return;
360 #ifdef DEBUG_SAX
361 xmlGenericError(xmlGenericErrorContext,
362 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
363 name, ExternalID, SystemID);
364 #endif
366 if (ctxt->myDoc == NULL)
367 return;
368 dtd = xmlGetIntSubset(ctxt->myDoc);
369 if (dtd != NULL) {
370 if (ctxt->html)
371 return;
372 xmlUnlinkNode((xmlNodePtr) dtd);
373 xmlFreeDtd(dtd);
374 ctxt->myDoc->intSubset = NULL;
376 ctxt->myDoc->intSubset =
377 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
378 if (ctxt->myDoc->intSubset == NULL)
379 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
383 * xmlSAX2ExternalSubset:
384 * @ctx: the user data (XML parser context)
385 * @name: the root element name
386 * @ExternalID: the external ID
387 * @SystemID: the SYSTEM ID (e.g. filename or URL)
389 * Callback on external subset declaration.
391 void
392 xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
393 const xmlChar *ExternalID, const xmlChar *SystemID)
395 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
396 if (ctx == NULL) return;
397 #ifdef DEBUG_SAX
398 xmlGenericError(xmlGenericErrorContext,
399 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
400 name, ExternalID, SystemID);
401 #endif
402 if (((ExternalID != NULL) || (SystemID != NULL)) &&
403 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
404 (ctxt->wellFormed && ctxt->myDoc))) {
406 * Try to fetch and parse the external subset.
408 xmlParserInputPtr oldinput;
409 int oldinputNr;
410 int oldinputMax;
411 xmlParserInputPtr *oldinputTab;
412 xmlParserInputPtr input = NULL;
413 xmlCharEncoding enc;
414 int oldcharset;
415 const xmlChar *oldencoding;
418 * Ask the Entity resolver to load the damn thing
420 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
421 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
422 SystemID);
423 if (input == NULL) {
424 return;
427 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
430 * make sure we won't destroy the main document context
432 oldinput = ctxt->input;
433 oldinputNr = ctxt->inputNr;
434 oldinputMax = ctxt->inputMax;
435 oldinputTab = ctxt->inputTab;
436 oldcharset = ctxt->charset;
437 oldencoding = ctxt->encoding;
438 ctxt->encoding = NULL;
440 ctxt->inputTab = (xmlParserInputPtr *)
441 xmlMalloc(5 * sizeof(xmlParserInputPtr));
442 if (ctxt->inputTab == NULL) {
443 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
444 ctxt->input = oldinput;
445 ctxt->inputNr = oldinputNr;
446 ctxt->inputMax = oldinputMax;
447 ctxt->inputTab = oldinputTab;
448 ctxt->charset = oldcharset;
449 ctxt->encoding = oldencoding;
450 return;
452 ctxt->inputNr = 0;
453 ctxt->inputMax = 5;
454 ctxt->input = NULL;
455 xmlPushInput(ctxt, input);
458 * On the fly encoding conversion if needed
460 if (ctxt->input->length >= 4) {
461 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
462 xmlSwitchEncoding(ctxt, enc);
465 if (input->filename == NULL)
466 input->filename = (char *) xmlCanonicPath(SystemID);
467 input->line = 1;
468 input->col = 1;
469 input->base = ctxt->input->cur;
470 input->cur = ctxt->input->cur;
471 input->free = NULL;
474 * let's parse that entity knowing it's an external subset.
476 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
479 * Free up the external entities
482 while (ctxt->inputNr > 1)
483 xmlPopInput(ctxt);
484 xmlFreeInputStream(ctxt->input);
485 xmlFree(ctxt->inputTab);
488 * Restore the parsing context of the main entity
490 ctxt->input = oldinput;
491 ctxt->inputNr = oldinputNr;
492 ctxt->inputMax = oldinputMax;
493 ctxt->inputTab = oldinputTab;
494 ctxt->charset = oldcharset;
495 if ((ctxt->encoding != NULL) &&
496 ((ctxt->dict == NULL) ||
497 (!xmlDictOwns(ctxt->dict, ctxt->encoding))))
498 xmlFree((xmlChar *) ctxt->encoding);
499 ctxt->encoding = oldencoding;
500 /* ctxt->wellFormed = oldwellFormed; */
505 * xmlSAX2ResolveEntity:
506 * @ctx: the user data (XML parser context)
507 * @publicId: The public ID of the entity
508 * @systemId: The system ID of the entity
510 * The entity loader, to control the loading of external entities,
511 * the application can either:
512 * - override this xmlSAX2ResolveEntity() callback in the SAX block
513 * - or better use the xmlSetExternalEntityLoader() function to
514 * set up it's own entity resolution routine
516 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
518 xmlParserInputPtr
519 xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
521 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
522 xmlParserInputPtr ret;
523 xmlChar *URI;
524 const char *base = NULL;
526 if (ctx == NULL) return(NULL);
527 if (ctxt->input != NULL)
528 base = ctxt->input->filename;
529 if (base == NULL)
530 base = ctxt->directory;
532 URI = xmlBuildURI(systemId, (const xmlChar *) base);
534 #ifdef DEBUG_SAX
535 xmlGenericError(xmlGenericErrorContext,
536 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
537 #endif
539 ret = xmlLoadExternalEntity((const char *) URI,
540 (const char *) publicId, ctxt);
541 if (URI != NULL)
542 xmlFree(URI);
543 return(ret);
547 * xmlSAX2GetEntity:
548 * @ctx: the user data (XML parser context)
549 * @name: The entity name
551 * Get an entity by name
553 * Returns the xmlEntityPtr if found.
555 xmlEntityPtr
556 xmlSAX2GetEntity(void *ctx, const xmlChar *name)
558 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
559 xmlEntityPtr ret = NULL;
561 if (ctx == NULL) return(NULL);
562 #ifdef DEBUG_SAX
563 xmlGenericError(xmlGenericErrorContext,
564 "SAX.xmlSAX2GetEntity(%s)\n", name);
565 #endif
567 if (ctxt->inSubset == 0) {
568 ret = xmlGetPredefinedEntity(name);
569 if (ret != NULL)
570 return(ret);
572 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
573 if (ctxt->inSubset == 2) {
574 ctxt->myDoc->standalone = 0;
575 ret = xmlGetDocEntity(ctxt->myDoc, name);
576 ctxt->myDoc->standalone = 1;
577 } else {
578 ret = xmlGetDocEntity(ctxt->myDoc, name);
579 if (ret == NULL) {
580 ctxt->myDoc->standalone = 0;
581 ret = xmlGetDocEntity(ctxt->myDoc, name);
582 if (ret != NULL) {
583 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
584 "Entity(%s) document marked standalone but requires external subset\n",
585 name, NULL);
587 ctxt->myDoc->standalone = 1;
590 } else {
591 ret = xmlGetDocEntity(ctxt->myDoc, name);
593 return(ret);
597 * xmlSAX2GetParameterEntity:
598 * @ctx: the user data (XML parser context)
599 * @name: The entity name
601 * Get a parameter entity by name
603 * Returns the xmlEntityPtr if found.
605 xmlEntityPtr
606 xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
608 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
609 xmlEntityPtr ret;
611 if (ctx == NULL) return(NULL);
612 #ifdef DEBUG_SAX
613 xmlGenericError(xmlGenericErrorContext,
614 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
615 #endif
617 ret = xmlGetParameterEntity(ctxt->myDoc, name);
618 return(ret);
623 * xmlSAX2EntityDecl:
624 * @ctx: the user data (XML parser context)
625 * @name: the entity name
626 * @type: the entity type
627 * @publicId: The public ID of the entity
628 * @systemId: The system ID of the entity
629 * @content: the entity value (without processing).
631 * An entity definition has been parsed
633 void
634 xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
635 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
637 xmlEntityPtr ent;
638 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
640 if (ctx == NULL) return;
641 #ifdef DEBUG_SAX
642 xmlGenericError(xmlGenericErrorContext,
643 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
644 name, type, publicId, systemId, content);
645 #endif
646 if (ctxt->inSubset == 1) {
647 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
648 systemId, content);
649 if ((ent == NULL) && (ctxt->pedantic))
650 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
651 "Entity(%s) already defined in the internal subset\n",
652 name);
653 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
654 xmlChar *URI;
655 const char *base = NULL;
657 if (ctxt->input != NULL)
658 base = ctxt->input->filename;
659 if (base == NULL)
660 base = ctxt->directory;
662 URI = xmlBuildURI(systemId, (const xmlChar *) base);
663 ent->URI = URI;
665 } else if (ctxt->inSubset == 2) {
666 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
667 systemId, content);
668 if ((ent == NULL) && (ctxt->pedantic) &&
669 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
670 ctxt->sax->warning(ctxt->userData,
671 "Entity(%s) already defined in the external subset\n", name);
672 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
673 xmlChar *URI;
674 const char *base = NULL;
676 if (ctxt->input != NULL)
677 base = ctxt->input->filename;
678 if (base == NULL)
679 base = ctxt->directory;
681 URI = xmlBuildURI(systemId, (const xmlChar *) base);
682 ent->URI = URI;
684 } else {
685 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
686 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
687 name, NULL);
692 * xmlSAX2AttributeDecl:
693 * @ctx: the user data (XML parser context)
694 * @elem: the name of the element
695 * @fullname: the attribute name
696 * @type: the attribute type
697 * @def: the type of default value
698 * @defaultValue: the attribute default value
699 * @tree: the tree of enumerated value set
701 * An attribute definition has been parsed
703 void
704 xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
705 int type, int def, const xmlChar *defaultValue,
706 xmlEnumerationPtr tree)
708 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
709 xmlAttributePtr attr;
710 xmlChar *name = NULL, *prefix = NULL;
712 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
713 return;
715 #ifdef DEBUG_SAX
716 xmlGenericError(xmlGenericErrorContext,
717 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
718 elem, fullname, type, def, defaultValue);
719 #endif
720 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
721 (type != XML_ATTRIBUTE_ID)) {
723 * Raise the error but keep the validity flag
725 int tmp = ctxt->valid;
726 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
727 "xml:id : attribute type should be ID\n", NULL, NULL);
728 ctxt->valid = tmp;
730 /* TODO: optimize name/prefix allocation */
731 name = xmlSplitQName(ctxt, fullname, &prefix);
732 ctxt->vctxt.valid = 1;
733 if (ctxt->inSubset == 1)
734 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
735 name, prefix, (xmlAttributeType) type,
736 (xmlAttributeDefault) def, defaultValue, tree);
737 else if (ctxt->inSubset == 2)
738 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
739 name, prefix, (xmlAttributeType) type,
740 (xmlAttributeDefault) def, defaultValue, tree);
741 else {
742 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
743 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
744 name, NULL);
745 xmlFree(name);
746 xmlFreeEnumeration(tree);
747 return;
749 #ifdef LIBXML_VALID_ENABLED
750 if (ctxt->vctxt.valid == 0)
751 ctxt->valid = 0;
752 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
753 (ctxt->myDoc->intSubset != NULL))
754 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
755 attr);
756 #endif /* LIBXML_VALID_ENABLED */
757 if (prefix != NULL)
758 xmlFree(prefix);
759 if (name != NULL)
760 xmlFree(name);
764 * xmlSAX2ElementDecl:
765 * @ctx: the user data (XML parser context)
766 * @name: the element name
767 * @type: the element type
768 * @content: the element value tree
770 * An element definition has been parsed
772 void
773 xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
774 xmlElementContentPtr content)
776 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
777 xmlElementPtr elem = NULL;
779 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
780 return;
782 #ifdef DEBUG_SAX
783 xmlGenericError(xmlGenericErrorContext,
784 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
785 #endif
787 if (ctxt->inSubset == 1)
788 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
789 name, (xmlElementTypeVal) type, content);
790 else if (ctxt->inSubset == 2)
791 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
792 name, (xmlElementTypeVal) type, content);
793 else {
794 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
795 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
796 name, NULL);
797 return;
799 #ifdef LIBXML_VALID_ENABLED
800 if (elem == NULL)
801 ctxt->valid = 0;
802 if (ctxt->validate && ctxt->wellFormed &&
803 ctxt->myDoc && ctxt->myDoc->intSubset)
804 ctxt->valid &=
805 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
806 #endif /* LIBXML_VALID_ENABLED */
810 * xmlSAX2NotationDecl:
811 * @ctx: the user data (XML parser context)
812 * @name: The name of the notation
813 * @publicId: The public ID of the entity
814 * @systemId: The system ID of the entity
816 * What to do when a notation declaration has been parsed.
818 void
819 xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
820 const xmlChar *publicId, const xmlChar *systemId)
822 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
823 xmlNotationPtr nota = NULL;
825 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
826 return;
828 #ifdef DEBUG_SAX
829 xmlGenericError(xmlGenericErrorContext,
830 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
831 #endif
833 if ((publicId == NULL) && (systemId == NULL)) {
834 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
835 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
836 name, NULL);
837 return;
838 } else if (ctxt->inSubset == 1)
839 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
840 publicId, systemId);
841 else if (ctxt->inSubset == 2)
842 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
843 publicId, systemId);
844 else {
845 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
846 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
847 name, NULL);
848 return;
850 #ifdef LIBXML_VALID_ENABLED
851 if (nota == NULL) ctxt->valid = 0;
852 if ((ctxt->validate) && (ctxt->wellFormed) &&
853 (ctxt->myDoc->intSubset != NULL))
854 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
855 nota);
856 #endif /* LIBXML_VALID_ENABLED */
860 * xmlSAX2UnparsedEntityDecl:
861 * @ctx: the user data (XML parser context)
862 * @name: The name of the entity
863 * @publicId: The public ID of the entity
864 * @systemId: The system ID of the entity
865 * @notationName: the name of the notation
867 * What to do when an unparsed entity declaration is parsed
869 void
870 xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
871 const xmlChar *publicId, const xmlChar *systemId,
872 const xmlChar *notationName)
874 xmlEntityPtr ent;
875 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
876 if (ctx == NULL) return;
877 #ifdef DEBUG_SAX
878 xmlGenericError(xmlGenericErrorContext,
879 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
880 name, publicId, systemId, notationName);
881 #endif
882 if (ctxt->inSubset == 1) {
883 ent = xmlAddDocEntity(ctxt->myDoc, name,
884 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
885 publicId, systemId, notationName);
886 if ((ent == NULL) && (ctxt->pedantic) &&
887 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
888 ctxt->sax->warning(ctxt->userData,
889 "Entity(%s) already defined in the internal subset\n", name);
890 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
891 xmlChar *URI;
892 const char *base = NULL;
894 if (ctxt->input != NULL)
895 base = ctxt->input->filename;
896 if (base == NULL)
897 base = ctxt->directory;
899 URI = xmlBuildURI(systemId, (const xmlChar *) base);
900 ent->URI = URI;
902 } else if (ctxt->inSubset == 2) {
903 ent = xmlAddDtdEntity(ctxt->myDoc, name,
904 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
905 publicId, systemId, notationName);
906 if ((ent == NULL) && (ctxt->pedantic) &&
907 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
908 ctxt->sax->warning(ctxt->userData,
909 "Entity(%s) already defined in the external subset\n", name);
910 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
911 xmlChar *URI;
912 const char *base = NULL;
914 if (ctxt->input != NULL)
915 base = ctxt->input->filename;
916 if (base == NULL)
917 base = ctxt->directory;
919 URI = xmlBuildURI(systemId, (const xmlChar *) base);
920 ent->URI = URI;
922 } else {
923 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
924 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
925 name, NULL);
930 * xmlSAX2SetDocumentLocator:
931 * @ctx: the user data (XML parser context)
932 * @loc: A SAX Locator
934 * Receive the document locator at startup, actually xmlDefaultSAXLocator
935 * Everything is available on the context, so this is useless in our case.
937 void
938 xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
940 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
941 #ifdef DEBUG_SAX
942 xmlGenericError(xmlGenericErrorContext,
943 "SAX.xmlSAX2SetDocumentLocator()\n");
944 #endif
948 * xmlSAX2StartDocument:
949 * @ctx: the user data (XML parser context)
951 * called when the document start being processed.
953 void
954 xmlSAX2StartDocument(void *ctx)
956 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
957 xmlDocPtr doc;
959 if (ctx == NULL) return;
961 #ifdef DEBUG_SAX
962 xmlGenericError(xmlGenericErrorContext,
963 "SAX.xmlSAX2StartDocument()\n");
964 #endif
965 if (ctxt->html) {
966 #ifdef LIBXML_HTML_ENABLED
967 if (ctxt->myDoc == NULL)
968 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
969 if (ctxt->myDoc == NULL) {
970 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
971 return;
973 ctxt->myDoc->properties = XML_DOC_HTML;
974 ctxt->myDoc->parseFlags = ctxt->options;
975 #else
976 xmlGenericError(xmlGenericErrorContext,
977 "libxml2 built without HTML support\n");
978 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
979 ctxt->instate = XML_PARSER_EOF;
980 ctxt->disableSAX = 1;
981 return;
982 #endif
983 } else {
984 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
985 if (doc != NULL) {
986 doc->properties = 0;
987 if (ctxt->options & XML_PARSE_OLD10)
988 doc->properties |= XML_DOC_OLD10;
989 doc->parseFlags = ctxt->options;
990 if (ctxt->encoding != NULL)
991 doc->encoding = xmlStrdup(ctxt->encoding);
992 else
993 doc->encoding = NULL;
994 doc->standalone = ctxt->standalone;
995 } else {
996 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
997 return;
999 if ((ctxt->dictNames) && (doc != NULL)) {
1000 doc->dict = ctxt->dict;
1001 xmlDictReference(doc->dict);
1004 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1005 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
1006 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
1007 if (ctxt->myDoc->URL == NULL)
1008 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
1013 * xmlSAX2EndDocument:
1014 * @ctx: the user data (XML parser context)
1016 * called when the document end has been detected.
1018 void
1019 xmlSAX2EndDocument(void *ctx)
1021 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1022 #ifdef DEBUG_SAX
1023 xmlGenericError(xmlGenericErrorContext,
1024 "SAX.xmlSAX2EndDocument()\n");
1025 #endif
1026 if (ctx == NULL) return;
1027 #ifdef LIBXML_VALID_ENABLED
1028 if (ctxt->validate && ctxt->wellFormed &&
1029 ctxt->myDoc && ctxt->myDoc->intSubset)
1030 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
1031 #endif /* LIBXML_VALID_ENABLED */
1034 * Grab the encoding if it was added on-the-fly
1036 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1037 (ctxt->myDoc->encoding == NULL)) {
1038 ctxt->myDoc->encoding = ctxt->encoding;
1039 ctxt->encoding = NULL;
1041 if ((ctxt->inputTab != NULL) &&
1042 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1043 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
1044 (ctxt->myDoc->encoding == NULL)) {
1045 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1047 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1048 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1049 ctxt->myDoc->charset = ctxt->charset;
1053 #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
1055 * xmlSAX2AttributeInternal:
1056 * @ctx: the user data (XML parser context)
1057 * @fullname: The attribute name, including namespace prefix
1058 * @value: The attribute value
1059 * @prefix: the prefix on the element node
1061 * Handle an attribute that has been read by the parser.
1062 * The default handling is to convert the attribute into an
1063 * DOM subtree and past it in a new xmlAttr element added to
1064 * the element.
1066 static void
1067 xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
1068 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
1070 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1071 xmlAttrPtr ret;
1072 xmlChar *name;
1073 xmlChar *ns;
1074 xmlChar *nval;
1075 xmlNsPtr namespace;
1077 if (ctxt->html) {
1078 name = xmlStrdup(fullname);
1079 ns = NULL;
1080 namespace = NULL;
1081 } else {
1083 * Split the full name into a namespace prefix and the tag name
1085 name = xmlSplitQName(ctxt, fullname, &ns);
1086 if ((name != NULL) && (name[0] == 0)) {
1087 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1088 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1089 "invalid namespace declaration '%s'\n",
1090 fullname, NULL);
1091 } else {
1092 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1093 "Avoid attribute ending with ':' like '%s'\n",
1094 fullname, NULL);
1096 if (ns != NULL)
1097 xmlFree(ns);
1098 ns = NULL;
1099 xmlFree(name);
1100 name = xmlStrdup(fullname);
1103 if (name == NULL) {
1104 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1105 if (ns != NULL)
1106 xmlFree(ns);
1107 return;
1110 #ifdef LIBXML_HTML_ENABLED
1111 if ((ctxt->html) &&
1112 (value == NULL) && (htmlIsBooleanAttr(fullname))) {
1113 nval = xmlStrdup(fullname);
1114 value = (const xmlChar *) nval;
1115 } else
1116 #endif
1118 #ifdef LIBXML_VALID_ENABLED
1120 * Do the last stage of the attribute normalization
1121 * Needed for HTML too:
1122 * http://www.w3.org/TR/html4/types.html#h-6.2
1124 ctxt->vctxt.valid = 1;
1125 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1126 ctxt->myDoc, ctxt->node,
1127 fullname, value);
1128 if (ctxt->vctxt.valid != 1) {
1129 ctxt->valid = 0;
1131 if (nval != NULL)
1132 value = nval;
1133 #else
1134 nval = NULL;
1135 #endif /* LIBXML_VALID_ENABLED */
1139 * Check whether it's a namespace definition
1141 if ((!ctxt->html) && (ns == NULL) &&
1142 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1143 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1144 xmlNsPtr nsret;
1145 xmlChar *val;
1147 if (!ctxt->replaceEntities) {
1148 ctxt->depth++;
1149 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1150 0,0,0);
1151 ctxt->depth--;
1152 if (val == NULL) {
1153 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1154 if (name != NULL)
1155 xmlFree(name);
1156 if (nval != NULL)
1157 xmlFree(nval);
1158 return;
1160 } else {
1161 val = (xmlChar *) value;
1164 if (val[0] != 0) {
1165 xmlURIPtr uri;
1167 uri = xmlParseURI((const char *)val);
1168 if (uri == NULL) {
1169 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1170 ctxt->sax->warning(ctxt->userData,
1171 "xmlns: %s not a valid URI\n", val);
1172 } else {
1173 if (uri->scheme == NULL) {
1174 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1175 ctxt->sax->warning(ctxt->userData,
1176 "xmlns: URI %s is not absolute\n", val);
1178 xmlFreeURI(uri);
1182 /* a default namespace definition */
1183 nsret = xmlNewNs(ctxt->node, val, NULL);
1185 #ifdef LIBXML_VALID_ENABLED
1187 * Validate also for namespace decls, they are attributes from
1188 * an XML-1.0 perspective
1190 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1191 ctxt->myDoc && ctxt->myDoc->intSubset)
1192 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1193 ctxt->node, prefix, nsret, val);
1194 #endif /* LIBXML_VALID_ENABLED */
1195 if (name != NULL)
1196 xmlFree(name);
1197 if (nval != NULL)
1198 xmlFree(nval);
1199 if (val != value)
1200 xmlFree(val);
1201 return;
1203 if ((!ctxt->html) &&
1204 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1205 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1206 xmlNsPtr nsret;
1207 xmlChar *val;
1209 if (!ctxt->replaceEntities) {
1210 ctxt->depth++;
1211 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1212 0,0,0);
1213 ctxt->depth--;
1214 if (val == NULL) {
1215 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1216 xmlFree(ns);
1217 if (name != NULL)
1218 xmlFree(name);
1219 if (nval != NULL)
1220 xmlFree(nval);
1221 return;
1223 } else {
1224 val = (xmlChar *) value;
1227 if (val[0] == 0) {
1228 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1229 "Empty namespace name for prefix %s\n", name, NULL);
1231 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1232 xmlURIPtr uri;
1234 uri = xmlParseURI((const char *)val);
1235 if (uri == NULL) {
1236 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
1237 "xmlns:%s: %s not a valid URI\n", name, value);
1238 } else {
1239 if (uri->scheme == NULL) {
1240 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
1241 "xmlns:%s: URI %s is not absolute\n", name, value);
1243 xmlFreeURI(uri);
1247 /* a standard namespace definition */
1248 nsret = xmlNewNs(ctxt->node, val, name);
1249 xmlFree(ns);
1250 #ifdef LIBXML_VALID_ENABLED
1252 * Validate also for namespace decls, they are attributes from
1253 * an XML-1.0 perspective
1255 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1256 ctxt->myDoc && ctxt->myDoc->intSubset)
1257 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1258 ctxt->node, prefix, nsret, value);
1259 #endif /* LIBXML_VALID_ENABLED */
1260 if (name != NULL)
1261 xmlFree(name);
1262 if (nval != NULL)
1263 xmlFree(nval);
1264 if (val != value)
1265 xmlFree(val);
1266 return;
1269 if (ns != NULL) {
1270 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
1272 if (namespace == NULL) {
1273 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1274 "Namespace prefix %s of attribute %s is not defined\n",
1275 ns, name);
1276 } else {
1277 xmlAttrPtr prop;
1279 prop = ctxt->node->properties;
1280 while (prop != NULL) {
1281 if (prop->ns != NULL) {
1282 if ((xmlStrEqual(name, prop->name)) &&
1283 ((namespace == prop->ns) ||
1284 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1285 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1286 "Attribute %s in %s redefined\n",
1287 name, namespace->href);
1288 ctxt->wellFormed = 0;
1289 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1290 if (name != NULL)
1291 xmlFree(name);
1292 goto error;
1295 prop = prop->next;
1298 } else {
1299 namespace = NULL;
1302 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1303 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1305 if (ret != NULL) {
1306 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1307 xmlNodePtr tmp;
1309 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1310 tmp = ret->children;
1311 while (tmp != NULL) {
1312 tmp->parent = (xmlNodePtr) ret;
1313 if (tmp->next == NULL)
1314 ret->last = tmp;
1315 tmp = tmp->next;
1317 } else if (value != NULL) {
1318 ret->children = xmlNewDocText(ctxt->myDoc, value);
1319 ret->last = ret->children;
1320 if (ret->children != NULL)
1321 ret->children->parent = (xmlNodePtr) ret;
1325 #ifdef LIBXML_VALID_ENABLED
1326 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1327 ctxt->myDoc && ctxt->myDoc->intSubset) {
1330 * If we don't substitute entities, the validation should be
1331 * done on a value with replaced entities anyway.
1333 if (!ctxt->replaceEntities) {
1334 xmlChar *val;
1336 ctxt->depth++;
1337 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1338 0,0,0);
1339 ctxt->depth--;
1341 if (val == NULL)
1342 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1343 ctxt->myDoc, ctxt->node, ret, value);
1344 else {
1345 xmlChar *nvalnorm;
1348 * Do the last stage of the attribute normalization
1349 * It need to be done twice ... it's an extra burden related
1350 * to the ability to keep xmlSAX2References in attributes
1352 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1353 ctxt->node, fullname, val);
1354 if (nvalnorm != NULL) {
1355 xmlFree(val);
1356 val = nvalnorm;
1359 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1360 ctxt->myDoc, ctxt->node, ret, val);
1361 xmlFree(val);
1363 } else {
1364 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1365 ctxt->node, ret, value);
1367 } else
1368 #endif /* LIBXML_VALID_ENABLED */
1369 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1370 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1371 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1373 * when validating, the ID registration is done at the attribute
1374 * validation level. Otherwise we have to do specific handling here.
1376 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
1378 * Add the xml:id value
1380 * Open issue: normalization of the value.
1382 if (xmlValidateNCName(value, 1) != 0) {
1383 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1384 "xml:id : attribute value %s is not an NCName\n",
1385 (const char *) value, NULL);
1387 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1388 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1389 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1390 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1391 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1394 error:
1395 if (nval != NULL)
1396 xmlFree(nval);
1397 if (ns != NULL)
1398 xmlFree(ns);
1402 * xmlCheckDefaultedAttributes:
1404 * Check defaulted attributes from the DTD
1406 static void
1407 xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1408 const xmlChar *prefix, const xmlChar **atts) {
1409 xmlElementPtr elemDecl;
1410 const xmlChar *att;
1411 int internal = 1;
1412 int i;
1414 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1415 if (elemDecl == NULL) {
1416 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1417 internal = 0;
1420 process_external_subset:
1422 if (elemDecl != NULL) {
1423 xmlAttributePtr attr = elemDecl->attributes;
1425 * Check against defaulted attributes from the external subset
1426 * if the document is stamped as standalone
1428 if ((ctxt->myDoc->standalone == 1) &&
1429 (ctxt->myDoc->extSubset != NULL) &&
1430 (ctxt->validate)) {
1431 while (attr != NULL) {
1432 if ((attr->defaultValue != NULL) &&
1433 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1434 attr->elem, attr->name,
1435 attr->prefix) == attr) &&
1436 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1437 attr->elem, attr->name,
1438 attr->prefix) == NULL)) {
1439 xmlChar *fulln;
1441 if (attr->prefix != NULL) {
1442 fulln = xmlStrdup(attr->prefix);
1443 fulln = xmlStrcat(fulln, BAD_CAST ":");
1444 fulln = xmlStrcat(fulln, attr->name);
1445 } else {
1446 fulln = xmlStrdup(attr->name);
1448 if (fulln == NULL) {
1449 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1450 break;
1454 * Check that the attribute is not declared in the
1455 * serialization
1457 att = NULL;
1458 if (atts != NULL) {
1459 i = 0;
1460 att = atts[i];
1461 while (att != NULL) {
1462 if (xmlStrEqual(att, fulln))
1463 break;
1464 i += 2;
1465 att = atts[i];
1468 if (att == NULL) {
1469 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
1470 "standalone: attribute %s on %s defaulted from external subset\n",
1471 (const char *)fulln,
1472 (const char *)attr->elem);
1474 xmlFree(fulln);
1476 attr = attr->nexth;
1481 * Actually insert defaulted values when needed
1483 attr = elemDecl->attributes;
1484 while (attr != NULL) {
1486 * Make sure that attributes redefinition occurring in the
1487 * internal subset are not overridden by definitions in the
1488 * external subset.
1490 if (attr->defaultValue != NULL) {
1492 * the element should be instantiated in the tree if:
1493 * - this is a namespace prefix
1494 * - the user required for completion in the tree
1495 * like XSLT
1496 * - there isn't already an attribute definition
1497 * in the internal subset overriding it.
1499 if (((attr->prefix != NULL) &&
1500 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1501 ((attr->prefix == NULL) &&
1502 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1503 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1504 xmlAttributePtr tst;
1506 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1507 attr->elem, attr->name,
1508 attr->prefix);
1509 if ((tst == attr) || (tst == NULL)) {
1510 xmlChar fn[50];
1511 xmlChar *fulln;
1513 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1514 if (fulln == NULL) {
1515 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1516 return;
1520 * Check that the attribute is not declared in the
1521 * serialization
1523 att = NULL;
1524 if (atts != NULL) {
1525 i = 0;
1526 att = atts[i];
1527 while (att != NULL) {
1528 if (xmlStrEqual(att, fulln))
1529 break;
1530 i += 2;
1531 att = atts[i];
1534 if (att == NULL) {
1535 xmlSAX2AttributeInternal(ctxt, fulln,
1536 attr->defaultValue, prefix);
1538 if ((fulln != fn) && (fulln != attr->name))
1539 xmlFree(fulln);
1543 attr = attr->nexth;
1545 if (internal == 1) {
1546 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1547 name, prefix);
1548 internal = 0;
1549 goto process_external_subset;
1555 * xmlSAX2StartElement:
1556 * @ctx: the user data (XML parser context)
1557 * @fullname: The element name, including namespace prefix
1558 * @atts: An array of name/value attributes pairs, NULL terminated
1560 * called when an opening tag has been processed.
1562 void
1563 xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1565 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1566 xmlNodePtr ret;
1567 xmlNodePtr parent;
1568 xmlNsPtr ns;
1569 xmlChar *name;
1570 xmlChar *prefix;
1571 const xmlChar *att;
1572 const xmlChar *value;
1573 int i;
1575 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
1576 parent = ctxt->node;
1577 #ifdef DEBUG_SAX
1578 xmlGenericError(xmlGenericErrorContext,
1579 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1580 #endif
1583 * First check on validity:
1585 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1586 ((ctxt->myDoc->intSubset == NULL) ||
1587 ((ctxt->myDoc->intSubset->notations == NULL) &&
1588 (ctxt->myDoc->intSubset->elements == NULL) &&
1589 (ctxt->myDoc->intSubset->attributes == NULL) &&
1590 (ctxt->myDoc->intSubset->entities == NULL)))) {
1591 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1592 "Validation failed: no DTD found !", NULL, NULL);
1593 ctxt->validate = 0;
1598 * Split the full name into a namespace prefix and the tag name
1600 name = xmlSplitQName(ctxt, fullname, &prefix);
1604 * Note : the namespace resolution is deferred until the end of the
1605 * attributes parsing, since local namespace can be defined as
1606 * an attribute at this level.
1608 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1609 if (ret == NULL) {
1610 if (prefix != NULL)
1611 xmlFree(prefix);
1612 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1613 return;
1615 if (ctxt->myDoc->children == NULL) {
1616 #ifdef DEBUG_SAX_TREE
1617 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1618 #endif
1619 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1620 } else if (parent == NULL) {
1621 parent = ctxt->myDoc->children;
1623 ctxt->nodemem = -1;
1624 if (ctxt->linenumbers) {
1625 if (ctxt->input != NULL) {
1626 if (ctxt->input->line < USHRT_MAX)
1627 ret->line = (unsigned short) ctxt->input->line;
1628 else
1629 ret->line = USHRT_MAX;
1634 * We are parsing a new node.
1636 #ifdef DEBUG_SAX_TREE
1637 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1638 #endif
1639 if (nodePush(ctxt, ret) < 0) {
1640 xmlUnlinkNode(ret);
1641 xmlFreeNode(ret);
1642 if (prefix != NULL)
1643 xmlFree(prefix);
1644 return;
1648 * Link the child element
1650 if (parent != NULL) {
1651 if (parent->type == XML_ELEMENT_NODE) {
1652 #ifdef DEBUG_SAX_TREE
1653 xmlGenericError(xmlGenericErrorContext,
1654 "adding child %s to %s\n", name, parent->name);
1655 #endif
1656 xmlAddChild(parent, ret);
1657 } else {
1658 #ifdef DEBUG_SAX_TREE
1659 xmlGenericError(xmlGenericErrorContext,
1660 "adding sibling %s to ", name);
1661 xmlDebugDumpOneNode(stderr, parent, 0);
1662 #endif
1663 xmlAddSibling(parent, ret);
1667 if (!ctxt->html) {
1669 * Insert all the defaulted attributes from the DTD especially
1670 * namespaces
1672 if ((ctxt->myDoc->intSubset != NULL) ||
1673 (ctxt->myDoc->extSubset != NULL)) {
1674 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1678 * process all the attributes whose name start with "xmlns"
1680 if (atts != NULL) {
1681 i = 0;
1682 att = atts[i++];
1683 value = atts[i++];
1684 while ((att != NULL) && (value != NULL)) {
1685 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1686 (att[3] == 'n') && (att[4] == 's'))
1687 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1689 att = atts[i++];
1690 value = atts[i++];
1695 * Search the namespace, note that since the attributes have been
1696 * processed, the local namespaces are available.
1698 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1699 if ((ns == NULL) && (parent != NULL))
1700 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1701 if ((prefix != NULL) && (ns == NULL)) {
1702 ns = xmlNewNs(ret, NULL, prefix);
1703 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1704 "Namespace prefix %s is not defined\n",
1705 prefix, NULL);
1709 * set the namespace node, making sure that if the default namespace
1710 * is unbound on a parent we simply keep it NULL
1712 if ((ns != NULL) && (ns->href != NULL) &&
1713 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1714 xmlSetNs(ret, ns);
1718 * process all the other attributes
1720 if (atts != NULL) {
1721 i = 0;
1722 att = atts[i++];
1723 value = atts[i++];
1724 if (ctxt->html) {
1725 while (att != NULL) {
1726 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1727 att = atts[i++];
1728 value = atts[i++];
1730 } else {
1731 while ((att != NULL) && (value != NULL)) {
1732 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1733 (att[3] != 'n') || (att[4] != 's'))
1734 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1737 * Next ones
1739 att = atts[i++];
1740 value = atts[i++];
1745 #ifdef LIBXML_VALID_ENABLED
1747 * If it's the Document root, finish the DTD validation and
1748 * check the document root element for validity
1750 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
1751 int chk;
1753 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1754 if (chk <= 0)
1755 ctxt->valid = 0;
1756 if (chk < 0)
1757 ctxt->wellFormed = 0;
1758 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1759 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
1761 #endif /* LIBXML_VALID_ENABLED */
1763 if (prefix != NULL)
1764 xmlFree(prefix);
1769 * xmlSAX2EndElement:
1770 * @ctx: the user data (XML parser context)
1771 * @name: The element name
1773 * called when the end of an element has been detected.
1775 void
1776 xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1778 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1779 xmlNodePtr cur;
1781 if (ctx == NULL) return;
1782 cur = ctxt->node;
1783 #ifdef DEBUG_SAX
1784 if (name == NULL)
1785 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1786 else
1787 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1788 #endif
1790 /* Capture end position and add node */
1791 if (cur != NULL && ctxt->record_info) {
1792 ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
1793 ctxt->nodeInfo->end_line = ctxt->input->line;
1794 ctxt->nodeInfo->node = cur;
1795 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
1797 ctxt->nodemem = -1;
1799 #ifdef LIBXML_VALID_ENABLED
1800 if (ctxt->validate && ctxt->wellFormed &&
1801 ctxt->myDoc && ctxt->myDoc->intSubset)
1802 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1803 cur);
1804 #endif /* LIBXML_VALID_ENABLED */
1808 * end of parsing of this node.
1810 #ifdef DEBUG_SAX_TREE
1811 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1812 #endif
1813 nodePop(ctxt);
1815 #endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLED || LIBXML_LEGACY_ENABLED */
1818 * xmlSAX2TextNode:
1819 * @ctxt: the parser context
1820 * @str: the input string
1821 * @len: the string length
1823 * Callback for a text node
1825 * Returns the newly allocated string or NULL if not needed or error
1827 static xmlNodePtr
1828 xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1829 xmlNodePtr ret;
1830 const xmlChar *intern = NULL;
1833 * Allocate
1835 if (ctxt->freeElems != NULL) {
1836 ret = ctxt->freeElems;
1837 ctxt->freeElems = ret->next;
1838 ctxt->freeElemsNr--;
1839 } else {
1840 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1842 if (ret == NULL) {
1843 xmlErrMemory(ctxt, "xmlSAX2Characters");
1844 return(NULL);
1846 memset(ret, 0, sizeof(xmlNode));
1848 * intern the formatting blanks found between tags, or the
1849 * very short strings
1851 if (ctxt->dictNames) {
1852 xmlChar cur = str[len];
1854 if ((len < (int) (2 * sizeof(void *))) &&
1855 (ctxt->options & XML_PARSE_COMPACT)) {
1856 /* store the string in the node overriding properties and nsDef */
1857 xmlChar *tmp = (xmlChar *) &(ret->properties);
1858 memcpy(tmp, str, len);
1859 tmp[len] = 0;
1860 intern = tmp;
1861 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1862 ((cur == '<') && (str[len + 1] != '!')))) {
1863 intern = xmlDictLookup(ctxt->dict, str, len);
1864 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
1865 (str[len + 1] != '!')) {
1866 int i;
1868 for (i = 1;i < len;i++) {
1869 if (!IS_BLANK_CH(str[i])) goto skip;
1871 intern = xmlDictLookup(ctxt->dict, str, len);
1874 skip:
1875 ret->type = XML_TEXT_NODE;
1877 ret->name = xmlStringText;
1878 if (intern == NULL) {
1879 ret->content = xmlStrndup(str, len);
1880 if (ret->content == NULL) {
1881 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1882 xmlFree(ret);
1883 return(NULL);
1885 } else
1886 ret->content = (xmlChar *) intern;
1888 if (ctxt->linenumbers) {
1889 if (ctxt->input != NULL) {
1890 if (ctxt->input->line < USHRT_MAX)
1891 ret->line = (unsigned short) ctxt->input->line;
1892 else {
1893 ret->line = USHRT_MAX;
1894 if (ctxt->options & XML_PARSE_BIG_LINES)
1895 ret->psvi = (void *) (ptrdiff_t) ctxt->input->line;
1900 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1901 xmlRegisterNodeDefaultValue(ret);
1902 return(ret);
1905 #ifdef LIBXML_VALID_ENABLED
1907 * xmlSAX2DecodeAttrEntities:
1908 * @ctxt: the parser context
1909 * @str: the input string
1910 * @len: the string length
1912 * Remove the entities from an attribute value
1914 * Returns the newly allocated string or NULL if not needed or error
1916 static xmlChar *
1917 xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1918 const xmlChar *end) {
1919 const xmlChar *in;
1920 xmlChar *ret;
1922 in = str;
1923 while (in < end)
1924 if (*in++ == '&')
1925 goto decode;
1926 return(NULL);
1927 decode:
1928 ctxt->depth++;
1929 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1930 XML_SUBSTITUTE_REF, 0,0,0);
1931 ctxt->depth--;
1932 return(ret);
1934 #endif /* LIBXML_VALID_ENABLED */
1937 * xmlSAX2AttributeNs:
1938 * @ctx: the user data (XML parser context)
1939 * @localname: the local name of the attribute
1940 * @prefix: the attribute namespace prefix if available
1941 * @URI: the attribute namespace name if available
1942 * @value: Start of the attribute value
1943 * @valueend: end of the attribute value
1945 * Handle an attribute that has been read by the parser.
1946 * The default handling is to convert the attribute into an
1947 * DOM subtree and past it in a new xmlAttr element added to
1948 * the element.
1950 static void
1951 xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1952 const xmlChar * localname,
1953 const xmlChar * prefix,
1954 const xmlChar * value,
1955 const xmlChar * valueend)
1957 xmlAttrPtr ret;
1958 xmlNsPtr namespace = NULL;
1959 xmlChar *dup = NULL;
1962 * Note: if prefix == NULL, the attribute is not in the default namespace
1964 if (prefix != NULL)
1965 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1968 * allocate the node
1970 if (ctxt->freeAttrs != NULL) {
1971 ret = ctxt->freeAttrs;
1972 ctxt->freeAttrs = ret->next;
1973 ctxt->freeAttrsNr--;
1974 memset(ret, 0, sizeof(xmlAttr));
1975 ret->type = XML_ATTRIBUTE_NODE;
1977 ret->parent = ctxt->node;
1978 ret->doc = ctxt->myDoc;
1979 ret->ns = namespace;
1981 if (ctxt->dictNames)
1982 ret->name = localname;
1983 else
1984 ret->name = xmlStrdup(localname);
1986 /* link at the end to preserve order, TODO speed up with a last */
1987 if (ctxt->node->properties == NULL) {
1988 ctxt->node->properties = ret;
1989 } else {
1990 xmlAttrPtr prev = ctxt->node->properties;
1992 while (prev->next != NULL) prev = prev->next;
1993 prev->next = ret;
1994 ret->prev = prev;
1997 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1998 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1999 } else {
2000 if (ctxt->dictNames)
2001 ret = xmlNewNsPropEatName(ctxt->node, namespace,
2002 (xmlChar *) localname, NULL);
2003 else
2004 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
2005 if (ret == NULL) {
2006 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
2007 return;
2011 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
2012 xmlNodePtr tmp;
2015 * We know that if there is an entity reference, then
2016 * the string has been dup'ed and terminates with 0
2017 * otherwise with ' or "
2019 if (*valueend != 0) {
2020 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2021 ret->children = tmp;
2022 ret->last = tmp;
2023 if (tmp != NULL) {
2024 tmp->doc = ret->doc;
2025 tmp->parent = (xmlNodePtr) ret;
2027 } else {
2028 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
2029 valueend - value);
2030 tmp = ret->children;
2031 while (tmp != NULL) {
2032 tmp->doc = ret->doc;
2033 tmp->parent = (xmlNodePtr) ret;
2034 if (tmp->next == NULL)
2035 ret->last = tmp;
2036 tmp = tmp->next;
2039 } else if (value != NULL) {
2040 xmlNodePtr tmp;
2042 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2043 ret->children = tmp;
2044 ret->last = tmp;
2045 if (tmp != NULL) {
2046 tmp->doc = ret->doc;
2047 tmp->parent = (xmlNodePtr) ret;
2051 #ifdef LIBXML_VALID_ENABLED
2052 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2053 ctxt->myDoc && ctxt->myDoc->intSubset) {
2055 * If we don't substitute entities, the validation should be
2056 * done on a value with replaced entities anyway.
2058 if (!ctxt->replaceEntities) {
2059 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2060 if (dup == NULL) {
2061 if (*valueend == 0) {
2062 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2063 ctxt->myDoc, ctxt->node, ret, value);
2064 } else {
2066 * That should already be normalized.
2067 * cheaper to finally allocate here than duplicate
2068 * entry points in the full validation code
2070 dup = xmlStrndup(value, valueend - value);
2072 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2073 ctxt->myDoc, ctxt->node, ret, dup);
2075 } else {
2077 * dup now contains a string of the flattened attribute
2078 * content with entities substituted. Check if we need to
2079 * apply an extra layer of normalization.
2080 * It need to be done twice ... it's an extra burden related
2081 * to the ability to keep references in attributes
2083 if (ctxt->attsSpecial != NULL) {
2084 xmlChar *nvalnorm;
2085 xmlChar fn[50];
2086 xmlChar *fullname;
2088 fullname = xmlBuildQName(localname, prefix, fn, 50);
2089 if (fullname != NULL) {
2090 ctxt->vctxt.valid = 1;
2091 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2092 &ctxt->vctxt, ctxt->myDoc,
2093 ctxt->node, fullname, dup);
2094 if (ctxt->vctxt.valid != 1)
2095 ctxt->valid = 0;
2097 if ((fullname != fn) && (fullname != localname))
2098 xmlFree(fullname);
2099 if (nvalnorm != NULL) {
2100 xmlFree(dup);
2101 dup = nvalnorm;
2106 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2107 ctxt->myDoc, ctxt->node, ret, dup);
2109 } else {
2111 * if entities already have been substituted, then
2112 * the attribute as passed is already normalized
2114 dup = xmlStrndup(value, valueend - value);
2116 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2117 ctxt->myDoc, ctxt->node, ret, dup);
2119 } else
2120 #endif /* LIBXML_VALID_ENABLED */
2121 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
2122 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2123 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2125 * when validating, the ID registration is done at the attribute
2126 * validation level. Otherwise we have to do specific handling here.
2128 if ((prefix == ctxt->str_xml) &&
2129 (localname[0] == 'i') && (localname[1] == 'd') &&
2130 (localname[2] == 0)) {
2132 * Add the xml:id value
2134 * Open issue: normalization of the value.
2136 if (dup == NULL)
2137 dup = xmlStrndup(value, valueend - value);
2138 #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
2139 #ifdef LIBXML_VALID_ENABLED
2140 if (xmlValidateNCName(dup, 1) != 0) {
2141 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2142 "xml:id : attribute value %s is not an NCName\n",
2143 (const char *) dup, NULL);
2145 #endif
2146 #endif
2147 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2148 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2149 /* might be worth duplicate entry points and not copy */
2150 if (dup == NULL)
2151 dup = xmlStrndup(value, valueend - value);
2152 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2153 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2154 if (dup == NULL)
2155 dup = xmlStrndup(value, valueend - value);
2156 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2159 if (dup != NULL)
2160 xmlFree(dup);
2164 * xmlSAX2StartElementNs:
2165 * @ctx: the user data (XML parser context)
2166 * @localname: the local name of the element
2167 * @prefix: the element namespace prefix if available
2168 * @URI: the element namespace name if available
2169 * @nb_namespaces: number of namespace definitions on that node
2170 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2171 * @nb_attributes: the number of attributes on that node
2172 * @nb_defaulted: the number of defaulted attributes.
2173 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2174 * attribute values.
2176 * SAX2 callback when an element start has been detected by the parser.
2177 * It provides the namespace information for the element, as well as
2178 * the new namespace declarations on the element.
2180 void
2181 xmlSAX2StartElementNs(void *ctx,
2182 const xmlChar *localname,
2183 const xmlChar *prefix,
2184 const xmlChar *URI,
2185 int nb_namespaces,
2186 const xmlChar **namespaces,
2187 int nb_attributes,
2188 int nb_defaulted,
2189 const xmlChar **attributes)
2191 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2192 xmlNodePtr ret;
2193 xmlNodePtr parent;
2194 xmlNsPtr last = NULL, ns;
2195 const xmlChar *uri, *pref;
2196 xmlChar *lname = NULL;
2197 int i, j;
2199 if (ctx == NULL) return;
2200 parent = ctxt->node;
2202 * First check on validity:
2204 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2205 ((ctxt->myDoc->intSubset == NULL) ||
2206 ((ctxt->myDoc->intSubset->notations == NULL) &&
2207 (ctxt->myDoc->intSubset->elements == NULL) &&
2208 (ctxt->myDoc->intSubset->attributes == NULL) &&
2209 (ctxt->myDoc->intSubset->entities == NULL)))) {
2210 xmlErrValid(ctxt, XML_DTD_NO_DTD,
2211 "Validation failed: no DTD found !", NULL, NULL);
2212 ctxt->validate = 0;
2216 * Take care of the rare case of an undefined namespace prefix
2218 if ((prefix != NULL) && (URI == NULL)) {
2219 if (ctxt->dictNames) {
2220 const xmlChar *fullname;
2222 fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
2223 if (fullname != NULL)
2224 localname = fullname;
2225 } else {
2226 lname = xmlBuildQName(localname, prefix, NULL, 0);
2230 * allocate the node
2232 if (ctxt->freeElems != NULL) {
2233 ret = ctxt->freeElems;
2234 ctxt->freeElems = ret->next;
2235 ctxt->freeElemsNr--;
2236 memset(ret, 0, sizeof(xmlNode));
2237 ret->doc = ctxt->myDoc;
2238 ret->type = XML_ELEMENT_NODE;
2240 if (ctxt->dictNames)
2241 ret->name = localname;
2242 else {
2243 if (lname == NULL)
2244 ret->name = xmlStrdup(localname);
2245 else
2246 ret->name = lname;
2247 if (ret->name == NULL) {
2248 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2249 return;
2252 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2253 xmlRegisterNodeDefaultValue(ret);
2254 } else {
2255 if (ctxt->dictNames)
2256 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2257 (xmlChar *) localname, NULL);
2258 else if (lname == NULL)
2259 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2260 else
2261 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2262 (xmlChar *) lname, NULL);
2263 if (ret == NULL) {
2264 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2265 return;
2268 if (ctxt->linenumbers) {
2269 if (ctxt->input != NULL) {
2270 if (ctxt->input->line < USHRT_MAX)
2271 ret->line = (unsigned short) ctxt->input->line;
2272 else
2273 ret->line = USHRT_MAX;
2277 if (parent == NULL) {
2278 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2281 * Build the namespace list
2283 for (i = 0,j = 0;j < nb_namespaces;j++) {
2284 pref = namespaces[i++];
2285 uri = namespaces[i++];
2286 ns = xmlNewNs(NULL, uri, pref);
2287 if (ns != NULL) {
2288 if (last == NULL) {
2289 ret->nsDef = last = ns;
2290 } else {
2291 last->next = ns;
2292 last = ns;
2294 if ((URI != NULL) && (prefix == pref))
2295 ret->ns = ns;
2296 } else {
2298 * any out of memory error would already have been raised
2299 * but we can't be guaranteed it's the actual error due to the
2300 * API, best is to skip in this case
2302 continue;
2304 #ifdef LIBXML_VALID_ENABLED
2305 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2306 ctxt->myDoc && ctxt->myDoc->intSubset) {
2307 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2308 ret, prefix, ns, uri);
2310 #endif /* LIBXML_VALID_ENABLED */
2312 ctxt->nodemem = -1;
2315 * We are parsing a new node.
2317 if (nodePush(ctxt, ret) < 0) {
2318 xmlUnlinkNode(ret);
2319 xmlFreeNode(ret);
2320 return;
2324 * Link the child element
2326 if (parent != NULL) {
2327 if (parent->type == XML_ELEMENT_NODE) {
2328 xmlAddChild(parent, ret);
2329 } else {
2330 xmlAddSibling(parent, ret);
2335 * Insert the defaulted attributes from the DTD only if requested:
2337 if ((nb_defaulted != 0) &&
2338 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2339 nb_attributes -= nb_defaulted;
2342 * Search the namespace if it wasn't already found
2343 * Note that, if prefix is NULL, this searches for the default Ns
2345 if ((URI != NULL) && (ret->ns == NULL)) {
2346 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2347 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2348 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2350 if (ret->ns == NULL) {
2351 ns = xmlNewNs(ret, NULL, prefix);
2352 if (ns == NULL) {
2354 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2355 return;
2357 if (prefix != NULL)
2358 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2359 "Namespace prefix %s was not found\n",
2360 prefix, NULL);
2361 else
2362 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2363 "Namespace default prefix was not found\n",
2364 NULL, NULL);
2369 * process all the other attributes
2371 if (nb_attributes > 0) {
2372 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2374 * Handle the rare case of an undefined attribute prefix
2376 if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
2377 if (ctxt->dictNames) {
2378 const xmlChar *fullname;
2380 fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
2381 attributes[j]);
2382 if (fullname != NULL) {
2383 xmlSAX2AttributeNs(ctxt, fullname, NULL,
2384 attributes[j+3], attributes[j+4]);
2385 continue;
2387 } else {
2388 lname = xmlBuildQName(attributes[j], attributes[j+1],
2389 NULL, 0);
2390 if (lname != NULL) {
2391 xmlSAX2AttributeNs(ctxt, lname, NULL,
2392 attributes[j+3], attributes[j+4]);
2393 xmlFree(lname);
2394 continue;
2398 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2399 attributes[j+3], attributes[j+4]);
2403 #ifdef LIBXML_VALID_ENABLED
2405 * If it's the Document root, finish the DTD validation and
2406 * check the document root element for validity
2408 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
2409 int chk;
2411 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2412 if (chk <= 0)
2413 ctxt->valid = 0;
2414 if (chk < 0)
2415 ctxt->wellFormed = 0;
2416 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2417 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
2419 #endif /* LIBXML_VALID_ENABLED */
2423 * xmlSAX2EndElementNs:
2424 * @ctx: the user data (XML parser context)
2425 * @localname: the local name of the element
2426 * @prefix: the element namespace prefix if available
2427 * @URI: the element namespace name if available
2429 * SAX2 callback when an element end has been detected by the parser.
2430 * It provides the namespace information for the element.
2432 void
2433 xmlSAX2EndElementNs(void *ctx,
2434 const xmlChar * localname ATTRIBUTE_UNUSED,
2435 const xmlChar * prefix ATTRIBUTE_UNUSED,
2436 const xmlChar * URI ATTRIBUTE_UNUSED)
2438 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2439 xmlParserNodeInfo node_info;
2440 xmlNodePtr cur;
2442 if (ctx == NULL) return;
2443 cur = ctxt->node;
2444 /* Capture end position and add node */
2445 if ((ctxt->record_info) && (cur != NULL)) {
2446 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2447 node_info.end_line = ctxt->input->line;
2448 node_info.node = cur;
2449 xmlParserAddNodeInfo(ctxt, &node_info);
2451 ctxt->nodemem = -1;
2453 #ifdef LIBXML_VALID_ENABLED
2454 if (ctxt->validate && ctxt->wellFormed &&
2455 ctxt->myDoc && ctxt->myDoc->intSubset)
2456 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
2457 #endif /* LIBXML_VALID_ENABLED */
2460 * end of parsing of this node.
2462 nodePop(ctxt);
2466 * xmlSAX2Reference:
2467 * @ctx: the user data (XML parser context)
2468 * @name: The entity name
2470 * called when an entity xmlSAX2Reference is detected.
2472 void
2473 xmlSAX2Reference(void *ctx, const xmlChar *name)
2475 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2476 xmlNodePtr ret;
2478 if (ctx == NULL) return;
2479 #ifdef DEBUG_SAX
2480 xmlGenericError(xmlGenericErrorContext,
2481 "SAX.xmlSAX2Reference(%s)\n", name);
2482 #endif
2483 if (name[0] == '#')
2484 ret = xmlNewCharRef(ctxt->myDoc, name);
2485 else
2486 ret = xmlNewReference(ctxt->myDoc, name);
2487 #ifdef DEBUG_SAX_TREE
2488 xmlGenericError(xmlGenericErrorContext,
2489 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2490 #endif
2491 if (xmlAddChild(ctxt->node, ret) == NULL) {
2492 xmlFreeNode(ret);
2497 * xmlSAX2Text:
2498 * @ctx: the user data (XML parser context)
2499 * @ch: a xmlChar string
2500 * @len: the number of xmlChar
2501 * @type: text or cdata
2503 * Append characters.
2505 static void
2506 xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
2507 xmlElementType type)
2509 xmlNodePtr lastChild;
2511 if (ctxt == NULL) return;
2512 #ifdef DEBUG_SAX
2513 xmlGenericError(xmlGenericErrorContext,
2514 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2515 #endif
2517 * Handle the data if any. If there is no child
2518 * add it as content, otherwise if the last child is text,
2519 * concatenate it, else create a new node of type text.
2522 if (ctxt->node == NULL) {
2523 #ifdef DEBUG_SAX_TREE
2524 xmlGenericError(xmlGenericErrorContext,
2525 "add chars: ctxt->node == NULL !\n");
2526 #endif
2527 return;
2529 lastChild = ctxt->node->last;
2530 #ifdef DEBUG_SAX_TREE
2531 xmlGenericError(xmlGenericErrorContext,
2532 "add chars to %s \n", ctxt->node->name);
2533 #endif
2536 * Here we needed an accelerator mechanism in case of very large
2537 * elements. Use an attribute in the structure !!!
2539 if (lastChild == NULL) {
2540 if (type == XML_TEXT_NODE)
2541 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2542 else
2543 lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
2544 if (lastChild != NULL) {
2545 ctxt->node->children = lastChild;
2546 ctxt->node->last = lastChild;
2547 lastChild->parent = ctxt->node;
2548 lastChild->doc = ctxt->node->doc;
2549 ctxt->nodelen = len;
2550 ctxt->nodemem = len + 1;
2551 } else {
2552 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2553 return;
2555 } else {
2556 int coalesceText = (lastChild != NULL) &&
2557 (lastChild->type == type) &&
2558 ((type != XML_TEXT_NODE) ||
2559 (lastChild->name == xmlStringText));
2560 if ((coalesceText) && (ctxt->nodemem != 0)) {
2562 * The whole point of maintaining nodelen and nodemem,
2563 * xmlTextConcat is too costly, i.e. compute length,
2564 * reallocate a new buffer, move data, append ch. Here
2565 * We try to minimize realloc() uses and avoid copying
2566 * and recomputing length over and over.
2568 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2569 lastChild->content = xmlStrdup(lastChild->content);
2570 lastChild->properties = NULL;
2571 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2572 (xmlDictOwns(ctxt->dict, lastChild->content))) {
2573 lastChild->content = xmlStrdup(lastChild->content);
2575 if (lastChild->content == NULL) {
2576 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: xmlStrdup returned NULL");
2577 return;
2579 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
2580 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2581 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2582 return;
2584 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
2585 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
2586 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2587 return;
2589 if (ctxt->nodelen + len >= ctxt->nodemem) {
2590 xmlChar *newbuf;
2591 size_t size;
2593 size = ctxt->nodemem + len;
2594 size *= 2;
2595 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2596 if (newbuf == NULL) {
2597 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2598 return;
2600 ctxt->nodemem = size;
2601 lastChild->content = newbuf;
2603 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2604 ctxt->nodelen += len;
2605 lastChild->content[ctxt->nodelen] = 0;
2606 } else if (coalesceText) {
2607 if (xmlTextConcat(lastChild, ch, len)) {
2608 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2610 if (ctxt->node->children != NULL) {
2611 ctxt->nodelen = xmlStrlen(lastChild->content);
2612 ctxt->nodemem = ctxt->nodelen + 1;
2614 } else {
2615 /* Mixed content, first time */
2616 if (type == XML_TEXT_NODE)
2617 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2618 else
2619 lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
2620 if (lastChild != NULL) {
2621 xmlAddChild(ctxt->node, lastChild);
2622 if (ctxt->node->children != NULL) {
2623 ctxt->nodelen = len;
2624 ctxt->nodemem = len + 1;
2632 * xmlSAX2Characters:
2633 * @ctx: the user data (XML parser context)
2634 * @ch: a xmlChar string
2635 * @len: the number of xmlChar
2637 * receiving some chars from the parser.
2639 void
2640 xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2642 xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE);
2646 * xmlSAX2IgnorableWhitespace:
2647 * @ctx: the user data (XML parser context)
2648 * @ch: a xmlChar string
2649 * @len: the number of xmlChar
2651 * receiving some ignorable whitespaces from the parser.
2652 * UNUSED: by default the DOM building will use xmlSAX2Characters
2654 void
2655 xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2657 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2658 #ifdef DEBUG_SAX
2659 xmlGenericError(xmlGenericErrorContext,
2660 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2661 #endif
2665 * xmlSAX2ProcessingInstruction:
2666 * @ctx: the user data (XML parser context)
2667 * @target: the target name
2668 * @data: the PI data's
2670 * A processing instruction has been parsed.
2672 void
2673 xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2674 const xmlChar *data)
2676 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2677 xmlNodePtr ret;
2678 xmlNodePtr parent;
2680 if (ctx == NULL) return;
2681 parent = ctxt->node;
2682 #ifdef DEBUG_SAX
2683 xmlGenericError(xmlGenericErrorContext,
2684 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2685 #endif
2687 ret = xmlNewDocPI(ctxt->myDoc, target, data);
2688 if (ret == NULL) return;
2690 if (ctxt->linenumbers) {
2691 if (ctxt->input != NULL) {
2692 if (ctxt->input->line < USHRT_MAX)
2693 ret->line = (unsigned short) ctxt->input->line;
2694 else
2695 ret->line = USHRT_MAX;
2698 if (ctxt->inSubset == 1) {
2699 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2700 return;
2701 } else if (ctxt->inSubset == 2) {
2702 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2703 return;
2705 if (parent == NULL) {
2706 #ifdef DEBUG_SAX_TREE
2707 xmlGenericError(xmlGenericErrorContext,
2708 "Setting PI %s as root\n", target);
2709 #endif
2710 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2711 return;
2713 if (parent->type == XML_ELEMENT_NODE) {
2714 #ifdef DEBUG_SAX_TREE
2715 xmlGenericError(xmlGenericErrorContext,
2716 "adding PI %s child to %s\n", target, parent->name);
2717 #endif
2718 xmlAddChild(parent, ret);
2719 } else {
2720 #ifdef DEBUG_SAX_TREE
2721 xmlGenericError(xmlGenericErrorContext,
2722 "adding PI %s sibling to ", target);
2723 xmlDebugDumpOneNode(stderr, parent, 0);
2724 #endif
2725 xmlAddSibling(parent, ret);
2730 * xmlSAX2Comment:
2731 * @ctx: the user data (XML parser context)
2732 * @value: the xmlSAX2Comment content
2734 * A xmlSAX2Comment has been parsed.
2736 void
2737 xmlSAX2Comment(void *ctx, const xmlChar *value)
2739 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2740 xmlNodePtr ret;
2741 xmlNodePtr parent;
2743 if (ctx == NULL) return;
2744 parent = ctxt->node;
2745 #ifdef DEBUG_SAX
2746 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2747 #endif
2748 ret = xmlNewDocComment(ctxt->myDoc, value);
2749 if (ret == NULL) return;
2750 if (ctxt->linenumbers) {
2751 if (ctxt->input != NULL) {
2752 if (ctxt->input->line < USHRT_MAX)
2753 ret->line = (unsigned short) ctxt->input->line;
2754 else
2755 ret->line = USHRT_MAX;
2759 if (ctxt->inSubset == 1) {
2760 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2761 return;
2762 } else if (ctxt->inSubset == 2) {
2763 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2764 return;
2766 if (parent == NULL) {
2767 #ifdef DEBUG_SAX_TREE
2768 xmlGenericError(xmlGenericErrorContext,
2769 "Setting xmlSAX2Comment as root\n");
2770 #endif
2771 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2772 return;
2774 if (parent->type == XML_ELEMENT_NODE) {
2775 #ifdef DEBUG_SAX_TREE
2776 xmlGenericError(xmlGenericErrorContext,
2777 "adding xmlSAX2Comment child to %s\n", parent->name);
2778 #endif
2779 xmlAddChild(parent, ret);
2780 } else {
2781 #ifdef DEBUG_SAX_TREE
2782 xmlGenericError(xmlGenericErrorContext,
2783 "adding xmlSAX2Comment sibling to ");
2784 xmlDebugDumpOneNode(stderr, parent, 0);
2785 #endif
2786 xmlAddSibling(parent, ret);
2791 * xmlSAX2CDataBlock:
2792 * @ctx: the user data (XML parser context)
2793 * @value: The pcdata content
2794 * @len: the block length
2796 * called when a pcdata block has been parsed
2798 void
2799 xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2801 xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE);
2804 static int xmlSAX2DefaultVersionValue = 2;
2806 #ifdef LIBXML_SAX1_ENABLED
2808 * xmlSAXDefaultVersion:
2809 * @version: the version, 1 or 2
2811 * Set the default version of SAX used globally by the library.
2812 * By default, during initialization the default is set to 2.
2813 * Note that it is generally a better coding style to use
2814 * xmlSAXVersion() to set up the version explicitly for a given
2815 * parsing context.
2817 * Returns the previous value in case of success and -1 in case of error.
2820 xmlSAXDefaultVersion(int version)
2822 int ret = xmlSAX2DefaultVersionValue;
2824 if ((version != 1) && (version != 2))
2825 return(-1);
2826 xmlSAX2DefaultVersionValue = version;
2827 return(ret);
2829 #endif /* LIBXML_SAX1_ENABLED */
2832 * xmlSAXVersion:
2833 * @hdlr: the SAX handler
2834 * @version: the version, 1 or 2
2836 * Initialize the default XML SAX handler according to the version
2838 * Returns 0 in case of success and -1 in case of error.
2841 xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2843 if (hdlr == NULL) return(-1);
2844 if (version == 2) {
2845 hdlr->startElement = NULL;
2846 hdlr->endElement = NULL;
2847 hdlr->startElementNs = xmlSAX2StartElementNs;
2848 hdlr->endElementNs = xmlSAX2EndElementNs;
2849 hdlr->serror = NULL;
2850 hdlr->initialized = XML_SAX2_MAGIC;
2851 #ifdef LIBXML_SAX1_ENABLED
2852 } else if (version == 1) {
2853 hdlr->startElement = xmlSAX2StartElement;
2854 hdlr->endElement = xmlSAX2EndElement;
2855 hdlr->initialized = 1;
2856 #endif /* LIBXML_SAX1_ENABLED */
2857 } else
2858 return(-1);
2859 hdlr->internalSubset = xmlSAX2InternalSubset;
2860 hdlr->externalSubset = xmlSAX2ExternalSubset;
2861 hdlr->isStandalone = xmlSAX2IsStandalone;
2862 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2863 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2864 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2865 hdlr->getEntity = xmlSAX2GetEntity;
2866 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2867 hdlr->entityDecl = xmlSAX2EntityDecl;
2868 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2869 hdlr->elementDecl = xmlSAX2ElementDecl;
2870 hdlr->notationDecl = xmlSAX2NotationDecl;
2871 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2872 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2873 hdlr->startDocument = xmlSAX2StartDocument;
2874 hdlr->endDocument = xmlSAX2EndDocument;
2875 hdlr->reference = xmlSAX2Reference;
2876 hdlr->characters = xmlSAX2Characters;
2877 hdlr->cdataBlock = xmlSAX2CDataBlock;
2878 hdlr->ignorableWhitespace = xmlSAX2Characters;
2879 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2880 hdlr->comment = xmlSAX2Comment;
2881 hdlr->warning = xmlParserWarning;
2882 hdlr->error = xmlParserError;
2883 hdlr->fatalError = xmlParserError;
2885 return(0);
2889 * xmlSAX2InitDefaultSAXHandler:
2890 * @hdlr: the SAX handler
2891 * @warning: flag if non-zero sets the handler warning procedure
2893 * Initialize the default XML SAX2 handler
2895 void
2896 xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2898 if ((hdlr == NULL) || (hdlr->initialized != 0))
2899 return;
2901 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2902 if (warning == 0)
2903 hdlr->warning = NULL;
2904 else
2905 hdlr->warning = xmlParserWarning;
2909 * xmlDefaultSAXHandlerInit:
2911 * Initialize the default SAX2 handler
2913 void
2914 xmlDefaultSAXHandlerInit(void)
2916 #ifdef LIBXML_SAX1_ENABLED
2917 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
2918 #endif /* LIBXML_SAX1_ENABLED */
2921 #ifdef LIBXML_HTML_ENABLED
2924 * xmlSAX2InitHtmlDefaultSAXHandler:
2925 * @hdlr: the SAX handler
2927 * Initialize the default HTML SAX2 handler
2929 void
2930 xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2932 if ((hdlr == NULL) || (hdlr->initialized != 0))
2933 return;
2935 hdlr->internalSubset = xmlSAX2InternalSubset;
2936 hdlr->externalSubset = NULL;
2937 hdlr->isStandalone = NULL;
2938 hdlr->hasInternalSubset = NULL;
2939 hdlr->hasExternalSubset = NULL;
2940 hdlr->resolveEntity = NULL;
2941 hdlr->getEntity = xmlSAX2GetEntity;
2942 hdlr->getParameterEntity = NULL;
2943 hdlr->entityDecl = NULL;
2944 hdlr->attributeDecl = NULL;
2945 hdlr->elementDecl = NULL;
2946 hdlr->notationDecl = NULL;
2947 hdlr->unparsedEntityDecl = NULL;
2948 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2949 hdlr->startDocument = xmlSAX2StartDocument;
2950 hdlr->endDocument = xmlSAX2EndDocument;
2951 hdlr->startElement = xmlSAX2StartElement;
2952 hdlr->endElement = xmlSAX2EndElement;
2953 hdlr->reference = NULL;
2954 hdlr->characters = xmlSAX2Characters;
2955 hdlr->cdataBlock = xmlSAX2CDataBlock;
2956 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2957 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2958 hdlr->comment = xmlSAX2Comment;
2959 hdlr->warning = xmlParserWarning;
2960 hdlr->error = xmlParserError;
2961 hdlr->fatalError = xmlParserError;
2963 hdlr->initialized = 1;
2967 * htmlDefaultSAXHandlerInit:
2969 * Initialize the default SAX handler
2971 void
2972 htmlDefaultSAXHandlerInit(void)
2974 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
2977 #endif /* LIBXML_HTML_ENABLED */
2979 #ifdef LIBXML_DOCB_ENABLED
2982 * xmlSAX2InitDocbDefaultSAXHandler:
2983 * @hdlr: the SAX handler
2985 * Initialize the default DocBook SAX2 handler
2987 void
2988 xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2990 if ((hdlr == NULL) || (hdlr->initialized != 0))
2991 return;
2993 hdlr->internalSubset = xmlSAX2InternalSubset;
2994 hdlr->externalSubset = NULL;
2995 hdlr->isStandalone = xmlSAX2IsStandalone;
2996 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2997 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2998 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2999 hdlr->getEntity = xmlSAX2GetEntity;
3000 hdlr->getParameterEntity = NULL;
3001 hdlr->entityDecl = xmlSAX2EntityDecl;
3002 hdlr->attributeDecl = NULL;
3003 hdlr->elementDecl = NULL;
3004 hdlr->notationDecl = NULL;
3005 hdlr->unparsedEntityDecl = NULL;
3006 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
3007 hdlr->startDocument = xmlSAX2StartDocument;
3008 hdlr->endDocument = xmlSAX2EndDocument;
3009 hdlr->startElement = xmlSAX2StartElement;
3010 hdlr->endElement = xmlSAX2EndElement;
3011 hdlr->reference = xmlSAX2Reference;
3012 hdlr->characters = xmlSAX2Characters;
3013 hdlr->cdataBlock = NULL;
3014 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
3015 hdlr->processingInstruction = NULL;
3016 hdlr->comment = xmlSAX2Comment;
3017 hdlr->warning = xmlParserWarning;
3018 hdlr->error = xmlParserError;
3019 hdlr->fatalError = xmlParserError;
3021 hdlr->initialized = 1;
3025 * docbDefaultSAXHandlerInit:
3027 * Initialize the default SAX handler
3029 void
3030 docbDefaultSAXHandlerInit(void)
3032 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
3035 #endif /* LIBXML_DOCB_ENABLED */
3036 #define bottom_SAX2
3037 #include "elfgcchack.h"