blob: 62ddcb2013598288676be4df42d82478d311535f [file] [log] [blame]
Daniel Veillard1af9a412003-08-20 22:54:39 +00001/*
2 * SAX2.c : Default SAX2 handler to build a tree.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <daniel@veillard.com>
7 */
8
9
10#define IN_LIBXML
11#include "libxml.h"
12#include <stdlib.h>
13#include <string.h>
Daniel Veillard1dc9feb2008-11-17 15:59:21 +000014#include <limits.h>
Daniel Veillard1af9a412003-08-20 22:54:39 +000015#include <libxml/xmlmemory.h>
16#include <libxml/tree.h>
17#include <libxml/parser.h>
18#include <libxml/parserInternals.h>
19#include <libxml/valid.h>
20#include <libxml/entities.h>
21#include <libxml/xmlerror.h>
22#include <libxml/debugXML.h>
23#include <libxml/xmlIO.h>
24#include <libxml/SAX.h>
25#include <libxml/uri.h>
26#include <libxml/valid.h>
27#include <libxml/HTMLtree.h>
28#include <libxml/globals.h>
29
Daniel Veillard1dc9feb2008-11-17 15:59:21 +000030/* Define SIZE_T_MAX unless defined through <limits.h>. */
31#ifndef SIZE_T_MAX
32# define SIZE_T_MAX ((size_t)-1)
33#endif /* !SIZE_T_MAX */
34
Daniel Veillard1af9a412003-08-20 22:54:39 +000035/* #define DEBUG_SAX2 */
36/* #define DEBUG_SAX2_TREE */
37
38/**
Daniel Veillarde57ec792003-09-10 10:50:59 +000039 * TODO:
40 *
41 * macro to flag unimplemented blocks
42 * XML_CATALOG_PREFER user env to select between system/public prefered
43 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
44 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
45 *> values "system" and "public". I have made the default be "system" to
46 *> match yours.
47 */
48#define TODO \
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
William M. Brack42331a92004-07-29 07:07:16 +000053/*
54 * xmlSAX2ErrMemory:
55 * @ctxt: an XML validation parser context
56 * @msg: a string to accompany the error message
57 */
58static void
William M. Bracka3215c72004-07-31 16:24:01 +000059xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
Daniel 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,
146 XML_ERR_FATAL, NULL, 0,
147 (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,
177 XML_ERR_WARNING, NULL, 0,
178 (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,
202 XML_ERR_ERROR, NULL, 0,
203 (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,
226 XML_ERR_WARNING, NULL, 0,
227 (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 Veillard1af9a412003-08-20 22:54:39 +0000260 return((const xmlChar *) ctxt->input->filename);
261}
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 }
375 ctxt->myDoc->intSubset =
376 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
William M. Brack42331a92004-07-29 07:07:16 +0000377 if (ctxt->myDoc->intSubset == NULL)
378 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000379}
380
381/**
382 * xmlSAX2ExternalSubset:
383 * @ctx: the user data (XML parser context)
384 * @name: the root element name
385 * @ExternalID: the external ID
386 * @SystemID: the SYSTEM ID (e.g. filename or URL)
387 *
388 * Callback on external subset declaration.
389 */
390void
391xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
392 const xmlChar *ExternalID, const xmlChar *SystemID)
393{
394 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000395 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000396#ifdef DEBUG_SAX
397 xmlGenericError(xmlGenericErrorContext,
398 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
399 name, ExternalID, SystemID);
400#endif
401 if (((ExternalID != NULL) || (SystemID != NULL)) &&
402 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
403 (ctxt->wellFormed && ctxt->myDoc))) {
404 /*
405 * Try to fetch and parse the external subset.
406 */
407 xmlParserInputPtr oldinput;
408 int oldinputNr;
409 int oldinputMax;
410 xmlParserInputPtr *oldinputTab;
411 xmlParserInputPtr input = NULL;
412 xmlCharEncoding enc;
413 int oldcharset;
414
415 /*
416 * Ask the Entity resolver to load the damn thing
417 */
418 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
419 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
420 SystemID);
421 if (input == NULL) {
422 return;
423 }
424
425 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
426
427 /*
428 * make sure we won't destroy the main document context
429 */
430 oldinput = ctxt->input;
431 oldinputNr = ctxt->inputNr;
432 oldinputMax = ctxt->inputMax;
433 oldinputTab = ctxt->inputTab;
434 oldcharset = ctxt->charset;
435
436 ctxt->inputTab = (xmlParserInputPtr *)
437 xmlMalloc(5 * sizeof(xmlParserInputPtr));
438 if (ctxt->inputTab == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000439 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000440 ctxt->input = oldinput;
441 ctxt->inputNr = oldinputNr;
442 ctxt->inputMax = oldinputMax;
443 ctxt->inputTab = oldinputTab;
444 ctxt->charset = oldcharset;
445 return;
446 }
447 ctxt->inputNr = 0;
448 ctxt->inputMax = 5;
449 ctxt->input = NULL;
450 xmlPushInput(ctxt, input);
451
452 /*
453 * On the fly encoding conversion if needed
454 */
455 if (ctxt->input->length >= 4) {
456 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
457 xmlSwitchEncoding(ctxt, enc);
458 }
459
460 if (input->filename == NULL)
461 input->filename = (char *) xmlCanonicPath(SystemID);
462 input->line = 1;
463 input->col = 1;
464 input->base = ctxt->input->cur;
465 input->cur = ctxt->input->cur;
466 input->free = NULL;
467
468 /*
469 * let's parse that entity knowing it's an external subset.
470 */
471 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
472
473 /*
474 * Free up the external entities
475 */
476
477 while (ctxt->inputNr > 1)
478 xmlPopInput(ctxt);
479 xmlFreeInputStream(ctxt->input);
480 xmlFree(ctxt->inputTab);
481
482 /*
483 * Restore the parsing context of the main entity
484 */
485 ctxt->input = oldinput;
486 ctxt->inputNr = oldinputNr;
487 ctxt->inputMax = oldinputMax;
488 ctxt->inputTab = oldinputTab;
489 ctxt->charset = oldcharset;
490 /* ctxt->wellFormed = oldwellFormed; */
491 }
492}
493
494/**
495 * xmlSAX2ResolveEntity:
496 * @ctx: the user data (XML parser context)
497 * @publicId: The public ID of the entity
498 * @systemId: The system ID of the entity
499 *
500 * The entity loader, to control the loading of external entities,
501 * the application can either:
502 * - override this xmlSAX2ResolveEntity() callback in the SAX block
503 * - or better use the xmlSetExternalEntityLoader() function to
504 * set up it's own entity resolution routine
505 *
506 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
507 */
508xmlParserInputPtr
509xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
510{
511 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
512 xmlParserInputPtr ret;
513 xmlChar *URI;
514 const char *base = NULL;
515
Daniel Veillard34099b42004-11-04 17:34:35 +0000516 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000517 if (ctxt->input != NULL)
518 base = ctxt->input->filename;
519 if (base == NULL)
520 base = ctxt->directory;
521
522 URI = xmlBuildURI(systemId, (const xmlChar *) base);
523
524#ifdef DEBUG_SAX
525 xmlGenericError(xmlGenericErrorContext,
526 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
527#endif
528
529 ret = xmlLoadExternalEntity((const char *) URI,
530 (const char *) publicId, ctxt);
531 if (URI != NULL)
532 xmlFree(URI);
533 return(ret);
534}
535
536/**
537 * xmlSAX2GetEntity:
538 * @ctx: the user data (XML parser context)
539 * @name: The entity name
540 *
541 * Get an entity by name
542 *
543 * Returns the xmlEntityPtr if found.
544 */
545xmlEntityPtr
546xmlSAX2GetEntity(void *ctx, const xmlChar *name)
547{
548 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
549 xmlEntityPtr ret = NULL;
550
Daniel Veillard34099b42004-11-04 17:34:35 +0000551 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000552#ifdef DEBUG_SAX
553 xmlGenericError(xmlGenericErrorContext,
554 "SAX.xmlSAX2GetEntity(%s)\n", name);
555#endif
556
557 if (ctxt->inSubset == 0) {
558 ret = xmlGetPredefinedEntity(name);
559 if (ret != NULL)
560 return(ret);
561 }
562 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
563 if (ctxt->inSubset == 2) {
564 ctxt->myDoc->standalone = 0;
565 ret = xmlGetDocEntity(ctxt->myDoc, name);
566 ctxt->myDoc->standalone = 1;
567 } else {
568 ret = xmlGetDocEntity(ctxt->myDoc, name);
569 if (ret == NULL) {
570 ctxt->myDoc->standalone = 0;
571 ret = xmlGetDocEntity(ctxt->myDoc, name);
572 if (ret != NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000573 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
574 "Entity(%s) document marked standalone but requires external subset\n",
575 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000576 }
577 ctxt->myDoc->standalone = 1;
578 }
579 }
580 } else {
581 ret = xmlGetDocEntity(ctxt->myDoc, name);
582 }
583 if ((ret != NULL) &&
584 ((ctxt->validate) || (ctxt->replaceEntities)) &&
585 (ret->children == NULL) &&
586 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
587 int val;
588
589 /*
590 * for validation purposes we really need to fetch and
591 * parse the external entity
592 */
593 xmlNodePtr children;
594
595 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
596 ret->ExternalID, &children);
597 if (val == 0) {
598 xmlAddChildList((xmlNodePtr) ret, children);
599 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000600 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
601 "Failure to process entity %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000602 ctxt->validate = 0;
603 return(NULL);
604 }
605 ret->owner = 1;
Daniel Veillardf4f4e482008-08-25 08:57:48 +0000606 if (ret->checked == 0)
607 ret->checked = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000608 }
609 return(ret);
610}
611
612/**
613 * xmlSAX2GetParameterEntity:
614 * @ctx: the user data (XML parser context)
615 * @name: The entity name
616 *
617 * Get a parameter entity by name
618 *
619 * Returns the xmlEntityPtr if found.
620 */
621xmlEntityPtr
622xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
623{
624 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
625 xmlEntityPtr ret;
626
Daniel Veillard34099b42004-11-04 17:34:35 +0000627 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000628#ifdef DEBUG_SAX
629 xmlGenericError(xmlGenericErrorContext,
630 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
631#endif
632
633 ret = xmlGetParameterEntity(ctxt->myDoc, name);
634 return(ret);
635}
636
637
638/**
639 * xmlSAX2EntityDecl:
640 * @ctx: the user data (XML parser context)
641 * @name: the entity name
642 * @type: the entity type
643 * @publicId: The public ID of the entity
644 * @systemId: The system ID of the entity
645 * @content: the entity value (without processing).
646 *
647 * An entity definition has been parsed
648 */
649void
650xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
651 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
652{
653 xmlEntityPtr ent;
654 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
655
Daniel Veillard34099b42004-11-04 17:34:35 +0000656 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000657#ifdef DEBUG_SAX
658 xmlGenericError(xmlGenericErrorContext,
659 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
660 name, type, publicId, systemId, content);
661#endif
662 if (ctxt->inSubset == 1) {
663 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
664 systemId, content);
Daniel Veillard87b30462005-07-05 14:04:36 +0000665 if ((ent == NULL) && (ctxt->pedantic))
666 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
667 "Entity(%s) already defined in the internal subset\n",
668 name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000669 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
670 xmlChar *URI;
671 const char *base = NULL;
672
673 if (ctxt->input != NULL)
674 base = ctxt->input->filename;
675 if (base == NULL)
676 base = ctxt->directory;
677
678 URI = xmlBuildURI(systemId, (const xmlChar *) base);
679 ent->URI = URI;
680 }
681 } else if (ctxt->inSubset == 2) {
682 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
683 systemId, content);
684 if ((ent == NULL) && (ctxt->pedantic) &&
685 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000686 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000687 "Entity(%s) already defined in the external subset\n", name);
688 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
689 xmlChar *URI;
690 const char *base = NULL;
691
692 if (ctxt->input != NULL)
693 base = ctxt->input->filename;
694 if (base == NULL)
695 base = ctxt->directory;
696
697 URI = xmlBuildURI(systemId, (const xmlChar *) base);
698 ent->URI = URI;
699 }
700 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000701 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
702 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
703 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000704 }
705}
706
707/**
708 * xmlSAX2AttributeDecl:
709 * @ctx: the user data (XML parser context)
710 * @elem: the name of the element
711 * @fullname: the attribute name
712 * @type: the attribute type
713 * @def: the type of default value
714 * @defaultValue: the attribute default value
715 * @tree: the tree of enumerated value set
716 *
717 * An attribute definition has been parsed
718 */
719void
720xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
721 int type, int def, const xmlChar *defaultValue,
722 xmlEnumerationPtr tree)
723{
724 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
725 xmlAttributePtr attr;
726 xmlChar *name = NULL, *prefix = NULL;
727
Daniel Veillard2728f842006-03-09 16:49:24 +0000728 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
729 return;
730
Daniel Veillard1af9a412003-08-20 22:54:39 +0000731#ifdef DEBUG_SAX
732 xmlGenericError(xmlGenericErrorContext,
733 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
734 elem, fullname, type, def, defaultValue);
735#endif
Daniel Veillard68cb4b22004-04-18 20:55:39 +0000736 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
737 (type != XML_ATTRIBUTE_ID)) {
738 /*
739 * Raise the error but keep the validity flag
740 */
741 int tmp = ctxt->valid;
742 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
743 "xml:id : attribute type should be ID\n", NULL, NULL);
744 ctxt->valid = tmp;
745 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000746 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000747 name = xmlSplitQName(ctxt, fullname, &prefix);
748 ctxt->vctxt.valid = 1;
749 if (ctxt->inSubset == 1)
750 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
751 name, prefix, (xmlAttributeType) type,
752 (xmlAttributeDefault) def, defaultValue, tree);
753 else if (ctxt->inSubset == 2)
754 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
755 name, prefix, (xmlAttributeType) type,
756 (xmlAttributeDefault) def, defaultValue, tree);
757 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000758 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
759 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
760 name, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000761 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000762 return;
763 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000764#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000765 if (ctxt->vctxt.valid == 0)
766 ctxt->valid = 0;
767 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
Daniel Veillard2728f842006-03-09 16:49:24 +0000768 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000769 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
770 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000771#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000772 if (prefix != NULL)
773 xmlFree(prefix);
774 if (name != NULL)
775 xmlFree(name);
776}
777
778/**
779 * xmlSAX2ElementDecl:
780 * @ctx: the user data (XML parser context)
781 * @name: the element name
782 * @type: the element type
783 * @content: the element value tree
784 *
785 * An element definition has been parsed
786 */
787void
788xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
789 xmlElementContentPtr content)
790{
791 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
792 xmlElementPtr elem = NULL;
793
Daniel Veillard2728f842006-03-09 16:49:24 +0000794 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
795 return;
796
Daniel Veillard1af9a412003-08-20 22:54:39 +0000797#ifdef DEBUG_SAX
798 xmlGenericError(xmlGenericErrorContext,
799 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
800#endif
801
802 if (ctxt->inSubset == 1)
803 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
804 name, (xmlElementTypeVal) type, content);
805 else if (ctxt->inSubset == 2)
806 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
807 name, (xmlElementTypeVal) type, content);
808 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000809 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
810 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
811 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000812 return;
813 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000814#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000815 if (elem == NULL)
816 ctxt->valid = 0;
817 if (ctxt->validate && ctxt->wellFormed &&
818 ctxt->myDoc && ctxt->myDoc->intSubset)
819 ctxt->valid &=
820 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +0000821#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000822}
823
824/**
825 * xmlSAX2NotationDecl:
826 * @ctx: the user data (XML parser context)
827 * @name: The name of the notation
828 * @publicId: The public ID of the entity
829 * @systemId: The system ID of the entity
830 *
831 * What to do when a notation declaration has been parsed.
832 */
833void
834xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
835 const xmlChar *publicId, const xmlChar *systemId)
836{
837 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
838 xmlNotationPtr nota = NULL;
839
Daniel Veillard2728f842006-03-09 16:49:24 +0000840 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
841 return;
842
Daniel Veillard1af9a412003-08-20 22:54:39 +0000843#ifdef DEBUG_SAX
844 xmlGenericError(xmlGenericErrorContext,
845 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
846#endif
847
848 if ((publicId == NULL) && (systemId == NULL)) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000849 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
850 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
851 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000852 return;
853 } else if (ctxt->inSubset == 1)
854 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
855 publicId, systemId);
856 else if (ctxt->inSubset == 2)
857 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
858 publicId, systemId);
859 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000860 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
861 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
862 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000863 return;
864 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000865#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000866 if (nota == NULL) ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000867 if ((ctxt->validate) && (ctxt->wellFormed) &&
868 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000869 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
870 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000871#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000872}
873
874/**
875 * xmlSAX2UnparsedEntityDecl:
876 * @ctx: the user data (XML parser context)
877 * @name: The name of the entity
878 * @publicId: The public ID of the entity
879 * @systemId: The system ID of the entity
880 * @notationName: the name of the notation
881 *
882 * What to do when an unparsed entity declaration is parsed
883 */
884void
885xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
886 const xmlChar *publicId, const xmlChar *systemId,
887 const xmlChar *notationName)
888{
889 xmlEntityPtr ent;
890 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000891 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000892#ifdef DEBUG_SAX
893 xmlGenericError(xmlGenericErrorContext,
894 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
895 name, publicId, systemId, notationName);
896#endif
897 if (ctxt->inSubset == 1) {
898 ent = xmlAddDocEntity(ctxt->myDoc, name,
899 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
900 publicId, systemId, notationName);
901 if ((ent == NULL) && (ctxt->pedantic) &&
902 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000903 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000904 "Entity(%s) already defined in the internal subset\n", name);
905 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
906 xmlChar *URI;
907 const char *base = NULL;
908
909 if (ctxt->input != NULL)
910 base = ctxt->input->filename;
911 if (base == NULL)
912 base = ctxt->directory;
913
914 URI = xmlBuildURI(systemId, (const xmlChar *) base);
915 ent->URI = URI;
916 }
917 } else if (ctxt->inSubset == 2) {
918 ent = xmlAddDtdEntity(ctxt->myDoc, name,
919 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
920 publicId, systemId, notationName);
921 if ((ent == NULL) && (ctxt->pedantic) &&
922 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000923 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000924 "Entity(%s) already defined in the external subset\n", name);
925 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
926 xmlChar *URI;
927 const char *base = NULL;
928
929 if (ctxt->input != NULL)
930 base = ctxt->input->filename;
931 if (base == NULL)
932 base = ctxt->directory;
933
934 URI = xmlBuildURI(systemId, (const xmlChar *) base);
935 ent->URI = URI;
936 }
937 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000938 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
939 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
940 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000941 }
942}
943
944/**
945 * xmlSAX2SetDocumentLocator:
946 * @ctx: the user data (XML parser context)
947 * @loc: A SAX Locator
948 *
949 * Receive the document locator at startup, actually xmlDefaultSAXLocator
950 * Everything is available on the context, so this is useless in our case.
951 */
952void
953xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
954{
955 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
956#ifdef DEBUG_SAX
957 xmlGenericError(xmlGenericErrorContext,
958 "SAX.xmlSAX2SetDocumentLocator()\n");
959#endif
960}
961
962/**
963 * xmlSAX2StartDocument:
964 * @ctx: the user data (XML parser context)
965 *
966 * called when the document start being processed.
967 */
968void
969xmlSAX2StartDocument(void *ctx)
970{
971 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
972 xmlDocPtr doc;
973
Daniel Veillard34099b42004-11-04 17:34:35 +0000974 if (ctx == NULL) return;
975
Daniel Veillard1af9a412003-08-20 22:54:39 +0000976#ifdef DEBUG_SAX
977 xmlGenericError(xmlGenericErrorContext,
978 "SAX.xmlSAX2StartDocument()\n");
979#endif
980 if (ctxt->html) {
981#ifdef LIBXML_HTML_ENABLED
982 if (ctxt->myDoc == NULL)
983 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000984 ctxt->myDoc->properties = XML_DOC_HTML;
985 ctxt->myDoc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000986 if (ctxt->myDoc == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000987 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000988 return;
989 }
990#else
991 xmlGenericError(xmlGenericErrorContext,
992 "libxml2 built without HTML support\n");
993 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
994 ctxt->instate = XML_PARSER_EOF;
995 ctxt->disableSAX = 1;
996 return;
997#endif
998 } else {
999 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
1000 if (doc != NULL) {
Daniel Veillardae0765b2008-07-31 19:54:59 +00001001 doc->properties = 0;
1002 if (ctxt->options & XML_PARSE_OLD10)
1003 doc->properties |= XML_DOC_OLD10;
1004 doc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001005 if (ctxt->encoding != NULL)
1006 doc->encoding = xmlStrdup(ctxt->encoding);
1007 else
1008 doc->encoding = NULL;
1009 doc->standalone = ctxt->standalone;
1010 } else {
William M. Brack42331a92004-07-29 07:07:16 +00001011 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001012 return;
1013 }
Daniel Veillard500a1de2004-03-22 15:22:58 +00001014 if ((ctxt->dictNames) && (doc != NULL)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001015 doc->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00001016 xmlDictReference(doc->dict);
1017 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001018 }
1019 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1020 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillardb8efdda2006-10-10 12:37:14 +00001021 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001022 if (ctxt->myDoc->URL == NULL)
William M. Bracka3215c72004-07-31 16:24:01 +00001023 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001024 }
1025}
1026
1027/**
1028 * xmlSAX2EndDocument:
1029 * @ctx: the user data (XML parser context)
1030 *
1031 * called when the document end has been detected.
1032 */
1033void
1034xmlSAX2EndDocument(void *ctx)
1035{
1036 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1037#ifdef DEBUG_SAX
1038 xmlGenericError(xmlGenericErrorContext,
1039 "SAX.xmlSAX2EndDocument()\n");
1040#endif
Daniel Veillard34099b42004-11-04 17:34:35 +00001041 if (ctx == NULL) return;
Daniel Veillard4432df22003-09-28 18:58:27 +00001042#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001043 if (ctxt->validate && ctxt->wellFormed &&
1044 ctxt->myDoc && ctxt->myDoc->intSubset)
1045 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00001046#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001047
1048 /*
1049 * Grab the encoding if it was added on-the-fly
1050 */
1051 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1052 (ctxt->myDoc->encoding == NULL)) {
1053 ctxt->myDoc->encoding = ctxt->encoding;
1054 ctxt->encoding = NULL;
1055 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001056 if ((ctxt->inputTab != NULL) &&
1057 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1058 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001059 (ctxt->myDoc->encoding == NULL)) {
1060 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1061 }
1062 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1063 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1064 ctxt->myDoc->charset = ctxt->charset;
1065 }
1066}
1067
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001068#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001069/**
1070 * xmlSAX2AttributeInternal:
1071 * @ctx: the user data (XML parser context)
1072 * @fullname: The attribute name, including namespace prefix
1073 * @value: The attribute value
1074 * @prefix: the prefix on the element node
1075 *
1076 * Handle an attribute that has been read by the parser.
1077 * The default handling is to convert the attribute into an
1078 * DOM subtree and past it in a new xmlAttr element added to
1079 * the element.
1080 */
1081static void
1082xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001083 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001084{
1085 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1086 xmlAttrPtr ret;
1087 xmlChar *name;
1088 xmlChar *ns;
1089 xmlChar *nval;
1090 xmlNsPtr namespace;
1091
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001092 if (ctxt->html) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001093 name = xmlStrdup(fullname);
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001094 ns = NULL;
1095 namespace = NULL;
1096 } else {
1097 /*
1098 * Split the full name into a namespace prefix and the tag name
1099 */
1100 name = xmlSplitQName(ctxt, fullname, &ns);
1101 if ((name != NULL) && (name[0] == 0)) {
1102 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1103 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1104 "invalid namespace declaration '%s'\n",
1105 fullname, NULL);
1106 } else {
1107 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1108 "Avoid attribute ending with ':' like '%s'\n",
1109 fullname, NULL);
1110 }
1111 if (ns != NULL)
1112 xmlFree(ns);
1113 ns = NULL;
1114 xmlFree(name);
1115 name = xmlStrdup(fullname);
1116 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001117 }
1118 if (name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001119 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001120 if (ns != NULL)
1121 xmlFree(ns);
1122 return;
1123 }
1124
Daniel Veillard3c080d62010-03-15 15:47:50 +01001125#ifdef LIBXML_HTML_ENABLED
1126 if ((ctxt->html) &&
1127 (value == NULL) && (htmlIsBooleanAttr(fullname))) {
Daniel Veillard06c93b72010-03-15 16:08:44 +01001128 nval = xmlStrdup(fullname);
1129 value = (const xmlChar *) nval;
Daniel Veillard3c080d62010-03-15 15:47:50 +01001130 } else
1131#endif
1132 {
Daniel Veillard4432df22003-09-28 18:58:27 +00001133#ifdef LIBXML_VALID_ENABLED
Daniel Veillard3c080d62010-03-15 15:47:50 +01001134 /*
1135 * Do the last stage of the attribute normalization
1136 * Needed for HTML too:
1137 * http://www.w3.org/TR/html4/types.html#h-6.2
1138 */
1139 ctxt->vctxt.valid = 1;
1140 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1141 ctxt->myDoc, ctxt->node,
1142 fullname, value);
1143 if (ctxt->vctxt.valid != 1) {
1144 ctxt->valid = 0;
1145 }
1146 if (nval != NULL)
1147 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +00001148#else
Daniel Veillard3c080d62010-03-15 15:47:50 +01001149 nval = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +00001150#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard3c080d62010-03-15 15:47:50 +01001151 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001152
1153 /*
1154 * Check whether it's a namespace definition
1155 */
1156 if ((!ctxt->html) && (ns == NULL) &&
1157 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1158 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1159 xmlNsPtr nsret;
1160 xmlChar *val;
1161
1162 if (!ctxt->replaceEntities) {
1163 ctxt->depth++;
1164 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1165 0,0,0);
1166 ctxt->depth--;
1167 } else {
1168 val = (xmlChar *) value;
1169 }
1170
1171 if (val[0] != 0) {
1172 xmlURIPtr uri;
1173
1174 uri = xmlParseURI((const char *)val);
1175 if (uri == NULL) {
1176 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1177 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +00001178 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001179 } else {
1180 if (uri->scheme == NULL) {
1181 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1182 ctxt->sax->warning(ctxt->userData,
1183 "xmlns: URI %s is not absolute\n", val);
1184 }
1185 xmlFreeURI(uri);
1186 }
1187 }
1188
1189 /* a default namespace definition */
1190 nsret = xmlNewNs(ctxt->node, val, NULL);
1191
Daniel Veillard4432df22003-09-28 18:58:27 +00001192#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001193 /*
1194 * Validate also for namespace decls, they are attributes from
1195 * an XML-1.0 perspective
1196 */
1197 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1198 ctxt->myDoc && ctxt->myDoc->intSubset)
1199 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1200 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +00001201#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001202 if (name != NULL)
1203 xmlFree(name);
1204 if (nval != NULL)
1205 xmlFree(nval);
1206 if (val != value)
1207 xmlFree(val);
1208 return;
1209 }
1210 if ((!ctxt->html) &&
1211 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1212 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1213 xmlNsPtr nsret;
1214 xmlChar *val;
1215
1216 if (!ctxt->replaceEntities) {
1217 ctxt->depth++;
1218 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1219 0,0,0);
1220 ctxt->depth--;
1221 if (val == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001222 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001223 xmlFree(ns);
1224 if (name != NULL)
1225 xmlFree(name);
1226 return;
1227 }
1228 } else {
1229 val = (xmlChar *) value;
1230 }
1231
1232 if (val[0] == 0) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001233 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1234 "Empty namespace name for prefix %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001235 }
1236 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1237 xmlURIPtr uri;
1238
1239 uri = xmlParseURI((const char *)val);
1240 if (uri == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001241 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001242 "xmlns:%s: %s not a valid URI\n", name, value);
1243 } else {
1244 if (uri->scheme == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001245 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001246 "xmlns:%s: URI %s is not absolute\n", name, value);
1247 }
1248 xmlFreeURI(uri);
1249 }
1250 }
1251
1252 /* a standard namespace definition */
1253 nsret = xmlNewNs(ctxt->node, val, name);
1254 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001255#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001256 /*
1257 * Validate also for namespace decls, they are attributes from
1258 * an XML-1.0 perspective
1259 */
1260 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1261 ctxt->myDoc && ctxt->myDoc->intSubset)
1262 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1263 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001264#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001265 if (name != NULL)
1266 xmlFree(name);
1267 if (nval != NULL)
1268 xmlFree(nval);
1269 if (val != value)
1270 xmlFree(val);
1271 return;
1272 }
1273
1274 if (ns != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001275 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardd44b9362009-09-07 12:15:08 +02001276
Daniel Veillard67906942003-08-28 21:13:25 +00001277 if (namespace == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001278 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
William M. Brack4811ba32003-09-06 18:02:53 +00001279 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001280 ns, name);
Daniel Veillardd44b9362009-09-07 12:15:08 +02001281 } else {
1282 xmlAttrPtr prop;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001283
Daniel Veillardd44b9362009-09-07 12:15:08 +02001284 prop = ctxt->node->properties;
1285 while (prop != NULL) {
1286 if (prop->ns != NULL) {
1287 if ((xmlStrEqual(name, prop->name)) &&
1288 ((namespace == prop->ns) ||
1289 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1290 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1291 "Attribute %s in %s redefined\n",
1292 name, namespace->href);
1293 ctxt->wellFormed = 0;
1294 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1295 goto error;
1296 }
1297 }
1298 prop = prop->next;
1299 }
1300 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001301 } else {
1302 namespace = NULL;
1303 }
1304
1305 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1306 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1307
1308 if (ret != NULL) {
1309 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1310 xmlNodePtr tmp;
1311
1312 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1313 tmp = ret->children;
1314 while (tmp != NULL) {
1315 tmp->parent = (xmlNodePtr) ret;
1316 if (tmp->next == NULL)
1317 ret->last = tmp;
1318 tmp = tmp->next;
1319 }
1320 } else if (value != NULL) {
1321 ret->children = xmlNewDocText(ctxt->myDoc, value);
1322 ret->last = ret->children;
1323 if (ret->children != NULL)
1324 ret->children->parent = (xmlNodePtr) ret;
1325 }
1326 }
1327
Daniel Veillard4432df22003-09-28 18:58:27 +00001328#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001329 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1330 ctxt->myDoc && ctxt->myDoc->intSubset) {
1331
1332 /*
1333 * If we don't substitute entities, the validation should be
1334 * done on a value with replaced entities anyway.
1335 */
1336 if (!ctxt->replaceEntities) {
1337 xmlChar *val;
1338
1339 ctxt->depth++;
1340 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1341 0,0,0);
1342 ctxt->depth--;
1343
1344 if (val == NULL)
1345 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1346 ctxt->myDoc, ctxt->node, ret, value);
1347 else {
1348 xmlChar *nvalnorm;
1349
1350 /*
1351 * Do the last stage of the attribute normalization
1352 * It need to be done twice ... it's an extra burden related
1353 * to the ability to keep xmlSAX2References in attributes
1354 */
1355 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1356 ctxt->node, fullname, val);
1357 if (nvalnorm != NULL) {
1358 xmlFree(val);
1359 val = nvalnorm;
1360 }
1361
1362 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1363 ctxt->myDoc, ctxt->node, ret, val);
1364 xmlFree(val);
1365 }
1366 } else {
1367 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1368 ctxt->node, ret, value);
1369 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001370 } else
1371#endif /* LIBXML_VALID_ENABLED */
1372 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001373 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1374 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1375 /*
1376 * when validating, the ID registration is done at the attribute
1377 * validation level. Otherwise we have to do specific handling here.
1378 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001379 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001380 /*
1381 * Add the xml:id value
1382 *
1383 * Open issue: normalization of the value.
1384 */
Daniel Veillard68cb4b22004-04-18 20:55:39 +00001385 if (xmlValidateNCName(value, 1) != 0) {
1386 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1387 "xml:id : attribute value %s is not an NCName\n",
1388 (const char *) value, NULL);
1389 }
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001390 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001391 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1392 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1393 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1394 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001395 }
1396
1397error:
1398 if (nval != NULL)
1399 xmlFree(nval);
1400 if (ns != NULL)
1401 xmlFree(ns);
1402}
1403
Daniel Veillard1af9a412003-08-20 22:54:39 +00001404/*
1405 * xmlCheckDefaultedAttributes:
1406 *
1407 * Check defaulted attributes from the DTD
1408 */
1409static void
1410xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1411 const xmlChar *prefix, const xmlChar **atts) {
1412 xmlElementPtr elemDecl;
1413 const xmlChar *att;
1414 int internal = 1;
1415 int i;
1416
1417 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1418 if (elemDecl == NULL) {
1419 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1420 internal = 0;
1421 }
1422
1423process_external_subset:
1424
1425 if (elemDecl != NULL) {
1426 xmlAttributePtr attr = elemDecl->attributes;
1427 /*
1428 * Check against defaulted attributes from the external subset
1429 * if the document is stamped as standalone
1430 */
1431 if ((ctxt->myDoc->standalone == 1) &&
1432 (ctxt->myDoc->extSubset != NULL) &&
1433 (ctxt->validate)) {
1434 while (attr != NULL) {
1435 if ((attr->defaultValue != NULL) &&
1436 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1437 attr->elem, attr->name,
1438 attr->prefix) == attr) &&
1439 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1440 attr->elem, attr->name,
1441 attr->prefix) == NULL)) {
1442 xmlChar *fulln;
1443
1444 if (attr->prefix != NULL) {
1445 fulln = xmlStrdup(attr->prefix);
1446 fulln = xmlStrcat(fulln, BAD_CAST ":");
1447 fulln = xmlStrcat(fulln, attr->name);
1448 } else {
1449 fulln = xmlStrdup(attr->name);
1450 }
Jim Meyering669e88c2009-07-29 11:33:32 +02001451 if (fulln == NULL) {
1452 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1453 break;
1454 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001455
1456 /*
1457 * Check that the attribute is not declared in the
1458 * serialization
1459 */
1460 att = NULL;
1461 if (atts != NULL) {
1462 i = 0;
1463 att = atts[i];
1464 while (att != NULL) {
1465 if (xmlStrEqual(att, fulln))
1466 break;
1467 i += 2;
1468 att = atts[i];
1469 }
1470 }
1471 if (att == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001472 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001473 "standalone: attribute %s on %s defaulted from external subset\n",
Daniel Veillard427174f2003-12-10 10:42:59 +00001474 (const char *)fulln,
1475 (const char *)attr->elem);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001476 }
Daniel Veillard023d0ba2009-07-29 11:34:50 +02001477 xmlFree(fulln);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001478 }
1479 attr = attr->nexth;
1480 }
1481 }
1482
1483 /*
1484 * Actually insert defaulted values when needed
1485 */
1486 attr = elemDecl->attributes;
1487 while (attr != NULL) {
1488 /*
1489 * Make sure that attributes redefinition occuring in the
1490 * internal subset are not overriden by definitions in the
1491 * external subset.
1492 */
1493 if (attr->defaultValue != NULL) {
1494 /*
1495 * the element should be instantiated in the tree if:
1496 * - this is a namespace prefix
1497 * - the user required for completion in the tree
1498 * like XSLT
1499 * - there isn't already an attribute definition
1500 * in the internal subset overriding it.
1501 */
1502 if (((attr->prefix != NULL) &&
1503 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1504 ((attr->prefix == NULL) &&
1505 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1506 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1507 xmlAttributePtr tst;
1508
1509 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1510 attr->elem, attr->name,
1511 attr->prefix);
1512 if ((tst == attr) || (tst == NULL)) {
1513 xmlChar fn[50];
1514 xmlChar *fulln;
1515
1516 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1517 if (fulln == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001518 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001519 return;
1520 }
1521
1522 /*
1523 * Check that the attribute is not declared in the
1524 * serialization
1525 */
1526 att = NULL;
1527 if (atts != NULL) {
1528 i = 0;
1529 att = atts[i];
1530 while (att != NULL) {
1531 if (xmlStrEqual(att, fulln))
1532 break;
1533 i += 2;
1534 att = atts[i];
1535 }
1536 }
1537 if (att == NULL) {
1538 xmlSAX2AttributeInternal(ctxt, fulln,
1539 attr->defaultValue, prefix);
1540 }
1541 if ((fulln != fn) && (fulln != attr->name))
1542 xmlFree(fulln);
1543 }
1544 }
1545 }
1546 attr = attr->nexth;
1547 }
1548 if (internal == 1) {
1549 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1550 name, prefix);
1551 internal = 0;
1552 goto process_external_subset;
1553 }
1554 }
1555}
1556
1557/**
1558 * xmlSAX2StartElement:
1559 * @ctx: the user data (XML parser context)
1560 * @fullname: The element name, including namespace prefix
1561 * @atts: An array of name/value attributes pairs, NULL terminated
1562 *
1563 * called when an opening tag has been processed.
1564 */
1565void
1566xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1567{
1568 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1569 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001570 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001571 xmlNsPtr ns;
1572 xmlChar *name;
1573 xmlChar *prefix;
1574 const xmlChar *att;
1575 const xmlChar *value;
1576 int i;
1577
Daniel Veillarda521d282004-11-09 14:59:59 +00001578 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001579 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001580#ifdef DEBUG_SAX
1581 xmlGenericError(xmlGenericErrorContext,
1582 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1583#endif
1584
1585 /*
1586 * First check on validity:
1587 */
1588 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1589 ((ctxt->myDoc->intSubset == NULL) ||
1590 ((ctxt->myDoc->intSubset->notations == NULL) &&
1591 (ctxt->myDoc->intSubset->elements == NULL) &&
1592 (ctxt->myDoc->intSubset->attributes == NULL) &&
1593 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001594 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1595 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001596 ctxt->validate = 0;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001597 }
1598
1599
1600 /*
1601 * Split the full name into a namespace prefix and the tag name
1602 */
1603 name = xmlSplitQName(ctxt, fullname, &prefix);
1604
1605
1606 /*
1607 * Note : the namespace resolution is deferred until the end of the
1608 * attributes parsing, since local namespace can be defined as
1609 * an attribute at this level.
1610 */
1611 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1612 if (ret == NULL) {
1613 if (prefix != NULL)
1614 xmlFree(prefix);
William M. Brack42331a92004-07-29 07:07:16 +00001615 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001616 return;
1617 }
1618 if (ctxt->myDoc->children == NULL) {
1619#ifdef DEBUG_SAX_TREE
1620 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1621#endif
1622 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1623 } else if (parent == NULL) {
1624 parent = ctxt->myDoc->children;
1625 }
1626 ctxt->nodemem = -1;
1627 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001628 if (ctxt->input != NULL) {
1629 if (ctxt->input->line < 65535)
1630 ret->line = (short) ctxt->input->line;
1631 else
1632 ret->line = 65535;
1633 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001634 }
1635
1636 /*
1637 * We are parsing a new node.
1638 */
1639#ifdef DEBUG_SAX_TREE
1640 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1641#endif
1642 nodePush(ctxt, ret);
1643
1644 /*
1645 * Link the child element
1646 */
1647 if (parent != NULL) {
1648 if (parent->type == XML_ELEMENT_NODE) {
1649#ifdef DEBUG_SAX_TREE
1650 xmlGenericError(xmlGenericErrorContext,
1651 "adding child %s to %s\n", name, parent->name);
1652#endif
1653 xmlAddChild(parent, ret);
1654 } else {
1655#ifdef DEBUG_SAX_TREE
1656 xmlGenericError(xmlGenericErrorContext,
1657 "adding sibling %s to ", name);
1658 xmlDebugDumpOneNode(stderr, parent, 0);
1659#endif
1660 xmlAddSibling(parent, ret);
1661 }
1662 }
1663
1664 /*
1665 * Insert all the defaulted attributes from the DTD especially namespaces
1666 */
1667 if ((!ctxt->html) &&
1668 ((ctxt->myDoc->intSubset != NULL) ||
1669 (ctxt->myDoc->extSubset != NULL))) {
1670 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1671 }
1672
1673 /*
1674 * process all the attributes whose name start with "xmlns"
1675 */
1676 if (atts != NULL) {
1677 i = 0;
1678 att = atts[i++];
1679 value = atts[i++];
1680 if (!ctxt->html) {
1681 while ((att != NULL) && (value != NULL)) {
1682 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1683 (att[3] == 'n') && (att[4] == 's'))
1684 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1685
1686 att = atts[i++];
1687 value = atts[i++];
1688 }
1689 }
1690 }
1691
1692 /*
1693 * Search the namespace, note that since the attributes have been
1694 * processed, the local namespaces are available.
1695 */
1696 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1697 if ((ns == NULL) && (parent != NULL))
1698 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1699 if ((prefix != NULL) && (ns == NULL)) {
1700 ns = xmlNewNs(ret, NULL, prefix);
Daniel Veillard77aad342006-07-13 06:21:09 +00001701 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1702 "Namespace prefix %s is not defined\n",
1703 prefix, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001704 }
1705
1706 /*
1707 * set the namespace node, making sure that if the default namspace
1708 * is unbound on a parent we simply kee it NULL
1709 */
1710 if ((ns != NULL) && (ns->href != NULL) &&
1711 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1712 xmlSetNs(ret, ns);
1713
1714 /*
1715 * process all the other attributes
1716 */
1717 if (atts != NULL) {
1718 i = 0;
1719 att = atts[i++];
1720 value = atts[i++];
1721 if (ctxt->html) {
1722 while (att != NULL) {
1723 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1724 att = atts[i++];
1725 value = atts[i++];
1726 }
1727 } else {
1728 while ((att != NULL) && (value != NULL)) {
1729 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1730 (att[3] != 'n') || (att[4] != 's'))
1731 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1732
1733 /*
1734 * Next ones
1735 */
1736 att = atts[i++];
1737 value = atts[i++];
1738 }
1739 }
1740 }
1741
Daniel Veillard4432df22003-09-28 18:58:27 +00001742#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001743 /*
1744 * If it's the Document root, finish the DTD validation and
1745 * check the document root element for validity
1746 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00001747 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001748 int chk;
1749
1750 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1751 if (chk <= 0)
1752 ctxt->valid = 0;
1753 if (chk < 0)
1754 ctxt->wellFormed = 0;
1755 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00001756 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001757 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001758#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001759
1760 if (prefix != NULL)
1761 xmlFree(prefix);
1762
1763}
1764
1765/**
1766 * xmlSAX2EndElement:
1767 * @ctx: the user data (XML parser context)
1768 * @name: The element name
1769 *
1770 * called when the end of an element has been detected.
1771 */
1772void
1773xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1774{
1775 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001776 xmlNodePtr cur;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001777
Daniel Veillard34099b42004-11-04 17:34:35 +00001778 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001779 cur = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001780#ifdef DEBUG_SAX
1781 if (name == NULL)
1782 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1783 else
1784 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1785#endif
1786
1787 /* Capture end position and add node */
1788 if (cur != NULL && ctxt->record_info) {
Pavel Andrejs8ad4da52012-05-08 11:01:12 +08001789 ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
1790 ctxt->nodeInfo->end_line = ctxt->input->line;
1791 ctxt->nodeInfo->node = cur;
1792 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001793 }
1794 ctxt->nodemem = -1;
1795
Daniel Veillard4432df22003-09-28 18:58:27 +00001796#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001797 if (ctxt->validate && ctxt->wellFormed &&
1798 ctxt->myDoc && ctxt->myDoc->intSubset)
1799 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1800 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001801#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001802
1803
1804 /*
1805 * end of parsing of this node.
1806 */
1807#ifdef DEBUG_SAX_TREE
1808 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1809#endif
1810 nodePop(ctxt);
1811}
Daniel Veillard81273902003-09-30 00:43:48 +00001812#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001813
Daniel Veillarde57ec792003-09-10 10:50:59 +00001814/*
Daniel Veillard19895052003-09-17 13:59:32 +00001815 * xmlSAX2TextNode:
1816 * @ctxt: the parser context
1817 * @str: the input string
1818 * @len: the string length
1819 *
1820 * Remove the entities from an attribute value
1821 *
1822 * Returns the newly allocated string or NULL if not needed or error
1823 */
1824static xmlNodePtr
1825xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1826 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001827 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001828
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001829 /*
1830 * Allocate
1831 */
Daniel Veillard19895052003-09-17 13:59:32 +00001832 if (ctxt->freeElems != NULL) {
1833 ret = ctxt->freeElems;
1834 ctxt->freeElems = ret->next;
1835 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001836 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001837 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001838 }
1839 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001840 xmlErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard19895052003-09-17 13:59:32 +00001841 return(NULL);
1842 }
Daniel Veillard8874b942005-08-25 13:19:21 +00001843 memset(ret, 0, sizeof(xmlNode));
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001844 /*
1845 * intern the formatting blanks found between tags, or the
1846 * very short strings
1847 */
1848 if (ctxt->dictNames) {
1849 xmlChar cur = str[len];
1850
Daniel Veillard8874b942005-08-25 13:19:21 +00001851 if ((len < (int) (2 * sizeof(void *))) &&
1852 (ctxt->options & XML_PARSE_COMPACT)) {
1853 /* store the string in the node overrithing properties and nsDef */
1854 xmlChar *tmp = (xmlChar *) &(ret->properties);
1855 memcpy(tmp, str, len);
1856 tmp[len] = 0;
1857 intern = tmp;
1858 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001859 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001860 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001861 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001862 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001863 int i;
1864
1865 for (i = 1;i < len;i++) {
Daniel Veillard1a9b7082004-01-02 10:42:01 +00001866 if (!IS_BLANK_CH(str[i])) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001867 }
1868 intern = xmlDictLookup(ctxt->dict, str, len);
1869 }
1870 }
1871skip:
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001872 ret->type = XML_TEXT_NODE;
1873
1874 ret->name = xmlStringText;
William M. Brack9f797ab2004-07-28 07:40:12 +00001875 if (intern == NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001876 ret->content = xmlStrndup(str, len);
William M. Brack9f797ab2004-07-28 07:40:12 +00001877 if (ret->content == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001878 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1879 xmlFree(ret);
William M. Brack9f797ab2004-07-28 07:40:12 +00001880 return(NULL);
1881 }
1882 } else
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001883 ret->content = (xmlChar *) intern;
1884
Daniel Veillard45efd082008-07-07 13:52:52 +00001885 if (ctxt->input != NULL)
1886 ret->line = ctxt->input->line;
1887
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001888 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1889 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001890 return(ret);
1891}
1892
Daniel Veillard4432df22003-09-28 18:58:27 +00001893#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001894/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001895 * xmlSAX2DecodeAttrEntities:
1896 * @ctxt: the parser context
1897 * @str: the input string
1898 * @len: the string length
1899 *
1900 * Remove the entities from an attribute value
1901 *
1902 * Returns the newly allocated string or NULL if not needed or error
1903 */
1904static xmlChar *
1905xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1906 const xmlChar *end) {
1907 const xmlChar *in;
1908 xmlChar *ret;
1909
1910 in = str;
1911 while (in < end)
1912 if (*in++ == '&')
1913 goto decode;
1914 return(NULL);
1915decode:
1916 ctxt->depth++;
1917 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1918 XML_SUBSTITUTE_REF, 0,0,0);
1919 ctxt->depth--;
1920 return(ret);
1921}
Daniel Veillard4432df22003-09-28 18:58:27 +00001922#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001923
1924/**
1925 * xmlSAX2AttributeNs:
1926 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001927 * @localname: the local name of the attribute
1928 * @prefix: the attribute namespace prefix if available
1929 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001930 * @value: Start of the attribute value
1931 * @valueend: end of the attribute value
1932 *
1933 * Handle an attribute that has been read by the parser.
1934 * The default handling is to convert the attribute into an
1935 * DOM subtree and past it in a new xmlAttr element added to
1936 * the element.
1937 */
1938static void
1939xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1940 const xmlChar * localname,
1941 const xmlChar * prefix,
1942 const xmlChar * value,
1943 const xmlChar * valueend)
1944{
1945 xmlAttrPtr ret;
1946 xmlNsPtr namespace = NULL;
1947 xmlChar *dup = NULL;
1948
Daniel Veillarde57ec792003-09-10 10:50:59 +00001949 /*
1950 * Note: if prefix == NULL, the attribute is not in the default namespace
1951 */
1952 if (prefix != NULL)
1953 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1954
Daniel Veillard8a44e592003-09-15 14:50:06 +00001955 /*
1956 * allocate the node
1957 */
1958 if (ctxt->freeAttrs != NULL) {
1959 ret = ctxt->freeAttrs;
1960 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001961 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001962 memset(ret, 0, sizeof(xmlAttr));
1963 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001964
Daniel Veillard8a44e592003-09-15 14:50:06 +00001965 ret->parent = ctxt->node;
1966 ret->doc = ctxt->myDoc;
1967 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001968
Daniel Veillard8a44e592003-09-15 14:50:06 +00001969 if (ctxt->dictNames)
1970 ret->name = localname;
1971 else
1972 ret->name = xmlStrdup(localname);
1973
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001974 /* link at the end to preserv order, TODO speed up with a last */
1975 if (ctxt->node->properties == NULL) {
1976 ctxt->node->properties = ret;
1977 } else {
1978 xmlAttrPtr prev = ctxt->node->properties;
1979
1980 while (prev->next != NULL) prev = prev->next;
1981 prev->next = ret;
1982 ret->prev = prev;
1983 }
1984
Daniel Veillard8a44e592003-09-15 14:50:06 +00001985 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1986 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1987 } else {
1988 if (ctxt->dictNames)
1989 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1990 (xmlChar *) localname, NULL);
1991 else
1992 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1993 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001994 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00001995 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001996 }
1997 }
1998
Daniel Veillard8a44e592003-09-15 14:50:06 +00001999 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
2000 xmlNodePtr tmp;
2001
Daniel Veillard19895052003-09-17 13:59:32 +00002002 /*
2003 * We know that if there is an entity reference, then
2004 * the string has been dup'ed and terminates with 0
2005 * otherwise with ' or "
2006 */
2007 if (*valueend != 0) {
2008 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2009 ret->children = tmp;
2010 ret->last = tmp;
2011 if (tmp != NULL) {
2012 tmp->doc = ret->doc;
2013 tmp->parent = (xmlNodePtr) ret;
2014 }
2015 } else {
2016 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
2017 valueend - value);
2018 tmp = ret->children;
2019 while (tmp != NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00002020 tmp->doc = ret->doc;
Daniel Veillard19895052003-09-17 13:59:32 +00002021 tmp->parent = (xmlNodePtr) ret;
2022 if (tmp->next == NULL)
2023 ret->last = tmp;
2024 tmp = tmp->next;
2025 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002026 }
2027 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002028 xmlNodePtr tmp;
2029
2030 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2031 ret->children = tmp;
2032 ret->last = tmp;
2033 if (tmp != NULL) {
2034 tmp->doc = ret->doc;
2035 tmp->parent = (xmlNodePtr) ret;
2036 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002037 }
2038
Daniel Veillard4432df22003-09-28 18:58:27 +00002039#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002040 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2041 ctxt->myDoc && ctxt->myDoc->intSubset) {
2042 /*
2043 * If we don't substitute entities, the validation should be
2044 * done on a value with replaced entities anyway.
2045 */
2046 if (!ctxt->replaceEntities) {
2047 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2048 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00002049 if (*valueend == 0) {
2050 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2051 ctxt->myDoc, ctxt->node, ret, value);
2052 } else {
2053 /*
2054 * That should already be normalized.
2055 * cheaper to finally allocate here than duplicate
2056 * entry points in the full validation code
2057 */
2058 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002059
Daniel Veillard62998c02003-09-15 12:56:36 +00002060 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2061 ctxt->myDoc, ctxt->node, ret, dup);
2062 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002063 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00002064 /*
2065 * dup now contains a string of the flattened attribute
2066 * content with entities substitued. Check if we need to
2067 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002068 * It need to be done twice ... it's an extra burden related
2069 * to the ability to keep references in attributes
2070 */
Daniel Veillard62998c02003-09-15 12:56:36 +00002071 if (ctxt->attsSpecial != NULL) {
2072 xmlChar *nvalnorm;
2073 xmlChar fn[50];
2074 xmlChar *fullname;
2075
2076 fullname = xmlBuildQName(localname, prefix, fn, 50);
2077 if (fullname != NULL) {
2078 ctxt->vctxt.valid = 1;
2079 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2080 &ctxt->vctxt, ctxt->myDoc,
2081 ctxt->node, fullname, dup);
2082 if (ctxt->vctxt.valid != 1)
2083 ctxt->valid = 0;
2084
2085 if ((fullname != fn) && (fullname != localname))
2086 xmlFree(fullname);
2087 if (nvalnorm != NULL) {
2088 xmlFree(dup);
2089 dup = nvalnorm;
2090 }
2091 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002092 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002093
2094 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2095 ctxt->myDoc, ctxt->node, ret, dup);
2096 }
2097 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002098 /*
2099 * if entities already have been substitued, then
2100 * the attribute as passed is already normalized
2101 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002102 dup = xmlStrndup(value, valueend - value);
2103
2104 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2105 ctxt->myDoc, ctxt->node, ret, dup);
2106 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002107 } else
2108#endif /* LIBXML_VALID_ENABLED */
2109 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002110 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2111 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2112 /*
2113 * when validating, the ID registration is done at the attribute
2114 * validation level. Otherwise we have to do specific handling here.
2115 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002116 if ((prefix == ctxt->str_xml) &&
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002117 (localname[0] == 'i') && (localname[1] == 'd') &&
2118 (localname[2] == 0)) {
2119 /*
2120 * Add the xml:id value
2121 *
2122 * Open issue: normalization of the value.
2123 */
2124 if (dup == NULL)
2125 dup = xmlStrndup(value, valueend - value);
William M. Brack5ef2f812004-05-23 23:56:47 +00002126#ifdef LIBXML_VALID_ENABLED
Daniel Veillard68cb4b22004-04-18 20:55:39 +00002127 if (xmlValidateNCName(dup, 1) != 0) {
2128 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2129 "xml:id : attribute value %s is not an NCName\n",
2130 (const char *) dup, NULL);
2131 }
William M. Brack3f147372004-05-22 01:09:26 +00002132#endif
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002133 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002134 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2135 /* might be worth duplicate entry points and not copy */
2136 if (dup == NULL)
2137 dup = xmlStrndup(value, valueend - value);
2138 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2139 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2140 if (dup == NULL)
2141 dup = xmlStrndup(value, valueend - value);
2142 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002143 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002144 }
2145 if (dup != NULL)
2146 xmlFree(dup);
2147}
2148
2149/**
2150 * xmlSAX2StartElementNs:
2151 * @ctx: the user data (XML parser context)
2152 * @localname: the local name of the element
2153 * @prefix: the element namespace prefix if available
2154 * @URI: the element namespace name if available
2155 * @nb_namespaces: number of namespace definitions on that node
2156 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2157 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002158 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002159 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2160 * attribute values.
2161 *
2162 * SAX2 callback when an element start has been detected by the parser.
2163 * It provides the namespace informations for the element, as well as
2164 * the new namespace declarations on the element.
2165 */
2166void
2167xmlSAX2StartElementNs(void *ctx,
2168 const xmlChar *localname,
2169 const xmlChar *prefix,
2170 const xmlChar *URI,
2171 int nb_namespaces,
2172 const xmlChar **namespaces,
2173 int nb_attributes,
2174 int nb_defaulted,
2175 const xmlChar **attributes)
2176{
2177 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2178 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002179 xmlNodePtr parent;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002180 xmlNsPtr last = NULL, ns;
2181 const xmlChar *uri, *pref;
Daniel Veillard77b77b12012-01-26 19:11:02 +08002182 xmlChar *lname = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002183 int i, j;
2184
Daniel Veillard34099b42004-11-04 17:34:35 +00002185 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002186 parent = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002187 /*
2188 * First check on validity:
2189 */
2190 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2191 ((ctxt->myDoc->intSubset == NULL) ||
2192 ((ctxt->myDoc->intSubset->notations == NULL) &&
2193 (ctxt->myDoc->intSubset->elements == NULL) &&
2194 (ctxt->myDoc->intSubset->attributes == NULL) &&
2195 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00002196 xmlErrValid(ctxt, XML_ERR_NO_DTD,
2197 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002198 ctxt->validate = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002199 }
2200
Daniel Veillard8a44e592003-09-15 14:50:06 +00002201 /*
Daniel Veillard77b77b12012-01-26 19:11:02 +08002202 * Take care of the rare case of an undefined namespace prefix
2203 */
2204 if ((prefix != NULL) && (URI == NULL)) {
2205 if (ctxt->dictNames) {
2206 const xmlChar *fullname;
2207
2208 fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
2209 if (fullname != NULL)
2210 localname = fullname;
2211 } else {
2212 lname = xmlBuildQName(localname, prefix, NULL, 0);
2213 }
2214 }
2215 /*
Daniel Veillard8a44e592003-09-15 14:50:06 +00002216 * allocate the node
2217 */
2218 if (ctxt->freeElems != NULL) {
2219 ret = ctxt->freeElems;
2220 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00002221 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00002222 memset(ret, 0, sizeof(xmlNode));
2223 ret->type = XML_ELEMENT_NODE;
2224
2225 if (ctxt->dictNames)
2226 ret->name = localname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002227 else {
Daniel Veillard77b77b12012-01-26 19:11:02 +08002228 if (lname == NULL)
2229 ret->name = xmlStrdup(localname);
2230 else
2231 ret->name = lname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002232 if (ret->name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002233 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002234 return;
2235 }
2236 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002237 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2238 xmlRegisterNodeDefaultValue(ret);
2239 } else {
2240 if (ctxt->dictNames)
2241 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2242 (xmlChar *) localname, NULL);
Daniel Veillard77b77b12012-01-26 19:11:02 +08002243 else if (lname == NULL)
Daniel Veillard8a44e592003-09-15 14:50:06 +00002244 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
Daniel Veillard77b77b12012-01-26 19:11:02 +08002245 else
2246 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2247 (xmlChar *) lname, NULL);
Daniel Veillard8a44e592003-09-15 14:50:06 +00002248 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002249 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002250 return;
2251 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002252 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002253 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00002254 if (ctxt->input != NULL) {
2255 if (ctxt->input->line < 65535)
2256 ret->line = (short) ctxt->input->line;
2257 else
2258 ret->line = 65535;
2259 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002260 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002261
Tim Elliott71a243d2012-01-17 19:25:08 -08002262 if (parent == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002263 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002264 }
2265 /*
2266 * Build the namespace list
2267 */
2268 for (i = 0,j = 0;j < nb_namespaces;j++) {
2269 pref = namespaces[i++];
2270 uri = namespaces[i++];
2271 ns = xmlNewNs(NULL, uri, pref);
2272 if (ns != NULL) {
2273 if (last == NULL) {
2274 ret->nsDef = last = ns;
2275 } else {
2276 last->next = ns;
2277 last = ns;
2278 }
2279 if ((URI != NULL) && (prefix == pref))
2280 ret->ns = ns;
2281 } else {
Daniel Veillardaa54d372010-09-09 18:17:47 +02002282 /*
2283 * any out of memory error would already have been raised
2284 * but we can't be garanteed it's the actual error due to the
2285 * API, best is to skip in this case
2286 */
2287 continue;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002288 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002289#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002290 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2291 ctxt->myDoc && ctxt->myDoc->intSubset) {
2292 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2293 ret, prefix, ns, uri);
2294 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002295#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002296 }
2297 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002298
2299 /*
2300 * We are parsing a new node.
2301 */
2302 nodePush(ctxt, ret);
2303
2304 /*
2305 * Link the child element
2306 */
2307 if (parent != NULL) {
2308 if (parent->type == XML_ELEMENT_NODE) {
2309 xmlAddChild(parent, ret);
2310 } else {
2311 xmlAddSibling(parent, ret);
2312 }
2313 }
2314
2315 /*
2316 * Insert the defaulted attributes from the DTD only if requested:
2317 */
2318 if ((nb_defaulted != 0) &&
2319 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2320 nb_attributes -= nb_defaulted;
2321
2322 /*
2323 * Search the namespace if it wasn't already found
William M. Brackbf5cf212004-08-31 06:47:17 +00002324 * Note that, if prefix is NULL, this searches for the default Ns
Daniel Veillarde57ec792003-09-10 10:50:59 +00002325 */
2326 if ((URI != NULL) && (ret->ns == NULL)) {
William M. Brackbf5cf212004-08-31 06:47:17 +00002327 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002328 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2329 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2330 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002331 if (ret->ns == NULL) {
2332 ns = xmlNewNs(ret, NULL, prefix);
William M. Brack9f797ab2004-07-28 07:40:12 +00002333 if (ns == NULL) {
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002334
William M. Brack42331a92004-07-29 07:07:16 +00002335 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002336 return;
2337 }
Daniel Veillard74eaec12009-08-26 15:57:20 +02002338 if (prefix != NULL)
2339 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2340 "Namespace prefix %s was not found\n",
2341 prefix, NULL);
2342 else
2343 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2344 "Namespace default prefix was not found\n",
2345 NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002346 }
2347 }
2348
2349 /*
2350 * process all the other attributes
2351 */
2352 if (nb_attributes > 0) {
2353 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
Daniel Veillard1c989272012-01-26 19:43:06 +08002354 /*
2355 * Handle the rare case of an undefined atribute prefix
2356 */
2357 if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
2358 if (ctxt->dictNames) {
2359 const xmlChar *fullname;
2360
2361 fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
2362 attributes[j]);
2363 if (fullname != NULL) {
2364 xmlSAX2AttributeNs(ctxt, fullname, NULL,
2365 attributes[j+3], attributes[j+4]);
2366 continue;
2367 }
2368 } else {
2369 lname = xmlBuildQName(attributes[j], attributes[j+1],
2370 NULL, 0);
2371 if (lname != NULL) {
2372 xmlSAX2AttributeNs(ctxt, lname, NULL,
2373 attributes[j+3], attributes[j+4]);
2374 xmlFree(lname);
2375 continue;
2376 }
2377 }
2378 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002379 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
Daniel Veillard1c989272012-01-26 19:43:06 +08002380 attributes[j+3], attributes[j+4]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002381 }
2382 }
2383
Daniel Veillard4432df22003-09-28 18:58:27 +00002384#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002385 /*
2386 * If it's the Document root, finish the DTD validation and
2387 * check the document root element for validity
2388 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00002389 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002390 int chk;
2391
2392 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2393 if (chk <= 0)
2394 ctxt->valid = 0;
2395 if (chk < 0)
2396 ctxt->wellFormed = 0;
2397 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00002398 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002399 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002400#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002401}
2402
2403/**
2404 * xmlSAX2EndElementNs:
2405 * @ctx: the user data (XML parser context)
2406 * @localname: the local name of the element
2407 * @prefix: the element namespace prefix if available
2408 * @URI: the element namespace name if available
2409 *
2410 * SAX2 callback when an element end has been detected by the parser.
2411 * It provides the namespace informations for the element.
2412 */
2413void
2414xmlSAX2EndElementNs(void *ctx,
2415 const xmlChar * localname ATTRIBUTE_UNUSED,
2416 const xmlChar * prefix ATTRIBUTE_UNUSED,
2417 const xmlChar * URI ATTRIBUTE_UNUSED)
2418{
2419 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2420 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002421 xmlNodePtr cur;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002422
Daniel Veillard34099b42004-11-04 17:34:35 +00002423 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002424 cur = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002425 /* Capture end position and add node */
2426 if ((ctxt->record_info) && (cur != NULL)) {
2427 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2428 node_info.end_line = ctxt->input->line;
2429 node_info.node = cur;
2430 xmlParserAddNodeInfo(ctxt, &node_info);
2431 }
2432 ctxt->nodemem = -1;
2433
Daniel Veillard4432df22003-09-28 18:58:27 +00002434#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002435 if (ctxt->validate && ctxt->wellFormed &&
2436 ctxt->myDoc && ctxt->myDoc->intSubset)
2437 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002438#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002439
2440 /*
2441 * end of parsing of this node.
2442 */
2443 nodePop(ctxt);
2444}
2445
Daniel Veillard1af9a412003-08-20 22:54:39 +00002446/**
2447 * xmlSAX2Reference:
2448 * @ctx: the user data (XML parser context)
2449 * @name: The entity name
2450 *
2451 * called when an entity xmlSAX2Reference is detected.
2452 */
2453void
2454xmlSAX2Reference(void *ctx, const xmlChar *name)
2455{
2456 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2457 xmlNodePtr ret;
2458
Daniel Veillard34099b42004-11-04 17:34:35 +00002459 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002460#ifdef DEBUG_SAX
2461 xmlGenericError(xmlGenericErrorContext,
2462 "SAX.xmlSAX2Reference(%s)\n", name);
2463#endif
2464 if (name[0] == '#')
2465 ret = xmlNewCharRef(ctxt->myDoc, name);
2466 else
2467 ret = xmlNewReference(ctxt->myDoc, name);
2468#ifdef DEBUG_SAX_TREE
2469 xmlGenericError(xmlGenericErrorContext,
2470 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2471#endif
Daniel Veillardb242b082008-02-08 09:56:31 +00002472 if (xmlAddChild(ctxt->node, ret) == NULL) {
2473 xmlFreeNode(ret);
2474 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002475}
2476
2477/**
2478 * xmlSAX2Characters:
2479 * @ctx: the user data (XML parser context)
2480 * @ch: a xmlChar string
2481 * @len: the number of xmlChar
2482 *
2483 * receiving some chars from the parser.
2484 */
2485void
2486xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2487{
2488 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2489 xmlNodePtr lastChild;
2490
Daniel Veillard34099b42004-11-04 17:34:35 +00002491 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002492#ifdef DEBUG_SAX
2493 xmlGenericError(xmlGenericErrorContext,
2494 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2495#endif
2496 /*
2497 * Handle the data if any. If there is no child
2498 * add it as content, otherwise if the last child is text,
2499 * concatenate it, else create a new node of type text.
2500 */
2501
2502 if (ctxt->node == NULL) {
2503#ifdef DEBUG_SAX_TREE
2504 xmlGenericError(xmlGenericErrorContext,
2505 "add chars: ctxt->node == NULL !\n");
2506#endif
2507 return;
2508 }
Daniel Veillard19895052003-09-17 13:59:32 +00002509 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002510#ifdef DEBUG_SAX_TREE
2511 xmlGenericError(xmlGenericErrorContext,
2512 "add chars to %s \n", ctxt->node->name);
2513#endif
2514
2515 /*
2516 * Here we needed an accelerator mechanism in case of very large
2517 * elements. Use an attribute in the structure !!!
2518 */
2519 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002520 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2521 if (lastChild != NULL) {
2522 ctxt->node->children = lastChild;
2523 ctxt->node->last = lastChild;
2524 lastChild->parent = ctxt->node;
2525 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002526 ctxt->nodelen = len;
2527 ctxt->nodemem = len + 1;
William M. Bracka3215c72004-07-31 16:24:01 +00002528 } else {
2529 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2530 return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002531 }
2532 } else {
2533 int coalesceText = (lastChild != NULL) &&
2534 (lastChild->type == XML_TEXT_NODE) &&
2535 (lastChild->name == xmlStringText);
2536 if ((coalesceText) && (ctxt->nodemem != 0)) {
2537 /*
2538 * The whole point of maintaining nodelen and nodemem,
2539 * xmlTextConcat is too costly, i.e. compute length,
2540 * reallocate a new buffer, move data, append ch. Here
2541 * We try to minimaze realloc() uses and avoid copying
2542 * and recomputing length over and over.
2543 */
Daniel Veillard8874b942005-08-25 13:19:21 +00002544 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2545 lastChild->content = xmlStrdup(lastChild->content);
2546 lastChild->properties = NULL;
2547 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2548 (xmlDictOwns(ctxt->dict, lastChild->content))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002549 lastChild->content = xmlStrdup(lastChild->content);
2550 }
Daniel Veillard97ff9b32009-01-18 21:43:30 +00002551 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002552 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2553 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2554 return;
2555 }
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002556 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
2557 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
Daniel Veillard1fb2e0d2009-01-18 14:08:36 +00002558 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2559 return;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002560 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002561 if (ctxt->nodelen + len >= ctxt->nodemem) {
2562 xmlChar *newbuf;
Daniel Veillard1dc9feb2008-11-17 15:59:21 +00002563 size_t size;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002564
2565 size = ctxt->nodemem + len;
2566 size *= 2;
2567 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2568 if (newbuf == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002569 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002570 return;
2571 }
2572 ctxt->nodemem = size;
2573 lastChild->content = newbuf;
2574 }
2575 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2576 ctxt->nodelen += len;
2577 lastChild->content[ctxt->nodelen] = 0;
2578 } else if (coalesceText) {
2579 if (xmlTextConcat(lastChild, ch, len)) {
William M. Brack42331a92004-07-29 07:07:16 +00002580 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002581 }
2582 if (ctxt->node->children != NULL) {
2583 ctxt->nodelen = xmlStrlen(lastChild->content);
2584 ctxt->nodemem = ctxt->nodelen + 1;
2585 }
2586 } else {
2587 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002588 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2589 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002590 xmlAddChild(ctxt->node, lastChild);
2591 if (ctxt->node->children != NULL) {
2592 ctxt->nodelen = len;
2593 ctxt->nodemem = len + 1;
2594 }
2595 }
2596 }
2597 }
2598}
2599
2600/**
2601 * xmlSAX2IgnorableWhitespace:
2602 * @ctx: the user data (XML parser context)
2603 * @ch: a xmlChar string
2604 * @len: the number of xmlChar
2605 *
2606 * receiving some ignorable whitespaces from the parser.
2607 * UNUSED: by default the DOM building will use xmlSAX2Characters
2608 */
2609void
2610xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2611{
2612 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2613#ifdef DEBUG_SAX
2614 xmlGenericError(xmlGenericErrorContext,
2615 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2616#endif
2617}
2618
2619/**
2620 * xmlSAX2ProcessingInstruction:
2621 * @ctx: the user data (XML parser context)
2622 * @target: the target name
2623 * @data: the PI data's
2624 *
2625 * A processing instruction has been parsed.
2626 */
2627void
2628xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2629 const xmlChar *data)
2630{
2631 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2632 xmlNodePtr ret;
Daniel Veillard6128c012004-11-08 17:16:15 +00002633 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002634
Daniel Veillard34099b42004-11-04 17:34:35 +00002635 if (ctx == NULL) return;
Daniel Veillard6128c012004-11-08 17:16:15 +00002636 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002637#ifdef DEBUG_SAX
2638 xmlGenericError(xmlGenericErrorContext,
2639 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2640#endif
2641
Daniel Veillard03a53c32004-10-26 16:06:51 +00002642 ret = xmlNewDocPI(ctxt->myDoc, target, data);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002643 if (ret == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002644
Daniel Veillard73da77e2005-08-24 14:05:37 +00002645 if (ctxt->linenumbers) {
2646 if (ctxt->input != NULL) {
2647 if (ctxt->input->line < 65535)
2648 ret->line = (short) ctxt->input->line;
2649 else
2650 ret->line = 65535;
2651 }
2652 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002653 if (ctxt->inSubset == 1) {
2654 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2655 return;
2656 } else if (ctxt->inSubset == 2) {
2657 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2658 return;
2659 }
Tim Elliott71a243d2012-01-17 19:25:08 -08002660 if (parent == NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002661#ifdef DEBUG_SAX_TREE
2662 xmlGenericError(xmlGenericErrorContext,
2663 "Setting PI %s as root\n", target);
2664#endif
2665 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2666 return;
2667 }
2668 if (parent->type == XML_ELEMENT_NODE) {
2669#ifdef DEBUG_SAX_TREE
2670 xmlGenericError(xmlGenericErrorContext,
2671 "adding PI %s child to %s\n", target, parent->name);
2672#endif
2673 xmlAddChild(parent, ret);
2674 } else {
2675#ifdef DEBUG_SAX_TREE
2676 xmlGenericError(xmlGenericErrorContext,
2677 "adding PI %s sibling to ", target);
2678 xmlDebugDumpOneNode(stderr, parent, 0);
2679#endif
2680 xmlAddSibling(parent, ret);
2681 }
2682}
2683
2684/**
2685 * xmlSAX2Comment:
2686 * @ctx: the user data (XML parser context)
2687 * @value: the xmlSAX2Comment content
2688 *
2689 * A xmlSAX2Comment has been parsed.
2690 */
2691void
2692xmlSAX2Comment(void *ctx, const xmlChar *value)
2693{
2694 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2695 xmlNodePtr ret;
Daniel Veillard34099b42004-11-04 17:34:35 +00002696 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002697
Daniel Veillard34099b42004-11-04 17:34:35 +00002698 if (ctx == NULL) return;
2699 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002700#ifdef DEBUG_SAX
2701 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2702#endif
2703 ret = xmlNewDocComment(ctxt->myDoc, value);
2704 if (ret == NULL) return;
Daniel Veillard73da77e2005-08-24 14:05:37 +00002705 if (ctxt->linenumbers) {
2706 if (ctxt->input != NULL) {
2707 if (ctxt->input->line < 65535)
2708 ret->line = (short) ctxt->input->line;
2709 else
2710 ret->line = 65535;
2711 }
2712 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002713
2714 if (ctxt->inSubset == 1) {
2715 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2716 return;
2717 } else if (ctxt->inSubset == 2) {
2718 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2719 return;
2720 }
Tim Elliott71a243d2012-01-17 19:25:08 -08002721 if (parent == NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002722#ifdef DEBUG_SAX_TREE
2723 xmlGenericError(xmlGenericErrorContext,
2724 "Setting xmlSAX2Comment as root\n");
2725#endif
2726 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2727 return;
2728 }
2729 if (parent->type == XML_ELEMENT_NODE) {
2730#ifdef DEBUG_SAX_TREE
2731 xmlGenericError(xmlGenericErrorContext,
2732 "adding xmlSAX2Comment child to %s\n", parent->name);
2733#endif
2734 xmlAddChild(parent, ret);
2735 } else {
2736#ifdef DEBUG_SAX_TREE
2737 xmlGenericError(xmlGenericErrorContext,
2738 "adding xmlSAX2Comment sibling to ");
2739 xmlDebugDumpOneNode(stderr, parent, 0);
2740#endif
2741 xmlAddSibling(parent, ret);
2742 }
2743}
2744
2745/**
2746 * xmlSAX2CDataBlock:
2747 * @ctx: the user data (XML parser context)
2748 * @value: The pcdata content
2749 * @len: the block length
2750 *
2751 * called when a pcdata block has been parsed
2752 */
2753void
2754xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2755{
2756 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2757 xmlNodePtr ret, lastChild;
2758
Daniel Veillard34099b42004-11-04 17:34:35 +00002759 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002760#ifdef DEBUG_SAX
2761 xmlGenericError(xmlGenericErrorContext,
2762 "SAX.pcdata(%.10s, %d)\n", value, len);
2763#endif
2764 lastChild = xmlGetLastChild(ctxt->node);
2765#ifdef DEBUG_SAX_TREE
2766 xmlGenericError(xmlGenericErrorContext,
2767 "add chars to %s \n", ctxt->node->name);
2768#endif
2769 if ((lastChild != NULL) &&
2770 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2771 xmlTextConcat(lastChild, value, len);
2772 } else {
2773 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2774 xmlAddChild(ctxt->node, ret);
2775 }
2776}
2777
Daniel Veillard62998c02003-09-15 12:56:36 +00002778static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002779
Daniel Veillard81273902003-09-30 00:43:48 +00002780#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002781/**
2782 * xmlSAXDefaultVersion:
2783 * @version: the version, 1 or 2
2784 *
2785 * Set the default version of SAX used globally by the library.
William M. Brack96d2eff2004-06-30 11:48:47 +00002786 * By default, during initialization the default is set to 2.
2787 * Note that it is generally a better coding style to use
2788 * xmlSAXVersion() to set up the version explicitly for a given
2789 * parsing context.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002790 *
2791 * Returns the previous value in case of success and -1 in case of error.
2792 */
2793int
2794xmlSAXDefaultVersion(int version)
2795{
2796 int ret = xmlSAX2DefaultVersionValue;
2797
2798 if ((version != 1) && (version != 2))
2799 return(-1);
2800 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002801 return(ret);
2802}
Daniel Veillard81273902003-09-30 00:43:48 +00002803#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002804
2805/**
2806 * xmlSAXVersion:
2807 * @hdlr: the SAX handler
2808 * @version: the version, 1 or 2
2809 *
2810 * Initialize the default XML SAX handler according to the version
2811 *
2812 * Returns 0 in case of success and -1 in case of error.
2813 */
2814int
2815xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2816{
2817 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002818 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002819 hdlr->startElement = NULL;
2820 hdlr->endElement = NULL;
2821 hdlr->startElementNs = xmlSAX2StartElementNs;
2822 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002823 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002824 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002825#ifdef LIBXML_SAX1_ENABLED
2826 } else if (version == 1) {
2827 hdlr->startElement = xmlSAX2StartElement;
2828 hdlr->endElement = xmlSAX2EndElement;
2829 hdlr->initialized = 1;
2830#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002831 } else
2832 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002833 hdlr->internalSubset = xmlSAX2InternalSubset;
2834 hdlr->externalSubset = xmlSAX2ExternalSubset;
2835 hdlr->isStandalone = xmlSAX2IsStandalone;
2836 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2837 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2838 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2839 hdlr->getEntity = xmlSAX2GetEntity;
2840 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2841 hdlr->entityDecl = xmlSAX2EntityDecl;
2842 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2843 hdlr->elementDecl = xmlSAX2ElementDecl;
2844 hdlr->notationDecl = xmlSAX2NotationDecl;
2845 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2846 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2847 hdlr->startDocument = xmlSAX2StartDocument;
2848 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002849 hdlr->reference = xmlSAX2Reference;
2850 hdlr->characters = xmlSAX2Characters;
2851 hdlr->cdataBlock = xmlSAX2CDataBlock;
2852 hdlr->ignorableWhitespace = xmlSAX2Characters;
2853 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2854 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002855 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002856 hdlr->error = xmlParserError;
2857 hdlr->fatalError = xmlParserError;
2858
Daniel Veillarde57ec792003-09-10 10:50:59 +00002859 return(0);
2860}
2861
2862/**
2863 * xmlSAX2InitDefaultSAXHandler:
2864 * @hdlr: the SAX handler
2865 * @warning: flag if non-zero sets the handler warning procedure
2866 *
2867 * Initialize the default XML SAX2 handler
2868 */
2869void
2870xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2871{
2872 if ((hdlr == NULL) || (hdlr->initialized != 0))
2873 return;
2874
2875 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2876 if (warning == 0)
2877 hdlr->warning = NULL;
2878 else
2879 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002880}
2881
2882/**
2883 * xmlDefaultSAXHandlerInit:
2884 *
2885 * Initialize the default SAX2 handler
2886 */
2887void
2888xmlDefaultSAXHandlerInit(void)
2889{
Daniel Veillard81273902003-09-30 00:43:48 +00002890#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002891 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002892#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002893}
2894
2895#ifdef LIBXML_HTML_ENABLED
2896
2897/**
2898 * xmlSAX2InitHtmlDefaultSAXHandler:
2899 * @hdlr: the SAX handler
2900 *
2901 * Initialize the default HTML SAX2 handler
2902 */
2903void
2904xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2905{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002906 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002907 return;
2908
2909 hdlr->internalSubset = xmlSAX2InternalSubset;
2910 hdlr->externalSubset = NULL;
2911 hdlr->isStandalone = NULL;
2912 hdlr->hasInternalSubset = NULL;
2913 hdlr->hasExternalSubset = NULL;
2914 hdlr->resolveEntity = NULL;
2915 hdlr->getEntity = xmlSAX2GetEntity;
2916 hdlr->getParameterEntity = NULL;
2917 hdlr->entityDecl = NULL;
2918 hdlr->attributeDecl = NULL;
2919 hdlr->elementDecl = NULL;
2920 hdlr->notationDecl = NULL;
2921 hdlr->unparsedEntityDecl = NULL;
2922 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2923 hdlr->startDocument = xmlSAX2StartDocument;
2924 hdlr->endDocument = xmlSAX2EndDocument;
2925 hdlr->startElement = xmlSAX2StartElement;
2926 hdlr->endElement = xmlSAX2EndElement;
2927 hdlr->reference = NULL;
2928 hdlr->characters = xmlSAX2Characters;
2929 hdlr->cdataBlock = xmlSAX2CDataBlock;
2930 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002931 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002932 hdlr->comment = xmlSAX2Comment;
2933 hdlr->warning = xmlParserWarning;
2934 hdlr->error = xmlParserError;
2935 hdlr->fatalError = xmlParserError;
2936
Daniel Veillard092643b2003-09-25 14:29:29 +00002937 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002938}
2939
2940/**
2941 * htmlDefaultSAXHandlerInit:
2942 *
2943 * Initialize the default SAX handler
2944 */
2945void
2946htmlDefaultSAXHandlerInit(void)
2947{
Daniel Veillard092643b2003-09-25 14:29:29 +00002948 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002949}
2950
2951#endif /* LIBXML_HTML_ENABLED */
2952
2953#ifdef LIBXML_DOCB_ENABLED
2954
2955/**
2956 * xmlSAX2InitDocbDefaultSAXHandler:
2957 * @hdlr: the SAX handler
2958 *
2959 * Initialize the default DocBook SAX2 handler
2960 */
2961void
2962xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2963{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002964 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002965 return;
2966
2967 hdlr->internalSubset = xmlSAX2InternalSubset;
2968 hdlr->externalSubset = NULL;
2969 hdlr->isStandalone = xmlSAX2IsStandalone;
2970 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2971 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2972 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2973 hdlr->getEntity = xmlSAX2GetEntity;
2974 hdlr->getParameterEntity = NULL;
2975 hdlr->entityDecl = xmlSAX2EntityDecl;
2976 hdlr->attributeDecl = NULL;
2977 hdlr->elementDecl = NULL;
2978 hdlr->notationDecl = NULL;
2979 hdlr->unparsedEntityDecl = NULL;
2980 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2981 hdlr->startDocument = xmlSAX2StartDocument;
2982 hdlr->endDocument = xmlSAX2EndDocument;
2983 hdlr->startElement = xmlSAX2StartElement;
2984 hdlr->endElement = xmlSAX2EndElement;
2985 hdlr->reference = xmlSAX2Reference;
2986 hdlr->characters = xmlSAX2Characters;
2987 hdlr->cdataBlock = NULL;
2988 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2989 hdlr->processingInstruction = NULL;
2990 hdlr->comment = xmlSAX2Comment;
2991 hdlr->warning = xmlParserWarning;
2992 hdlr->error = xmlParserError;
2993 hdlr->fatalError = xmlParserError;
2994
Daniel Veillardffbbed42003-10-10 14:46:54 +00002995 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002996}
2997
2998/**
2999 * docbDefaultSAXHandlerInit:
3000 *
3001 * Initialize the default SAX handler
3002 */
3003void
3004docbDefaultSAXHandlerInit(void)
3005{
Daniel Veillard092643b2003-09-25 14:29:29 +00003006 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00003007}
3008
3009#endif /* LIBXML_DOCB_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003010#define bottom_SAX2
3011#include "elfgcchack.h"