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>
15 #include <libxml/xmlmemory.h>
16 #include <libxml/tree.h>
17 #include <libxml/parser.h>
18 #include <libxml/parserInternals.h>
19 #include <libxml/valid.h>
20 #include <libxml/entities.h>
21 #include <libxml/xmlerror.h>
22 #include <libxml/debugXML.h>
23 #include <libxml/xmlIO.h>
24 #include <libxml/SAX.h>
25 #include <libxml/uri.h>
26 #include <libxml/valid.h>
27 #include <libxml/HTMLtree.h>
28 #include <libxml/globals.h>
30 /* Define SIZE_T_MAX unless defined through <limits.h>. */
32 # define SIZE_T_MAX ((size_t)-1)
33 #endif /* !SIZE_T_MAX */
35 /* #define DEBUG_SAX2 */
36 /* #define DEBUG_SAX2_TREE */
41 * macro to flag unimplemented blocks
42 * XML_CATALOG_PREFER user env to select between system/public prefered
43 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
44 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
45 *> values "system" and "public". I have made the default be "system" to
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
55 * @ctxt: an XML validation parser context
56 * @msg: a string to accompany the error message
59 xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
60 xmlStructuredErrorFunc schannel = NULL;
61 const char *str1 = "out of memory\n";
64 ctxt->errNo = XML_ERR_NO_MEMORY;
65 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
66 schannel = ctxt->sax->serror;
67 __xmlRaiseError(schannel,
68 ctxt->vctxt.error, ctxt->vctxt.userData,
69 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
70 XML_ERR_ERROR, NULL, 0, (const char *) str1,
72 msg, (const char *) str1, NULL);
73 ctxt->errNo = XML_ERR_NO_MEMORY;
74 ctxt->instate = XML_PARSER_EOF;
77 __xmlRaiseError(schannel,
79 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
80 XML_ERR_ERROR, NULL, 0, (const char *) str1,
82 msg, (const char *) str1, NULL);
88 * @ctxt: an XML validation parser context
89 * @error: the error number
90 * @msg: the error message
94 * Handle a validation error
97 xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
98 const char *msg, const char *str1, const char *str2)
100 xmlStructuredErrorFunc schannel = NULL;
102 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
103 (ctxt->instate == XML_PARSER_EOF))
107 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
108 schannel = ctxt->sax->serror;
109 __xmlRaiseError(schannel,
110 ctxt->vctxt.error, ctxt->vctxt.userData,
111 ctxt, NULL, XML_FROM_DTD, error,
112 XML_ERR_ERROR, NULL, 0, (const char *) str1,
113 (const char *) str2, NULL, 0, 0,
114 msg, (const char *) str1, (const char *) str2);
117 __xmlRaiseError(schannel,
119 ctxt, NULL, XML_FROM_DTD, error,
120 XML_ERR_ERROR, NULL, 0, (const char *) str1,
121 (const char *) str2, NULL, 0, 0,
122 msg, (const char *) str1, (const char *) str2);
128 * @ctxt: an XML parser context
129 * @error: the error number
130 * @msg: the error message
131 * @str1: an error string
132 * @str2: an error string
134 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
137 xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
138 const char *msg, const xmlChar *str1, const xmlChar *str2)
140 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
141 (ctxt->instate == XML_PARSER_EOF))
145 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
146 XML_ERR_FATAL, NULL, 0,
147 (const char *) str1, (const char *) str2,
148 NULL, 0, 0, msg, str1, str2);
150 ctxt->wellFormed = 0;
152 if (ctxt->recovery == 0)
153 ctxt->disableSAX = 1;
159 * @ctxt: an XML parser context
160 * @error: the error number
161 * @msg: the error message
162 * @str1: an error string
163 * @str2: an error string
165 * Handle a parser warning
168 xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
169 const char *msg, const xmlChar *str1)
171 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
172 (ctxt->instate == XML_PARSER_EOF))
176 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
177 XML_ERR_WARNING, NULL, 0,
178 (const char *) str1, NULL,
179 NULL, 0, 0, msg, str1);
184 * @ctxt: an XML parser context
185 * @error: the error number
186 * @msg: the error message
187 * @str1: an error string
188 * @str2: an error string
190 * Handle a namespace error
193 xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
194 const char *msg, const xmlChar *str1, const xmlChar *str2)
196 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
197 (ctxt->instate == XML_PARSER_EOF))
201 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
202 XML_ERR_ERROR, NULL, 0,
203 (const char *) str1, (const char *) str2,
204 NULL, 0, 0, msg, str1, str2);
209 * @ctxt: an XML parser context
210 * @error: the error number
211 * @msg: the error message
212 * @str1: an error string
214 * Handle a namespace warning
217 xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
218 const char *msg, const xmlChar *str1, const xmlChar *str2)
220 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
221 (ctxt->instate == XML_PARSER_EOF))
225 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
226 XML_ERR_WARNING, NULL, 0,
227 (const char *) str1, (const char *) str2,
228 NULL, 0, 0, msg, str1, str2);
232 * xmlSAX2GetPublicId:
233 * @ctx: the user data (XML parser context)
235 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
237 * Returns a xmlChar *
240 xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
242 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
247 * xmlSAX2GetSystemId:
248 * @ctx: the user data (XML parser context)
250 * Provides the system ID, basically URL or filename e.g.
251 * http://www.sgmlsource.com/dtds/memo.dtd
253 * Returns a xmlChar *
256 xmlSAX2GetSystemId(void *ctx)
258 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
259 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
260 return((const xmlChar *) ctxt->input->filename);
264 * xmlSAX2GetLineNumber:
265 * @ctx: the user data (XML parser context)
267 * Provide the line number of the current parsing point.
272 xmlSAX2GetLineNumber(void *ctx)
274 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
275 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
276 return(ctxt->input->line);
280 * xmlSAX2GetColumnNumber:
281 * @ctx: the user data (XML parser context)
283 * Provide the column number of the current parsing point.
288 xmlSAX2GetColumnNumber(void *ctx)
290 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
291 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
292 return(ctxt->input->col);
296 * xmlSAX2IsStandalone:
297 * @ctx: the user data (XML parser context)
299 * Is this document tagged standalone ?
304 xmlSAX2IsStandalone(void *ctx)
306 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
307 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
308 return(ctxt->myDoc->standalone == 1);
312 * xmlSAX2HasInternalSubset:
313 * @ctx: the user data (XML parser context)
315 * Does this document has an internal subset
320 xmlSAX2HasInternalSubset(void *ctx)
322 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
323 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
324 return(ctxt->myDoc->intSubset != NULL);
328 * xmlSAX2HasExternalSubset:
329 * @ctx: the user data (XML parser context)
331 * Does this document has an external subset
336 xmlSAX2HasExternalSubset(void *ctx)
338 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
339 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
340 return(ctxt->myDoc->extSubset != NULL);
344 * xmlSAX2InternalSubset:
345 * @ctx: the user data (XML parser context)
346 * @name: the root element name
347 * @ExternalID: the external ID
348 * @SystemID: the SYSTEM ID (e.g. filename or URL)
350 * Callback on internal subset declaration.
353 xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
354 const xmlChar *ExternalID, const xmlChar *SystemID)
356 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
358 if (ctx == NULL) return;
360 xmlGenericError(xmlGenericErrorContext,
361 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
362 name, ExternalID, SystemID);
365 if (ctxt->myDoc == NULL)
367 dtd = xmlGetIntSubset(ctxt->myDoc);
371 xmlUnlinkNode((xmlNodePtr) dtd);
373 ctxt->myDoc->intSubset = NULL;
375 ctxt->myDoc->intSubset =
376 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
377 if (ctxt->myDoc->intSubset == NULL)
378 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
382 * xmlSAX2ExternalSubset:
383 * @ctx: the user data (XML parser context)
384 * @name: the root element name
385 * @ExternalID: the external ID
386 * @SystemID: the SYSTEM ID (e.g. filename or URL)
388 * Callback on external subset declaration.
391 xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
392 const xmlChar *ExternalID, const xmlChar *SystemID)
394 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
395 if (ctx == NULL) return;
397 xmlGenericError(xmlGenericErrorContext,
398 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
399 name, ExternalID, SystemID);
401 if (((ExternalID != NULL) || (SystemID != NULL)) &&
402 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
403 (ctxt->wellFormed && ctxt->myDoc))) {
405 * Try to fetch and parse the external subset.
407 xmlParserInputPtr oldinput;
410 xmlParserInputPtr *oldinputTab;
411 xmlParserInputPtr input = NULL;
414 const xmlChar *oldencoding;
417 * Ask the Entity resolver to load the damn thing
419 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
420 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
426 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
429 * make sure we won't destroy the main document context
431 oldinput = ctxt->input;
432 oldinputNr = ctxt->inputNr;
433 oldinputMax = ctxt->inputMax;
434 oldinputTab = ctxt->inputTab;
435 oldcharset = ctxt->charset;
436 oldencoding = ctxt->encoding;
437 ctxt->encoding = NULL;
439 ctxt->inputTab = (xmlParserInputPtr *)
440 xmlMalloc(5 * sizeof(xmlParserInputPtr));
441 if (ctxt->inputTab == NULL) {
442 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
443 ctxt->input = oldinput;
444 ctxt->inputNr = oldinputNr;
445 ctxt->inputMax = oldinputMax;
446 ctxt->inputTab = oldinputTab;
447 ctxt->charset = oldcharset;
448 ctxt->encoding = oldencoding;
454 xmlPushInput(ctxt, input);
457 * On the fly encoding conversion if needed
459 if (ctxt->input->length >= 4) {
460 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
461 xmlSwitchEncoding(ctxt, enc);
464 if (input->filename == NULL)
465 input->filename = (char *) xmlCanonicPath(SystemID);
468 input->base = ctxt->input->cur;
469 input->cur = ctxt->input->cur;
473 * let's parse that entity knowing it's an external subset.
475 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
478 * Free up the external entities
481 while (ctxt->inputNr > 1)
483 xmlFreeInputStream(ctxt->input);
484 xmlFree(ctxt->inputTab);
487 * Restore the parsing context of the main entity
489 ctxt->input = oldinput;
490 ctxt->inputNr = oldinputNr;
491 ctxt->inputMax = oldinputMax;
492 ctxt->inputTab = oldinputTab;
493 ctxt->charset = oldcharset;
494 if ((ctxt->encoding != NULL) &&
495 ((ctxt->dict == NULL) ||
496 (!xmlDictOwns(ctxt->dict, ctxt->encoding))))
497 xmlFree((xmlChar *) ctxt->encoding);
498 ctxt->encoding = oldencoding;
499 /* ctxt->wellFormed = oldwellFormed; */
504 * xmlSAX2ResolveEntity:
505 * @ctx: the user data (XML parser context)
506 * @publicId: The public ID of the entity
507 * @systemId: The system ID of the entity
509 * The entity loader, to control the loading of external entities,
510 * the application can either:
511 * - override this xmlSAX2ResolveEntity() callback in the SAX block
512 * - or better use the xmlSetExternalEntityLoader() function to
513 * set up it's own entity resolution routine
515 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
518 xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
520 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
521 xmlParserInputPtr ret;
523 const char *base = NULL;
525 if (ctx == NULL) return(NULL);
526 if (ctxt->input != NULL)
527 base = ctxt->input->filename;
529 base = ctxt->directory;
531 URI = xmlBuildURI(systemId, (const xmlChar *) base);
534 xmlGenericError(xmlGenericErrorContext,
535 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
538 ret = xmlLoadExternalEntity((const char *) URI,
539 (const char *) publicId, ctxt);
547 * @ctx: the user data (XML parser context)
548 * @name: The entity name
550 * Get an entity by name
552 * Returns the xmlEntityPtr if found.
555 xmlSAX2GetEntity(void *ctx, const xmlChar *name)
557 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
558 xmlEntityPtr ret = NULL;
560 if (ctx == NULL) return(NULL);
562 xmlGenericError(xmlGenericErrorContext,
563 "SAX.xmlSAX2GetEntity(%s)\n", name);
566 if (ctxt->inSubset == 0) {
567 ret = xmlGetPredefinedEntity(name);
571 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
572 if (ctxt->inSubset == 2) {
573 ctxt->myDoc->standalone = 0;
574 ret = xmlGetDocEntity(ctxt->myDoc, name);
575 ctxt->myDoc->standalone = 1;
577 ret = xmlGetDocEntity(ctxt->myDoc, name);
579 ctxt->myDoc->standalone = 0;
580 ret = xmlGetDocEntity(ctxt->myDoc, name);
582 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
583 "Entity(%s) document marked standalone but requires external subset\n",
586 ctxt->myDoc->standalone = 1;
590 ret = xmlGetDocEntity(ctxt->myDoc, name);
593 ((ctxt->validate) || (ctxt->replaceEntities)) &&
594 (ret->children == NULL) &&
595 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
599 * for validation purposes we really need to fetch and
600 * parse the external entity
603 unsigned long oldnbent = ctxt->nbentities;
605 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
606 ret->ExternalID, &children);
608 xmlAddChildList((xmlNodePtr) ret, children);
610 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
611 "Failure to process entity %s\n", name, NULL);
616 if (ret->checked == 0) {
617 ret->checked = (ctxt->nbentities - oldnbent + 1) * 2;
618 if ((ret->content != NULL) && (xmlStrchr(ret->content, '<')))
626 * xmlSAX2GetParameterEntity:
627 * @ctx: the user data (XML parser context)
628 * @name: The entity name
630 * Get a parameter entity by name
632 * Returns the xmlEntityPtr if found.
635 xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
637 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
640 if (ctx == NULL) return(NULL);
642 xmlGenericError(xmlGenericErrorContext,
643 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
646 ret = xmlGetParameterEntity(ctxt->myDoc, name);
653 * @ctx: the user data (XML parser context)
654 * @name: the entity name
655 * @type: the entity type
656 * @publicId: The public ID of the entity
657 * @systemId: The system ID of the entity
658 * @content: the entity value (without processing).
660 * An entity definition has been parsed
663 xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
664 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
667 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
669 if (ctx == NULL) return;
671 xmlGenericError(xmlGenericErrorContext,
672 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
673 name, type, publicId, systemId, content);
675 if (ctxt->inSubset == 1) {
676 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
678 if ((ent == NULL) && (ctxt->pedantic))
679 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
680 "Entity(%s) already defined in the internal subset\n",
682 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
684 const char *base = NULL;
686 if (ctxt->input != NULL)
687 base = ctxt->input->filename;
689 base = ctxt->directory;
691 URI = xmlBuildURI(systemId, (const xmlChar *) base);
694 } else if (ctxt->inSubset == 2) {
695 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
697 if ((ent == NULL) && (ctxt->pedantic) &&
698 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
699 ctxt->sax->warning(ctxt->userData,
700 "Entity(%s) already defined in the external subset\n", name);
701 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
703 const char *base = NULL;
705 if (ctxt->input != NULL)
706 base = ctxt->input->filename;
708 base = ctxt->directory;
710 URI = xmlBuildURI(systemId, (const xmlChar *) base);
714 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
715 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
721 * xmlSAX2AttributeDecl:
722 * @ctx: the user data (XML parser context)
723 * @elem: the name of the element
724 * @fullname: the attribute name
725 * @type: the attribute type
726 * @def: the type of default value
727 * @defaultValue: the attribute default value
728 * @tree: the tree of enumerated value set
730 * An attribute definition has been parsed
733 xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
734 int type, int def, const xmlChar *defaultValue,
735 xmlEnumerationPtr tree)
737 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
738 xmlAttributePtr attr;
739 xmlChar *name = NULL, *prefix = NULL;
741 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
745 xmlGenericError(xmlGenericErrorContext,
746 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
747 elem, fullname, type, def, defaultValue);
749 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
750 (type != XML_ATTRIBUTE_ID)) {
752 * Raise the error but keep the validity flag
754 int tmp = ctxt->valid;
755 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
756 "xml:id : attribute type should be ID\n", NULL, NULL);
759 /* TODO: optimize name/prefix allocation */
760 name = xmlSplitQName(ctxt, fullname, &prefix);
761 ctxt->vctxt.valid = 1;
762 if (ctxt->inSubset == 1)
763 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
764 name, prefix, (xmlAttributeType) type,
765 (xmlAttributeDefault) def, defaultValue, tree);
766 else if (ctxt->inSubset == 2)
767 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
768 name, prefix, (xmlAttributeType) type,
769 (xmlAttributeDefault) def, defaultValue, tree);
771 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
772 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
774 xmlFreeEnumeration(tree);
777 #ifdef LIBXML_VALID_ENABLED
778 if (ctxt->vctxt.valid == 0)
780 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
781 (ctxt->myDoc->intSubset != NULL))
782 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
784 #endif /* LIBXML_VALID_ENABLED */
792 * xmlSAX2ElementDecl:
793 * @ctx: the user data (XML parser context)
794 * @name: the element name
795 * @type: the element type
796 * @content: the element value tree
798 * An element definition has been parsed
801 xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
802 xmlElementContentPtr content)
804 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
805 xmlElementPtr elem = NULL;
807 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
811 xmlGenericError(xmlGenericErrorContext,
812 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
815 if (ctxt->inSubset == 1)
816 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
817 name, (xmlElementTypeVal) type, content);
818 else if (ctxt->inSubset == 2)
819 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
820 name, (xmlElementTypeVal) type, content);
822 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
823 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
827 #ifdef LIBXML_VALID_ENABLED
830 if (ctxt->validate && ctxt->wellFormed &&
831 ctxt->myDoc && ctxt->myDoc->intSubset)
833 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
834 #endif /* LIBXML_VALID_ENABLED */
838 * xmlSAX2NotationDecl:
839 * @ctx: the user data (XML parser context)
840 * @name: The name of the notation
841 * @publicId: The public ID of the entity
842 * @systemId: The system ID of the entity
844 * What to do when a notation declaration has been parsed.
847 xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
848 const xmlChar *publicId, const xmlChar *systemId)
850 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
851 xmlNotationPtr nota = NULL;
853 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
857 xmlGenericError(xmlGenericErrorContext,
858 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
861 if ((publicId == NULL) && (systemId == NULL)) {
862 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
863 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
866 } else if (ctxt->inSubset == 1)
867 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
869 else if (ctxt->inSubset == 2)
870 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
873 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
874 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
878 #ifdef LIBXML_VALID_ENABLED
879 if (nota == NULL) ctxt->valid = 0;
880 if ((ctxt->validate) && (ctxt->wellFormed) &&
881 (ctxt->myDoc->intSubset != NULL))
882 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
884 #endif /* LIBXML_VALID_ENABLED */
888 * xmlSAX2UnparsedEntityDecl:
889 * @ctx: the user data (XML parser context)
890 * @name: The name of the entity
891 * @publicId: The public ID of the entity
892 * @systemId: The system ID of the entity
893 * @notationName: the name of the notation
895 * What to do when an unparsed entity declaration is parsed
898 xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
899 const xmlChar *publicId, const xmlChar *systemId,
900 const xmlChar *notationName)
903 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
904 if (ctx == NULL) return;
906 xmlGenericError(xmlGenericErrorContext,
907 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
908 name, publicId, systemId, notationName);
910 if (ctxt->inSubset == 1) {
911 ent = xmlAddDocEntity(ctxt->myDoc, name,
912 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
913 publicId, systemId, notationName);
914 if ((ent == NULL) && (ctxt->pedantic) &&
915 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
916 ctxt->sax->warning(ctxt->userData,
917 "Entity(%s) already defined in the internal subset\n", name);
918 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
920 const char *base = NULL;
922 if (ctxt->input != NULL)
923 base = ctxt->input->filename;
925 base = ctxt->directory;
927 URI = xmlBuildURI(systemId, (const xmlChar *) base);
930 } else if (ctxt->inSubset == 2) {
931 ent = xmlAddDtdEntity(ctxt->myDoc, name,
932 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
933 publicId, systemId, notationName);
934 if ((ent == NULL) && (ctxt->pedantic) &&
935 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
936 ctxt->sax->warning(ctxt->userData,
937 "Entity(%s) already defined in the external subset\n", name);
938 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
940 const char *base = NULL;
942 if (ctxt->input != NULL)
943 base = ctxt->input->filename;
945 base = ctxt->directory;
947 URI = xmlBuildURI(systemId, (const xmlChar *) base);
951 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
952 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
958 * xmlSAX2SetDocumentLocator:
959 * @ctx: the user data (XML parser context)
960 * @loc: A SAX Locator
962 * Receive the document locator at startup, actually xmlDefaultSAXLocator
963 * Everything is available on the context, so this is useless in our case.
966 xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
968 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
970 xmlGenericError(xmlGenericErrorContext,
971 "SAX.xmlSAX2SetDocumentLocator()\n");
976 * xmlSAX2StartDocument:
977 * @ctx: the user data (XML parser context)
979 * called when the document start being processed.
982 xmlSAX2StartDocument(void *ctx)
984 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
987 if (ctx == NULL) return;
990 xmlGenericError(xmlGenericErrorContext,
991 "SAX.xmlSAX2StartDocument()\n");
994 #ifdef LIBXML_HTML_ENABLED
995 if (ctxt->myDoc == NULL)
996 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
997 ctxt->myDoc->properties = XML_DOC_HTML;
998 ctxt->myDoc->parseFlags = ctxt->options;
999 if (ctxt->myDoc == NULL) {
1000 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
1004 xmlGenericError(xmlGenericErrorContext,
1005 "libxml2 built without HTML support\n");
1006 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
1007 ctxt->instate = XML_PARSER_EOF;
1008 ctxt->disableSAX = 1;
1012 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
1014 doc->properties = 0;
1015 if (ctxt->options & XML_PARSE_OLD10)
1016 doc->properties |= XML_DOC_OLD10;
1017 doc->parseFlags = ctxt->options;
1018 if (ctxt->encoding != NULL)
1019 doc->encoding = xmlStrdup(ctxt->encoding);
1021 doc->encoding = NULL;
1022 doc->standalone = ctxt->standalone;
1024 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
1027 if ((ctxt->dictNames) && (doc != NULL)) {
1028 doc->dict = ctxt->dict;
1029 xmlDictReference(doc->dict);
1032 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1033 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
1034 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
1035 if (ctxt->myDoc->URL == NULL)
1036 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
1041 * xmlSAX2EndDocument:
1042 * @ctx: the user data (XML parser context)
1044 * called when the document end has been detected.
1047 xmlSAX2EndDocument(void *ctx)
1049 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1051 xmlGenericError(xmlGenericErrorContext,
1052 "SAX.xmlSAX2EndDocument()\n");
1054 if (ctx == NULL) return;
1055 #ifdef LIBXML_VALID_ENABLED
1056 if (ctxt->validate && ctxt->wellFormed &&
1057 ctxt->myDoc && ctxt->myDoc->intSubset)
1058 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
1059 #endif /* LIBXML_VALID_ENABLED */
1062 * Grab the encoding if it was added on-the-fly
1064 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1065 (ctxt->myDoc->encoding == NULL)) {
1066 ctxt->myDoc->encoding = ctxt->encoding;
1067 ctxt->encoding = NULL;
1069 if ((ctxt->inputTab != NULL) &&
1070 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1071 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
1072 (ctxt->myDoc->encoding == NULL)) {
1073 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1075 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1076 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1077 ctxt->myDoc->charset = ctxt->charset;
1081 #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
1083 * xmlSAX2AttributeInternal:
1084 * @ctx: the user data (XML parser context)
1085 * @fullname: The attribute name, including namespace prefix
1086 * @value: The attribute value
1087 * @prefix: the prefix on the element node
1089 * Handle an attribute that has been read by the parser.
1090 * The default handling is to convert the attribute into an
1091 * DOM subtree and past it in a new xmlAttr element added to
1095 xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
1096 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
1098 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1106 name = xmlStrdup(fullname);
1111 * Split the full name into a namespace prefix and the tag name
1113 name = xmlSplitQName(ctxt, fullname, &ns);
1114 if ((name != NULL) && (name[0] == 0)) {
1115 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1116 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1117 "invalid namespace declaration '%s'\n",
1120 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1121 "Avoid attribute ending with ':' like '%s'\n",
1128 name = xmlStrdup(fullname);
1132 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1138 #ifdef LIBXML_HTML_ENABLED
1140 (value == NULL) && (htmlIsBooleanAttr(fullname))) {
1141 nval = xmlStrdup(fullname);
1142 value = (const xmlChar *) nval;
1146 #ifdef LIBXML_VALID_ENABLED
1148 * Do the last stage of the attribute normalization
1149 * Needed for HTML too:
1150 * http://www.w3.org/TR/html4/types.html#h-6.2
1152 ctxt->vctxt.valid = 1;
1153 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1154 ctxt->myDoc, ctxt->node,
1156 if (ctxt->vctxt.valid != 1) {
1163 #endif /* LIBXML_VALID_ENABLED */
1167 * Check whether it's a namespace definition
1169 if ((!ctxt->html) && (ns == NULL) &&
1170 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1171 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1175 if (!ctxt->replaceEntities) {
1177 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1181 val = (xmlChar *) value;
1187 uri = xmlParseURI((const char *)val);
1189 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1190 ctxt->sax->warning(ctxt->userData,
1191 "xmlns: %s not a valid URI\n", val);
1193 if (uri->scheme == NULL) {
1194 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1195 ctxt->sax->warning(ctxt->userData,
1196 "xmlns: URI %s is not absolute\n", val);
1202 /* a default namespace definition */
1203 nsret = xmlNewNs(ctxt->node, val, NULL);
1205 #ifdef LIBXML_VALID_ENABLED
1207 * Validate also for namespace decls, they are attributes from
1208 * an XML-1.0 perspective
1210 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1211 ctxt->myDoc && ctxt->myDoc->intSubset)
1212 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1213 ctxt->node, prefix, nsret, val);
1214 #endif /* LIBXML_VALID_ENABLED */
1223 if ((!ctxt->html) &&
1224 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1225 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1229 if (!ctxt->replaceEntities) {
1231 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1235 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1242 val = (xmlChar *) value;
1246 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1247 "Empty namespace name for prefix %s\n", name, NULL);
1249 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1252 uri = xmlParseURI((const char *)val);
1254 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
1255 "xmlns:%s: %s not a valid URI\n", name, value);
1257 if (uri->scheme == NULL) {
1258 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
1259 "xmlns:%s: URI %s is not absolute\n", name, value);
1265 /* a standard namespace definition */
1266 nsret = xmlNewNs(ctxt->node, val, name);
1268 #ifdef LIBXML_VALID_ENABLED
1270 * Validate also for namespace decls, they are attributes from
1271 * an XML-1.0 perspective
1273 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1274 ctxt->myDoc && ctxt->myDoc->intSubset)
1275 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1276 ctxt->node, prefix, nsret, value);
1277 #endif /* LIBXML_VALID_ENABLED */
1288 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
1290 if (namespace == NULL) {
1291 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1292 "Namespace prefix %s of attribute %s is not defined\n",
1297 prop = ctxt->node->properties;
1298 while (prop != NULL) {
1299 if (prop->ns != NULL) {
1300 if ((xmlStrEqual(name, prop->name)) &&
1301 ((namespace == prop->ns) ||
1302 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1303 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1304 "Attribute %s in %s redefined\n",
1305 name, namespace->href);
1306 ctxt->wellFormed = 0;
1307 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1318 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1319 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1322 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1325 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1326 tmp = ret->children;
1327 while (tmp != NULL) {
1328 tmp->parent = (xmlNodePtr) ret;
1329 if (tmp->next == NULL)
1333 } else if (value != NULL) {
1334 ret->children = xmlNewDocText(ctxt->myDoc, value);
1335 ret->last = ret->children;
1336 if (ret->children != NULL)
1337 ret->children->parent = (xmlNodePtr) ret;
1341 #ifdef LIBXML_VALID_ENABLED
1342 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1343 ctxt->myDoc && ctxt->myDoc->intSubset) {
1346 * If we don't substitute entities, the validation should be
1347 * done on a value with replaced entities anyway.
1349 if (!ctxt->replaceEntities) {
1353 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1358 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1359 ctxt->myDoc, ctxt->node, ret, value);
1364 * Do the last stage of the attribute normalization
1365 * It need to be done twice ... it's an extra burden related
1366 * to the ability to keep xmlSAX2References in attributes
1368 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1369 ctxt->node, fullname, val);
1370 if (nvalnorm != NULL) {
1375 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1376 ctxt->myDoc, ctxt->node, ret, val);
1380 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1381 ctxt->node, ret, value);
1384 #endif /* LIBXML_VALID_ENABLED */
1385 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1386 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1387 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1389 * when validating, the ID registration is done at the attribute
1390 * validation level. Otherwise we have to do specific handling here.
1392 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
1394 * Add the xml:id value
1396 * Open issue: normalization of the value.
1398 if (xmlValidateNCName(value, 1) != 0) {
1399 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1400 "xml:id : attribute value %s is not an NCName\n",
1401 (const char *) value, NULL);
1403 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1404 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1405 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1406 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1407 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1418 * xmlCheckDefaultedAttributes:
1420 * Check defaulted attributes from the DTD
1423 xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1424 const xmlChar *prefix, const xmlChar **atts) {
1425 xmlElementPtr elemDecl;
1430 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1431 if (elemDecl == NULL) {
1432 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1436 process_external_subset:
1438 if (elemDecl != NULL) {
1439 xmlAttributePtr attr = elemDecl->attributes;
1441 * Check against defaulted attributes from the external subset
1442 * if the document is stamped as standalone
1444 if ((ctxt->myDoc->standalone == 1) &&
1445 (ctxt->myDoc->extSubset != NULL) &&
1447 while (attr != NULL) {
1448 if ((attr->defaultValue != NULL) &&
1449 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1450 attr->elem, attr->name,
1451 attr->prefix) == attr) &&
1452 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1453 attr->elem, attr->name,
1454 attr->prefix) == NULL)) {
1457 if (attr->prefix != NULL) {
1458 fulln = xmlStrdup(attr->prefix);
1459 fulln = xmlStrcat(fulln, BAD_CAST ":");
1460 fulln = xmlStrcat(fulln, attr->name);
1462 fulln = xmlStrdup(attr->name);
1464 if (fulln == NULL) {
1465 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1470 * Check that the attribute is not declared in the
1477 while (att != NULL) {
1478 if (xmlStrEqual(att, fulln))
1485 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
1486 "standalone: attribute %s on %s defaulted from external subset\n",
1487 (const char *)fulln,
1488 (const char *)attr->elem);
1497 * Actually insert defaulted values when needed
1499 attr = elemDecl->attributes;
1500 while (attr != NULL) {
1502 * Make sure that attributes redefinition occuring in the
1503 * internal subset are not overriden by definitions in the
1506 if (attr->defaultValue != NULL) {
1508 * the element should be instantiated in the tree if:
1509 * - this is a namespace prefix
1510 * - the user required for completion in the tree
1512 * - there isn't already an attribute definition
1513 * in the internal subset overriding it.
1515 if (((attr->prefix != NULL) &&
1516 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1517 ((attr->prefix == NULL) &&
1518 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1519 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1520 xmlAttributePtr tst;
1522 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1523 attr->elem, attr->name,
1525 if ((tst == attr) || (tst == NULL)) {
1529 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1530 if (fulln == NULL) {
1531 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1536 * Check that the attribute is not declared in the
1543 while (att != NULL) {
1544 if (xmlStrEqual(att, fulln))
1551 xmlSAX2AttributeInternal(ctxt, fulln,
1552 attr->defaultValue, prefix);
1554 if ((fulln != fn) && (fulln != attr->name))
1561 if (internal == 1) {
1562 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1565 goto process_external_subset;
1571 * xmlSAX2StartElement:
1572 * @ctx: the user data (XML parser context)
1573 * @fullname: The element name, including namespace prefix
1574 * @atts: An array of name/value attributes pairs, NULL terminated
1576 * called when an opening tag has been processed.
1579 xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1581 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1588 const xmlChar *value;
1591 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
1592 parent = ctxt->node;
1594 xmlGenericError(xmlGenericErrorContext,
1595 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1599 * First check on validity:
1601 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1602 ((ctxt->myDoc->intSubset == NULL) ||
1603 ((ctxt->myDoc->intSubset->notations == NULL) &&
1604 (ctxt->myDoc->intSubset->elements == NULL) &&
1605 (ctxt->myDoc->intSubset->attributes == NULL) &&
1606 (ctxt->myDoc->intSubset->entities == NULL)))) {
1607 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1608 "Validation failed: no DTD found !", NULL, NULL);
1614 * Split the full name into a namespace prefix and the tag name
1616 name = xmlSplitQName(ctxt, fullname, &prefix);
1620 * Note : the namespace resolution is deferred until the end of the
1621 * attributes parsing, since local namespace can be defined as
1622 * an attribute at this level.
1624 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1628 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1631 if (ctxt->myDoc->children == NULL) {
1632 #ifdef DEBUG_SAX_TREE
1633 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1635 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1636 } else if (parent == NULL) {
1637 parent = ctxt->myDoc->children;
1640 if (ctxt->linenumbers) {
1641 if (ctxt->input != NULL) {
1642 if (ctxt->input->line < 65535)
1643 ret->line = (short) ctxt->input->line;
1650 * We are parsing a new node.
1652 #ifdef DEBUG_SAX_TREE
1653 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1655 nodePush(ctxt, ret);
1658 * Link the child element
1660 if (parent != NULL) {
1661 if (parent->type == XML_ELEMENT_NODE) {
1662 #ifdef DEBUG_SAX_TREE
1663 xmlGenericError(xmlGenericErrorContext,
1664 "adding child %s to %s\n", name, parent->name);
1666 xmlAddChild(parent, ret);
1668 #ifdef DEBUG_SAX_TREE
1669 xmlGenericError(xmlGenericErrorContext,
1670 "adding sibling %s to ", name);
1671 xmlDebugDumpOneNode(stderr, parent, 0);
1673 xmlAddSibling(parent, ret);
1678 * Insert all the defaulted attributes from the DTD especially namespaces
1680 if ((!ctxt->html) &&
1681 ((ctxt->myDoc->intSubset != NULL) ||
1682 (ctxt->myDoc->extSubset != NULL))) {
1683 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1687 * process all the attributes whose name start with "xmlns"
1694 while ((att != NULL) && (value != NULL)) {
1695 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1696 (att[3] == 'n') && (att[4] == 's'))
1697 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1706 * Search the namespace, note that since the attributes have been
1707 * processed, the local namespaces are available.
1709 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1710 if ((ns == NULL) && (parent != NULL))
1711 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1712 if ((prefix != NULL) && (ns == NULL)) {
1713 ns = xmlNewNs(ret, NULL, prefix);
1714 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1715 "Namespace prefix %s is not defined\n",
1720 * set the namespace node, making sure that if the default namspace
1721 * is unbound on a parent we simply kee it NULL
1723 if ((ns != NULL) && (ns->href != NULL) &&
1724 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1728 * process all the other attributes
1735 while (att != NULL) {
1736 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1741 while ((att != NULL) && (value != NULL)) {
1742 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1743 (att[3] != 'n') || (att[4] != 's'))
1744 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1755 #ifdef LIBXML_VALID_ENABLED
1757 * If it's the Document root, finish the DTD validation and
1758 * check the document root element for validity
1760 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
1763 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1767 ctxt->wellFormed = 0;
1768 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1769 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
1771 #endif /* LIBXML_VALID_ENABLED */
1779 * xmlSAX2EndElement:
1780 * @ctx: the user data (XML parser context)
1781 * @name: The element name
1783 * called when the end of an element has been detected.
1786 xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1788 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1791 if (ctx == NULL) return;
1795 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1797 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1800 /* Capture end position and add node */
1801 if (cur != NULL && ctxt->record_info) {
1802 ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
1803 ctxt->nodeInfo->end_line = ctxt->input->line;
1804 ctxt->nodeInfo->node = cur;
1805 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
1809 #ifdef LIBXML_VALID_ENABLED
1810 if (ctxt->validate && ctxt->wellFormed &&
1811 ctxt->myDoc && ctxt->myDoc->intSubset)
1812 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1814 #endif /* LIBXML_VALID_ENABLED */
1818 * end of parsing of this node.
1820 #ifdef DEBUG_SAX_TREE
1821 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1825 #endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
1829 * @ctxt: the parser context
1830 * @str: the input string
1831 * @len: the string length
1833 * Callback for a text node
1835 * Returns the newly allocated string or NULL if not needed or error
1838 xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1840 const xmlChar *intern = NULL;
1845 if (ctxt->freeElems != NULL) {
1846 ret = ctxt->freeElems;
1847 ctxt->freeElems = ret->next;
1848 ctxt->freeElemsNr--;
1850 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1853 xmlErrMemory(ctxt, "xmlSAX2Characters");
1856 memset(ret, 0, sizeof(xmlNode));
1858 * intern the formatting blanks found between tags, or the
1859 * very short strings
1861 if (ctxt->dictNames) {
1862 xmlChar cur = str[len];
1864 if ((len < (int) (2 * sizeof(void *))) &&
1865 (ctxt->options & XML_PARSE_COMPACT)) {
1866 /* store the string in the node overriding properties and nsDef */
1867 xmlChar *tmp = (xmlChar *) &(ret->properties);
1868 memcpy(tmp, str, len);
1871 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1872 ((cur == '<') && (str[len + 1] != '!')))) {
1873 intern = xmlDictLookup(ctxt->dict, str, len);
1874 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
1875 (str[len + 1] != '!')) {
1878 for (i = 1;i < len;i++) {
1879 if (!IS_BLANK_CH(str[i])) goto skip;
1881 intern = xmlDictLookup(ctxt->dict, str, len);
1885 ret->type = XML_TEXT_NODE;
1887 ret->name = xmlStringText;
1888 if (intern == NULL) {
1889 ret->content = xmlStrndup(str, len);
1890 if (ret->content == NULL) {
1891 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1896 ret->content = (xmlChar *) intern;
1898 if (ctxt->linenumbers) {
1899 if (ctxt->input != NULL) {
1900 if (ctxt->input->line < 65535)
1901 ret->line = (short) ctxt->input->line;
1904 if (ctxt->options & XML_PARSE_BIG_LINES)
1905 ret->psvi = (void *) (long) ctxt->input->line;
1910 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1911 xmlRegisterNodeDefaultValue(ret);
1915 #ifdef LIBXML_VALID_ENABLED
1917 * xmlSAX2DecodeAttrEntities:
1918 * @ctxt: the parser context
1919 * @str: the input string
1920 * @len: the string length
1922 * Remove the entities from an attribute value
1924 * Returns the newly allocated string or NULL if not needed or error
1927 xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1928 const xmlChar *end) {
1939 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1940 XML_SUBSTITUTE_REF, 0,0,0);
1944 #endif /* LIBXML_VALID_ENABLED */
1947 * xmlSAX2AttributeNs:
1948 * @ctx: the user data (XML parser context)
1949 * @localname: the local name of the attribute
1950 * @prefix: the attribute namespace prefix if available
1951 * @URI: the attribute namespace name if available
1952 * @value: Start of the attribute value
1953 * @valueend: end of the attribute value
1955 * Handle an attribute that has been read by the parser.
1956 * The default handling is to convert the attribute into an
1957 * DOM subtree and past it in a new xmlAttr element added to
1961 xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1962 const xmlChar * localname,
1963 const xmlChar * prefix,
1964 const xmlChar * value,
1965 const xmlChar * valueend)
1968 xmlNsPtr namespace = NULL;
1969 xmlChar *dup = NULL;
1972 * Note: if prefix == NULL, the attribute is not in the default namespace
1975 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1980 if (ctxt->freeAttrs != NULL) {
1981 ret = ctxt->freeAttrs;
1982 ctxt->freeAttrs = ret->next;
1983 ctxt->freeAttrsNr--;
1984 memset(ret, 0, sizeof(xmlAttr));
1985 ret->type = XML_ATTRIBUTE_NODE;
1987 ret->parent = ctxt->node;
1988 ret->doc = ctxt->myDoc;
1989 ret->ns = namespace;
1991 if (ctxt->dictNames)
1992 ret->name = localname;
1994 ret->name = xmlStrdup(localname);
1996 /* link at the end to preserv order, TODO speed up with a last */
1997 if (ctxt->node->properties == NULL) {
1998 ctxt->node->properties = ret;
2000 xmlAttrPtr prev = ctxt->node->properties;
2002 while (prev->next != NULL) prev = prev->next;
2007 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2008 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
2010 if (ctxt->dictNames)
2011 ret = xmlNewNsPropEatName(ctxt->node, namespace,
2012 (xmlChar *) localname, NULL);
2014 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
2016 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
2021 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
2025 * We know that if there is an entity reference, then
2026 * the string has been dup'ed and terminates with 0
2027 * otherwise with ' or "
2029 if (*valueend != 0) {
2030 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2031 ret->children = tmp;
2034 tmp->doc = ret->doc;
2035 tmp->parent = (xmlNodePtr) ret;
2038 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
2040 tmp = ret->children;
2041 while (tmp != NULL) {
2042 tmp->doc = ret->doc;
2043 tmp->parent = (xmlNodePtr) ret;
2044 if (tmp->next == NULL)
2049 } else if (value != NULL) {
2052 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2053 ret->children = tmp;
2056 tmp->doc = ret->doc;
2057 tmp->parent = (xmlNodePtr) ret;
2061 #ifdef LIBXML_VALID_ENABLED
2062 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2063 ctxt->myDoc && ctxt->myDoc->intSubset) {
2065 * If we don't substitute entities, the validation should be
2066 * done on a value with replaced entities anyway.
2068 if (!ctxt->replaceEntities) {
2069 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2071 if (*valueend == 0) {
2072 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2073 ctxt->myDoc, ctxt->node, ret, value);
2076 * That should already be normalized.
2077 * cheaper to finally allocate here than duplicate
2078 * entry points in the full validation code
2080 dup = xmlStrndup(value, valueend - value);
2082 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2083 ctxt->myDoc, ctxt->node, ret, dup);
2087 * dup now contains a string of the flattened attribute
2088 * content with entities substitued. Check if we need to
2089 * apply an extra layer of normalization.
2090 * It need to be done twice ... it's an extra burden related
2091 * to the ability to keep references in attributes
2093 if (ctxt->attsSpecial != NULL) {
2098 fullname = xmlBuildQName(localname, prefix, fn, 50);
2099 if (fullname != NULL) {
2100 ctxt->vctxt.valid = 1;
2101 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2102 &ctxt->vctxt, ctxt->myDoc,
2103 ctxt->node, fullname, dup);
2104 if (ctxt->vctxt.valid != 1)
2107 if ((fullname != fn) && (fullname != localname))
2109 if (nvalnorm != NULL) {
2116 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2117 ctxt->myDoc, ctxt->node, ret, dup);
2121 * if entities already have been substitued, then
2122 * the attribute as passed is already normalized
2124 dup = xmlStrndup(value, valueend - value);
2126 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2127 ctxt->myDoc, ctxt->node, ret, dup);
2130 #endif /* LIBXML_VALID_ENABLED */
2131 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
2132 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2133 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2135 * when validating, the ID registration is done at the attribute
2136 * validation level. Otherwise we have to do specific handling here.
2138 if ((prefix == ctxt->str_xml) &&
2139 (localname[0] == 'i') && (localname[1] == 'd') &&
2140 (localname[2] == 0)) {
2142 * Add the xml:id value
2144 * Open issue: normalization of the value.
2147 dup = xmlStrndup(value, valueend - value);
2148 #ifdef LIBXML_VALID_ENABLED
2149 if (xmlValidateNCName(dup, 1) != 0) {
2150 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2151 "xml:id : attribute value %s is not an NCName\n",
2152 (const char *) dup, NULL);
2155 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2156 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2157 /* might be worth duplicate entry points and not copy */
2159 dup = xmlStrndup(value, valueend - value);
2160 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2161 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2163 dup = xmlStrndup(value, valueend - value);
2164 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2172 * xmlSAX2StartElementNs:
2173 * @ctx: the user data (XML parser context)
2174 * @localname: the local name of the element
2175 * @prefix: the element namespace prefix if available
2176 * @URI: the element namespace name if available
2177 * @nb_namespaces: number of namespace definitions on that node
2178 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2179 * @nb_attributes: the number of attributes on that node
2180 * @nb_defaulted: the number of defaulted attributes.
2181 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2184 * SAX2 callback when an element start has been detected by the parser.
2185 * It provides the namespace informations for the element, as well as
2186 * the new namespace declarations on the element.
2189 xmlSAX2StartElementNs(void *ctx,
2190 const xmlChar *localname,
2191 const xmlChar *prefix,
2194 const xmlChar **namespaces,
2197 const xmlChar **attributes)
2199 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2202 xmlNsPtr last = NULL, ns;
2203 const xmlChar *uri, *pref;
2206 if (ctx == NULL) return;
2207 parent = ctxt->node;
2209 * First check on validity:
2211 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2212 ((ctxt->myDoc->intSubset == NULL) ||
2213 ((ctxt->myDoc->intSubset->notations == NULL) &&
2214 (ctxt->myDoc->intSubset->elements == NULL) &&
2215 (ctxt->myDoc->intSubset->attributes == NULL) &&
2216 (ctxt->myDoc->intSubset->entities == NULL)))) {
2217 xmlErrValid(ctxt, XML_DTD_NO_DTD,
2218 "Validation failed: no DTD found !", NULL, NULL);
2225 if (ctxt->freeElems != NULL) {
2226 ret = ctxt->freeElems;
2227 ctxt->freeElems = ret->next;
2228 ctxt->freeElemsNr--;
2229 memset(ret, 0, sizeof(xmlNode));
2230 ret->type = XML_ELEMENT_NODE;
2232 if (ctxt->dictNames)
2233 ret->name = localname;
2235 ret->name = xmlStrdup(localname);
2236 if (ret->name == NULL) {
2237 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2241 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2242 xmlRegisterNodeDefaultValue(ret);
2244 if (ctxt->dictNames)
2245 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2246 (xmlChar *) localname, NULL);
2248 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2250 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2254 if (ctxt->linenumbers) {
2255 if (ctxt->input != NULL) {
2256 if (ctxt->input->line < 65535)
2257 ret->line = (short) ctxt->input->line;
2263 if (parent == NULL) {
2264 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2267 * Build the namespace list
2269 for (i = 0,j = 0;j < nb_namespaces;j++) {
2270 pref = namespaces[i++];
2271 uri = namespaces[i++];
2272 ns = xmlNewNs(NULL, uri, pref);
2275 ret->nsDef = last = ns;
2280 if ((URI != NULL) && (prefix == pref))
2284 * any out of memory error would already have been raised
2285 * but we can't be garanteed it's the actual error due to the
2286 * API, best is to skip in this case
2290 #ifdef LIBXML_VALID_ENABLED
2291 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2292 ctxt->myDoc && ctxt->myDoc->intSubset) {
2293 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2294 ret, prefix, ns, uri);
2296 #endif /* LIBXML_VALID_ENABLED */
2301 * We are parsing a new node.
2303 nodePush(ctxt, ret);
2306 * Link the child element
2308 if (parent != NULL) {
2309 if (parent->type == XML_ELEMENT_NODE) {
2310 xmlAddChild(parent, ret);
2312 xmlAddSibling(parent, ret);
2317 * Insert the defaulted attributes from the DTD only if requested:
2319 if ((nb_defaulted != 0) &&
2320 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2321 nb_attributes -= nb_defaulted;
2324 * Search the namespace if it wasn't already found
2325 * Note that, if prefix is NULL, this searches for the default Ns
2327 if ((URI != NULL) && (ret->ns == NULL)) {
2328 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2329 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2330 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2332 if (ret->ns == NULL) {
2333 ns = xmlNewNs(ret, NULL, prefix);
2336 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2340 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2341 "Namespace prefix %s was not found\n",
2344 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2345 "Namespace default prefix was not found\n",
2351 * process all the other attributes
2353 if (nb_attributes > 0) {
2354 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2355 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2356 attributes[j+3], attributes[j+4]);
2360 #ifdef LIBXML_VALID_ENABLED
2362 * If it's the Document root, finish the DTD validation and
2363 * check the document root element for validity
2365 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
2368 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2372 ctxt->wellFormed = 0;
2373 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2374 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
2376 #endif /* LIBXML_VALID_ENABLED */
2380 * xmlSAX2EndElementNs:
2381 * @ctx: the user data (XML parser context)
2382 * @localname: the local name of the element
2383 * @prefix: the element namespace prefix if available
2384 * @URI: the element namespace name if available
2386 * SAX2 callback when an element end has been detected by the parser.
2387 * It provides the namespace informations for the element.
2390 xmlSAX2EndElementNs(void *ctx,
2391 const xmlChar * localname ATTRIBUTE_UNUSED,
2392 const xmlChar * prefix ATTRIBUTE_UNUSED,
2393 const xmlChar * URI ATTRIBUTE_UNUSED)
2395 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2396 xmlParserNodeInfo node_info;
2399 if (ctx == NULL) return;
2401 /* Capture end position and add node */
2402 if ((ctxt->record_info) && (cur != NULL)) {
2403 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2404 node_info.end_line = ctxt->input->line;
2405 node_info.node = cur;
2406 xmlParserAddNodeInfo(ctxt, &node_info);
2410 #ifdef LIBXML_VALID_ENABLED
2411 if (ctxt->validate && ctxt->wellFormed &&
2412 ctxt->myDoc && ctxt->myDoc->intSubset)
2413 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
2414 #endif /* LIBXML_VALID_ENABLED */
2417 * end of parsing of this node.
2424 * @ctx: the user data (XML parser context)
2425 * @name: The entity name
2427 * called when an entity xmlSAX2Reference is detected.
2430 xmlSAX2Reference(void *ctx, const xmlChar *name)
2432 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2435 if (ctx == NULL) return;
2437 xmlGenericError(xmlGenericErrorContext,
2438 "SAX.xmlSAX2Reference(%s)\n", name);
2441 ret = xmlNewCharRef(ctxt->myDoc, name);
2443 ret = xmlNewReference(ctxt->myDoc, name);
2444 #ifdef DEBUG_SAX_TREE
2445 xmlGenericError(xmlGenericErrorContext,
2446 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2448 if (xmlAddChild(ctxt->node, ret) == NULL) {
2454 * xmlSAX2Characters:
2455 * @ctx: the user data (XML parser context)
2456 * @ch: a xmlChar string
2457 * @len: the number of xmlChar
2459 * receiving some chars from the parser.
2462 xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2464 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2465 xmlNodePtr lastChild;
2467 if (ctx == NULL) return;
2469 xmlGenericError(xmlGenericErrorContext,
2470 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2473 * Handle the data if any. If there is no child
2474 * add it as content, otherwise if the last child is text,
2475 * concatenate it, else create a new node of type text.
2478 if (ctxt->node == NULL) {
2479 #ifdef DEBUG_SAX_TREE
2480 xmlGenericError(xmlGenericErrorContext,
2481 "add chars: ctxt->node == NULL !\n");
2485 lastChild = ctxt->node->last;
2486 #ifdef DEBUG_SAX_TREE
2487 xmlGenericError(xmlGenericErrorContext,
2488 "add chars to %s \n", ctxt->node->name);
2492 * Here we needed an accelerator mechanism in case of very large
2493 * elements. Use an attribute in the structure !!!
2495 if (lastChild == NULL) {
2496 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2497 if (lastChild != NULL) {
2498 ctxt->node->children = lastChild;
2499 ctxt->node->last = lastChild;
2500 lastChild->parent = ctxt->node;
2501 lastChild->doc = ctxt->node->doc;
2502 ctxt->nodelen = len;
2503 ctxt->nodemem = len + 1;
2505 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2509 int coalesceText = (lastChild != NULL) &&
2510 (lastChild->type == XML_TEXT_NODE) &&
2511 (lastChild->name == xmlStringText);
2512 if ((coalesceText) && (ctxt->nodemem != 0)) {
2514 * The whole point of maintaining nodelen and nodemem,
2515 * xmlTextConcat is too costly, i.e. compute length,
2516 * reallocate a new buffer, move data, append ch. Here
2517 * We try to minimaze realloc() uses and avoid copying
2518 * and recomputing length over and over.
2520 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2521 lastChild->content = xmlStrdup(lastChild->content);
2522 lastChild->properties = NULL;
2523 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2524 (xmlDictOwns(ctxt->dict, lastChild->content))) {
2525 lastChild->content = xmlStrdup(lastChild->content);
2527 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
2528 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2529 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2532 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
2533 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
2534 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2537 if (ctxt->nodelen + len >= ctxt->nodemem) {
2541 size = ctxt->nodemem + len;
2543 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2544 if (newbuf == NULL) {
2545 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2548 ctxt->nodemem = size;
2549 lastChild->content = newbuf;
2551 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2552 ctxt->nodelen += len;
2553 lastChild->content[ctxt->nodelen] = 0;
2554 } else if (coalesceText) {
2555 if (xmlTextConcat(lastChild, ch, len)) {
2556 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2558 if (ctxt->node->children != NULL) {
2559 ctxt->nodelen = xmlStrlen(lastChild->content);
2560 ctxt->nodemem = ctxt->nodelen + 1;
2563 /* Mixed content, first time */
2564 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2565 if (lastChild != NULL) {
2566 xmlAddChild(ctxt->node, lastChild);
2567 if (ctxt->node->children != NULL) {
2568 ctxt->nodelen = len;
2569 ctxt->nodemem = len + 1;
2577 * xmlSAX2IgnorableWhitespace:
2578 * @ctx: the user data (XML parser context)
2579 * @ch: a xmlChar string
2580 * @len: the number of xmlChar
2582 * receiving some ignorable whitespaces from the parser.
2583 * UNUSED: by default the DOM building will use xmlSAX2Characters
2586 xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2588 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2590 xmlGenericError(xmlGenericErrorContext,
2591 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2596 * xmlSAX2ProcessingInstruction:
2597 * @ctx: the user data (XML parser context)
2598 * @target: the target name
2599 * @data: the PI data's
2601 * A processing instruction has been parsed.
2604 xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2605 const xmlChar *data)
2607 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2611 if (ctx == NULL) return;
2612 parent = ctxt->node;
2614 xmlGenericError(xmlGenericErrorContext,
2615 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2618 ret = xmlNewDocPI(ctxt->myDoc, target, data);
2619 if (ret == NULL) return;
2621 if (ctxt->linenumbers) {
2622 if (ctxt->input != NULL) {
2623 if (ctxt->input->line < 65535)
2624 ret->line = (short) ctxt->input->line;
2629 if (ctxt->inSubset == 1) {
2630 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2632 } else if (ctxt->inSubset == 2) {
2633 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2636 if (parent == NULL) {
2637 #ifdef DEBUG_SAX_TREE
2638 xmlGenericError(xmlGenericErrorContext,
2639 "Setting PI %s as root\n", target);
2641 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2644 if (parent->type == XML_ELEMENT_NODE) {
2645 #ifdef DEBUG_SAX_TREE
2646 xmlGenericError(xmlGenericErrorContext,
2647 "adding PI %s child to %s\n", target, parent->name);
2649 xmlAddChild(parent, ret);
2651 #ifdef DEBUG_SAX_TREE
2652 xmlGenericError(xmlGenericErrorContext,
2653 "adding PI %s sibling to ", target);
2654 xmlDebugDumpOneNode(stderr, parent, 0);
2656 xmlAddSibling(parent, ret);
2662 * @ctx: the user data (XML parser context)
2663 * @value: the xmlSAX2Comment content
2665 * A xmlSAX2Comment has been parsed.
2668 xmlSAX2Comment(void *ctx, const xmlChar *value)
2670 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2674 if (ctx == NULL) return;
2675 parent = ctxt->node;
2677 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2679 ret = xmlNewDocComment(ctxt->myDoc, value);
2680 if (ret == NULL) return;
2681 if (ctxt->linenumbers) {
2682 if (ctxt->input != NULL) {
2683 if (ctxt->input->line < 65535)
2684 ret->line = (short) ctxt->input->line;
2690 if (ctxt->inSubset == 1) {
2691 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2693 } else if (ctxt->inSubset == 2) {
2694 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2697 if (parent == NULL) {
2698 #ifdef DEBUG_SAX_TREE
2699 xmlGenericError(xmlGenericErrorContext,
2700 "Setting xmlSAX2Comment as root\n");
2702 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2705 if (parent->type == XML_ELEMENT_NODE) {
2706 #ifdef DEBUG_SAX_TREE
2707 xmlGenericError(xmlGenericErrorContext,
2708 "adding xmlSAX2Comment child to %s\n", parent->name);
2710 xmlAddChild(parent, ret);
2712 #ifdef DEBUG_SAX_TREE
2713 xmlGenericError(xmlGenericErrorContext,
2714 "adding xmlSAX2Comment sibling to ");
2715 xmlDebugDumpOneNode(stderr, parent, 0);
2717 xmlAddSibling(parent, ret);
2722 * xmlSAX2CDataBlock:
2723 * @ctx: the user data (XML parser context)
2724 * @value: The pcdata content
2725 * @len: the block length
2727 * called when a pcdata block has been parsed
2730 xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2732 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2733 xmlNodePtr ret, lastChild;
2735 if (ctx == NULL) return;
2737 xmlGenericError(xmlGenericErrorContext,
2738 "SAX.pcdata(%.10s, %d)\n", value, len);
2740 lastChild = xmlGetLastChild(ctxt->node);
2741 #ifdef DEBUG_SAX_TREE
2742 xmlGenericError(xmlGenericErrorContext,
2743 "add chars to %s \n", ctxt->node->name);
2745 if ((lastChild != NULL) &&
2746 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2747 xmlTextConcat(lastChild, value, len);
2749 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2750 xmlAddChild(ctxt->node, ret);
2754 static int xmlSAX2DefaultVersionValue = 2;
2756 #ifdef LIBXML_SAX1_ENABLED
2758 * xmlSAXDefaultVersion:
2759 * @version: the version, 1 or 2
2761 * Set the default version of SAX used globally by the library.
2762 * By default, during initialization the default is set to 2.
2763 * Note that it is generally a better coding style to use
2764 * xmlSAXVersion() to set up the version explicitly for a given
2767 * Returns the previous value in case of success and -1 in case of error.
2770 xmlSAXDefaultVersion(int version)
2772 int ret = xmlSAX2DefaultVersionValue;
2774 if ((version != 1) && (version != 2))
2776 xmlSAX2DefaultVersionValue = version;
2779 #endif /* LIBXML_SAX1_ENABLED */
2783 * @hdlr: the SAX handler
2784 * @version: the version, 1 or 2
2786 * Initialize the default XML SAX handler according to the version
2788 * Returns 0 in case of success and -1 in case of error.
2791 xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2793 if (hdlr == NULL) return(-1);
2795 hdlr->startElement = NULL;
2796 hdlr->endElement = NULL;
2797 hdlr->startElementNs = xmlSAX2StartElementNs;
2798 hdlr->endElementNs = xmlSAX2EndElementNs;
2799 hdlr->serror = NULL;
2800 hdlr->initialized = XML_SAX2_MAGIC;
2801 #ifdef LIBXML_SAX1_ENABLED
2802 } else if (version == 1) {
2803 hdlr->startElement = xmlSAX2StartElement;
2804 hdlr->endElement = xmlSAX2EndElement;
2805 hdlr->initialized = 1;
2806 #endif /* LIBXML_SAX1_ENABLED */
2809 hdlr->internalSubset = xmlSAX2InternalSubset;
2810 hdlr->externalSubset = xmlSAX2ExternalSubset;
2811 hdlr->isStandalone = xmlSAX2IsStandalone;
2812 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2813 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2814 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2815 hdlr->getEntity = xmlSAX2GetEntity;
2816 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2817 hdlr->entityDecl = xmlSAX2EntityDecl;
2818 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2819 hdlr->elementDecl = xmlSAX2ElementDecl;
2820 hdlr->notationDecl = xmlSAX2NotationDecl;
2821 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2822 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2823 hdlr->startDocument = xmlSAX2StartDocument;
2824 hdlr->endDocument = xmlSAX2EndDocument;
2825 hdlr->reference = xmlSAX2Reference;
2826 hdlr->characters = xmlSAX2Characters;
2827 hdlr->cdataBlock = xmlSAX2CDataBlock;
2828 hdlr->ignorableWhitespace = xmlSAX2Characters;
2829 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2830 hdlr->comment = xmlSAX2Comment;
2831 hdlr->warning = xmlParserWarning;
2832 hdlr->error = xmlParserError;
2833 hdlr->fatalError = xmlParserError;
2839 * xmlSAX2InitDefaultSAXHandler:
2840 * @hdlr: the SAX handler
2841 * @warning: flag if non-zero sets the handler warning procedure
2843 * Initialize the default XML SAX2 handler
2846 xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2848 if ((hdlr == NULL) || (hdlr->initialized != 0))
2851 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2853 hdlr->warning = NULL;
2855 hdlr->warning = xmlParserWarning;
2859 * xmlDefaultSAXHandlerInit:
2861 * Initialize the default SAX2 handler
2864 xmlDefaultSAXHandlerInit(void)
2866 #ifdef LIBXML_SAX1_ENABLED
2867 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
2868 #endif /* LIBXML_SAX1_ENABLED */
2871 #ifdef LIBXML_HTML_ENABLED
2874 * xmlSAX2InitHtmlDefaultSAXHandler:
2875 * @hdlr: the SAX handler
2877 * Initialize the default HTML SAX2 handler
2880 xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2882 if ((hdlr == NULL) || (hdlr->initialized != 0))
2885 hdlr->internalSubset = xmlSAX2InternalSubset;
2886 hdlr->externalSubset = NULL;
2887 hdlr->isStandalone = NULL;
2888 hdlr->hasInternalSubset = NULL;
2889 hdlr->hasExternalSubset = NULL;
2890 hdlr->resolveEntity = NULL;
2891 hdlr->getEntity = xmlSAX2GetEntity;
2892 hdlr->getParameterEntity = NULL;
2893 hdlr->entityDecl = NULL;
2894 hdlr->attributeDecl = NULL;
2895 hdlr->elementDecl = NULL;
2896 hdlr->notationDecl = NULL;
2897 hdlr->unparsedEntityDecl = NULL;
2898 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2899 hdlr->startDocument = xmlSAX2StartDocument;
2900 hdlr->endDocument = xmlSAX2EndDocument;
2901 hdlr->startElement = xmlSAX2StartElement;
2902 hdlr->endElement = xmlSAX2EndElement;
2903 hdlr->reference = NULL;
2904 hdlr->characters = xmlSAX2Characters;
2905 hdlr->cdataBlock = xmlSAX2CDataBlock;
2906 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2907 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2908 hdlr->comment = xmlSAX2Comment;
2909 hdlr->warning = xmlParserWarning;
2910 hdlr->error = xmlParserError;
2911 hdlr->fatalError = xmlParserError;
2913 hdlr->initialized = 1;
2917 * htmlDefaultSAXHandlerInit:
2919 * Initialize the default SAX handler
2922 htmlDefaultSAXHandlerInit(void)
2924 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
2927 #endif /* LIBXML_HTML_ENABLED */
2929 #ifdef LIBXML_DOCB_ENABLED
2932 * xmlSAX2InitDocbDefaultSAXHandler:
2933 * @hdlr: the SAX handler
2935 * Initialize the default DocBook SAX2 handler
2938 xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2940 if ((hdlr == NULL) || (hdlr->initialized != 0))
2943 hdlr->internalSubset = xmlSAX2InternalSubset;
2944 hdlr->externalSubset = NULL;
2945 hdlr->isStandalone = xmlSAX2IsStandalone;
2946 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2947 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2948 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2949 hdlr->getEntity = xmlSAX2GetEntity;
2950 hdlr->getParameterEntity = NULL;
2951 hdlr->entityDecl = xmlSAX2EntityDecl;
2952 hdlr->attributeDecl = NULL;
2953 hdlr->elementDecl = NULL;
2954 hdlr->notationDecl = NULL;
2955 hdlr->unparsedEntityDecl = NULL;
2956 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2957 hdlr->startDocument = xmlSAX2StartDocument;
2958 hdlr->endDocument = xmlSAX2EndDocument;
2959 hdlr->startElement = xmlSAX2StartElement;
2960 hdlr->endElement = xmlSAX2EndElement;
2961 hdlr->reference = xmlSAX2Reference;
2962 hdlr->characters = xmlSAX2Characters;
2963 hdlr->cdataBlock = NULL;
2964 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2965 hdlr->processingInstruction = NULL;
2966 hdlr->comment = xmlSAX2Comment;
2967 hdlr->warning = xmlParserWarning;
2968 hdlr->error = xmlParserError;
2969 hdlr->fatalError = xmlParserError;
2971 hdlr->initialized = 1;
2975 * docbDefaultSAXHandlerInit:
2977 * Initialize the default SAX handler
2980 docbDefaultSAXHandlerInit(void)
2982 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
2985 #endif /* LIBXML_DOCB_ENABLED */
2987 #include "elfgcchack.h"