blob: e5d6eb50810aa31a56456593c08a5870d882fdc3 [file] [log] [blame]
Daniel Veillard1af9a412003-08-20 22:54:39 +00001/*
2 * SAX2.c : Default SAX2 handler to build a tree.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <daniel@veillard.com>
7 */
8
9
10#define IN_LIBXML
11#include "libxml.h"
12#include <stdlib.h>
13#include <string.h>
Daniel Veillard1dc9feb2008-11-17 15:59:21 +000014#include <limits.h>
Daniel Veillard1af9a412003-08-20 22:54:39 +000015#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
Daniel Veillard1dc9feb2008-11-17 15:59:21 +000030/* 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
Daniel Veillard1af9a412003-08-20 22:54:39 +000035/* #define DEBUG_SAX2 */
36/* #define DEBUG_SAX2_TREE */
37
38/**
Daniel Veillarde57ec792003-09-10 10:50:59 +000039 * 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
William M. Brack42331a92004-07-29 07:07:16 +000053/*
54 * xmlSAX2ErrMemory:
55 * @ctxt: an XML validation parser context
56 * @msg: a string to accompany the error message
57 */
58static void
William M. Bracka3215c72004-07-31 16:24:01 +000059xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
Daniel Veillard34099b42004-11-04 17:34:35 +000060 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 }
William M. Brack42331a92004-07-29 07:07:16 +000067}
68
Daniel Veillarde57ec792003-09-10 10:50:59 +000069/**
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000070 * 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;
Daniel Veillard34099b42004-11-04 17:34:35 +000088 if (ctxt != NULL) {
89 ctxt->errNo = error;
90 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
91 schannel = ctxt->sax->serror;
Daniel Veillard2728f842006-03-09 16:49:24 +000092 __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);
Daniel Veillard34099b42004-11-04 17:34:35 +000098 ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +000099 } 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 }
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000107}
108
109/**
Daniel Veillard87b30462005-07-05 14:04:36 +0000110 * 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/**
Daniel Veillard1af9a412003-08-20 22:54:39 +0000215 * 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;
Daniel Veillard24505b02005-07-28 23:49:35 +0000242 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000243 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;
Daniel Veillard34099b42004-11-04 17:34:35 +0000258 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000259 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;
Daniel Veillard34099b42004-11-04 17:34:35 +0000274 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000275 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;
Daniel Veillard34099b42004-11-04 17:34:35 +0000290 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000291 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;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000306 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000307 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;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000322 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000323 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;
Daniel Veillard34099b42004-11-04 17:34:35 +0000341 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000342#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);
William M. Brack42331a92004-07-29 07:07:16 +0000360 if (ctxt->myDoc->intSubset == NULL)
361 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000362}
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;
Daniel Veillard34099b42004-11-04 17:34:35 +0000378 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000379#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) {
William M. Brack42331a92004-07-29 07:07:16 +0000422 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000423 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
Daniel Veillard34099b42004-11-04 17:34:35 +0000499 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000500 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
Daniel Veillard34099b42004-11-04 17:34:35 +0000534 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000535#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) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000556 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
557 "Entity(%s) document marked standalone but requires external subset\n",
558 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000559 }
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 {
Daniel Veillard87b30462005-07-05 14:04:36 +0000583 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
584 "Failure to process entity %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000585 ctxt->validate = 0;
586 return(NULL);
587 }
588 ret->owner = 1;
Daniel Veillardf4f4e482008-08-25 08:57:48 +0000589 if (ret->checked == 0)
590 ret->checked = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000591 }
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
Daniel Veillard34099b42004-11-04 17:34:35 +0000610 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000611#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
Daniel Veillard34099b42004-11-04 17:34:35 +0000639 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000640#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);
Daniel Veillard87b30462005-07-05 14:04:36 +0000648 if ((ent == NULL) && (ctxt->pedantic))
649 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
650 "Entity(%s) already defined in the internal subset\n",
651 name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000652 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))
William M. Brack4811ba32003-09-06 18:02:53 +0000669 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000670 "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 {
Daniel Veillard87b30462005-07-05 14:04:36 +0000684 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
685 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
686 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000687 }
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
Daniel Veillard2728f842006-03-09 16:49:24 +0000711 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
712 return;
713
Daniel Veillard1af9a412003-08-20 22:54:39 +0000714#ifdef DEBUG_SAX
715 xmlGenericError(xmlGenericErrorContext,
716 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
717 elem, fullname, type, def, defaultValue);
718#endif
Daniel Veillard68cb4b22004-04-18 20:55:39 +0000719 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 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000729 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000730 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 {
Daniel Veillard87b30462005-07-05 14:04:36 +0000741 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
742 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
743 name, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000744 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000745 return;
746 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000747#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000748 if (ctxt->vctxt.valid == 0)
749 ctxt->valid = 0;
750 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
Daniel Veillard2728f842006-03-09 16:49:24 +0000751 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000752 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
753 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000754#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000755 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
Daniel Veillard2728f842006-03-09 16:49:24 +0000777 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
778 return;
779
Daniel Veillard1af9a412003-08-20 22:54:39 +0000780#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 {
Daniel Veillard87b30462005-07-05 14:04:36 +0000792 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
793 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
794 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000795 return;
796 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000797#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000798 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);
Daniel Veillard4432df22003-09-28 18:58:27 +0000804#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000805}
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
Daniel Veillard2728f842006-03-09 16:49:24 +0000823 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
824 return;
825
Daniel Veillard1af9a412003-08-20 22:54:39 +0000826#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)) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000832 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
833 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
834 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000835 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 {
Daniel Veillard87b30462005-07-05 14:04:36 +0000843 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
844 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
845 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000846 return;
847 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000848#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000849 if (nota == NULL) ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000850 if ((ctxt->validate) && (ctxt->wellFormed) &&
851 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000852 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
853 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000854#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000855}
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;
Daniel Veillard34099b42004-11-04 17:34:35 +0000874 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000875#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))
William M. Brack4811ba32003-09-06 18:02:53 +0000886 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000887 "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))
William M. Brack4811ba32003-09-06 18:02:53 +0000906 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000907 "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 {
Daniel Veillard87b30462005-07-05 14:04:36 +0000921 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
922 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
923 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000924 }
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
Daniel Veillard34099b42004-11-04 17:34:35 +0000957 if (ctx == NULL) return;
958
Daniel Veillard1af9a412003-08-20 22:54:39 +0000959#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);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000967 ctxt->myDoc->properties = XML_DOC_HTML;
968 ctxt->myDoc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000969 if (ctxt->myDoc == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000970 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000971 return;
972 }
973#else
974 xmlGenericError(xmlGenericErrorContext,
975 "libxml2 built without HTML support\n");
976 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
977 ctxt->instate = XML_PARSER_EOF;
978 ctxt->disableSAX = 1;
979 return;
980#endif
981 } else {
982 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
983 if (doc != NULL) {
Daniel Veillardae0765b2008-07-31 19:54:59 +0000984 doc->properties = 0;
985 if (ctxt->options & XML_PARSE_OLD10)
986 doc->properties |= XML_DOC_OLD10;
987 doc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000988 if (ctxt->encoding != NULL)
989 doc->encoding = xmlStrdup(ctxt->encoding);
990 else
991 doc->encoding = NULL;
992 doc->standalone = ctxt->standalone;
993 } else {
William M. Brack42331a92004-07-29 07:07:16 +0000994 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000995 return;
996 }
Daniel Veillard500a1de2004-03-22 15:22:58 +0000997 if ((ctxt->dictNames) && (doc != NULL)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000998 doc->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000999 xmlDictReference(doc->dict);
1000 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001001 }
1002 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1003 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillardb8efdda2006-10-10 12:37:14 +00001004 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001005 if (ctxt->myDoc->URL == NULL)
William M. Bracka3215c72004-07-31 16:24:01 +00001006 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001007 }
1008}
1009
1010/**
1011 * xmlSAX2EndDocument:
1012 * @ctx: the user data (XML parser context)
1013 *
1014 * called when the document end has been detected.
1015 */
1016void
1017xmlSAX2EndDocument(void *ctx)
1018{
1019 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1020#ifdef DEBUG_SAX
1021 xmlGenericError(xmlGenericErrorContext,
1022 "SAX.xmlSAX2EndDocument()\n");
1023#endif
Daniel Veillard34099b42004-11-04 17:34:35 +00001024 if (ctx == NULL) return;
Daniel Veillard4432df22003-09-28 18:58:27 +00001025#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001026 if (ctxt->validate && ctxt->wellFormed &&
1027 ctxt->myDoc && ctxt->myDoc->intSubset)
1028 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00001029#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001030
1031 /*
1032 * Grab the encoding if it was added on-the-fly
1033 */
1034 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1035 (ctxt->myDoc->encoding == NULL)) {
1036 ctxt->myDoc->encoding = ctxt->encoding;
1037 ctxt->encoding = NULL;
1038 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001039 if ((ctxt->inputTab != NULL) &&
1040 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1041 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001042 (ctxt->myDoc->encoding == NULL)) {
1043 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1044 }
1045 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1046 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1047 ctxt->myDoc->charset = ctxt->charset;
1048 }
1049}
1050
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001051#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001052/**
1053 * xmlSAX2AttributeInternal:
1054 * @ctx: the user data (XML parser context)
1055 * @fullname: The attribute name, including namespace prefix
1056 * @value: The attribute value
1057 * @prefix: the prefix on the element node
1058 *
1059 * Handle an attribute that has been read by the parser.
1060 * The default handling is to convert the attribute into an
1061 * DOM subtree and past it in a new xmlAttr element added to
1062 * the element.
1063 */
1064static void
1065xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001066 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001067{
1068 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1069 xmlAttrPtr ret;
1070 xmlChar *name;
1071 xmlChar *ns;
1072 xmlChar *nval;
1073 xmlNsPtr namespace;
1074
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001075 if (ctxt->html) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001076 name = xmlStrdup(fullname);
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001077 ns = NULL;
1078 namespace = NULL;
1079 } else {
1080 /*
1081 * Split the full name into a namespace prefix and the tag name
1082 */
1083 name = xmlSplitQName(ctxt, fullname, &ns);
1084 if ((name != NULL) && (name[0] == 0)) {
1085 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1086 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1087 "invalid namespace declaration '%s'\n",
1088 fullname, NULL);
1089 } else {
1090 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1091 "Avoid attribute ending with ':' like '%s'\n",
1092 fullname, NULL);
1093 }
1094 if (ns != NULL)
1095 xmlFree(ns);
1096 ns = NULL;
1097 xmlFree(name);
1098 name = xmlStrdup(fullname);
1099 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001100 }
1101 if (name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001102 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001103 if (ns != NULL)
1104 xmlFree(ns);
1105 return;
1106 }
1107
Daniel Veillard4432df22003-09-28 18:58:27 +00001108#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001109 /*
1110 * Do the last stage of the attribute normalization
1111 * Needed for HTML too:
1112 * http://www.w3.org/TR/html4/types.html#h-6.2
1113 */
1114 ctxt->vctxt.valid = 1;
1115 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1116 ctxt->myDoc, ctxt->node,
1117 fullname, value);
1118 if (ctxt->vctxt.valid != 1) {
1119 ctxt->valid = 0;
1120 }
1121 if (nval != NULL)
1122 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +00001123#else
1124 nval = NULL;
1125#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001126
1127 /*
1128 * Check whether it's a namespace definition
1129 */
1130 if ((!ctxt->html) && (ns == NULL) &&
1131 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1132 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1133 xmlNsPtr nsret;
1134 xmlChar *val;
1135
1136 if (!ctxt->replaceEntities) {
1137 ctxt->depth++;
1138 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1139 0,0,0);
1140 ctxt->depth--;
1141 } else {
1142 val = (xmlChar *) value;
1143 }
1144
1145 if (val[0] != 0) {
1146 xmlURIPtr uri;
1147
1148 uri = xmlParseURI((const char *)val);
1149 if (uri == NULL) {
1150 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1151 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +00001152 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001153 } else {
1154 if (uri->scheme == NULL) {
1155 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1156 ctxt->sax->warning(ctxt->userData,
1157 "xmlns: URI %s is not absolute\n", val);
1158 }
1159 xmlFreeURI(uri);
1160 }
1161 }
1162
1163 /* a default namespace definition */
1164 nsret = xmlNewNs(ctxt->node, val, NULL);
1165
Daniel Veillard4432df22003-09-28 18:58:27 +00001166#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001167 /*
1168 * Validate also for namespace decls, they are attributes from
1169 * an XML-1.0 perspective
1170 */
1171 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1172 ctxt->myDoc && ctxt->myDoc->intSubset)
1173 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1174 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +00001175#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001176 if (name != NULL)
1177 xmlFree(name);
1178 if (nval != NULL)
1179 xmlFree(nval);
1180 if (val != value)
1181 xmlFree(val);
1182 return;
1183 }
1184 if ((!ctxt->html) &&
1185 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1186 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1187 xmlNsPtr nsret;
1188 xmlChar *val;
1189
1190 if (!ctxt->replaceEntities) {
1191 ctxt->depth++;
1192 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1193 0,0,0);
1194 ctxt->depth--;
1195 if (val == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001196 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001197 xmlFree(ns);
1198 if (name != NULL)
1199 xmlFree(name);
1200 return;
1201 }
1202 } else {
1203 val = (xmlChar *) value;
1204 }
1205
1206 if (val[0] == 0) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001207 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1208 "Empty namespace name for prefix %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001209 }
1210 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1211 xmlURIPtr uri;
1212
1213 uri = xmlParseURI((const char *)val);
1214 if (uri == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001215 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001216 "xmlns:%s: %s not a valid URI\n", name, value);
1217 } else {
1218 if (uri->scheme == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001219 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001220 "xmlns:%s: URI %s is not absolute\n", name, value);
1221 }
1222 xmlFreeURI(uri);
1223 }
1224 }
1225
1226 /* a standard namespace definition */
1227 nsret = xmlNewNs(ctxt->node, val, name);
1228 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001229#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001230 /*
1231 * Validate also for namespace decls, they are attributes from
1232 * an XML-1.0 perspective
1233 */
1234 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1235 ctxt->myDoc && ctxt->myDoc->intSubset)
1236 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1237 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001238#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001239 if (name != NULL)
1240 xmlFree(name);
1241 if (nval != NULL)
1242 xmlFree(nval);
1243 if (val != value)
1244 xmlFree(val);
1245 return;
1246 }
1247
1248 if (ns != NULL) {
1249 xmlAttrPtr prop;
1250 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillard67906942003-08-28 21:13:25 +00001251 if (namespace == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001252 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
William M. Brack4811ba32003-09-06 18:02:53 +00001253 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001254 ns, name);
1255 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001256
1257 prop = ctxt->node->properties;
1258 while (prop != NULL) {
1259 if (prop->ns != NULL) {
1260 if ((xmlStrEqual(name, prop->name)) &&
1261 ((namespace == prop->ns) ||
1262 (xmlStrEqual(namespace->href, prop->ns->href)))) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001263 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001264 "Attribute %s in %s redefined\n",
1265 name, namespace->href);
1266 ctxt->wellFormed = 0;
1267 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1268 goto error;
1269 }
1270 }
1271 prop = prop->next;
1272 }
1273 } else {
1274 namespace = NULL;
1275 }
1276
1277 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1278 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1279
1280 if (ret != NULL) {
1281 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1282 xmlNodePtr tmp;
1283
1284 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1285 tmp = ret->children;
1286 while (tmp != NULL) {
1287 tmp->parent = (xmlNodePtr) ret;
1288 if (tmp->next == NULL)
1289 ret->last = tmp;
1290 tmp = tmp->next;
1291 }
1292 } else if (value != NULL) {
1293 ret->children = xmlNewDocText(ctxt->myDoc, value);
1294 ret->last = ret->children;
1295 if (ret->children != NULL)
1296 ret->children->parent = (xmlNodePtr) ret;
1297 }
1298 }
1299
Daniel Veillard4432df22003-09-28 18:58:27 +00001300#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001301 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1302 ctxt->myDoc && ctxt->myDoc->intSubset) {
1303
1304 /*
1305 * If we don't substitute entities, the validation should be
1306 * done on a value with replaced entities anyway.
1307 */
1308 if (!ctxt->replaceEntities) {
1309 xmlChar *val;
1310
1311 ctxt->depth++;
1312 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1313 0,0,0);
1314 ctxt->depth--;
1315
1316 if (val == NULL)
1317 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1318 ctxt->myDoc, ctxt->node, ret, value);
1319 else {
1320 xmlChar *nvalnorm;
1321
1322 /*
1323 * Do the last stage of the attribute normalization
1324 * It need to be done twice ... it's an extra burden related
1325 * to the ability to keep xmlSAX2References in attributes
1326 */
1327 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1328 ctxt->node, fullname, val);
1329 if (nvalnorm != NULL) {
1330 xmlFree(val);
1331 val = nvalnorm;
1332 }
1333
1334 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1335 ctxt->myDoc, ctxt->node, ret, val);
1336 xmlFree(val);
1337 }
1338 } else {
1339 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1340 ctxt->node, ret, value);
1341 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001342 } else
1343#endif /* LIBXML_VALID_ENABLED */
1344 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001345 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1346 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1347 /*
1348 * when validating, the ID registration is done at the attribute
1349 * validation level. Otherwise we have to do specific handling here.
1350 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001351 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001352 /*
1353 * Add the xml:id value
1354 *
1355 * Open issue: normalization of the value.
1356 */
Daniel Veillard68cb4b22004-04-18 20:55:39 +00001357 if (xmlValidateNCName(value, 1) != 0) {
1358 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1359 "xml:id : attribute value %s is not an NCName\n",
1360 (const char *) value, NULL);
1361 }
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001362 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001363 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1364 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1365 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1366 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001367 }
1368
1369error:
1370 if (nval != NULL)
1371 xmlFree(nval);
1372 if (ns != NULL)
1373 xmlFree(ns);
1374}
1375
Daniel Veillard1af9a412003-08-20 22:54:39 +00001376/*
1377 * xmlCheckDefaultedAttributes:
1378 *
1379 * Check defaulted attributes from the DTD
1380 */
1381static void
1382xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1383 const xmlChar *prefix, const xmlChar **atts) {
1384 xmlElementPtr elemDecl;
1385 const xmlChar *att;
1386 int internal = 1;
1387 int i;
1388
1389 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1390 if (elemDecl == NULL) {
1391 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1392 internal = 0;
1393 }
1394
1395process_external_subset:
1396
1397 if (elemDecl != NULL) {
1398 xmlAttributePtr attr = elemDecl->attributes;
1399 /*
1400 * Check against defaulted attributes from the external subset
1401 * if the document is stamped as standalone
1402 */
1403 if ((ctxt->myDoc->standalone == 1) &&
1404 (ctxt->myDoc->extSubset != NULL) &&
1405 (ctxt->validate)) {
1406 while (attr != NULL) {
1407 if ((attr->defaultValue != NULL) &&
1408 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1409 attr->elem, attr->name,
1410 attr->prefix) == attr) &&
1411 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1412 attr->elem, attr->name,
1413 attr->prefix) == NULL)) {
1414 xmlChar *fulln;
1415
1416 if (attr->prefix != NULL) {
1417 fulln = xmlStrdup(attr->prefix);
1418 fulln = xmlStrcat(fulln, BAD_CAST ":");
1419 fulln = xmlStrcat(fulln, attr->name);
1420 } else {
1421 fulln = xmlStrdup(attr->name);
1422 }
Jim Meyering669e88c2009-07-29 11:33:32 +02001423 if (fulln == NULL) {
1424 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1425 break;
1426 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001427
1428 /*
1429 * Check that the attribute is not declared in the
1430 * serialization
1431 */
1432 att = NULL;
1433 if (atts != NULL) {
1434 i = 0;
1435 att = atts[i];
1436 while (att != NULL) {
1437 if (xmlStrEqual(att, fulln))
1438 break;
1439 i += 2;
1440 att = atts[i];
1441 }
1442 }
1443 if (att == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001444 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001445 "standalone: attribute %s on %s defaulted from external subset\n",
Daniel Veillard427174f2003-12-10 10:42:59 +00001446 (const char *)fulln,
1447 (const char *)attr->elem);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001448 }
Daniel Veillard023d0ba2009-07-29 11:34:50 +02001449 xmlFree(fulln);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001450 }
1451 attr = attr->nexth;
1452 }
1453 }
1454
1455 /*
1456 * Actually insert defaulted values when needed
1457 */
1458 attr = elemDecl->attributes;
1459 while (attr != NULL) {
1460 /*
1461 * Make sure that attributes redefinition occuring in the
1462 * internal subset are not overriden by definitions in the
1463 * external subset.
1464 */
1465 if (attr->defaultValue != NULL) {
1466 /*
1467 * the element should be instantiated in the tree if:
1468 * - this is a namespace prefix
1469 * - the user required for completion in the tree
1470 * like XSLT
1471 * - there isn't already an attribute definition
1472 * in the internal subset overriding it.
1473 */
1474 if (((attr->prefix != NULL) &&
1475 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1476 ((attr->prefix == NULL) &&
1477 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1478 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1479 xmlAttributePtr tst;
1480
1481 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1482 attr->elem, attr->name,
1483 attr->prefix);
1484 if ((tst == attr) || (tst == NULL)) {
1485 xmlChar fn[50];
1486 xmlChar *fulln;
1487
1488 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1489 if (fulln == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001490 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001491 return;
1492 }
1493
1494 /*
1495 * Check that the attribute is not declared in the
1496 * serialization
1497 */
1498 att = NULL;
1499 if (atts != NULL) {
1500 i = 0;
1501 att = atts[i];
1502 while (att != NULL) {
1503 if (xmlStrEqual(att, fulln))
1504 break;
1505 i += 2;
1506 att = atts[i];
1507 }
1508 }
1509 if (att == NULL) {
1510 xmlSAX2AttributeInternal(ctxt, fulln,
1511 attr->defaultValue, prefix);
1512 }
1513 if ((fulln != fn) && (fulln != attr->name))
1514 xmlFree(fulln);
1515 }
1516 }
1517 }
1518 attr = attr->nexth;
1519 }
1520 if (internal == 1) {
1521 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1522 name, prefix);
1523 internal = 0;
1524 goto process_external_subset;
1525 }
1526 }
1527}
1528
1529/**
1530 * xmlSAX2StartElement:
1531 * @ctx: the user data (XML parser context)
1532 * @fullname: The element name, including namespace prefix
1533 * @atts: An array of name/value attributes pairs, NULL terminated
1534 *
1535 * called when an opening tag has been processed.
1536 */
1537void
1538xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1539{
1540 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1541 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001542 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001543 xmlNsPtr ns;
1544 xmlChar *name;
1545 xmlChar *prefix;
1546 const xmlChar *att;
1547 const xmlChar *value;
1548 int i;
1549
Daniel Veillarda521d282004-11-09 14:59:59 +00001550 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001551 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001552#ifdef DEBUG_SAX
1553 xmlGenericError(xmlGenericErrorContext,
1554 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1555#endif
1556
1557 /*
1558 * First check on validity:
1559 */
1560 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1561 ((ctxt->myDoc->intSubset == NULL) ||
1562 ((ctxt->myDoc->intSubset->notations == NULL) &&
1563 (ctxt->myDoc->intSubset->elements == NULL) &&
1564 (ctxt->myDoc->intSubset->attributes == NULL) &&
1565 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001566 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1567 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001568 ctxt->validate = 0;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001569 }
1570
1571
1572 /*
1573 * Split the full name into a namespace prefix and the tag name
1574 */
1575 name = xmlSplitQName(ctxt, fullname, &prefix);
1576
1577
1578 /*
1579 * Note : the namespace resolution is deferred until the end of the
1580 * attributes parsing, since local namespace can be defined as
1581 * an attribute at this level.
1582 */
1583 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1584 if (ret == NULL) {
1585 if (prefix != NULL)
1586 xmlFree(prefix);
William M. Brack42331a92004-07-29 07:07:16 +00001587 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001588 return;
1589 }
1590 if (ctxt->myDoc->children == NULL) {
1591#ifdef DEBUG_SAX_TREE
1592 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1593#endif
1594 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1595 } else if (parent == NULL) {
1596 parent = ctxt->myDoc->children;
1597 }
1598 ctxt->nodemem = -1;
1599 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001600 if (ctxt->input != NULL) {
1601 if (ctxt->input->line < 65535)
1602 ret->line = (short) ctxt->input->line;
1603 else
1604 ret->line = 65535;
1605 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001606 }
1607
1608 /*
1609 * We are parsing a new node.
1610 */
1611#ifdef DEBUG_SAX_TREE
1612 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1613#endif
1614 nodePush(ctxt, ret);
1615
1616 /*
1617 * Link the child element
1618 */
1619 if (parent != NULL) {
1620 if (parent->type == XML_ELEMENT_NODE) {
1621#ifdef DEBUG_SAX_TREE
1622 xmlGenericError(xmlGenericErrorContext,
1623 "adding child %s to %s\n", name, parent->name);
1624#endif
1625 xmlAddChild(parent, ret);
1626 } else {
1627#ifdef DEBUG_SAX_TREE
1628 xmlGenericError(xmlGenericErrorContext,
1629 "adding sibling %s to ", name);
1630 xmlDebugDumpOneNode(stderr, parent, 0);
1631#endif
1632 xmlAddSibling(parent, ret);
1633 }
1634 }
1635
1636 /*
1637 * Insert all the defaulted attributes from the DTD especially namespaces
1638 */
1639 if ((!ctxt->html) &&
1640 ((ctxt->myDoc->intSubset != NULL) ||
1641 (ctxt->myDoc->extSubset != NULL))) {
1642 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1643 }
1644
1645 /*
1646 * process all the attributes whose name start with "xmlns"
1647 */
1648 if (atts != NULL) {
1649 i = 0;
1650 att = atts[i++];
1651 value = atts[i++];
1652 if (!ctxt->html) {
1653 while ((att != NULL) && (value != NULL)) {
1654 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1655 (att[3] == 'n') && (att[4] == 's'))
1656 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1657
1658 att = atts[i++];
1659 value = atts[i++];
1660 }
1661 }
1662 }
1663
1664 /*
1665 * Search the namespace, note that since the attributes have been
1666 * processed, the local namespaces are available.
1667 */
1668 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1669 if ((ns == NULL) && (parent != NULL))
1670 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1671 if ((prefix != NULL) && (ns == NULL)) {
1672 ns = xmlNewNs(ret, NULL, prefix);
Daniel Veillard77aad342006-07-13 06:21:09 +00001673 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1674 "Namespace prefix %s is not defined\n",
1675 prefix, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001676 }
1677
1678 /*
1679 * set the namespace node, making sure that if the default namspace
1680 * is unbound on a parent we simply kee it NULL
1681 */
1682 if ((ns != NULL) && (ns->href != NULL) &&
1683 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1684 xmlSetNs(ret, ns);
1685
1686 /*
1687 * process all the other attributes
1688 */
1689 if (atts != NULL) {
1690 i = 0;
1691 att = atts[i++];
1692 value = atts[i++];
1693 if (ctxt->html) {
1694 while (att != NULL) {
1695 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1696 att = atts[i++];
1697 value = atts[i++];
1698 }
1699 } else {
1700 while ((att != NULL) && (value != NULL)) {
1701 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1702 (att[3] != 'n') || (att[4] != 's'))
1703 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1704
1705 /*
1706 * Next ones
1707 */
1708 att = atts[i++];
1709 value = atts[i++];
1710 }
1711 }
1712 }
1713
Daniel Veillard4432df22003-09-28 18:58:27 +00001714#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001715 /*
1716 * If it's the Document root, finish the DTD validation and
1717 * check the document root element for validity
1718 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00001719 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001720 int chk;
1721
1722 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1723 if (chk <= 0)
1724 ctxt->valid = 0;
1725 if (chk < 0)
1726 ctxt->wellFormed = 0;
1727 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00001728 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001729 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001730#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001731
1732 if (prefix != NULL)
1733 xmlFree(prefix);
1734
1735}
1736
1737/**
1738 * xmlSAX2EndElement:
1739 * @ctx: the user data (XML parser context)
1740 * @name: The element name
1741 *
1742 * called when the end of an element has been detected.
1743 */
1744void
1745xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1746{
1747 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1748 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001749 xmlNodePtr cur;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001750
Daniel Veillard34099b42004-11-04 17:34:35 +00001751 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001752 cur = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001753#ifdef DEBUG_SAX
1754 if (name == NULL)
1755 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1756 else
1757 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1758#endif
1759
1760 /* Capture end position and add node */
1761 if (cur != NULL && ctxt->record_info) {
1762 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1763 node_info.end_line = ctxt->input->line;
1764 node_info.node = cur;
1765 xmlParserAddNodeInfo(ctxt, &node_info);
1766 }
1767 ctxt->nodemem = -1;
1768
Daniel Veillard4432df22003-09-28 18:58:27 +00001769#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001770 if (ctxt->validate && ctxt->wellFormed &&
1771 ctxt->myDoc && ctxt->myDoc->intSubset)
1772 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1773 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001774#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001775
1776
1777 /*
1778 * end of parsing of this node.
1779 */
1780#ifdef DEBUG_SAX_TREE
1781 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1782#endif
1783 nodePop(ctxt);
1784}
Daniel Veillard81273902003-09-30 00:43:48 +00001785#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001786
Daniel Veillarde57ec792003-09-10 10:50:59 +00001787/*
Daniel Veillard19895052003-09-17 13:59:32 +00001788 * xmlSAX2TextNode:
1789 * @ctxt: the parser context
1790 * @str: the input string
1791 * @len: the string length
1792 *
1793 * Remove the entities from an attribute value
1794 *
1795 * Returns the newly allocated string or NULL if not needed or error
1796 */
1797static xmlNodePtr
1798xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1799 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001800 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001801
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001802 /*
1803 * Allocate
1804 */
Daniel Veillard19895052003-09-17 13:59:32 +00001805 if (ctxt->freeElems != NULL) {
1806 ret = ctxt->freeElems;
1807 ctxt->freeElems = ret->next;
1808 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001809 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001810 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001811 }
1812 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001813 xmlErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard19895052003-09-17 13:59:32 +00001814 return(NULL);
1815 }
Daniel Veillard8874b942005-08-25 13:19:21 +00001816 memset(ret, 0, sizeof(xmlNode));
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001817 /*
1818 * intern the formatting blanks found between tags, or the
1819 * very short strings
1820 */
1821 if (ctxt->dictNames) {
1822 xmlChar cur = str[len];
1823
Daniel Veillard8874b942005-08-25 13:19:21 +00001824 if ((len < (int) (2 * sizeof(void *))) &&
1825 (ctxt->options & XML_PARSE_COMPACT)) {
1826 /* store the string in the node overrithing properties and nsDef */
1827 xmlChar *tmp = (xmlChar *) &(ret->properties);
1828 memcpy(tmp, str, len);
1829 tmp[len] = 0;
1830 intern = tmp;
1831 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001832 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001833 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001834 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001835 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001836 int i;
1837
1838 for (i = 1;i < len;i++) {
Daniel Veillard1a9b7082004-01-02 10:42:01 +00001839 if (!IS_BLANK_CH(str[i])) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001840 }
1841 intern = xmlDictLookup(ctxt->dict, str, len);
1842 }
1843 }
1844skip:
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001845 ret->type = XML_TEXT_NODE;
1846
1847 ret->name = xmlStringText;
William M. Brack9f797ab2004-07-28 07:40:12 +00001848 if (intern == NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001849 ret->content = xmlStrndup(str, len);
William M. Brack9f797ab2004-07-28 07:40:12 +00001850 if (ret->content == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001851 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1852 xmlFree(ret);
William M. Brack9f797ab2004-07-28 07:40:12 +00001853 return(NULL);
1854 }
1855 } else
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001856 ret->content = (xmlChar *) intern;
1857
Daniel Veillard45efd082008-07-07 13:52:52 +00001858 if (ctxt->input != NULL)
1859 ret->line = ctxt->input->line;
1860
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001861 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1862 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001863 return(ret);
1864}
1865
Daniel Veillard4432df22003-09-28 18:58:27 +00001866#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001867/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001868 * xmlSAX2DecodeAttrEntities:
1869 * @ctxt: the parser context
1870 * @str: the input string
1871 * @len: the string length
1872 *
1873 * Remove the entities from an attribute value
1874 *
1875 * Returns the newly allocated string or NULL if not needed or error
1876 */
1877static xmlChar *
1878xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1879 const xmlChar *end) {
1880 const xmlChar *in;
1881 xmlChar *ret;
1882
1883 in = str;
1884 while (in < end)
1885 if (*in++ == '&')
1886 goto decode;
1887 return(NULL);
1888decode:
1889 ctxt->depth++;
1890 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1891 XML_SUBSTITUTE_REF, 0,0,0);
1892 ctxt->depth--;
1893 return(ret);
1894}
Daniel Veillard4432df22003-09-28 18:58:27 +00001895#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001896
1897/**
1898 * xmlSAX2AttributeNs:
1899 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001900 * @localname: the local name of the attribute
1901 * @prefix: the attribute namespace prefix if available
1902 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001903 * @value: Start of the attribute value
1904 * @valueend: end of the attribute value
1905 *
1906 * Handle an attribute that has been read by the parser.
1907 * The default handling is to convert the attribute into an
1908 * DOM subtree and past it in a new xmlAttr element added to
1909 * the element.
1910 */
1911static void
1912xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1913 const xmlChar * localname,
1914 const xmlChar * prefix,
1915 const xmlChar * value,
1916 const xmlChar * valueend)
1917{
1918 xmlAttrPtr ret;
1919 xmlNsPtr namespace = NULL;
1920 xmlChar *dup = NULL;
1921
Daniel Veillarde57ec792003-09-10 10:50:59 +00001922 /*
1923 * Note: if prefix == NULL, the attribute is not in the default namespace
1924 */
1925 if (prefix != NULL)
1926 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1927
Daniel Veillard8a44e592003-09-15 14:50:06 +00001928 /*
1929 * allocate the node
1930 */
1931 if (ctxt->freeAttrs != NULL) {
1932 ret = ctxt->freeAttrs;
1933 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001934 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001935 memset(ret, 0, sizeof(xmlAttr));
1936 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001937
Daniel Veillard8a44e592003-09-15 14:50:06 +00001938 ret->parent = ctxt->node;
1939 ret->doc = ctxt->myDoc;
1940 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001941
Daniel Veillard8a44e592003-09-15 14:50:06 +00001942 if (ctxt->dictNames)
1943 ret->name = localname;
1944 else
1945 ret->name = xmlStrdup(localname);
1946
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001947 /* link at the end to preserv order, TODO speed up with a last */
1948 if (ctxt->node->properties == NULL) {
1949 ctxt->node->properties = ret;
1950 } else {
1951 xmlAttrPtr prev = ctxt->node->properties;
1952
1953 while (prev->next != NULL) prev = prev->next;
1954 prev->next = ret;
1955 ret->prev = prev;
1956 }
1957
Daniel Veillard8a44e592003-09-15 14:50:06 +00001958 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1959 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1960 } else {
1961 if (ctxt->dictNames)
1962 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1963 (xmlChar *) localname, NULL);
1964 else
1965 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1966 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001967 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00001968 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001969 }
1970 }
1971
Daniel Veillard8a44e592003-09-15 14:50:06 +00001972 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1973 xmlNodePtr tmp;
1974
Daniel Veillard19895052003-09-17 13:59:32 +00001975 /*
1976 * We know that if there is an entity reference, then
1977 * the string has been dup'ed and terminates with 0
1978 * otherwise with ' or "
1979 */
1980 if (*valueend != 0) {
1981 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1982 ret->children = tmp;
1983 ret->last = tmp;
1984 if (tmp != NULL) {
1985 tmp->doc = ret->doc;
1986 tmp->parent = (xmlNodePtr) ret;
1987 }
1988 } else {
1989 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1990 valueend - value);
1991 tmp = ret->children;
1992 while (tmp != NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001993 tmp->doc = ret->doc;
Daniel Veillard19895052003-09-17 13:59:32 +00001994 tmp->parent = (xmlNodePtr) ret;
1995 if (tmp->next == NULL)
1996 ret->last = tmp;
1997 tmp = tmp->next;
1998 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001999 }
2000 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002001 xmlNodePtr tmp;
2002
2003 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2004 ret->children = tmp;
2005 ret->last = tmp;
2006 if (tmp != NULL) {
2007 tmp->doc = ret->doc;
2008 tmp->parent = (xmlNodePtr) ret;
2009 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002010 }
2011
Daniel Veillard4432df22003-09-28 18:58:27 +00002012#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002013 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2014 ctxt->myDoc && ctxt->myDoc->intSubset) {
2015 /*
2016 * If we don't substitute entities, the validation should be
2017 * done on a value with replaced entities anyway.
2018 */
2019 if (!ctxt->replaceEntities) {
2020 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2021 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00002022 if (*valueend == 0) {
2023 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2024 ctxt->myDoc, ctxt->node, ret, value);
2025 } else {
2026 /*
2027 * That should already be normalized.
2028 * cheaper to finally allocate here than duplicate
2029 * entry points in the full validation code
2030 */
2031 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002032
Daniel Veillard62998c02003-09-15 12:56:36 +00002033 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2034 ctxt->myDoc, ctxt->node, ret, dup);
2035 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002036 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00002037 /*
2038 * dup now contains a string of the flattened attribute
2039 * content with entities substitued. Check if we need to
2040 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002041 * It need to be done twice ... it's an extra burden related
2042 * to the ability to keep references in attributes
2043 */
Daniel Veillard62998c02003-09-15 12:56:36 +00002044 if (ctxt->attsSpecial != NULL) {
2045 xmlChar *nvalnorm;
2046 xmlChar fn[50];
2047 xmlChar *fullname;
2048
2049 fullname = xmlBuildQName(localname, prefix, fn, 50);
2050 if (fullname != NULL) {
2051 ctxt->vctxt.valid = 1;
2052 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2053 &ctxt->vctxt, ctxt->myDoc,
2054 ctxt->node, fullname, dup);
2055 if (ctxt->vctxt.valid != 1)
2056 ctxt->valid = 0;
2057
2058 if ((fullname != fn) && (fullname != localname))
2059 xmlFree(fullname);
2060 if (nvalnorm != NULL) {
2061 xmlFree(dup);
2062 dup = nvalnorm;
2063 }
2064 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002065 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002066
2067 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2068 ctxt->myDoc, ctxt->node, ret, dup);
2069 }
2070 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002071 /*
2072 * if entities already have been substitued, then
2073 * the attribute as passed is already normalized
2074 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002075 dup = xmlStrndup(value, valueend - value);
2076
2077 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2078 ctxt->myDoc, ctxt->node, ret, dup);
2079 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002080 } else
2081#endif /* LIBXML_VALID_ENABLED */
2082 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002083 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2084 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2085 /*
2086 * when validating, the ID registration is done at the attribute
2087 * validation level. Otherwise we have to do specific handling here.
2088 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002089 if ((prefix == ctxt->str_xml) &&
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002090 (localname[0] == 'i') && (localname[1] == 'd') &&
2091 (localname[2] == 0)) {
2092 /*
2093 * Add the xml:id value
2094 *
2095 * Open issue: normalization of the value.
2096 */
2097 if (dup == NULL)
2098 dup = xmlStrndup(value, valueend - value);
William M. Brack5ef2f812004-05-23 23:56:47 +00002099#ifdef LIBXML_VALID_ENABLED
Daniel Veillard68cb4b22004-04-18 20:55:39 +00002100 if (xmlValidateNCName(dup, 1) != 0) {
2101 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2102 "xml:id : attribute value %s is not an NCName\n",
2103 (const char *) dup, NULL);
2104 }
William M. Brack3f147372004-05-22 01:09:26 +00002105#endif
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002106 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002107 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2108 /* might be worth duplicate entry points and not copy */
2109 if (dup == NULL)
2110 dup = xmlStrndup(value, valueend - value);
2111 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2112 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2113 if (dup == NULL)
2114 dup = xmlStrndup(value, valueend - value);
2115 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002116 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002117 }
2118 if (dup != NULL)
2119 xmlFree(dup);
2120}
2121
2122/**
2123 * xmlSAX2StartElementNs:
2124 * @ctx: the user data (XML parser context)
2125 * @localname: the local name of the element
2126 * @prefix: the element namespace prefix if available
2127 * @URI: the element namespace name if available
2128 * @nb_namespaces: number of namespace definitions on that node
2129 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2130 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002131 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002132 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2133 * attribute values.
2134 *
2135 * SAX2 callback when an element start has been detected by the parser.
2136 * It provides the namespace informations for the element, as well as
2137 * the new namespace declarations on the element.
2138 */
2139void
2140xmlSAX2StartElementNs(void *ctx,
2141 const xmlChar *localname,
2142 const xmlChar *prefix,
2143 const xmlChar *URI,
2144 int nb_namespaces,
2145 const xmlChar **namespaces,
2146 int nb_attributes,
2147 int nb_defaulted,
2148 const xmlChar **attributes)
2149{
2150 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2151 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002152 xmlNodePtr parent;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002153 xmlNsPtr last = NULL, ns;
2154 const xmlChar *uri, *pref;
2155 int i, j;
2156
Daniel Veillard34099b42004-11-04 17:34:35 +00002157 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002158 parent = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002159 /*
2160 * First check on validity:
2161 */
2162 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2163 ((ctxt->myDoc->intSubset == NULL) ||
2164 ((ctxt->myDoc->intSubset->notations == NULL) &&
2165 (ctxt->myDoc->intSubset->elements == NULL) &&
2166 (ctxt->myDoc->intSubset->attributes == NULL) &&
2167 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00002168 xmlErrValid(ctxt, XML_ERR_NO_DTD,
2169 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002170 ctxt->validate = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002171 }
2172
Daniel Veillard8a44e592003-09-15 14:50:06 +00002173 /*
2174 * allocate the node
2175 */
2176 if (ctxt->freeElems != NULL) {
2177 ret = ctxt->freeElems;
2178 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00002179 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00002180 memset(ret, 0, sizeof(xmlNode));
2181 ret->type = XML_ELEMENT_NODE;
2182
2183 if (ctxt->dictNames)
2184 ret->name = localname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002185 else {
Daniel Veillard8a44e592003-09-15 14:50:06 +00002186 ret->name = xmlStrdup(localname);
William M. Brack9f797ab2004-07-28 07:40:12 +00002187 if (ret->name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002188 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002189 return;
2190 }
2191 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002192 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2193 xmlRegisterNodeDefaultValue(ret);
2194 } else {
2195 if (ctxt->dictNames)
2196 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2197 (xmlChar *) localname, NULL);
2198 else
2199 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2200 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002201 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002202 return;
2203 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002204 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002205 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00002206 if (ctxt->input != NULL) {
2207 if (ctxt->input->line < 65535)
2208 ret->line = (short) ctxt->input->line;
2209 else
2210 ret->line = 65535;
2211 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002212 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002213
Daniel Veillard29b17482004-08-16 00:39:03 +00002214 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002215 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002216 }
2217 /*
2218 * Build the namespace list
2219 */
2220 for (i = 0,j = 0;j < nb_namespaces;j++) {
2221 pref = namespaces[i++];
2222 uri = namespaces[i++];
2223 ns = xmlNewNs(NULL, uri, pref);
2224 if (ns != NULL) {
2225 if (last == NULL) {
2226 ret->nsDef = last = ns;
2227 } else {
2228 last->next = ns;
2229 last = ns;
2230 }
2231 if ((URI != NULL) && (prefix == pref))
2232 ret->ns = ns;
2233 } else {
William M. Brack42331a92004-07-29 07:07:16 +00002234 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillarde57ec792003-09-10 10:50:59 +00002235 return;
2236 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002237#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002238 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2239 ctxt->myDoc && ctxt->myDoc->intSubset) {
2240 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2241 ret, prefix, ns, uri);
2242 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002243#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002244 }
2245 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002246
2247 /*
2248 * We are parsing a new node.
2249 */
2250 nodePush(ctxt, ret);
2251
2252 /*
2253 * Link the child element
2254 */
2255 if (parent != NULL) {
2256 if (parent->type == XML_ELEMENT_NODE) {
2257 xmlAddChild(parent, ret);
2258 } else {
2259 xmlAddSibling(parent, ret);
2260 }
2261 }
2262
2263 /*
2264 * Insert the defaulted attributes from the DTD only if requested:
2265 */
2266 if ((nb_defaulted != 0) &&
2267 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2268 nb_attributes -= nb_defaulted;
2269
2270 /*
2271 * Search the namespace if it wasn't already found
William M. Brackbf5cf212004-08-31 06:47:17 +00002272 * Note that, if prefix is NULL, this searches for the default Ns
Daniel Veillarde57ec792003-09-10 10:50:59 +00002273 */
2274 if ((URI != NULL) && (ret->ns == NULL)) {
William M. Brackbf5cf212004-08-31 06:47:17 +00002275 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002276 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2277 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2278 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002279 if (ret->ns == NULL) {
2280 ns = xmlNewNs(ret, NULL, prefix);
William M. Brack9f797ab2004-07-28 07:40:12 +00002281 if (ns == NULL) {
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002282
William M. Brack42331a92004-07-29 07:07:16 +00002283 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002284 return;
2285 }
Daniel Veillard77aad342006-07-13 06:21:09 +00002286 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2287 "Namespace prefix %s was not found\n",
2288 prefix, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002289 }
2290 }
2291
2292 /*
2293 * process all the other attributes
2294 */
2295 if (nb_attributes > 0) {
2296 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2297 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2298 attributes[j+3], attributes[j+4]);
2299 }
2300 }
2301
Daniel Veillard4432df22003-09-28 18:58:27 +00002302#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002303 /*
2304 * If it's the Document root, finish the DTD validation and
2305 * check the document root element for validity
2306 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00002307 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002308 int chk;
2309
2310 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2311 if (chk <= 0)
2312 ctxt->valid = 0;
2313 if (chk < 0)
2314 ctxt->wellFormed = 0;
2315 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00002316 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002317 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002318#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002319}
2320
2321/**
2322 * xmlSAX2EndElementNs:
2323 * @ctx: the user data (XML parser context)
2324 * @localname: the local name of the element
2325 * @prefix: the element namespace prefix if available
2326 * @URI: the element namespace name if available
2327 *
2328 * SAX2 callback when an element end has been detected by the parser.
2329 * It provides the namespace informations for the element.
2330 */
2331void
2332xmlSAX2EndElementNs(void *ctx,
2333 const xmlChar * localname ATTRIBUTE_UNUSED,
2334 const xmlChar * prefix ATTRIBUTE_UNUSED,
2335 const xmlChar * URI ATTRIBUTE_UNUSED)
2336{
2337 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2338 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002339 xmlNodePtr cur;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002340
Daniel Veillard34099b42004-11-04 17:34:35 +00002341 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002342 cur = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002343 /* Capture end position and add node */
2344 if ((ctxt->record_info) && (cur != NULL)) {
2345 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2346 node_info.end_line = ctxt->input->line;
2347 node_info.node = cur;
2348 xmlParserAddNodeInfo(ctxt, &node_info);
2349 }
2350 ctxt->nodemem = -1;
2351
Daniel Veillard4432df22003-09-28 18:58:27 +00002352#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002353 if (ctxt->validate && ctxt->wellFormed &&
2354 ctxt->myDoc && ctxt->myDoc->intSubset)
2355 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002356#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002357
2358 /*
2359 * end of parsing of this node.
2360 */
2361 nodePop(ctxt);
2362}
2363
Daniel Veillard1af9a412003-08-20 22:54:39 +00002364/**
2365 * xmlSAX2Reference:
2366 * @ctx: the user data (XML parser context)
2367 * @name: The entity name
2368 *
2369 * called when an entity xmlSAX2Reference is detected.
2370 */
2371void
2372xmlSAX2Reference(void *ctx, const xmlChar *name)
2373{
2374 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2375 xmlNodePtr ret;
2376
Daniel Veillard34099b42004-11-04 17:34:35 +00002377 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002378#ifdef DEBUG_SAX
2379 xmlGenericError(xmlGenericErrorContext,
2380 "SAX.xmlSAX2Reference(%s)\n", name);
2381#endif
2382 if (name[0] == '#')
2383 ret = xmlNewCharRef(ctxt->myDoc, name);
2384 else
2385 ret = xmlNewReference(ctxt->myDoc, name);
2386#ifdef DEBUG_SAX_TREE
2387 xmlGenericError(xmlGenericErrorContext,
2388 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2389#endif
Daniel Veillardb242b082008-02-08 09:56:31 +00002390 if (xmlAddChild(ctxt->node, ret) == NULL) {
2391 xmlFreeNode(ret);
2392 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002393}
2394
2395/**
2396 * xmlSAX2Characters:
2397 * @ctx: the user data (XML parser context)
2398 * @ch: a xmlChar string
2399 * @len: the number of xmlChar
2400 *
2401 * receiving some chars from the parser.
2402 */
2403void
2404xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2405{
2406 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2407 xmlNodePtr lastChild;
2408
Daniel Veillard34099b42004-11-04 17:34:35 +00002409 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002410#ifdef DEBUG_SAX
2411 xmlGenericError(xmlGenericErrorContext,
2412 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2413#endif
2414 /*
2415 * Handle the data if any. If there is no child
2416 * add it as content, otherwise if the last child is text,
2417 * concatenate it, else create a new node of type text.
2418 */
2419
2420 if (ctxt->node == NULL) {
2421#ifdef DEBUG_SAX_TREE
2422 xmlGenericError(xmlGenericErrorContext,
2423 "add chars: ctxt->node == NULL !\n");
2424#endif
2425 return;
2426 }
Daniel Veillard19895052003-09-17 13:59:32 +00002427 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002428#ifdef DEBUG_SAX_TREE
2429 xmlGenericError(xmlGenericErrorContext,
2430 "add chars to %s \n", ctxt->node->name);
2431#endif
2432
2433 /*
2434 * Here we needed an accelerator mechanism in case of very large
2435 * elements. Use an attribute in the structure !!!
2436 */
2437 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002438 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2439 if (lastChild != NULL) {
2440 ctxt->node->children = lastChild;
2441 ctxt->node->last = lastChild;
2442 lastChild->parent = ctxt->node;
2443 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002444 ctxt->nodelen = len;
2445 ctxt->nodemem = len + 1;
William M. Bracka3215c72004-07-31 16:24:01 +00002446 } else {
2447 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2448 return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002449 }
2450 } else {
2451 int coalesceText = (lastChild != NULL) &&
2452 (lastChild->type == XML_TEXT_NODE) &&
2453 (lastChild->name == xmlStringText);
2454 if ((coalesceText) && (ctxt->nodemem != 0)) {
2455 /*
2456 * The whole point of maintaining nodelen and nodemem,
2457 * xmlTextConcat is too costly, i.e. compute length,
2458 * reallocate a new buffer, move data, append ch. Here
2459 * We try to minimaze realloc() uses and avoid copying
2460 * and recomputing length over and over.
2461 */
Daniel Veillard8874b942005-08-25 13:19:21 +00002462 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2463 lastChild->content = xmlStrdup(lastChild->content);
2464 lastChild->properties = NULL;
2465 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2466 (xmlDictOwns(ctxt->dict, lastChild->content))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002467 lastChild->content = xmlStrdup(lastChild->content);
2468 }
Daniel Veillard97ff9b32009-01-18 21:43:30 +00002469 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002470 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2471 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2472 return;
2473 }
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002474 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
2475 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002476 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2477 return;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002478 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002479 if (ctxt->nodelen + len >= ctxt->nodemem) {
2480 xmlChar *newbuf;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002481 size_t size;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002482
2483 size = ctxt->nodemem + len;
2484 size *= 2;
2485 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2486 if (newbuf == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002487 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002488 return;
2489 }
2490 ctxt->nodemem = size;
2491 lastChild->content = newbuf;
2492 }
2493 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2494 ctxt->nodelen += len;
2495 lastChild->content[ctxt->nodelen] = 0;
2496 } else if (coalesceText) {
2497 if (xmlTextConcat(lastChild, ch, len)) {
William M. Brack42331a92004-07-29 07:07:16 +00002498 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002499 }
2500 if (ctxt->node->children != NULL) {
2501 ctxt->nodelen = xmlStrlen(lastChild->content);
2502 ctxt->nodemem = ctxt->nodelen + 1;
2503 }
2504 } else {
2505 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002506 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2507 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002508 xmlAddChild(ctxt->node, lastChild);
2509 if (ctxt->node->children != NULL) {
2510 ctxt->nodelen = len;
2511 ctxt->nodemem = len + 1;
2512 }
2513 }
2514 }
2515 }
2516}
2517
2518/**
2519 * xmlSAX2IgnorableWhitespace:
2520 * @ctx: the user data (XML parser context)
2521 * @ch: a xmlChar string
2522 * @len: the number of xmlChar
2523 *
2524 * receiving some ignorable whitespaces from the parser.
2525 * UNUSED: by default the DOM building will use xmlSAX2Characters
2526 */
2527void
2528xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2529{
2530 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2531#ifdef DEBUG_SAX
2532 xmlGenericError(xmlGenericErrorContext,
2533 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2534#endif
2535}
2536
2537/**
2538 * xmlSAX2ProcessingInstruction:
2539 * @ctx: the user data (XML parser context)
2540 * @target: the target name
2541 * @data: the PI data's
2542 *
2543 * A processing instruction has been parsed.
2544 */
2545void
2546xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2547 const xmlChar *data)
2548{
2549 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2550 xmlNodePtr ret;
Daniel Veillard6128c012004-11-08 17:16:15 +00002551 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002552
Daniel Veillard34099b42004-11-04 17:34:35 +00002553 if (ctx == NULL) return;
Daniel Veillard6128c012004-11-08 17:16:15 +00002554 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002555#ifdef DEBUG_SAX
2556 xmlGenericError(xmlGenericErrorContext,
2557 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2558#endif
2559
Daniel Veillard03a53c32004-10-26 16:06:51 +00002560 ret = xmlNewDocPI(ctxt->myDoc, target, data);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002561 if (ret == NULL) return;
2562 parent = ctxt->node;
2563
Daniel Veillard73da77e2005-08-24 14:05:37 +00002564 if (ctxt->linenumbers) {
2565 if (ctxt->input != NULL) {
2566 if (ctxt->input->line < 65535)
2567 ret->line = (short) ctxt->input->line;
2568 else
2569 ret->line = 65535;
2570 }
2571 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002572 if (ctxt->inSubset == 1) {
2573 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2574 return;
2575 } else if (ctxt->inSubset == 2) {
2576 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2577 return;
2578 }
2579 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2580#ifdef DEBUG_SAX_TREE
2581 xmlGenericError(xmlGenericErrorContext,
2582 "Setting PI %s as root\n", target);
2583#endif
2584 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2585 return;
2586 }
2587 if (parent->type == XML_ELEMENT_NODE) {
2588#ifdef DEBUG_SAX_TREE
2589 xmlGenericError(xmlGenericErrorContext,
2590 "adding PI %s child to %s\n", target, parent->name);
2591#endif
2592 xmlAddChild(parent, ret);
2593 } else {
2594#ifdef DEBUG_SAX_TREE
2595 xmlGenericError(xmlGenericErrorContext,
2596 "adding PI %s sibling to ", target);
2597 xmlDebugDumpOneNode(stderr, parent, 0);
2598#endif
2599 xmlAddSibling(parent, ret);
2600 }
2601}
2602
2603/**
2604 * xmlSAX2Comment:
2605 * @ctx: the user data (XML parser context)
2606 * @value: the xmlSAX2Comment content
2607 *
2608 * A xmlSAX2Comment has been parsed.
2609 */
2610void
2611xmlSAX2Comment(void *ctx, const xmlChar *value)
2612{
2613 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2614 xmlNodePtr ret;
Daniel Veillard34099b42004-11-04 17:34:35 +00002615 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002616
Daniel Veillard34099b42004-11-04 17:34:35 +00002617 if (ctx == NULL) return;
2618 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002619#ifdef DEBUG_SAX
2620 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2621#endif
2622 ret = xmlNewDocComment(ctxt->myDoc, value);
2623 if (ret == NULL) return;
Daniel Veillard73da77e2005-08-24 14:05:37 +00002624 if (ctxt->linenumbers) {
2625 if (ctxt->input != NULL) {
2626 if (ctxt->input->line < 65535)
2627 ret->line = (short) ctxt->input->line;
2628 else
2629 ret->line = 65535;
2630 }
2631 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002632
2633 if (ctxt->inSubset == 1) {
2634 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2635 return;
2636 } else if (ctxt->inSubset == 2) {
2637 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2638 return;
2639 }
2640 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2641#ifdef DEBUG_SAX_TREE
2642 xmlGenericError(xmlGenericErrorContext,
2643 "Setting xmlSAX2Comment as root\n");
2644#endif
2645 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2646 return;
2647 }
2648 if (parent->type == XML_ELEMENT_NODE) {
2649#ifdef DEBUG_SAX_TREE
2650 xmlGenericError(xmlGenericErrorContext,
2651 "adding xmlSAX2Comment child to %s\n", parent->name);
2652#endif
2653 xmlAddChild(parent, ret);
2654 } else {
2655#ifdef DEBUG_SAX_TREE
2656 xmlGenericError(xmlGenericErrorContext,
2657 "adding xmlSAX2Comment sibling to ");
2658 xmlDebugDumpOneNode(stderr, parent, 0);
2659#endif
2660 xmlAddSibling(parent, ret);
2661 }
2662}
2663
2664/**
2665 * xmlSAX2CDataBlock:
2666 * @ctx: the user data (XML parser context)
2667 * @value: The pcdata content
2668 * @len: the block length
2669 *
2670 * called when a pcdata block has been parsed
2671 */
2672void
2673xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2674{
2675 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2676 xmlNodePtr ret, lastChild;
2677
Daniel Veillard34099b42004-11-04 17:34:35 +00002678 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002679#ifdef DEBUG_SAX
2680 xmlGenericError(xmlGenericErrorContext,
2681 "SAX.pcdata(%.10s, %d)\n", value, len);
2682#endif
2683 lastChild = xmlGetLastChild(ctxt->node);
2684#ifdef DEBUG_SAX_TREE
2685 xmlGenericError(xmlGenericErrorContext,
2686 "add chars to %s \n", ctxt->node->name);
2687#endif
2688 if ((lastChild != NULL) &&
2689 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2690 xmlTextConcat(lastChild, value, len);
2691 } else {
2692 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2693 xmlAddChild(ctxt->node, ret);
2694 }
2695}
2696
Daniel Veillard62998c02003-09-15 12:56:36 +00002697static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002698
Daniel Veillard81273902003-09-30 00:43:48 +00002699#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002700/**
2701 * xmlSAXDefaultVersion:
2702 * @version: the version, 1 or 2
2703 *
2704 * Set the default version of SAX used globally by the library.
William M. Brack96d2eff2004-06-30 11:48:47 +00002705 * By default, during initialization the default is set to 2.
2706 * Note that it is generally a better coding style to use
2707 * xmlSAXVersion() to set up the version explicitly for a given
2708 * parsing context.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002709 *
2710 * Returns the previous value in case of success and -1 in case of error.
2711 */
2712int
2713xmlSAXDefaultVersion(int version)
2714{
2715 int ret = xmlSAX2DefaultVersionValue;
2716
2717 if ((version != 1) && (version != 2))
2718 return(-1);
2719 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002720 return(ret);
2721}
Daniel Veillard81273902003-09-30 00:43:48 +00002722#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002723
2724/**
2725 * xmlSAXVersion:
2726 * @hdlr: the SAX handler
2727 * @version: the version, 1 or 2
2728 *
2729 * Initialize the default XML SAX handler according to the version
2730 *
2731 * Returns 0 in case of success and -1 in case of error.
2732 */
2733int
2734xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2735{
2736 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002737 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002738 hdlr->startElement = NULL;
2739 hdlr->endElement = NULL;
2740 hdlr->startElementNs = xmlSAX2StartElementNs;
2741 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002742 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002743 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002744#ifdef LIBXML_SAX1_ENABLED
2745 } else if (version == 1) {
2746 hdlr->startElement = xmlSAX2StartElement;
2747 hdlr->endElement = xmlSAX2EndElement;
2748 hdlr->initialized = 1;
2749#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002750 } else
2751 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002752 hdlr->internalSubset = xmlSAX2InternalSubset;
2753 hdlr->externalSubset = xmlSAX2ExternalSubset;
2754 hdlr->isStandalone = xmlSAX2IsStandalone;
2755 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2756 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2757 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2758 hdlr->getEntity = xmlSAX2GetEntity;
2759 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2760 hdlr->entityDecl = xmlSAX2EntityDecl;
2761 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2762 hdlr->elementDecl = xmlSAX2ElementDecl;
2763 hdlr->notationDecl = xmlSAX2NotationDecl;
2764 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2765 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2766 hdlr->startDocument = xmlSAX2StartDocument;
2767 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002768 hdlr->reference = xmlSAX2Reference;
2769 hdlr->characters = xmlSAX2Characters;
2770 hdlr->cdataBlock = xmlSAX2CDataBlock;
2771 hdlr->ignorableWhitespace = xmlSAX2Characters;
2772 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2773 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002774 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002775 hdlr->error = xmlParserError;
2776 hdlr->fatalError = xmlParserError;
2777
Daniel Veillarde57ec792003-09-10 10:50:59 +00002778 return(0);
2779}
2780
2781/**
2782 * xmlSAX2InitDefaultSAXHandler:
2783 * @hdlr: the SAX handler
2784 * @warning: flag if non-zero sets the handler warning procedure
2785 *
2786 * Initialize the default XML SAX2 handler
2787 */
2788void
2789xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2790{
2791 if ((hdlr == NULL) || (hdlr->initialized != 0))
2792 return;
2793
2794 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2795 if (warning == 0)
2796 hdlr->warning = NULL;
2797 else
2798 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002799}
2800
2801/**
2802 * xmlDefaultSAXHandlerInit:
2803 *
2804 * Initialize the default SAX2 handler
2805 */
2806void
2807xmlDefaultSAXHandlerInit(void)
2808{
Daniel Veillard81273902003-09-30 00:43:48 +00002809#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002810 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002811#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002812}
2813
2814#ifdef LIBXML_HTML_ENABLED
2815
2816/**
2817 * xmlSAX2InitHtmlDefaultSAXHandler:
2818 * @hdlr: the SAX handler
2819 *
2820 * Initialize the default HTML SAX2 handler
2821 */
2822void
2823xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2824{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002825 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002826 return;
2827
2828 hdlr->internalSubset = xmlSAX2InternalSubset;
2829 hdlr->externalSubset = NULL;
2830 hdlr->isStandalone = NULL;
2831 hdlr->hasInternalSubset = NULL;
2832 hdlr->hasExternalSubset = NULL;
2833 hdlr->resolveEntity = NULL;
2834 hdlr->getEntity = xmlSAX2GetEntity;
2835 hdlr->getParameterEntity = NULL;
2836 hdlr->entityDecl = NULL;
2837 hdlr->attributeDecl = NULL;
2838 hdlr->elementDecl = NULL;
2839 hdlr->notationDecl = NULL;
2840 hdlr->unparsedEntityDecl = NULL;
2841 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2842 hdlr->startDocument = xmlSAX2StartDocument;
2843 hdlr->endDocument = xmlSAX2EndDocument;
2844 hdlr->startElement = xmlSAX2StartElement;
2845 hdlr->endElement = xmlSAX2EndElement;
2846 hdlr->reference = NULL;
2847 hdlr->characters = xmlSAX2Characters;
2848 hdlr->cdataBlock = xmlSAX2CDataBlock;
2849 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002850 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002851 hdlr->comment = xmlSAX2Comment;
2852 hdlr->warning = xmlParserWarning;
2853 hdlr->error = xmlParserError;
2854 hdlr->fatalError = xmlParserError;
2855
Daniel Veillard092643b2003-09-25 14:29:29 +00002856 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002857}
2858
2859/**
2860 * htmlDefaultSAXHandlerInit:
2861 *
2862 * Initialize the default SAX handler
2863 */
2864void
2865htmlDefaultSAXHandlerInit(void)
2866{
Daniel Veillard092643b2003-09-25 14:29:29 +00002867 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002868}
2869
2870#endif /* LIBXML_HTML_ENABLED */
2871
2872#ifdef LIBXML_DOCB_ENABLED
2873
2874/**
2875 * xmlSAX2InitDocbDefaultSAXHandler:
2876 * @hdlr: the SAX handler
2877 *
2878 * Initialize the default DocBook SAX2 handler
2879 */
2880void
2881xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2882{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002883 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002884 return;
2885
2886 hdlr->internalSubset = xmlSAX2InternalSubset;
2887 hdlr->externalSubset = NULL;
2888 hdlr->isStandalone = xmlSAX2IsStandalone;
2889 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2890 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2891 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2892 hdlr->getEntity = xmlSAX2GetEntity;
2893 hdlr->getParameterEntity = NULL;
2894 hdlr->entityDecl = xmlSAX2EntityDecl;
2895 hdlr->attributeDecl = NULL;
2896 hdlr->elementDecl = NULL;
2897 hdlr->notationDecl = NULL;
2898 hdlr->unparsedEntityDecl = NULL;
2899 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2900 hdlr->startDocument = xmlSAX2StartDocument;
2901 hdlr->endDocument = xmlSAX2EndDocument;
2902 hdlr->startElement = xmlSAX2StartElement;
2903 hdlr->endElement = xmlSAX2EndElement;
2904 hdlr->reference = xmlSAX2Reference;
2905 hdlr->characters = xmlSAX2Characters;
2906 hdlr->cdataBlock = NULL;
2907 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2908 hdlr->processingInstruction = NULL;
2909 hdlr->comment = xmlSAX2Comment;
2910 hdlr->warning = xmlParserWarning;
2911 hdlr->error = xmlParserError;
2912 hdlr->fatalError = xmlParserError;
2913
Daniel Veillardffbbed42003-10-10 14:46:54 +00002914 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002915}
2916
2917/**
2918 * docbDefaultSAXHandlerInit:
2919 *
2920 * Initialize the default SAX handler
2921 */
2922void
2923docbDefaultSAXHandlerInit(void)
2924{
Daniel Veillard092643b2003-09-25 14:29:29 +00002925 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002926}
2927
2928#endif /* LIBXML_DOCB_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002929#define bottom_SAX2
2930#include "elfgcchack.h"