blob: 90ba5daa44bda4392f96c30f09c1589ae51ee1d8 [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 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +080048#define TODO \
Daniel Veillarde57ec792003-09-10 10:50:59 +000049 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 Veillard740cb1a2012-07-18 16:05:37 +080060 xmlStructuredErrorFunc schannel = NULL;
61 const char *str1 = "out of memory\n";
62
Daniel Veillard34099b42004-11-04 17:34:35 +000063 if (ctxt != NULL) {
Daniel Veillard740cb1a2012-07-18 16:05:37 +080064 ctxt->errNo = XML_ERR_NO_MEMORY;
65 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
66 schannel = ctxt->sax->serror;
67 __xmlRaiseError(schannel,
68 ctxt->vctxt.error, ctxt->vctxt.userData,
69 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
70 XML_ERR_ERROR, NULL, 0, (const char *) str1,
71 NULL, NULL, 0, 0,
72 msg, (const char *) str1, NULL);
Daniel Veillard34099b42004-11-04 17:34:35 +000073 ctxt->errNo = XML_ERR_NO_MEMORY;
74 ctxt->instate = XML_PARSER_EOF;
75 ctxt->disableSAX = 1;
Daniel Veillard740cb1a2012-07-18 16:05:37 +080076 } else {
77 __xmlRaiseError(schannel,
78 NULL, NULL,
79 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
80 XML_ERR_ERROR, NULL, 0, (const char *) str1,
81 NULL, NULL, 0, 0,
82 msg, (const char *) str1, NULL);
Daniel Veillard34099b42004-11-04 17:34:35 +000083 }
William M. Brack42331a92004-07-29 07:07:16 +000084}
85
Daniel Veillarde57ec792003-09-10 10:50:59 +000086/**
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000087 * xmlValidError:
88 * @ctxt: an XML validation parser context
89 * @error: the error number
90 * @msg: the error message
91 * @str1: extra data
92 * @str2: extra data
93 *
94 * Handle a validation error
95 */
96static void
97xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
98 const char *msg, const char *str1, const char *str2)
99{
100 xmlStructuredErrorFunc schannel = NULL;
101
102 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
103 (ctxt->instate == XML_PARSER_EOF))
104 return;
Daniel Veillard34099b42004-11-04 17:34:35 +0000105 if (ctxt != NULL) {
106 ctxt->errNo = error;
107 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
108 schannel = ctxt->sax->serror;
Daniel Veillard2728f842006-03-09 16:49:24 +0000109 __xmlRaiseError(schannel,
110 ctxt->vctxt.error, ctxt->vctxt.userData,
111 ctxt, NULL, XML_FROM_DTD, error,
112 XML_ERR_ERROR, NULL, 0, (const char *) str1,
113 (const char *) str2, NULL, 0, 0,
114 msg, (const char *) str1, (const char *) str2);
Daniel Veillard34099b42004-11-04 17:34:35 +0000115 ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000116 } else {
117 __xmlRaiseError(schannel,
118 NULL, NULL,
119 ctxt, NULL, XML_FROM_DTD, error,
120 XML_ERR_ERROR, NULL, 0, (const char *) str1,
121 (const char *) str2, NULL, 0, 0,
122 msg, (const char *) str1, (const char *) str2);
123 }
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000124}
125
126/**
Daniel Veillard87b30462005-07-05 14:04:36 +0000127 * xmlFatalErrMsg:
128 * @ctxt: an XML parser context
129 * @error: the error number
130 * @msg: the error message
131 * @str1: an error string
132 * @str2: an error string
133 *
134 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
135 */
136static void
137xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
138 const char *msg, const xmlChar *str1, const xmlChar *str2)
139{
140 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
141 (ctxt->instate == XML_PARSER_EOF))
142 return;
143 if (ctxt != NULL)
144 ctxt->errNo = error;
145 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800146 XML_ERR_FATAL, NULL, 0,
Daniel Veillard87b30462005-07-05 14:04:36 +0000147 (const char *) str1, (const char *) str2,
148 NULL, 0, 0, msg, str1, str2);
149 if (ctxt != NULL) {
150 ctxt->wellFormed = 0;
151 ctxt->valid = 0;
152 if (ctxt->recovery == 0)
153 ctxt->disableSAX = 1;
154 }
155}
156
157/**
158 * xmlWarnMsg:
159 * @ctxt: an XML parser context
160 * @error: the error number
161 * @msg: the error message
162 * @str1: an error string
163 * @str2: an error string
164 *
165 * Handle a parser warning
166 */
167static void
168xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
169 const char *msg, const xmlChar *str1)
170{
171 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
172 (ctxt->instate == XML_PARSER_EOF))
173 return;
174 if (ctxt != NULL)
175 ctxt->errNo = error;
176 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800177 XML_ERR_WARNING, NULL, 0,
Daniel Veillard87b30462005-07-05 14:04:36 +0000178 (const char *) str1, NULL,
179 NULL, 0, 0, msg, str1);
180}
181
182/**
183 * xmlNsErrMsg:
184 * @ctxt: an XML parser context
185 * @error: the error number
186 * @msg: the error message
187 * @str1: an error string
188 * @str2: an error string
189 *
190 * Handle a namespace error
191 */
192static void
193xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
194 const char *msg, const xmlChar *str1, const xmlChar *str2)
195{
196 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
197 (ctxt->instate == XML_PARSER_EOF))
198 return;
199 if (ctxt != NULL)
200 ctxt->errNo = error;
201 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800202 XML_ERR_ERROR, NULL, 0,
Daniel Veillard87b30462005-07-05 14:04:36 +0000203 (const char *) str1, (const char *) str2,
204 NULL, 0, 0, msg, str1, str2);
205}
206
207/**
208 * xmlNsWarnMsg:
209 * @ctxt: an XML parser context
210 * @error: the error number
211 * @msg: the error message
212 * @str1: an error string
213 *
214 * Handle a namespace warning
215 */
216static void
217xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
218 const char *msg, const xmlChar *str1, const xmlChar *str2)
219{
220 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
221 (ctxt->instate == XML_PARSER_EOF))
222 return;
223 if (ctxt != NULL)
224 ctxt->errNo = error;
225 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800226 XML_ERR_WARNING, NULL, 0,
Daniel Veillard87b30462005-07-05 14:04:36 +0000227 (const char *) str1, (const char *) str2,
228 NULL, 0, 0, msg, str1, str2);
229}
230
231/**
Daniel Veillard1af9a412003-08-20 22:54:39 +0000232 * xmlSAX2GetPublicId:
233 * @ctx: the user data (XML parser context)
234 *
235 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
236 *
237 * Returns a xmlChar *
238 */
239const xmlChar *
240xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
241{
242 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
243 return(NULL);
244}
245
246/**
247 * xmlSAX2GetSystemId:
248 * @ctx: the user data (XML parser context)
249 *
250 * Provides the system ID, basically URL or filename e.g.
251 * http://www.sgmlsource.com/dtds/memo.dtd
252 *
253 * Returns a xmlChar *
254 */
255const xmlChar *
256xmlSAX2GetSystemId(void *ctx)
257{
258 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard24505b02005-07-28 23:49:35 +0000259 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800260 return((const xmlChar *) ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000261}
262
263/**
264 * xmlSAX2GetLineNumber:
265 * @ctx: the user data (XML parser context)
266 *
267 * Provide the line number of the current parsing point.
268 *
269 * Returns an int
270 */
271int
272xmlSAX2GetLineNumber(void *ctx)
273{
274 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000275 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000276 return(ctxt->input->line);
277}
278
279/**
280 * xmlSAX2GetColumnNumber:
281 * @ctx: the user data (XML parser context)
282 *
283 * Provide the column number of the current parsing point.
284 *
285 * Returns an int
286 */
287int
288xmlSAX2GetColumnNumber(void *ctx)
289{
290 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000291 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000292 return(ctxt->input->col);
293}
294
295/**
296 * xmlSAX2IsStandalone:
297 * @ctx: the user data (XML parser context)
298 *
299 * Is this document tagged standalone ?
300 *
301 * Returns 1 if true
302 */
303int
304xmlSAX2IsStandalone(void *ctx)
305{
306 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000307 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000308 return(ctxt->myDoc->standalone == 1);
309}
310
311/**
312 * xmlSAX2HasInternalSubset:
313 * @ctx: the user data (XML parser context)
314 *
315 * Does this document has an internal subset
316 *
317 * Returns 1 if true
318 */
319int
320xmlSAX2HasInternalSubset(void *ctx)
321{
322 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000323 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000324 return(ctxt->myDoc->intSubset != NULL);
325}
326
327/**
328 * xmlSAX2HasExternalSubset:
329 * @ctx: the user data (XML parser context)
330 *
331 * Does this document has an external subset
332 *
333 * Returns 1 if true
334 */
335int
336xmlSAX2HasExternalSubset(void *ctx)
337{
338 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000339 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000340 return(ctxt->myDoc->extSubset != NULL);
341}
342
343/**
344 * xmlSAX2InternalSubset:
345 * @ctx: the user data (XML parser context)
346 * @name: the root element name
347 * @ExternalID: the external ID
348 * @SystemID: the SYSTEM ID (e.g. filename or URL)
349 *
350 * Callback on internal subset declaration.
351 */
352void
353xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
354 const xmlChar *ExternalID, const xmlChar *SystemID)
355{
356 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
357 xmlDtdPtr dtd;
Daniel Veillard34099b42004-11-04 17:34:35 +0000358 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000359#ifdef DEBUG_SAX
360 xmlGenericError(xmlGenericErrorContext,
361 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
362 name, ExternalID, SystemID);
363#endif
364
365 if (ctxt->myDoc == NULL)
366 return;
367 dtd = xmlGetIntSubset(ctxt->myDoc);
368 if (dtd != NULL) {
369 if (ctxt->html)
370 return;
371 xmlUnlinkNode((xmlNodePtr) dtd);
372 xmlFreeDtd(dtd);
373 ctxt->myDoc->intSubset = NULL;
374 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800375 ctxt->myDoc->intSubset =
Daniel Veillard1af9a412003-08-20 22:54:39 +0000376 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
William M. Brack42331a92004-07-29 07:07:16 +0000377 if (ctxt->myDoc->intSubset == NULL)
378 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000379}
380
381/**
382 * xmlSAX2ExternalSubset:
383 * @ctx: the user data (XML parser context)
384 * @name: the root element name
385 * @ExternalID: the external ID
386 * @SystemID: the SYSTEM ID (e.g. filename or URL)
387 *
388 * Callback on external subset declaration.
389 */
390void
391xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
392 const xmlChar *ExternalID, const xmlChar *SystemID)
393{
394 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000395 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000396#ifdef DEBUG_SAX
397 xmlGenericError(xmlGenericErrorContext,
398 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
399 name, ExternalID, SystemID);
400#endif
401 if (((ExternalID != NULL) || (SystemID != NULL)) &&
402 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
403 (ctxt->wellFormed && ctxt->myDoc))) {
404 /*
405 * Try to fetch and parse the external subset.
406 */
407 xmlParserInputPtr oldinput;
408 int oldinputNr;
409 int oldinputMax;
410 xmlParserInputPtr *oldinputTab;
411 xmlParserInputPtr input = NULL;
412 xmlCharEncoding enc;
413 int oldcharset;
414
415 /*
416 * Ask the Entity resolver to load the damn thing
417 */
418 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
419 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
420 SystemID);
421 if (input == NULL) {
422 return;
423 }
424
425 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
426
427 /*
428 * make sure we won't destroy the main document context
429 */
430 oldinput = ctxt->input;
431 oldinputNr = ctxt->inputNr;
432 oldinputMax = ctxt->inputMax;
433 oldinputTab = ctxt->inputTab;
434 oldcharset = ctxt->charset;
435
436 ctxt->inputTab = (xmlParserInputPtr *)
437 xmlMalloc(5 * sizeof(xmlParserInputPtr));
438 if (ctxt->inputTab == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000439 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000440 ctxt->input = oldinput;
441 ctxt->inputNr = oldinputNr;
442 ctxt->inputMax = oldinputMax;
443 ctxt->inputTab = oldinputTab;
444 ctxt->charset = oldcharset;
445 return;
446 }
447 ctxt->inputNr = 0;
448 ctxt->inputMax = 5;
449 ctxt->input = NULL;
450 xmlPushInput(ctxt, input);
451
452 /*
453 * On the fly encoding conversion if needed
454 */
455 if (ctxt->input->length >= 4) {
456 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
457 xmlSwitchEncoding(ctxt, enc);
458 }
459
460 if (input->filename == NULL)
461 input->filename = (char *) xmlCanonicPath(SystemID);
462 input->line = 1;
463 input->col = 1;
464 input->base = ctxt->input->cur;
465 input->cur = ctxt->input->cur;
466 input->free = NULL;
467
468 /*
469 * let's parse that entity knowing it's an external subset.
470 */
471 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
472
473 /*
474 * Free up the external entities
475 */
476
477 while (ctxt->inputNr > 1)
478 xmlPopInput(ctxt);
479 xmlFreeInputStream(ctxt->input);
480 xmlFree(ctxt->inputTab);
481
482 /*
483 * Restore the parsing context of the main entity
484 */
485 ctxt->input = oldinput;
486 ctxt->inputNr = oldinputNr;
487 ctxt->inputMax = oldinputMax;
488 ctxt->inputTab = oldinputTab;
489 ctxt->charset = oldcharset;
490 /* ctxt->wellFormed = oldwellFormed; */
491 }
492}
493
494/**
495 * xmlSAX2ResolveEntity:
496 * @ctx: the user data (XML parser context)
497 * @publicId: The public ID of the entity
498 * @systemId: The system ID of the entity
499 *
500 * The entity loader, to control the loading of external entities,
501 * the application can either:
502 * - override this xmlSAX2ResolveEntity() callback in the SAX block
503 * - or better use the xmlSetExternalEntityLoader() function to
504 * set up it's own entity resolution routine
505 *
506 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
507 */
508xmlParserInputPtr
509xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
510{
511 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
512 xmlParserInputPtr ret;
513 xmlChar *URI;
514 const char *base = NULL;
515
Daniel Veillard34099b42004-11-04 17:34:35 +0000516 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000517 if (ctxt->input != NULL)
518 base = ctxt->input->filename;
519 if (base == NULL)
520 base = ctxt->directory;
521
522 URI = xmlBuildURI(systemId, (const xmlChar *) base);
523
524#ifdef DEBUG_SAX
525 xmlGenericError(xmlGenericErrorContext,
526 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
527#endif
528
529 ret = xmlLoadExternalEntity((const char *) URI,
530 (const char *) publicId, ctxt);
531 if (URI != NULL)
532 xmlFree(URI);
533 return(ret);
534}
535
536/**
537 * xmlSAX2GetEntity:
538 * @ctx: the user data (XML parser context)
539 * @name: The entity name
540 *
541 * Get an entity by name
542 *
543 * Returns the xmlEntityPtr if found.
544 */
545xmlEntityPtr
546xmlSAX2GetEntity(void *ctx, const xmlChar *name)
547{
548 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
549 xmlEntityPtr ret = NULL;
550
Daniel Veillard34099b42004-11-04 17:34:35 +0000551 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000552#ifdef DEBUG_SAX
553 xmlGenericError(xmlGenericErrorContext,
554 "SAX.xmlSAX2GetEntity(%s)\n", name);
555#endif
556
557 if (ctxt->inSubset == 0) {
558 ret = xmlGetPredefinedEntity(name);
559 if (ret != NULL)
560 return(ret);
561 }
562 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
563 if (ctxt->inSubset == 2) {
564 ctxt->myDoc->standalone = 0;
565 ret = xmlGetDocEntity(ctxt->myDoc, name);
566 ctxt->myDoc->standalone = 1;
567 } else {
568 ret = xmlGetDocEntity(ctxt->myDoc, name);
569 if (ret == NULL) {
570 ctxt->myDoc->standalone = 0;
571 ret = xmlGetDocEntity(ctxt->myDoc, name);
572 if (ret != NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000573 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
574 "Entity(%s) document marked standalone but requires external subset\n",
575 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000576 }
577 ctxt->myDoc->standalone = 1;
578 }
579 }
580 } else {
581 ret = xmlGetDocEntity(ctxt->myDoc, name);
582 }
583 if ((ret != NULL) &&
584 ((ctxt->validate) || (ctxt->replaceEntities)) &&
585 (ret->children == NULL) &&
586 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
587 int val;
588
589 /*
590 * for validation purposes we really need to fetch and
591 * parse the external entity
592 */
593 xmlNodePtr children;
594
595 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
596 ret->ExternalID, &children);
597 if (val == 0) {
598 xmlAddChildList((xmlNodePtr) ret, children);
599 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000600 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
601 "Failure to process entity %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000602 ctxt->validate = 0;
603 return(NULL);
604 }
605 ret->owner = 1;
Daniel Veillardf4f4e482008-08-25 08:57:48 +0000606 if (ret->checked == 0)
607 ret->checked = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000608 }
609 return(ret);
610}
611
612/**
613 * xmlSAX2GetParameterEntity:
614 * @ctx: the user data (XML parser context)
615 * @name: The entity name
616 *
617 * Get a parameter entity by name
618 *
619 * Returns the xmlEntityPtr if found.
620 */
621xmlEntityPtr
622xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
623{
624 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
625 xmlEntityPtr ret;
626
Daniel Veillard34099b42004-11-04 17:34:35 +0000627 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000628#ifdef DEBUG_SAX
629 xmlGenericError(xmlGenericErrorContext,
630 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
631#endif
632
633 ret = xmlGetParameterEntity(ctxt->myDoc, name);
634 return(ret);
635}
636
637
638/**
639 * xmlSAX2EntityDecl:
640 * @ctx: the user data (XML parser context)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800641 * @name: the entity name
642 * @type: the entity type
Daniel Veillard1af9a412003-08-20 22:54:39 +0000643 * @publicId: The public ID of the entity
644 * @systemId: The system ID of the entity
645 * @content: the entity value (without processing).
646 *
647 * An entity definition has been parsed
648 */
649void
650xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
651 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
652{
653 xmlEntityPtr ent;
654 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
655
Daniel Veillard34099b42004-11-04 17:34:35 +0000656 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000657#ifdef DEBUG_SAX
658 xmlGenericError(xmlGenericErrorContext,
659 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
660 name, type, publicId, systemId, content);
661#endif
662 if (ctxt->inSubset == 1) {
663 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
664 systemId, content);
Daniel Veillard87b30462005-07-05 14:04:36 +0000665 if ((ent == NULL) && (ctxt->pedantic))
666 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
667 "Entity(%s) already defined in the internal subset\n",
668 name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000669 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
670 xmlChar *URI;
671 const char *base = NULL;
672
673 if (ctxt->input != NULL)
674 base = ctxt->input->filename;
675 if (base == NULL)
676 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800677
Daniel Veillard1af9a412003-08-20 22:54:39 +0000678 URI = xmlBuildURI(systemId, (const xmlChar *) base);
679 ent->URI = URI;
680 }
681 } else if (ctxt->inSubset == 2) {
682 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
683 systemId, content);
684 if ((ent == NULL) && (ctxt->pedantic) &&
685 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800686 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000687 "Entity(%s) already defined in the external subset\n", name);
688 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
689 xmlChar *URI;
690 const char *base = NULL;
691
692 if (ctxt->input != NULL)
693 base = ctxt->input->filename;
694 if (base == NULL)
695 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800696
Daniel Veillard1af9a412003-08-20 22:54:39 +0000697 URI = xmlBuildURI(systemId, (const xmlChar *) base);
698 ent->URI = URI;
699 }
700 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000701 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
702 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
703 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000704 }
705}
706
707/**
708 * xmlSAX2AttributeDecl:
709 * @ctx: the user data (XML parser context)
710 * @elem: the name of the element
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800711 * @fullname: the attribute name
712 * @type: the attribute type
Daniel Veillard1af9a412003-08-20 22:54:39 +0000713 * @def: the type of default value
714 * @defaultValue: the attribute default value
715 * @tree: the tree of enumerated value set
716 *
717 * An attribute definition has been parsed
718 */
719void
720xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
721 int type, int def, const xmlChar *defaultValue,
722 xmlEnumerationPtr tree)
723{
724 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
725 xmlAttributePtr attr;
726 xmlChar *name = NULL, *prefix = NULL;
727
Daniel Veillard2728f842006-03-09 16:49:24 +0000728 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
729 return;
730
Daniel Veillard1af9a412003-08-20 22:54:39 +0000731#ifdef DEBUG_SAX
732 xmlGenericError(xmlGenericErrorContext,
733 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
734 elem, fullname, type, def, defaultValue);
735#endif
Daniel Veillard68cb4b22004-04-18 20:55:39 +0000736 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
737 (type != XML_ATTRIBUTE_ID)) {
738 /*
739 * Raise the error but keep the validity flag
740 */
741 int tmp = ctxt->valid;
742 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
743 "xml:id : attribute type should be ID\n", NULL, NULL);
744 ctxt->valid = tmp;
745 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000746 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000747 name = xmlSplitQName(ctxt, fullname, &prefix);
748 ctxt->vctxt.valid = 1;
749 if (ctxt->inSubset == 1)
750 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
751 name, prefix, (xmlAttributeType) type,
752 (xmlAttributeDefault) def, defaultValue, tree);
753 else if (ctxt->inSubset == 2)
754 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800755 name, prefix, (xmlAttributeType) type,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000756 (xmlAttributeDefault) def, defaultValue, tree);
757 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000758 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
759 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
760 name, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000761 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000762 return;
763 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000764#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000765 if (ctxt->vctxt.valid == 0)
766 ctxt->valid = 0;
767 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
Daniel Veillard2728f842006-03-09 16:49:24 +0000768 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000769 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
770 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000771#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000772 if (prefix != NULL)
773 xmlFree(prefix);
774 if (name != NULL)
775 xmlFree(name);
776}
777
778/**
779 * xmlSAX2ElementDecl:
780 * @ctx: the user data (XML parser context)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800781 * @name: the element name
782 * @type: the element type
Daniel Veillard1af9a412003-08-20 22:54:39 +0000783 * @content: the element value tree
784 *
785 * An element definition has been parsed
786 */
787void
788xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
789 xmlElementContentPtr content)
790{
791 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
792 xmlElementPtr elem = NULL;
793
Daniel Veillard2728f842006-03-09 16:49:24 +0000794 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
795 return;
796
Daniel Veillard1af9a412003-08-20 22:54:39 +0000797#ifdef DEBUG_SAX
798 xmlGenericError(xmlGenericErrorContext,
799 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
800#endif
801
802 if (ctxt->inSubset == 1)
803 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
804 name, (xmlElementTypeVal) type, content);
805 else if (ctxt->inSubset == 2)
806 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
807 name, (xmlElementTypeVal) type, content);
808 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000809 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
810 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
811 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000812 return;
813 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000814#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000815 if (elem == NULL)
816 ctxt->valid = 0;
817 if (ctxt->validate && ctxt->wellFormed &&
818 ctxt->myDoc && ctxt->myDoc->intSubset)
819 ctxt->valid &=
820 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +0000821#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000822}
823
824/**
825 * xmlSAX2NotationDecl:
826 * @ctx: the user data (XML parser context)
827 * @name: The name of the notation
828 * @publicId: The public ID of the entity
829 * @systemId: The system ID of the entity
830 *
831 * What to do when a notation declaration has been parsed.
832 */
833void
834xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
835 const xmlChar *publicId, const xmlChar *systemId)
836{
837 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
838 xmlNotationPtr nota = NULL;
839
Daniel Veillard2728f842006-03-09 16:49:24 +0000840 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
841 return;
842
Daniel Veillard1af9a412003-08-20 22:54:39 +0000843#ifdef DEBUG_SAX
844 xmlGenericError(xmlGenericErrorContext,
845 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
846#endif
847
848 if ((publicId == NULL) && (systemId == NULL)) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000849 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
850 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
851 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000852 return;
853 } else if (ctxt->inSubset == 1)
854 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
855 publicId, systemId);
856 else if (ctxt->inSubset == 2)
857 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
858 publicId, systemId);
859 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000860 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
861 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
862 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000863 return;
864 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000865#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000866 if (nota == NULL) ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000867 if ((ctxt->validate) && (ctxt->wellFormed) &&
868 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000869 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
870 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000871#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000872}
873
874/**
875 * xmlSAX2UnparsedEntityDecl:
876 * @ctx: the user data (XML parser context)
877 * @name: The name of the entity
878 * @publicId: The public ID of the entity
879 * @systemId: The system ID of the entity
880 * @notationName: the name of the notation
881 *
882 * What to do when an unparsed entity declaration is parsed
883 */
884void
885xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
886 const xmlChar *publicId, const xmlChar *systemId,
887 const xmlChar *notationName)
888{
889 xmlEntityPtr ent;
890 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000891 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000892#ifdef DEBUG_SAX
893 xmlGenericError(xmlGenericErrorContext,
894 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
895 name, publicId, systemId, notationName);
896#endif
897 if (ctxt->inSubset == 1) {
898 ent = xmlAddDocEntity(ctxt->myDoc, name,
899 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
900 publicId, systemId, notationName);
901 if ((ent == NULL) && (ctxt->pedantic) &&
902 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800903 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000904 "Entity(%s) already defined in the internal subset\n", name);
905 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
906 xmlChar *URI;
907 const char *base = NULL;
908
909 if (ctxt->input != NULL)
910 base = ctxt->input->filename;
911 if (base == NULL)
912 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800913
Daniel Veillard1af9a412003-08-20 22:54:39 +0000914 URI = xmlBuildURI(systemId, (const xmlChar *) base);
915 ent->URI = URI;
916 }
917 } else if (ctxt->inSubset == 2) {
918 ent = xmlAddDtdEntity(ctxt->myDoc, name,
919 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
920 publicId, systemId, notationName);
921 if ((ent == NULL) && (ctxt->pedantic) &&
922 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800923 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000924 "Entity(%s) already defined in the external subset\n", name);
925 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
926 xmlChar *URI;
927 const char *base = NULL;
928
929 if (ctxt->input != NULL)
930 base = ctxt->input->filename;
931 if (base == NULL)
932 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800933
Daniel Veillard1af9a412003-08-20 22:54:39 +0000934 URI = xmlBuildURI(systemId, (const xmlChar *) base);
935 ent->URI = URI;
936 }
937 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000938 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
939 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
940 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000941 }
942}
943
944/**
945 * xmlSAX2SetDocumentLocator:
946 * @ctx: the user data (XML parser context)
947 * @loc: A SAX Locator
948 *
949 * Receive the document locator at startup, actually xmlDefaultSAXLocator
950 * Everything is available on the context, so this is useless in our case.
951 */
952void
953xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
954{
955 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
956#ifdef DEBUG_SAX
957 xmlGenericError(xmlGenericErrorContext,
958 "SAX.xmlSAX2SetDocumentLocator()\n");
959#endif
960}
961
962/**
963 * xmlSAX2StartDocument:
964 * @ctx: the user data (XML parser context)
965 *
966 * called when the document start being processed.
967 */
968void
969xmlSAX2StartDocument(void *ctx)
970{
971 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
972 xmlDocPtr doc;
973
Daniel Veillard34099b42004-11-04 17:34:35 +0000974 if (ctx == NULL) return;
975
Daniel Veillard1af9a412003-08-20 22:54:39 +0000976#ifdef DEBUG_SAX
977 xmlGenericError(xmlGenericErrorContext,
978 "SAX.xmlSAX2StartDocument()\n");
979#endif
980 if (ctxt->html) {
981#ifdef LIBXML_HTML_ENABLED
982 if (ctxt->myDoc == NULL)
983 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000984 ctxt->myDoc->properties = XML_DOC_HTML;
985 ctxt->myDoc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000986 if (ctxt->myDoc == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000987 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000988 return;
989 }
990#else
991 xmlGenericError(xmlGenericErrorContext,
992 "libxml2 built without HTML support\n");
993 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
994 ctxt->instate = XML_PARSER_EOF;
995 ctxt->disableSAX = 1;
996 return;
997#endif
998 } else {
999 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
1000 if (doc != NULL) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00001001 doc->properties = 0;
1002 if (ctxt->options & XML_PARSE_OLD10)
1003 doc->properties |= XML_DOC_OLD10;
1004 doc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001005 if (ctxt->encoding != NULL)
1006 doc->encoding = xmlStrdup(ctxt->encoding);
1007 else
1008 doc->encoding = NULL;
1009 doc->standalone = ctxt->standalone;
1010 } else {
William M. Brack42331a92004-07-29 07:07:16 +00001011 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001012 return;
1013 }
Daniel Veillard500a1de2004-03-22 15:22:58 +00001014 if ((ctxt->dictNames) && (doc != NULL)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001015 doc->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00001016 xmlDictReference(doc->dict);
1017 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001018 }
1019 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1020 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillardb8efdda2006-10-10 12:37:14 +00001021 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001022 if (ctxt->myDoc->URL == NULL)
William M. Bracka3215c72004-07-31 16:24:01 +00001023 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001024 }
1025}
1026
1027/**
1028 * xmlSAX2EndDocument:
1029 * @ctx: the user data (XML parser context)
1030 *
1031 * called when the document end has been detected.
1032 */
1033void
1034xmlSAX2EndDocument(void *ctx)
1035{
1036 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1037#ifdef DEBUG_SAX
1038 xmlGenericError(xmlGenericErrorContext,
1039 "SAX.xmlSAX2EndDocument()\n");
1040#endif
Daniel Veillard34099b42004-11-04 17:34:35 +00001041 if (ctx == NULL) return;
Daniel Veillard4432df22003-09-28 18:58:27 +00001042#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001043 if (ctxt->validate && ctxt->wellFormed &&
1044 ctxt->myDoc && ctxt->myDoc->intSubset)
1045 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00001046#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001047
1048 /*
1049 * Grab the encoding if it was added on-the-fly
1050 */
1051 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1052 (ctxt->myDoc->encoding == NULL)) {
1053 ctxt->myDoc->encoding = ctxt->encoding;
1054 ctxt->encoding = NULL;
1055 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001056 if ((ctxt->inputTab != NULL) &&
1057 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1058 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001059 (ctxt->myDoc->encoding == NULL)) {
1060 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1061 }
1062 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1063 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1064 ctxt->myDoc->charset = ctxt->charset;
1065 }
1066}
1067
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001068#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001069/**
1070 * xmlSAX2AttributeInternal:
1071 * @ctx: the user data (XML parser context)
1072 * @fullname: The attribute name, including namespace prefix
1073 * @value: The attribute value
1074 * @prefix: the prefix on the element node
1075 *
1076 * Handle an attribute that has been read by the parser.
1077 * The default handling is to convert the attribute into an
1078 * DOM subtree and past it in a new xmlAttr element added to
1079 * the element.
1080 */
1081static void
1082xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001083 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001084{
1085 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1086 xmlAttrPtr ret;
1087 xmlChar *name;
1088 xmlChar *ns;
1089 xmlChar *nval;
1090 xmlNsPtr namespace;
1091
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001092 if (ctxt->html) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001093 name = xmlStrdup(fullname);
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001094 ns = NULL;
1095 namespace = NULL;
1096 } else {
1097 /*
1098 * Split the full name into a namespace prefix and the tag name
1099 */
1100 name = xmlSplitQName(ctxt, fullname, &ns);
1101 if ((name != NULL) && (name[0] == 0)) {
1102 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1103 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1104 "invalid namespace declaration '%s'\n",
1105 fullname, NULL);
1106 } else {
1107 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1108 "Avoid attribute ending with ':' like '%s'\n",
1109 fullname, NULL);
1110 }
1111 if (ns != NULL)
1112 xmlFree(ns);
1113 ns = NULL;
1114 xmlFree(name);
1115 name = xmlStrdup(fullname);
1116 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001117 }
1118 if (name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001119 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001120 if (ns != NULL)
1121 xmlFree(ns);
1122 return;
1123 }
1124
Daniel Veillard3c080d62010-03-15 15:47:50 +01001125#ifdef LIBXML_HTML_ENABLED
1126 if ((ctxt->html) &&
1127 (value == NULL) && (htmlIsBooleanAttr(fullname))) {
Daniel Veillard06c93b72010-03-15 16:08:44 +01001128 nval = xmlStrdup(fullname);
1129 value = (const xmlChar *) nval;
Daniel Veillard3c080d62010-03-15 15:47:50 +01001130 } else
1131#endif
1132 {
Daniel Veillard4432df22003-09-28 18:58:27 +00001133#ifdef LIBXML_VALID_ENABLED
Daniel Veillard3c080d62010-03-15 15:47:50 +01001134 /*
1135 * Do the last stage of the attribute normalization
1136 * Needed for HTML too:
1137 * http://www.w3.org/TR/html4/types.html#h-6.2
1138 */
1139 ctxt->vctxt.valid = 1;
1140 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1141 ctxt->myDoc, ctxt->node,
1142 fullname, value);
1143 if (ctxt->vctxt.valid != 1) {
1144 ctxt->valid = 0;
1145 }
1146 if (nval != NULL)
1147 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +00001148#else
Daniel Veillard3c080d62010-03-15 15:47:50 +01001149 nval = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +00001150#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard3c080d62010-03-15 15:47:50 +01001151 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001152
1153 /*
1154 * Check whether it's a namespace definition
1155 */
1156 if ((!ctxt->html) && (ns == NULL) &&
1157 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1158 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1159 xmlNsPtr nsret;
1160 xmlChar *val;
1161
1162 if (!ctxt->replaceEntities) {
1163 ctxt->depth++;
1164 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1165 0,0,0);
1166 ctxt->depth--;
1167 } else {
1168 val = (xmlChar *) value;
1169 }
1170
1171 if (val[0] != 0) {
1172 xmlURIPtr uri;
1173
1174 uri = xmlParseURI((const char *)val);
1175 if (uri == NULL) {
1176 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001177 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +00001178 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001179 } else {
1180 if (uri->scheme == NULL) {
1181 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001182 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001183 "xmlns: URI %s is not absolute\n", val);
1184 }
1185 xmlFreeURI(uri);
1186 }
1187 }
1188
1189 /* a default namespace definition */
1190 nsret = xmlNewNs(ctxt->node, val, NULL);
1191
Daniel Veillard4432df22003-09-28 18:58:27 +00001192#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001193 /*
1194 * Validate also for namespace decls, they are attributes from
1195 * an XML-1.0 perspective
1196 */
1197 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1198 ctxt->myDoc && ctxt->myDoc->intSubset)
1199 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1200 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +00001201#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001202 if (name != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001203 xmlFree(name);
1204 if (nval != NULL)
1205 xmlFree(nval);
1206 if (val != value)
1207 xmlFree(val);
1208 return;
1209 }
1210 if ((!ctxt->html) &&
1211 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1212 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1213 xmlNsPtr nsret;
1214 xmlChar *val;
1215
1216 if (!ctxt->replaceEntities) {
1217 ctxt->depth++;
1218 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1219 0,0,0);
1220 ctxt->depth--;
1221 if (val == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001222 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001223 xmlFree(ns);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001224 if (name != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001225 xmlFree(name);
1226 return;
1227 }
1228 } else {
1229 val = (xmlChar *) value;
1230 }
1231
1232 if (val[0] == 0) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001233 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1234 "Empty namespace name for prefix %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001235 }
1236 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1237 xmlURIPtr uri;
1238
1239 uri = xmlParseURI((const char *)val);
1240 if (uri == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001241 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001242 "xmlns:%s: %s not a valid URI\n", name, value);
1243 } else {
1244 if (uri->scheme == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001245 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001246 "xmlns:%s: URI %s is not absolute\n", name, value);
1247 }
1248 xmlFreeURI(uri);
1249 }
1250 }
1251
1252 /* a standard namespace definition */
1253 nsret = xmlNewNs(ctxt->node, val, name);
1254 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001255#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001256 /*
1257 * Validate also for namespace decls, they are attributes from
1258 * an XML-1.0 perspective
1259 */
1260 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1261 ctxt->myDoc && ctxt->myDoc->intSubset)
1262 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1263 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001264#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001265 if (name != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001266 xmlFree(name);
1267 if (nval != NULL)
1268 xmlFree(nval);
1269 if (val != value)
1270 xmlFree(val);
1271 return;
1272 }
1273
1274 if (ns != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001275 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardd44b9362009-09-07 12:15:08 +02001276
Daniel Veillard67906942003-08-28 21:13:25 +00001277 if (namespace == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001278 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
William M. Brack4811ba32003-09-06 18:02:53 +00001279 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001280 ns, name);
Daniel Veillardd44b9362009-09-07 12:15:08 +02001281 } else {
1282 xmlAttrPtr prop;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001283
Daniel Veillardd44b9362009-09-07 12:15:08 +02001284 prop = ctxt->node->properties;
1285 while (prop != NULL) {
1286 if (prop->ns != NULL) {
1287 if ((xmlStrEqual(name, prop->name)) &&
1288 ((namespace == prop->ns) ||
1289 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1290 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1291 "Attribute %s in %s redefined\n",
1292 name, namespace->href);
1293 ctxt->wellFormed = 0;
1294 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1295 goto error;
1296 }
1297 }
1298 prop = prop->next;
1299 }
1300 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001301 } else {
1302 namespace = NULL;
1303 }
1304
1305 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1306 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1307
1308 if (ret != NULL) {
1309 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1310 xmlNodePtr tmp;
1311
1312 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1313 tmp = ret->children;
1314 while (tmp != NULL) {
1315 tmp->parent = (xmlNodePtr) ret;
1316 if (tmp->next == NULL)
1317 ret->last = tmp;
1318 tmp = tmp->next;
1319 }
1320 } else if (value != NULL) {
1321 ret->children = xmlNewDocText(ctxt->myDoc, value);
1322 ret->last = ret->children;
1323 if (ret->children != NULL)
1324 ret->children->parent = (xmlNodePtr) ret;
1325 }
1326 }
1327
Daniel Veillard4432df22003-09-28 18:58:27 +00001328#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001329 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1330 ctxt->myDoc && ctxt->myDoc->intSubset) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001331
Daniel Veillard1af9a412003-08-20 22:54:39 +00001332 /*
1333 * If we don't substitute entities, the validation should be
1334 * done on a value with replaced entities anyway.
1335 */
1336 if (!ctxt->replaceEntities) {
1337 xmlChar *val;
1338
1339 ctxt->depth++;
1340 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1341 0,0,0);
1342 ctxt->depth--;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001343
Daniel Veillard1af9a412003-08-20 22:54:39 +00001344 if (val == NULL)
1345 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1346 ctxt->myDoc, ctxt->node, ret, value);
1347 else {
1348 xmlChar *nvalnorm;
1349
1350 /*
1351 * Do the last stage of the attribute normalization
1352 * It need to be done twice ... it's an extra burden related
1353 * to the ability to keep xmlSAX2References in attributes
1354 */
1355 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1356 ctxt->node, fullname, val);
1357 if (nvalnorm != NULL) {
1358 xmlFree(val);
1359 val = nvalnorm;
1360 }
1361
1362 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1363 ctxt->myDoc, ctxt->node, ret, val);
1364 xmlFree(val);
1365 }
1366 } else {
1367 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1368 ctxt->node, ret, value);
1369 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001370 } else
1371#endif /* LIBXML_VALID_ENABLED */
1372 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001373 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1374 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1375 /*
1376 * when validating, the ID registration is done at the attribute
1377 * validation level. Otherwise we have to do specific handling here.
1378 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001379 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001380 /*
1381 * Add the xml:id value
1382 *
1383 * Open issue: normalization of the value.
1384 */
Daniel Veillard68cb4b22004-04-18 20:55:39 +00001385 if (xmlValidateNCName(value, 1) != 0) {
1386 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1387 "xml:id : attribute value %s is not an NCName\n",
1388 (const char *) value, NULL);
1389 }
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001390 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001391 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1392 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1393 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1394 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001395 }
1396
1397error:
1398 if (nval != NULL)
1399 xmlFree(nval);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001400 if (ns != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001401 xmlFree(ns);
1402}
1403
Daniel Veillard1af9a412003-08-20 22:54:39 +00001404/*
1405 * xmlCheckDefaultedAttributes:
1406 *
1407 * Check defaulted attributes from the DTD
1408 */
1409static void
1410xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1411 const xmlChar *prefix, const xmlChar **atts) {
1412 xmlElementPtr elemDecl;
1413 const xmlChar *att;
1414 int internal = 1;
1415 int i;
1416
1417 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1418 if (elemDecl == NULL) {
1419 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1420 internal = 0;
1421 }
1422
1423process_external_subset:
1424
1425 if (elemDecl != NULL) {
1426 xmlAttributePtr attr = elemDecl->attributes;
1427 /*
1428 * Check against defaulted attributes from the external subset
1429 * if the document is stamped as standalone
1430 */
1431 if ((ctxt->myDoc->standalone == 1) &&
1432 (ctxt->myDoc->extSubset != NULL) &&
1433 (ctxt->validate)) {
1434 while (attr != NULL) {
1435 if ((attr->defaultValue != NULL) &&
1436 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1437 attr->elem, attr->name,
1438 attr->prefix) == attr) &&
1439 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1440 attr->elem, attr->name,
1441 attr->prefix) == NULL)) {
1442 xmlChar *fulln;
1443
1444 if (attr->prefix != NULL) {
1445 fulln = xmlStrdup(attr->prefix);
1446 fulln = xmlStrcat(fulln, BAD_CAST ":");
1447 fulln = xmlStrcat(fulln, attr->name);
1448 } else {
1449 fulln = xmlStrdup(attr->name);
1450 }
Jim Meyering669e88c2009-07-29 11:33:32 +02001451 if (fulln == NULL) {
1452 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1453 break;
1454 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001455
1456 /*
1457 * Check that the attribute is not declared in the
1458 * serialization
1459 */
1460 att = NULL;
1461 if (atts != NULL) {
1462 i = 0;
1463 att = atts[i];
1464 while (att != NULL) {
1465 if (xmlStrEqual(att, fulln))
1466 break;
1467 i += 2;
1468 att = atts[i];
1469 }
1470 }
1471 if (att == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001472 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001473 "standalone: attribute %s on %s defaulted from external subset\n",
Daniel Veillard427174f2003-12-10 10:42:59 +00001474 (const char *)fulln,
1475 (const char *)attr->elem);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001476 }
Daniel Veillard023d0ba2009-07-29 11:34:50 +02001477 xmlFree(fulln);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001478 }
1479 attr = attr->nexth;
1480 }
1481 }
1482
1483 /*
1484 * Actually insert defaulted values when needed
1485 */
1486 attr = elemDecl->attributes;
1487 while (attr != NULL) {
1488 /*
1489 * Make sure that attributes redefinition occuring in the
1490 * internal subset are not overriden by definitions in the
1491 * external subset.
1492 */
1493 if (attr->defaultValue != NULL) {
1494 /*
1495 * the element should be instantiated in the tree if:
1496 * - this is a namespace prefix
1497 * - the user required for completion in the tree
1498 * like XSLT
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001499 * - there isn't already an attribute definition
Daniel Veillard1af9a412003-08-20 22:54:39 +00001500 * in the internal subset overriding it.
1501 */
1502 if (((attr->prefix != NULL) &&
1503 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1504 ((attr->prefix == NULL) &&
1505 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1506 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1507 xmlAttributePtr tst;
1508
1509 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1510 attr->elem, attr->name,
1511 attr->prefix);
1512 if ((tst == attr) || (tst == NULL)) {
1513 xmlChar fn[50];
1514 xmlChar *fulln;
1515
1516 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1517 if (fulln == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001518 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001519 return;
1520 }
1521
1522 /*
1523 * Check that the attribute is not declared in the
1524 * serialization
1525 */
1526 att = NULL;
1527 if (atts != NULL) {
1528 i = 0;
1529 att = atts[i];
1530 while (att != NULL) {
1531 if (xmlStrEqual(att, fulln))
1532 break;
1533 i += 2;
1534 att = atts[i];
1535 }
1536 }
1537 if (att == NULL) {
1538 xmlSAX2AttributeInternal(ctxt, fulln,
1539 attr->defaultValue, prefix);
1540 }
1541 if ((fulln != fn) && (fulln != attr->name))
1542 xmlFree(fulln);
1543 }
1544 }
1545 }
1546 attr = attr->nexth;
1547 }
1548 if (internal == 1) {
1549 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1550 name, prefix);
1551 internal = 0;
1552 goto process_external_subset;
1553 }
1554 }
1555}
1556
1557/**
1558 * xmlSAX2StartElement:
1559 * @ctx: the user data (XML parser context)
1560 * @fullname: The element name, including namespace prefix
1561 * @atts: An array of name/value attributes pairs, NULL terminated
1562 *
1563 * called when an opening tag has been processed.
1564 */
1565void
1566xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1567{
1568 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1569 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001570 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001571 xmlNsPtr ns;
1572 xmlChar *name;
1573 xmlChar *prefix;
1574 const xmlChar *att;
1575 const xmlChar *value;
1576 int i;
1577
Daniel Veillarda521d282004-11-09 14:59:59 +00001578 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001579 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001580#ifdef DEBUG_SAX
1581 xmlGenericError(xmlGenericErrorContext,
1582 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1583#endif
1584
1585 /*
1586 * First check on validity:
1587 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001588 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001589 ((ctxt->myDoc->intSubset == NULL) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001590 ((ctxt->myDoc->intSubset->notations == NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001591 (ctxt->myDoc->intSubset->elements == NULL) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001592 (ctxt->myDoc->intSubset->attributes == NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001593 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001594 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1595 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001596 ctxt->validate = 0;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001597 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001598
Daniel Veillard1af9a412003-08-20 22:54:39 +00001599
1600 /*
1601 * Split the full name into a namespace prefix and the tag name
1602 */
1603 name = xmlSplitQName(ctxt, fullname, &prefix);
1604
1605
1606 /*
1607 * Note : the namespace resolution is deferred until the end of the
1608 * attributes parsing, since local namespace can be defined as
1609 * an attribute at this level.
1610 */
1611 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1612 if (ret == NULL) {
1613 if (prefix != NULL)
1614 xmlFree(prefix);
William M. Brack42331a92004-07-29 07:07:16 +00001615 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001616 return;
1617 }
1618 if (ctxt->myDoc->children == NULL) {
1619#ifdef DEBUG_SAX_TREE
1620 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1621#endif
1622 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1623 } else if (parent == NULL) {
1624 parent = ctxt->myDoc->children;
1625 }
1626 ctxt->nodemem = -1;
1627 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001628 if (ctxt->input != NULL) {
1629 if (ctxt->input->line < 65535)
1630 ret->line = (short) ctxt->input->line;
1631 else
1632 ret->line = 65535;
1633 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001634 }
1635
1636 /*
1637 * We are parsing a new node.
1638 */
1639#ifdef DEBUG_SAX_TREE
1640 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1641#endif
1642 nodePush(ctxt, ret);
1643
1644 /*
1645 * Link the child element
1646 */
1647 if (parent != NULL) {
1648 if (parent->type == XML_ELEMENT_NODE) {
1649#ifdef DEBUG_SAX_TREE
1650 xmlGenericError(xmlGenericErrorContext,
1651 "adding child %s to %s\n", name, parent->name);
1652#endif
1653 xmlAddChild(parent, ret);
1654 } else {
1655#ifdef DEBUG_SAX_TREE
1656 xmlGenericError(xmlGenericErrorContext,
1657 "adding sibling %s to ", name);
1658 xmlDebugDumpOneNode(stderr, parent, 0);
1659#endif
1660 xmlAddSibling(parent, ret);
1661 }
1662 }
1663
1664 /*
1665 * Insert all the defaulted attributes from the DTD especially namespaces
1666 */
1667 if ((!ctxt->html) &&
1668 ((ctxt->myDoc->intSubset != NULL) ||
1669 (ctxt->myDoc->extSubset != NULL))) {
1670 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1671 }
1672
1673 /*
1674 * process all the attributes whose name start with "xmlns"
1675 */
1676 if (atts != NULL) {
1677 i = 0;
1678 att = atts[i++];
1679 value = atts[i++];
1680 if (!ctxt->html) {
1681 while ((att != NULL) && (value != NULL)) {
1682 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1683 (att[3] == 'n') && (att[4] == 's'))
1684 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1685
1686 att = atts[i++];
1687 value = atts[i++];
1688 }
1689 }
1690 }
1691
1692 /*
1693 * Search the namespace, note that since the attributes have been
1694 * processed, the local namespaces are available.
1695 */
1696 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1697 if ((ns == NULL) && (parent != NULL))
1698 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1699 if ((prefix != NULL) && (ns == NULL)) {
1700 ns = xmlNewNs(ret, NULL, prefix);
Daniel Veillard77aad342006-07-13 06:21:09 +00001701 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1702 "Namespace prefix %s is not defined\n",
1703 prefix, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001704 }
1705
1706 /*
1707 * set the namespace node, making sure that if the default namspace
1708 * is unbound on a parent we simply kee it NULL
1709 */
1710 if ((ns != NULL) && (ns->href != NULL) &&
1711 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1712 xmlSetNs(ret, ns);
1713
1714 /*
1715 * process all the other attributes
1716 */
1717 if (atts != NULL) {
1718 i = 0;
1719 att = atts[i++];
1720 value = atts[i++];
1721 if (ctxt->html) {
1722 while (att != NULL) {
1723 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1724 att = atts[i++];
1725 value = atts[i++];
1726 }
1727 } else {
1728 while ((att != NULL) && (value != NULL)) {
1729 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1730 (att[3] != 'n') || (att[4] != 's'))
1731 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1732
1733 /*
1734 * Next ones
1735 */
1736 att = atts[i++];
1737 value = atts[i++];
1738 }
1739 }
1740 }
1741
Daniel Veillard4432df22003-09-28 18:58:27 +00001742#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001743 /*
1744 * If it's the Document root, finish the DTD validation and
1745 * check the document root element for validity
1746 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00001747 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001748 int chk;
1749
1750 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1751 if (chk <= 0)
1752 ctxt->valid = 0;
1753 if (chk < 0)
1754 ctxt->wellFormed = 0;
1755 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00001756 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001757 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001758#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001759
1760 if (prefix != NULL)
1761 xmlFree(prefix);
1762
1763}
1764
1765/**
1766 * xmlSAX2EndElement:
1767 * @ctx: the user data (XML parser context)
1768 * @name: The element name
1769 *
1770 * called when the end of an element has been detected.
1771 */
1772void
1773xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1774{
1775 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001776 xmlNodePtr cur;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001777
Daniel Veillard34099b42004-11-04 17:34:35 +00001778 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001779 cur = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001780#ifdef DEBUG_SAX
1781 if (name == NULL)
1782 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1783 else
1784 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1785#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001786
Daniel Veillard1af9a412003-08-20 22:54:39 +00001787 /* Capture end position and add node */
1788 if (cur != NULL && ctxt->record_info) {
Pavel Andrejs8ad4da52012-05-08 11:01:12 +08001789 ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
1790 ctxt->nodeInfo->end_line = ctxt->input->line;
1791 ctxt->nodeInfo->node = cur;
1792 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001793 }
1794 ctxt->nodemem = -1;
1795
Daniel Veillard4432df22003-09-28 18:58:27 +00001796#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001797 if (ctxt->validate && ctxt->wellFormed &&
1798 ctxt->myDoc && ctxt->myDoc->intSubset)
1799 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1800 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001801#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001802
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001803
Daniel Veillard1af9a412003-08-20 22:54:39 +00001804 /*
1805 * end of parsing of this node.
1806 */
1807#ifdef DEBUG_SAX_TREE
1808 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1809#endif
1810 nodePop(ctxt);
1811}
Daniel Veillard81273902003-09-30 00:43:48 +00001812#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001813
Daniel Veillarde57ec792003-09-10 10:50:59 +00001814/*
Daniel Veillard19895052003-09-17 13:59:32 +00001815 * xmlSAX2TextNode:
1816 * @ctxt: the parser context
1817 * @str: the input string
1818 * @len: the string length
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001819 *
Daniel Veillard968a03a2012-08-13 12:41:33 +08001820 * Callback for a text node
Daniel Veillard19895052003-09-17 13:59:32 +00001821 *
1822 * Returns the newly allocated string or NULL if not needed or error
1823 */
1824static xmlNodePtr
1825xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1826 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001827 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001828
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001829 /*
1830 * Allocate
1831 */
Daniel Veillard19895052003-09-17 13:59:32 +00001832 if (ctxt->freeElems != NULL) {
1833 ret = ctxt->freeElems;
1834 ctxt->freeElems = ret->next;
1835 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001836 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001837 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001838 }
1839 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001840 xmlErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard19895052003-09-17 13:59:32 +00001841 return(NULL);
1842 }
Daniel Veillard8874b942005-08-25 13:19:21 +00001843 memset(ret, 0, sizeof(xmlNode));
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001844 /*
1845 * intern the formatting blanks found between tags, or the
1846 * very short strings
1847 */
1848 if (ctxt->dictNames) {
1849 xmlChar cur = str[len];
1850
Daniel Veillard8874b942005-08-25 13:19:21 +00001851 if ((len < (int) (2 * sizeof(void *))) &&
1852 (ctxt->options & XML_PARSE_COMPACT)) {
Daniel Veillard968a03a2012-08-13 12:41:33 +08001853 /* store the string in the node overriding properties and nsDef */
Daniel Veillard8874b942005-08-25 13:19:21 +00001854 xmlChar *tmp = (xmlChar *) &(ret->properties);
1855 memcpy(tmp, str, len);
1856 tmp[len] = 0;
1857 intern = tmp;
1858 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001859 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001860 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001861 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001862 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001863 int i;
1864
1865 for (i = 1;i < len;i++) {
Daniel Veillard1a9b7082004-01-02 10:42:01 +00001866 if (!IS_BLANK_CH(str[i])) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001867 }
1868 intern = xmlDictLookup(ctxt->dict, str, len);
1869 }
1870 }
1871skip:
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001872 ret->type = XML_TEXT_NODE;
1873
1874 ret->name = xmlStringText;
William M. Brack9f797ab2004-07-28 07:40:12 +00001875 if (intern == NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001876 ret->content = xmlStrndup(str, len);
William M. Brack9f797ab2004-07-28 07:40:12 +00001877 if (ret->content == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001878 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1879 xmlFree(ret);
William M. Brack9f797ab2004-07-28 07:40:12 +00001880 return(NULL);
1881 }
1882 } else
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001883 ret->content = (xmlChar *) intern;
1884
Daniel Veillard968a03a2012-08-13 12:41:33 +08001885 if (ctxt->linenumbers) {
1886 if (ctxt->input != NULL) {
1887 if (ctxt->input->line < 65535)
1888 ret->line = (short) ctxt->input->line;
1889 else {
1890 ret->line = 65535;
1891 if (ctxt->options & XML_PARSE_BIG_LINES)
1892 ret->psvi = (void *) ctxt->input->line;
1893 }
1894 }
1895 }
Daniel Veillard45efd082008-07-07 13:52:52 +00001896
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001897 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1898 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001899 return(ret);
1900}
1901
Daniel Veillard4432df22003-09-28 18:58:27 +00001902#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001903/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001904 * xmlSAX2DecodeAttrEntities:
1905 * @ctxt: the parser context
1906 * @str: the input string
1907 * @len: the string length
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001908 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001909 * Remove the entities from an attribute value
1910 *
1911 * Returns the newly allocated string or NULL if not needed or error
1912 */
1913static xmlChar *
1914xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1915 const xmlChar *end) {
1916 const xmlChar *in;
1917 xmlChar *ret;
1918
1919 in = str;
1920 while (in < end)
1921 if (*in++ == '&')
1922 goto decode;
1923 return(NULL);
1924decode:
1925 ctxt->depth++;
1926 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1927 XML_SUBSTITUTE_REF, 0,0,0);
1928 ctxt->depth--;
1929 return(ret);
1930}
Daniel Veillard4432df22003-09-28 18:58:27 +00001931#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001932
1933/**
1934 * xmlSAX2AttributeNs:
1935 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001936 * @localname: the local name of the attribute
1937 * @prefix: the attribute namespace prefix if available
1938 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001939 * @value: Start of the attribute value
1940 * @valueend: end of the attribute value
1941 *
1942 * Handle an attribute that has been read by the parser.
1943 * The default handling is to convert the attribute into an
1944 * DOM subtree and past it in a new xmlAttr element added to
1945 * the element.
1946 */
1947static void
1948xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1949 const xmlChar * localname,
1950 const xmlChar * prefix,
1951 const xmlChar * value,
1952 const xmlChar * valueend)
1953{
1954 xmlAttrPtr ret;
1955 xmlNsPtr namespace = NULL;
1956 xmlChar *dup = NULL;
1957
Daniel Veillarde57ec792003-09-10 10:50:59 +00001958 /*
1959 * Note: if prefix == NULL, the attribute is not in the default namespace
1960 */
1961 if (prefix != NULL)
1962 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1963
Daniel Veillard8a44e592003-09-15 14:50:06 +00001964 /*
1965 * allocate the node
1966 */
1967 if (ctxt->freeAttrs != NULL) {
1968 ret = ctxt->freeAttrs;
1969 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001970 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001971 memset(ret, 0, sizeof(xmlAttr));
1972 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001973
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001974 ret->parent = ctxt->node;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001975 ret->doc = ctxt->myDoc;
1976 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001977
Daniel Veillard8a44e592003-09-15 14:50:06 +00001978 if (ctxt->dictNames)
1979 ret->name = localname;
1980 else
1981 ret->name = xmlStrdup(localname);
1982
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001983 /* link at the end to preserv order, TODO speed up with a last */
1984 if (ctxt->node->properties == NULL) {
1985 ctxt->node->properties = ret;
1986 } else {
1987 xmlAttrPtr prev = ctxt->node->properties;
1988
1989 while (prev->next != NULL) prev = prev->next;
1990 prev->next = ret;
1991 ret->prev = prev;
1992 }
1993
Daniel Veillard8a44e592003-09-15 14:50:06 +00001994 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1995 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1996 } else {
1997 if (ctxt->dictNames)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001998 ret = xmlNewNsPropEatName(ctxt->node, namespace,
Daniel Veillard8a44e592003-09-15 14:50:06 +00001999 (xmlChar *) localname, NULL);
2000 else
2001 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
2002 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002003 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002004 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002005 }
2006 }
2007
Daniel Veillard8a44e592003-09-15 14:50:06 +00002008 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
2009 xmlNodePtr tmp;
2010
Daniel Veillard19895052003-09-17 13:59:32 +00002011 /*
2012 * We know that if there is an entity reference, then
2013 * the string has been dup'ed and terminates with 0
2014 * otherwise with ' or "
2015 */
2016 if (*valueend != 0) {
2017 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2018 ret->children = tmp;
2019 ret->last = tmp;
2020 if (tmp != NULL) {
2021 tmp->doc = ret->doc;
2022 tmp->parent = (xmlNodePtr) ret;
2023 }
2024 } else {
2025 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
2026 valueend - value);
2027 tmp = ret->children;
2028 while (tmp != NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00002029 tmp->doc = ret->doc;
Daniel Veillard19895052003-09-17 13:59:32 +00002030 tmp->parent = (xmlNodePtr) ret;
2031 if (tmp->next == NULL)
2032 ret->last = tmp;
2033 tmp = tmp->next;
2034 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002035 }
2036 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002037 xmlNodePtr tmp;
2038
2039 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2040 ret->children = tmp;
2041 ret->last = tmp;
2042 if (tmp != NULL) {
2043 tmp->doc = ret->doc;
2044 tmp->parent = (xmlNodePtr) ret;
2045 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002046 }
2047
Daniel Veillard4432df22003-09-28 18:58:27 +00002048#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002049 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2050 ctxt->myDoc && ctxt->myDoc->intSubset) {
2051 /*
2052 * If we don't substitute entities, the validation should be
2053 * done on a value with replaced entities anyway.
2054 */
2055 if (!ctxt->replaceEntities) {
2056 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2057 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00002058 if (*valueend == 0) {
2059 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2060 ctxt->myDoc, ctxt->node, ret, value);
2061 } else {
2062 /*
2063 * That should already be normalized.
2064 * cheaper to finally allocate here than duplicate
2065 * entry points in the full validation code
2066 */
2067 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002068
Daniel Veillard62998c02003-09-15 12:56:36 +00002069 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2070 ctxt->myDoc, ctxt->node, ret, dup);
2071 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002072 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00002073 /*
2074 * dup now contains a string of the flattened attribute
2075 * content with entities substitued. Check if we need to
2076 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002077 * It need to be done twice ... it's an extra burden related
2078 * to the ability to keep references in attributes
2079 */
Daniel Veillard62998c02003-09-15 12:56:36 +00002080 if (ctxt->attsSpecial != NULL) {
2081 xmlChar *nvalnorm;
2082 xmlChar fn[50];
2083 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002084
Daniel Veillard62998c02003-09-15 12:56:36 +00002085 fullname = xmlBuildQName(localname, prefix, fn, 50);
2086 if (fullname != NULL) {
2087 ctxt->vctxt.valid = 1;
2088 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2089 &ctxt->vctxt, ctxt->myDoc,
2090 ctxt->node, fullname, dup);
2091 if (ctxt->vctxt.valid != 1)
2092 ctxt->valid = 0;
2093
2094 if ((fullname != fn) && (fullname != localname))
2095 xmlFree(fullname);
2096 if (nvalnorm != NULL) {
2097 xmlFree(dup);
2098 dup = nvalnorm;
2099 }
2100 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002101 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002102
2103 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2104 ctxt->myDoc, ctxt->node, ret, dup);
2105 }
2106 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002107 /*
2108 * if entities already have been substitued, then
2109 * the attribute as passed is already normalized
2110 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002111 dup = xmlStrndup(value, valueend - value);
2112
2113 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2114 ctxt->myDoc, ctxt->node, ret, dup);
2115 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002116 } else
2117#endif /* LIBXML_VALID_ENABLED */
2118 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002119 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2120 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2121 /*
2122 * when validating, the ID registration is done at the attribute
2123 * validation level. Otherwise we have to do specific handling here.
2124 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002125 if ((prefix == ctxt->str_xml) &&
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002126 (localname[0] == 'i') && (localname[1] == 'd') &&
2127 (localname[2] == 0)) {
2128 /*
2129 * Add the xml:id value
2130 *
2131 * Open issue: normalization of the value.
2132 */
2133 if (dup == NULL)
2134 dup = xmlStrndup(value, valueend - value);
William M. Brack5ef2f812004-05-23 23:56:47 +00002135#ifdef LIBXML_VALID_ENABLED
Daniel Veillard68cb4b22004-04-18 20:55:39 +00002136 if (xmlValidateNCName(dup, 1) != 0) {
2137 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2138 "xml:id : attribute value %s is not an NCName\n",
2139 (const char *) dup, NULL);
2140 }
William M. Brack3f147372004-05-22 01:09:26 +00002141#endif
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002142 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002143 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2144 /* might be worth duplicate entry points and not copy */
2145 if (dup == NULL)
2146 dup = xmlStrndup(value, valueend - value);
2147 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2148 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2149 if (dup == NULL)
2150 dup = xmlStrndup(value, valueend - value);
2151 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002152 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002153 }
2154 if (dup != NULL)
2155 xmlFree(dup);
2156}
2157
2158/**
2159 * xmlSAX2StartElementNs:
2160 * @ctx: the user data (XML parser context)
2161 * @localname: the local name of the element
2162 * @prefix: the element namespace prefix if available
2163 * @URI: the element namespace name if available
2164 * @nb_namespaces: number of namespace definitions on that node
2165 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2166 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002167 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002168 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2169 * attribute values.
2170 *
2171 * SAX2 callback when an element start has been detected by the parser.
2172 * It provides the namespace informations for the element, as well as
2173 * the new namespace declarations on the element.
2174 */
2175void
2176xmlSAX2StartElementNs(void *ctx,
2177 const xmlChar *localname,
2178 const xmlChar *prefix,
2179 const xmlChar *URI,
2180 int nb_namespaces,
2181 const xmlChar **namespaces,
2182 int nb_attributes,
2183 int nb_defaulted,
2184 const xmlChar **attributes)
2185{
2186 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2187 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002188 xmlNodePtr parent;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002189 xmlNsPtr last = NULL, ns;
2190 const xmlChar *uri, *pref;
Daniel Veillard77b77b12012-01-26 19:11:02 +08002191 xmlChar *lname = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002192 int i, j;
2193
Daniel Veillard34099b42004-11-04 17:34:35 +00002194 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002195 parent = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002196 /*
2197 * First check on validity:
2198 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002199 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002200 ((ctxt->myDoc->intSubset == NULL) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002201 ((ctxt->myDoc->intSubset->notations == NULL) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002202 (ctxt->myDoc->intSubset->elements == NULL) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002203 (ctxt->myDoc->intSubset->attributes == NULL) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002204 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00002205 xmlErrValid(ctxt, XML_ERR_NO_DTD,
2206 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002207 ctxt->validate = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002208 }
2209
Daniel Veillard8a44e592003-09-15 14:50:06 +00002210 /*
Daniel Veillard77b77b12012-01-26 19:11:02 +08002211 * Take care of the rare case of an undefined namespace prefix
2212 */
2213 if ((prefix != NULL) && (URI == NULL)) {
2214 if (ctxt->dictNames) {
2215 const xmlChar *fullname;
2216
2217 fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
2218 if (fullname != NULL)
2219 localname = fullname;
2220 } else {
2221 lname = xmlBuildQName(localname, prefix, NULL, 0);
2222 }
2223 }
2224 /*
Daniel Veillard8a44e592003-09-15 14:50:06 +00002225 * allocate the node
2226 */
2227 if (ctxt->freeElems != NULL) {
2228 ret = ctxt->freeElems;
2229 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00002230 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00002231 memset(ret, 0, sizeof(xmlNode));
2232 ret->type = XML_ELEMENT_NODE;
2233
2234 if (ctxt->dictNames)
2235 ret->name = localname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002236 else {
Daniel Veillard77b77b12012-01-26 19:11:02 +08002237 if (lname == NULL)
2238 ret->name = xmlStrdup(localname);
2239 else
2240 ret->name = lname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002241 if (ret->name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002242 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002243 return;
2244 }
2245 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002246 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2247 xmlRegisterNodeDefaultValue(ret);
2248 } else {
2249 if (ctxt->dictNames)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002250 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
Daniel Veillard8a44e592003-09-15 14:50:06 +00002251 (xmlChar *) localname, NULL);
Daniel Veillard77b77b12012-01-26 19:11:02 +08002252 else if (lname == NULL)
Daniel Veillard8a44e592003-09-15 14:50:06 +00002253 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
Daniel Veillard77b77b12012-01-26 19:11:02 +08002254 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002255 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
Daniel Veillard77b77b12012-01-26 19:11:02 +08002256 (xmlChar *) lname, NULL);
Daniel Veillard8a44e592003-09-15 14:50:06 +00002257 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002258 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002259 return;
2260 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002261 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002262 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00002263 if (ctxt->input != NULL) {
2264 if (ctxt->input->line < 65535)
2265 ret->line = (short) ctxt->input->line;
2266 else
2267 ret->line = 65535;
2268 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002269 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002270
Tim Elliott71a243d2012-01-17 19:25:08 -08002271 if (parent == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002272 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002273 }
2274 /*
2275 * Build the namespace list
2276 */
2277 for (i = 0,j = 0;j < nb_namespaces;j++) {
2278 pref = namespaces[i++];
2279 uri = namespaces[i++];
2280 ns = xmlNewNs(NULL, uri, pref);
2281 if (ns != NULL) {
2282 if (last == NULL) {
2283 ret->nsDef = last = ns;
2284 } else {
2285 last->next = ns;
2286 last = ns;
2287 }
2288 if ((URI != NULL) && (prefix == pref))
2289 ret->ns = ns;
2290 } else {
Daniel Veillardaa54d372010-09-09 18:17:47 +02002291 /*
2292 * any out of memory error would already have been raised
2293 * but we can't be garanteed it's the actual error due to the
2294 * API, best is to skip in this case
2295 */
2296 continue;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002297 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002298#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002299 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2300 ctxt->myDoc && ctxt->myDoc->intSubset) {
2301 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2302 ret, prefix, ns, uri);
2303 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002304#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002305 }
2306 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002307
2308 /*
2309 * We are parsing a new node.
2310 */
2311 nodePush(ctxt, ret);
2312
2313 /*
2314 * Link the child element
2315 */
2316 if (parent != NULL) {
2317 if (parent->type == XML_ELEMENT_NODE) {
2318 xmlAddChild(parent, ret);
2319 } else {
2320 xmlAddSibling(parent, ret);
2321 }
2322 }
2323
2324 /*
2325 * Insert the defaulted attributes from the DTD only if requested:
2326 */
2327 if ((nb_defaulted != 0) &&
2328 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2329 nb_attributes -= nb_defaulted;
2330
2331 /*
2332 * Search the namespace if it wasn't already found
William M. Brackbf5cf212004-08-31 06:47:17 +00002333 * Note that, if prefix is NULL, this searches for the default Ns
Daniel Veillarde57ec792003-09-10 10:50:59 +00002334 */
2335 if ((URI != NULL) && (ret->ns == NULL)) {
William M. Brackbf5cf212004-08-31 06:47:17 +00002336 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002337 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2338 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2339 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002340 if (ret->ns == NULL) {
2341 ns = xmlNewNs(ret, NULL, prefix);
William M. Brack9f797ab2004-07-28 07:40:12 +00002342 if (ns == NULL) {
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002343
William M. Brack42331a92004-07-29 07:07:16 +00002344 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002345 return;
2346 }
Daniel Veillard74eaec12009-08-26 15:57:20 +02002347 if (prefix != NULL)
2348 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2349 "Namespace prefix %s was not found\n",
2350 prefix, NULL);
2351 else
2352 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2353 "Namespace default prefix was not found\n",
2354 NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002355 }
2356 }
2357
2358 /*
2359 * process all the other attributes
2360 */
2361 if (nb_attributes > 0) {
2362 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
Daniel Veillard1c989272012-01-26 19:43:06 +08002363 /*
2364 * Handle the rare case of an undefined atribute prefix
2365 */
2366 if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
2367 if (ctxt->dictNames) {
2368 const xmlChar *fullname;
2369
2370 fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
2371 attributes[j]);
2372 if (fullname != NULL) {
2373 xmlSAX2AttributeNs(ctxt, fullname, NULL,
2374 attributes[j+3], attributes[j+4]);
2375 continue;
2376 }
2377 } else {
2378 lname = xmlBuildQName(attributes[j], attributes[j+1],
2379 NULL, 0);
2380 if (lname != NULL) {
2381 xmlSAX2AttributeNs(ctxt, lname, NULL,
2382 attributes[j+3], attributes[j+4]);
2383 xmlFree(lname);
2384 continue;
2385 }
2386 }
2387 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002388 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
Daniel Veillard1c989272012-01-26 19:43:06 +08002389 attributes[j+3], attributes[j+4]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002390 }
2391 }
2392
Daniel Veillard4432df22003-09-28 18:58:27 +00002393#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002394 /*
2395 * If it's the Document root, finish the DTD validation and
2396 * check the document root element for validity
2397 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00002398 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002399 int chk;
2400
2401 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2402 if (chk <= 0)
2403 ctxt->valid = 0;
2404 if (chk < 0)
2405 ctxt->wellFormed = 0;
2406 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00002407 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002408 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002409#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002410}
2411
2412/**
2413 * xmlSAX2EndElementNs:
2414 * @ctx: the user data (XML parser context)
2415 * @localname: the local name of the element
2416 * @prefix: the element namespace prefix if available
2417 * @URI: the element namespace name if available
2418 *
2419 * SAX2 callback when an element end has been detected by the parser.
2420 * It provides the namespace informations for the element.
2421 */
2422void
2423xmlSAX2EndElementNs(void *ctx,
2424 const xmlChar * localname ATTRIBUTE_UNUSED,
2425 const xmlChar * prefix ATTRIBUTE_UNUSED,
2426 const xmlChar * URI ATTRIBUTE_UNUSED)
2427{
2428 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2429 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002430 xmlNodePtr cur;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002431
Daniel Veillard34099b42004-11-04 17:34:35 +00002432 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002433 cur = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002434 /* Capture end position and add node */
2435 if ((ctxt->record_info) && (cur != NULL)) {
2436 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2437 node_info.end_line = ctxt->input->line;
2438 node_info.node = cur;
2439 xmlParserAddNodeInfo(ctxt, &node_info);
2440 }
2441 ctxt->nodemem = -1;
2442
Daniel Veillard4432df22003-09-28 18:58:27 +00002443#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002444 if (ctxt->validate && ctxt->wellFormed &&
2445 ctxt->myDoc && ctxt->myDoc->intSubset)
2446 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002447#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002448
2449 /*
2450 * end of parsing of this node.
2451 */
2452 nodePop(ctxt);
2453}
2454
Daniel Veillard1af9a412003-08-20 22:54:39 +00002455/**
2456 * xmlSAX2Reference:
2457 * @ctx: the user data (XML parser context)
2458 * @name: The entity name
2459 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002460 * called when an entity xmlSAX2Reference is detected.
Daniel Veillard1af9a412003-08-20 22:54:39 +00002461 */
2462void
2463xmlSAX2Reference(void *ctx, const xmlChar *name)
2464{
2465 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2466 xmlNodePtr ret;
2467
Daniel Veillard34099b42004-11-04 17:34:35 +00002468 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002469#ifdef DEBUG_SAX
2470 xmlGenericError(xmlGenericErrorContext,
2471 "SAX.xmlSAX2Reference(%s)\n", name);
2472#endif
2473 if (name[0] == '#')
2474 ret = xmlNewCharRef(ctxt->myDoc, name);
2475 else
2476 ret = xmlNewReference(ctxt->myDoc, name);
2477#ifdef DEBUG_SAX_TREE
2478 xmlGenericError(xmlGenericErrorContext,
2479 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2480#endif
Daniel Veillardb242b082008-02-08 09:56:31 +00002481 if (xmlAddChild(ctxt->node, ret) == NULL) {
2482 xmlFreeNode(ret);
2483 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002484}
2485
2486/**
2487 * xmlSAX2Characters:
2488 * @ctx: the user data (XML parser context)
2489 * @ch: a xmlChar string
2490 * @len: the number of xmlChar
2491 *
2492 * receiving some chars from the parser.
2493 */
2494void
2495xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2496{
2497 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2498 xmlNodePtr lastChild;
2499
Daniel Veillard34099b42004-11-04 17:34:35 +00002500 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002501#ifdef DEBUG_SAX
2502 xmlGenericError(xmlGenericErrorContext,
2503 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2504#endif
2505 /*
2506 * Handle the data if any. If there is no child
2507 * add it as content, otherwise if the last child is text,
2508 * concatenate it, else create a new node of type text.
2509 */
2510
2511 if (ctxt->node == NULL) {
2512#ifdef DEBUG_SAX_TREE
2513 xmlGenericError(xmlGenericErrorContext,
2514 "add chars: ctxt->node == NULL !\n");
2515#endif
2516 return;
2517 }
Daniel Veillard19895052003-09-17 13:59:32 +00002518 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002519#ifdef DEBUG_SAX_TREE
2520 xmlGenericError(xmlGenericErrorContext,
2521 "add chars to %s \n", ctxt->node->name);
2522#endif
2523
2524 /*
2525 * Here we needed an accelerator mechanism in case of very large
2526 * elements. Use an attribute in the structure !!!
2527 */
2528 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002529 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2530 if (lastChild != NULL) {
2531 ctxt->node->children = lastChild;
2532 ctxt->node->last = lastChild;
2533 lastChild->parent = ctxt->node;
2534 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002535 ctxt->nodelen = len;
2536 ctxt->nodemem = len + 1;
William M. Bracka3215c72004-07-31 16:24:01 +00002537 } else {
2538 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2539 return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002540 }
2541 } else {
2542 int coalesceText = (lastChild != NULL) &&
2543 (lastChild->type == XML_TEXT_NODE) &&
2544 (lastChild->name == xmlStringText);
2545 if ((coalesceText) && (ctxt->nodemem != 0)) {
2546 /*
2547 * The whole point of maintaining nodelen and nodemem,
2548 * xmlTextConcat is too costly, i.e. compute length,
2549 * reallocate a new buffer, move data, append ch. Here
2550 * We try to minimaze realloc() uses and avoid copying
2551 * and recomputing length over and over.
2552 */
Daniel Veillard8874b942005-08-25 13:19:21 +00002553 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2554 lastChild->content = xmlStrdup(lastChild->content);
2555 lastChild->properties = NULL;
2556 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2557 (xmlDictOwns(ctxt->dict, lastChild->content))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002558 lastChild->content = xmlStrdup(lastChild->content);
2559 }
Daniel Veillard97ff9b32009-01-18 21:43:30 +00002560 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002561 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2562 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2563 return;
2564 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002565 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002566 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002567 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2568 return;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002569 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002570 if (ctxt->nodelen + len >= ctxt->nodemem) {
2571 xmlChar *newbuf;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002572 size_t size;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002573
2574 size = ctxt->nodemem + len;
2575 size *= 2;
2576 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2577 if (newbuf == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002578 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002579 return;
2580 }
2581 ctxt->nodemem = size;
2582 lastChild->content = newbuf;
2583 }
2584 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2585 ctxt->nodelen += len;
2586 lastChild->content[ctxt->nodelen] = 0;
2587 } else if (coalesceText) {
2588 if (xmlTextConcat(lastChild, ch, len)) {
William M. Brack42331a92004-07-29 07:07:16 +00002589 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002590 }
2591 if (ctxt->node->children != NULL) {
2592 ctxt->nodelen = xmlStrlen(lastChild->content);
2593 ctxt->nodemem = ctxt->nodelen + 1;
2594 }
2595 } else {
2596 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002597 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2598 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002599 xmlAddChild(ctxt->node, lastChild);
2600 if (ctxt->node->children != NULL) {
2601 ctxt->nodelen = len;
2602 ctxt->nodemem = len + 1;
2603 }
2604 }
2605 }
2606 }
2607}
2608
2609/**
2610 * xmlSAX2IgnorableWhitespace:
2611 * @ctx: the user data (XML parser context)
2612 * @ch: a xmlChar string
2613 * @len: the number of xmlChar
2614 *
2615 * receiving some ignorable whitespaces from the parser.
2616 * UNUSED: by default the DOM building will use xmlSAX2Characters
2617 */
2618void
2619xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2620{
2621 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2622#ifdef DEBUG_SAX
2623 xmlGenericError(xmlGenericErrorContext,
2624 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2625#endif
2626}
2627
2628/**
2629 * xmlSAX2ProcessingInstruction:
2630 * @ctx: the user data (XML parser context)
2631 * @target: the target name
2632 * @data: the PI data's
2633 *
2634 * A processing instruction has been parsed.
2635 */
2636void
2637xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2638 const xmlChar *data)
2639{
2640 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2641 xmlNodePtr ret;
Daniel Veillard6128c012004-11-08 17:16:15 +00002642 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002643
Daniel Veillard34099b42004-11-04 17:34:35 +00002644 if (ctx == NULL) return;
Daniel Veillard6128c012004-11-08 17:16:15 +00002645 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002646#ifdef DEBUG_SAX
2647 xmlGenericError(xmlGenericErrorContext,
2648 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2649#endif
2650
Daniel Veillard03a53c32004-10-26 16:06:51 +00002651 ret = xmlNewDocPI(ctxt->myDoc, target, data);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002652 if (ret == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002653
Daniel Veillard73da77e2005-08-24 14:05:37 +00002654 if (ctxt->linenumbers) {
2655 if (ctxt->input != NULL) {
2656 if (ctxt->input->line < 65535)
2657 ret->line = (short) ctxt->input->line;
2658 else
2659 ret->line = 65535;
2660 }
2661 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002662 if (ctxt->inSubset == 1) {
2663 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2664 return;
2665 } else if (ctxt->inSubset == 2) {
2666 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2667 return;
2668 }
Tim Elliott71a243d2012-01-17 19:25:08 -08002669 if (parent == NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002670#ifdef DEBUG_SAX_TREE
2671 xmlGenericError(xmlGenericErrorContext,
2672 "Setting PI %s as root\n", target);
2673#endif
2674 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2675 return;
2676 }
2677 if (parent->type == XML_ELEMENT_NODE) {
2678#ifdef DEBUG_SAX_TREE
2679 xmlGenericError(xmlGenericErrorContext,
2680 "adding PI %s child to %s\n", target, parent->name);
2681#endif
2682 xmlAddChild(parent, ret);
2683 } else {
2684#ifdef DEBUG_SAX_TREE
2685 xmlGenericError(xmlGenericErrorContext,
2686 "adding PI %s sibling to ", target);
2687 xmlDebugDumpOneNode(stderr, parent, 0);
2688#endif
2689 xmlAddSibling(parent, ret);
2690 }
2691}
2692
2693/**
2694 * xmlSAX2Comment:
2695 * @ctx: the user data (XML parser context)
2696 * @value: the xmlSAX2Comment content
2697 *
2698 * A xmlSAX2Comment has been parsed.
2699 */
2700void
2701xmlSAX2Comment(void *ctx, const xmlChar *value)
2702{
2703 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2704 xmlNodePtr ret;
Daniel Veillard34099b42004-11-04 17:34:35 +00002705 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002706
Daniel Veillard34099b42004-11-04 17:34:35 +00002707 if (ctx == NULL) return;
2708 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002709#ifdef DEBUG_SAX
2710 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2711#endif
2712 ret = xmlNewDocComment(ctxt->myDoc, value);
2713 if (ret == NULL) return;
Daniel Veillard73da77e2005-08-24 14:05:37 +00002714 if (ctxt->linenumbers) {
2715 if (ctxt->input != NULL) {
2716 if (ctxt->input->line < 65535)
2717 ret->line = (short) ctxt->input->line;
2718 else
2719 ret->line = 65535;
2720 }
2721 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002722
2723 if (ctxt->inSubset == 1) {
2724 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2725 return;
2726 } else if (ctxt->inSubset == 2) {
2727 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2728 return;
2729 }
Tim Elliott71a243d2012-01-17 19:25:08 -08002730 if (parent == NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002731#ifdef DEBUG_SAX_TREE
2732 xmlGenericError(xmlGenericErrorContext,
2733 "Setting xmlSAX2Comment as root\n");
2734#endif
2735 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2736 return;
2737 }
2738 if (parent->type == XML_ELEMENT_NODE) {
2739#ifdef DEBUG_SAX_TREE
2740 xmlGenericError(xmlGenericErrorContext,
2741 "adding xmlSAX2Comment child to %s\n", parent->name);
2742#endif
2743 xmlAddChild(parent, ret);
2744 } else {
2745#ifdef DEBUG_SAX_TREE
2746 xmlGenericError(xmlGenericErrorContext,
2747 "adding xmlSAX2Comment sibling to ");
2748 xmlDebugDumpOneNode(stderr, parent, 0);
2749#endif
2750 xmlAddSibling(parent, ret);
2751 }
2752}
2753
2754/**
2755 * xmlSAX2CDataBlock:
2756 * @ctx: the user data (XML parser context)
2757 * @value: The pcdata content
2758 * @len: the block length
2759 *
2760 * called when a pcdata block has been parsed
2761 */
2762void
2763xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2764{
2765 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2766 xmlNodePtr ret, lastChild;
2767
Daniel Veillard34099b42004-11-04 17:34:35 +00002768 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002769#ifdef DEBUG_SAX
2770 xmlGenericError(xmlGenericErrorContext,
2771 "SAX.pcdata(%.10s, %d)\n", value, len);
2772#endif
2773 lastChild = xmlGetLastChild(ctxt->node);
2774#ifdef DEBUG_SAX_TREE
2775 xmlGenericError(xmlGenericErrorContext,
2776 "add chars to %s \n", ctxt->node->name);
2777#endif
2778 if ((lastChild != NULL) &&
2779 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2780 xmlTextConcat(lastChild, value, len);
2781 } else {
2782 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2783 xmlAddChild(ctxt->node, ret);
2784 }
2785}
2786
Daniel Veillard62998c02003-09-15 12:56:36 +00002787static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002788
Daniel Veillard81273902003-09-30 00:43:48 +00002789#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002790/**
2791 * xmlSAXDefaultVersion:
2792 * @version: the version, 1 or 2
2793 *
2794 * Set the default version of SAX used globally by the library.
William M. Brack96d2eff2004-06-30 11:48:47 +00002795 * By default, during initialization the default is set to 2.
2796 * Note that it is generally a better coding style to use
2797 * xmlSAXVersion() to set up the version explicitly for a given
2798 * parsing context.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002799 *
2800 * Returns the previous value in case of success and -1 in case of error.
2801 */
2802int
2803xmlSAXDefaultVersion(int version)
2804{
2805 int ret = xmlSAX2DefaultVersionValue;
2806
2807 if ((version != 1) && (version != 2))
2808 return(-1);
2809 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002810 return(ret);
2811}
Daniel Veillard81273902003-09-30 00:43:48 +00002812#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002813
2814/**
2815 * xmlSAXVersion:
2816 * @hdlr: the SAX handler
2817 * @version: the version, 1 or 2
2818 *
2819 * Initialize the default XML SAX handler according to the version
2820 *
2821 * Returns 0 in case of success and -1 in case of error.
2822 */
2823int
2824xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2825{
2826 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002827 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002828 hdlr->startElement = NULL;
2829 hdlr->endElement = NULL;
2830 hdlr->startElementNs = xmlSAX2StartElementNs;
2831 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002832 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002833 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002834#ifdef LIBXML_SAX1_ENABLED
2835 } else if (version == 1) {
2836 hdlr->startElement = xmlSAX2StartElement;
2837 hdlr->endElement = xmlSAX2EndElement;
2838 hdlr->initialized = 1;
2839#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002840 } else
2841 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002842 hdlr->internalSubset = xmlSAX2InternalSubset;
2843 hdlr->externalSubset = xmlSAX2ExternalSubset;
2844 hdlr->isStandalone = xmlSAX2IsStandalone;
2845 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2846 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2847 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2848 hdlr->getEntity = xmlSAX2GetEntity;
2849 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2850 hdlr->entityDecl = xmlSAX2EntityDecl;
2851 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2852 hdlr->elementDecl = xmlSAX2ElementDecl;
2853 hdlr->notationDecl = xmlSAX2NotationDecl;
2854 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2855 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2856 hdlr->startDocument = xmlSAX2StartDocument;
2857 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002858 hdlr->reference = xmlSAX2Reference;
2859 hdlr->characters = xmlSAX2Characters;
2860 hdlr->cdataBlock = xmlSAX2CDataBlock;
2861 hdlr->ignorableWhitespace = xmlSAX2Characters;
2862 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2863 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002864 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002865 hdlr->error = xmlParserError;
2866 hdlr->fatalError = xmlParserError;
2867
Daniel Veillarde57ec792003-09-10 10:50:59 +00002868 return(0);
2869}
2870
2871/**
2872 * xmlSAX2InitDefaultSAXHandler:
2873 * @hdlr: the SAX handler
2874 * @warning: flag if non-zero sets the handler warning procedure
2875 *
2876 * Initialize the default XML SAX2 handler
2877 */
2878void
2879xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2880{
2881 if ((hdlr == NULL) || (hdlr->initialized != 0))
2882 return;
2883
2884 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2885 if (warning == 0)
2886 hdlr->warning = NULL;
2887 else
2888 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002889}
2890
2891/**
2892 * xmlDefaultSAXHandlerInit:
2893 *
2894 * Initialize the default SAX2 handler
2895 */
2896void
2897xmlDefaultSAXHandlerInit(void)
2898{
Daniel Veillard81273902003-09-30 00:43:48 +00002899#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002900 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002901#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002902}
2903
2904#ifdef LIBXML_HTML_ENABLED
2905
2906/**
2907 * xmlSAX2InitHtmlDefaultSAXHandler:
2908 * @hdlr: the SAX handler
2909 *
2910 * Initialize the default HTML SAX2 handler
2911 */
2912void
2913xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2914{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002915 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002916 return;
2917
2918 hdlr->internalSubset = xmlSAX2InternalSubset;
2919 hdlr->externalSubset = NULL;
2920 hdlr->isStandalone = NULL;
2921 hdlr->hasInternalSubset = NULL;
2922 hdlr->hasExternalSubset = NULL;
2923 hdlr->resolveEntity = NULL;
2924 hdlr->getEntity = xmlSAX2GetEntity;
2925 hdlr->getParameterEntity = NULL;
2926 hdlr->entityDecl = NULL;
2927 hdlr->attributeDecl = NULL;
2928 hdlr->elementDecl = NULL;
2929 hdlr->notationDecl = NULL;
2930 hdlr->unparsedEntityDecl = NULL;
2931 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2932 hdlr->startDocument = xmlSAX2StartDocument;
2933 hdlr->endDocument = xmlSAX2EndDocument;
2934 hdlr->startElement = xmlSAX2StartElement;
2935 hdlr->endElement = xmlSAX2EndElement;
2936 hdlr->reference = NULL;
2937 hdlr->characters = xmlSAX2Characters;
2938 hdlr->cdataBlock = xmlSAX2CDataBlock;
2939 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002940 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002941 hdlr->comment = xmlSAX2Comment;
2942 hdlr->warning = xmlParserWarning;
2943 hdlr->error = xmlParserError;
2944 hdlr->fatalError = xmlParserError;
2945
Daniel Veillard092643b2003-09-25 14:29:29 +00002946 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002947}
2948
2949/**
2950 * htmlDefaultSAXHandlerInit:
2951 *
2952 * Initialize the default SAX handler
2953 */
2954void
2955htmlDefaultSAXHandlerInit(void)
2956{
Daniel Veillard092643b2003-09-25 14:29:29 +00002957 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002958}
2959
2960#endif /* LIBXML_HTML_ENABLED */
2961
2962#ifdef LIBXML_DOCB_ENABLED
2963
2964/**
2965 * xmlSAX2InitDocbDefaultSAXHandler:
2966 * @hdlr: the SAX handler
2967 *
2968 * Initialize the default DocBook SAX2 handler
2969 */
2970void
2971xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2972{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002973 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002974 return;
2975
2976 hdlr->internalSubset = xmlSAX2InternalSubset;
2977 hdlr->externalSubset = NULL;
2978 hdlr->isStandalone = xmlSAX2IsStandalone;
2979 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2980 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2981 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2982 hdlr->getEntity = xmlSAX2GetEntity;
2983 hdlr->getParameterEntity = NULL;
2984 hdlr->entityDecl = xmlSAX2EntityDecl;
2985 hdlr->attributeDecl = NULL;
2986 hdlr->elementDecl = NULL;
2987 hdlr->notationDecl = NULL;
2988 hdlr->unparsedEntityDecl = NULL;
2989 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2990 hdlr->startDocument = xmlSAX2StartDocument;
2991 hdlr->endDocument = xmlSAX2EndDocument;
2992 hdlr->startElement = xmlSAX2StartElement;
2993 hdlr->endElement = xmlSAX2EndElement;
2994 hdlr->reference = xmlSAX2Reference;
2995 hdlr->characters = xmlSAX2Characters;
2996 hdlr->cdataBlock = NULL;
2997 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2998 hdlr->processingInstruction = NULL;
2999 hdlr->comment = xmlSAX2Comment;
3000 hdlr->warning = xmlParserWarning;
3001 hdlr->error = xmlParserError;
3002 hdlr->fatalError = xmlParserError;
3003
Daniel Veillardffbbed42003-10-10 14:46:54 +00003004 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00003005}
3006
3007/**
3008 * docbDefaultSAXHandlerInit:
3009 *
3010 * Initialize the default SAX handler
3011 */
3012void
3013docbDefaultSAXHandlerInit(void)
3014{
Daniel Veillard092643b2003-09-25 14:29:29 +00003015 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00003016}
3017
3018#endif /* LIBXML_DOCB_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003019#define bottom_SAX2
3020#include "elfgcchack.h"