blob: 1c5bb89eef0c089777c243f02d41e2d96d08af00 [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>
14#include <libxml/xmlmemory.h>
15#include <libxml/tree.h>
16#include <libxml/parser.h>
17#include <libxml/parserInternals.h>
18#include <libxml/valid.h>
19#include <libxml/entities.h>
20#include <libxml/xmlerror.h>
21#include <libxml/debugXML.h>
22#include <libxml/xmlIO.h>
23#include <libxml/SAX.h>
24#include <libxml/uri.h>
25#include <libxml/valid.h>
26#include <libxml/HTMLtree.h>
27#include <libxml/globals.h>
28
29/* #define DEBUG_SAX2 */
30/* #define DEBUG_SAX2_TREE */
31
32/**
Daniel Veillarde57ec792003-09-10 10:50:59 +000033 * TODO:
34 *
35 * macro to flag unimplemented blocks
36 * XML_CATALOG_PREFER user env to select between system/public prefered
37 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
38 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
39 *> values "system" and "public". I have made the default be "system" to
40 *> match yours.
41 */
42#define TODO \
43 xmlGenericError(xmlGenericErrorContext, \
44 "Unimplemented block at %s:%d\n", \
45 __FILE__, __LINE__);
46
William M. Brack42331a92004-07-29 07:07:16 +000047/*
48 * xmlSAX2ErrMemory:
49 * @ctxt: an XML validation parser context
50 * @msg: a string to accompany the error message
51 */
52static void
William M. Bracka3215c72004-07-31 16:24:01 +000053xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
Daniel Veillard34099b42004-11-04 17:34:35 +000054 if (ctxt != NULL) {
55 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
56 ctxt->sax->error(ctxt->userData, "%s: out of memory\n", msg);
57 ctxt->errNo = XML_ERR_NO_MEMORY;
58 ctxt->instate = XML_PARSER_EOF;
59 ctxt->disableSAX = 1;
60 }
William M. Brack42331a92004-07-29 07:07:16 +000061}
62
Daniel Veillarde57ec792003-09-10 10:50:59 +000063/**
Daniel Veillardf88d8cf2003-12-08 10:25:02 +000064 * xmlValidError:
65 * @ctxt: an XML validation parser context
66 * @error: the error number
67 * @msg: the error message
68 * @str1: extra data
69 * @str2: extra data
70 *
71 * Handle a validation error
72 */
73static void
74xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
75 const char *msg, const char *str1, const char *str2)
76{
77 xmlStructuredErrorFunc schannel = NULL;
78
79 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
80 (ctxt->instate == XML_PARSER_EOF))
81 return;
Daniel Veillard34099b42004-11-04 17:34:35 +000082 if (ctxt != NULL) {
83 ctxt->errNo = error;
84 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
85 schannel = ctxt->sax->serror;
Daniel Veillard2728f842006-03-09 16:49:24 +000086 __xmlRaiseError(schannel,
87 ctxt->vctxt.error, ctxt->vctxt.userData,
88 ctxt, NULL, XML_FROM_DTD, error,
89 XML_ERR_ERROR, NULL, 0, (const char *) str1,
90 (const char *) str2, NULL, 0, 0,
91 msg, (const char *) str1, (const char *) str2);
Daniel Veillard34099b42004-11-04 17:34:35 +000092 ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +000093 } else {
94 __xmlRaiseError(schannel,
95 NULL, NULL,
96 ctxt, NULL, XML_FROM_DTD, error,
97 XML_ERR_ERROR, NULL, 0, (const char *) str1,
98 (const char *) str2, NULL, 0, 0,
99 msg, (const char *) str1, (const char *) str2);
100 }
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000101}
102
103/**
Daniel Veillard87b30462005-07-05 14:04:36 +0000104 * xmlFatalErrMsg:
105 * @ctxt: an XML parser context
106 * @error: the error number
107 * @msg: the error message
108 * @str1: an error string
109 * @str2: an error string
110 *
111 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
112 */
113static void
114xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
115 const char *msg, const xmlChar *str1, const xmlChar *str2)
116{
117 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
118 (ctxt->instate == XML_PARSER_EOF))
119 return;
120 if (ctxt != NULL)
121 ctxt->errNo = error;
122 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
123 XML_ERR_FATAL, NULL, 0,
124 (const char *) str1, (const char *) str2,
125 NULL, 0, 0, msg, str1, str2);
126 if (ctxt != NULL) {
127 ctxt->wellFormed = 0;
128 ctxt->valid = 0;
129 if (ctxt->recovery == 0)
130 ctxt->disableSAX = 1;
131 }
132}
133
134/**
135 * xmlWarnMsg:
136 * @ctxt: an XML parser context
137 * @error: the error number
138 * @msg: the error message
139 * @str1: an error string
140 * @str2: an error string
141 *
142 * Handle a parser warning
143 */
144static void
145xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
146 const char *msg, const xmlChar *str1)
147{
148 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
149 (ctxt->instate == XML_PARSER_EOF))
150 return;
151 if (ctxt != NULL)
152 ctxt->errNo = error;
153 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
154 XML_ERR_WARNING, NULL, 0,
155 (const char *) str1, NULL,
156 NULL, 0, 0, msg, str1);
157}
158
159/**
160 * xmlNsErrMsg:
161 * @ctxt: an XML parser context
162 * @error: the error number
163 * @msg: the error message
164 * @str1: an error string
165 * @str2: an error string
166 *
167 * Handle a namespace error
168 */
169static void
170xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
171 const char *msg, const xmlChar *str1, const xmlChar *str2)
172{
173 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
174 (ctxt->instate == XML_PARSER_EOF))
175 return;
176 if (ctxt != NULL)
177 ctxt->errNo = error;
178 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
179 XML_ERR_ERROR, NULL, 0,
180 (const char *) str1, (const char *) str2,
181 NULL, 0, 0, msg, str1, str2);
182}
183
184/**
185 * xmlNsWarnMsg:
186 * @ctxt: an XML parser context
187 * @error: the error number
188 * @msg: the error message
189 * @str1: an error string
190 *
191 * Handle a namespace warning
192 */
193static void
194xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
195 const char *msg, const xmlChar *str1, const xmlChar *str2)
196{
197 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
198 (ctxt->instate == XML_PARSER_EOF))
199 return;
200 if (ctxt != NULL)
201 ctxt->errNo = error;
202 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
203 XML_ERR_WARNING, NULL, 0,
204 (const char *) str1, (const char *) str2,
205 NULL, 0, 0, msg, str1, str2);
206}
207
208/**
Daniel Veillard1af9a412003-08-20 22:54:39 +0000209 * xmlSAX2GetPublicId:
210 * @ctx: the user data (XML parser context)
211 *
212 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
213 *
214 * Returns a xmlChar *
215 */
216const xmlChar *
217xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
218{
219 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
220 return(NULL);
221}
222
223/**
224 * xmlSAX2GetSystemId:
225 * @ctx: the user data (XML parser context)
226 *
227 * Provides the system ID, basically URL or filename e.g.
228 * http://www.sgmlsource.com/dtds/memo.dtd
229 *
230 * Returns a xmlChar *
231 */
232const xmlChar *
233xmlSAX2GetSystemId(void *ctx)
234{
235 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard24505b02005-07-28 23:49:35 +0000236 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000237 return((const xmlChar *) ctxt->input->filename);
238}
239
240/**
241 * xmlSAX2GetLineNumber:
242 * @ctx: the user data (XML parser context)
243 *
244 * Provide the line number of the current parsing point.
245 *
246 * Returns an int
247 */
248int
249xmlSAX2GetLineNumber(void *ctx)
250{
251 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000252 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000253 return(ctxt->input->line);
254}
255
256/**
257 * xmlSAX2GetColumnNumber:
258 * @ctx: the user data (XML parser context)
259 *
260 * Provide the column number of the current parsing point.
261 *
262 * Returns an int
263 */
264int
265xmlSAX2GetColumnNumber(void *ctx)
266{
267 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000268 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000269 return(ctxt->input->col);
270}
271
272/**
273 * xmlSAX2IsStandalone:
274 * @ctx: the user data (XML parser context)
275 *
276 * Is this document tagged standalone ?
277 *
278 * Returns 1 if true
279 */
280int
281xmlSAX2IsStandalone(void *ctx)
282{
283 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000284 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000285 return(ctxt->myDoc->standalone == 1);
286}
287
288/**
289 * xmlSAX2HasInternalSubset:
290 * @ctx: the user data (XML parser context)
291 *
292 * Does this document has an internal subset
293 *
294 * Returns 1 if true
295 */
296int
297xmlSAX2HasInternalSubset(void *ctx)
298{
299 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000300 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000301 return(ctxt->myDoc->intSubset != NULL);
302}
303
304/**
305 * xmlSAX2HasExternalSubset:
306 * @ctx: the user data (XML parser context)
307 *
308 * Does this document has an external subset
309 *
310 * Returns 1 if true
311 */
312int
313xmlSAX2HasExternalSubset(void *ctx)
314{
315 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000316 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000317 return(ctxt->myDoc->extSubset != NULL);
318}
319
320/**
321 * xmlSAX2InternalSubset:
322 * @ctx: the user data (XML parser context)
323 * @name: the root element name
324 * @ExternalID: the external ID
325 * @SystemID: the SYSTEM ID (e.g. filename or URL)
326 *
327 * Callback on internal subset declaration.
328 */
329void
330xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
331 const xmlChar *ExternalID, const xmlChar *SystemID)
332{
333 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
334 xmlDtdPtr dtd;
Daniel Veillard34099b42004-11-04 17:34:35 +0000335 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000336#ifdef DEBUG_SAX
337 xmlGenericError(xmlGenericErrorContext,
338 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
339 name, ExternalID, SystemID);
340#endif
341
342 if (ctxt->myDoc == NULL)
343 return;
344 dtd = xmlGetIntSubset(ctxt->myDoc);
345 if (dtd != NULL) {
346 if (ctxt->html)
347 return;
348 xmlUnlinkNode((xmlNodePtr) dtd);
349 xmlFreeDtd(dtd);
350 ctxt->myDoc->intSubset = NULL;
351 }
352 ctxt->myDoc->intSubset =
353 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
William M. Brack42331a92004-07-29 07:07:16 +0000354 if (ctxt->myDoc->intSubset == NULL)
355 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000356}
357
358/**
359 * xmlSAX2ExternalSubset:
360 * @ctx: the user data (XML parser context)
361 * @name: the root element name
362 * @ExternalID: the external ID
363 * @SystemID: the SYSTEM ID (e.g. filename or URL)
364 *
365 * Callback on external subset declaration.
366 */
367void
368xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
369 const xmlChar *ExternalID, const xmlChar *SystemID)
370{
371 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000372 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000373#ifdef DEBUG_SAX
374 xmlGenericError(xmlGenericErrorContext,
375 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
376 name, ExternalID, SystemID);
377#endif
378 if (((ExternalID != NULL) || (SystemID != NULL)) &&
379 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
380 (ctxt->wellFormed && ctxt->myDoc))) {
381 /*
382 * Try to fetch and parse the external subset.
383 */
384 xmlParserInputPtr oldinput;
385 int oldinputNr;
386 int oldinputMax;
387 xmlParserInputPtr *oldinputTab;
388 xmlParserInputPtr input = NULL;
389 xmlCharEncoding enc;
390 int oldcharset;
391
392 /*
393 * Ask the Entity resolver to load the damn thing
394 */
395 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
396 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
397 SystemID);
398 if (input == NULL) {
399 return;
400 }
401
402 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
403
404 /*
405 * make sure we won't destroy the main document context
406 */
407 oldinput = ctxt->input;
408 oldinputNr = ctxt->inputNr;
409 oldinputMax = ctxt->inputMax;
410 oldinputTab = ctxt->inputTab;
411 oldcharset = ctxt->charset;
412
413 ctxt->inputTab = (xmlParserInputPtr *)
414 xmlMalloc(5 * sizeof(xmlParserInputPtr));
415 if (ctxt->inputTab == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000416 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000417 ctxt->input = oldinput;
418 ctxt->inputNr = oldinputNr;
419 ctxt->inputMax = oldinputMax;
420 ctxt->inputTab = oldinputTab;
421 ctxt->charset = oldcharset;
422 return;
423 }
424 ctxt->inputNr = 0;
425 ctxt->inputMax = 5;
426 ctxt->input = NULL;
427 xmlPushInput(ctxt, input);
428
429 /*
430 * On the fly encoding conversion if needed
431 */
432 if (ctxt->input->length >= 4) {
433 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
434 xmlSwitchEncoding(ctxt, enc);
435 }
436
437 if (input->filename == NULL)
438 input->filename = (char *) xmlCanonicPath(SystemID);
439 input->line = 1;
440 input->col = 1;
441 input->base = ctxt->input->cur;
442 input->cur = ctxt->input->cur;
443 input->free = NULL;
444
445 /*
446 * let's parse that entity knowing it's an external subset.
447 */
448 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
449
450 /*
451 * Free up the external entities
452 */
453
454 while (ctxt->inputNr > 1)
455 xmlPopInput(ctxt);
456 xmlFreeInputStream(ctxt->input);
457 xmlFree(ctxt->inputTab);
458
459 /*
460 * Restore the parsing context of the main entity
461 */
462 ctxt->input = oldinput;
463 ctxt->inputNr = oldinputNr;
464 ctxt->inputMax = oldinputMax;
465 ctxt->inputTab = oldinputTab;
466 ctxt->charset = oldcharset;
467 /* ctxt->wellFormed = oldwellFormed; */
468 }
469}
470
471/**
472 * xmlSAX2ResolveEntity:
473 * @ctx: the user data (XML parser context)
474 * @publicId: The public ID of the entity
475 * @systemId: The system ID of the entity
476 *
477 * The entity loader, to control the loading of external entities,
478 * the application can either:
479 * - override this xmlSAX2ResolveEntity() callback in the SAX block
480 * - or better use the xmlSetExternalEntityLoader() function to
481 * set up it's own entity resolution routine
482 *
483 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
484 */
485xmlParserInputPtr
486xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
487{
488 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
489 xmlParserInputPtr ret;
490 xmlChar *URI;
491 const char *base = NULL;
492
Daniel Veillard34099b42004-11-04 17:34:35 +0000493 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000494 if (ctxt->input != NULL)
495 base = ctxt->input->filename;
496 if (base == NULL)
497 base = ctxt->directory;
498
499 URI = xmlBuildURI(systemId, (const xmlChar *) base);
500
501#ifdef DEBUG_SAX
502 xmlGenericError(xmlGenericErrorContext,
503 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
504#endif
505
506 ret = xmlLoadExternalEntity((const char *) URI,
507 (const char *) publicId, ctxt);
508 if (URI != NULL)
509 xmlFree(URI);
510 return(ret);
511}
512
513/**
514 * xmlSAX2GetEntity:
515 * @ctx: the user data (XML parser context)
516 * @name: The entity name
517 *
518 * Get an entity by name
519 *
520 * Returns the xmlEntityPtr if found.
521 */
522xmlEntityPtr
523xmlSAX2GetEntity(void *ctx, const xmlChar *name)
524{
525 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
526 xmlEntityPtr ret = NULL;
527
Daniel Veillard34099b42004-11-04 17:34:35 +0000528 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000529#ifdef DEBUG_SAX
530 xmlGenericError(xmlGenericErrorContext,
531 "SAX.xmlSAX2GetEntity(%s)\n", name);
532#endif
533
534 if (ctxt->inSubset == 0) {
535 ret = xmlGetPredefinedEntity(name);
536 if (ret != NULL)
537 return(ret);
538 }
539 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
540 if (ctxt->inSubset == 2) {
541 ctxt->myDoc->standalone = 0;
542 ret = xmlGetDocEntity(ctxt->myDoc, name);
543 ctxt->myDoc->standalone = 1;
544 } else {
545 ret = xmlGetDocEntity(ctxt->myDoc, name);
546 if (ret == NULL) {
547 ctxt->myDoc->standalone = 0;
548 ret = xmlGetDocEntity(ctxt->myDoc, name);
549 if (ret != NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000550 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
551 "Entity(%s) document marked standalone but requires external subset\n",
552 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000553 }
554 ctxt->myDoc->standalone = 1;
555 }
556 }
557 } else {
558 ret = xmlGetDocEntity(ctxt->myDoc, name);
559 }
560 if ((ret != NULL) &&
561 ((ctxt->validate) || (ctxt->replaceEntities)) &&
562 (ret->children == NULL) &&
563 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
564 int val;
565
566 /*
567 * for validation purposes we really need to fetch and
568 * parse the external entity
569 */
570 xmlNodePtr children;
571
572 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
573 ret->ExternalID, &children);
574 if (val == 0) {
575 xmlAddChildList((xmlNodePtr) ret, children);
576 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000577 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
578 "Failure to process entity %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000579 ctxt->validate = 0;
580 return(NULL);
581 }
582 ret->owner = 1;
583 }
584 return(ret);
585}
586
587/**
588 * xmlSAX2GetParameterEntity:
589 * @ctx: the user data (XML parser context)
590 * @name: The entity name
591 *
592 * Get a parameter entity by name
593 *
594 * Returns the xmlEntityPtr if found.
595 */
596xmlEntityPtr
597xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
598{
599 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
600 xmlEntityPtr ret;
601
Daniel Veillard34099b42004-11-04 17:34:35 +0000602 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000603#ifdef DEBUG_SAX
604 xmlGenericError(xmlGenericErrorContext,
605 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
606#endif
607
608 ret = xmlGetParameterEntity(ctxt->myDoc, name);
609 return(ret);
610}
611
612
613/**
614 * xmlSAX2EntityDecl:
615 * @ctx: the user data (XML parser context)
616 * @name: the entity name
617 * @type: the entity type
618 * @publicId: The public ID of the entity
619 * @systemId: The system ID of the entity
620 * @content: the entity value (without processing).
621 *
622 * An entity definition has been parsed
623 */
624void
625xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
626 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
627{
628 xmlEntityPtr ent;
629 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
630
Daniel Veillard34099b42004-11-04 17:34:35 +0000631 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000632#ifdef DEBUG_SAX
633 xmlGenericError(xmlGenericErrorContext,
634 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
635 name, type, publicId, systemId, content);
636#endif
637 if (ctxt->inSubset == 1) {
638 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
639 systemId, content);
Daniel Veillard87b30462005-07-05 14:04:36 +0000640 if ((ent == NULL) && (ctxt->pedantic))
641 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
642 "Entity(%s) already defined in the internal subset\n",
643 name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000644 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
645 xmlChar *URI;
646 const char *base = NULL;
647
648 if (ctxt->input != NULL)
649 base = ctxt->input->filename;
650 if (base == NULL)
651 base = ctxt->directory;
652
653 URI = xmlBuildURI(systemId, (const xmlChar *) base);
654 ent->URI = URI;
655 }
656 } else if (ctxt->inSubset == 2) {
657 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
658 systemId, content);
659 if ((ent == NULL) && (ctxt->pedantic) &&
660 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000661 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000662 "Entity(%s) already defined in the external subset\n", name);
663 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
664 xmlChar *URI;
665 const char *base = NULL;
666
667 if (ctxt->input != NULL)
668 base = ctxt->input->filename;
669 if (base == NULL)
670 base = ctxt->directory;
671
672 URI = xmlBuildURI(systemId, (const xmlChar *) base);
673 ent->URI = URI;
674 }
675 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000676 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
677 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
678 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000679 }
680}
681
682/**
683 * xmlSAX2AttributeDecl:
684 * @ctx: the user data (XML parser context)
685 * @elem: the name of the element
686 * @fullname: the attribute name
687 * @type: the attribute type
688 * @def: the type of default value
689 * @defaultValue: the attribute default value
690 * @tree: the tree of enumerated value set
691 *
692 * An attribute definition has been parsed
693 */
694void
695xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
696 int type, int def, const xmlChar *defaultValue,
697 xmlEnumerationPtr tree)
698{
699 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
700 xmlAttributePtr attr;
701 xmlChar *name = NULL, *prefix = NULL;
702
Daniel Veillard2728f842006-03-09 16:49:24 +0000703 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
704 return;
705
Daniel Veillard1af9a412003-08-20 22:54:39 +0000706#ifdef DEBUG_SAX
707 xmlGenericError(xmlGenericErrorContext,
708 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
709 elem, fullname, type, def, defaultValue);
710#endif
Daniel Veillard68cb4b22004-04-18 20:55:39 +0000711 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
712 (type != XML_ATTRIBUTE_ID)) {
713 /*
714 * Raise the error but keep the validity flag
715 */
716 int tmp = ctxt->valid;
717 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
718 "xml:id : attribute type should be ID\n", NULL, NULL);
719 ctxt->valid = tmp;
720 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000721 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000722 name = xmlSplitQName(ctxt, fullname, &prefix);
723 ctxt->vctxt.valid = 1;
724 if (ctxt->inSubset == 1)
725 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
726 name, prefix, (xmlAttributeType) type,
727 (xmlAttributeDefault) def, defaultValue, tree);
728 else if (ctxt->inSubset == 2)
729 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
730 name, prefix, (xmlAttributeType) type,
731 (xmlAttributeDefault) def, defaultValue, tree);
732 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000733 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
734 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
735 name, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000736 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000737 return;
738 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000739#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000740 if (ctxt->vctxt.valid == 0)
741 ctxt->valid = 0;
742 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
Daniel Veillard2728f842006-03-09 16:49:24 +0000743 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000744 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
745 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000746#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000747 if (prefix != NULL)
748 xmlFree(prefix);
749 if (name != NULL)
750 xmlFree(name);
751}
752
753/**
754 * xmlSAX2ElementDecl:
755 * @ctx: the user data (XML parser context)
756 * @name: the element name
757 * @type: the element type
758 * @content: the element value tree
759 *
760 * An element definition has been parsed
761 */
762void
763xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
764 xmlElementContentPtr content)
765{
766 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
767 xmlElementPtr elem = NULL;
768
Daniel Veillard2728f842006-03-09 16:49:24 +0000769 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
770 return;
771
Daniel Veillard1af9a412003-08-20 22:54:39 +0000772#ifdef DEBUG_SAX
773 xmlGenericError(xmlGenericErrorContext,
774 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
775#endif
776
777 if (ctxt->inSubset == 1)
778 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
779 name, (xmlElementTypeVal) type, content);
780 else if (ctxt->inSubset == 2)
781 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
782 name, (xmlElementTypeVal) type, content);
783 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000784 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
785 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
786 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000787 return;
788 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000789#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000790 if (elem == NULL)
791 ctxt->valid = 0;
792 if (ctxt->validate && ctxt->wellFormed &&
793 ctxt->myDoc && ctxt->myDoc->intSubset)
794 ctxt->valid &=
795 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +0000796#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000797}
798
799/**
800 * xmlSAX2NotationDecl:
801 * @ctx: the user data (XML parser context)
802 * @name: The name of the notation
803 * @publicId: The public ID of the entity
804 * @systemId: The system ID of the entity
805 *
806 * What to do when a notation declaration has been parsed.
807 */
808void
809xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
810 const xmlChar *publicId, const xmlChar *systemId)
811{
812 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
813 xmlNotationPtr nota = NULL;
814
Daniel Veillard2728f842006-03-09 16:49:24 +0000815 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
816 return;
817
Daniel Veillard1af9a412003-08-20 22:54:39 +0000818#ifdef DEBUG_SAX
819 xmlGenericError(xmlGenericErrorContext,
820 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
821#endif
822
823 if ((publicId == NULL) && (systemId == NULL)) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000824 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
825 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
826 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000827 return;
828 } else if (ctxt->inSubset == 1)
829 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
830 publicId, systemId);
831 else if (ctxt->inSubset == 2)
832 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
833 publicId, systemId);
834 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000835 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
836 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
837 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000838 return;
839 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000840#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000841 if (nota == NULL) ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000842 if ((ctxt->validate) && (ctxt->wellFormed) &&
843 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000844 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
845 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000846#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000847}
848
849/**
850 * xmlSAX2UnparsedEntityDecl:
851 * @ctx: the user data (XML parser context)
852 * @name: The name of the entity
853 * @publicId: The public ID of the entity
854 * @systemId: The system ID of the entity
855 * @notationName: the name of the notation
856 *
857 * What to do when an unparsed entity declaration is parsed
858 */
859void
860xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
861 const xmlChar *publicId, const xmlChar *systemId,
862 const xmlChar *notationName)
863{
864 xmlEntityPtr ent;
865 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000866 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000867#ifdef DEBUG_SAX
868 xmlGenericError(xmlGenericErrorContext,
869 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
870 name, publicId, systemId, notationName);
871#endif
872 if (ctxt->inSubset == 1) {
873 ent = xmlAddDocEntity(ctxt->myDoc, name,
874 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
875 publicId, systemId, notationName);
876 if ((ent == NULL) && (ctxt->pedantic) &&
877 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000878 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000879 "Entity(%s) already defined in the internal subset\n", name);
880 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
881 xmlChar *URI;
882 const char *base = NULL;
883
884 if (ctxt->input != NULL)
885 base = ctxt->input->filename;
886 if (base == NULL)
887 base = ctxt->directory;
888
889 URI = xmlBuildURI(systemId, (const xmlChar *) base);
890 ent->URI = URI;
891 }
892 } else if (ctxt->inSubset == 2) {
893 ent = xmlAddDtdEntity(ctxt->myDoc, name,
894 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
895 publicId, systemId, notationName);
896 if ((ent == NULL) && (ctxt->pedantic) &&
897 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000898 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000899 "Entity(%s) already defined in the external subset\n", name);
900 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
901 xmlChar *URI;
902 const char *base = NULL;
903
904 if (ctxt->input != NULL)
905 base = ctxt->input->filename;
906 if (base == NULL)
907 base = ctxt->directory;
908
909 URI = xmlBuildURI(systemId, (const xmlChar *) base);
910 ent->URI = URI;
911 }
912 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000913 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
914 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
915 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000916 }
917}
918
919/**
920 * xmlSAX2SetDocumentLocator:
921 * @ctx: the user data (XML parser context)
922 * @loc: A SAX Locator
923 *
924 * Receive the document locator at startup, actually xmlDefaultSAXLocator
925 * Everything is available on the context, so this is useless in our case.
926 */
927void
928xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
929{
930 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
931#ifdef DEBUG_SAX
932 xmlGenericError(xmlGenericErrorContext,
933 "SAX.xmlSAX2SetDocumentLocator()\n");
934#endif
935}
936
937/**
938 * xmlSAX2StartDocument:
939 * @ctx: the user data (XML parser context)
940 *
941 * called when the document start being processed.
942 */
943void
944xmlSAX2StartDocument(void *ctx)
945{
946 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
947 xmlDocPtr doc;
948
Daniel Veillard34099b42004-11-04 17:34:35 +0000949 if (ctx == NULL) return;
950
Daniel Veillard1af9a412003-08-20 22:54:39 +0000951#ifdef DEBUG_SAX
952 xmlGenericError(xmlGenericErrorContext,
953 "SAX.xmlSAX2StartDocument()\n");
954#endif
955 if (ctxt->html) {
956#ifdef LIBXML_HTML_ENABLED
957 if (ctxt->myDoc == NULL)
958 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
959 if (ctxt->myDoc == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000960 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000961 return;
962 }
963#else
964 xmlGenericError(xmlGenericErrorContext,
965 "libxml2 built without HTML support\n");
966 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
967 ctxt->instate = XML_PARSER_EOF;
968 ctxt->disableSAX = 1;
969 return;
970#endif
971 } else {
972 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
973 if (doc != NULL) {
974 if (ctxt->encoding != NULL)
975 doc->encoding = xmlStrdup(ctxt->encoding);
976 else
977 doc->encoding = NULL;
978 doc->standalone = ctxt->standalone;
979 } else {
William M. Brack42331a92004-07-29 07:07:16 +0000980 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000981 return;
982 }
Daniel Veillard500a1de2004-03-22 15:22:58 +0000983 if ((ctxt->dictNames) && (doc != NULL)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000984 doc->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000985 xmlDictReference(doc->dict);
986 }
Daniel Veillard1af9a412003-08-20 22:54:39 +0000987 }
988 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
989 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillardb8efdda2006-10-10 12:37:14 +0000990 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000991 if (ctxt->myDoc->URL == NULL)
William M. Bracka3215c72004-07-31 16:24:01 +0000992 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000993 }
994}
995
996/**
997 * xmlSAX2EndDocument:
998 * @ctx: the user data (XML parser context)
999 *
1000 * called when the document end has been detected.
1001 */
1002void
1003xmlSAX2EndDocument(void *ctx)
1004{
1005 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1006#ifdef DEBUG_SAX
1007 xmlGenericError(xmlGenericErrorContext,
1008 "SAX.xmlSAX2EndDocument()\n");
1009#endif
Daniel Veillard34099b42004-11-04 17:34:35 +00001010 if (ctx == NULL) return;
Daniel Veillard4432df22003-09-28 18:58:27 +00001011#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001012 if (ctxt->validate && ctxt->wellFormed &&
1013 ctxt->myDoc && ctxt->myDoc->intSubset)
1014 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00001015#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001016
1017 /*
1018 * Grab the encoding if it was added on-the-fly
1019 */
1020 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1021 (ctxt->myDoc->encoding == NULL)) {
1022 ctxt->myDoc->encoding = ctxt->encoding;
1023 ctxt->encoding = NULL;
1024 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001025 if ((ctxt->inputTab != NULL) &&
1026 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1027 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001028 (ctxt->myDoc->encoding == NULL)) {
1029 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1030 }
1031 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1032 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1033 ctxt->myDoc->charset = ctxt->charset;
1034 }
1035}
1036
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001037#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001038/**
1039 * xmlSAX2AttributeInternal:
1040 * @ctx: the user data (XML parser context)
1041 * @fullname: The attribute name, including namespace prefix
1042 * @value: The attribute value
1043 * @prefix: the prefix on the element node
1044 *
1045 * Handle an attribute that has been read by the parser.
1046 * The default handling is to convert the attribute into an
1047 * DOM subtree and past it in a new xmlAttr element added to
1048 * the element.
1049 */
1050static void
1051xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001052 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001053{
1054 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1055 xmlAttrPtr ret;
1056 xmlChar *name;
1057 xmlChar *ns;
1058 xmlChar *nval;
1059 xmlNsPtr namespace;
1060
1061 /*
1062 * Split the full name into a namespace prefix and the tag name
1063 */
1064 name = xmlSplitQName(ctxt, fullname, &ns);
1065 if ((name != NULL) && (name[0] == 0)) {
1066 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001067 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1068 "invalid namespace declaration '%s'\n",
1069 fullname, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001070 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +00001071 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1072 "Avoid attribute ending with ':' like '%s'\n",
1073 fullname, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001074 }
1075 if (ns != NULL)
1076 xmlFree(ns);
1077 ns = NULL;
1078 xmlFree(name);
1079 name = xmlStrdup(fullname);
1080 }
1081 if (name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001082 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001083 if (ns != NULL)
1084 xmlFree(ns);
1085 return;
1086 }
1087
Daniel Veillard4432df22003-09-28 18:58:27 +00001088#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001089 /*
1090 * Do the last stage of the attribute normalization
1091 * Needed for HTML too:
1092 * http://www.w3.org/TR/html4/types.html#h-6.2
1093 */
1094 ctxt->vctxt.valid = 1;
1095 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1096 ctxt->myDoc, ctxt->node,
1097 fullname, value);
1098 if (ctxt->vctxt.valid != 1) {
1099 ctxt->valid = 0;
1100 }
1101 if (nval != NULL)
1102 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +00001103#else
1104 nval = NULL;
1105#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001106
1107 /*
1108 * Check whether it's a namespace definition
1109 */
1110 if ((!ctxt->html) && (ns == NULL) &&
1111 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1112 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1113 xmlNsPtr nsret;
1114 xmlChar *val;
1115
1116 if (!ctxt->replaceEntities) {
1117 ctxt->depth++;
1118 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1119 0,0,0);
1120 ctxt->depth--;
1121 } else {
1122 val = (xmlChar *) value;
1123 }
1124
1125 if (val[0] != 0) {
1126 xmlURIPtr uri;
1127
1128 uri = xmlParseURI((const char *)val);
1129 if (uri == NULL) {
1130 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1131 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +00001132 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001133 } else {
1134 if (uri->scheme == NULL) {
1135 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1136 ctxt->sax->warning(ctxt->userData,
1137 "xmlns: URI %s is not absolute\n", val);
1138 }
1139 xmlFreeURI(uri);
1140 }
1141 }
1142
1143 /* a default namespace definition */
1144 nsret = xmlNewNs(ctxt->node, val, NULL);
1145
Daniel Veillard4432df22003-09-28 18:58:27 +00001146#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001147 /*
1148 * Validate also for namespace decls, they are attributes from
1149 * an XML-1.0 perspective
1150 */
1151 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1152 ctxt->myDoc && ctxt->myDoc->intSubset)
1153 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1154 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +00001155#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001156 if (name != NULL)
1157 xmlFree(name);
1158 if (nval != NULL)
1159 xmlFree(nval);
1160 if (val != value)
1161 xmlFree(val);
1162 return;
1163 }
1164 if ((!ctxt->html) &&
1165 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1166 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1167 xmlNsPtr nsret;
1168 xmlChar *val;
1169
1170 if (!ctxt->replaceEntities) {
1171 ctxt->depth++;
1172 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1173 0,0,0);
1174 ctxt->depth--;
1175 if (val == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001176 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001177 xmlFree(ns);
1178 if (name != NULL)
1179 xmlFree(name);
1180 return;
1181 }
1182 } else {
1183 val = (xmlChar *) value;
1184 }
1185
1186 if (val[0] == 0) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001187 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1188 "Empty namespace name for prefix %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001189 }
1190 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1191 xmlURIPtr uri;
1192
1193 uri = xmlParseURI((const char *)val);
1194 if (uri == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001195 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001196 "xmlns:%s: %s not a valid URI\n", name, value);
1197 } else {
1198 if (uri->scheme == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001199 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001200 "xmlns:%s: URI %s is not absolute\n", name, value);
1201 }
1202 xmlFreeURI(uri);
1203 }
1204 }
1205
1206 /* a standard namespace definition */
1207 nsret = xmlNewNs(ctxt->node, val, name);
1208 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001209#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001210 /*
1211 * Validate also for namespace decls, they are attributes from
1212 * an XML-1.0 perspective
1213 */
1214 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1215 ctxt->myDoc && ctxt->myDoc->intSubset)
1216 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1217 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001218#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001219 if (name != NULL)
1220 xmlFree(name);
1221 if (nval != NULL)
1222 xmlFree(nval);
1223 if (val != value)
1224 xmlFree(val);
1225 return;
1226 }
1227
1228 if (ns != NULL) {
1229 xmlAttrPtr prop;
1230 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillard67906942003-08-28 21:13:25 +00001231 if (namespace == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001232 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
William M. Brack4811ba32003-09-06 18:02:53 +00001233 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001234 ns, name);
1235 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001236
1237 prop = ctxt->node->properties;
1238 while (prop != NULL) {
1239 if (prop->ns != NULL) {
1240 if ((xmlStrEqual(name, prop->name)) &&
1241 ((namespace == prop->ns) ||
1242 (xmlStrEqual(namespace->href, prop->ns->href)))) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001243 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001244 "Attribute %s in %s redefined\n",
1245 name, namespace->href);
1246 ctxt->wellFormed = 0;
1247 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1248 goto error;
1249 }
1250 }
1251 prop = prop->next;
1252 }
1253 } else {
1254 namespace = NULL;
1255 }
1256
1257 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1258 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1259
1260 if (ret != NULL) {
1261 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1262 xmlNodePtr tmp;
1263
1264 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1265 tmp = ret->children;
1266 while (tmp != NULL) {
1267 tmp->parent = (xmlNodePtr) ret;
1268 if (tmp->next == NULL)
1269 ret->last = tmp;
1270 tmp = tmp->next;
1271 }
1272 } else if (value != NULL) {
1273 ret->children = xmlNewDocText(ctxt->myDoc, value);
1274 ret->last = ret->children;
1275 if (ret->children != NULL)
1276 ret->children->parent = (xmlNodePtr) ret;
1277 }
1278 }
1279
Daniel Veillard4432df22003-09-28 18:58:27 +00001280#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001281 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1282 ctxt->myDoc && ctxt->myDoc->intSubset) {
1283
1284 /*
1285 * If we don't substitute entities, the validation should be
1286 * done on a value with replaced entities anyway.
1287 */
1288 if (!ctxt->replaceEntities) {
1289 xmlChar *val;
1290
1291 ctxt->depth++;
1292 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1293 0,0,0);
1294 ctxt->depth--;
1295
1296 if (val == NULL)
1297 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1298 ctxt->myDoc, ctxt->node, ret, value);
1299 else {
1300 xmlChar *nvalnorm;
1301
1302 /*
1303 * Do the last stage of the attribute normalization
1304 * It need to be done twice ... it's an extra burden related
1305 * to the ability to keep xmlSAX2References in attributes
1306 */
1307 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1308 ctxt->node, fullname, val);
1309 if (nvalnorm != NULL) {
1310 xmlFree(val);
1311 val = nvalnorm;
1312 }
1313
1314 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1315 ctxt->myDoc, ctxt->node, ret, val);
1316 xmlFree(val);
1317 }
1318 } else {
1319 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1320 ctxt->node, ret, value);
1321 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001322 } else
1323#endif /* LIBXML_VALID_ENABLED */
1324 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001325 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1326 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1327 /*
1328 * when validating, the ID registration is done at the attribute
1329 * validation level. Otherwise we have to do specific handling here.
1330 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001331 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001332 /*
1333 * Add the xml:id value
1334 *
1335 * Open issue: normalization of the value.
1336 */
Daniel Veillard68cb4b22004-04-18 20:55:39 +00001337 if (xmlValidateNCName(value, 1) != 0) {
1338 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1339 "xml:id : attribute value %s is not an NCName\n",
1340 (const char *) value, NULL);
1341 }
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001342 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001343 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1344 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1345 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1346 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001347 }
1348
1349error:
1350 if (nval != NULL)
1351 xmlFree(nval);
1352 if (ns != NULL)
1353 xmlFree(ns);
1354}
1355
Daniel Veillard1af9a412003-08-20 22:54:39 +00001356/*
1357 * xmlCheckDefaultedAttributes:
1358 *
1359 * Check defaulted attributes from the DTD
1360 */
1361static void
1362xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1363 const xmlChar *prefix, const xmlChar **atts) {
1364 xmlElementPtr elemDecl;
1365 const xmlChar *att;
1366 int internal = 1;
1367 int i;
1368
1369 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1370 if (elemDecl == NULL) {
1371 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1372 internal = 0;
1373 }
1374
1375process_external_subset:
1376
1377 if (elemDecl != NULL) {
1378 xmlAttributePtr attr = elemDecl->attributes;
1379 /*
1380 * Check against defaulted attributes from the external subset
1381 * if the document is stamped as standalone
1382 */
1383 if ((ctxt->myDoc->standalone == 1) &&
1384 (ctxt->myDoc->extSubset != NULL) &&
1385 (ctxt->validate)) {
1386 while (attr != NULL) {
1387 if ((attr->defaultValue != NULL) &&
1388 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1389 attr->elem, attr->name,
1390 attr->prefix) == attr) &&
1391 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1392 attr->elem, attr->name,
1393 attr->prefix) == NULL)) {
1394 xmlChar *fulln;
1395
1396 if (attr->prefix != NULL) {
1397 fulln = xmlStrdup(attr->prefix);
1398 fulln = xmlStrcat(fulln, BAD_CAST ":");
1399 fulln = xmlStrcat(fulln, attr->name);
1400 } else {
1401 fulln = xmlStrdup(attr->name);
1402 }
1403
1404 /*
1405 * Check that the attribute is not declared in the
1406 * serialization
1407 */
1408 att = NULL;
1409 if (atts != NULL) {
1410 i = 0;
1411 att = atts[i];
1412 while (att != NULL) {
1413 if (xmlStrEqual(att, fulln))
1414 break;
1415 i += 2;
1416 att = atts[i];
1417 }
1418 }
1419 if (att == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001420 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001421 "standalone: attribute %s on %s defaulted from external subset\n",
Daniel Veillard427174f2003-12-10 10:42:59 +00001422 (const char *)fulln,
1423 (const char *)attr->elem);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001424 }
1425 }
1426 attr = attr->nexth;
1427 }
1428 }
1429
1430 /*
1431 * Actually insert defaulted values when needed
1432 */
1433 attr = elemDecl->attributes;
1434 while (attr != NULL) {
1435 /*
1436 * Make sure that attributes redefinition occuring in the
1437 * internal subset are not overriden by definitions in the
1438 * external subset.
1439 */
1440 if (attr->defaultValue != NULL) {
1441 /*
1442 * the element should be instantiated in the tree if:
1443 * - this is a namespace prefix
1444 * - the user required for completion in the tree
1445 * like XSLT
1446 * - there isn't already an attribute definition
1447 * in the internal subset overriding it.
1448 */
1449 if (((attr->prefix != NULL) &&
1450 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1451 ((attr->prefix == NULL) &&
1452 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1453 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1454 xmlAttributePtr tst;
1455
1456 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1457 attr->elem, attr->name,
1458 attr->prefix);
1459 if ((tst == attr) || (tst == NULL)) {
1460 xmlChar fn[50];
1461 xmlChar *fulln;
1462
1463 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1464 if (fulln == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001465 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001466 return;
1467 }
1468
1469 /*
1470 * Check that the attribute is not declared in the
1471 * serialization
1472 */
1473 att = NULL;
1474 if (atts != NULL) {
1475 i = 0;
1476 att = atts[i];
1477 while (att != NULL) {
1478 if (xmlStrEqual(att, fulln))
1479 break;
1480 i += 2;
1481 att = atts[i];
1482 }
1483 }
1484 if (att == NULL) {
1485 xmlSAX2AttributeInternal(ctxt, fulln,
1486 attr->defaultValue, prefix);
1487 }
1488 if ((fulln != fn) && (fulln != attr->name))
1489 xmlFree(fulln);
1490 }
1491 }
1492 }
1493 attr = attr->nexth;
1494 }
1495 if (internal == 1) {
1496 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1497 name, prefix);
1498 internal = 0;
1499 goto process_external_subset;
1500 }
1501 }
1502}
1503
1504/**
1505 * xmlSAX2StartElement:
1506 * @ctx: the user data (XML parser context)
1507 * @fullname: The element name, including namespace prefix
1508 * @atts: An array of name/value attributes pairs, NULL terminated
1509 *
1510 * called when an opening tag has been processed.
1511 */
1512void
1513xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1514{
1515 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1516 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001517 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001518 xmlNsPtr ns;
1519 xmlChar *name;
1520 xmlChar *prefix;
1521 const xmlChar *att;
1522 const xmlChar *value;
1523 int i;
1524
Daniel Veillarda521d282004-11-09 14:59:59 +00001525 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001526 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001527#ifdef DEBUG_SAX
1528 xmlGenericError(xmlGenericErrorContext,
1529 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1530#endif
1531
1532 /*
1533 * First check on validity:
1534 */
1535 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1536 ((ctxt->myDoc->intSubset == NULL) ||
1537 ((ctxt->myDoc->intSubset->notations == NULL) &&
1538 (ctxt->myDoc->intSubset->elements == NULL) &&
1539 (ctxt->myDoc->intSubset->attributes == NULL) &&
1540 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001541 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1542 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001543 ctxt->validate = 0;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001544 }
1545
1546
1547 /*
1548 * Split the full name into a namespace prefix and the tag name
1549 */
1550 name = xmlSplitQName(ctxt, fullname, &prefix);
1551
1552
1553 /*
1554 * Note : the namespace resolution is deferred until the end of the
1555 * attributes parsing, since local namespace can be defined as
1556 * an attribute at this level.
1557 */
1558 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1559 if (ret == NULL) {
1560 if (prefix != NULL)
1561 xmlFree(prefix);
William M. Brack42331a92004-07-29 07:07:16 +00001562 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001563 return;
1564 }
1565 if (ctxt->myDoc->children == NULL) {
1566#ifdef DEBUG_SAX_TREE
1567 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1568#endif
1569 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1570 } else if (parent == NULL) {
1571 parent = ctxt->myDoc->children;
1572 }
1573 ctxt->nodemem = -1;
1574 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001575 if (ctxt->input != NULL) {
1576 if (ctxt->input->line < 65535)
1577 ret->line = (short) ctxt->input->line;
1578 else
1579 ret->line = 65535;
1580 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001581 }
1582
1583 /*
1584 * We are parsing a new node.
1585 */
1586#ifdef DEBUG_SAX_TREE
1587 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1588#endif
1589 nodePush(ctxt, ret);
1590
1591 /*
1592 * Link the child element
1593 */
1594 if (parent != NULL) {
1595 if (parent->type == XML_ELEMENT_NODE) {
1596#ifdef DEBUG_SAX_TREE
1597 xmlGenericError(xmlGenericErrorContext,
1598 "adding child %s to %s\n", name, parent->name);
1599#endif
1600 xmlAddChild(parent, ret);
1601 } else {
1602#ifdef DEBUG_SAX_TREE
1603 xmlGenericError(xmlGenericErrorContext,
1604 "adding sibling %s to ", name);
1605 xmlDebugDumpOneNode(stderr, parent, 0);
1606#endif
1607 xmlAddSibling(parent, ret);
1608 }
1609 }
1610
1611 /*
1612 * Insert all the defaulted attributes from the DTD especially namespaces
1613 */
1614 if ((!ctxt->html) &&
1615 ((ctxt->myDoc->intSubset != NULL) ||
1616 (ctxt->myDoc->extSubset != NULL))) {
1617 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1618 }
1619
1620 /*
1621 * process all the attributes whose name start with "xmlns"
1622 */
1623 if (atts != NULL) {
1624 i = 0;
1625 att = atts[i++];
1626 value = atts[i++];
1627 if (!ctxt->html) {
1628 while ((att != NULL) && (value != NULL)) {
1629 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1630 (att[3] == 'n') && (att[4] == 's'))
1631 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1632
1633 att = atts[i++];
1634 value = atts[i++];
1635 }
1636 }
1637 }
1638
1639 /*
1640 * Search the namespace, note that since the attributes have been
1641 * processed, the local namespaces are available.
1642 */
1643 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1644 if ((ns == NULL) && (parent != NULL))
1645 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1646 if ((prefix != NULL) && (ns == NULL)) {
1647 ns = xmlNewNs(ret, NULL, prefix);
Daniel Veillard77aad342006-07-13 06:21:09 +00001648 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1649 "Namespace prefix %s is not defined\n",
1650 prefix, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001651 }
1652
1653 /*
1654 * set the namespace node, making sure that if the default namspace
1655 * is unbound on a parent we simply kee it NULL
1656 */
1657 if ((ns != NULL) && (ns->href != NULL) &&
1658 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1659 xmlSetNs(ret, ns);
1660
1661 /*
1662 * process all the other attributes
1663 */
1664 if (atts != NULL) {
1665 i = 0;
1666 att = atts[i++];
1667 value = atts[i++];
1668 if (ctxt->html) {
1669 while (att != NULL) {
1670 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1671 att = atts[i++];
1672 value = atts[i++];
1673 }
1674 } else {
1675 while ((att != NULL) && (value != NULL)) {
1676 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1677 (att[3] != 'n') || (att[4] != 's'))
1678 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1679
1680 /*
1681 * Next ones
1682 */
1683 att = atts[i++];
1684 value = atts[i++];
1685 }
1686 }
1687 }
1688
Daniel Veillard4432df22003-09-28 18:58:27 +00001689#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001690 /*
1691 * If it's the Document root, finish the DTD validation and
1692 * check the document root element for validity
1693 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00001694 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001695 int chk;
1696
1697 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1698 if (chk <= 0)
1699 ctxt->valid = 0;
1700 if (chk < 0)
1701 ctxt->wellFormed = 0;
1702 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00001703 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001704 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001705#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001706
1707 if (prefix != NULL)
1708 xmlFree(prefix);
1709
1710}
1711
1712/**
1713 * xmlSAX2EndElement:
1714 * @ctx: the user data (XML parser context)
1715 * @name: The element name
1716 *
1717 * called when the end of an element has been detected.
1718 */
1719void
1720xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1721{
1722 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1723 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001724 xmlNodePtr cur;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001725
Daniel Veillard34099b42004-11-04 17:34:35 +00001726 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001727 cur = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001728#ifdef DEBUG_SAX
1729 if (name == NULL)
1730 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1731 else
1732 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1733#endif
1734
1735 /* Capture end position and add node */
1736 if (cur != NULL && ctxt->record_info) {
1737 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1738 node_info.end_line = ctxt->input->line;
1739 node_info.node = cur;
1740 xmlParserAddNodeInfo(ctxt, &node_info);
1741 }
1742 ctxt->nodemem = -1;
1743
Daniel Veillard4432df22003-09-28 18:58:27 +00001744#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001745 if (ctxt->validate && ctxt->wellFormed &&
1746 ctxt->myDoc && ctxt->myDoc->intSubset)
1747 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1748 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001749#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001750
1751
1752 /*
1753 * end of parsing of this node.
1754 */
1755#ifdef DEBUG_SAX_TREE
1756 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1757#endif
1758 nodePop(ctxt);
1759}
Daniel Veillard81273902003-09-30 00:43:48 +00001760#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001761
Daniel Veillarde57ec792003-09-10 10:50:59 +00001762/*
Daniel Veillard19895052003-09-17 13:59:32 +00001763 * xmlSAX2TextNode:
1764 * @ctxt: the parser context
1765 * @str: the input string
1766 * @len: the string length
1767 *
1768 * Remove the entities from an attribute value
1769 *
1770 * Returns the newly allocated string or NULL if not needed or error
1771 */
1772static xmlNodePtr
1773xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1774 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001775 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001776
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001777 /*
1778 * Allocate
1779 */
Daniel Veillard19895052003-09-17 13:59:32 +00001780 if (ctxt->freeElems != NULL) {
1781 ret = ctxt->freeElems;
1782 ctxt->freeElems = ret->next;
1783 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001784 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001785 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001786 }
1787 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001788 xmlErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard19895052003-09-17 13:59:32 +00001789 return(NULL);
1790 }
Daniel Veillard8874b942005-08-25 13:19:21 +00001791 memset(ret, 0, sizeof(xmlNode));
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001792 /*
1793 * intern the formatting blanks found between tags, or the
1794 * very short strings
1795 */
1796 if (ctxt->dictNames) {
1797 xmlChar cur = str[len];
1798
Daniel Veillard8874b942005-08-25 13:19:21 +00001799 if ((len < (int) (2 * sizeof(void *))) &&
1800 (ctxt->options & XML_PARSE_COMPACT)) {
1801 /* store the string in the node overrithing properties and nsDef */
1802 xmlChar *tmp = (xmlChar *) &(ret->properties);
1803 memcpy(tmp, str, len);
1804 tmp[len] = 0;
1805 intern = tmp;
1806 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001807 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001808 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001809 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001810 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001811 int i;
1812
1813 for (i = 1;i < len;i++) {
Daniel Veillard1a9b7082004-01-02 10:42:01 +00001814 if (!IS_BLANK_CH(str[i])) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001815 }
1816 intern = xmlDictLookup(ctxt->dict, str, len);
1817 }
1818 }
1819skip:
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001820 ret->type = XML_TEXT_NODE;
1821
1822 ret->name = xmlStringText;
William M. Brack9f797ab2004-07-28 07:40:12 +00001823 if (intern == NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001824 ret->content = xmlStrndup(str, len);
William M. Brack9f797ab2004-07-28 07:40:12 +00001825 if (ret->content == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001826 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1827 xmlFree(ret);
William M. Brack9f797ab2004-07-28 07:40:12 +00001828 return(NULL);
1829 }
1830 } else
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001831 ret->content = (xmlChar *) intern;
1832
1833 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1834 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001835 return(ret);
1836}
1837
Daniel Veillard4432df22003-09-28 18:58:27 +00001838#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001839/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001840 * xmlSAX2DecodeAttrEntities:
1841 * @ctxt: the parser context
1842 * @str: the input string
1843 * @len: the string length
1844 *
1845 * Remove the entities from an attribute value
1846 *
1847 * Returns the newly allocated string or NULL if not needed or error
1848 */
1849static xmlChar *
1850xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1851 const xmlChar *end) {
1852 const xmlChar *in;
1853 xmlChar *ret;
1854
1855 in = str;
1856 while (in < end)
1857 if (*in++ == '&')
1858 goto decode;
1859 return(NULL);
1860decode:
1861 ctxt->depth++;
1862 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1863 XML_SUBSTITUTE_REF, 0,0,0);
1864 ctxt->depth--;
1865 return(ret);
1866}
Daniel Veillard4432df22003-09-28 18:58:27 +00001867#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001868
1869/**
1870 * xmlSAX2AttributeNs:
1871 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001872 * @localname: the local name of the attribute
1873 * @prefix: the attribute namespace prefix if available
1874 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001875 * @value: Start of the attribute value
1876 * @valueend: end of the attribute value
1877 *
1878 * Handle an attribute that has been read by the parser.
1879 * The default handling is to convert the attribute into an
1880 * DOM subtree and past it in a new xmlAttr element added to
1881 * the element.
1882 */
1883static void
1884xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1885 const xmlChar * localname,
1886 const xmlChar * prefix,
1887 const xmlChar * value,
1888 const xmlChar * valueend)
1889{
1890 xmlAttrPtr ret;
1891 xmlNsPtr namespace = NULL;
1892 xmlChar *dup = NULL;
1893
Daniel Veillarde57ec792003-09-10 10:50:59 +00001894 /*
1895 * Note: if prefix == NULL, the attribute is not in the default namespace
1896 */
1897 if (prefix != NULL)
1898 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1899
Daniel Veillard8a44e592003-09-15 14:50:06 +00001900 /*
1901 * allocate the node
1902 */
1903 if (ctxt->freeAttrs != NULL) {
1904 ret = ctxt->freeAttrs;
1905 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001906 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001907 memset(ret, 0, sizeof(xmlAttr));
1908 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001909
Daniel Veillard8a44e592003-09-15 14:50:06 +00001910 ret->parent = ctxt->node;
1911 ret->doc = ctxt->myDoc;
1912 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001913
Daniel Veillard8a44e592003-09-15 14:50:06 +00001914 if (ctxt->dictNames)
1915 ret->name = localname;
1916 else
1917 ret->name = xmlStrdup(localname);
1918
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001919 /* link at the end to preserv order, TODO speed up with a last */
1920 if (ctxt->node->properties == NULL) {
1921 ctxt->node->properties = ret;
1922 } else {
1923 xmlAttrPtr prev = ctxt->node->properties;
1924
1925 while (prev->next != NULL) prev = prev->next;
1926 prev->next = ret;
1927 ret->prev = prev;
1928 }
1929
Daniel Veillard8a44e592003-09-15 14:50:06 +00001930 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1931 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1932 } else {
1933 if (ctxt->dictNames)
1934 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1935 (xmlChar *) localname, NULL);
1936 else
1937 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1938 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001939 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00001940 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001941 }
1942 }
1943
Daniel Veillard8a44e592003-09-15 14:50:06 +00001944 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1945 xmlNodePtr tmp;
1946
Daniel Veillard19895052003-09-17 13:59:32 +00001947 /*
1948 * We know that if there is an entity reference, then
1949 * the string has been dup'ed and terminates with 0
1950 * otherwise with ' or "
1951 */
1952 if (*valueend != 0) {
1953 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1954 ret->children = tmp;
1955 ret->last = tmp;
1956 if (tmp != NULL) {
1957 tmp->doc = ret->doc;
1958 tmp->parent = (xmlNodePtr) ret;
1959 }
1960 } else {
1961 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1962 valueend - value);
1963 tmp = ret->children;
1964 while (tmp != NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001965 tmp->doc = ret->doc;
Daniel Veillard19895052003-09-17 13:59:32 +00001966 tmp->parent = (xmlNodePtr) ret;
1967 if (tmp->next == NULL)
1968 ret->last = tmp;
1969 tmp = tmp->next;
1970 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001971 }
1972 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00001973 xmlNodePtr tmp;
1974
1975 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1976 ret->children = tmp;
1977 ret->last = tmp;
1978 if (tmp != NULL) {
1979 tmp->doc = ret->doc;
1980 tmp->parent = (xmlNodePtr) ret;
1981 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001982 }
1983
Daniel Veillard4432df22003-09-28 18:58:27 +00001984#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00001985 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1986 ctxt->myDoc && ctxt->myDoc->intSubset) {
1987 /*
1988 * If we don't substitute entities, the validation should be
1989 * done on a value with replaced entities anyway.
1990 */
1991 if (!ctxt->replaceEntities) {
1992 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
1993 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00001994 if (*valueend == 0) {
1995 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1996 ctxt->myDoc, ctxt->node, ret, value);
1997 } else {
1998 /*
1999 * That should already be normalized.
2000 * cheaper to finally allocate here than duplicate
2001 * entry points in the full validation code
2002 */
2003 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002004
Daniel Veillard62998c02003-09-15 12:56:36 +00002005 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2006 ctxt->myDoc, ctxt->node, ret, dup);
2007 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002008 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00002009 /*
2010 * dup now contains a string of the flattened attribute
2011 * content with entities substitued. Check if we need to
2012 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002013 * It need to be done twice ... it's an extra burden related
2014 * to the ability to keep references in attributes
2015 */
Daniel Veillard62998c02003-09-15 12:56:36 +00002016 if (ctxt->attsSpecial != NULL) {
2017 xmlChar *nvalnorm;
2018 xmlChar fn[50];
2019 xmlChar *fullname;
2020
2021 fullname = xmlBuildQName(localname, prefix, fn, 50);
2022 if (fullname != NULL) {
2023 ctxt->vctxt.valid = 1;
2024 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2025 &ctxt->vctxt, ctxt->myDoc,
2026 ctxt->node, fullname, dup);
2027 if (ctxt->vctxt.valid != 1)
2028 ctxt->valid = 0;
2029
2030 if ((fullname != fn) && (fullname != localname))
2031 xmlFree(fullname);
2032 if (nvalnorm != NULL) {
2033 xmlFree(dup);
2034 dup = nvalnorm;
2035 }
2036 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002037 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002038
2039 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2040 ctxt->myDoc, ctxt->node, ret, dup);
2041 }
2042 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002043 /*
2044 * if entities already have been substitued, then
2045 * the attribute as passed is already normalized
2046 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002047 dup = xmlStrndup(value, valueend - value);
2048
2049 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2050 ctxt->myDoc, ctxt->node, ret, dup);
2051 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002052 } else
2053#endif /* LIBXML_VALID_ENABLED */
2054 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002055 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2056 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2057 /*
2058 * when validating, the ID registration is done at the attribute
2059 * validation level. Otherwise we have to do specific handling here.
2060 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002061 if ((prefix == ctxt->str_xml) &&
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002062 (localname[0] == 'i') && (localname[1] == 'd') &&
2063 (localname[2] == 0)) {
2064 /*
2065 * Add the xml:id value
2066 *
2067 * Open issue: normalization of the value.
2068 */
2069 if (dup == NULL)
2070 dup = xmlStrndup(value, valueend - value);
William M. Brack5ef2f812004-05-23 23:56:47 +00002071#ifdef LIBXML_VALID_ENABLED
Daniel Veillard68cb4b22004-04-18 20:55:39 +00002072 if (xmlValidateNCName(dup, 1) != 0) {
2073 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2074 "xml:id : attribute value %s is not an NCName\n",
2075 (const char *) dup, NULL);
2076 }
William M. Brack3f147372004-05-22 01:09:26 +00002077#endif
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002078 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002079 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2080 /* might be worth duplicate entry points and not copy */
2081 if (dup == NULL)
2082 dup = xmlStrndup(value, valueend - value);
2083 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2084 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2085 if (dup == NULL)
2086 dup = xmlStrndup(value, valueend - value);
2087 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002088 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002089 }
2090 if (dup != NULL)
2091 xmlFree(dup);
2092}
2093
2094/**
2095 * xmlSAX2StartElementNs:
2096 * @ctx: the user data (XML parser context)
2097 * @localname: the local name of the element
2098 * @prefix: the element namespace prefix if available
2099 * @URI: the element namespace name if available
2100 * @nb_namespaces: number of namespace definitions on that node
2101 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2102 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002103 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002104 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2105 * attribute values.
2106 *
2107 * SAX2 callback when an element start has been detected by the parser.
2108 * It provides the namespace informations for the element, as well as
2109 * the new namespace declarations on the element.
2110 */
2111void
2112xmlSAX2StartElementNs(void *ctx,
2113 const xmlChar *localname,
2114 const xmlChar *prefix,
2115 const xmlChar *URI,
2116 int nb_namespaces,
2117 const xmlChar **namespaces,
2118 int nb_attributes,
2119 int nb_defaulted,
2120 const xmlChar **attributes)
2121{
2122 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2123 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002124 xmlNodePtr parent;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002125 xmlNsPtr last = NULL, ns;
2126 const xmlChar *uri, *pref;
2127 int i, j;
2128
Daniel Veillard34099b42004-11-04 17:34:35 +00002129 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002130 parent = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002131 /*
2132 * First check on validity:
2133 */
2134 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2135 ((ctxt->myDoc->intSubset == NULL) ||
2136 ((ctxt->myDoc->intSubset->notations == NULL) &&
2137 (ctxt->myDoc->intSubset->elements == NULL) &&
2138 (ctxt->myDoc->intSubset->attributes == NULL) &&
2139 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00002140 xmlErrValid(ctxt, XML_ERR_NO_DTD,
2141 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002142 ctxt->validate = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002143 }
2144
Daniel Veillard8a44e592003-09-15 14:50:06 +00002145 /*
2146 * allocate the node
2147 */
2148 if (ctxt->freeElems != NULL) {
2149 ret = ctxt->freeElems;
2150 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00002151 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00002152 memset(ret, 0, sizeof(xmlNode));
2153 ret->type = XML_ELEMENT_NODE;
2154
2155 if (ctxt->dictNames)
2156 ret->name = localname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002157 else {
Daniel Veillard8a44e592003-09-15 14:50:06 +00002158 ret->name = xmlStrdup(localname);
William M. Brack9f797ab2004-07-28 07:40:12 +00002159 if (ret->name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002160 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002161 return;
2162 }
2163 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002164 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2165 xmlRegisterNodeDefaultValue(ret);
2166 } else {
2167 if (ctxt->dictNames)
2168 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2169 (xmlChar *) localname, NULL);
2170 else
2171 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2172 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002173 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002174 return;
2175 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002176 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002177 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00002178 if (ctxt->input != NULL) {
2179 if (ctxt->input->line < 65535)
2180 ret->line = (short) ctxt->input->line;
2181 else
2182 ret->line = 65535;
2183 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002184 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002185
Daniel Veillard29b17482004-08-16 00:39:03 +00002186 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002187 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002188 }
2189 /*
2190 * Build the namespace list
2191 */
2192 for (i = 0,j = 0;j < nb_namespaces;j++) {
2193 pref = namespaces[i++];
2194 uri = namespaces[i++];
2195 ns = xmlNewNs(NULL, uri, pref);
2196 if (ns != NULL) {
2197 if (last == NULL) {
2198 ret->nsDef = last = ns;
2199 } else {
2200 last->next = ns;
2201 last = ns;
2202 }
2203 if ((URI != NULL) && (prefix == pref))
2204 ret->ns = ns;
2205 } else {
William M. Brack42331a92004-07-29 07:07:16 +00002206 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillarde57ec792003-09-10 10:50:59 +00002207 return;
2208 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002209#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002210 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2211 ctxt->myDoc && ctxt->myDoc->intSubset) {
2212 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2213 ret, prefix, ns, uri);
2214 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002215#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002216 }
2217 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002218
2219 /*
2220 * We are parsing a new node.
2221 */
2222 nodePush(ctxt, ret);
2223
2224 /*
2225 * Link the child element
2226 */
2227 if (parent != NULL) {
2228 if (parent->type == XML_ELEMENT_NODE) {
2229 xmlAddChild(parent, ret);
2230 } else {
2231 xmlAddSibling(parent, ret);
2232 }
2233 }
2234
2235 /*
2236 * Insert the defaulted attributes from the DTD only if requested:
2237 */
2238 if ((nb_defaulted != 0) &&
2239 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2240 nb_attributes -= nb_defaulted;
2241
2242 /*
2243 * Search the namespace if it wasn't already found
William M. Brackbf5cf212004-08-31 06:47:17 +00002244 * Note that, if prefix is NULL, this searches for the default Ns
Daniel Veillarde57ec792003-09-10 10:50:59 +00002245 */
2246 if ((URI != NULL) && (ret->ns == NULL)) {
William M. Brackbf5cf212004-08-31 06:47:17 +00002247 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002248 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2249 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2250 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002251 if (ret->ns == NULL) {
2252 ns = xmlNewNs(ret, NULL, prefix);
William M. Brack9f797ab2004-07-28 07:40:12 +00002253 if (ns == NULL) {
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002254
William M. Brack42331a92004-07-29 07:07:16 +00002255 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002256 return;
2257 }
Daniel Veillard77aad342006-07-13 06:21:09 +00002258 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2259 "Namespace prefix %s was not found\n",
2260 prefix, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002261 }
2262 }
2263
2264 /*
2265 * process all the other attributes
2266 */
2267 if (nb_attributes > 0) {
2268 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2269 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2270 attributes[j+3], attributes[j+4]);
2271 }
2272 }
2273
Daniel Veillard4432df22003-09-28 18:58:27 +00002274#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002275 /*
2276 * If it's the Document root, finish the DTD validation and
2277 * check the document root element for validity
2278 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00002279 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002280 int chk;
2281
2282 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2283 if (chk <= 0)
2284 ctxt->valid = 0;
2285 if (chk < 0)
2286 ctxt->wellFormed = 0;
2287 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00002288 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002289 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002290#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002291}
2292
2293/**
2294 * xmlSAX2EndElementNs:
2295 * @ctx: the user data (XML parser context)
2296 * @localname: the local name of the element
2297 * @prefix: the element namespace prefix if available
2298 * @URI: the element namespace name if available
2299 *
2300 * SAX2 callback when an element end has been detected by the parser.
2301 * It provides the namespace informations for the element.
2302 */
2303void
2304xmlSAX2EndElementNs(void *ctx,
2305 const xmlChar * localname ATTRIBUTE_UNUSED,
2306 const xmlChar * prefix ATTRIBUTE_UNUSED,
2307 const xmlChar * URI ATTRIBUTE_UNUSED)
2308{
2309 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2310 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002311 xmlNodePtr cur;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002312
Daniel Veillard34099b42004-11-04 17:34:35 +00002313 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002314 cur = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002315 /* Capture end position and add node */
2316 if ((ctxt->record_info) && (cur != NULL)) {
2317 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2318 node_info.end_line = ctxt->input->line;
2319 node_info.node = cur;
2320 xmlParserAddNodeInfo(ctxt, &node_info);
2321 }
2322 ctxt->nodemem = -1;
2323
Daniel Veillard4432df22003-09-28 18:58:27 +00002324#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002325 if (ctxt->validate && ctxt->wellFormed &&
2326 ctxt->myDoc && ctxt->myDoc->intSubset)
2327 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002328#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002329
2330 /*
2331 * end of parsing of this node.
2332 */
2333 nodePop(ctxt);
2334}
2335
Daniel Veillard1af9a412003-08-20 22:54:39 +00002336/**
2337 * xmlSAX2Reference:
2338 * @ctx: the user data (XML parser context)
2339 * @name: The entity name
2340 *
2341 * called when an entity xmlSAX2Reference is detected.
2342 */
2343void
2344xmlSAX2Reference(void *ctx, const xmlChar *name)
2345{
2346 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2347 xmlNodePtr ret;
2348
Daniel Veillard34099b42004-11-04 17:34:35 +00002349 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002350#ifdef DEBUG_SAX
2351 xmlGenericError(xmlGenericErrorContext,
2352 "SAX.xmlSAX2Reference(%s)\n", name);
2353#endif
2354 if (name[0] == '#')
2355 ret = xmlNewCharRef(ctxt->myDoc, name);
2356 else
2357 ret = xmlNewReference(ctxt->myDoc, name);
2358#ifdef DEBUG_SAX_TREE
2359 xmlGenericError(xmlGenericErrorContext,
2360 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2361#endif
2362 xmlAddChild(ctxt->node, ret);
2363}
2364
2365/**
2366 * xmlSAX2Characters:
2367 * @ctx: the user data (XML parser context)
2368 * @ch: a xmlChar string
2369 * @len: the number of xmlChar
2370 *
2371 * receiving some chars from the parser.
2372 */
2373void
2374xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2375{
2376 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2377 xmlNodePtr lastChild;
2378
Daniel Veillard34099b42004-11-04 17:34:35 +00002379 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002380#ifdef DEBUG_SAX
2381 xmlGenericError(xmlGenericErrorContext,
2382 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2383#endif
2384 /*
2385 * Handle the data if any. If there is no child
2386 * add it as content, otherwise if the last child is text,
2387 * concatenate it, else create a new node of type text.
2388 */
2389
2390 if (ctxt->node == NULL) {
2391#ifdef DEBUG_SAX_TREE
2392 xmlGenericError(xmlGenericErrorContext,
2393 "add chars: ctxt->node == NULL !\n");
2394#endif
2395 return;
2396 }
Daniel Veillard19895052003-09-17 13:59:32 +00002397 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002398#ifdef DEBUG_SAX_TREE
2399 xmlGenericError(xmlGenericErrorContext,
2400 "add chars to %s \n", ctxt->node->name);
2401#endif
2402
2403 /*
2404 * Here we needed an accelerator mechanism in case of very large
2405 * elements. Use an attribute in the structure !!!
2406 */
2407 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002408 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2409 if (lastChild != NULL) {
2410 ctxt->node->children = lastChild;
2411 ctxt->node->last = lastChild;
2412 lastChild->parent = ctxt->node;
2413 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002414 ctxt->nodelen = len;
2415 ctxt->nodemem = len + 1;
William M. Bracka3215c72004-07-31 16:24:01 +00002416 } else {
2417 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2418 return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002419 }
2420 } else {
2421 int coalesceText = (lastChild != NULL) &&
2422 (lastChild->type == XML_TEXT_NODE) &&
2423 (lastChild->name == xmlStringText);
2424 if ((coalesceText) && (ctxt->nodemem != 0)) {
2425 /*
2426 * The whole point of maintaining nodelen and nodemem,
2427 * xmlTextConcat is too costly, i.e. compute length,
2428 * reallocate a new buffer, move data, append ch. Here
2429 * We try to minimaze realloc() uses and avoid copying
2430 * and recomputing length over and over.
2431 */
Daniel Veillard8874b942005-08-25 13:19:21 +00002432 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2433 lastChild->content = xmlStrdup(lastChild->content);
2434 lastChild->properties = NULL;
2435 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2436 (xmlDictOwns(ctxt->dict, lastChild->content))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002437 lastChild->content = xmlStrdup(lastChild->content);
2438 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002439 if (ctxt->nodelen + len >= ctxt->nodemem) {
2440 xmlChar *newbuf;
2441 int size;
2442
2443 size = ctxt->nodemem + len;
2444 size *= 2;
2445 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2446 if (newbuf == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002447 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002448 return;
2449 }
2450 ctxt->nodemem = size;
2451 lastChild->content = newbuf;
2452 }
2453 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2454 ctxt->nodelen += len;
2455 lastChild->content[ctxt->nodelen] = 0;
2456 } else if (coalesceText) {
2457 if (xmlTextConcat(lastChild, ch, len)) {
William M. Brack42331a92004-07-29 07:07:16 +00002458 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002459 }
2460 if (ctxt->node->children != NULL) {
2461 ctxt->nodelen = xmlStrlen(lastChild->content);
2462 ctxt->nodemem = ctxt->nodelen + 1;
2463 }
2464 } else {
2465 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002466 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2467 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002468 xmlAddChild(ctxt->node, lastChild);
2469 if (ctxt->node->children != NULL) {
2470 ctxt->nodelen = len;
2471 ctxt->nodemem = len + 1;
2472 }
2473 }
2474 }
2475 }
2476}
2477
2478/**
2479 * xmlSAX2IgnorableWhitespace:
2480 * @ctx: the user data (XML parser context)
2481 * @ch: a xmlChar string
2482 * @len: the number of xmlChar
2483 *
2484 * receiving some ignorable whitespaces from the parser.
2485 * UNUSED: by default the DOM building will use xmlSAX2Characters
2486 */
2487void
2488xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2489{
2490 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2491#ifdef DEBUG_SAX
2492 xmlGenericError(xmlGenericErrorContext,
2493 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2494#endif
2495}
2496
2497/**
2498 * xmlSAX2ProcessingInstruction:
2499 * @ctx: the user data (XML parser context)
2500 * @target: the target name
2501 * @data: the PI data's
2502 *
2503 * A processing instruction has been parsed.
2504 */
2505void
2506xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2507 const xmlChar *data)
2508{
2509 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2510 xmlNodePtr ret;
Daniel Veillard6128c012004-11-08 17:16:15 +00002511 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002512
Daniel Veillard34099b42004-11-04 17:34:35 +00002513 if (ctx == NULL) return;
Daniel Veillard6128c012004-11-08 17:16:15 +00002514 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002515#ifdef DEBUG_SAX
2516 xmlGenericError(xmlGenericErrorContext,
2517 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2518#endif
2519
Daniel Veillard03a53c32004-10-26 16:06:51 +00002520 ret = xmlNewDocPI(ctxt->myDoc, target, data);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002521 if (ret == NULL) return;
2522 parent = ctxt->node;
2523
Daniel Veillard73da77e2005-08-24 14:05:37 +00002524 if (ctxt->linenumbers) {
2525 if (ctxt->input != NULL) {
2526 if (ctxt->input->line < 65535)
2527 ret->line = (short) ctxt->input->line;
2528 else
2529 ret->line = 65535;
2530 }
2531 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002532 if (ctxt->inSubset == 1) {
2533 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2534 return;
2535 } else if (ctxt->inSubset == 2) {
2536 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2537 return;
2538 }
2539 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2540#ifdef DEBUG_SAX_TREE
2541 xmlGenericError(xmlGenericErrorContext,
2542 "Setting PI %s as root\n", target);
2543#endif
2544 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2545 return;
2546 }
2547 if (parent->type == XML_ELEMENT_NODE) {
2548#ifdef DEBUG_SAX_TREE
2549 xmlGenericError(xmlGenericErrorContext,
2550 "adding PI %s child to %s\n", target, parent->name);
2551#endif
2552 xmlAddChild(parent, ret);
2553 } else {
2554#ifdef DEBUG_SAX_TREE
2555 xmlGenericError(xmlGenericErrorContext,
2556 "adding PI %s sibling to ", target);
2557 xmlDebugDumpOneNode(stderr, parent, 0);
2558#endif
2559 xmlAddSibling(parent, ret);
2560 }
2561}
2562
2563/**
2564 * xmlSAX2Comment:
2565 * @ctx: the user data (XML parser context)
2566 * @value: the xmlSAX2Comment content
2567 *
2568 * A xmlSAX2Comment has been parsed.
2569 */
2570void
2571xmlSAX2Comment(void *ctx, const xmlChar *value)
2572{
2573 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2574 xmlNodePtr ret;
Daniel Veillard34099b42004-11-04 17:34:35 +00002575 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002576
Daniel Veillard34099b42004-11-04 17:34:35 +00002577 if (ctx == NULL) return;
2578 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002579#ifdef DEBUG_SAX
2580 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2581#endif
2582 ret = xmlNewDocComment(ctxt->myDoc, value);
2583 if (ret == NULL) return;
Daniel Veillard73da77e2005-08-24 14:05:37 +00002584 if (ctxt->linenumbers) {
2585 if (ctxt->input != NULL) {
2586 if (ctxt->input->line < 65535)
2587 ret->line = (short) ctxt->input->line;
2588 else
2589 ret->line = 65535;
2590 }
2591 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002592
2593 if (ctxt->inSubset == 1) {
2594 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2595 return;
2596 } else if (ctxt->inSubset == 2) {
2597 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2598 return;
2599 }
2600 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2601#ifdef DEBUG_SAX_TREE
2602 xmlGenericError(xmlGenericErrorContext,
2603 "Setting xmlSAX2Comment as root\n");
2604#endif
2605 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2606 return;
2607 }
2608 if (parent->type == XML_ELEMENT_NODE) {
2609#ifdef DEBUG_SAX_TREE
2610 xmlGenericError(xmlGenericErrorContext,
2611 "adding xmlSAX2Comment child to %s\n", parent->name);
2612#endif
2613 xmlAddChild(parent, ret);
2614 } else {
2615#ifdef DEBUG_SAX_TREE
2616 xmlGenericError(xmlGenericErrorContext,
2617 "adding xmlSAX2Comment sibling to ");
2618 xmlDebugDumpOneNode(stderr, parent, 0);
2619#endif
2620 xmlAddSibling(parent, ret);
2621 }
2622}
2623
2624/**
2625 * xmlSAX2CDataBlock:
2626 * @ctx: the user data (XML parser context)
2627 * @value: The pcdata content
2628 * @len: the block length
2629 *
2630 * called when a pcdata block has been parsed
2631 */
2632void
2633xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2634{
2635 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2636 xmlNodePtr ret, lastChild;
2637
Daniel Veillard34099b42004-11-04 17:34:35 +00002638 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002639#ifdef DEBUG_SAX
2640 xmlGenericError(xmlGenericErrorContext,
2641 "SAX.pcdata(%.10s, %d)\n", value, len);
2642#endif
2643 lastChild = xmlGetLastChild(ctxt->node);
2644#ifdef DEBUG_SAX_TREE
2645 xmlGenericError(xmlGenericErrorContext,
2646 "add chars to %s \n", ctxt->node->name);
2647#endif
2648 if ((lastChild != NULL) &&
2649 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2650 xmlTextConcat(lastChild, value, len);
2651 } else {
2652 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2653 xmlAddChild(ctxt->node, ret);
2654 }
2655}
2656
Daniel Veillard62998c02003-09-15 12:56:36 +00002657static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002658
Daniel Veillard81273902003-09-30 00:43:48 +00002659#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002660/**
2661 * xmlSAXDefaultVersion:
2662 * @version: the version, 1 or 2
2663 *
2664 * Set the default version of SAX used globally by the library.
William M. Brack96d2eff2004-06-30 11:48:47 +00002665 * By default, during initialization the default is set to 2.
2666 * Note that it is generally a better coding style to use
2667 * xmlSAXVersion() to set up the version explicitly for a given
2668 * parsing context.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002669 *
2670 * Returns the previous value in case of success and -1 in case of error.
2671 */
2672int
2673xmlSAXDefaultVersion(int version)
2674{
2675 int ret = xmlSAX2DefaultVersionValue;
2676
2677 if ((version != 1) && (version != 2))
2678 return(-1);
2679 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002680 return(ret);
2681}
Daniel Veillard81273902003-09-30 00:43:48 +00002682#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002683
2684/**
2685 * xmlSAXVersion:
2686 * @hdlr: the SAX handler
2687 * @version: the version, 1 or 2
2688 *
2689 * Initialize the default XML SAX handler according to the version
2690 *
2691 * Returns 0 in case of success and -1 in case of error.
2692 */
2693int
2694xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2695{
2696 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002697 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002698 hdlr->startElement = NULL;
2699 hdlr->endElement = NULL;
2700 hdlr->startElementNs = xmlSAX2StartElementNs;
2701 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002702 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002703 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002704#ifdef LIBXML_SAX1_ENABLED
2705 } else if (version == 1) {
2706 hdlr->startElement = xmlSAX2StartElement;
2707 hdlr->endElement = xmlSAX2EndElement;
2708 hdlr->initialized = 1;
2709#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002710 } else
2711 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002712 hdlr->internalSubset = xmlSAX2InternalSubset;
2713 hdlr->externalSubset = xmlSAX2ExternalSubset;
2714 hdlr->isStandalone = xmlSAX2IsStandalone;
2715 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2716 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2717 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2718 hdlr->getEntity = xmlSAX2GetEntity;
2719 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2720 hdlr->entityDecl = xmlSAX2EntityDecl;
2721 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2722 hdlr->elementDecl = xmlSAX2ElementDecl;
2723 hdlr->notationDecl = xmlSAX2NotationDecl;
2724 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2725 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2726 hdlr->startDocument = xmlSAX2StartDocument;
2727 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002728 hdlr->reference = xmlSAX2Reference;
2729 hdlr->characters = xmlSAX2Characters;
2730 hdlr->cdataBlock = xmlSAX2CDataBlock;
2731 hdlr->ignorableWhitespace = xmlSAX2Characters;
2732 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2733 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002734 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002735 hdlr->error = xmlParserError;
2736 hdlr->fatalError = xmlParserError;
2737
Daniel Veillarde57ec792003-09-10 10:50:59 +00002738 return(0);
2739}
2740
2741/**
2742 * xmlSAX2InitDefaultSAXHandler:
2743 * @hdlr: the SAX handler
2744 * @warning: flag if non-zero sets the handler warning procedure
2745 *
2746 * Initialize the default XML SAX2 handler
2747 */
2748void
2749xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2750{
2751 if ((hdlr == NULL) || (hdlr->initialized != 0))
2752 return;
2753
2754 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2755 if (warning == 0)
2756 hdlr->warning = NULL;
2757 else
2758 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002759}
2760
2761/**
2762 * xmlDefaultSAXHandlerInit:
2763 *
2764 * Initialize the default SAX2 handler
2765 */
2766void
2767xmlDefaultSAXHandlerInit(void)
2768{
Daniel Veillard81273902003-09-30 00:43:48 +00002769#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002770 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002771#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002772}
2773
2774#ifdef LIBXML_HTML_ENABLED
2775
2776/**
2777 * xmlSAX2InitHtmlDefaultSAXHandler:
2778 * @hdlr: the SAX handler
2779 *
2780 * Initialize the default HTML SAX2 handler
2781 */
2782void
2783xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2784{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002785 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002786 return;
2787
2788 hdlr->internalSubset = xmlSAX2InternalSubset;
2789 hdlr->externalSubset = NULL;
2790 hdlr->isStandalone = NULL;
2791 hdlr->hasInternalSubset = NULL;
2792 hdlr->hasExternalSubset = NULL;
2793 hdlr->resolveEntity = NULL;
2794 hdlr->getEntity = xmlSAX2GetEntity;
2795 hdlr->getParameterEntity = NULL;
2796 hdlr->entityDecl = NULL;
2797 hdlr->attributeDecl = NULL;
2798 hdlr->elementDecl = NULL;
2799 hdlr->notationDecl = NULL;
2800 hdlr->unparsedEntityDecl = NULL;
2801 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2802 hdlr->startDocument = xmlSAX2StartDocument;
2803 hdlr->endDocument = xmlSAX2EndDocument;
2804 hdlr->startElement = xmlSAX2StartElement;
2805 hdlr->endElement = xmlSAX2EndElement;
2806 hdlr->reference = NULL;
2807 hdlr->characters = xmlSAX2Characters;
2808 hdlr->cdataBlock = xmlSAX2CDataBlock;
2809 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002810 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002811 hdlr->comment = xmlSAX2Comment;
2812 hdlr->warning = xmlParserWarning;
2813 hdlr->error = xmlParserError;
2814 hdlr->fatalError = xmlParserError;
2815
Daniel Veillard092643b2003-09-25 14:29:29 +00002816 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002817}
2818
2819/**
2820 * htmlDefaultSAXHandlerInit:
2821 *
2822 * Initialize the default SAX handler
2823 */
2824void
2825htmlDefaultSAXHandlerInit(void)
2826{
Daniel Veillard092643b2003-09-25 14:29:29 +00002827 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002828}
2829
2830#endif /* LIBXML_HTML_ENABLED */
2831
2832#ifdef LIBXML_DOCB_ENABLED
2833
2834/**
2835 * xmlSAX2InitDocbDefaultSAXHandler:
2836 * @hdlr: the SAX handler
2837 *
2838 * Initialize the default DocBook SAX2 handler
2839 */
2840void
2841xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2842{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002843 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002844 return;
2845
2846 hdlr->internalSubset = xmlSAX2InternalSubset;
2847 hdlr->externalSubset = NULL;
2848 hdlr->isStandalone = xmlSAX2IsStandalone;
2849 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2850 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2851 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2852 hdlr->getEntity = xmlSAX2GetEntity;
2853 hdlr->getParameterEntity = NULL;
2854 hdlr->entityDecl = xmlSAX2EntityDecl;
2855 hdlr->attributeDecl = NULL;
2856 hdlr->elementDecl = NULL;
2857 hdlr->notationDecl = NULL;
2858 hdlr->unparsedEntityDecl = NULL;
2859 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2860 hdlr->startDocument = xmlSAX2StartDocument;
2861 hdlr->endDocument = xmlSAX2EndDocument;
2862 hdlr->startElement = xmlSAX2StartElement;
2863 hdlr->endElement = xmlSAX2EndElement;
2864 hdlr->reference = xmlSAX2Reference;
2865 hdlr->characters = xmlSAX2Characters;
2866 hdlr->cdataBlock = NULL;
2867 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2868 hdlr->processingInstruction = NULL;
2869 hdlr->comment = xmlSAX2Comment;
2870 hdlr->warning = xmlParserWarning;
2871 hdlr->error = xmlParserError;
2872 hdlr->fatalError = xmlParserError;
2873
Daniel Veillardffbbed42003-10-10 14:46:54 +00002874 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002875}
2876
2877/**
2878 * docbDefaultSAXHandlerInit:
2879 *
2880 * Initialize the default SAX handler
2881 */
2882void
2883docbDefaultSAXHandlerInit(void)
2884{
Daniel Veillard092643b2003-09-25 14:29:29 +00002885 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002886}
2887
2888#endif /* LIBXML_DOCB_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002889#define bottom_SAX2
2890#include "elfgcchack.h"