blob: 80cd1bdbedd507db00544266df12adc93748611a [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;
Daniel Veillardf4f4e482008-08-25 08:57:48 +0000583 if (ret->checked == 0)
584 ret->checked = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000585 }
586 return(ret);
587}
588
589/**
590 * xmlSAX2GetParameterEntity:
591 * @ctx: the user data (XML parser context)
592 * @name: The entity name
593 *
594 * Get a parameter entity by name
595 *
596 * Returns the xmlEntityPtr if found.
597 */
598xmlEntityPtr
599xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
600{
601 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
602 xmlEntityPtr ret;
603
Daniel Veillard34099b42004-11-04 17:34:35 +0000604 if (ctx == NULL) return(NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000605#ifdef DEBUG_SAX
606 xmlGenericError(xmlGenericErrorContext,
607 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
608#endif
609
610 ret = xmlGetParameterEntity(ctxt->myDoc, name);
611 return(ret);
612}
613
614
615/**
616 * xmlSAX2EntityDecl:
617 * @ctx: the user data (XML parser context)
618 * @name: the entity name
619 * @type: the entity type
620 * @publicId: The public ID of the entity
621 * @systemId: The system ID of the entity
622 * @content: the entity value (without processing).
623 *
624 * An entity definition has been parsed
625 */
626void
627xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
628 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
629{
630 xmlEntityPtr ent;
631 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
632
Daniel Veillard34099b42004-11-04 17:34:35 +0000633 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000634#ifdef DEBUG_SAX
635 xmlGenericError(xmlGenericErrorContext,
636 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
637 name, type, publicId, systemId, content);
638#endif
639 if (ctxt->inSubset == 1) {
640 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
641 systemId, content);
Daniel Veillard87b30462005-07-05 14:04:36 +0000642 if ((ent == NULL) && (ctxt->pedantic))
643 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
644 "Entity(%s) already defined in the internal subset\n",
645 name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000646 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
647 xmlChar *URI;
648 const char *base = NULL;
649
650 if (ctxt->input != NULL)
651 base = ctxt->input->filename;
652 if (base == NULL)
653 base = ctxt->directory;
654
655 URI = xmlBuildURI(systemId, (const xmlChar *) base);
656 ent->URI = URI;
657 }
658 } else if (ctxt->inSubset == 2) {
659 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
660 systemId, content);
661 if ((ent == NULL) && (ctxt->pedantic) &&
662 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000663 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000664 "Entity(%s) already defined in the external subset\n", name);
665 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
666 xmlChar *URI;
667 const char *base = NULL;
668
669 if (ctxt->input != NULL)
670 base = ctxt->input->filename;
671 if (base == NULL)
672 base = ctxt->directory;
673
674 URI = xmlBuildURI(systemId, (const xmlChar *) base);
675 ent->URI = URI;
676 }
677 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000678 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
679 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
680 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000681 }
682}
683
684/**
685 * xmlSAX2AttributeDecl:
686 * @ctx: the user data (XML parser context)
687 * @elem: the name of the element
688 * @fullname: the attribute name
689 * @type: the attribute type
690 * @def: the type of default value
691 * @defaultValue: the attribute default value
692 * @tree: the tree of enumerated value set
693 *
694 * An attribute definition has been parsed
695 */
696void
697xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
698 int type, int def, const xmlChar *defaultValue,
699 xmlEnumerationPtr tree)
700{
701 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
702 xmlAttributePtr attr;
703 xmlChar *name = NULL, *prefix = NULL;
704
Daniel Veillard2728f842006-03-09 16:49:24 +0000705 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
706 return;
707
Daniel Veillard1af9a412003-08-20 22:54:39 +0000708#ifdef DEBUG_SAX
709 xmlGenericError(xmlGenericErrorContext,
710 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
711 elem, fullname, type, def, defaultValue);
712#endif
Daniel Veillard68cb4b22004-04-18 20:55:39 +0000713 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
714 (type != XML_ATTRIBUTE_ID)) {
715 /*
716 * Raise the error but keep the validity flag
717 */
718 int tmp = ctxt->valid;
719 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
720 "xml:id : attribute type should be ID\n", NULL, NULL);
721 ctxt->valid = tmp;
722 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000723 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000724 name = xmlSplitQName(ctxt, fullname, &prefix);
725 ctxt->vctxt.valid = 1;
726 if (ctxt->inSubset == 1)
727 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
728 name, prefix, (xmlAttributeType) type,
729 (xmlAttributeDefault) def, defaultValue, tree);
730 else if (ctxt->inSubset == 2)
731 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
732 name, prefix, (xmlAttributeType) type,
733 (xmlAttributeDefault) def, defaultValue, tree);
734 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000735 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
736 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
737 name, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000738 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000739 return;
740 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000741#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000742 if (ctxt->vctxt.valid == 0)
743 ctxt->valid = 0;
744 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
Daniel Veillard2728f842006-03-09 16:49:24 +0000745 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000746 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
747 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000748#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000749 if (prefix != NULL)
750 xmlFree(prefix);
751 if (name != NULL)
752 xmlFree(name);
753}
754
755/**
756 * xmlSAX2ElementDecl:
757 * @ctx: the user data (XML parser context)
758 * @name: the element name
759 * @type: the element type
760 * @content: the element value tree
761 *
762 * An element definition has been parsed
763 */
764void
765xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
766 xmlElementContentPtr content)
767{
768 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
769 xmlElementPtr elem = NULL;
770
Daniel Veillard2728f842006-03-09 16:49:24 +0000771 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
772 return;
773
Daniel Veillard1af9a412003-08-20 22:54:39 +0000774#ifdef DEBUG_SAX
775 xmlGenericError(xmlGenericErrorContext,
776 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
777#endif
778
779 if (ctxt->inSubset == 1)
780 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
781 name, (xmlElementTypeVal) type, content);
782 else if (ctxt->inSubset == 2)
783 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
784 name, (xmlElementTypeVal) type, content);
785 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000786 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
787 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
788 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000789 return;
790 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000791#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000792 if (elem == NULL)
793 ctxt->valid = 0;
794 if (ctxt->validate && ctxt->wellFormed &&
795 ctxt->myDoc && ctxt->myDoc->intSubset)
796 ctxt->valid &=
797 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +0000798#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000799}
800
801/**
802 * xmlSAX2NotationDecl:
803 * @ctx: the user data (XML parser context)
804 * @name: The name of the notation
805 * @publicId: The public ID of the entity
806 * @systemId: The system ID of the entity
807 *
808 * What to do when a notation declaration has been parsed.
809 */
810void
811xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
812 const xmlChar *publicId, const xmlChar *systemId)
813{
814 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
815 xmlNotationPtr nota = NULL;
816
Daniel Veillard2728f842006-03-09 16:49:24 +0000817 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
818 return;
819
Daniel Veillard1af9a412003-08-20 22:54:39 +0000820#ifdef DEBUG_SAX
821 xmlGenericError(xmlGenericErrorContext,
822 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
823#endif
824
825 if ((publicId == NULL) && (systemId == NULL)) {
Daniel Veillard87b30462005-07-05 14:04:36 +0000826 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
827 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
828 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000829 return;
830 } else if (ctxt->inSubset == 1)
831 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
832 publicId, systemId);
833 else if (ctxt->inSubset == 2)
834 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
835 publicId, systemId);
836 else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000837 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
838 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
839 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000840 return;
841 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000842#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000843 if (nota == NULL) ctxt->valid = 0;
Daniel Veillard2728f842006-03-09 16:49:24 +0000844 if ((ctxt->validate) && (ctxt->wellFormed) &&
845 (ctxt->myDoc->intSubset != NULL))
Daniel Veillard1af9a412003-08-20 22:54:39 +0000846 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
847 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000848#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000849}
850
851/**
852 * xmlSAX2UnparsedEntityDecl:
853 * @ctx: the user data (XML parser context)
854 * @name: The name of the entity
855 * @publicId: The public ID of the entity
856 * @systemId: The system ID of the entity
857 * @notationName: the name of the notation
858 *
859 * What to do when an unparsed entity declaration is parsed
860 */
861void
862xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
863 const xmlChar *publicId, const xmlChar *systemId,
864 const xmlChar *notationName)
865{
866 xmlEntityPtr ent;
867 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard34099b42004-11-04 17:34:35 +0000868 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000869#ifdef DEBUG_SAX
870 xmlGenericError(xmlGenericErrorContext,
871 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
872 name, publicId, systemId, notationName);
873#endif
874 if (ctxt->inSubset == 1) {
875 ent = xmlAddDocEntity(ctxt->myDoc, name,
876 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
877 publicId, systemId, notationName);
878 if ((ent == NULL) && (ctxt->pedantic) &&
879 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000880 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000881 "Entity(%s) already defined in the internal subset\n", name);
882 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
883 xmlChar *URI;
884 const char *base = NULL;
885
886 if (ctxt->input != NULL)
887 base = ctxt->input->filename;
888 if (base == NULL)
889 base = ctxt->directory;
890
891 URI = xmlBuildURI(systemId, (const xmlChar *) base);
892 ent->URI = URI;
893 }
894 } else if (ctxt->inSubset == 2) {
895 ent = xmlAddDtdEntity(ctxt->myDoc, name,
896 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
897 publicId, systemId, notationName);
898 if ((ent == NULL) && (ctxt->pedantic) &&
899 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000900 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000901 "Entity(%s) already defined in the external subset\n", name);
902 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
903 xmlChar *URI;
904 const char *base = NULL;
905
906 if (ctxt->input != NULL)
907 base = ctxt->input->filename;
908 if (base == NULL)
909 base = ctxt->directory;
910
911 URI = xmlBuildURI(systemId, (const xmlChar *) base);
912 ent->URI = URI;
913 }
914 } else {
Daniel Veillard87b30462005-07-05 14:04:36 +0000915 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
916 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
917 name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000918 }
919}
920
921/**
922 * xmlSAX2SetDocumentLocator:
923 * @ctx: the user data (XML parser context)
924 * @loc: A SAX Locator
925 *
926 * Receive the document locator at startup, actually xmlDefaultSAXLocator
927 * Everything is available on the context, so this is useless in our case.
928 */
929void
930xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
931{
932 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
933#ifdef DEBUG_SAX
934 xmlGenericError(xmlGenericErrorContext,
935 "SAX.xmlSAX2SetDocumentLocator()\n");
936#endif
937}
938
939/**
940 * xmlSAX2StartDocument:
941 * @ctx: the user data (XML parser context)
942 *
943 * called when the document start being processed.
944 */
945void
946xmlSAX2StartDocument(void *ctx)
947{
948 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
949 xmlDocPtr doc;
950
Daniel Veillard34099b42004-11-04 17:34:35 +0000951 if (ctx == NULL) return;
952
Daniel Veillard1af9a412003-08-20 22:54:39 +0000953#ifdef DEBUG_SAX
954 xmlGenericError(xmlGenericErrorContext,
955 "SAX.xmlSAX2StartDocument()\n");
956#endif
957 if (ctxt->html) {
958#ifdef LIBXML_HTML_ENABLED
959 if (ctxt->myDoc == NULL)
960 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
Daniel Veillardae0765b2008-07-31 19:54:59 +0000961 ctxt->myDoc->properties = XML_DOC_HTML;
962 ctxt->myDoc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000963 if (ctxt->myDoc == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +0000964 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000965 return;
966 }
967#else
968 xmlGenericError(xmlGenericErrorContext,
969 "libxml2 built without HTML support\n");
970 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
971 ctxt->instate = XML_PARSER_EOF;
972 ctxt->disableSAX = 1;
973 return;
974#endif
975 } else {
976 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
977 if (doc != NULL) {
Daniel Veillardae0765b2008-07-31 19:54:59 +0000978 doc->properties = 0;
979 if (ctxt->options & XML_PARSE_OLD10)
980 doc->properties |= XML_DOC_OLD10;
981 doc->parseFlags = ctxt->options;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000982 if (ctxt->encoding != NULL)
983 doc->encoding = xmlStrdup(ctxt->encoding);
984 else
985 doc->encoding = NULL;
986 doc->standalone = ctxt->standalone;
987 } else {
William M. Brack42331a92004-07-29 07:07:16 +0000988 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +0000989 return;
990 }
Daniel Veillard500a1de2004-03-22 15:22:58 +0000991 if ((ctxt->dictNames) && (doc != NULL)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000992 doc->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +0000993 xmlDictReference(doc->dict);
994 }
Daniel Veillard1af9a412003-08-20 22:54:39 +0000995 }
996 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
997 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillardb8efdda2006-10-10 12:37:14 +0000998 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000999 if (ctxt->myDoc->URL == NULL)
William M. Bracka3215c72004-07-31 16:24:01 +00001000 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001001 }
1002}
1003
1004/**
1005 * xmlSAX2EndDocument:
1006 * @ctx: the user data (XML parser context)
1007 *
1008 * called when the document end has been detected.
1009 */
1010void
1011xmlSAX2EndDocument(void *ctx)
1012{
1013 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1014#ifdef DEBUG_SAX
1015 xmlGenericError(xmlGenericErrorContext,
1016 "SAX.xmlSAX2EndDocument()\n");
1017#endif
Daniel Veillard34099b42004-11-04 17:34:35 +00001018 if (ctx == NULL) return;
Daniel Veillard4432df22003-09-28 18:58:27 +00001019#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001020 if (ctxt->validate && ctxt->wellFormed &&
1021 ctxt->myDoc && ctxt->myDoc->intSubset)
1022 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00001023#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001024
1025 /*
1026 * Grab the encoding if it was added on-the-fly
1027 */
1028 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1029 (ctxt->myDoc->encoding == NULL)) {
1030 ctxt->myDoc->encoding = ctxt->encoding;
1031 ctxt->encoding = NULL;
1032 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001033 if ((ctxt->inputTab != NULL) &&
1034 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1035 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001036 (ctxt->myDoc->encoding == NULL)) {
1037 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1038 }
1039 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1040 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1041 ctxt->myDoc->charset = ctxt->charset;
1042 }
1043}
1044
Daniel Veillardbca3ad22005-08-23 22:14:02 +00001045#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001046/**
1047 * xmlSAX2AttributeInternal:
1048 * @ctx: the user data (XML parser context)
1049 * @fullname: The attribute name, including namespace prefix
1050 * @value: The attribute value
1051 * @prefix: the prefix on the element node
1052 *
1053 * Handle an attribute that has been read by the parser.
1054 * The default handling is to convert the attribute into an
1055 * DOM subtree and past it in a new xmlAttr element added to
1056 * the element.
1057 */
1058static void
1059xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001060 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +00001061{
1062 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1063 xmlAttrPtr ret;
1064 xmlChar *name;
1065 xmlChar *ns;
1066 xmlChar *nval;
1067 xmlNsPtr namespace;
1068
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001069 if (ctxt->html) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001070 name = xmlStrdup(fullname);
Daniel Veillarddbbd72b2007-06-12 15:15:52 +00001071 ns = NULL;
1072 namespace = NULL;
1073 } else {
1074 /*
1075 * Split the full name into a namespace prefix and the tag name
1076 */
1077 name = xmlSplitQName(ctxt, fullname, &ns);
1078 if ((name != NULL) && (name[0] == 0)) {
1079 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1080 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1081 "invalid namespace declaration '%s'\n",
1082 fullname, NULL);
1083 } else {
1084 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1085 "Avoid attribute ending with ':' like '%s'\n",
1086 fullname, NULL);
1087 }
1088 if (ns != NULL)
1089 xmlFree(ns);
1090 ns = NULL;
1091 xmlFree(name);
1092 name = xmlStrdup(fullname);
1093 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001094 }
1095 if (name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001096 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001097 if (ns != NULL)
1098 xmlFree(ns);
1099 return;
1100 }
1101
Daniel Veillard4432df22003-09-28 18:58:27 +00001102#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001103 /*
1104 * Do the last stage of the attribute normalization
1105 * Needed for HTML too:
1106 * http://www.w3.org/TR/html4/types.html#h-6.2
1107 */
1108 ctxt->vctxt.valid = 1;
1109 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1110 ctxt->myDoc, ctxt->node,
1111 fullname, value);
1112 if (ctxt->vctxt.valid != 1) {
1113 ctxt->valid = 0;
1114 }
1115 if (nval != NULL)
1116 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +00001117#else
1118 nval = NULL;
1119#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001120
1121 /*
1122 * Check whether it's a namespace definition
1123 */
1124 if ((!ctxt->html) && (ns == NULL) &&
1125 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1126 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1127 xmlNsPtr nsret;
1128 xmlChar *val;
1129
1130 if (!ctxt->replaceEntities) {
1131 ctxt->depth++;
1132 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1133 0,0,0);
1134 ctxt->depth--;
1135 } else {
1136 val = (xmlChar *) value;
1137 }
1138
1139 if (val[0] != 0) {
1140 xmlURIPtr uri;
1141
1142 uri = xmlParseURI((const char *)val);
1143 if (uri == NULL) {
1144 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1145 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +00001146 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001147 } else {
1148 if (uri->scheme == NULL) {
1149 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1150 ctxt->sax->warning(ctxt->userData,
1151 "xmlns: URI %s is not absolute\n", val);
1152 }
1153 xmlFreeURI(uri);
1154 }
1155 }
1156
1157 /* a default namespace definition */
1158 nsret = xmlNewNs(ctxt->node, val, NULL);
1159
Daniel Veillard4432df22003-09-28 18:58:27 +00001160#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001161 /*
1162 * Validate also for namespace decls, they are attributes from
1163 * an XML-1.0 perspective
1164 */
1165 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1166 ctxt->myDoc && ctxt->myDoc->intSubset)
1167 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1168 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +00001169#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001170 if (name != NULL)
1171 xmlFree(name);
1172 if (nval != NULL)
1173 xmlFree(nval);
1174 if (val != value)
1175 xmlFree(val);
1176 return;
1177 }
1178 if ((!ctxt->html) &&
1179 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1180 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1181 xmlNsPtr nsret;
1182 xmlChar *val;
1183
1184 if (!ctxt->replaceEntities) {
1185 ctxt->depth++;
1186 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1187 0,0,0);
1188 ctxt->depth--;
1189 if (val == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001190 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001191 xmlFree(ns);
1192 if (name != NULL)
1193 xmlFree(name);
1194 return;
1195 }
1196 } else {
1197 val = (xmlChar *) value;
1198 }
1199
1200 if (val[0] == 0) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001201 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1202 "Empty namespace name for prefix %s\n", name, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001203 }
1204 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1205 xmlURIPtr uri;
1206
1207 uri = xmlParseURI((const char *)val);
1208 if (uri == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001209 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001210 "xmlns:%s: %s not a valid URI\n", name, value);
1211 } else {
1212 if (uri->scheme == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001213 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001214 "xmlns:%s: URI %s is not absolute\n", name, value);
1215 }
1216 xmlFreeURI(uri);
1217 }
1218 }
1219
1220 /* a standard namespace definition */
1221 nsret = xmlNewNs(ctxt->node, val, name);
1222 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001223#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001224 /*
1225 * Validate also for namespace decls, they are attributes from
1226 * an XML-1.0 perspective
1227 */
1228 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1229 ctxt->myDoc && ctxt->myDoc->intSubset)
1230 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1231 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001232#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001233 if (name != NULL)
1234 xmlFree(name);
1235 if (nval != NULL)
1236 xmlFree(nval);
1237 if (val != value)
1238 xmlFree(val);
1239 return;
1240 }
1241
1242 if (ns != NULL) {
1243 xmlAttrPtr prop;
1244 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillard67906942003-08-28 21:13:25 +00001245 if (namespace == NULL) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001246 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
William M. Brack4811ba32003-09-06 18:02:53 +00001247 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001248 ns, name);
1249 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001250
1251 prop = ctxt->node->properties;
1252 while (prop != NULL) {
1253 if (prop->ns != NULL) {
1254 if ((xmlStrEqual(name, prop->name)) &&
1255 ((namespace == prop->ns) ||
1256 (xmlStrEqual(namespace->href, prop->ns->href)))) {
Daniel Veillard87b30462005-07-05 14:04:36 +00001257 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001258 "Attribute %s in %s redefined\n",
1259 name, namespace->href);
1260 ctxt->wellFormed = 0;
1261 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1262 goto error;
1263 }
1264 }
1265 prop = prop->next;
1266 }
1267 } else {
1268 namespace = NULL;
1269 }
1270
1271 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1272 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1273
1274 if (ret != NULL) {
1275 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1276 xmlNodePtr tmp;
1277
1278 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1279 tmp = ret->children;
1280 while (tmp != NULL) {
1281 tmp->parent = (xmlNodePtr) ret;
1282 if (tmp->next == NULL)
1283 ret->last = tmp;
1284 tmp = tmp->next;
1285 }
1286 } else if (value != NULL) {
1287 ret->children = xmlNewDocText(ctxt->myDoc, value);
1288 ret->last = ret->children;
1289 if (ret->children != NULL)
1290 ret->children->parent = (xmlNodePtr) ret;
1291 }
1292 }
1293
Daniel Veillard4432df22003-09-28 18:58:27 +00001294#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001295 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1296 ctxt->myDoc && ctxt->myDoc->intSubset) {
1297
1298 /*
1299 * If we don't substitute entities, the validation should be
1300 * done on a value with replaced entities anyway.
1301 */
1302 if (!ctxt->replaceEntities) {
1303 xmlChar *val;
1304
1305 ctxt->depth++;
1306 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1307 0,0,0);
1308 ctxt->depth--;
1309
1310 if (val == NULL)
1311 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1312 ctxt->myDoc, ctxt->node, ret, value);
1313 else {
1314 xmlChar *nvalnorm;
1315
1316 /*
1317 * Do the last stage of the attribute normalization
1318 * It need to be done twice ... it's an extra burden related
1319 * to the ability to keep xmlSAX2References in attributes
1320 */
1321 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1322 ctxt->node, fullname, val);
1323 if (nvalnorm != NULL) {
1324 xmlFree(val);
1325 val = nvalnorm;
1326 }
1327
1328 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1329 ctxt->myDoc, ctxt->node, ret, val);
1330 xmlFree(val);
1331 }
1332 } else {
1333 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1334 ctxt->node, ret, value);
1335 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001336 } else
1337#endif /* LIBXML_VALID_ENABLED */
1338 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001339 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1340 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1341 /*
1342 * when validating, the ID registration is done at the attribute
1343 * validation level. Otherwise we have to do specific handling here.
1344 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001345 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001346 /*
1347 * Add the xml:id value
1348 *
1349 * Open issue: normalization of the value.
1350 */
Daniel Veillard68cb4b22004-04-18 20:55:39 +00001351 if (xmlValidateNCName(value, 1) != 0) {
1352 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1353 "xml:id : attribute value %s is not an NCName\n",
1354 (const char *) value, NULL);
1355 }
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00001356 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00001357 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1358 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1359 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1360 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001361 }
1362
1363error:
1364 if (nval != NULL)
1365 xmlFree(nval);
1366 if (ns != NULL)
1367 xmlFree(ns);
1368}
1369
Daniel Veillard1af9a412003-08-20 22:54:39 +00001370/*
1371 * xmlCheckDefaultedAttributes:
1372 *
1373 * Check defaulted attributes from the DTD
1374 */
1375static void
1376xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1377 const xmlChar *prefix, const xmlChar **atts) {
1378 xmlElementPtr elemDecl;
1379 const xmlChar *att;
1380 int internal = 1;
1381 int i;
1382
1383 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1384 if (elemDecl == NULL) {
1385 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1386 internal = 0;
1387 }
1388
1389process_external_subset:
1390
1391 if (elemDecl != NULL) {
1392 xmlAttributePtr attr = elemDecl->attributes;
1393 /*
1394 * Check against defaulted attributes from the external subset
1395 * if the document is stamped as standalone
1396 */
1397 if ((ctxt->myDoc->standalone == 1) &&
1398 (ctxt->myDoc->extSubset != NULL) &&
1399 (ctxt->validate)) {
1400 while (attr != NULL) {
1401 if ((attr->defaultValue != NULL) &&
1402 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1403 attr->elem, attr->name,
1404 attr->prefix) == attr) &&
1405 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1406 attr->elem, attr->name,
1407 attr->prefix) == NULL)) {
1408 xmlChar *fulln;
1409
1410 if (attr->prefix != NULL) {
1411 fulln = xmlStrdup(attr->prefix);
1412 fulln = xmlStrcat(fulln, BAD_CAST ":");
1413 fulln = xmlStrcat(fulln, attr->name);
1414 } else {
1415 fulln = xmlStrdup(attr->name);
1416 }
1417
1418 /*
1419 * Check that the attribute is not declared in the
1420 * serialization
1421 */
1422 att = NULL;
1423 if (atts != NULL) {
1424 i = 0;
1425 att = atts[i];
1426 while (att != NULL) {
1427 if (xmlStrEqual(att, fulln))
1428 break;
1429 i += 2;
1430 att = atts[i];
1431 }
1432 }
1433 if (att == NULL) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001434 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
Daniel Veillard1af9a412003-08-20 22:54:39 +00001435 "standalone: attribute %s on %s defaulted from external subset\n",
Daniel Veillard427174f2003-12-10 10:42:59 +00001436 (const char *)fulln,
1437 (const char *)attr->elem);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001438 }
1439 }
1440 attr = attr->nexth;
1441 }
1442 }
1443
1444 /*
1445 * Actually insert defaulted values when needed
1446 */
1447 attr = elemDecl->attributes;
1448 while (attr != NULL) {
1449 /*
1450 * Make sure that attributes redefinition occuring in the
1451 * internal subset are not overriden by definitions in the
1452 * external subset.
1453 */
1454 if (attr->defaultValue != NULL) {
1455 /*
1456 * the element should be instantiated in the tree if:
1457 * - this is a namespace prefix
1458 * - the user required for completion in the tree
1459 * like XSLT
1460 * - there isn't already an attribute definition
1461 * in the internal subset overriding it.
1462 */
1463 if (((attr->prefix != NULL) &&
1464 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1465 ((attr->prefix == NULL) &&
1466 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1467 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1468 xmlAttributePtr tst;
1469
1470 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1471 attr->elem, attr->name,
1472 attr->prefix);
1473 if ((tst == attr) || (tst == NULL)) {
1474 xmlChar fn[50];
1475 xmlChar *fulln;
1476
1477 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1478 if (fulln == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001479 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001480 return;
1481 }
1482
1483 /*
1484 * Check that the attribute is not declared in the
1485 * serialization
1486 */
1487 att = NULL;
1488 if (atts != NULL) {
1489 i = 0;
1490 att = atts[i];
1491 while (att != NULL) {
1492 if (xmlStrEqual(att, fulln))
1493 break;
1494 i += 2;
1495 att = atts[i];
1496 }
1497 }
1498 if (att == NULL) {
1499 xmlSAX2AttributeInternal(ctxt, fulln,
1500 attr->defaultValue, prefix);
1501 }
1502 if ((fulln != fn) && (fulln != attr->name))
1503 xmlFree(fulln);
1504 }
1505 }
1506 }
1507 attr = attr->nexth;
1508 }
1509 if (internal == 1) {
1510 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1511 name, prefix);
1512 internal = 0;
1513 goto process_external_subset;
1514 }
1515 }
1516}
1517
1518/**
1519 * xmlSAX2StartElement:
1520 * @ctx: the user data (XML parser context)
1521 * @fullname: The element name, including namespace prefix
1522 * @atts: An array of name/value attributes pairs, NULL terminated
1523 *
1524 * called when an opening tag has been processed.
1525 */
1526void
1527xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1528{
1529 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1530 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001531 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001532 xmlNsPtr ns;
1533 xmlChar *name;
1534 xmlChar *prefix;
1535 const xmlChar *att;
1536 const xmlChar *value;
1537 int i;
1538
Daniel Veillarda521d282004-11-09 14:59:59 +00001539 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001540 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001541#ifdef DEBUG_SAX
1542 xmlGenericError(xmlGenericErrorContext,
1543 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1544#endif
1545
1546 /*
1547 * First check on validity:
1548 */
1549 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1550 ((ctxt->myDoc->intSubset == NULL) ||
1551 ((ctxt->myDoc->intSubset->notations == NULL) &&
1552 (ctxt->myDoc->intSubset->elements == NULL) &&
1553 (ctxt->myDoc->intSubset->attributes == NULL) &&
1554 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00001555 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1556 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001557 ctxt->validate = 0;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001558 }
1559
1560
1561 /*
1562 * Split the full name into a namespace prefix and the tag name
1563 */
1564 name = xmlSplitQName(ctxt, fullname, &prefix);
1565
1566
1567 /*
1568 * Note : the namespace resolution is deferred until the end of the
1569 * attributes parsing, since local namespace can be defined as
1570 * an attribute at this level.
1571 */
1572 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1573 if (ret == NULL) {
1574 if (prefix != NULL)
1575 xmlFree(prefix);
William M. Brack42331a92004-07-29 07:07:16 +00001576 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
Daniel Veillard1af9a412003-08-20 22:54:39 +00001577 return;
1578 }
1579 if (ctxt->myDoc->children == NULL) {
1580#ifdef DEBUG_SAX_TREE
1581 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1582#endif
1583 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1584 } else if (parent == NULL) {
1585 parent = ctxt->myDoc->children;
1586 }
1587 ctxt->nodemem = -1;
1588 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001589 if (ctxt->input != NULL) {
1590 if (ctxt->input->line < 65535)
1591 ret->line = (short) ctxt->input->line;
1592 else
1593 ret->line = 65535;
1594 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001595 }
1596
1597 /*
1598 * We are parsing a new node.
1599 */
1600#ifdef DEBUG_SAX_TREE
1601 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1602#endif
1603 nodePush(ctxt, ret);
1604
1605 /*
1606 * Link the child element
1607 */
1608 if (parent != NULL) {
1609 if (parent->type == XML_ELEMENT_NODE) {
1610#ifdef DEBUG_SAX_TREE
1611 xmlGenericError(xmlGenericErrorContext,
1612 "adding child %s to %s\n", name, parent->name);
1613#endif
1614 xmlAddChild(parent, ret);
1615 } else {
1616#ifdef DEBUG_SAX_TREE
1617 xmlGenericError(xmlGenericErrorContext,
1618 "adding sibling %s to ", name);
1619 xmlDebugDumpOneNode(stderr, parent, 0);
1620#endif
1621 xmlAddSibling(parent, ret);
1622 }
1623 }
1624
1625 /*
1626 * Insert all the defaulted attributes from the DTD especially namespaces
1627 */
1628 if ((!ctxt->html) &&
1629 ((ctxt->myDoc->intSubset != NULL) ||
1630 (ctxt->myDoc->extSubset != NULL))) {
1631 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1632 }
1633
1634 /*
1635 * process all the attributes whose name start with "xmlns"
1636 */
1637 if (atts != NULL) {
1638 i = 0;
1639 att = atts[i++];
1640 value = atts[i++];
1641 if (!ctxt->html) {
1642 while ((att != NULL) && (value != NULL)) {
1643 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1644 (att[3] == 'n') && (att[4] == 's'))
1645 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1646
1647 att = atts[i++];
1648 value = atts[i++];
1649 }
1650 }
1651 }
1652
1653 /*
1654 * Search the namespace, note that since the attributes have been
1655 * processed, the local namespaces are available.
1656 */
1657 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1658 if ((ns == NULL) && (parent != NULL))
1659 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1660 if ((prefix != NULL) && (ns == NULL)) {
1661 ns = xmlNewNs(ret, NULL, prefix);
Daniel Veillard77aad342006-07-13 06:21:09 +00001662 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1663 "Namespace prefix %s is not defined\n",
1664 prefix, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00001665 }
1666
1667 /*
1668 * set the namespace node, making sure that if the default namspace
1669 * is unbound on a parent we simply kee it NULL
1670 */
1671 if ((ns != NULL) && (ns->href != NULL) &&
1672 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1673 xmlSetNs(ret, ns);
1674
1675 /*
1676 * process all the other attributes
1677 */
1678 if (atts != NULL) {
1679 i = 0;
1680 att = atts[i++];
1681 value = atts[i++];
1682 if (ctxt->html) {
1683 while (att != NULL) {
1684 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1685 att = atts[i++];
1686 value = atts[i++];
1687 }
1688 } else {
1689 while ((att != NULL) && (value != NULL)) {
1690 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1691 (att[3] != 'n') || (att[4] != 's'))
1692 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1693
1694 /*
1695 * Next ones
1696 */
1697 att = atts[i++];
1698 value = atts[i++];
1699 }
1700 }
1701 }
1702
Daniel Veillard4432df22003-09-28 18:58:27 +00001703#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001704 /*
1705 * If it's the Document root, finish the DTD validation and
1706 * check the document root element for validity
1707 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00001708 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00001709 int chk;
1710
1711 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1712 if (chk <= 0)
1713 ctxt->valid = 0;
1714 if (chk < 0)
1715 ctxt->wellFormed = 0;
1716 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00001717 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001718 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001719#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001720
1721 if (prefix != NULL)
1722 xmlFree(prefix);
1723
1724}
1725
1726/**
1727 * xmlSAX2EndElement:
1728 * @ctx: the user data (XML parser context)
1729 * @name: The element name
1730 *
1731 * called when the end of an element has been detected.
1732 */
1733void
1734xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1735{
1736 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1737 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001738 xmlNodePtr cur;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001739
Daniel Veillard34099b42004-11-04 17:34:35 +00001740 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001741 cur = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00001742#ifdef DEBUG_SAX
1743 if (name == NULL)
1744 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1745 else
1746 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1747#endif
1748
1749 /* Capture end position and add node */
1750 if (cur != NULL && ctxt->record_info) {
1751 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1752 node_info.end_line = ctxt->input->line;
1753 node_info.node = cur;
1754 xmlParserAddNodeInfo(ctxt, &node_info);
1755 }
1756 ctxt->nodemem = -1;
1757
Daniel Veillard4432df22003-09-28 18:58:27 +00001758#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001759 if (ctxt->validate && ctxt->wellFormed &&
1760 ctxt->myDoc && ctxt->myDoc->intSubset)
1761 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1762 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001763#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001764
1765
1766 /*
1767 * end of parsing of this node.
1768 */
1769#ifdef DEBUG_SAX_TREE
1770 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1771#endif
1772 nodePop(ctxt);
1773}
Daniel Veillard81273902003-09-30 00:43:48 +00001774#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001775
Daniel Veillarde57ec792003-09-10 10:50:59 +00001776/*
Daniel Veillard19895052003-09-17 13:59:32 +00001777 * xmlSAX2TextNode:
1778 * @ctxt: the parser context
1779 * @str: the input string
1780 * @len: the string length
1781 *
1782 * Remove the entities from an attribute value
1783 *
1784 * Returns the newly allocated string or NULL if not needed or error
1785 */
1786static xmlNodePtr
1787xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1788 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001789 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001790
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001791 /*
1792 * Allocate
1793 */
Daniel Veillard19895052003-09-17 13:59:32 +00001794 if (ctxt->freeElems != NULL) {
1795 ret = ctxt->freeElems;
1796 ctxt->freeElems = ret->next;
1797 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001798 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001799 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001800 }
1801 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001802 xmlErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard19895052003-09-17 13:59:32 +00001803 return(NULL);
1804 }
Daniel Veillard8874b942005-08-25 13:19:21 +00001805 memset(ret, 0, sizeof(xmlNode));
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001806 /*
1807 * intern the formatting blanks found between tags, or the
1808 * very short strings
1809 */
1810 if (ctxt->dictNames) {
1811 xmlChar cur = str[len];
1812
Daniel Veillard8874b942005-08-25 13:19:21 +00001813 if ((len < (int) (2 * sizeof(void *))) &&
1814 (ctxt->options & XML_PARSE_COMPACT)) {
1815 /* store the string in the node overrithing properties and nsDef */
1816 xmlChar *tmp = (xmlChar *) &(ret->properties);
1817 memcpy(tmp, str, len);
1818 tmp[len] = 0;
1819 intern = tmp;
1820 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001821 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001822 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001823 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001824 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001825 int i;
1826
1827 for (i = 1;i < len;i++) {
Daniel Veillard1a9b7082004-01-02 10:42:01 +00001828 if (!IS_BLANK_CH(str[i])) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001829 }
1830 intern = xmlDictLookup(ctxt->dict, str, len);
1831 }
1832 }
1833skip:
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001834 ret->type = XML_TEXT_NODE;
1835
1836 ret->name = xmlStringText;
William M. Brack9f797ab2004-07-28 07:40:12 +00001837 if (intern == NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001838 ret->content = xmlStrndup(str, len);
William M. Brack9f797ab2004-07-28 07:40:12 +00001839 if (ret->content == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001840 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1841 xmlFree(ret);
William M. Brack9f797ab2004-07-28 07:40:12 +00001842 return(NULL);
1843 }
1844 } else
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001845 ret->content = (xmlChar *) intern;
1846
Daniel Veillard45efd082008-07-07 13:52:52 +00001847 if (ctxt->input != NULL)
1848 ret->line = ctxt->input->line;
1849
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001850 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1851 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001852 return(ret);
1853}
1854
Daniel Veillard4432df22003-09-28 18:58:27 +00001855#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001856/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001857 * xmlSAX2DecodeAttrEntities:
1858 * @ctxt: the parser context
1859 * @str: the input string
1860 * @len: the string length
1861 *
1862 * Remove the entities from an attribute value
1863 *
1864 * Returns the newly allocated string or NULL if not needed or error
1865 */
1866static xmlChar *
1867xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1868 const xmlChar *end) {
1869 const xmlChar *in;
1870 xmlChar *ret;
1871
1872 in = str;
1873 while (in < end)
1874 if (*in++ == '&')
1875 goto decode;
1876 return(NULL);
1877decode:
1878 ctxt->depth++;
1879 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1880 XML_SUBSTITUTE_REF, 0,0,0);
1881 ctxt->depth--;
1882 return(ret);
1883}
Daniel Veillard4432df22003-09-28 18:58:27 +00001884#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001885
1886/**
1887 * xmlSAX2AttributeNs:
1888 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001889 * @localname: the local name of the attribute
1890 * @prefix: the attribute namespace prefix if available
1891 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001892 * @value: Start of the attribute value
1893 * @valueend: end of the attribute value
1894 *
1895 * Handle an attribute that has been read by the parser.
1896 * The default handling is to convert the attribute into an
1897 * DOM subtree and past it in a new xmlAttr element added to
1898 * the element.
1899 */
1900static void
1901xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1902 const xmlChar * localname,
1903 const xmlChar * prefix,
1904 const xmlChar * value,
1905 const xmlChar * valueend)
1906{
1907 xmlAttrPtr ret;
1908 xmlNsPtr namespace = NULL;
1909 xmlChar *dup = NULL;
1910
Daniel Veillarde57ec792003-09-10 10:50:59 +00001911 /*
1912 * Note: if prefix == NULL, the attribute is not in the default namespace
1913 */
1914 if (prefix != NULL)
1915 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1916
Daniel Veillard8a44e592003-09-15 14:50:06 +00001917 /*
1918 * allocate the node
1919 */
1920 if (ctxt->freeAttrs != NULL) {
1921 ret = ctxt->freeAttrs;
1922 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001923 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001924 memset(ret, 0, sizeof(xmlAttr));
1925 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001926
Daniel Veillard8a44e592003-09-15 14:50:06 +00001927 ret->parent = ctxt->node;
1928 ret->doc = ctxt->myDoc;
1929 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001930
Daniel Veillard8a44e592003-09-15 14:50:06 +00001931 if (ctxt->dictNames)
1932 ret->name = localname;
1933 else
1934 ret->name = xmlStrdup(localname);
1935
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001936 /* link at the end to preserv order, TODO speed up with a last */
1937 if (ctxt->node->properties == NULL) {
1938 ctxt->node->properties = ret;
1939 } else {
1940 xmlAttrPtr prev = ctxt->node->properties;
1941
1942 while (prev->next != NULL) prev = prev->next;
1943 prev->next = ret;
1944 ret->prev = prev;
1945 }
1946
Daniel Veillard8a44e592003-09-15 14:50:06 +00001947 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1948 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1949 } else {
1950 if (ctxt->dictNames)
1951 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1952 (xmlChar *) localname, NULL);
1953 else
1954 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1955 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00001956 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00001957 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001958 }
1959 }
1960
Daniel Veillard8a44e592003-09-15 14:50:06 +00001961 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1962 xmlNodePtr tmp;
1963
Daniel Veillard19895052003-09-17 13:59:32 +00001964 /*
1965 * We know that if there is an entity reference, then
1966 * the string has been dup'ed and terminates with 0
1967 * otherwise with ' or "
1968 */
1969 if (*valueend != 0) {
1970 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1971 ret->children = tmp;
1972 ret->last = tmp;
1973 if (tmp != NULL) {
1974 tmp->doc = ret->doc;
1975 tmp->parent = (xmlNodePtr) ret;
1976 }
1977 } else {
1978 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1979 valueend - value);
1980 tmp = ret->children;
1981 while (tmp != NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001982 tmp->doc = ret->doc;
Daniel Veillard19895052003-09-17 13:59:32 +00001983 tmp->parent = (xmlNodePtr) ret;
1984 if (tmp->next == NULL)
1985 ret->last = tmp;
1986 tmp = tmp->next;
1987 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001988 }
1989 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00001990 xmlNodePtr tmp;
1991
1992 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1993 ret->children = tmp;
1994 ret->last = tmp;
1995 if (tmp != NULL) {
1996 tmp->doc = ret->doc;
1997 tmp->parent = (xmlNodePtr) ret;
1998 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001999 }
2000
Daniel Veillard4432df22003-09-28 18:58:27 +00002001#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002002 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2003 ctxt->myDoc && ctxt->myDoc->intSubset) {
2004 /*
2005 * If we don't substitute entities, the validation should be
2006 * done on a value with replaced entities anyway.
2007 */
2008 if (!ctxt->replaceEntities) {
2009 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2010 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00002011 if (*valueend == 0) {
2012 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2013 ctxt->myDoc, ctxt->node, ret, value);
2014 } else {
2015 /*
2016 * That should already be normalized.
2017 * cheaper to finally allocate here than duplicate
2018 * entry points in the full validation code
2019 */
2020 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002021
Daniel Veillard62998c02003-09-15 12:56:36 +00002022 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2023 ctxt->myDoc, ctxt->node, ret, dup);
2024 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002025 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00002026 /*
2027 * dup now contains a string of the flattened attribute
2028 * content with entities substitued. Check if we need to
2029 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002030 * It need to be done twice ... it's an extra burden related
2031 * to the ability to keep references in attributes
2032 */
Daniel Veillard62998c02003-09-15 12:56:36 +00002033 if (ctxt->attsSpecial != NULL) {
2034 xmlChar *nvalnorm;
2035 xmlChar fn[50];
2036 xmlChar *fullname;
2037
2038 fullname = xmlBuildQName(localname, prefix, fn, 50);
2039 if (fullname != NULL) {
2040 ctxt->vctxt.valid = 1;
2041 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2042 &ctxt->vctxt, ctxt->myDoc,
2043 ctxt->node, fullname, dup);
2044 if (ctxt->vctxt.valid != 1)
2045 ctxt->valid = 0;
2046
2047 if ((fullname != fn) && (fullname != localname))
2048 xmlFree(fullname);
2049 if (nvalnorm != NULL) {
2050 xmlFree(dup);
2051 dup = nvalnorm;
2052 }
2053 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002054 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002055
2056 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2057 ctxt->myDoc, ctxt->node, ret, dup);
2058 }
2059 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002060 /*
2061 * if entities already have been substitued, then
2062 * the attribute as passed is already normalized
2063 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002064 dup = xmlStrndup(value, valueend - value);
2065
2066 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2067 ctxt->myDoc, ctxt->node, ret, dup);
2068 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002069 } else
2070#endif /* LIBXML_VALID_ENABLED */
2071 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00002072 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2073 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2074 /*
2075 * when validating, the ID registration is done at the attribute
2076 * validation level. Otherwise we have to do specific handling here.
2077 */
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002078 if ((prefix == ctxt->str_xml) &&
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002079 (localname[0] == 'i') && (localname[1] == 'd') &&
2080 (localname[2] == 0)) {
2081 /*
2082 * Add the xml:id value
2083 *
2084 * Open issue: normalization of the value.
2085 */
2086 if (dup == NULL)
2087 dup = xmlStrndup(value, valueend - value);
William M. Brack5ef2f812004-05-23 23:56:47 +00002088#ifdef LIBXML_VALID_ENABLED
Daniel Veillard68cb4b22004-04-18 20:55:39 +00002089 if (xmlValidateNCName(dup, 1) != 0) {
2090 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2091 "xml:id : attribute value %s is not an NCName\n",
2092 (const char *) dup, NULL);
2093 }
William M. Brack3f147372004-05-22 01:09:26 +00002094#endif
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002095 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard54f9a4f2005-09-03 13:28:24 +00002096 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2097 /* might be worth duplicate entry points and not copy */
2098 if (dup == NULL)
2099 dup = xmlStrndup(value, valueend - value);
2100 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2101 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2102 if (dup == NULL)
2103 dup = xmlStrndup(value, valueend - value);
2104 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
Daniel Veillard67f8b1c2004-04-09 21:51:49 +00002105 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002106 }
2107 if (dup != NULL)
2108 xmlFree(dup);
2109}
2110
2111/**
2112 * xmlSAX2StartElementNs:
2113 * @ctx: the user data (XML parser context)
2114 * @localname: the local name of the element
2115 * @prefix: the element namespace prefix if available
2116 * @URI: the element namespace name if available
2117 * @nb_namespaces: number of namespace definitions on that node
2118 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2119 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00002120 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002121 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2122 * attribute values.
2123 *
2124 * SAX2 callback when an element start has been detected by the parser.
2125 * It provides the namespace informations for the element, as well as
2126 * the new namespace declarations on the element.
2127 */
2128void
2129xmlSAX2StartElementNs(void *ctx,
2130 const xmlChar *localname,
2131 const xmlChar *prefix,
2132 const xmlChar *URI,
2133 int nb_namespaces,
2134 const xmlChar **namespaces,
2135 int nb_attributes,
2136 int nb_defaulted,
2137 const xmlChar **attributes)
2138{
2139 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2140 xmlNodePtr ret;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002141 xmlNodePtr parent;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002142 xmlNsPtr last = NULL, ns;
2143 const xmlChar *uri, *pref;
2144 int i, j;
2145
Daniel Veillard34099b42004-11-04 17:34:35 +00002146 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002147 parent = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002148 /*
2149 * First check on validity:
2150 */
2151 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2152 ((ctxt->myDoc->intSubset == NULL) ||
2153 ((ctxt->myDoc->intSubset->notations == NULL) &&
2154 (ctxt->myDoc->intSubset->elements == NULL) &&
2155 (ctxt->myDoc->intSubset->attributes == NULL) &&
2156 (ctxt->myDoc->intSubset->entities == NULL)))) {
Daniel Veillardf88d8cf2003-12-08 10:25:02 +00002157 xmlErrValid(ctxt, XML_ERR_NO_DTD,
2158 "Validation failed: no DTD found !", NULL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002159 ctxt->validate = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002160 }
2161
Daniel Veillard8a44e592003-09-15 14:50:06 +00002162 /*
2163 * allocate the node
2164 */
2165 if (ctxt->freeElems != NULL) {
2166 ret = ctxt->freeElems;
2167 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00002168 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00002169 memset(ret, 0, sizeof(xmlNode));
2170 ret->type = XML_ELEMENT_NODE;
2171
2172 if (ctxt->dictNames)
2173 ret->name = localname;
William M. Brack9f797ab2004-07-28 07:40:12 +00002174 else {
Daniel Veillard8a44e592003-09-15 14:50:06 +00002175 ret->name = xmlStrdup(localname);
William M. Brack9f797ab2004-07-28 07:40:12 +00002176 if (ret->name == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002177 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002178 return;
2179 }
2180 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002181 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2182 xmlRegisterNodeDefaultValue(ret);
2183 } else {
2184 if (ctxt->dictNames)
2185 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2186 (xmlChar *) localname, NULL);
2187 else
2188 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2189 if (ret == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002190 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillard8a44e592003-09-15 14:50:06 +00002191 return;
2192 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002193 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002194 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00002195 if (ctxt->input != NULL) {
2196 if (ctxt->input->line < 65535)
2197 ret->line = (short) ctxt->input->line;
2198 else
2199 ret->line = 65535;
2200 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002201 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00002202
Daniel Veillard29b17482004-08-16 00:39:03 +00002203 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002204 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002205 }
2206 /*
2207 * Build the namespace list
2208 */
2209 for (i = 0,j = 0;j < nb_namespaces;j++) {
2210 pref = namespaces[i++];
2211 uri = namespaces[i++];
2212 ns = xmlNewNs(NULL, uri, pref);
2213 if (ns != NULL) {
2214 if (last == NULL) {
2215 ret->nsDef = last = ns;
2216 } else {
2217 last->next = ns;
2218 last = ns;
2219 }
2220 if ((URI != NULL) && (prefix == pref))
2221 ret->ns = ns;
2222 } else {
William M. Brack42331a92004-07-29 07:07:16 +00002223 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
Daniel Veillarde57ec792003-09-10 10:50:59 +00002224 return;
2225 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002226#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002227 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2228 ctxt->myDoc && ctxt->myDoc->intSubset) {
2229 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2230 ret, prefix, ns, uri);
2231 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002232#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002233 }
2234 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002235
2236 /*
2237 * We are parsing a new node.
2238 */
2239 nodePush(ctxt, ret);
2240
2241 /*
2242 * Link the child element
2243 */
2244 if (parent != NULL) {
2245 if (parent->type == XML_ELEMENT_NODE) {
2246 xmlAddChild(parent, ret);
2247 } else {
2248 xmlAddSibling(parent, ret);
2249 }
2250 }
2251
2252 /*
2253 * Insert the defaulted attributes from the DTD only if requested:
2254 */
2255 if ((nb_defaulted != 0) &&
2256 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2257 nb_attributes -= nb_defaulted;
2258
2259 /*
2260 * Search the namespace if it wasn't already found
William M. Brackbf5cf212004-08-31 06:47:17 +00002261 * Note that, if prefix is NULL, this searches for the default Ns
Daniel Veillarde57ec792003-09-10 10:50:59 +00002262 */
2263 if ((URI != NULL) && (ret->ns == NULL)) {
William M. Brackbf5cf212004-08-31 06:47:17 +00002264 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002265 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2266 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2267 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002268 if (ret->ns == NULL) {
2269 ns = xmlNewNs(ret, NULL, prefix);
William M. Brack9f797ab2004-07-28 07:40:12 +00002270 if (ns == NULL) {
Daniel Veillard6977c6c2006-01-04 14:03:10 +00002271
William M. Brack42331a92004-07-29 07:07:16 +00002272 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
William M. Brack9f797ab2004-07-28 07:40:12 +00002273 return;
2274 }
Daniel Veillard77aad342006-07-13 06:21:09 +00002275 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2276 "Namespace prefix %s was not found\n",
2277 prefix, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002278 }
2279 }
2280
2281 /*
2282 * process all the other attributes
2283 */
2284 if (nb_attributes > 0) {
2285 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2286 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2287 attributes[j+3], attributes[j+4]);
2288 }
2289 }
2290
Daniel Veillard4432df22003-09-28 18:58:27 +00002291#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002292 /*
2293 * If it's the Document root, finish the DTD validation and
2294 * check the document root element for validity
2295 */
Daniel Veillardeff45a92004-10-29 12:10:55 +00002296 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002297 int chk;
2298
2299 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2300 if (chk <= 0)
2301 ctxt->valid = 0;
2302 if (chk < 0)
2303 ctxt->wellFormed = 0;
2304 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillardeff45a92004-10-29 12:10:55 +00002305 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002306 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002307#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002308}
2309
2310/**
2311 * xmlSAX2EndElementNs:
2312 * @ctx: the user data (XML parser context)
2313 * @localname: the local name of the element
2314 * @prefix: the element namespace prefix if available
2315 * @URI: the element namespace name if available
2316 *
2317 * SAX2 callback when an element end has been detected by the parser.
2318 * It provides the namespace informations for the element.
2319 */
2320void
2321xmlSAX2EndElementNs(void *ctx,
2322 const xmlChar * localname ATTRIBUTE_UNUSED,
2323 const xmlChar * prefix ATTRIBUTE_UNUSED,
2324 const xmlChar * URI ATTRIBUTE_UNUSED)
2325{
2326 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2327 xmlParserNodeInfo node_info;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002328 xmlNodePtr cur;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002329
Daniel Veillard34099b42004-11-04 17:34:35 +00002330 if (ctx == NULL) return;
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002331 cur = ctxt->node;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002332 /* Capture end position and add node */
2333 if ((ctxt->record_info) && (cur != NULL)) {
2334 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2335 node_info.end_line = ctxt->input->line;
2336 node_info.node = cur;
2337 xmlParserAddNodeInfo(ctxt, &node_info);
2338 }
2339 ctxt->nodemem = -1;
2340
Daniel Veillard4432df22003-09-28 18:58:27 +00002341#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002342 if (ctxt->validate && ctxt->wellFormed &&
2343 ctxt->myDoc && ctxt->myDoc->intSubset)
2344 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002345#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002346
2347 /*
2348 * end of parsing of this node.
2349 */
2350 nodePop(ctxt);
2351}
2352
Daniel Veillard1af9a412003-08-20 22:54:39 +00002353/**
2354 * xmlSAX2Reference:
2355 * @ctx: the user data (XML parser context)
2356 * @name: The entity name
2357 *
2358 * called when an entity xmlSAX2Reference is detected.
2359 */
2360void
2361xmlSAX2Reference(void *ctx, const xmlChar *name)
2362{
2363 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2364 xmlNodePtr ret;
2365
Daniel Veillard34099b42004-11-04 17:34:35 +00002366 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002367#ifdef DEBUG_SAX
2368 xmlGenericError(xmlGenericErrorContext,
2369 "SAX.xmlSAX2Reference(%s)\n", name);
2370#endif
2371 if (name[0] == '#')
2372 ret = xmlNewCharRef(ctxt->myDoc, name);
2373 else
2374 ret = xmlNewReference(ctxt->myDoc, name);
2375#ifdef DEBUG_SAX_TREE
2376 xmlGenericError(xmlGenericErrorContext,
2377 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2378#endif
Daniel Veillardb242b082008-02-08 09:56:31 +00002379 if (xmlAddChild(ctxt->node, ret) == NULL) {
2380 xmlFreeNode(ret);
2381 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002382}
2383
2384/**
2385 * xmlSAX2Characters:
2386 * @ctx: the user data (XML parser context)
2387 * @ch: a xmlChar string
2388 * @len: the number of xmlChar
2389 *
2390 * receiving some chars from the parser.
2391 */
2392void
2393xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2394{
2395 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2396 xmlNodePtr lastChild;
2397
Daniel Veillard34099b42004-11-04 17:34:35 +00002398 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002399#ifdef DEBUG_SAX
2400 xmlGenericError(xmlGenericErrorContext,
2401 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2402#endif
2403 /*
2404 * Handle the data if any. If there is no child
2405 * add it as content, otherwise if the last child is text,
2406 * concatenate it, else create a new node of type text.
2407 */
2408
2409 if (ctxt->node == NULL) {
2410#ifdef DEBUG_SAX_TREE
2411 xmlGenericError(xmlGenericErrorContext,
2412 "add chars: ctxt->node == NULL !\n");
2413#endif
2414 return;
2415 }
Daniel Veillard19895052003-09-17 13:59:32 +00002416 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002417#ifdef DEBUG_SAX_TREE
2418 xmlGenericError(xmlGenericErrorContext,
2419 "add chars to %s \n", ctxt->node->name);
2420#endif
2421
2422 /*
2423 * Here we needed an accelerator mechanism in case of very large
2424 * elements. Use an attribute in the structure !!!
2425 */
2426 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002427 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2428 if (lastChild != NULL) {
2429 ctxt->node->children = lastChild;
2430 ctxt->node->last = lastChild;
2431 lastChild->parent = ctxt->node;
2432 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002433 ctxt->nodelen = len;
2434 ctxt->nodemem = len + 1;
William M. Bracka3215c72004-07-31 16:24:01 +00002435 } else {
2436 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2437 return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002438 }
2439 } else {
2440 int coalesceText = (lastChild != NULL) &&
2441 (lastChild->type == XML_TEXT_NODE) &&
2442 (lastChild->name == xmlStringText);
2443 if ((coalesceText) && (ctxt->nodemem != 0)) {
2444 /*
2445 * The whole point of maintaining nodelen and nodemem,
2446 * xmlTextConcat is too costly, i.e. compute length,
2447 * reallocate a new buffer, move data, append ch. Here
2448 * We try to minimaze realloc() uses and avoid copying
2449 * and recomputing length over and over.
2450 */
Daniel Veillard8874b942005-08-25 13:19:21 +00002451 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2452 lastChild->content = xmlStrdup(lastChild->content);
2453 lastChild->properties = NULL;
2454 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2455 (xmlDictOwns(ctxt->dict, lastChild->content))) {
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002456 lastChild->content = xmlStrdup(lastChild->content);
2457 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002458 if (ctxt->nodelen + len >= ctxt->nodemem) {
2459 xmlChar *newbuf;
2460 int size;
2461
2462 size = ctxt->nodemem + len;
2463 size *= 2;
2464 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2465 if (newbuf == NULL) {
William M. Brack42331a92004-07-29 07:07:16 +00002466 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002467 return;
2468 }
2469 ctxt->nodemem = size;
2470 lastChild->content = newbuf;
2471 }
2472 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2473 ctxt->nodelen += len;
2474 lastChild->content[ctxt->nodelen] = 0;
2475 } else if (coalesceText) {
2476 if (xmlTextConcat(lastChild, ch, len)) {
William M. Brack42331a92004-07-29 07:07:16 +00002477 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
Daniel Veillard1af9a412003-08-20 22:54:39 +00002478 }
2479 if (ctxt->node->children != NULL) {
2480 ctxt->nodelen = xmlStrlen(lastChild->content);
2481 ctxt->nodemem = ctxt->nodelen + 1;
2482 }
2483 } else {
2484 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002485 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2486 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002487 xmlAddChild(ctxt->node, lastChild);
2488 if (ctxt->node->children != NULL) {
2489 ctxt->nodelen = len;
2490 ctxt->nodemem = len + 1;
2491 }
2492 }
2493 }
2494 }
2495}
2496
2497/**
2498 * xmlSAX2IgnorableWhitespace:
2499 * @ctx: the user data (XML parser context)
2500 * @ch: a xmlChar string
2501 * @len: the number of xmlChar
2502 *
2503 * receiving some ignorable whitespaces from the parser.
2504 * UNUSED: by default the DOM building will use xmlSAX2Characters
2505 */
2506void
2507xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2508{
2509 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2510#ifdef DEBUG_SAX
2511 xmlGenericError(xmlGenericErrorContext,
2512 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2513#endif
2514}
2515
2516/**
2517 * xmlSAX2ProcessingInstruction:
2518 * @ctx: the user data (XML parser context)
2519 * @target: the target name
2520 * @data: the PI data's
2521 *
2522 * A processing instruction has been parsed.
2523 */
2524void
2525xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2526 const xmlChar *data)
2527{
2528 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2529 xmlNodePtr ret;
Daniel Veillard6128c012004-11-08 17:16:15 +00002530 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002531
Daniel Veillard34099b42004-11-04 17:34:35 +00002532 if (ctx == NULL) return;
Daniel Veillard6128c012004-11-08 17:16:15 +00002533 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002534#ifdef DEBUG_SAX
2535 xmlGenericError(xmlGenericErrorContext,
2536 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2537#endif
2538
Daniel Veillard03a53c32004-10-26 16:06:51 +00002539 ret = xmlNewDocPI(ctxt->myDoc, target, data);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002540 if (ret == NULL) return;
2541 parent = ctxt->node;
2542
Daniel Veillard73da77e2005-08-24 14:05:37 +00002543 if (ctxt->linenumbers) {
2544 if (ctxt->input != NULL) {
2545 if (ctxt->input->line < 65535)
2546 ret->line = (short) ctxt->input->line;
2547 else
2548 ret->line = 65535;
2549 }
2550 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002551 if (ctxt->inSubset == 1) {
2552 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2553 return;
2554 } else if (ctxt->inSubset == 2) {
2555 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2556 return;
2557 }
2558 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2559#ifdef DEBUG_SAX_TREE
2560 xmlGenericError(xmlGenericErrorContext,
2561 "Setting PI %s as root\n", target);
2562#endif
2563 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2564 return;
2565 }
2566 if (parent->type == XML_ELEMENT_NODE) {
2567#ifdef DEBUG_SAX_TREE
2568 xmlGenericError(xmlGenericErrorContext,
2569 "adding PI %s child to %s\n", target, parent->name);
2570#endif
2571 xmlAddChild(parent, ret);
2572 } else {
2573#ifdef DEBUG_SAX_TREE
2574 xmlGenericError(xmlGenericErrorContext,
2575 "adding PI %s sibling to ", target);
2576 xmlDebugDumpOneNode(stderr, parent, 0);
2577#endif
2578 xmlAddSibling(parent, ret);
2579 }
2580}
2581
2582/**
2583 * xmlSAX2Comment:
2584 * @ctx: the user data (XML parser context)
2585 * @value: the xmlSAX2Comment content
2586 *
2587 * A xmlSAX2Comment has been parsed.
2588 */
2589void
2590xmlSAX2Comment(void *ctx, const xmlChar *value)
2591{
2592 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2593 xmlNodePtr ret;
Daniel Veillard34099b42004-11-04 17:34:35 +00002594 xmlNodePtr parent;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002595
Daniel Veillard34099b42004-11-04 17:34:35 +00002596 if (ctx == NULL) return;
2597 parent = ctxt->node;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002598#ifdef DEBUG_SAX
2599 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2600#endif
2601 ret = xmlNewDocComment(ctxt->myDoc, value);
2602 if (ret == NULL) return;
Daniel Veillard73da77e2005-08-24 14:05:37 +00002603 if (ctxt->linenumbers) {
2604 if (ctxt->input != NULL) {
2605 if (ctxt->input->line < 65535)
2606 ret->line = (short) ctxt->input->line;
2607 else
2608 ret->line = 65535;
2609 }
2610 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002611
2612 if (ctxt->inSubset == 1) {
2613 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2614 return;
2615 } else if (ctxt->inSubset == 2) {
2616 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2617 return;
2618 }
2619 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2620#ifdef DEBUG_SAX_TREE
2621 xmlGenericError(xmlGenericErrorContext,
2622 "Setting xmlSAX2Comment as root\n");
2623#endif
2624 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2625 return;
2626 }
2627 if (parent->type == XML_ELEMENT_NODE) {
2628#ifdef DEBUG_SAX_TREE
2629 xmlGenericError(xmlGenericErrorContext,
2630 "adding xmlSAX2Comment child to %s\n", parent->name);
2631#endif
2632 xmlAddChild(parent, ret);
2633 } else {
2634#ifdef DEBUG_SAX_TREE
2635 xmlGenericError(xmlGenericErrorContext,
2636 "adding xmlSAX2Comment sibling to ");
2637 xmlDebugDumpOneNode(stderr, parent, 0);
2638#endif
2639 xmlAddSibling(parent, ret);
2640 }
2641}
2642
2643/**
2644 * xmlSAX2CDataBlock:
2645 * @ctx: the user data (XML parser context)
2646 * @value: The pcdata content
2647 * @len: the block length
2648 *
2649 * called when a pcdata block has been parsed
2650 */
2651void
2652xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2653{
2654 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2655 xmlNodePtr ret, lastChild;
2656
Daniel Veillard34099b42004-11-04 17:34:35 +00002657 if (ctx == NULL) return;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002658#ifdef DEBUG_SAX
2659 xmlGenericError(xmlGenericErrorContext,
2660 "SAX.pcdata(%.10s, %d)\n", value, len);
2661#endif
2662 lastChild = xmlGetLastChild(ctxt->node);
2663#ifdef DEBUG_SAX_TREE
2664 xmlGenericError(xmlGenericErrorContext,
2665 "add chars to %s \n", ctxt->node->name);
2666#endif
2667 if ((lastChild != NULL) &&
2668 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2669 xmlTextConcat(lastChild, value, len);
2670 } else {
2671 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2672 xmlAddChild(ctxt->node, ret);
2673 }
2674}
2675
Daniel Veillard62998c02003-09-15 12:56:36 +00002676static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002677
Daniel Veillard81273902003-09-30 00:43:48 +00002678#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002679/**
2680 * xmlSAXDefaultVersion:
2681 * @version: the version, 1 or 2
2682 *
2683 * Set the default version of SAX used globally by the library.
William M. Brack96d2eff2004-06-30 11:48:47 +00002684 * By default, during initialization the default is set to 2.
2685 * Note that it is generally a better coding style to use
2686 * xmlSAXVersion() to set up the version explicitly for a given
2687 * parsing context.
Daniel Veillarde57ec792003-09-10 10:50:59 +00002688 *
2689 * Returns the previous value in case of success and -1 in case of error.
2690 */
2691int
2692xmlSAXDefaultVersion(int version)
2693{
2694 int ret = xmlSAX2DefaultVersionValue;
2695
2696 if ((version != 1) && (version != 2))
2697 return(-1);
2698 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002699 return(ret);
2700}
Daniel Veillard81273902003-09-30 00:43:48 +00002701#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002702
2703/**
2704 * xmlSAXVersion:
2705 * @hdlr: the SAX handler
2706 * @version: the version, 1 or 2
2707 *
2708 * Initialize the default XML SAX handler according to the version
2709 *
2710 * Returns 0 in case of success and -1 in case of error.
2711 */
2712int
2713xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2714{
2715 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002716 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002717 hdlr->startElement = NULL;
2718 hdlr->endElement = NULL;
2719 hdlr->startElementNs = xmlSAX2StartElementNs;
2720 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002721 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002722 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002723#ifdef LIBXML_SAX1_ENABLED
2724 } else if (version == 1) {
2725 hdlr->startElement = xmlSAX2StartElement;
2726 hdlr->endElement = xmlSAX2EndElement;
2727 hdlr->initialized = 1;
2728#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002729 } else
2730 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002731 hdlr->internalSubset = xmlSAX2InternalSubset;
2732 hdlr->externalSubset = xmlSAX2ExternalSubset;
2733 hdlr->isStandalone = xmlSAX2IsStandalone;
2734 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2735 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2736 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2737 hdlr->getEntity = xmlSAX2GetEntity;
2738 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2739 hdlr->entityDecl = xmlSAX2EntityDecl;
2740 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2741 hdlr->elementDecl = xmlSAX2ElementDecl;
2742 hdlr->notationDecl = xmlSAX2NotationDecl;
2743 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2744 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2745 hdlr->startDocument = xmlSAX2StartDocument;
2746 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002747 hdlr->reference = xmlSAX2Reference;
2748 hdlr->characters = xmlSAX2Characters;
2749 hdlr->cdataBlock = xmlSAX2CDataBlock;
2750 hdlr->ignorableWhitespace = xmlSAX2Characters;
2751 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2752 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002753 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002754 hdlr->error = xmlParserError;
2755 hdlr->fatalError = xmlParserError;
2756
Daniel Veillarde57ec792003-09-10 10:50:59 +00002757 return(0);
2758}
2759
2760/**
2761 * xmlSAX2InitDefaultSAXHandler:
2762 * @hdlr: the SAX handler
2763 * @warning: flag if non-zero sets the handler warning procedure
2764 *
2765 * Initialize the default XML SAX2 handler
2766 */
2767void
2768xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2769{
2770 if ((hdlr == NULL) || (hdlr->initialized != 0))
2771 return;
2772
2773 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2774 if (warning == 0)
2775 hdlr->warning = NULL;
2776 else
2777 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002778}
2779
2780/**
2781 * xmlDefaultSAXHandlerInit:
2782 *
2783 * Initialize the default SAX2 handler
2784 */
2785void
2786xmlDefaultSAXHandlerInit(void)
2787{
Daniel Veillard81273902003-09-30 00:43:48 +00002788#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002789 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002790#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002791}
2792
2793#ifdef LIBXML_HTML_ENABLED
2794
2795/**
2796 * xmlSAX2InitHtmlDefaultSAXHandler:
2797 * @hdlr: the SAX handler
2798 *
2799 * Initialize the default HTML SAX2 handler
2800 */
2801void
2802xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2803{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002804 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002805 return;
2806
2807 hdlr->internalSubset = xmlSAX2InternalSubset;
2808 hdlr->externalSubset = NULL;
2809 hdlr->isStandalone = NULL;
2810 hdlr->hasInternalSubset = NULL;
2811 hdlr->hasExternalSubset = NULL;
2812 hdlr->resolveEntity = NULL;
2813 hdlr->getEntity = xmlSAX2GetEntity;
2814 hdlr->getParameterEntity = NULL;
2815 hdlr->entityDecl = NULL;
2816 hdlr->attributeDecl = NULL;
2817 hdlr->elementDecl = NULL;
2818 hdlr->notationDecl = NULL;
2819 hdlr->unparsedEntityDecl = NULL;
2820 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2821 hdlr->startDocument = xmlSAX2StartDocument;
2822 hdlr->endDocument = xmlSAX2EndDocument;
2823 hdlr->startElement = xmlSAX2StartElement;
2824 hdlr->endElement = xmlSAX2EndElement;
2825 hdlr->reference = NULL;
2826 hdlr->characters = xmlSAX2Characters;
2827 hdlr->cdataBlock = xmlSAX2CDataBlock;
2828 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardfc484dd2004-10-22 14:34:23 +00002829 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002830 hdlr->comment = xmlSAX2Comment;
2831 hdlr->warning = xmlParserWarning;
2832 hdlr->error = xmlParserError;
2833 hdlr->fatalError = xmlParserError;
2834
Daniel Veillard092643b2003-09-25 14:29:29 +00002835 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002836}
2837
2838/**
2839 * htmlDefaultSAXHandlerInit:
2840 *
2841 * Initialize the default SAX handler
2842 */
2843void
2844htmlDefaultSAXHandlerInit(void)
2845{
Daniel Veillard092643b2003-09-25 14:29:29 +00002846 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002847}
2848
2849#endif /* LIBXML_HTML_ENABLED */
2850
2851#ifdef LIBXML_DOCB_ENABLED
2852
2853/**
2854 * xmlSAX2InitDocbDefaultSAXHandler:
2855 * @hdlr: the SAX handler
2856 *
2857 * Initialize the default DocBook SAX2 handler
2858 */
2859void
2860xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2861{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002862 if ((hdlr == NULL) || (hdlr->initialized != 0))
Daniel Veillard1af9a412003-08-20 22:54:39 +00002863 return;
2864
2865 hdlr->internalSubset = xmlSAX2InternalSubset;
2866 hdlr->externalSubset = NULL;
2867 hdlr->isStandalone = xmlSAX2IsStandalone;
2868 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2869 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2870 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2871 hdlr->getEntity = xmlSAX2GetEntity;
2872 hdlr->getParameterEntity = NULL;
2873 hdlr->entityDecl = xmlSAX2EntityDecl;
2874 hdlr->attributeDecl = NULL;
2875 hdlr->elementDecl = NULL;
2876 hdlr->notationDecl = NULL;
2877 hdlr->unparsedEntityDecl = NULL;
2878 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2879 hdlr->startDocument = xmlSAX2StartDocument;
2880 hdlr->endDocument = xmlSAX2EndDocument;
2881 hdlr->startElement = xmlSAX2StartElement;
2882 hdlr->endElement = xmlSAX2EndElement;
2883 hdlr->reference = xmlSAX2Reference;
2884 hdlr->characters = xmlSAX2Characters;
2885 hdlr->cdataBlock = NULL;
2886 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2887 hdlr->processingInstruction = NULL;
2888 hdlr->comment = xmlSAX2Comment;
2889 hdlr->warning = xmlParserWarning;
2890 hdlr->error = xmlParserError;
2891 hdlr->fatalError = xmlParserError;
2892
Daniel Veillardffbbed42003-10-10 14:46:54 +00002893 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002894}
2895
2896/**
2897 * docbDefaultSAXHandlerInit:
2898 *
2899 * Initialize the default SAX handler
2900 */
2901void
2902docbDefaultSAXHandlerInit(void)
2903{
Daniel Veillard092643b2003-09-25 14:29:29 +00002904 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002905}
2906
2907#endif /* LIBXML_DOCB_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002908#define bottom_SAX2
2909#include "elfgcchack.h"