VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.30/SAX2.c@ 25275

Last change on this file since 25275 was 16778, checked in by vboxsync, 15 years ago

libxml2: some fixes from upstream

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 79.6 KB
Line 
1/*
2 * SAX2.c : Default SAX2 handler to build a tree.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <daniel@veillard.com>
7 */
8
9
10#define IN_LIBXML
11#include "libxml.h"
12#include <stdlib.h>
13#include <string.h>
14#include <limits.h>
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>
29
30/* Define SIZE_T_MAX unless defined through <limits.h>. */
31#ifndef SIZE_T_MAX
32# define SIZE_T_MAX ((size_t)-1)
33#endif /* !SIZE_T_MAX */
34
35/* #define DEBUG_SAX2 */
36/* #define DEBUG_SAX2_TREE */
37
38/**
39 * TODO:
40 *
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
46 *> match yours.
47 */
48#define TODO \
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
53/*
54 * xmlSAX2ErrMemory:
55 * @ctxt: an XML validation parser context
56 * @msg: a string to accompany the error message
57 */
58static void
59xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
60 if (ctxt != NULL) {
61 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
62 ctxt->sax->error(ctxt->userData, "%s: out of memory\n", msg);
63 ctxt->errNo = XML_ERR_NO_MEMORY;
64 ctxt->instate = XML_PARSER_EOF;
65 ctxt->disableSAX = 1;
66 }
67}
68
69/**
70 * xmlValidError:
71 * @ctxt: an XML validation parser context
72 * @error: the error number
73 * @msg: the error message
74 * @str1: extra data
75 * @str2: extra data
76 *
77 * Handle a validation error
78 */
79static void
80xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
81 const char *msg, const char *str1, const char *str2)
82{
83 xmlStructuredErrorFunc schannel = NULL;
84
85 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
86 (ctxt->instate == XML_PARSER_EOF))
87 return;
88 if (ctxt != NULL) {
89 ctxt->errNo = error;
90 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
91 schannel = ctxt->sax->serror;
92 __xmlRaiseError(schannel,
93 ctxt->vctxt.error, ctxt->vctxt.userData,
94 ctxt, NULL, XML_FROM_DTD, error,
95 XML_ERR_ERROR, NULL, 0, (const char *) str1,
96 (const char *) str2, NULL, 0, 0,
97 msg, (const char *) str1, (const char *) str2);
98 ctxt->valid = 0;
99 } else {
100 __xmlRaiseError(schannel,
101 NULL, NULL,
102 ctxt, NULL, XML_FROM_DTD, error,
103 XML_ERR_ERROR, NULL, 0, (const char *) str1,
104 (const char *) str2, NULL, 0, 0,
105 msg, (const char *) str1, (const char *) str2);
106 }
107}
108
109/**
110 * xmlFatalErrMsg:
111 * @ctxt: an XML parser context
112 * @error: the error number
113 * @msg: the error message
114 * @str1: an error string
115 * @str2: an error string
116 *
117 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
118 */
119static void
120xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
121 const char *msg, const xmlChar *str1, const xmlChar *str2)
122{
123 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
124 (ctxt->instate == XML_PARSER_EOF))
125 return;
126 if (ctxt != NULL)
127 ctxt->errNo = error;
128 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
129 XML_ERR_FATAL, NULL, 0,
130 (const char *) str1, (const char *) str2,
131 NULL, 0, 0, msg, str1, str2);
132 if (ctxt != NULL) {
133 ctxt->wellFormed = 0;
134 ctxt->valid = 0;
135 if (ctxt->recovery == 0)
136 ctxt->disableSAX = 1;
137 }
138}
139
140/**
141 * xmlWarnMsg:
142 * @ctxt: an XML parser context
143 * @error: the error number
144 * @msg: the error message
145 * @str1: an error string
146 * @str2: an error string
147 *
148 * Handle a parser warning
149 */
150static void
151xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
152 const char *msg, const xmlChar *str1)
153{
154 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
155 (ctxt->instate == XML_PARSER_EOF))
156 return;
157 if (ctxt != NULL)
158 ctxt->errNo = error;
159 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
160 XML_ERR_WARNING, NULL, 0,
161 (const char *) str1, NULL,
162 NULL, 0, 0, msg, str1);
163}
164
165/**
166 * xmlNsErrMsg:
167 * @ctxt: an XML parser context
168 * @error: the error number
169 * @msg: the error message
170 * @str1: an error string
171 * @str2: an error string
172 *
173 * Handle a namespace error
174 */
175static void
176xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
177 const char *msg, const xmlChar *str1, const xmlChar *str2)
178{
179 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
180 (ctxt->instate == XML_PARSER_EOF))
181 return;
182 if (ctxt != NULL)
183 ctxt->errNo = error;
184 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
185 XML_ERR_ERROR, NULL, 0,
186 (const char *) str1, (const char *) str2,
187 NULL, 0, 0, msg, str1, str2);
188}
189
190/**
191 * xmlNsWarnMsg:
192 * @ctxt: an XML parser context
193 * @error: the error number
194 * @msg: the error message
195 * @str1: an error string
196 *
197 * Handle a namespace warning
198 */
199static void
200xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
201 const char *msg, const xmlChar *str1, const xmlChar *str2)
202{
203 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
204 (ctxt->instate == XML_PARSER_EOF))
205 return;
206 if (ctxt != NULL)
207 ctxt->errNo = error;
208 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
209 XML_ERR_WARNING, NULL, 0,
210 (const char *) str1, (const char *) str2,
211 NULL, 0, 0, msg, str1, str2);
212}
213
214/**
215 * xmlSAX2GetPublicId:
216 * @ctx: the user data (XML parser context)
217 *
218 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
219 *
220 * Returns a xmlChar *
221 */
222const xmlChar *
223xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
224{
225 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
226 return(NULL);
227}
228
229/**
230 * xmlSAX2GetSystemId:
231 * @ctx: the user data (XML parser context)
232 *
233 * Provides the system ID, basically URL or filename e.g.
234 * http://www.sgmlsource.com/dtds/memo.dtd
235 *
236 * Returns a xmlChar *
237 */
238const xmlChar *
239xmlSAX2GetSystemId(void *ctx)
240{
241 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
242 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
243 return((const xmlChar *) ctxt->input->filename);
244}
245
246/**
247 * xmlSAX2GetLineNumber:
248 * @ctx: the user data (XML parser context)
249 *
250 * Provide the line number of the current parsing point.
251 *
252 * Returns an int
253 */
254int
255xmlSAX2GetLineNumber(void *ctx)
256{
257 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
258 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
259 return(ctxt->input->line);
260}
261
262/**
263 * xmlSAX2GetColumnNumber:
264 * @ctx: the user data (XML parser context)
265 *
266 * Provide the column number of the current parsing point.
267 *
268 * Returns an int
269 */
270int
271xmlSAX2GetColumnNumber(void *ctx)
272{
273 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
274 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
275 return(ctxt->input->col);
276}
277
278/**
279 * xmlSAX2IsStandalone:
280 * @ctx: the user data (XML parser context)
281 *
282 * Is this document tagged standalone ?
283 *
284 * Returns 1 if true
285 */
286int
287xmlSAX2IsStandalone(void *ctx)
288{
289 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
290 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
291 return(ctxt->myDoc->standalone == 1);
292}
293
294/**
295 * xmlSAX2HasInternalSubset:
296 * @ctx: the user data (XML parser context)
297 *
298 * Does this document has an internal subset
299 *
300 * Returns 1 if true
301 */
302int
303xmlSAX2HasInternalSubset(void *ctx)
304{
305 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
306 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
307 return(ctxt->myDoc->intSubset != NULL);
308}
309
310/**
311 * xmlSAX2HasExternalSubset:
312 * @ctx: the user data (XML parser context)
313 *
314 * Does this document has an external subset
315 *
316 * Returns 1 if true
317 */
318int
319xmlSAX2HasExternalSubset(void *ctx)
320{
321 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
322 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
323 return(ctxt->myDoc->extSubset != NULL);
324}
325
326/**
327 * xmlSAX2InternalSubset:
328 * @ctx: the user data (XML parser context)
329 * @name: the root element name
330 * @ExternalID: the external ID
331 * @SystemID: the SYSTEM ID (e.g. filename or URL)
332 *
333 * Callback on internal subset declaration.
334 */
335void
336xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
337 const xmlChar *ExternalID, const xmlChar *SystemID)
338{
339 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
340 xmlDtdPtr dtd;
341 if (ctx == NULL) return;
342#ifdef DEBUG_SAX
343 xmlGenericError(xmlGenericErrorContext,
344 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
345 name, ExternalID, SystemID);
346#endif
347
348 if (ctxt->myDoc == NULL)
349 return;
350 dtd = xmlGetIntSubset(ctxt->myDoc);
351 if (dtd != NULL) {
352 if (ctxt->html)
353 return;
354 xmlUnlinkNode((xmlNodePtr) dtd);
355 xmlFreeDtd(dtd);
356 ctxt->myDoc->intSubset = NULL;
357 }
358 ctxt->myDoc->intSubset =
359 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
360 if (ctxt->myDoc->intSubset == NULL)
361 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
362}
363
364/**
365 * xmlSAX2ExternalSubset:
366 * @ctx: the user data (XML parser context)
367 * @name: the root element name
368 * @ExternalID: the external ID
369 * @SystemID: the SYSTEM ID (e.g. filename or URL)
370 *
371 * Callback on external subset declaration.
372 */
373void
374xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
375 const xmlChar *ExternalID, const xmlChar *SystemID)
376{
377 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
378 if (ctx == NULL) return;
379#ifdef DEBUG_SAX
380 xmlGenericError(xmlGenericErrorContext,
381 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
382 name, ExternalID, SystemID);
383#endif
384 if (((ExternalID != NULL) || (SystemID != NULL)) &&
385 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
386 (ctxt->wellFormed && ctxt->myDoc))) {
387 /*
388 * Try to fetch and parse the external subset.
389 */
390 xmlParserInputPtr oldinput;
391 int oldinputNr;
392 int oldinputMax;
393 xmlParserInputPtr *oldinputTab;
394 xmlParserInputPtr input = NULL;
395 xmlCharEncoding enc;
396 int oldcharset;
397
398 /*
399 * Ask the Entity resolver to load the damn thing
400 */
401 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
402 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
403 SystemID);
404 if (input == NULL) {
405 return;
406 }
407
408 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
409
410 /*
411 * make sure we won't destroy the main document context
412 */
413 oldinput = ctxt->input;
414 oldinputNr = ctxt->inputNr;
415 oldinputMax = ctxt->inputMax;
416 oldinputTab = ctxt->inputTab;
417 oldcharset = ctxt->charset;
418
419 ctxt->inputTab = (xmlParserInputPtr *)
420 xmlMalloc(5 * sizeof(xmlParserInputPtr));
421 if (ctxt->inputTab == NULL) {
422 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
423 ctxt->input = oldinput;
424 ctxt->inputNr = oldinputNr;
425 ctxt->inputMax = oldinputMax;
426 ctxt->inputTab = oldinputTab;
427 ctxt->charset = oldcharset;
428 return;
429 }
430 ctxt->inputNr = 0;
431 ctxt->inputMax = 5;
432 ctxt->input = NULL;
433 xmlPushInput(ctxt, input);
434
435 /*
436 * On the fly encoding conversion if needed
437 */
438 if (ctxt->input->length >= 4) {
439 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
440 xmlSwitchEncoding(ctxt, enc);
441 }
442
443 if (input->filename == NULL)
444 input->filename = (char *) xmlCanonicPath(SystemID);
445 input->line = 1;
446 input->col = 1;
447 input->base = ctxt->input->cur;
448 input->cur = ctxt->input->cur;
449 input->free = NULL;
450
451 /*
452 * let's parse that entity knowing it's an external subset.
453 */
454 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
455
456 /*
457 * Free up the external entities
458 */
459
460 while (ctxt->inputNr > 1)
461 xmlPopInput(ctxt);
462 xmlFreeInputStream(ctxt->input);
463 xmlFree(ctxt->inputTab);
464
465 /*
466 * Restore the parsing context of the main entity
467 */
468 ctxt->input = oldinput;
469 ctxt->inputNr = oldinputNr;
470 ctxt->inputMax = oldinputMax;
471 ctxt->inputTab = oldinputTab;
472 ctxt->charset = oldcharset;
473 /* ctxt->wellFormed = oldwellFormed; */
474 }
475}
476
477/**
478 * xmlSAX2ResolveEntity:
479 * @ctx: the user data (XML parser context)
480 * @publicId: The public ID of the entity
481 * @systemId: The system ID of the entity
482 *
483 * The entity loader, to control the loading of external entities,
484 * the application can either:
485 * - override this xmlSAX2ResolveEntity() callback in the SAX block
486 * - or better use the xmlSetExternalEntityLoader() function to
487 * set up it's own entity resolution routine
488 *
489 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
490 */
491xmlParserInputPtr
492xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
493{
494 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
495 xmlParserInputPtr ret;
496 xmlChar *URI;
497 const char *base = NULL;
498
499 if (ctx == NULL) return(NULL);
500 if (ctxt->input != NULL)
501 base = ctxt->input->filename;
502 if (base == NULL)
503 base = ctxt->directory;
504
505 URI = xmlBuildURI(systemId, (const xmlChar *) base);
506
507#ifdef DEBUG_SAX
508 xmlGenericError(xmlGenericErrorContext,
509 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
510#endif
511
512 ret = xmlLoadExternalEntity((const char *) URI,
513 (const char *) publicId, ctxt);
514 if (URI != NULL)
515 xmlFree(URI);
516 return(ret);
517}
518
519/**
520 * xmlSAX2GetEntity:
521 * @ctx: the user data (XML parser context)
522 * @name: The entity name
523 *
524 * Get an entity by name
525 *
526 * Returns the xmlEntityPtr if found.
527 */
528xmlEntityPtr
529xmlSAX2GetEntity(void *ctx, const xmlChar *name)
530{
531 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
532 xmlEntityPtr ret = NULL;
533
534 if (ctx == NULL) return(NULL);
535#ifdef DEBUG_SAX
536 xmlGenericError(xmlGenericErrorContext,
537 "SAX.xmlSAX2GetEntity(%s)\n", name);
538#endif
539
540 if (ctxt->inSubset == 0) {
541 ret = xmlGetPredefinedEntity(name);
542 if (ret != NULL)
543 return(ret);
544 }
545 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
546 if (ctxt->inSubset == 2) {
547 ctxt->myDoc->standalone = 0;
548 ret = xmlGetDocEntity(ctxt->myDoc, name);
549 ctxt->myDoc->standalone = 1;
550 } else {
551 ret = xmlGetDocEntity(ctxt->myDoc, name);
552 if (ret == NULL) {
553 ctxt->myDoc->standalone = 0;
554 ret = xmlGetDocEntity(ctxt->myDoc, name);
555 if (ret != NULL) {
556 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
557 "Entity(%s) document marked standalone but requires external subset\n",
558 name, NULL);
559 }
560 ctxt->myDoc->standalone = 1;
561 }
562 }
563 } else {
564 ret = xmlGetDocEntity(ctxt->myDoc, name);
565 }
566 if ((ret != NULL) &&
567 ((ctxt->validate) || (ctxt->replaceEntities)) &&
568 (ret->children == NULL) &&
569 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
570 int val;
571
572 /*
573 * for validation purposes we really need to fetch and
574 * parse the external entity
575 */
576 xmlNodePtr children;
577
578 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
579 ret->ExternalID, &children);
580 if (val == 0) {
581 xmlAddChildList((xmlNodePtr) ret, children);
582 } else {
583 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
584 "Failure to process entity %s\n", name, NULL);
585 ctxt->validate = 0;
586 return(NULL);
587 }
588 ret->owner = 1;
589 if (ret->checked == 0)
590 ret->checked = 1;
591 }
592 return(ret);
593}
594
595/**
596 * xmlSAX2GetParameterEntity:
597 * @ctx: the user data (XML parser context)
598 * @name: The entity name
599 *
600 * Get a parameter entity by name
601 *
602 * Returns the xmlEntityPtr if found.
603 */
604xmlEntityPtr
605xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
606{
607 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
608 xmlEntityPtr ret;
609
610 if (ctx == NULL) return(NULL);
611#ifdef DEBUG_SAX
612 xmlGenericError(xmlGenericErrorContext,
613 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
614#endif
615
616 ret = xmlGetParameterEntity(ctxt->myDoc, name);
617 return(ret);
618}
619
620
621/**
622 * xmlSAX2EntityDecl:
623 * @ctx: the user data (XML parser context)
624 * @name: the entity name
625 * @type: the entity type
626 * @publicId: The public ID of the entity
627 * @systemId: The system ID of the entity
628 * @content: the entity value (without processing).
629 *
630 * An entity definition has been parsed
631 */
632void
633xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
634 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
635{
636 xmlEntityPtr ent;
637 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
638
639 if (ctx == NULL) return;
640#ifdef DEBUG_SAX
641 xmlGenericError(xmlGenericErrorContext,
642 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
643 name, type, publicId, systemId, content);
644#endif
645 if (ctxt->inSubset == 1) {
646 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
647 systemId, content);
648 if ((ent == NULL) && (ctxt->pedantic))
649 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
650 "Entity(%s) already defined in the internal subset\n",
651 name);
652 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
653 xmlChar *URI;
654 const char *base = NULL;
655
656 if (ctxt->input != NULL)
657 base = ctxt->input->filename;
658 if (base == NULL)
659 base = ctxt->directory;
660
661 URI = xmlBuildURI(systemId, (const xmlChar *) base);
662 ent->URI = URI;
663 }
664 } else if (ctxt->inSubset == 2) {
665 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
666 systemId, content);
667 if ((ent == NULL) && (ctxt->pedantic) &&
668 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
669 ctxt->sax->warning(ctxt->userData,
670 "Entity(%s) already defined in the external subset\n", name);
671 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
672 xmlChar *URI;
673 const char *base = NULL;
674
675 if (ctxt->input != NULL)
676 base = ctxt->input->filename;
677 if (base == NULL)
678 base = ctxt->directory;
679
680 URI = xmlBuildURI(systemId, (const xmlChar *) base);
681 ent->URI = URI;
682 }
683 } else {
684 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
685 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
686 name, NULL);
687 }
688}
689
690/**
691 * xmlSAX2AttributeDecl:
692 * @ctx: the user data (XML parser context)
693 * @elem: the name of the element
694 * @fullname: the attribute name
695 * @type: the attribute type
696 * @def: the type of default value
697 * @defaultValue: the attribute default value
698 * @tree: the tree of enumerated value set
699 *
700 * An attribute definition has been parsed
701 */
702void
703xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
704 int type, int def, const xmlChar *defaultValue,
705 xmlEnumerationPtr tree)
706{
707 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
708 xmlAttributePtr attr;
709 xmlChar *name = NULL, *prefix = NULL;
710
711 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
712 return;
713
714#ifdef DEBUG_SAX
715 xmlGenericError(xmlGenericErrorContext,
716 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
717 elem, fullname, type, def, defaultValue);
718#endif
719 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
720 (type != XML_ATTRIBUTE_ID)) {
721 /*
722 * Raise the error but keep the validity flag
723 */
724 int tmp = ctxt->valid;
725 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
726 "xml:id : attribute type should be ID\n", NULL, NULL);
727 ctxt->valid = tmp;
728 }
729 /* TODO: optimize name/prefix allocation */
730 name = xmlSplitQName(ctxt, fullname, &prefix);
731 ctxt->vctxt.valid = 1;
732 if (ctxt->inSubset == 1)
733 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
734 name, prefix, (xmlAttributeType) type,
735 (xmlAttributeDefault) def, defaultValue, tree);
736 else if (ctxt->inSubset == 2)
737 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
738 name, prefix, (xmlAttributeType) type,
739 (xmlAttributeDefault) def, defaultValue, tree);
740 else {
741 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
742 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
743 name, NULL);
744 xmlFreeEnumeration(tree);
745 return;
746 }
747#ifdef LIBXML_VALID_ENABLED
748 if (ctxt->vctxt.valid == 0)
749 ctxt->valid = 0;
750 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
751 (ctxt->myDoc->intSubset != NULL))
752 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
753 attr);
754#endif /* LIBXML_VALID_ENABLED */
755 if (prefix != NULL)
756 xmlFree(prefix);
757 if (name != NULL)
758 xmlFree(name);
759}
760
761/**
762 * xmlSAX2ElementDecl:
763 * @ctx: the user data (XML parser context)
764 * @name: the element name
765 * @type: the element type
766 * @content: the element value tree
767 *
768 * An element definition has been parsed
769 */
770void
771xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
772 xmlElementContentPtr content)
773{
774 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
775 xmlElementPtr elem = NULL;
776
777 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
778 return;
779
780#ifdef DEBUG_SAX
781 xmlGenericError(xmlGenericErrorContext,
782 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
783#endif
784
785 if (ctxt->inSubset == 1)
786 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
787 name, (xmlElementTypeVal) type, content);
788 else if (ctxt->inSubset == 2)
789 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
790 name, (xmlElementTypeVal) type, content);
791 else {
792 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
793 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
794 name, NULL);
795 return;
796 }
797#ifdef LIBXML_VALID_ENABLED
798 if (elem == NULL)
799 ctxt->valid = 0;
800 if (ctxt->validate && ctxt->wellFormed &&
801 ctxt->myDoc && ctxt->myDoc->intSubset)
802 ctxt->valid &=
803 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
804#endif /* LIBXML_VALID_ENABLED */
805}
806
807/**
808 * xmlSAX2NotationDecl:
809 * @ctx: the user data (XML parser context)
810 * @name: The name of the notation
811 * @publicId: The public ID of the entity
812 * @systemId: The system ID of the entity
813 *
814 * What to do when a notation declaration has been parsed.
815 */
816void
817xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
818 const xmlChar *publicId, const xmlChar *systemId)
819{
820 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
821 xmlNotationPtr nota = NULL;
822
823 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
824 return;
825
826#ifdef DEBUG_SAX
827 xmlGenericError(xmlGenericErrorContext,
828 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
829#endif
830
831 if ((publicId == NULL) && (systemId == NULL)) {
832 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
833 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
834 name, NULL);
835 return;
836 } else if (ctxt->inSubset == 1)
837 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
838 publicId, systemId);
839 else if (ctxt->inSubset == 2)
840 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
841 publicId, systemId);
842 else {
843 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
844 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
845 name, NULL);
846 return;
847 }
848#ifdef LIBXML_VALID_ENABLED
849 if (nota == NULL) ctxt->valid = 0;
850 if ((ctxt->validate) && (ctxt->wellFormed) &&
851 (ctxt->myDoc->intSubset != NULL))
852 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
853 nota);
854#endif /* LIBXML_VALID_ENABLED */
855}
856
857/**
858 * xmlSAX2UnparsedEntityDecl:
859 * @ctx: the user data (XML parser context)
860 * @name: The name of the entity
861 * @publicId: The public ID of the entity
862 * @systemId: The system ID of the entity
863 * @notationName: the name of the notation
864 *
865 * What to do when an unparsed entity declaration is parsed
866 */
867void
868xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
869 const xmlChar *publicId, const xmlChar *systemId,
870 const xmlChar *notationName)
871{
872 xmlEntityPtr ent;
873 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
874 if (ctx == NULL) return;
875#ifdef DEBUG_SAX
876 xmlGenericError(xmlGenericErrorContext,
877 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
878 name, publicId, systemId, notationName);
879#endif
880 if (ctxt->inSubset == 1) {
881 ent = xmlAddDocEntity(ctxt->myDoc, name,
882 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
883 publicId, systemId, notationName);
884 if ((ent == NULL) && (ctxt->pedantic) &&
885 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
886 ctxt->sax->warning(ctxt->userData,
887 "Entity(%s) already defined in the internal subset\n", name);
888 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
889 xmlChar *URI;
890 const char *base = NULL;
891
892 if (ctxt->input != NULL)
893 base = ctxt->input->filename;
894 if (base == NULL)
895 base = ctxt->directory;
896
897 URI = xmlBuildURI(systemId, (const xmlChar *) base);
898 ent->URI = URI;
899 }
900 } else if (ctxt->inSubset == 2) {
901 ent = xmlAddDtdEntity(ctxt->myDoc, name,
902 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
903 publicId, systemId, notationName);
904 if ((ent == NULL) && (ctxt->pedantic) &&
905 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
906 ctxt->sax->warning(ctxt->userData,
907 "Entity(%s) already defined in the external subset\n", name);
908 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
909 xmlChar *URI;
910 const char *base = NULL;
911
912 if (ctxt->input != NULL)
913 base = ctxt->input->filename;
914 if (base == NULL)
915 base = ctxt->directory;
916
917 URI = xmlBuildURI(systemId, (const xmlChar *) base);
918 ent->URI = URI;
919 }
920 } else {
921 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
922 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
923 name, NULL);
924 }
925}
926
927/**
928 * xmlSAX2SetDocumentLocator:
929 * @ctx: the user data (XML parser context)
930 * @loc: A SAX Locator
931 *
932 * Receive the document locator at startup, actually xmlDefaultSAXLocator
933 * Everything is available on the context, so this is useless in our case.
934 */
935void
936xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
937{
938 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
939#ifdef DEBUG_SAX
940 xmlGenericError(xmlGenericErrorContext,
941 "SAX.xmlSAX2SetDocumentLocator()\n");
942#endif
943}
944
945/**
946 * xmlSAX2StartDocument:
947 * @ctx: the user data (XML parser context)
948 *
949 * called when the document start being processed.
950 */
951void
952xmlSAX2StartDocument(void *ctx)
953{
954 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
955 xmlDocPtr doc;
956
957 if (ctx == NULL) return;
958
959#ifdef DEBUG_SAX
960 xmlGenericError(xmlGenericErrorContext,
961 "SAX.xmlSAX2StartDocument()\n");
962#endif
963 if (ctxt->html) {
964#ifdef LIBXML_HTML_ENABLED
965 if (ctxt->myDoc == NULL)
966 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
967 if (ctxt->myDoc == NULL) {
968 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
969 return;
970 }
971#else
972 xmlGenericError(xmlGenericErrorContext,
973 "libxml2 built without HTML support\n");
974 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
975 ctxt->instate = XML_PARSER_EOF;
976 ctxt->disableSAX = 1;
977 return;
978#endif
979 } else {
980 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
981 if (doc != NULL) {
982 if (ctxt->encoding != NULL)
983 doc->encoding = xmlStrdup(ctxt->encoding);
984 else
985 doc->encoding = NULL;
986 doc->standalone = ctxt->standalone;
987 } else {
988 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
989 return;
990 }
991 if ((ctxt->dictNames) && (doc != NULL)) {
992 doc->dict = ctxt->dict;
993 xmlDictReference(doc->dict);
994 }
995 }
996 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
997 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
998 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
999 if (ctxt->myDoc->URL == NULL)
1000 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
1001 }
1002}
1003
1004/**
1005 * xmlSAX2EndDocument:
1006 * @ctx: the user data (XML parser context)
1007 *
1008 * called when the document end has been detected.
1009 */
1010void
1011xmlSAX2EndDocument(void *ctx)
1012{
1013 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1014#ifdef DEBUG_SAX
1015 xmlGenericError(xmlGenericErrorContext,
1016 "SAX.xmlSAX2EndDocument()\n");
1017#endif
1018 if (ctx == NULL) return;
1019#ifdef LIBXML_VALID_ENABLED
1020 if (ctxt->validate && ctxt->wellFormed &&
1021 ctxt->myDoc && ctxt->myDoc->intSubset)
1022 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
1023#endif /* LIBXML_VALID_ENABLED */
1024
1025 /*
1026 * Grab the encoding if it was added on-the-fly
1027 */
1028 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1029 (ctxt->myDoc->encoding == NULL)) {
1030 ctxt->myDoc->encoding = ctxt->encoding;
1031 ctxt->encoding = NULL;
1032 }
1033 if ((ctxt->inputTab != NULL) &&
1034 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1035 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
1036 (ctxt->myDoc->encoding == NULL)) {
1037 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1038 }
1039 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1040 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1041 ctxt->myDoc->charset = ctxt->charset;
1042 }
1043}
1044
1045#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
1046/**
1047 * xmlSAX2AttributeInternal:
1048 * @ctx: the user data (XML parser context)
1049 * @fullname: The attribute name, including namespace prefix
1050 * @value: The attribute value
1051 * @prefix: the prefix on the element node
1052 *
1053 * Handle an attribute that has been read by the parser.
1054 * The default handling is to convert the attribute into an
1055 * DOM subtree and past it in a new xmlAttr element added to
1056 * the element.
1057 */
1058static void
1059xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
1060 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
1061{
1062 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1063 xmlAttrPtr ret;
1064 xmlChar *name;
1065 xmlChar *ns;
1066 xmlChar *nval;
1067 xmlNsPtr namespace;
1068
1069 if (ctxt->html) {
1070 name = xmlStrdup(fullname);
1071 ns = NULL;
1072 namespace = NULL;
1073 } else {
1074 /*
1075 * Split the full name into a namespace prefix and the tag name
1076 */
1077 name = xmlSplitQName(ctxt, fullname, &ns);
1078 if ((name != NULL) && (name[0] == 0)) {
1079 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1080 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1081 "invalid namespace declaration '%s'\n",
1082 fullname, NULL);
1083 } else {
1084 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1085 "Avoid attribute ending with ':' like '%s'\n",
1086 fullname, NULL);
1087 }
1088 if (ns != NULL)
1089 xmlFree(ns);
1090 ns = NULL;
1091 xmlFree(name);
1092 name = xmlStrdup(fullname);
1093 }
1094 }
1095 if (name == NULL) {
1096 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1097 if (ns != NULL)
1098 xmlFree(ns);
1099 return;
1100 }
1101
1102#ifdef LIBXML_VALID_ENABLED
1103 /*
1104 * Do the last stage of the attribute normalization
1105 * Needed for HTML too:
1106 * http://www.w3.org/TR/html4/types.html#h-6.2
1107 */
1108 ctxt->vctxt.valid = 1;
1109 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1110 ctxt->myDoc, ctxt->node,
1111 fullname, value);
1112 if (ctxt->vctxt.valid != 1) {
1113 ctxt->valid = 0;
1114 }
1115 if (nval != NULL)
1116 value = nval;
1117#else
1118 nval = NULL;
1119#endif /* LIBXML_VALID_ENABLED */
1120
1121 /*
1122 * Check whether it's a namespace definition
1123 */
1124 if ((!ctxt->html) && (ns == NULL) &&
1125 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1126 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1127 xmlNsPtr nsret;
1128 xmlChar *val;
1129
1130 if (!ctxt->replaceEntities) {
1131 ctxt->depth++;
1132 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1133 0,0,0);
1134 ctxt->depth--;
1135 } else {
1136 val = (xmlChar *) value;
1137 }
1138
1139 if (val[0] != 0) {
1140 xmlURIPtr uri;
1141
1142 uri = xmlParseURI((const char *)val);
1143 if (uri == NULL) {
1144 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1145 ctxt->sax->warning(ctxt->userData,
1146 "xmlns: %s not a valid URI\n", val);
1147 } else {
1148 if (uri->scheme == NULL) {
1149 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1150 ctxt->sax->warning(ctxt->userData,
1151 "xmlns: URI %s is not absolute\n", val);
1152 }
1153 xmlFreeURI(uri);
1154 }
1155 }
1156
1157 /* a default namespace definition */
1158 nsret = xmlNewNs(ctxt->node, val, NULL);
1159
1160#ifdef LIBXML_VALID_ENABLED
1161 /*
1162 * Validate also for namespace decls, they are attributes from
1163 * an XML-1.0 perspective
1164 */
1165 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1166 ctxt->myDoc && ctxt->myDoc->intSubset)
1167 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1168 ctxt->node, prefix, nsret, val);
1169#endif /* LIBXML_VALID_ENABLED */
1170 if (name != NULL)
1171 xmlFree(name);
1172 if (nval != NULL)
1173 xmlFree(nval);
1174 if (val != value)
1175 xmlFree(val);
1176 return;
1177 }
1178 if ((!ctxt->html) &&
1179 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1180 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1181 xmlNsPtr nsret;
1182 xmlChar *val;
1183
1184 if (!ctxt->replaceEntities) {
1185 ctxt->depth++;
1186 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1187 0,0,0);
1188 ctxt->depth--;
1189 if (val == NULL) {
1190 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1191 xmlFree(ns);
1192 if (name != NULL)
1193 xmlFree(name);
1194 return;
1195 }
1196 } else {
1197 val = (xmlChar *) value;
1198 }
1199
1200 if (val[0] == 0) {
1201 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1202 "Empty namespace name for prefix %s\n", name, NULL);
1203 }
1204 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1205 xmlURIPtr uri;
1206
1207 uri = xmlParseURI((const char *)val);
1208 if (uri == NULL) {
1209 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
1210 "xmlns:%s: %s not a valid URI\n", name, value);
1211 } else {
1212 if (uri->scheme == NULL) {
1213 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
1214 "xmlns:%s: URI %s is not absolute\n", name, value);
1215 }
1216 xmlFreeURI(uri);
1217 }
1218 }
1219
1220 /* a standard namespace definition */
1221 nsret = xmlNewNs(ctxt->node, val, name);
1222 xmlFree(ns);
1223#ifdef LIBXML_VALID_ENABLED
1224 /*
1225 * Validate also for namespace decls, they are attributes from
1226 * an XML-1.0 perspective
1227 */
1228 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1229 ctxt->myDoc && ctxt->myDoc->intSubset)
1230 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1231 ctxt->node, prefix, nsret, value);
1232#endif /* LIBXML_VALID_ENABLED */
1233 if (name != NULL)
1234 xmlFree(name);
1235 if (nval != NULL)
1236 xmlFree(nval);
1237 if (val != value)
1238 xmlFree(val);
1239 return;
1240 }
1241
1242 if (ns != NULL) {
1243 xmlAttrPtr prop;
1244 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
1245 if (namespace == NULL) {
1246 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1247 "Namespace prefix %s of attribute %s is not defined\n",
1248 ns, name);
1249 }
1250
1251 prop = ctxt->node->properties;
1252 while (prop != NULL) {
1253 if (prop->ns != NULL) {
1254 if ((xmlStrEqual(name, prop->name)) &&
1255 ((namespace == prop->ns) ||
1256 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1257 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1258 "Attribute %s in %s redefined\n",
1259 name, namespace->href);
1260 ctxt->wellFormed = 0;
1261 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1262 goto error;
1263 }
1264 }
1265 prop = prop->next;
1266 }
1267 } else {
1268 namespace = NULL;
1269 }
1270
1271 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1272 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1273
1274 if (ret != NULL) {
1275 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1276 xmlNodePtr tmp;
1277
1278 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1279 tmp = ret->children;
1280 while (tmp != NULL) {
1281 tmp->parent = (xmlNodePtr) ret;
1282 if (tmp->next == NULL)
1283 ret->last = tmp;
1284 tmp = tmp->next;
1285 }
1286 } else if (value != NULL) {
1287 ret->children = xmlNewDocText(ctxt->myDoc, value);
1288 ret->last = ret->children;
1289 if (ret->children != NULL)
1290 ret->children->parent = (xmlNodePtr) ret;
1291 }
1292 }
1293
1294#ifdef LIBXML_VALID_ENABLED
1295 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1296 ctxt->myDoc && ctxt->myDoc->intSubset) {
1297
1298 /*
1299 * If we don't substitute entities, the validation should be
1300 * done on a value with replaced entities anyway.
1301 */
1302 if (!ctxt->replaceEntities) {
1303 xmlChar *val;
1304
1305 ctxt->depth++;
1306 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1307 0,0,0);
1308 ctxt->depth--;
1309
1310 if (val == NULL)
1311 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1312 ctxt->myDoc, ctxt->node, ret, value);
1313 else {
1314 xmlChar *nvalnorm;
1315
1316 /*
1317 * Do the last stage of the attribute normalization
1318 * It need to be done twice ... it's an extra burden related
1319 * to the ability to keep xmlSAX2References in attributes
1320 */
1321 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1322 ctxt->node, fullname, val);
1323 if (nvalnorm != NULL) {
1324 xmlFree(val);
1325 val = nvalnorm;
1326 }
1327
1328 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1329 ctxt->myDoc, ctxt->node, ret, val);
1330 xmlFree(val);
1331 }
1332 } else {
1333 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1334 ctxt->node, ret, value);
1335 }
1336 } else
1337#endif /* LIBXML_VALID_ENABLED */
1338 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1339 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1340 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1341 /*
1342 * when validating, the ID registration is done at the attribute
1343 * validation level. Otherwise we have to do specific handling here.
1344 */
1345 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
1346 /*
1347 * Add the xml:id value
1348 *
1349 * Open issue: normalization of the value.
1350 */
1351 if (xmlValidateNCName(value, 1) != 0) {
1352 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1353 "xml:id : attribute value %s is not an NCName\n",
1354 (const char *) value, NULL);
1355 }
1356 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1357 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1358 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1359 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1360 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1361 }
1362
1363error:
1364 if (nval != NULL)
1365 xmlFree(nval);
1366 if (ns != NULL)
1367 xmlFree(ns);
1368}
1369
1370/*
1371 * xmlCheckDefaultedAttributes:
1372 *
1373 * Check defaulted attributes from the DTD
1374 */
1375static void
1376xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1377 const xmlChar *prefix, const xmlChar **atts) {
1378 xmlElementPtr elemDecl;
1379 const xmlChar *att;
1380 int internal = 1;
1381 int i;
1382
1383 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1384 if (elemDecl == NULL) {
1385 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1386 internal = 0;
1387 }
1388
1389process_external_subset:
1390
1391 if (elemDecl != NULL) {
1392 xmlAttributePtr attr = elemDecl->attributes;
1393 /*
1394 * Check against defaulted attributes from the external subset
1395 * if the document is stamped as standalone
1396 */
1397 if ((ctxt->myDoc->standalone == 1) &&
1398 (ctxt->myDoc->extSubset != NULL) &&
1399 (ctxt->validate)) {
1400 while (attr != NULL) {
1401 if ((attr->defaultValue != NULL) &&
1402 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1403 attr->elem, attr->name,
1404 attr->prefix) == attr) &&
1405 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1406 attr->elem, attr->name,
1407 attr->prefix) == NULL)) {
1408 xmlChar *fulln;
1409
1410 if (attr->prefix != NULL) {
1411 fulln = xmlStrdup(attr->prefix);
1412 fulln = xmlStrcat(fulln, BAD_CAST ":");
1413 fulln = xmlStrcat(fulln, attr->name);
1414 } else {
1415 fulln = xmlStrdup(attr->name);
1416 }
1417
1418 /*
1419 * Check that the attribute is not declared in the
1420 * serialization
1421 */
1422 att = NULL;
1423 if (atts != NULL) {
1424 i = 0;
1425 att = atts[i];
1426 while (att != NULL) {
1427 if (xmlStrEqual(att, fulln))
1428 break;
1429 i += 2;
1430 att = atts[i];
1431 }
1432 }
1433 if (att == NULL) {
1434 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
1435 "standalone: attribute %s on %s defaulted from external subset\n",
1436 (const char *)fulln,
1437 (const char *)attr->elem);
1438 }
1439 }
1440 attr = attr->nexth;
1441 }
1442 }
1443
1444 /*
1445 * Actually insert defaulted values when needed
1446 */
1447 attr = elemDecl->attributes;
1448 while (attr != NULL) {
1449 /*
1450 * Make sure that attributes redefinition occuring in the
1451 * internal subset are not overriden by definitions in the
1452 * external subset.
1453 */
1454 if (attr->defaultValue != NULL) {
1455 /*
1456 * the element should be instantiated in the tree if:
1457 * - this is a namespace prefix
1458 * - the user required for completion in the tree
1459 * like XSLT
1460 * - there isn't already an attribute definition
1461 * in the internal subset overriding it.
1462 */
1463 if (((attr->prefix != NULL) &&
1464 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1465 ((attr->prefix == NULL) &&
1466 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1467 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1468 xmlAttributePtr tst;
1469
1470 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1471 attr->elem, attr->name,
1472 attr->prefix);
1473 if ((tst == attr) || (tst == NULL)) {
1474 xmlChar fn[50];
1475 xmlChar *fulln;
1476
1477 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1478 if (fulln == NULL) {
1479 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1480 return;
1481 }
1482
1483 /*
1484 * Check that the attribute is not declared in the
1485 * serialization
1486 */
1487 att = NULL;
1488 if (atts != NULL) {
1489 i = 0;
1490 att = atts[i];
1491 while (att != NULL) {
1492 if (xmlStrEqual(att, fulln))
1493 break;
1494 i += 2;
1495 att = atts[i];
1496 }
1497 }
1498 if (att == NULL) {
1499 xmlSAX2AttributeInternal(ctxt, fulln,
1500 attr->defaultValue, prefix);
1501 }
1502 if ((fulln != fn) && (fulln != attr->name))
1503 xmlFree(fulln);
1504 }
1505 }
1506 }
1507 attr = attr->nexth;
1508 }
1509 if (internal == 1) {
1510 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1511 name, prefix);
1512 internal = 0;
1513 goto process_external_subset;
1514 }
1515 }
1516}
1517
1518/**
1519 * xmlSAX2StartElement:
1520 * @ctx: the user data (XML parser context)
1521 * @fullname: The element name, including namespace prefix
1522 * @atts: An array of name/value attributes pairs, NULL terminated
1523 *
1524 * called when an opening tag has been processed.
1525 */
1526void
1527xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1528{
1529 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1530 xmlNodePtr ret;
1531 xmlNodePtr parent;
1532 xmlNsPtr ns;
1533 xmlChar *name;
1534 xmlChar *prefix;
1535 const xmlChar *att;
1536 const xmlChar *value;
1537 int i;
1538
1539 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
1540 parent = ctxt->node;
1541#ifdef DEBUG_SAX
1542 xmlGenericError(xmlGenericErrorContext,
1543 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1544#endif
1545
1546 /*
1547 * First check on validity:
1548 */
1549 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1550 ((ctxt->myDoc->intSubset == NULL) ||
1551 ((ctxt->myDoc->intSubset->notations == NULL) &&
1552 (ctxt->myDoc->intSubset->elements == NULL) &&
1553 (ctxt->myDoc->intSubset->attributes == NULL) &&
1554 (ctxt->myDoc->intSubset->entities == NULL)))) {
1555 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1556 "Validation failed: no DTD found !", NULL, NULL);
1557 ctxt->validate = 0;
1558 }
1559
1560
1561 /*
1562 * Split the full name into a namespace prefix and the tag name
1563 */
1564 name = xmlSplitQName(ctxt, fullname, &prefix);
1565
1566
1567 /*
1568 * Note : the namespace resolution is deferred until the end of the
1569 * attributes parsing, since local namespace can be defined as
1570 * an attribute at this level.
1571 */
1572 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1573 if (ret == NULL) {
1574 if (prefix != NULL)
1575 xmlFree(prefix);
1576 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1577 return;
1578 }
1579 if (ctxt->myDoc->children == NULL) {
1580#ifdef DEBUG_SAX_TREE
1581 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1582#endif
1583 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1584 } else if (parent == NULL) {
1585 parent = ctxt->myDoc->children;
1586 }
1587 ctxt->nodemem = -1;
1588 if (ctxt->linenumbers) {
1589 if (ctxt->input != NULL) {
1590 if (ctxt->input->line < 65535)
1591 ret->line = (short) ctxt->input->line;
1592 else
1593 ret->line = 65535;
1594 }
1595 }
1596
1597 /*
1598 * We are parsing a new node.
1599 */
1600#ifdef DEBUG_SAX_TREE
1601 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1602#endif
1603 nodePush(ctxt, ret);
1604
1605 /*
1606 * Link the child element
1607 */
1608 if (parent != NULL) {
1609 if (parent->type == XML_ELEMENT_NODE) {
1610#ifdef DEBUG_SAX_TREE
1611 xmlGenericError(xmlGenericErrorContext,
1612 "adding child %s to %s\n", name, parent->name);
1613#endif
1614 xmlAddChild(parent, ret);
1615 } else {
1616#ifdef DEBUG_SAX_TREE
1617 xmlGenericError(xmlGenericErrorContext,
1618 "adding sibling %s to ", name);
1619 xmlDebugDumpOneNode(stderr, parent, 0);
1620#endif
1621 xmlAddSibling(parent, ret);
1622 }
1623 }
1624
1625 /*
1626 * Insert all the defaulted attributes from the DTD especially namespaces
1627 */
1628 if ((!ctxt->html) &&
1629 ((ctxt->myDoc->intSubset != NULL) ||
1630 (ctxt->myDoc->extSubset != NULL))) {
1631 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1632 }
1633
1634 /*
1635 * process all the attributes whose name start with "xmlns"
1636 */
1637 if (atts != NULL) {
1638 i = 0;
1639 att = atts[i++];
1640 value = atts[i++];
1641 if (!ctxt->html) {
1642 while ((att != NULL) && (value != NULL)) {
1643 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1644 (att[3] == 'n') && (att[4] == 's'))
1645 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1646
1647 att = atts[i++];
1648 value = atts[i++];
1649 }
1650 }
1651 }
1652
1653 /*
1654 * Search the namespace, note that since the attributes have been
1655 * processed, the local namespaces are available.
1656 */
1657 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1658 if ((ns == NULL) && (parent != NULL))
1659 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1660 if ((prefix != NULL) && (ns == NULL)) {
1661 ns = xmlNewNs(ret, NULL, prefix);
1662 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1663 "Namespace prefix %s is not defined\n",
1664 prefix, NULL);
1665 }
1666
1667 /*
1668 * set the namespace node, making sure that if the default namspace
1669 * is unbound on a parent we simply kee it NULL
1670 */
1671 if ((ns != NULL) && (ns->href != NULL) &&
1672 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1673 xmlSetNs(ret, ns);
1674
1675 /*
1676 * process all the other attributes
1677 */
1678 if (atts != NULL) {
1679 i = 0;
1680 att = atts[i++];
1681 value = atts[i++];
1682 if (ctxt->html) {
1683 while (att != NULL) {
1684 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1685 att = atts[i++];
1686 value = atts[i++];
1687 }
1688 } else {
1689 while ((att != NULL) && (value != NULL)) {
1690 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1691 (att[3] != 'n') || (att[4] != 's'))
1692 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1693
1694 /*
1695 * Next ones
1696 */
1697 att = atts[i++];
1698 value = atts[i++];
1699 }
1700 }
1701 }
1702
1703#ifdef LIBXML_VALID_ENABLED
1704 /*
1705 * If it's the Document root, finish the DTD validation and
1706 * check the document root element for validity
1707 */
1708 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
1709 int chk;
1710
1711 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1712 if (chk <= 0)
1713 ctxt->valid = 0;
1714 if (chk < 0)
1715 ctxt->wellFormed = 0;
1716 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1717 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
1718 }
1719#endif /* LIBXML_VALID_ENABLED */
1720
1721 if (prefix != NULL)
1722 xmlFree(prefix);
1723
1724}
1725
1726/**
1727 * xmlSAX2EndElement:
1728 * @ctx: the user data (XML parser context)
1729 * @name: The element name
1730 *
1731 * called when the end of an element has been detected.
1732 */
1733void
1734xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1735{
1736 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1737 xmlParserNodeInfo node_info;
1738 xmlNodePtr cur;
1739
1740 if (ctx == NULL) return;
1741 cur = ctxt->node;
1742#ifdef DEBUG_SAX
1743 if (name == NULL)
1744 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1745 else
1746 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1747#endif
1748
1749 /* Capture end position and add node */
1750 if (cur != NULL && ctxt->record_info) {
1751 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1752 node_info.end_line = ctxt->input->line;
1753 node_info.node = cur;
1754 xmlParserAddNodeInfo(ctxt, &node_info);
1755 }
1756 ctxt->nodemem = -1;
1757
1758#ifdef LIBXML_VALID_ENABLED
1759 if (ctxt->validate && ctxt->wellFormed &&
1760 ctxt->myDoc && ctxt->myDoc->intSubset)
1761 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1762 cur);
1763#endif /* LIBXML_VALID_ENABLED */
1764
1765
1766 /*
1767 * end of parsing of this node.
1768 */
1769#ifdef DEBUG_SAX_TREE
1770 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1771#endif
1772 nodePop(ctxt);
1773}
1774#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
1775
1776/*
1777 * xmlSAX2TextNode:
1778 * @ctxt: the parser context
1779 * @str: the input string
1780 * @len: the string length
1781 *
1782 * Remove the entities from an attribute value
1783 *
1784 * Returns the newly allocated string or NULL if not needed or error
1785 */
1786static xmlNodePtr
1787xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1788 xmlNodePtr ret;
1789 const xmlChar *intern = NULL;
1790
1791 /*
1792 * Allocate
1793 */
1794 if (ctxt->freeElems != NULL) {
1795 ret = ctxt->freeElems;
1796 ctxt->freeElems = ret->next;
1797 ctxt->freeElemsNr--;
1798 } else {
1799 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1800 }
1801 if (ret == NULL) {
1802 xmlErrMemory(ctxt, "xmlSAX2Characters");
1803 return(NULL);
1804 }
1805 memset(ret, 0, sizeof(xmlNode));
1806 /*
1807 * intern the formatting blanks found between tags, or the
1808 * very short strings
1809 */
1810 if (ctxt->dictNames) {
1811 xmlChar cur = str[len];
1812
1813 if ((len < (int) (2 * sizeof(void *))) &&
1814 (ctxt->options & XML_PARSE_COMPACT)) {
1815 /* store the string in the node overrithing properties and nsDef */
1816 xmlChar *tmp = (xmlChar *) &(ret->properties);
1817 memcpy(tmp, str, len);
1818 tmp[len] = 0;
1819 intern = tmp;
1820 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1821 ((cur == '<') && (str[len + 1] != '!')))) {
1822 intern = xmlDictLookup(ctxt->dict, str, len);
1823 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
1824 (str[len + 1] != '!')) {
1825 int i;
1826
1827 for (i = 1;i < len;i++) {
1828 if (!IS_BLANK_CH(str[i])) goto skip;
1829 }
1830 intern = xmlDictLookup(ctxt->dict, str, len);
1831 }
1832 }
1833skip:
1834 ret->type = XML_TEXT_NODE;
1835
1836 ret->name = xmlStringText;
1837 if (intern == NULL) {
1838 ret->content = xmlStrndup(str, len);
1839 if (ret->content == NULL) {
1840 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1841 xmlFree(ret);
1842 return(NULL);
1843 }
1844 } else
1845 ret->content = (xmlChar *) intern;
1846
1847 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1848 xmlRegisterNodeDefaultValue(ret);
1849 return(ret);
1850}
1851
1852#ifdef LIBXML_VALID_ENABLED
1853/*
1854 * xmlSAX2DecodeAttrEntities:
1855 * @ctxt: the parser context
1856 * @str: the input string
1857 * @len: the string length
1858 *
1859 * Remove the entities from an attribute value
1860 *
1861 * Returns the newly allocated string or NULL if not needed or error
1862 */
1863static xmlChar *
1864xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1865 const xmlChar *end) {
1866 const xmlChar *in;
1867 xmlChar *ret;
1868
1869 in = str;
1870 while (in < end)
1871 if (*in++ == '&')
1872 goto decode;
1873 return(NULL);
1874decode:
1875 ctxt->depth++;
1876 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1877 XML_SUBSTITUTE_REF, 0,0,0);
1878 ctxt->depth--;
1879 return(ret);
1880}
1881#endif /* LIBXML_VALID_ENABLED */
1882
1883/**
1884 * xmlSAX2AttributeNs:
1885 * @ctx: the user data (XML parser context)
1886 * @localname: the local name of the attribute
1887 * @prefix: the attribute namespace prefix if available
1888 * @URI: the attribute namespace name if available
1889 * @value: Start of the attribute value
1890 * @valueend: end of the attribute value
1891 *
1892 * Handle an attribute that has been read by the parser.
1893 * The default handling is to convert the attribute into an
1894 * DOM subtree and past it in a new xmlAttr element added to
1895 * the element.
1896 */
1897static void
1898xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1899 const xmlChar * localname,
1900 const xmlChar * prefix,
1901 const xmlChar * value,
1902 const xmlChar * valueend)
1903{
1904 xmlAttrPtr ret;
1905 xmlNsPtr namespace = NULL;
1906 xmlChar *dup = NULL;
1907
1908 /*
1909 * Note: if prefix == NULL, the attribute is not in the default namespace
1910 */
1911 if (prefix != NULL)
1912 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1913
1914 /*
1915 * allocate the node
1916 */
1917 if (ctxt->freeAttrs != NULL) {
1918 ret = ctxt->freeAttrs;
1919 ctxt->freeAttrs = ret->next;
1920 ctxt->freeAttrsNr--;
1921 memset(ret, 0, sizeof(xmlAttr));
1922 ret->type = XML_ATTRIBUTE_NODE;
1923
1924 ret->parent = ctxt->node;
1925 ret->doc = ctxt->myDoc;
1926 ret->ns = namespace;
1927
1928 if (ctxt->dictNames)
1929 ret->name = localname;
1930 else
1931 ret->name = xmlStrdup(localname);
1932
1933 /* link at the end to preserv order, TODO speed up with a last */
1934 if (ctxt->node->properties == NULL) {
1935 ctxt->node->properties = ret;
1936 } else {
1937 xmlAttrPtr prev = ctxt->node->properties;
1938
1939 while (prev->next != NULL) prev = prev->next;
1940 prev->next = ret;
1941 ret->prev = prev;
1942 }
1943
1944 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1945 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1946 } else {
1947 if (ctxt->dictNames)
1948 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1949 (xmlChar *) localname, NULL);
1950 else
1951 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1952 if (ret == NULL) {
1953 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
1954 return;
1955 }
1956 }
1957
1958 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1959 xmlNodePtr tmp;
1960
1961 /*
1962 * We know that if there is an entity reference, then
1963 * the string has been dup'ed and terminates with 0
1964 * otherwise with ' or "
1965 */
1966 if (*valueend != 0) {
1967 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1968 ret->children = tmp;
1969 ret->last = tmp;
1970 if (tmp != NULL) {
1971 tmp->doc = ret->doc;
1972 tmp->parent = (xmlNodePtr) ret;
1973 }
1974 } else {
1975 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1976 valueend - value);
1977 tmp = ret->children;
1978 while (tmp != NULL) {
1979 tmp->doc = ret->doc;
1980 tmp->parent = (xmlNodePtr) ret;
1981 if (tmp->next == NULL)
1982 ret->last = tmp;
1983 tmp = tmp->next;
1984 }
1985 }
1986 } else if (value != NULL) {
1987 xmlNodePtr tmp;
1988
1989 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1990 ret->children = tmp;
1991 ret->last = tmp;
1992 if (tmp != NULL) {
1993 tmp->doc = ret->doc;
1994 tmp->parent = (xmlNodePtr) ret;
1995 }
1996 }
1997
1998#ifdef LIBXML_VALID_ENABLED
1999 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2000 ctxt->myDoc && ctxt->myDoc->intSubset) {
2001 /*
2002 * If we don't substitute entities, the validation should be
2003 * done on a value with replaced entities anyway.
2004 */
2005 if (!ctxt->replaceEntities) {
2006 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2007 if (dup == NULL) {
2008 if (*valueend == 0) {
2009 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2010 ctxt->myDoc, ctxt->node, ret, value);
2011 } else {
2012 /*
2013 * That should already be normalized.
2014 * cheaper to finally allocate here than duplicate
2015 * entry points in the full validation code
2016 */
2017 dup = xmlStrndup(value, valueend - value);
2018
2019 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2020 ctxt->myDoc, ctxt->node, ret, dup);
2021 }
2022 } else {
2023 /*
2024 * dup now contains a string of the flattened attribute
2025 * content with entities substitued. Check if we need to
2026 * apply an extra layer of normalization.
2027 * It need to be done twice ... it's an extra burden related
2028 * to the ability to keep references in attributes
2029 */
2030 if (ctxt->attsSpecial != NULL) {
2031 xmlChar *nvalnorm;
2032 xmlChar fn[50];
2033 xmlChar *fullname;
2034
2035 fullname = xmlBuildQName(localname, prefix, fn, 50);
2036 if (fullname != NULL) {
2037 ctxt->vctxt.valid = 1;
2038 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2039 &ctxt->vctxt, ctxt->myDoc,
2040 ctxt->node, fullname, dup);
2041 if (ctxt->vctxt.valid != 1)
2042 ctxt->valid = 0;
2043
2044 if ((fullname != fn) && (fullname != localname))
2045 xmlFree(fullname);
2046 if (nvalnorm != NULL) {
2047 xmlFree(dup);
2048 dup = nvalnorm;
2049 }
2050 }
2051 }
2052
2053 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2054 ctxt->myDoc, ctxt->node, ret, dup);
2055 }
2056 } else {
2057 /*
2058 * if entities already have been substitued, then
2059 * the attribute as passed is already normalized
2060 */
2061 dup = xmlStrndup(value, valueend - value);
2062
2063 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2064 ctxt->myDoc, ctxt->node, ret, dup);
2065 }
2066 } else
2067#endif /* LIBXML_VALID_ENABLED */
2068 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
2069 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2070 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2071 /*
2072 * when validating, the ID registration is done at the attribute
2073 * validation level. Otherwise we have to do specific handling here.
2074 */
2075 if ((prefix == ctxt->str_xml) &&
2076 (localname[0] == 'i') && (localname[1] == 'd') &&
2077 (localname[2] == 0)) {
2078 /*
2079 * Add the xml:id value
2080 *
2081 * Open issue: normalization of the value.
2082 */
2083 if (dup == NULL)
2084 dup = xmlStrndup(value, valueend - value);
2085#ifdef LIBXML_VALID_ENABLED
2086 if (xmlValidateNCName(dup, 1) != 0) {
2087 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2088 "xml:id : attribute value %s is not an NCName\n",
2089 (const char *) dup, NULL);
2090 }
2091#endif
2092 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2093 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2094 /* might be worth duplicate entry points and not copy */
2095 if (dup == NULL)
2096 dup = xmlStrndup(value, valueend - value);
2097 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2098 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2099 if (dup == NULL)
2100 dup = xmlStrndup(value, valueend - value);
2101 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2102 }
2103 }
2104 if (dup != NULL)
2105 xmlFree(dup);
2106}
2107
2108/**
2109 * xmlSAX2StartElementNs:
2110 * @ctx: the user data (XML parser context)
2111 * @localname: the local name of the element
2112 * @prefix: the element namespace prefix if available
2113 * @URI: the element namespace name if available
2114 * @nb_namespaces: number of namespace definitions on that node
2115 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2116 * @nb_attributes: the number of attributes on that node
2117 * @nb_defaulted: the number of defaulted attributes.
2118 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2119 * attribute values.
2120 *
2121 * SAX2 callback when an element start has been detected by the parser.
2122 * It provides the namespace informations for the element, as well as
2123 * the new namespace declarations on the element.
2124 */
2125void
2126xmlSAX2StartElementNs(void *ctx,
2127 const xmlChar *localname,
2128 const xmlChar *prefix,
2129 const xmlChar *URI,
2130 int nb_namespaces,
2131 const xmlChar **namespaces,
2132 int nb_attributes,
2133 int nb_defaulted,
2134 const xmlChar **attributes)
2135{
2136 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2137 xmlNodePtr ret;
2138 xmlNodePtr parent;
2139 xmlNsPtr last = NULL, ns;
2140 const xmlChar *uri, *pref;
2141 int i, j;
2142
2143 if (ctx == NULL) return;
2144 parent = ctxt->node;
2145 /*
2146 * First check on validity:
2147 */
2148 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2149 ((ctxt->myDoc->intSubset == NULL) ||
2150 ((ctxt->myDoc->intSubset->notations == NULL) &&
2151 (ctxt->myDoc->intSubset->elements == NULL) &&
2152 (ctxt->myDoc->intSubset->attributes == NULL) &&
2153 (ctxt->myDoc->intSubset->entities == NULL)))) {
2154 xmlErrValid(ctxt, XML_ERR_NO_DTD,
2155 "Validation failed: no DTD found !", NULL, NULL);
2156 ctxt->validate = 0;
2157 }
2158
2159 /*
2160 * allocate the node
2161 */
2162 if (ctxt->freeElems != NULL) {
2163 ret = ctxt->freeElems;
2164 ctxt->freeElems = ret->next;
2165 ctxt->freeElemsNr--;
2166 memset(ret, 0, sizeof(xmlNode));
2167 ret->type = XML_ELEMENT_NODE;
2168
2169 if (ctxt->dictNames)
2170 ret->name = localname;
2171 else {
2172 ret->name = xmlStrdup(localname);
2173 if (ret->name == NULL) {
2174 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2175 return;
2176 }
2177 }
2178 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2179 xmlRegisterNodeDefaultValue(ret);
2180 } else {
2181 if (ctxt->dictNames)
2182 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2183 (xmlChar *) localname, NULL);
2184 else
2185 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2186 if (ret == NULL) {
2187 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2188 return;
2189 }
2190 }
2191 if (ctxt->linenumbers) {
2192 if (ctxt->input != NULL) {
2193 if (ctxt->input->line < 65535)
2194 ret->line = (short) ctxt->input->line;
2195 else
2196 ret->line = 65535;
2197 }
2198 }
2199
2200 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2201 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2202 }
2203 /*
2204 * Build the namespace list
2205 */
2206 for (i = 0,j = 0;j < nb_namespaces;j++) {
2207 pref = namespaces[i++];
2208 uri = namespaces[i++];
2209 ns = xmlNewNs(NULL, uri, pref);
2210 if (ns != NULL) {
2211 if (last == NULL) {
2212 ret->nsDef = last = ns;
2213 } else {
2214 last->next = ns;
2215 last = ns;
2216 }
2217 if ((URI != NULL) && (prefix == pref))
2218 ret->ns = ns;
2219 } else {
2220 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2221 return;
2222 }
2223#ifdef LIBXML_VALID_ENABLED
2224 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2225 ctxt->myDoc && ctxt->myDoc->intSubset) {
2226 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2227 ret, prefix, ns, uri);
2228 }
2229#endif /* LIBXML_VALID_ENABLED */
2230 }
2231 ctxt->nodemem = -1;
2232
2233 /*
2234 * We are parsing a new node.
2235 */
2236 nodePush(ctxt, ret);
2237
2238 /*
2239 * Link the child element
2240 */
2241 if (parent != NULL) {
2242 if (parent->type == XML_ELEMENT_NODE) {
2243 xmlAddChild(parent, ret);
2244 } else {
2245 xmlAddSibling(parent, ret);
2246 }
2247 }
2248
2249 /*
2250 * Insert the defaulted attributes from the DTD only if requested:
2251 */
2252 if ((nb_defaulted != 0) &&
2253 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2254 nb_attributes -= nb_defaulted;
2255
2256 /*
2257 * Search the namespace if it wasn't already found
2258 * Note that, if prefix is NULL, this searches for the default Ns
2259 */
2260 if ((URI != NULL) && (ret->ns == NULL)) {
2261 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2262 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2263 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2264 }
2265 if (ret->ns == NULL) {
2266 ns = xmlNewNs(ret, NULL, prefix);
2267 if (ns == NULL) {
2268
2269 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2270 return;
2271 }
2272 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2273 "Namespace prefix %s was not found\n",
2274 prefix, NULL);
2275 }
2276 }
2277
2278 /*
2279 * process all the other attributes
2280 */
2281 if (nb_attributes > 0) {
2282 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2283 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2284 attributes[j+3], attributes[j+4]);
2285 }
2286 }
2287
2288#ifdef LIBXML_VALID_ENABLED
2289 /*
2290 * If it's the Document root, finish the DTD validation and
2291 * check the document root element for validity
2292 */
2293 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
2294 int chk;
2295
2296 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2297 if (chk <= 0)
2298 ctxt->valid = 0;
2299 if (chk < 0)
2300 ctxt->wellFormed = 0;
2301 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2302 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
2303 }
2304#endif /* LIBXML_VALID_ENABLED */
2305}
2306
2307/**
2308 * xmlSAX2EndElementNs:
2309 * @ctx: the user data (XML parser context)
2310 * @localname: the local name of the element
2311 * @prefix: the element namespace prefix if available
2312 * @URI: the element namespace name if available
2313 *
2314 * SAX2 callback when an element end has been detected by the parser.
2315 * It provides the namespace informations for the element.
2316 */
2317void
2318xmlSAX2EndElementNs(void *ctx,
2319 const xmlChar * localname ATTRIBUTE_UNUSED,
2320 const xmlChar * prefix ATTRIBUTE_UNUSED,
2321 const xmlChar * URI ATTRIBUTE_UNUSED)
2322{
2323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2324 xmlParserNodeInfo node_info;
2325 xmlNodePtr cur;
2326
2327 if (ctx == NULL) return;
2328 cur = ctxt->node;
2329 /* Capture end position and add node */
2330 if ((ctxt->record_info) && (cur != NULL)) {
2331 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2332 node_info.end_line = ctxt->input->line;
2333 node_info.node = cur;
2334 xmlParserAddNodeInfo(ctxt, &node_info);
2335 }
2336 ctxt->nodemem = -1;
2337
2338#ifdef LIBXML_VALID_ENABLED
2339 if (ctxt->validate && ctxt->wellFormed &&
2340 ctxt->myDoc && ctxt->myDoc->intSubset)
2341 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
2342#endif /* LIBXML_VALID_ENABLED */
2343
2344 /*
2345 * end of parsing of this node.
2346 */
2347 nodePop(ctxt);
2348}
2349
2350/**
2351 * xmlSAX2Reference:
2352 * @ctx: the user data (XML parser context)
2353 * @name: The entity name
2354 *
2355 * called when an entity xmlSAX2Reference is detected.
2356 */
2357void
2358xmlSAX2Reference(void *ctx, const xmlChar *name)
2359{
2360 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2361 xmlNodePtr ret;
2362
2363 if (ctx == NULL) return;
2364#ifdef DEBUG_SAX
2365 xmlGenericError(xmlGenericErrorContext,
2366 "SAX.xmlSAX2Reference(%s)\n", name);
2367#endif
2368 if (name[0] == '#')
2369 ret = xmlNewCharRef(ctxt->myDoc, name);
2370 else
2371 ret = xmlNewReference(ctxt->myDoc, name);
2372#ifdef DEBUG_SAX_TREE
2373 xmlGenericError(xmlGenericErrorContext,
2374 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2375#endif
2376 xmlAddChild(ctxt->node, ret);
2377}
2378
2379/**
2380 * xmlSAX2Characters:
2381 * @ctx: the user data (XML parser context)
2382 * @ch: a xmlChar string
2383 * @len: the number of xmlChar
2384 *
2385 * receiving some chars from the parser.
2386 */
2387void
2388xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2389{
2390 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2391 xmlNodePtr lastChild;
2392
2393 if (ctx == NULL) return;
2394#ifdef DEBUG_SAX
2395 xmlGenericError(xmlGenericErrorContext,
2396 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2397#endif
2398 /*
2399 * Handle the data if any. If there is no child
2400 * add it as content, otherwise if the last child is text,
2401 * concatenate it, else create a new node of type text.
2402 */
2403
2404 if (ctxt->node == NULL) {
2405#ifdef DEBUG_SAX_TREE
2406 xmlGenericError(xmlGenericErrorContext,
2407 "add chars: ctxt->node == NULL !\n");
2408#endif
2409 return;
2410 }
2411 lastChild = ctxt->node->last;
2412#ifdef DEBUG_SAX_TREE
2413 xmlGenericError(xmlGenericErrorContext,
2414 "add chars to %s \n", ctxt->node->name);
2415#endif
2416
2417 /*
2418 * Here we needed an accelerator mechanism in case of very large
2419 * elements. Use an attribute in the structure !!!
2420 */
2421 if (lastChild == NULL) {
2422 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2423 if (lastChild != NULL) {
2424 ctxt->node->children = lastChild;
2425 ctxt->node->last = lastChild;
2426 lastChild->parent = ctxt->node;
2427 lastChild->doc = ctxt->node->doc;
2428 ctxt->nodelen = len;
2429 ctxt->nodemem = len + 1;
2430 } else {
2431 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2432 return;
2433 }
2434 } else {
2435 int coalesceText = (lastChild != NULL) &&
2436 (lastChild->type == XML_TEXT_NODE) &&
2437 (lastChild->name == xmlStringText);
2438 if ((coalesceText) && (ctxt->nodemem != 0)) {
2439 /*
2440 * The whole point of maintaining nodelen and nodemem,
2441 * xmlTextConcat is too costly, i.e. compute length,
2442 * reallocate a new buffer, move data, append ch. Here
2443 * We try to minimaze realloc() uses and avoid copying
2444 * and recomputing length over and over.
2445 */
2446 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2447 lastChild->content = xmlStrdup(lastChild->content);
2448 lastChild->properties = NULL;
2449 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2450 (xmlDictOwns(ctxt->dict, lastChild->content))) {
2451 lastChild->content = xmlStrdup(lastChild->content);
2452 }
2453 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
2454 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
2455 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2456 return;
2457 }
2458 if (ctxt->nodelen + len >= ctxt->nodemem) {
2459 xmlChar *newbuf;
2460 size_t size;
2461
2462 size = ctxt->nodemem + len;
2463 size *= 2;
2464 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2465 if (newbuf == NULL) {
2466 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2467 return;
2468 }
2469 ctxt->nodemem = size;
2470 lastChild->content = newbuf;
2471 }
2472 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2473 ctxt->nodelen += len;
2474 lastChild->content[ctxt->nodelen] = 0;
2475 } else if (coalesceText) {
2476 if (xmlTextConcat(lastChild, ch, len)) {
2477 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2478 }
2479 if (ctxt->node->children != NULL) {
2480 ctxt->nodelen = xmlStrlen(lastChild->content);
2481 ctxt->nodemem = ctxt->nodelen + 1;
2482 }
2483 } else {
2484 /* Mixed content, first time */
2485 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2486 if (lastChild != NULL) {
2487 xmlAddChild(ctxt->node, lastChild);
2488 if (ctxt->node->children != NULL) {
2489 ctxt->nodelen = len;
2490 ctxt->nodemem = len + 1;
2491 }
2492 }
2493 }
2494 }
2495}
2496
2497/**
2498 * xmlSAX2IgnorableWhitespace:
2499 * @ctx: the user data (XML parser context)
2500 * @ch: a xmlChar string
2501 * @len: the number of xmlChar
2502 *
2503 * receiving some ignorable whitespaces from the parser.
2504 * UNUSED: by default the DOM building will use xmlSAX2Characters
2505 */
2506void
2507xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2508{
2509 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2510#ifdef DEBUG_SAX
2511 xmlGenericError(xmlGenericErrorContext,
2512 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2513#endif
2514}
2515
2516/**
2517 * xmlSAX2ProcessingInstruction:
2518 * @ctx: the user data (XML parser context)
2519 * @target: the target name
2520 * @data: the PI data's
2521 *
2522 * A processing instruction has been parsed.
2523 */
2524void
2525xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2526 const xmlChar *data)
2527{
2528 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2529 xmlNodePtr ret;
2530 xmlNodePtr parent;
2531
2532 if (ctx == NULL) return;
2533 parent = ctxt->node;
2534#ifdef DEBUG_SAX
2535 xmlGenericError(xmlGenericErrorContext,
2536 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2537#endif
2538
2539 ret = xmlNewDocPI(ctxt->myDoc, target, data);
2540 if (ret == NULL) return;
2541 parent = ctxt->node;
2542
2543 if (ctxt->linenumbers) {
2544 if (ctxt->input != NULL) {
2545 if (ctxt->input->line < 65535)
2546 ret->line = (short) ctxt->input->line;
2547 else
2548 ret->line = 65535;
2549 }
2550 }
2551 if (ctxt->inSubset == 1) {
2552 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2553 return;
2554 } else if (ctxt->inSubset == 2) {
2555 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2556 return;
2557 }
2558 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2559#ifdef DEBUG_SAX_TREE
2560 xmlGenericError(xmlGenericErrorContext,
2561 "Setting PI %s as root\n", target);
2562#endif
2563 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2564 return;
2565 }
2566 if (parent->type == XML_ELEMENT_NODE) {
2567#ifdef DEBUG_SAX_TREE
2568 xmlGenericError(xmlGenericErrorContext,
2569 "adding PI %s child to %s\n", target, parent->name);
2570#endif
2571 xmlAddChild(parent, ret);
2572 } else {
2573#ifdef DEBUG_SAX_TREE
2574 xmlGenericError(xmlGenericErrorContext,
2575 "adding PI %s sibling to ", target);
2576 xmlDebugDumpOneNode(stderr, parent, 0);
2577#endif
2578 xmlAddSibling(parent, ret);
2579 }
2580}
2581
2582/**
2583 * xmlSAX2Comment:
2584 * @ctx: the user data (XML parser context)
2585 * @value: the xmlSAX2Comment content
2586 *
2587 * A xmlSAX2Comment has been parsed.
2588 */
2589void
2590xmlSAX2Comment(void *ctx, const xmlChar *value)
2591{
2592 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2593 xmlNodePtr ret;
2594 xmlNodePtr parent;
2595
2596 if (ctx == NULL) return;
2597 parent = ctxt->node;
2598#ifdef DEBUG_SAX
2599 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2600#endif
2601 ret = xmlNewDocComment(ctxt->myDoc, value);
2602 if (ret == NULL) return;
2603 if (ctxt->linenumbers) {
2604 if (ctxt->input != NULL) {
2605 if (ctxt->input->line < 65535)
2606 ret->line = (short) ctxt->input->line;
2607 else
2608 ret->line = 65535;
2609 }
2610 }
2611
2612 if (ctxt->inSubset == 1) {
2613 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2614 return;
2615 } else if (ctxt->inSubset == 2) {
2616 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2617 return;
2618 }
2619 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2620#ifdef DEBUG_SAX_TREE
2621 xmlGenericError(xmlGenericErrorContext,
2622 "Setting xmlSAX2Comment as root\n");
2623#endif
2624 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2625 return;
2626 }
2627 if (parent->type == XML_ELEMENT_NODE) {
2628#ifdef DEBUG_SAX_TREE
2629 xmlGenericError(xmlGenericErrorContext,
2630 "adding xmlSAX2Comment child to %s\n", parent->name);
2631#endif
2632 xmlAddChild(parent, ret);
2633 } else {
2634#ifdef DEBUG_SAX_TREE
2635 xmlGenericError(xmlGenericErrorContext,
2636 "adding xmlSAX2Comment sibling to ");
2637 xmlDebugDumpOneNode(stderr, parent, 0);
2638#endif
2639 xmlAddSibling(parent, ret);
2640 }
2641}
2642
2643/**
2644 * xmlSAX2CDataBlock:
2645 * @ctx: the user data (XML parser context)
2646 * @value: The pcdata content
2647 * @len: the block length
2648 *
2649 * called when a pcdata block has been parsed
2650 */
2651void
2652xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2653{
2654 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2655 xmlNodePtr ret, lastChild;
2656
2657 if (ctx == NULL) return;
2658#ifdef DEBUG_SAX
2659 xmlGenericError(xmlGenericErrorContext,
2660 "SAX.pcdata(%.10s, %d)\n", value, len);
2661#endif
2662 lastChild = xmlGetLastChild(ctxt->node);
2663#ifdef DEBUG_SAX_TREE
2664 xmlGenericError(xmlGenericErrorContext,
2665 "add chars to %s \n", ctxt->node->name);
2666#endif
2667 if ((lastChild != NULL) &&
2668 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2669 xmlTextConcat(lastChild, value, len);
2670 } else {
2671 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2672 xmlAddChild(ctxt->node, ret);
2673 }
2674}
2675
2676static int xmlSAX2DefaultVersionValue = 2;
2677
2678#ifdef LIBXML_SAX1_ENABLED
2679/**
2680 * xmlSAXDefaultVersion:
2681 * @version: the version, 1 or 2
2682 *
2683 * Set the default version of SAX used globally by the library.
2684 * By default, during initialization the default is set to 2.
2685 * Note that it is generally a better coding style to use
2686 * xmlSAXVersion() to set up the version explicitly for a given
2687 * parsing context.
2688 *
2689 * Returns the previous value in case of success and -1 in case of error.
2690 */
2691int
2692xmlSAXDefaultVersion(int version)
2693{
2694 int ret = xmlSAX2DefaultVersionValue;
2695
2696 if ((version != 1) && (version != 2))
2697 return(-1);
2698 xmlSAX2DefaultVersionValue = version;
2699 return(ret);
2700}
2701#endif /* LIBXML_SAX1_ENABLED */
2702
2703/**
2704 * xmlSAXVersion:
2705 * @hdlr: the SAX handler
2706 * @version: the version, 1 or 2
2707 *
2708 * Initialize the default XML SAX handler according to the version
2709 *
2710 * Returns 0 in case of success and -1 in case of error.
2711 */
2712int
2713xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2714{
2715 if (hdlr == NULL) return(-1);
2716 if (version == 2) {
2717 hdlr->startElement = NULL;
2718 hdlr->endElement = NULL;
2719 hdlr->startElementNs = xmlSAX2StartElementNs;
2720 hdlr->endElementNs = xmlSAX2EndElementNs;
2721 hdlr->serror = NULL;
2722 hdlr->initialized = XML_SAX2_MAGIC;
2723#ifdef LIBXML_SAX1_ENABLED
2724 } else if (version == 1) {
2725 hdlr->startElement = xmlSAX2StartElement;
2726 hdlr->endElement = xmlSAX2EndElement;
2727 hdlr->initialized = 1;
2728#endif /* LIBXML_SAX1_ENABLED */
2729 } else
2730 return(-1);
2731 hdlr->internalSubset = xmlSAX2InternalSubset;
2732 hdlr->externalSubset = xmlSAX2ExternalSubset;
2733 hdlr->isStandalone = xmlSAX2IsStandalone;
2734 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2735 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2736 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2737 hdlr->getEntity = xmlSAX2GetEntity;
2738 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2739 hdlr->entityDecl = xmlSAX2EntityDecl;
2740 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2741 hdlr->elementDecl = xmlSAX2ElementDecl;
2742 hdlr->notationDecl = xmlSAX2NotationDecl;
2743 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2744 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2745 hdlr->startDocument = xmlSAX2StartDocument;
2746 hdlr->endDocument = xmlSAX2EndDocument;
2747 hdlr->reference = xmlSAX2Reference;
2748 hdlr->characters = xmlSAX2Characters;
2749 hdlr->cdataBlock = xmlSAX2CDataBlock;
2750 hdlr->ignorableWhitespace = xmlSAX2Characters;
2751 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2752 hdlr->comment = xmlSAX2Comment;
2753 hdlr->warning = xmlParserWarning;
2754 hdlr->error = xmlParserError;
2755 hdlr->fatalError = xmlParserError;
2756
2757 return(0);
2758}
2759
2760/**
2761 * xmlSAX2InitDefaultSAXHandler:
2762 * @hdlr: the SAX handler
2763 * @warning: flag if non-zero sets the handler warning procedure
2764 *
2765 * Initialize the default XML SAX2 handler
2766 */
2767void
2768xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2769{
2770 if ((hdlr == NULL) || (hdlr->initialized != 0))
2771 return;
2772
2773 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2774 if (warning == 0)
2775 hdlr->warning = NULL;
2776 else
2777 hdlr->warning = xmlParserWarning;
2778}
2779
2780/**
2781 * xmlDefaultSAXHandlerInit:
2782 *
2783 * Initialize the default SAX2 handler
2784 */
2785void
2786xmlDefaultSAXHandlerInit(void)
2787{
2788#ifdef LIBXML_SAX1_ENABLED
2789 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
2790#endif /* LIBXML_SAX1_ENABLED */
2791}
2792
2793#ifdef LIBXML_HTML_ENABLED
2794
2795/**
2796 * xmlSAX2InitHtmlDefaultSAXHandler:
2797 * @hdlr: the SAX handler
2798 *
2799 * Initialize the default HTML SAX2 handler
2800 */
2801void
2802xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2803{
2804 if ((hdlr == NULL) || (hdlr->initialized != 0))
2805 return;
2806
2807 hdlr->internalSubset = xmlSAX2InternalSubset;
2808 hdlr->externalSubset = NULL;
2809 hdlr->isStandalone = NULL;
2810 hdlr->hasInternalSubset = NULL;
2811 hdlr->hasExternalSubset = NULL;
2812 hdlr->resolveEntity = NULL;
2813 hdlr->getEntity = xmlSAX2GetEntity;
2814 hdlr->getParameterEntity = NULL;
2815 hdlr->entityDecl = NULL;
2816 hdlr->attributeDecl = NULL;
2817 hdlr->elementDecl = NULL;
2818 hdlr->notationDecl = NULL;
2819 hdlr->unparsedEntityDecl = NULL;
2820 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2821 hdlr->startDocument = xmlSAX2StartDocument;
2822 hdlr->endDocument = xmlSAX2EndDocument;
2823 hdlr->startElement = xmlSAX2StartElement;
2824 hdlr->endElement = xmlSAX2EndElement;
2825 hdlr->reference = NULL;
2826 hdlr->characters = xmlSAX2Characters;
2827 hdlr->cdataBlock = xmlSAX2CDataBlock;
2828 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2829 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2830 hdlr->comment = xmlSAX2Comment;
2831 hdlr->warning = xmlParserWarning;
2832 hdlr->error = xmlParserError;
2833 hdlr->fatalError = xmlParserError;
2834
2835 hdlr->initialized = 1;
2836}
2837
2838/**
2839 * htmlDefaultSAXHandlerInit:
2840 *
2841 * Initialize the default SAX handler
2842 */
2843void
2844htmlDefaultSAXHandlerInit(void)
2845{
2846 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
2847}
2848
2849#endif /* LIBXML_HTML_ENABLED */
2850
2851#ifdef LIBXML_DOCB_ENABLED
2852
2853/**
2854 * xmlSAX2InitDocbDefaultSAXHandler:
2855 * @hdlr: the SAX handler
2856 *
2857 * Initialize the default DocBook SAX2 handler
2858 */
2859void
2860xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2861{
2862 if ((hdlr == NULL) || (hdlr->initialized != 0))
2863 return;
2864
2865 hdlr->internalSubset = xmlSAX2InternalSubset;
2866 hdlr->externalSubset = NULL;
2867 hdlr->isStandalone = xmlSAX2IsStandalone;
2868 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2869 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2870 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2871 hdlr->getEntity = xmlSAX2GetEntity;
2872 hdlr->getParameterEntity = NULL;
2873 hdlr->entityDecl = xmlSAX2EntityDecl;
2874 hdlr->attributeDecl = NULL;
2875 hdlr->elementDecl = NULL;
2876 hdlr->notationDecl = NULL;
2877 hdlr->unparsedEntityDecl = NULL;
2878 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2879 hdlr->startDocument = xmlSAX2StartDocument;
2880 hdlr->endDocument = xmlSAX2EndDocument;
2881 hdlr->startElement = xmlSAX2StartElement;
2882 hdlr->endElement = xmlSAX2EndElement;
2883 hdlr->reference = xmlSAX2Reference;
2884 hdlr->characters = xmlSAX2Characters;
2885 hdlr->cdataBlock = NULL;
2886 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2887 hdlr->processingInstruction = NULL;
2888 hdlr->comment = xmlSAX2Comment;
2889 hdlr->warning = xmlParserWarning;
2890 hdlr->error = xmlParserError;
2891 hdlr->fatalError = xmlParserError;
2892
2893 hdlr->initialized = 1;
2894}
2895
2896/**
2897 * docbDefaultSAXHandlerInit:
2898 *
2899 * Initialize the default SAX handler
2900 */
2901void
2902docbDefaultSAXHandlerInit(void)
2903{
2904 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
2905}
2906
2907#endif /* LIBXML_DOCB_ENABLED */
2908#define bottom_SAX2
2909#include "elfgcchack.h"
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use