blob: c7ecd7f364c2a24e59a7452ef9ac95c9838a0133 [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;
Daniel Veillarda3f1e3e2013-03-11 13:57:53 +0800594 unsigned long oldnbent = ctxt->nbentities;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000595
596 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
597 ret->ExternalID, &children);
598 if (val == 0) {
599 xmlAddChildList((xmlNodePtr) ret, children);
600 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000601 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
602 "Failure to process entity %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000603 ctxt->validate = 0;
604 return(NULL);
605 }
606 ret->owner = 1;
Daniel Veillarda3f1e3e2013-03-11 13:57:53 +0800607 if (ret->checked == 0) {
Daniel Veillardcff25462013-03-11 15:57:55 +0800608 ret->checked = (ctxt->nbentities - oldnbent + 1) * 2;
609 if ((ret->content != NULL) && (xmlStrchr(ret->content, '<')))
610 ret->checked |= 1;
Daniel Veillarda3f1e3e2013-03-11 13:57:53 +0800611 }
Daniel Veillard1af9a412003-08-20 22:54:39 +0000612 }
613 return(ret);
614}
615
616/**
617 * xmlSAX2GetParameterEntity:
618 * @ctx: the user data (XML parser context)
619 * @name: The entity name
620 *
621 * Get a parameter entity by name
622 *
623 * Returns the xmlEntityPtr if found.
624 */
625xmlEntityPtr
626xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
627{
628 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
629 xmlEntityPtr ret;
630
Daniel Veillard34099b42004-11-04 17:34:35 +0000631 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000632#ifdef DEBUG_SAX
633 xmlGenericError(xmlGenericErrorContext,
634 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
635#endif
636
637 ret = xmlGetParameterEntity(ctxt->myDoc, name);
638 return(ret);
639}
640
641
642/**
643 * xmlSAX2EntityDecl:
644 * @ctx: the user data (XML parser context)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800645 * @name: the entity name
646 * @type: the entity type
Daniel Veillard1af9a412003-08-20 22:54:39 +0000647 * @publicId: The public ID of the entity
648 * @systemId: The system ID of the entity
649 * @content: the entity value (without processing).
650 *
651 * An entity definition has been parsed
652 */
653void
654xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
655 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
656{
657 xmlEntityPtr ent;
658 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
659
Daniel Veillard34099b42004-11-04 17:34:35 +0000660 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000661#ifdef DEBUG_SAX
662 xmlGenericError(xmlGenericErrorContext,
663 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
664 name, type, publicId, systemId, content);
665#endif
666 if (ctxt->inSubset == 1) {
667 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
668 systemId, content);
Daniel Veillard87b30462005-07-05 14:04:36 +0000669 if ((ent == NULL) && (ctxt->pedantic))
670 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
671 "Entity(%s) already defined in the internal subset\n",
672 name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000673 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
674 xmlChar *URI;
675 const char *base = NULL;
676
677 if (ctxt->input != NULL)
678 base = ctxt->input->filename;
679 if (base == NULL)
680 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800681
Daniel Veillard1af9a412003-08-20 22:54:39 +0000682 URI = xmlBuildURI(systemId, (const xmlChar *) base);
683 ent->URI = URI;
684 }
685 } else if (ctxt->inSubset == 2) {
686 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
687 systemId, content);
688 if ((ent == NULL) && (ctxt->pedantic) &&
689 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800690 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000691 "Entity(%s) already defined in the external subset\n", name);
692 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
693 xmlChar *URI;
694 const char *base = NULL;
695
696 if (ctxt->input != NULL)
697 base = ctxt->input->filename;
698 if (base == NULL)
699 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800700
Daniel Veillard1af9a412003-08-20 22:54:39 +0000701 URI = xmlBuildURI(systemId, (const xmlChar *) base);
702 ent->URI = URI;
703 }
704 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000705 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
706 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
707 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000708 }
709}
710
711/**
712 * xmlSAX2AttributeDecl:
713 * @ctx: the user data (XML parser context)
714 * @elem: the name of the element
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800715 * @fullname: the attribute name
716 * @type: the attribute type
Daniel Veillard1af9a412003-08-20 22:54:39 +0000717 * @def: the type of default value
718 * @defaultValue: the attribute default value
719 * @tree: the tree of enumerated value set
720 *
721 * An attribute definition has been parsed
722 */
723void
724xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
725 int type, int def, const xmlChar *defaultValue,
726 xmlEnumerationPtr tree)
727{
728 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
729 xmlAttributePtr attr;
730 xmlChar *name = NULL, *prefix = NULL;
731
Daniel Veillard2728f842006-03-09 16:49:24 +0000732 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
733 return;
734
Daniel Veillard1af9a412003-08-20 22:54:39 +0000735#ifdef DEBUG_SAX
736 xmlGenericError(xmlGenericErrorContext,
737 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
738 elem, fullname, type, def, defaultValue);
739#endif
Daniel Veillard68cb4b22004-04-18 20:55:39 +0000740 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
741 (type != XML_ATTRIBUTE_ID)) {
742 /*
743 * Raise the error but keep the validity flag
744 */
745 int tmp = ctxt->valid;
746 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
747 "xml:id : attribute type should be ID\n", NULL, NULL);
748 ctxt->valid = tmp;
749 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000750 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000751 name = xmlSplitQName(ctxt, fullname, &prefix);
752 ctxt->vctxt.valid = 1;
753 if (ctxt->inSubset == 1)
754 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
755 name, prefix, (xmlAttributeType) type,
756 (xmlAttributeDefault) def, defaultValue, tree);
757 else if (ctxt->inSubset == 2)
758 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800759 name, prefix, (xmlAttributeType) type,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000760 (xmlAttributeDefault) def, defaultValue, tree);
761 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000762 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
763 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
764 name, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000765 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000766 return;
767 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000768#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000769 if (ctxt->vctxt.valid == 0)
770 ctxt->valid = 0;
771 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
Daniel Veillard2728f842006-03-09 16:49:24 +0000772 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000773 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
774 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000775#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000776 if (prefix != NULL)
777 xmlFree(prefix);
778 if (name != NULL)
779 xmlFree(name);
780}
781
782/**
783 * xmlSAX2ElementDecl:
784 * @ctx: the user data (XML parser context)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800785 * @name: the element name
786 * @type: the element type
Daniel Veillard1af9a412003-08-20 22:54:39 +0000787 * @content: the element value tree
788 *
789 * An element definition has been parsed
790 */
791void
792xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
793 xmlElementContentPtr content)
794{
795 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
796 xmlElementPtr elem = NULL;
797
Daniel Veillard2728f842006-03-09 16:49:24 +0000798 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
799 return;
800
Daniel Veillard1af9a412003-08-20 22:54:39 +0000801#ifdef DEBUG_SAX
802 xmlGenericError(xmlGenericErrorContext,
803 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
804#endif
805
806 if (ctxt->inSubset == 1)
807 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
808 name, (xmlElementTypeVal) type, content);
809 else if (ctxt->inSubset == 2)
810 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
811 name, (xmlElementTypeVal) type, content);
812 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000813 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
814 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
815 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000816 return;
817 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000818#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000819 if (elem == NULL)
820 ctxt->valid = 0;
821 if (ctxt->validate && ctxt->wellFormed &&
822 ctxt->myDoc && ctxt->myDoc->intSubset)
823 ctxt->valid &=
824 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +0000825#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000826}
827
828/**
829 * xmlSAX2NotationDecl:
830 * @ctx: the user data (XML parser context)
831 * @name: The name of the notation
832 * @publicId: The public ID of the entity
833 * @systemId: The system ID of the entity
834 *
835 * What to do when a notation declaration has been parsed.
836 */
837void
838xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
839 const xmlChar *publicId, const xmlChar *systemId)
840{
841 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
842 xmlNotationPtr nota = NULL;
843
Daniel Veillard2728f842006-03-09 16:49:24 +0000844 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
845 return;
846
Daniel Veillard1af9a412003-08-20 22:54:39 +0000847#ifdef DEBUG_SAX
848 xmlGenericError(xmlGenericErrorContext,
849 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
850#endif
851
852 if ((publicId == NULL) && (systemId == NULL)) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000853 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
854 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
855 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000856 return;
857 } else if (ctxt->inSubset == 1)
858 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
859 publicId, systemId);
860 else if (ctxt->inSubset == 2)
861 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
862 publicId, systemId);
863 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000864 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
865 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
866 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000867 return;
868 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000869#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000870 if (nota == NULL) ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000871 if ((ctxt->validate) && (ctxt->wellFormed) &&
872 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000873 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
874 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000875#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000876}
877
878/**
879 * xmlSAX2UnparsedEntityDecl:
880 * @ctx: the user data (XML parser context)
881 * @name: The name of the entity
882 * @publicId: The public ID of the entity
883 * @systemId: The system ID of the entity
884 * @notationName: the name of the notation
885 *
886 * What to do when an unparsed entity declaration is parsed
887 */
888void
889xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
890 const xmlChar *publicId, const xmlChar *systemId,
891 const xmlChar *notationName)
892{
893 xmlEntityPtr ent;
894 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000895 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000896#ifdef DEBUG_SAX
897 xmlGenericError(xmlGenericErrorContext,
898 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
899 name, publicId, systemId, notationName);
900#endif
901 if (ctxt->inSubset == 1) {
902 ent = xmlAddDocEntity(ctxt->myDoc, name,
903 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
904 publicId, systemId, notationName);
905 if ((ent == NULL) && (ctxt->pedantic) &&
906 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800907 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000908 "Entity(%s) already defined in the internal subset\n", name);
909 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
910 xmlChar *URI;
911 const char *base = NULL;
912
913 if (ctxt->input != NULL)
914 base = ctxt->input->filename;
915 if (base == NULL)
916 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800917
Daniel Veillard1af9a412003-08-20 22:54:39 +0000918 URI = xmlBuildURI(systemId, (const xmlChar *) base);
919 ent->URI = URI;
920 }
921 } else if (ctxt->inSubset == 2) {
922 ent = xmlAddDtdEntity(ctxt->myDoc, name,
923 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
924 publicId, systemId, notationName);
925 if ((ent == NULL) && (ctxt->pedantic) &&
926 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800927 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000928 "Entity(%s) already defined in the external subset\n", name);
929 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
930 xmlChar *URI;
931 const char *base = NULL;
932
933 if (ctxt->input != NULL)
934 base = ctxt->input->filename;
935 if (base == NULL)
936 base = ctxt->directory;
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800937
Daniel Veillard1af9a412003-08-20 22:54:39 +0000938 URI = xmlBuildURI(systemId, (const xmlChar *) base);
939 ent->URI = URI;
940 }
941 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000942 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
943 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
944 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000945 }
946}
947
948/**
949 * xmlSAX2SetDocumentLocator:
950 * @ctx: the user data (XML parser context)
951 * @loc: A SAX Locator
952 *
953 * Receive the document locator at startup, actually xmlDefaultSAXLocator
954 * Everything is available on the context, so this is useless in our case.
955 */
956void
957xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
958{
959 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
960#ifdef DEBUG_SAX
961 xmlGenericError(xmlGenericErrorContext,
962 "SAX.xmlSAX2SetDocumentLocator()\n");
963#endif
964}
965
966/**
967 * xmlSAX2StartDocument:
968 * @ctx: the user data (XML parser context)
969 *
970 * called when the document start being processed.
971 */
972void
973xmlSAX2StartDocument(void *ctx)
974{
975 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
976 xmlDocPtr doc;
977
Daniel Veillard34099b42004-11-04 17:34:35 +0000978 if (ctx == NULL) return;
979
Daniel Veillard1af9a412003-08-20 22:54:39 +0000980#ifdef DEBUG_SAX
981 xmlGenericError(xmlGenericErrorContext,
982 "SAX.xmlSAX2StartDocument()\n");
983#endif
984 if (ctxt->html) {
985#ifdef LIBXML_HTML_ENABLED
986 if (ctxt->myDoc == NULL)
987 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000988 ctxt->myDoc->properties = XML_DOC_HTML;
989 ctxt->myDoc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000990 if (ctxt->myDoc == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000991 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000992 return;
993 }
994#else
995 xmlGenericError(xmlGenericErrorContext,
996 "libxml2 built without HTML support\n");
997 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
998 ctxt->instate = XML_PARSER_EOF;
999 ctxt->disableSAX = 1;
1000 return;
1001#endif
1002 } else {
1003 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
1004 if (doc != NULL) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00001005 doc->properties = 0;
1006 if (ctxt->options & XML_PARSE_OLD10)
1007 doc->properties |= XML_DOC_OLD10;
1008 doc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001009 if (ctxt->encoding != NULL)
1010 doc->encoding = xmlStrdup(ctxt->encoding);
1011 else
1012 doc->encoding = NULL;
1013 doc->standalone = ctxt->standalone;
1014 } else {
William M. Brack42331a92004-07-29 07:07:16 +00001015 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001016 return;
1017 }
Daniel Veillard500a1de2004-03-22 15:22:58 +00001018 if ((ctxt->dictNames) && (doc != NULL)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001019 doc->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00001020 xmlDictReference(doc->dict);
1021 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001022 }
1023 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1024 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillardb8efdda2006-10-10 12:37:14 +00001025 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001026 if (ctxt->myDoc->URL == NULL)
William M. Bracka3215c72004-07-31 16:24:01 +00001027 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001028 }
1029}
1030
1031/**
1032 * xmlSAX2EndDocument:
1033 * @ctx: the user data (XML parser context)
1034 *
1035 * called when the document end has been detected.
1036 */
1037void
1038xmlSAX2EndDocument(void *ctx)
1039{
1040 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1041#ifdef DEBUG_SAX
1042 xmlGenericError(xmlGenericErrorContext,
1043 "SAX.xmlSAX2EndDocument()\n");
1044#endif
Daniel Veillard34099b42004-11-04 17:34:35 +00001045 if (ctx == NULL) return;
Daniel Veillard4432df22003-09-28 18:58:27 +00001046#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001047 if (ctxt->validate && ctxt->wellFormed &&
1048 ctxt->myDoc && ctxt->myDoc->intSubset)
1049 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00001050#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001051
1052 /*
1053 * Grab the encoding if it was added on-the-fly
1054 */
1055 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1056 (ctxt->myDoc->encoding == NULL)) {
1057 ctxt->myDoc->encoding = ctxt->encoding;
1058 ctxt->encoding = NULL;
1059 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001060 if ((ctxt->inputTab != NULL) &&
1061 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1062 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001063 (ctxt->myDoc->encoding == NULL)) {
1064 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1065 }
1066 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1067 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1068 ctxt->myDoc->charset = ctxt->charset;
1069 }
1070}
1071
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001072#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001073/**
1074 * xmlSAX2AttributeInternal:
1075 * @ctx: the user data (XML parser context)
1076 * @fullname: The attribute name, including namespace prefix
1077 * @value: The attribute value
1078 * @prefix: the prefix on the element node
1079 *
1080 * Handle an attribute that has been read by the parser.
1081 * The default handling is to convert the attribute into an
1082 * DOM subtree and past it in a new xmlAttr element added to
1083 * the element.
1084 */
1085static void
1086xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001087 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001088{
1089 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1090 xmlAttrPtr ret;
1091 xmlChar *name;
1092 xmlChar *ns;
1093 xmlChar *nval;
1094 xmlNsPtr namespace;
1095
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001096 if (ctxt->html) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001097 name = xmlStrdup(fullname);
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001098 ns = NULL;
1099 namespace = NULL;
1100 } else {
1101 /*
1102 * Split the full name into a namespace prefix and the tag name
1103 */
1104 name = xmlSplitQName(ctxt, fullname, &ns);
1105 if ((name != NULL) && (name[0] == 0)) {
1106 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1107 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1108 "invalid namespace declaration '%s'\n",
1109 fullname, NULL);
1110 } else {
1111 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1112 "Avoid attribute ending with ':' like '%s'\n",
1113 fullname, NULL);
1114 }
1115 if (ns != NULL)
1116 xmlFree(ns);
1117 ns = NULL;
1118 xmlFree(name);
1119 name = xmlStrdup(fullname);
1120 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001121 }
1122 if (name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001123 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001124 if (ns != NULL)
1125 xmlFree(ns);
1126 return;
1127 }
1128
Daniel Veillard3c080d62010-03-15 15:47:50 +01001129#ifdef LIBXML_HTML_ENABLED
1130 if ((ctxt->html) &&
1131 (value == NULL) && (htmlIsBooleanAttr(fullname))) {
Daniel Veillard06c93b72010-03-15 16:08:44 +01001132 nval = xmlStrdup(fullname);
1133 value = (const xmlChar *) nval;
Daniel Veillard3c080d62010-03-15 15:47:50 +01001134 } else
1135#endif
1136 {
Daniel Veillard4432df22003-09-28 18:58:27 +00001137#ifdef LIBXML_VALID_ENABLED
Daniel Veillard3c080d62010-03-15 15:47:50 +01001138 /*
1139 * Do the last stage of the attribute normalization
1140 * Needed for HTML too:
1141 * http://www.w3.org/TR/html4/types.html#h-6.2
1142 */
1143 ctxt->vctxt.valid = 1;
1144 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1145 ctxt->myDoc, ctxt->node,
1146 fullname, value);
1147 if (ctxt->vctxt.valid != 1) {
1148 ctxt->valid = 0;
1149 }
1150 if (nval != NULL)
1151 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +00001152#else
Daniel Veillard3c080d62010-03-15 15:47:50 +01001153 nval = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +00001154#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard3c080d62010-03-15 15:47:50 +01001155 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001156
1157 /*
1158 * Check whether it's a namespace definition
1159 */
1160 if ((!ctxt->html) && (ns == NULL) &&
1161 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1162 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1163 xmlNsPtr nsret;
1164 xmlChar *val;
1165
1166 if (!ctxt->replaceEntities) {
1167 ctxt->depth++;
1168 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1169 0,0,0);
1170 ctxt->depth--;
1171 } else {
1172 val = (xmlChar *) value;
1173 }
1174
1175 if (val[0] != 0) {
1176 xmlURIPtr uri;
1177
1178 uri = xmlParseURI((const char *)val);
1179 if (uri == NULL) {
1180 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001181 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +00001182 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001183 } else {
1184 if (uri->scheme == NULL) {
1185 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001186 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001187 "xmlns: URI %s is not absolute\n", val);
1188 }
1189 xmlFreeURI(uri);
1190 }
1191 }
1192
1193 /* a default namespace definition */
1194 nsret = xmlNewNs(ctxt->node, val, NULL);
1195
Daniel Veillard4432df22003-09-28 18:58:27 +00001196#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001197 /*
1198 * Validate also for namespace decls, they are attributes from
1199 * an XML-1.0 perspective
1200 */
1201 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1202 ctxt->myDoc && ctxt->myDoc->intSubset)
1203 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1204 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +00001205#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001206 if (name != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001207 xmlFree(name);
1208 if (nval != NULL)
1209 xmlFree(nval);
1210 if (val != value)
1211 xmlFree(val);
1212 return;
1213 }
1214 if ((!ctxt->html) &&
1215 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1216 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1217 xmlNsPtr nsret;
1218 xmlChar *val;
1219
1220 if (!ctxt->replaceEntities) {
1221 ctxt->depth++;
1222 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1223 0,0,0);
1224 ctxt->depth--;
1225 if (val == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001226 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001227 xmlFree(ns);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001228 if (name != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001229 xmlFree(name);
1230 return;
1231 }
1232 } else {
1233 val = (xmlChar *) value;
1234 }
1235
1236 if (val[0] == 0) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001237 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1238 "Empty namespace name for prefix %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001239 }
1240 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1241 xmlURIPtr uri;
1242
1243 uri = xmlParseURI((const char *)val);
1244 if (uri == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001245 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001246 "xmlns:%s: %s not a valid URI\n", name, value);
1247 } else {
1248 if (uri->scheme == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001249 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001250 "xmlns:%s: URI %s is not absolute\n", name, value);
1251 }
1252 xmlFreeURI(uri);
1253 }
1254 }
1255
1256 /* a standard namespace definition */
1257 nsret = xmlNewNs(ctxt->node, val, name);
1258 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001259#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001260 /*
1261 * Validate also for namespace decls, they are attributes from
1262 * an XML-1.0 perspective
1263 */
1264 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1265 ctxt->myDoc && ctxt->myDoc->intSubset)
1266 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1267 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001268#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001269 if (name != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001270 xmlFree(name);
1271 if (nval != NULL)
1272 xmlFree(nval);
1273 if (val != value)
1274 xmlFree(val);
1275 return;
1276 }
1277
1278 if (ns != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001279 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardd44b9362009-09-07 12:15:08 +02001280
Daniel Veillard67906942003-08-28 21:13:25 +00001281 if (namespace == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001282 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
William M. Brack4811ba32003-09-06 18:02:53 +00001283 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001284 ns, name);
Daniel Veillardd44b9362009-09-07 12:15:08 +02001285 } else {
1286 xmlAttrPtr prop;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001287
Daniel Veillardd44b9362009-09-07 12:15:08 +02001288 prop = ctxt->node->properties;
1289 while (prop != NULL) {
1290 if (prop->ns != NULL) {
1291 if ((xmlStrEqual(name, prop->name)) &&
1292 ((namespace == prop->ns) ||
1293 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1294 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1295 "Attribute %s in %s redefined\n",
1296 name, namespace->href);
1297 ctxt->wellFormed = 0;
1298 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1299 goto error;
1300 }
1301 }
1302 prop = prop->next;
1303 }
1304 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001305 } else {
1306 namespace = NULL;
1307 }
1308
1309 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1310 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1311
1312 if (ret != NULL) {
1313 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1314 xmlNodePtr tmp;
1315
1316 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1317 tmp = ret->children;
1318 while (tmp != NULL) {
1319 tmp->parent = (xmlNodePtr) ret;
1320 if (tmp->next == NULL)
1321 ret->last = tmp;
1322 tmp = tmp->next;
1323 }
1324 } else if (value != NULL) {
1325 ret->children = xmlNewDocText(ctxt->myDoc, value);
1326 ret->last = ret->children;
1327 if (ret->children != NULL)
1328 ret->children->parent = (xmlNodePtr) ret;
1329 }
1330 }
1331
Daniel Veillard4432df22003-09-28 18:58:27 +00001332#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001333 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1334 ctxt->myDoc && ctxt->myDoc->intSubset) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001335
Daniel Veillard1af9a412003-08-20 22:54:39 +00001336 /*
1337 * If we don't substitute entities, the validation should be
1338 * done on a value with replaced entities anyway.
1339 */
1340 if (!ctxt->replaceEntities) {
1341 xmlChar *val;
1342
1343 ctxt->depth++;
1344 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1345 0,0,0);
1346 ctxt->depth--;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001347
Daniel Veillard1af9a412003-08-20 22:54:39 +00001348 if (val == NULL)
1349 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1350 ctxt->myDoc, ctxt->node, ret, value);
1351 else {
1352 xmlChar *nvalnorm;
1353
1354 /*
1355 * Do the last stage of the attribute normalization
1356 * It need to be done twice ... it's an extra burden related
1357 * to the ability to keep xmlSAX2References in attributes
1358 */
1359 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1360 ctxt->node, fullname, val);
1361 if (nvalnorm != NULL) {
1362 xmlFree(val);
1363 val = nvalnorm;
1364 }
1365
1366 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1367 ctxt->myDoc, ctxt->node, ret, val);
1368 xmlFree(val);
1369 }
1370 } else {
1371 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1372 ctxt->node, ret, value);
1373 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001374 } else
1375#endif /* LIBXML_VALID_ENABLED */
1376 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001377 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1378 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1379 /*
1380 * when validating, the ID registration is done at the attribute
1381 * validation level. Otherwise we have to do specific handling here.
1382 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001383 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001384 /*
1385 * Add the xml:id value
1386 *
1387 * Open issue: normalization of the value.
1388 */
Daniel Veillard68cb4b22004-04-18 20:55:39 +00001389 if (xmlValidateNCName(value, 1) != 0) {
1390 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1391 "xml:id : attribute value %s is not an NCName\n",
1392 (const char *) value, NULL);
1393 }
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001394 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001395 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1396 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1397 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1398 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001399 }
1400
1401error:
1402 if (nval != NULL)
1403 xmlFree(nval);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001404 if (ns != NULL)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001405 xmlFree(ns);
1406}
1407
Daniel Veillard1af9a412003-08-20 22:54:39 +00001408/*
1409 * xmlCheckDefaultedAttributes:
1410 *
1411 * Check defaulted attributes from the DTD
1412 */
1413static void
1414xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1415 const xmlChar *prefix, const xmlChar **atts) {
1416 xmlElementPtr elemDecl;
1417 const xmlChar *att;
1418 int internal = 1;
1419 int i;
1420
1421 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1422 if (elemDecl == NULL) {
1423 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1424 internal = 0;
1425 }
1426
1427process_external_subset:
1428
1429 if (elemDecl != NULL) {
1430 xmlAttributePtr attr = elemDecl->attributes;
1431 /*
1432 * Check against defaulted attributes from the external subset
1433 * if the document is stamped as standalone
1434 */
1435 if ((ctxt->myDoc->standalone == 1) &&
1436 (ctxt->myDoc->extSubset != NULL) &&
1437 (ctxt->validate)) {
1438 while (attr != NULL) {
1439 if ((attr->defaultValue != NULL) &&
1440 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1441 attr->elem, attr->name,
1442 attr->prefix) == attr) &&
1443 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1444 attr->elem, attr->name,
1445 attr->prefix) == NULL)) {
1446 xmlChar *fulln;
1447
1448 if (attr->prefix != NULL) {
1449 fulln = xmlStrdup(attr->prefix);
1450 fulln = xmlStrcat(fulln, BAD_CAST ":");
1451 fulln = xmlStrcat(fulln, attr->name);
1452 } else {
1453 fulln = xmlStrdup(attr->name);
1454 }
Jim Meyering669e88c2009-07-29 11:33:32 +02001455 if (fulln == NULL) {
1456 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1457 break;
1458 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001459
1460 /*
1461 * Check that the attribute is not declared in the
1462 * serialization
1463 */
1464 att = NULL;
1465 if (atts != NULL) {
1466 i = 0;
1467 att = atts[i];
1468 while (att != NULL) {
1469 if (xmlStrEqual(att, fulln))
1470 break;
1471 i += 2;
1472 att = atts[i];
1473 }
1474 }
1475 if (att == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001476 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001477 "standalone: attribute %s on %s defaulted from external subset\n",
Daniel Veillard427174f2003-12-10 10:42:59 +00001478 (const char *)fulln,
1479 (const char *)attr->elem);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001480 }
Daniel Veillard023d0ba2009-07-29 11:34:50 +02001481 xmlFree(fulln);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001482 }
1483 attr = attr->nexth;
1484 }
1485 }
1486
1487 /*
1488 * Actually insert defaulted values when needed
1489 */
1490 attr = elemDecl->attributes;
1491 while (attr != NULL) {
1492 /*
1493 * Make sure that attributes redefinition occuring in the
1494 * internal subset are not overriden by definitions in the
1495 * external subset.
1496 */
1497 if (attr->defaultValue != NULL) {
1498 /*
1499 * the element should be instantiated in the tree if:
1500 * - this is a namespace prefix
1501 * - the user required for completion in the tree
1502 * like XSLT
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001503 * - there isn't already an attribute definition
Daniel Veillard1af9a412003-08-20 22:54:39 +00001504 * in the internal subset overriding it.
1505 */
1506 if (((attr->prefix != NULL) &&
1507 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1508 ((attr->prefix == NULL) &&
1509 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1510 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1511 xmlAttributePtr tst;
1512
1513 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1514 attr->elem, attr->name,
1515 attr->prefix);
1516 if ((tst == attr) || (tst == NULL)) {
1517 xmlChar fn[50];
1518 xmlChar *fulln;
1519
1520 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1521 if (fulln == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001522 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001523 return;
1524 }
1525
1526 /*
1527 * Check that the attribute is not declared in the
1528 * serialization
1529 */
1530 att = NULL;
1531 if (atts != NULL) {
1532 i = 0;
1533 att = atts[i];
1534 while (att != NULL) {
1535 if (xmlStrEqual(att, fulln))
1536 break;
1537 i += 2;
1538 att = atts[i];
1539 }
1540 }
1541 if (att == NULL) {
1542 xmlSAX2AttributeInternal(ctxt, fulln,
1543 attr->defaultValue, prefix);
1544 }
1545 if ((fulln != fn) && (fulln != attr->name))
1546 xmlFree(fulln);
1547 }
1548 }
1549 }
1550 attr = attr->nexth;
1551 }
1552 if (internal == 1) {
1553 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1554 name, prefix);
1555 internal = 0;
1556 goto process_external_subset;
1557 }
1558 }
1559}
1560
1561/**
1562 * xmlSAX2StartElement:
1563 * @ctx: the user data (XML parser context)
1564 * @fullname: The element name, including namespace prefix
1565 * @atts: An array of name/value attributes pairs, NULL terminated
1566 *
1567 * called when an opening tag has been processed.
1568 */
1569void
1570xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1571{
1572 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1573 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001574 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001575 xmlNsPtr ns;
1576 xmlChar *name;
1577 xmlChar *prefix;
1578 const xmlChar *att;
1579 const xmlChar *value;
1580 int i;
1581
Daniel Veillarda521d282004-11-09 14:59:59 +00001582 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001583 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001584#ifdef DEBUG_SAX
1585 xmlGenericError(xmlGenericErrorContext,
1586 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1587#endif
1588
1589 /*
1590 * First check on validity:
1591 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001592 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001593 ((ctxt->myDoc->intSubset == NULL) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001594 ((ctxt->myDoc->intSubset->notations == NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001595 (ctxt->myDoc->intSubset->elements == NULL) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001596 (ctxt->myDoc->intSubset->attributes == NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001597 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001598 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1599 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001600 ctxt->validate = 0;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001601 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001602
Daniel Veillard1af9a412003-08-20 22:54:39 +00001603
1604 /*
1605 * Split the full name into a namespace prefix and the tag name
1606 */
1607 name = xmlSplitQName(ctxt, fullname, &prefix);
1608
1609
1610 /*
1611 * Note : the namespace resolution is deferred until the end of the
1612 * attributes parsing, since local namespace can be defined as
1613 * an attribute at this level.
1614 */
1615 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1616 if (ret == NULL) {
1617 if (prefix != NULL)
1618 xmlFree(prefix);
William M. Brack42331a92004-07-29 07:07:16 +00001619 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001620 return;
1621 }
1622 if (ctxt->myDoc->children == NULL) {
1623#ifdef DEBUG_SAX_TREE
1624 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1625#endif
1626 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1627 } else if (parent == NULL) {
1628 parent = ctxt->myDoc->children;
1629 }
1630 ctxt->nodemem = -1;
1631 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001632 if (ctxt->input != NULL) {
1633 if (ctxt->input->line < 65535)
1634 ret->line = (short) ctxt->input->line;
1635 else
1636 ret->line = 65535;
1637 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001638 }
1639
1640 /*
1641 * We are parsing a new node.
1642 */
1643#ifdef DEBUG_SAX_TREE
1644 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1645#endif
1646 nodePush(ctxt, ret);
1647
1648 /*
1649 * Link the child element
1650 */
1651 if (parent != NULL) {
1652 if (parent->type == XML_ELEMENT_NODE) {
1653#ifdef DEBUG_SAX_TREE
1654 xmlGenericError(xmlGenericErrorContext,
1655 "adding child %s to %s\n", name, parent->name);
1656#endif
1657 xmlAddChild(parent, ret);
1658 } else {
1659#ifdef DEBUG_SAX_TREE
1660 xmlGenericError(xmlGenericErrorContext,
1661 "adding sibling %s to ", name);
1662 xmlDebugDumpOneNode(stderr, parent, 0);
1663#endif
1664 xmlAddSibling(parent, ret);
1665 }
1666 }
1667
1668 /*
1669 * Insert all the defaulted attributes from the DTD especially namespaces
1670 */
1671 if ((!ctxt->html) &&
1672 ((ctxt->myDoc->intSubset != NULL) ||
1673 (ctxt->myDoc->extSubset != NULL))) {
1674 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1675 }
1676
1677 /*
1678 * process all the attributes whose name start with "xmlns"
1679 */
1680 if (atts != NULL) {
1681 i = 0;
1682 att = atts[i++];
1683 value = atts[i++];
1684 if (!ctxt->html) {
1685 while ((att != NULL) && (value != NULL)) {
1686 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1687 (att[3] == 'n') && (att[4] == 's'))
1688 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1689
1690 att = atts[i++];
1691 value = atts[i++];
1692 }
1693 }
1694 }
1695
1696 /*
1697 * Search the namespace, note that since the attributes have been
1698 * processed, the local namespaces are available.
1699 */
1700 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1701 if ((ns == NULL) && (parent != NULL))
1702 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1703 if ((prefix != NULL) && (ns == NULL)) {
1704 ns = xmlNewNs(ret, NULL, prefix);
Daniel Veillard77aad342006-07-13 06:21:09 +00001705 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1706 "Namespace prefix %s is not defined\n",
1707 prefix, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001708 }
1709
1710 /*
1711 * set the namespace node, making sure that if the default namspace
1712 * is unbound on a parent we simply kee it NULL
1713 */
1714 if ((ns != NULL) && (ns->href != NULL) &&
1715 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1716 xmlSetNs(ret, ns);
1717
1718 /*
1719 * process all the other attributes
1720 */
1721 if (atts != NULL) {
1722 i = 0;
1723 att = atts[i++];
1724 value = atts[i++];
1725 if (ctxt->html) {
1726 while (att != NULL) {
1727 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1728 att = atts[i++];
1729 value = atts[i++];
1730 }
1731 } else {
1732 while ((att != NULL) && (value != NULL)) {
1733 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1734 (att[3] != 'n') || (att[4] != 's'))
1735 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1736
1737 /*
1738 * Next ones
1739 */
1740 att = atts[i++];
1741 value = atts[i++];
1742 }
1743 }
1744 }
1745
Daniel Veillard4432df22003-09-28 18:58:27 +00001746#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001747 /*
1748 * If it's the Document root, finish the DTD validation and
1749 * check the document root element for validity
1750 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00001751 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001752 int chk;
1753
1754 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1755 if (chk <= 0)
1756 ctxt->valid = 0;
1757 if (chk < 0)
1758 ctxt->wellFormed = 0;
1759 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00001760 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001761 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001762#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001763
1764 if (prefix != NULL)
1765 xmlFree(prefix);
1766
1767}
1768
1769/**
1770 * xmlSAX2EndElement:
1771 * @ctx: the user data (XML parser context)
1772 * @name: The element name
1773 *
1774 * called when the end of an element has been detected.
1775 */
1776void
1777xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1778{
1779 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001780 xmlNodePtr cur;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001781
Daniel Veillard34099b42004-11-04 17:34:35 +00001782 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001783 cur = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001784#ifdef DEBUG_SAX
1785 if (name == NULL)
1786 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1787 else
1788 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1789#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001790
Daniel Veillard1af9a412003-08-20 22:54:39 +00001791 /* Capture end position and add node */
1792 if (cur != NULL && ctxt->record_info) {
Pavel Andrejs8ad4da52012-05-08 11:01:12 +08001793 ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
1794 ctxt->nodeInfo->end_line = ctxt->input->line;
1795 ctxt->nodeInfo->node = cur;
1796 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001797 }
1798 ctxt->nodemem = -1;
1799
Daniel Veillard4432df22003-09-28 18:58:27 +00001800#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001801 if (ctxt->validate && ctxt->wellFormed &&
1802 ctxt->myDoc && ctxt->myDoc->intSubset)
1803 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1804 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001805#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001806
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001807
Daniel Veillard1af9a412003-08-20 22:54:39 +00001808 /*
1809 * end of parsing of this node.
1810 */
1811#ifdef DEBUG_SAX_TREE
1812 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1813#endif
1814 nodePop(ctxt);
1815}
Daniel Veillard81273902003-09-30 00:43:48 +00001816#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001817
Daniel Veillarde57ec792003-09-10 10:50:59 +00001818/*
Daniel Veillard19895052003-09-17 13:59:32 +00001819 * xmlSAX2TextNode:
1820 * @ctxt: the parser context
1821 * @str: the input string
1822 * @len: the string length
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001823 *
Daniel Veillard968a03a2012-08-13 12:41:33 +08001824 * Callback for a text node
Daniel Veillard19895052003-09-17 13:59:32 +00001825 *
1826 * Returns the newly allocated string or NULL if not needed or error
1827 */
1828static xmlNodePtr
1829xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1830 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001831 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001832
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001833 /*
1834 * Allocate
1835 */
Daniel Veillard19895052003-09-17 13:59:32 +00001836 if (ctxt->freeElems != NULL) {
1837 ret = ctxt->freeElems;
1838 ctxt->freeElems = ret->next;
1839 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001840 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001841 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001842 }
1843 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001844 xmlErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard19895052003-09-17 13:59:32 +00001845 return(NULL);
1846 }
Daniel Veillard8874b942005-08-25 13:19:21 +00001847 memset(ret, 0, sizeof(xmlNode));
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001848 /*
1849 * intern the formatting blanks found between tags, or the
1850 * very short strings
1851 */
1852 if (ctxt->dictNames) {
1853 xmlChar cur = str[len];
1854
Daniel Veillard8874b942005-08-25 13:19:21 +00001855 if ((len < (int) (2 * sizeof(void *))) &&
1856 (ctxt->options & XML_PARSE_COMPACT)) {
Daniel Veillard968a03a2012-08-13 12:41:33 +08001857 /* store the string in the node overriding properties and nsDef */
Daniel Veillard8874b942005-08-25 13:19:21 +00001858 xmlChar *tmp = (xmlChar *) &(ret->properties);
1859 memcpy(tmp, str, len);
1860 tmp[len] = 0;
1861 intern = tmp;
1862 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001863 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001864 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001865 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001866 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001867 int i;
1868
1869 for (i = 1;i < len;i++) {
Daniel Veillard1a9b7082004-01-02 10:42:01 +00001870 if (!IS_BLANK_CH(str[i])) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001871 }
1872 intern = xmlDictLookup(ctxt->dict, str, len);
1873 }
1874 }
1875skip:
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001876 ret->type = XML_TEXT_NODE;
1877
1878 ret->name = xmlStringText;
William M. Brack9f797ab2004-07-28 07:40:12 +00001879 if (intern == NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001880 ret->content = xmlStrndup(str, len);
William M. Brack9f797ab2004-07-28 07:40:12 +00001881 if (ret->content == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001882 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1883 xmlFree(ret);
William M. Brack9f797ab2004-07-28 07:40:12 +00001884 return(NULL);
1885 }
1886 } else
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001887 ret->content = (xmlChar *) intern;
1888
Daniel Veillard968a03a2012-08-13 12:41:33 +08001889 if (ctxt->linenumbers) {
1890 if (ctxt->input != NULL) {
1891 if (ctxt->input->line < 65535)
1892 ret->line = (short) ctxt->input->line;
1893 else {
1894 ret->line = 65535;
1895 if (ctxt->options & XML_PARSE_BIG_LINES)
Daniel Veillard76516062012-09-11 14:02:08 +08001896 ret->psvi = (void *) (long) ctxt->input->line;
Daniel Veillard968a03a2012-08-13 12:41:33 +08001897 }
1898 }
1899 }
Daniel Veillard45efd082008-07-07 13:52:52 +00001900
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001901 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1902 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001903 return(ret);
1904}
1905
Daniel Veillard4432df22003-09-28 18:58:27 +00001906#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001907/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001908 * xmlSAX2DecodeAttrEntities:
1909 * @ctxt: the parser context
1910 * @str: the input string
1911 * @len: the string length
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001912 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001913 * Remove the entities from an attribute value
1914 *
1915 * Returns the newly allocated string or NULL if not needed or error
1916 */
1917static xmlChar *
1918xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1919 const xmlChar *end) {
1920 const xmlChar *in;
1921 xmlChar *ret;
1922
1923 in = str;
1924 while (in < end)
1925 if (*in++ == '&')
1926 goto decode;
1927 return(NULL);
1928decode:
1929 ctxt->depth++;
1930 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1931 XML_SUBSTITUTE_REF, 0,0,0);
1932 ctxt->depth--;
1933 return(ret);
1934}
Daniel Veillard4432df22003-09-28 18:58:27 +00001935#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001936
1937/**
1938 * xmlSAX2AttributeNs:
1939 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001940 * @localname: the local name of the attribute
1941 * @prefix: the attribute namespace prefix if available
1942 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001943 * @value: Start of the attribute value
1944 * @valueend: end of the attribute value
1945 *
1946 * Handle an attribute that has been read by the parser.
1947 * The default handling is to convert the attribute into an
1948 * DOM subtree and past it in a new xmlAttr element added to
1949 * the element.
1950 */
1951static void
1952xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1953 const xmlChar * localname,
1954 const xmlChar * prefix,
1955 const xmlChar * value,
1956 const xmlChar * valueend)
1957{
1958 xmlAttrPtr ret;
1959 xmlNsPtr namespace = NULL;
1960 xmlChar *dup = NULL;
1961
Daniel Veillarde57ec792003-09-10 10:50:59 +00001962 /*
1963 * Note: if prefix == NULL, the attribute is not in the default namespace
1964 */
1965 if (prefix != NULL)
1966 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1967
Daniel Veillard8a44e592003-09-15 14:50:06 +00001968 /*
1969 * allocate the node
1970 */
1971 if (ctxt->freeAttrs != NULL) {
1972 ret = ctxt->freeAttrs;
1973 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001974 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001975 memset(ret, 0, sizeof(xmlAttr));
1976 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001977
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001978 ret->parent = ctxt->node;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001979 ret->doc = ctxt->myDoc;
1980 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001981
Daniel Veillard8a44e592003-09-15 14:50:06 +00001982 if (ctxt->dictNames)
1983 ret->name = localname;
1984 else
1985 ret->name = xmlStrdup(localname);
1986
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001987 /* link at the end to preserv order, TODO speed up with a last */
1988 if (ctxt->node->properties == NULL) {
1989 ctxt->node->properties = ret;
1990 } else {
1991 xmlAttrPtr prev = ctxt->node->properties;
1992
1993 while (prev->next != NULL) prev = prev->next;
1994 prev->next = ret;
1995 ret->prev = prev;
1996 }
1997
Daniel Veillard8a44e592003-09-15 14:50:06 +00001998 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1999 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
2000 } else {
2001 if (ctxt->dictNames)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002002 ret = xmlNewNsPropEatName(ctxt->node, namespace,
Daniel Veillard8a44e592003-09-15 14:50:06 +00002003 (xmlChar *) localname, NULL);
2004 else
2005 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
2006 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002007 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002008 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002009 }
2010 }
2011
Daniel Veillard8a44e592003-09-15 14:50:06 +00002012 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
2013 xmlNodePtr tmp;
2014
Daniel Veillard19895052003-09-17 13:59:32 +00002015 /*
2016 * We know that if there is an entity reference, then
2017 * the string has been dup'ed and terminates with 0
2018 * otherwise with ' or "
2019 */
2020 if (*valueend != 0) {
2021 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2022 ret->children = tmp;
2023 ret->last = tmp;
2024 if (tmp != NULL) {
2025 tmp->doc = ret->doc;
2026 tmp->parent = (xmlNodePtr) ret;
2027 }
2028 } else {
2029 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
2030 valueend - value);
2031 tmp = ret->children;
2032 while (tmp != NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00002033 tmp->doc = ret->doc;
Daniel Veillard19895052003-09-17 13:59:32 +00002034 tmp->parent = (xmlNodePtr) ret;
2035 if (tmp->next == NULL)
2036 ret->last = tmp;
2037 tmp = tmp->next;
2038 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002039 }
2040 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002041 xmlNodePtr tmp;
2042
2043 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2044 ret->children = tmp;
2045 ret->last = tmp;
2046 if (tmp != NULL) {
2047 tmp->doc = ret->doc;
2048 tmp->parent = (xmlNodePtr) ret;
2049 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002050 }
2051
Daniel Veillard4432df22003-09-28 18:58:27 +00002052#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002053 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2054 ctxt->myDoc && ctxt->myDoc->intSubset) {
2055 /*
2056 * If we don't substitute entities, the validation should be
2057 * done on a value with replaced entities anyway.
2058 */
2059 if (!ctxt->replaceEntities) {
2060 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2061 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00002062 if (*valueend == 0) {
2063 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2064 ctxt->myDoc, ctxt->node, ret, value);
2065 } else {
2066 /*
2067 * That should already be normalized.
2068 * cheaper to finally allocate here than duplicate
2069 * entry points in the full validation code
2070 */
2071 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002072
Daniel Veillard62998c02003-09-15 12:56:36 +00002073 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2074 ctxt->myDoc, ctxt->node, ret, dup);
2075 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002076 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00002077 /*
2078 * dup now contains a string of the flattened attribute
2079 * content with entities substitued. Check if we need to
2080 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002081 * It need to be done twice ... it's an extra burden related
2082 * to the ability to keep references in attributes
2083 */
Daniel Veillard62998c02003-09-15 12:56:36 +00002084 if (ctxt->attsSpecial != NULL) {
2085 xmlChar *nvalnorm;
2086 xmlChar fn[50];
2087 xmlChar *fullname;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002088
Daniel Veillard62998c02003-09-15 12:56:36 +00002089 fullname = xmlBuildQName(localname, prefix, fn, 50);
2090 if (fullname != NULL) {
2091 ctxt->vctxt.valid = 1;
2092 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2093 &ctxt->vctxt, ctxt->myDoc,
2094 ctxt->node, fullname, dup);
2095 if (ctxt->vctxt.valid != 1)
2096 ctxt->valid = 0;
2097
2098 if ((fullname != fn) && (fullname != localname))
2099 xmlFree(fullname);
2100 if (nvalnorm != NULL) {
2101 xmlFree(dup);
2102 dup = nvalnorm;
2103 }
2104 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002105 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002106
2107 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2108 ctxt->myDoc, ctxt->node, ret, dup);
2109 }
2110 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002111 /*
2112 * if entities already have been substitued, then
2113 * the attribute as passed is already normalized
2114 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002115 dup = xmlStrndup(value, valueend - value);
2116
2117 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2118 ctxt->myDoc, ctxt->node, ret, dup);
2119 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002120 } else
2121#endif /* LIBXML_VALID_ENABLED */
2122 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002123 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2124 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2125 /*
2126 * when validating, the ID registration is done at the attribute
2127 * validation level. Otherwise we have to do specific handling here.
2128 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002129 if ((prefix == ctxt->str_xml) &&
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002130 (localname[0] == 'i') && (localname[1] == 'd') &&
2131 (localname[2] == 0)) {
2132 /*
2133 * Add the xml:id value
2134 *
2135 * Open issue: normalization of the value.
2136 */
2137 if (dup == NULL)
2138 dup = xmlStrndup(value, valueend - value);
William M. Brack5ef2f812004-05-23 23:56:47 +00002139#ifdef LIBXML_VALID_ENABLED
Daniel Veillard68cb4b22004-04-18 20:55:39 +00002140 if (xmlValidateNCName(dup, 1) != 0) {
2141 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2142 "xml:id : attribute value %s is not an NCName\n",
2143 (const char *) dup, NULL);
2144 }
William M. Brack3f147372004-05-22 01:09:26 +00002145#endif
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002146 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002147 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2148 /* might be worth duplicate entry points and not copy */
2149 if (dup == NULL)
2150 dup = xmlStrndup(value, valueend - value);
2151 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2152 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2153 if (dup == NULL)
2154 dup = xmlStrndup(value, valueend - value);
2155 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002156 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002157 }
2158 if (dup != NULL)
2159 xmlFree(dup);
2160}
2161
2162/**
2163 * xmlSAX2StartElementNs:
2164 * @ctx: the user data (XML parser context)
2165 * @localname: the local name of the element
2166 * @prefix: the element namespace prefix if available
2167 * @URI: the element namespace name if available
2168 * @nb_namespaces: number of namespace definitions on that node
2169 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2170 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002171 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002172 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2173 * attribute values.
2174 *
2175 * SAX2 callback when an element start has been detected by the parser.
2176 * It provides the namespace informations for the element, as well as
2177 * the new namespace declarations on the element.
2178 */
2179void
2180xmlSAX2StartElementNs(void *ctx,
2181 const xmlChar *localname,
2182 const xmlChar *prefix,
2183 const xmlChar *URI,
2184 int nb_namespaces,
2185 const xmlChar **namespaces,
2186 int nb_attributes,
2187 int nb_defaulted,
2188 const xmlChar **attributes)
2189{
2190 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2191 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002192 xmlNodePtr parent;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002193 xmlNsPtr last = NULL, ns;
2194 const xmlChar *uri, *pref;
Daniel Veillard77b77b12012-01-26 19:11:02 +08002195 xmlChar *lname = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002196 int i, j;
2197
Daniel Veillard34099b42004-11-04 17:34:35 +00002198 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002199 parent = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002200 /*
2201 * First check on validity:
2202 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002203 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002204 ((ctxt->myDoc->intSubset == NULL) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002205 ((ctxt->myDoc->intSubset->notations == NULL) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002206 (ctxt->myDoc->intSubset->elements == NULL) &&
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002207 (ctxt->myDoc->intSubset->attributes == NULL) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002208 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillard6c91aa32012-10-25 15:33:59 +08002209 xmlErrValid(ctxt, XML_DTD_NO_DTD,
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00002210 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002211 ctxt->validate = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002212 }
2213
Daniel Veillard8a44e592003-09-15 14:50:06 +00002214 /*
Daniel Veillard77b77b12012-01-26 19:11:02 +08002215 * Take care of the rare case of an undefined namespace prefix
2216 */
2217 if ((prefix != NULL) && (URI == NULL)) {
2218 if (ctxt->dictNames) {
2219 const xmlChar *fullname;
2220
2221 fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
2222 if (fullname != NULL)
2223 localname = fullname;
2224 } else {
2225 lname = xmlBuildQName(localname, prefix, NULL, 0);
2226 }
2227 }
2228 /*
Daniel Veillard8a44e592003-09-15 14:50:06 +00002229 * allocate the node
2230 */
2231 if (ctxt->freeElems != NULL) {
2232 ret = ctxt->freeElems;
2233 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00002234 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00002235 memset(ret, 0, sizeof(xmlNode));
2236 ret->type = XML_ELEMENT_NODE;
2237
2238 if (ctxt->dictNames)
2239 ret->name = localname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002240 else {
Daniel Veillard77b77b12012-01-26 19:11:02 +08002241 if (lname == NULL)
2242 ret->name = xmlStrdup(localname);
2243 else
2244 ret->name = lname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002245 if (ret->name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002246 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002247 return;
2248 }
2249 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002250 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2251 xmlRegisterNodeDefaultValue(ret);
2252 } else {
2253 if (ctxt->dictNames)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002254 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
Daniel Veillard8a44e592003-09-15 14:50:06 +00002255 (xmlChar *) localname, NULL);
Daniel Veillard77b77b12012-01-26 19:11:02 +08002256 else if (lname == NULL)
Daniel Veillard8a44e592003-09-15 14:50:06 +00002257 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
Daniel Veillard77b77b12012-01-26 19:11:02 +08002258 else
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002259 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
Daniel Veillard77b77b12012-01-26 19:11:02 +08002260 (xmlChar *) lname, NULL);
Daniel Veillard8a44e592003-09-15 14:50:06 +00002261 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002262 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002263 return;
2264 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002265 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002266 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00002267 if (ctxt->input != NULL) {
2268 if (ctxt->input->line < 65535)
2269 ret->line = (short) ctxt->input->line;
2270 else
2271 ret->line = 65535;
2272 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002273 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002274
Tim Elliott71a243d2012-01-17 19:25:08 -08002275 if (parent == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002276 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002277 }
2278 /*
2279 * Build the namespace list
2280 */
2281 for (i = 0,j = 0;j < nb_namespaces;j++) {
2282 pref = namespaces[i++];
2283 uri = namespaces[i++];
2284 ns = xmlNewNs(NULL, uri, pref);
2285 if (ns != NULL) {
2286 if (last == NULL) {
2287 ret->nsDef = last = ns;
2288 } else {
2289 last->next = ns;
2290 last = ns;
2291 }
2292 if ((URI != NULL) && (prefix == pref))
2293 ret->ns = ns;
2294 } else {
Daniel Veillardaa54d372010-09-09 18:17:47 +02002295 /*
2296 * any out of memory error would already have been raised
2297 * but we can't be garanteed it's the actual error due to the
2298 * API, best is to skip in this case
2299 */
2300 continue;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002301 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002302#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002303 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2304 ctxt->myDoc && ctxt->myDoc->intSubset) {
2305 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2306 ret, prefix, ns, uri);
2307 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002308#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002309 }
2310 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002311
2312 /*
2313 * We are parsing a new node.
2314 */
2315 nodePush(ctxt, ret);
2316
2317 /*
2318 * Link the child element
2319 */
2320 if (parent != NULL) {
2321 if (parent->type == XML_ELEMENT_NODE) {
2322 xmlAddChild(parent, ret);
2323 } else {
2324 xmlAddSibling(parent, ret);
2325 }
2326 }
2327
2328 /*
2329 * Insert the defaulted attributes from the DTD only if requested:
2330 */
2331 if ((nb_defaulted != 0) &&
2332 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2333 nb_attributes -= nb_defaulted;
2334
2335 /*
2336 * Search the namespace if it wasn't already found
William M. Brackbf5cf212004-08-31 06:47:17 +00002337 * Note that, if prefix is NULL, this searches for the default Ns
Daniel Veillarde57ec792003-09-10 10:50:59 +00002338 */
2339 if ((URI != NULL) && (ret->ns == NULL)) {
William M. Brackbf5cf212004-08-31 06:47:17 +00002340 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002341 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2342 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2343 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002344 if (ret->ns == NULL) {
2345 ns = xmlNewNs(ret, NULL, prefix);
William M. Brack9f797ab2004-07-28 07:40:12 +00002346 if (ns == NULL) {
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002347
William M. Brack42331a92004-07-29 07:07:16 +00002348 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002349 return;
2350 }
Daniel Veillard74eaec12009-08-26 15:57:20 +02002351 if (prefix != NULL)
2352 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2353 "Namespace prefix %s was not found\n",
2354 prefix, NULL);
2355 else
2356 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2357 "Namespace default prefix was not found\n",
2358 NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002359 }
2360 }
2361
2362 /*
2363 * process all the other attributes
2364 */
2365 if (nb_attributes > 0) {
2366 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
Daniel Veillard1c989272012-01-26 19:43:06 +08002367 /*
2368 * Handle the rare case of an undefined atribute prefix
2369 */
2370 if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
2371 if (ctxt->dictNames) {
2372 const xmlChar *fullname;
2373
2374 fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
2375 attributes[j]);
2376 if (fullname != NULL) {
2377 xmlSAX2AttributeNs(ctxt, fullname, NULL,
2378 attributes[j+3], attributes[j+4]);
2379 continue;
2380 }
2381 } else {
2382 lname = xmlBuildQName(attributes[j], attributes[j+1],
2383 NULL, 0);
2384 if (lname != NULL) {
2385 xmlSAX2AttributeNs(ctxt, lname, NULL,
2386 attributes[j+3], attributes[j+4]);
2387 xmlFree(lname);
2388 continue;
2389 }
2390 }
2391 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002392 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
Daniel Veillard1c989272012-01-26 19:43:06 +08002393 attributes[j+3], attributes[j+4]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002394 }
2395 }
2396
Daniel Veillard4432df22003-09-28 18:58:27 +00002397#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002398 /*
2399 * If it's the Document root, finish the DTD validation and
2400 * check the document root element for validity
2401 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00002402 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002403 int chk;
2404
2405 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2406 if (chk <= 0)
2407 ctxt->valid = 0;
2408 if (chk < 0)
2409 ctxt->wellFormed = 0;
2410 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00002411 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002412 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002413#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002414}
2415
2416/**
2417 * xmlSAX2EndElementNs:
2418 * @ctx: the user data (XML parser context)
2419 * @localname: the local name of the element
2420 * @prefix: the element namespace prefix if available
2421 * @URI: the element namespace name if available
2422 *
2423 * SAX2 callback when an element end has been detected by the parser.
2424 * It provides the namespace informations for the element.
2425 */
2426void
2427xmlSAX2EndElementNs(void *ctx,
2428 const xmlChar * localname ATTRIBUTE_UNUSED,
2429 const xmlChar * prefix ATTRIBUTE_UNUSED,
2430 const xmlChar * URI ATTRIBUTE_UNUSED)
2431{
2432 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2433 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002434 xmlNodePtr cur;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002435
Daniel Veillard34099b42004-11-04 17:34:35 +00002436 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002437 cur = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002438 /* Capture end position and add node */
2439 if ((ctxt->record_info) && (cur != NULL)) {
2440 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2441 node_info.end_line = ctxt->input->line;
2442 node_info.node = cur;
2443 xmlParserAddNodeInfo(ctxt, &node_info);
2444 }
2445 ctxt->nodemem = -1;
2446
Daniel Veillard4432df22003-09-28 18:58:27 +00002447#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002448 if (ctxt->validate && ctxt->wellFormed &&
2449 ctxt->myDoc && ctxt->myDoc->intSubset)
2450 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002451#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002452
2453 /*
2454 * end of parsing of this node.
2455 */
2456 nodePop(ctxt);
2457}
2458
Daniel Veillard1af9a412003-08-20 22:54:39 +00002459/**
2460 * xmlSAX2Reference:
2461 * @ctx: the user data (XML parser context)
2462 * @name: The entity name
2463 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002464 * called when an entity xmlSAX2Reference is detected.
Daniel Veillard1af9a412003-08-20 22:54:39 +00002465 */
2466void
2467xmlSAX2Reference(void *ctx, const xmlChar *name)
2468{
2469 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2470 xmlNodePtr ret;
2471
Daniel Veillard34099b42004-11-04 17:34:35 +00002472 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002473#ifdef DEBUG_SAX
2474 xmlGenericError(xmlGenericErrorContext,
2475 "SAX.xmlSAX2Reference(%s)\n", name);
2476#endif
2477 if (name[0] == '#')
2478 ret = xmlNewCharRef(ctxt->myDoc, name);
2479 else
2480 ret = xmlNewReference(ctxt->myDoc, name);
2481#ifdef DEBUG_SAX_TREE
2482 xmlGenericError(xmlGenericErrorContext,
2483 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2484#endif
Daniel Veillardb242b082008-02-08 09:56:31 +00002485 if (xmlAddChild(ctxt->node, ret) == NULL) {
2486 xmlFreeNode(ret);
2487 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002488}
2489
2490/**
2491 * xmlSAX2Characters:
2492 * @ctx: the user data (XML parser context)
2493 * @ch: a xmlChar string
2494 * @len: the number of xmlChar
2495 *
2496 * receiving some chars from the parser.
2497 */
2498void
2499xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2500{
2501 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2502 xmlNodePtr lastChild;
2503
Daniel Veillard34099b42004-11-04 17:34:35 +00002504 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002505#ifdef DEBUG_SAX
2506 xmlGenericError(xmlGenericErrorContext,
2507 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2508#endif
2509 /*
2510 * Handle the data if any. If there is no child
2511 * add it as content, otherwise if the last child is text,
2512 * concatenate it, else create a new node of type text.
2513 */
2514
2515 if (ctxt->node == NULL) {
2516#ifdef DEBUG_SAX_TREE
2517 xmlGenericError(xmlGenericErrorContext,
2518 "add chars: ctxt->node == NULL !\n");
2519#endif
2520 return;
2521 }
Daniel Veillard19895052003-09-17 13:59:32 +00002522 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002523#ifdef DEBUG_SAX_TREE
2524 xmlGenericError(xmlGenericErrorContext,
2525 "add chars to %s \n", ctxt->node->name);
2526#endif
2527
2528 /*
2529 * Here we needed an accelerator mechanism in case of very large
2530 * elements. Use an attribute in the structure !!!
2531 */
2532 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002533 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2534 if (lastChild != NULL) {
2535 ctxt->node->children = lastChild;
2536 ctxt->node->last = lastChild;
2537 lastChild->parent = ctxt->node;
2538 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002539 ctxt->nodelen = len;
2540 ctxt->nodemem = len + 1;
William M. Bracka3215c72004-07-31 16:24:01 +00002541 } else {
2542 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2543 return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002544 }
2545 } else {
2546 int coalesceText = (lastChild != NULL) &&
2547 (lastChild->type == XML_TEXT_NODE) &&
2548 (lastChild->name == xmlStringText);
2549 if ((coalesceText) && (ctxt->nodemem != 0)) {
2550 /*
2551 * The whole point of maintaining nodelen and nodemem,
2552 * xmlTextConcat is too costly, i.e. compute length,
2553 * reallocate a new buffer, move data, append ch. Here
2554 * We try to minimaze realloc() uses and avoid copying
2555 * and recomputing length over and over.
2556 */
Daniel Veillard8874b942005-08-25 13:19:21 +00002557 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2558 lastChild->content = xmlStrdup(lastChild->content);
2559 lastChild->properties = NULL;
2560 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2561 (xmlDictOwns(ctxt->dict, lastChild->content))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002562 lastChild->content = xmlStrdup(lastChild->content);
2563 }
Daniel Veillard97ff9b32009-01-18 21:43:30 +00002564 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002565 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2566 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2567 return;
2568 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002569 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002570 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002571 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2572 return;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002573 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002574 if (ctxt->nodelen + len >= ctxt->nodemem) {
2575 xmlChar *newbuf;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002576 size_t size;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002577
2578 size = ctxt->nodemem + len;
2579 size *= 2;
2580 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2581 if (newbuf == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002582 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002583 return;
2584 }
2585 ctxt->nodemem = size;
2586 lastChild->content = newbuf;
2587 }
2588 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2589 ctxt->nodelen += len;
2590 lastChild->content[ctxt->nodelen] = 0;
2591 } else if (coalesceText) {
2592 if (xmlTextConcat(lastChild, ch, len)) {
William M. Brack42331a92004-07-29 07:07:16 +00002593 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002594 }
2595 if (ctxt->node->children != NULL) {
2596 ctxt->nodelen = xmlStrlen(lastChild->content);
2597 ctxt->nodemem = ctxt->nodelen + 1;
2598 }
2599 } else {
2600 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002601 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2602 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002603 xmlAddChild(ctxt->node, lastChild);
2604 if (ctxt->node->children != NULL) {
2605 ctxt->nodelen = len;
2606 ctxt->nodemem = len + 1;
2607 }
2608 }
2609 }
2610 }
2611}
2612
2613/**
2614 * xmlSAX2IgnorableWhitespace:
2615 * @ctx: the user data (XML parser context)
2616 * @ch: a xmlChar string
2617 * @len: the number of xmlChar
2618 *
2619 * receiving some ignorable whitespaces from the parser.
2620 * UNUSED: by default the DOM building will use xmlSAX2Characters
2621 */
2622void
2623xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2624{
2625 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2626#ifdef DEBUG_SAX
2627 xmlGenericError(xmlGenericErrorContext,
2628 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2629#endif
2630}
2631
2632/**
2633 * xmlSAX2ProcessingInstruction:
2634 * @ctx: the user data (XML parser context)
2635 * @target: the target name
2636 * @data: the PI data's
2637 *
2638 * A processing instruction has been parsed.
2639 */
2640void
2641xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2642 const xmlChar *data)
2643{
2644 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2645 xmlNodePtr ret;
Daniel Veillard6128c012004-11-08 17:16:15 +00002646 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002647
Daniel Veillard34099b42004-11-04 17:34:35 +00002648 if (ctx == NULL) return;
Daniel Veillard6128c012004-11-08 17:16:15 +00002649 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002650#ifdef DEBUG_SAX
2651 xmlGenericError(xmlGenericErrorContext,
2652 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2653#endif
2654
Daniel Veillard03a53c32004-10-26 16:06:51 +00002655 ret = xmlNewDocPI(ctxt->myDoc, target, data);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002656 if (ret == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002657
Daniel Veillard73da77e2005-08-24 14:05:37 +00002658 if (ctxt->linenumbers) {
2659 if (ctxt->input != NULL) {
2660 if (ctxt->input->line < 65535)
2661 ret->line = (short) ctxt->input->line;
2662 else
2663 ret->line = 65535;
2664 }
2665 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002666 if (ctxt->inSubset == 1) {
2667 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2668 return;
2669 } else if (ctxt->inSubset == 2) {
2670 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2671 return;
2672 }
Tim Elliott71a243d2012-01-17 19:25:08 -08002673 if (parent == NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002674#ifdef DEBUG_SAX_TREE
2675 xmlGenericError(xmlGenericErrorContext,
2676 "Setting PI %s as root\n", target);
2677#endif
2678 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2679 return;
2680 }
2681 if (parent->type == XML_ELEMENT_NODE) {
2682#ifdef DEBUG_SAX_TREE
2683 xmlGenericError(xmlGenericErrorContext,
2684 "adding PI %s child to %s\n", target, parent->name);
2685#endif
2686 xmlAddChild(parent, ret);
2687 } else {
2688#ifdef DEBUG_SAX_TREE
2689 xmlGenericError(xmlGenericErrorContext,
2690 "adding PI %s sibling to ", target);
2691 xmlDebugDumpOneNode(stderr, parent, 0);
2692#endif
2693 xmlAddSibling(parent, ret);
2694 }
2695}
2696
2697/**
2698 * xmlSAX2Comment:
2699 * @ctx: the user data (XML parser context)
2700 * @value: the xmlSAX2Comment content
2701 *
2702 * A xmlSAX2Comment has been parsed.
2703 */
2704void
2705xmlSAX2Comment(void *ctx, const xmlChar *value)
2706{
2707 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2708 xmlNodePtr ret;
Daniel Veillard34099b42004-11-04 17:34:35 +00002709 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002710
Daniel Veillard34099b42004-11-04 17:34:35 +00002711 if (ctx == NULL) return;
2712 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002713#ifdef DEBUG_SAX
2714 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2715#endif
2716 ret = xmlNewDocComment(ctxt->myDoc, value);
2717 if (ret == NULL) return;
Daniel Veillard73da77e2005-08-24 14:05:37 +00002718 if (ctxt->linenumbers) {
2719 if (ctxt->input != NULL) {
2720 if (ctxt->input->line < 65535)
2721 ret->line = (short) ctxt->input->line;
2722 else
2723 ret->line = 65535;
2724 }
2725 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002726
2727 if (ctxt->inSubset == 1) {
2728 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2729 return;
2730 } else if (ctxt->inSubset == 2) {
2731 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2732 return;
2733 }
Tim Elliott71a243d2012-01-17 19:25:08 -08002734 if (parent == NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002735#ifdef DEBUG_SAX_TREE
2736 xmlGenericError(xmlGenericErrorContext,
2737 "Setting xmlSAX2Comment as root\n");
2738#endif
2739 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2740 return;
2741 }
2742 if (parent->type == XML_ELEMENT_NODE) {
2743#ifdef DEBUG_SAX_TREE
2744 xmlGenericError(xmlGenericErrorContext,
2745 "adding xmlSAX2Comment child to %s\n", parent->name);
2746#endif
2747 xmlAddChild(parent, ret);
2748 } else {
2749#ifdef DEBUG_SAX_TREE
2750 xmlGenericError(xmlGenericErrorContext,
2751 "adding xmlSAX2Comment sibling to ");
2752 xmlDebugDumpOneNode(stderr, parent, 0);
2753#endif
2754 xmlAddSibling(parent, ret);
2755 }
2756}
2757
2758/**
2759 * xmlSAX2CDataBlock:
2760 * @ctx: the user data (XML parser context)
2761 * @value: The pcdata content
2762 * @len: the block length
2763 *
2764 * called when a pcdata block has been parsed
2765 */
2766void
2767xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2768{
2769 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2770 xmlNodePtr ret, lastChild;
2771
Daniel Veillard34099b42004-11-04 17:34:35 +00002772 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002773#ifdef DEBUG_SAX
2774 xmlGenericError(xmlGenericErrorContext,
2775 "SAX.pcdata(%.10s, %d)\n", value, len);
2776#endif
2777 lastChild = xmlGetLastChild(ctxt->node);
2778#ifdef DEBUG_SAX_TREE
2779 xmlGenericError(xmlGenericErrorContext,
2780 "add chars to %s \n", ctxt->node->name);
2781#endif
2782 if ((lastChild != NULL) &&
2783 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2784 xmlTextConcat(lastChild, value, len);
2785 } else {
2786 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2787 xmlAddChild(ctxt->node, ret);
2788 }
2789}
2790
Daniel Veillard62998c02003-09-15 12:56:36 +00002791static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002792
Daniel Veillard81273902003-09-30 00:43:48 +00002793#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002794/**
2795 * xmlSAXDefaultVersion:
2796 * @version: the version, 1 or 2
2797 *
2798 * Set the default version of SAX used globally by the library.
William M. Brack96d2eff2004-06-30 11:48:47 +00002799 * By default, during initialization the default is set to 2.
2800 * Note that it is generally a better coding style to use
2801 * xmlSAXVersion() to set up the version explicitly for a given
2802 * parsing context.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002803 *
2804 * Returns the previous value in case of success and -1 in case of error.
2805 */
2806int
2807xmlSAXDefaultVersion(int version)
2808{
2809 int ret = xmlSAX2DefaultVersionValue;
2810
2811 if ((version != 1) && (version != 2))
2812 return(-1);
2813 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002814 return(ret);
2815}
Daniel Veillard81273902003-09-30 00:43:48 +00002816#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002817
2818/**
2819 * xmlSAXVersion:
2820 * @hdlr: the SAX handler
2821 * @version: the version, 1 or 2
2822 *
2823 * Initialize the default XML SAX handler according to the version
2824 *
2825 * Returns 0 in case of success and -1 in case of error.
2826 */
2827int
2828xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2829{
2830 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002831 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002832 hdlr->startElement = NULL;
2833 hdlr->endElement = NULL;
2834 hdlr->startElementNs = xmlSAX2StartElementNs;
2835 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002836 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002837 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002838#ifdef LIBXML_SAX1_ENABLED
2839 } else if (version == 1) {
2840 hdlr->startElement = xmlSAX2StartElement;
2841 hdlr->endElement = xmlSAX2EndElement;
2842 hdlr->initialized = 1;
2843#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002844 } else
2845 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002846 hdlr->internalSubset = xmlSAX2InternalSubset;
2847 hdlr->externalSubset = xmlSAX2ExternalSubset;
2848 hdlr->isStandalone = xmlSAX2IsStandalone;
2849 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2850 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2851 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2852 hdlr->getEntity = xmlSAX2GetEntity;
2853 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2854 hdlr->entityDecl = xmlSAX2EntityDecl;
2855 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2856 hdlr->elementDecl = xmlSAX2ElementDecl;
2857 hdlr->notationDecl = xmlSAX2NotationDecl;
2858 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2859 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2860 hdlr->startDocument = xmlSAX2StartDocument;
2861 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002862 hdlr->reference = xmlSAX2Reference;
2863 hdlr->characters = xmlSAX2Characters;
2864 hdlr->cdataBlock = xmlSAX2CDataBlock;
2865 hdlr->ignorableWhitespace = xmlSAX2Characters;
2866 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2867 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002868 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002869 hdlr->error = xmlParserError;
2870 hdlr->fatalError = xmlParserError;
2871
Daniel Veillarde57ec792003-09-10 10:50:59 +00002872 return(0);
2873}
2874
2875/**
2876 * xmlSAX2InitDefaultSAXHandler:
2877 * @hdlr: the SAX handler
2878 * @warning: flag if non-zero sets the handler warning procedure
2879 *
2880 * Initialize the default XML SAX2 handler
2881 */
2882void
2883xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2884{
2885 if ((hdlr == NULL) || (hdlr->initialized != 0))
2886 return;
2887
2888 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2889 if (warning == 0)
2890 hdlr->warning = NULL;
2891 else
2892 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002893}
2894
2895/**
2896 * xmlDefaultSAXHandlerInit:
2897 *
2898 * Initialize the default SAX2 handler
2899 */
2900void
2901xmlDefaultSAXHandlerInit(void)
2902{
Daniel Veillard81273902003-09-30 00:43:48 +00002903#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002904 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002905#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002906}
2907
2908#ifdef LIBXML_HTML_ENABLED
2909
2910/**
2911 * xmlSAX2InitHtmlDefaultSAXHandler:
2912 * @hdlr: the SAX handler
2913 *
2914 * Initialize the default HTML SAX2 handler
2915 */
2916void
2917xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2918{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002919 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002920 return;
2921
2922 hdlr->internalSubset = xmlSAX2InternalSubset;
2923 hdlr->externalSubset = NULL;
2924 hdlr->isStandalone = NULL;
2925 hdlr->hasInternalSubset = NULL;
2926 hdlr->hasExternalSubset = NULL;
2927 hdlr->resolveEntity = NULL;
2928 hdlr->getEntity = xmlSAX2GetEntity;
2929 hdlr->getParameterEntity = NULL;
2930 hdlr->entityDecl = NULL;
2931 hdlr->attributeDecl = NULL;
2932 hdlr->elementDecl = NULL;
2933 hdlr->notationDecl = NULL;
2934 hdlr->unparsedEntityDecl = NULL;
2935 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2936 hdlr->startDocument = xmlSAX2StartDocument;
2937 hdlr->endDocument = xmlSAX2EndDocument;
2938 hdlr->startElement = xmlSAX2StartElement;
2939 hdlr->endElement = xmlSAX2EndElement;
2940 hdlr->reference = NULL;
2941 hdlr->characters = xmlSAX2Characters;
2942 hdlr->cdataBlock = xmlSAX2CDataBlock;
2943 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002944 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002945 hdlr->comment = xmlSAX2Comment;
2946 hdlr->warning = xmlParserWarning;
2947 hdlr->error = xmlParserError;
2948 hdlr->fatalError = xmlParserError;
2949
Daniel Veillard092643b2003-09-25 14:29:29 +00002950 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002951}
2952
2953/**
2954 * htmlDefaultSAXHandlerInit:
2955 *
2956 * Initialize the default SAX handler
2957 */
2958void
2959htmlDefaultSAXHandlerInit(void)
2960{
Daniel Veillard092643b2003-09-25 14:29:29 +00002961 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002962}
2963
2964#endif /* LIBXML_HTML_ENABLED */
2965
2966#ifdef LIBXML_DOCB_ENABLED
2967
2968/**
2969 * xmlSAX2InitDocbDefaultSAXHandler:
2970 * @hdlr: the SAX handler
2971 *
2972 * Initialize the default DocBook SAX2 handler
2973 */
2974void
2975xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2976{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002977 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002978 return;
2979
2980 hdlr->internalSubset = xmlSAX2InternalSubset;
2981 hdlr->externalSubset = NULL;
2982 hdlr->isStandalone = xmlSAX2IsStandalone;
2983 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2984 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2985 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2986 hdlr->getEntity = xmlSAX2GetEntity;
2987 hdlr->getParameterEntity = NULL;
2988 hdlr->entityDecl = xmlSAX2EntityDecl;
2989 hdlr->attributeDecl = NULL;
2990 hdlr->elementDecl = NULL;
2991 hdlr->notationDecl = NULL;
2992 hdlr->unparsedEntityDecl = NULL;
2993 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2994 hdlr->startDocument = xmlSAX2StartDocument;
2995 hdlr->endDocument = xmlSAX2EndDocument;
2996 hdlr->startElement = xmlSAX2StartElement;
2997 hdlr->endElement = xmlSAX2EndElement;
2998 hdlr->reference = xmlSAX2Reference;
2999 hdlr->characters = xmlSAX2Characters;
3000 hdlr->cdataBlock = NULL;
3001 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
3002 hdlr->processingInstruction = NULL;
3003 hdlr->comment = xmlSAX2Comment;
3004 hdlr->warning = xmlParserWarning;
3005 hdlr->error = xmlParserError;
3006 hdlr->fatalError = xmlParserError;
3007
Daniel Veillardffbbed42003-10-10 14:46:54 +00003008 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00003009}
3010
3011/**
3012 * docbDefaultSAXHandlerInit:
3013 *
3014 * Initialize the default SAX handler
3015 */
3016void
3017docbDefaultSAXHandlerInit(void)
3018{
Daniel Veillard092643b2003-09-25 14:29:29 +00003019 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00003020}
3021
3022#endif /* LIBXML_DOCB_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003023#define bottom_SAX2
3024#include "elfgcchack.h"