blob: 0625b8a901cf7ceae66e5efa8a87d19669483779 [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
47/**
Daniel Veillard1af9a412003-08-20 22:54:39 +000048 * xmlSAX2GetPublicId:
49 * @ctx: the user data (XML parser context)
50 *
51 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
52 *
53 * Returns a xmlChar *
54 */
55const xmlChar *
56xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
57{
58 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
59 return(NULL);
60}
61
62/**
63 * xmlSAX2GetSystemId:
64 * @ctx: the user data (XML parser context)
65 *
66 * Provides the system ID, basically URL or filename e.g.
67 * http://www.sgmlsource.com/dtds/memo.dtd
68 *
69 * Returns a xmlChar *
70 */
71const xmlChar *
72xmlSAX2GetSystemId(void *ctx)
73{
74 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
75 return((const xmlChar *) ctxt->input->filename);
76}
77
78/**
79 * xmlSAX2GetLineNumber:
80 * @ctx: the user data (XML parser context)
81 *
82 * Provide the line number of the current parsing point.
83 *
84 * Returns an int
85 */
86int
87xmlSAX2GetLineNumber(void *ctx)
88{
89 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
90 return(ctxt->input->line);
91}
92
93/**
94 * xmlSAX2GetColumnNumber:
95 * @ctx: the user data (XML parser context)
96 *
97 * Provide the column number of the current parsing point.
98 *
99 * Returns an int
100 */
101int
102xmlSAX2GetColumnNumber(void *ctx)
103{
104 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
105 return(ctxt->input->col);
106}
107
108/**
109 * xmlSAX2IsStandalone:
110 * @ctx: the user data (XML parser context)
111 *
112 * Is this document tagged standalone ?
113 *
114 * Returns 1 if true
115 */
116int
117xmlSAX2IsStandalone(void *ctx)
118{
119 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
120 return(ctxt->myDoc->standalone == 1);
121}
122
123/**
124 * xmlSAX2HasInternalSubset:
125 * @ctx: the user data (XML parser context)
126 *
127 * Does this document has an internal subset
128 *
129 * Returns 1 if true
130 */
131int
132xmlSAX2HasInternalSubset(void *ctx)
133{
134 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
135 return(ctxt->myDoc->intSubset != NULL);
136}
137
138/**
139 * xmlSAX2HasExternalSubset:
140 * @ctx: the user data (XML parser context)
141 *
142 * Does this document has an external subset
143 *
144 * Returns 1 if true
145 */
146int
147xmlSAX2HasExternalSubset(void *ctx)
148{
149 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
150 return(ctxt->myDoc->extSubset != NULL);
151}
152
153/**
154 * xmlSAX2InternalSubset:
155 * @ctx: the user data (XML parser context)
156 * @name: the root element name
157 * @ExternalID: the external ID
158 * @SystemID: the SYSTEM ID (e.g. filename or URL)
159 *
160 * Callback on internal subset declaration.
161 */
162void
163xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
164 const xmlChar *ExternalID, const xmlChar *SystemID)
165{
166 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
167 xmlDtdPtr dtd;
168#ifdef DEBUG_SAX
169 xmlGenericError(xmlGenericErrorContext,
170 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
171 name, ExternalID, SystemID);
172#endif
173
174 if (ctxt->myDoc == NULL)
175 return;
176 dtd = xmlGetIntSubset(ctxt->myDoc);
177 if (dtd != NULL) {
178 if (ctxt->html)
179 return;
180 xmlUnlinkNode((xmlNodePtr) dtd);
181 xmlFreeDtd(dtd);
182 ctxt->myDoc->intSubset = NULL;
183 }
184 ctxt->myDoc->intSubset =
185 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
186}
187
188/**
189 * xmlSAX2ExternalSubset:
190 * @ctx: the user data (XML parser context)
191 * @name: the root element name
192 * @ExternalID: the external ID
193 * @SystemID: the SYSTEM ID (e.g. filename or URL)
194 *
195 * Callback on external subset declaration.
196 */
197void
198xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
199 const xmlChar *ExternalID, const xmlChar *SystemID)
200{
201 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
202#ifdef DEBUG_SAX
203 xmlGenericError(xmlGenericErrorContext,
204 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
205 name, ExternalID, SystemID);
206#endif
207 if (((ExternalID != NULL) || (SystemID != NULL)) &&
208 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
209 (ctxt->wellFormed && ctxt->myDoc))) {
210 /*
211 * Try to fetch and parse the external subset.
212 */
213 xmlParserInputPtr oldinput;
214 int oldinputNr;
215 int oldinputMax;
216 xmlParserInputPtr *oldinputTab;
217 xmlParserInputPtr input = NULL;
218 xmlCharEncoding enc;
219 int oldcharset;
220
221 /*
222 * Ask the Entity resolver to load the damn thing
223 */
224 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
225 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
226 SystemID);
227 if (input == NULL) {
228 return;
229 }
230
231 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
232
233 /*
234 * make sure we won't destroy the main document context
235 */
236 oldinput = ctxt->input;
237 oldinputNr = ctxt->inputNr;
238 oldinputMax = ctxt->inputMax;
239 oldinputTab = ctxt->inputTab;
240 oldcharset = ctxt->charset;
241
242 ctxt->inputTab = (xmlParserInputPtr *)
243 xmlMalloc(5 * sizeof(xmlParserInputPtr));
244 if (ctxt->inputTab == NULL) {
245 ctxt->errNo = XML_ERR_NO_MEMORY;
246 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
247 ctxt->sax->error(ctxt->userData,
248 "xmlSAX2ExternalSubset: out of memory\n");
249 ctxt->errNo = XML_ERR_NO_MEMORY;
250 ctxt->instate = XML_PARSER_EOF;
251 ctxt->disableSAX = 1;
252 ctxt->input = oldinput;
253 ctxt->inputNr = oldinputNr;
254 ctxt->inputMax = oldinputMax;
255 ctxt->inputTab = oldinputTab;
256 ctxt->charset = oldcharset;
257 return;
258 }
259 ctxt->inputNr = 0;
260 ctxt->inputMax = 5;
261 ctxt->input = NULL;
262 xmlPushInput(ctxt, input);
263
264 /*
265 * On the fly encoding conversion if needed
266 */
267 if (ctxt->input->length >= 4) {
268 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
269 xmlSwitchEncoding(ctxt, enc);
270 }
271
272 if (input->filename == NULL)
273 input->filename = (char *) xmlCanonicPath(SystemID);
274 input->line = 1;
275 input->col = 1;
276 input->base = ctxt->input->cur;
277 input->cur = ctxt->input->cur;
278 input->free = NULL;
279
280 /*
281 * let's parse that entity knowing it's an external subset.
282 */
283 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
284
285 /*
286 * Free up the external entities
287 */
288
289 while (ctxt->inputNr > 1)
290 xmlPopInput(ctxt);
291 xmlFreeInputStream(ctxt->input);
292 xmlFree(ctxt->inputTab);
293
294 /*
295 * Restore the parsing context of the main entity
296 */
297 ctxt->input = oldinput;
298 ctxt->inputNr = oldinputNr;
299 ctxt->inputMax = oldinputMax;
300 ctxt->inputTab = oldinputTab;
301 ctxt->charset = oldcharset;
302 /* ctxt->wellFormed = oldwellFormed; */
303 }
304}
305
306/**
307 * xmlSAX2ResolveEntity:
308 * @ctx: the user data (XML parser context)
309 * @publicId: The public ID of the entity
310 * @systemId: The system ID of the entity
311 *
312 * The entity loader, to control the loading of external entities,
313 * the application can either:
314 * - override this xmlSAX2ResolveEntity() callback in the SAX block
315 * - or better use the xmlSetExternalEntityLoader() function to
316 * set up it's own entity resolution routine
317 *
318 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
319 */
320xmlParserInputPtr
321xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
322{
323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
324 xmlParserInputPtr ret;
325 xmlChar *URI;
326 const char *base = NULL;
327
328 if (ctxt->input != NULL)
329 base = ctxt->input->filename;
330 if (base == NULL)
331 base = ctxt->directory;
332
333 URI = xmlBuildURI(systemId, (const xmlChar *) base);
334
335#ifdef DEBUG_SAX
336 xmlGenericError(xmlGenericErrorContext,
337 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
338#endif
339
340 ret = xmlLoadExternalEntity((const char *) URI,
341 (const char *) publicId, ctxt);
342 if (URI != NULL)
343 xmlFree(URI);
344 return(ret);
345}
346
347/**
348 * xmlSAX2GetEntity:
349 * @ctx: the user data (XML parser context)
350 * @name: The entity name
351 *
352 * Get an entity by name
353 *
354 * Returns the xmlEntityPtr if found.
355 */
356xmlEntityPtr
357xmlSAX2GetEntity(void *ctx, const xmlChar *name)
358{
359 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
360 xmlEntityPtr ret = NULL;
361
362#ifdef DEBUG_SAX
363 xmlGenericError(xmlGenericErrorContext,
364 "SAX.xmlSAX2GetEntity(%s)\n", name);
365#endif
366
367 if (ctxt->inSubset == 0) {
368 ret = xmlGetPredefinedEntity(name);
369 if (ret != NULL)
370 return(ret);
371 }
372 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
373 if (ctxt->inSubset == 2) {
374 ctxt->myDoc->standalone = 0;
375 ret = xmlGetDocEntity(ctxt->myDoc, name);
376 ctxt->myDoc->standalone = 1;
377 } else {
378 ret = xmlGetDocEntity(ctxt->myDoc, name);
379 if (ret == NULL) {
380 ctxt->myDoc->standalone = 0;
381 ret = xmlGetDocEntity(ctxt->myDoc, name);
382 if (ret != NULL) {
383 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000384 ctxt->sax->error(ctxt->userData,
385 "Entity(%s) document marked standalone but requires external subset\n",
Daniel Veillard1af9a412003-08-20 22:54:39 +0000386 name);
387 ctxt->valid = 0;
388 ctxt->wellFormed = 0;
389 }
390 ctxt->myDoc->standalone = 1;
391 }
392 }
393 } else {
394 ret = xmlGetDocEntity(ctxt->myDoc, name);
395 }
396 if ((ret != NULL) &&
397 ((ctxt->validate) || (ctxt->replaceEntities)) &&
398 (ret->children == NULL) &&
399 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
400 int val;
401
402 /*
403 * for validation purposes we really need to fetch and
404 * parse the external entity
405 */
406 xmlNodePtr children;
407
408 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
409 ret->ExternalID, &children);
410 if (val == 0) {
411 xmlAddChildList((xmlNodePtr) ret, children);
412 } else {
William M. Brack4811ba32003-09-06 18:02:53 +0000413 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
414 ctxt->sax->error(ctxt->userData,
415 "Failure to process entity %s\n", name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000416 ctxt->wellFormed = 0;
417 ctxt->valid = 0;
418 ctxt->validate = 0;
419 return(NULL);
420 }
421 ret->owner = 1;
422 }
423 return(ret);
424}
425
426/**
427 * xmlSAX2GetParameterEntity:
428 * @ctx: the user data (XML parser context)
429 * @name: The entity name
430 *
431 * Get a parameter entity by name
432 *
433 * Returns the xmlEntityPtr if found.
434 */
435xmlEntityPtr
436xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
437{
438 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
439 xmlEntityPtr ret;
440
441#ifdef DEBUG_SAX
442 xmlGenericError(xmlGenericErrorContext,
443 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
444#endif
445
446 ret = xmlGetParameterEntity(ctxt->myDoc, name);
447 return(ret);
448}
449
450
451/**
452 * xmlSAX2EntityDecl:
453 * @ctx: the user data (XML parser context)
454 * @name: the entity name
455 * @type: the entity type
456 * @publicId: The public ID of the entity
457 * @systemId: The system ID of the entity
458 * @content: the entity value (without processing).
459 *
460 * An entity definition has been parsed
461 */
462void
463xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
464 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
465{
466 xmlEntityPtr ent;
467 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
468
469#ifdef DEBUG_SAX
470 xmlGenericError(xmlGenericErrorContext,
471 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
472 name, type, publicId, systemId, content);
473#endif
474 if (ctxt->inSubset == 1) {
475 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
476 systemId, content);
477 if ((ent == NULL) && (ctxt->pedantic) &&
478 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000479 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000480 "Entity(%s) already defined in the internal subset\n", name);
481 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
482 xmlChar *URI;
483 const char *base = NULL;
484
485 if (ctxt->input != NULL)
486 base = ctxt->input->filename;
487 if (base == NULL)
488 base = ctxt->directory;
489
490 URI = xmlBuildURI(systemId, (const xmlChar *) base);
491 ent->URI = URI;
492 }
493 } else if (ctxt->inSubset == 2) {
494 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
495 systemId, content);
496 if ((ent == NULL) && (ctxt->pedantic) &&
497 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000498 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000499 "Entity(%s) already defined in the external subset\n", name);
500 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
501 xmlChar *URI;
502 const char *base = NULL;
503
504 if (ctxt->input != NULL)
505 base = ctxt->input->filename;
506 if (base == NULL)
507 base = ctxt->directory;
508
509 URI = xmlBuildURI(systemId, (const xmlChar *) base);
510 ent->URI = URI;
511 }
512 } else {
513 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000514 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000515 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n", name);
516 }
517}
518
519/**
520 * xmlSAX2AttributeDecl:
521 * @ctx: the user data (XML parser context)
522 * @elem: the name of the element
523 * @fullname: the attribute name
524 * @type: the attribute type
525 * @def: the type of default value
526 * @defaultValue: the attribute default value
527 * @tree: the tree of enumerated value set
528 *
529 * An attribute definition has been parsed
530 */
531void
532xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
533 int type, int def, const xmlChar *defaultValue,
534 xmlEnumerationPtr tree)
535{
536 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
537 xmlAttributePtr attr;
538 xmlChar *name = NULL, *prefix = NULL;
539
540#ifdef DEBUG_SAX
541 xmlGenericError(xmlGenericErrorContext,
542 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
543 elem, fullname, type, def, defaultValue);
544#endif
Daniel Veillarde57ec792003-09-10 10:50:59 +0000545 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000546 name = xmlSplitQName(ctxt, fullname, &prefix);
547 ctxt->vctxt.valid = 1;
548 if (ctxt->inSubset == 1)
549 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
550 name, prefix, (xmlAttributeType) type,
551 (xmlAttributeDefault) def, defaultValue, tree);
552 else if (ctxt->inSubset == 2)
553 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
554 name, prefix, (xmlAttributeType) type,
555 (xmlAttributeDefault) def, defaultValue, tree);
556 else {
557 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000558 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000559 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n", name);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000560 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000561 return;
562 }
563 if (ctxt->vctxt.valid == 0)
564 ctxt->valid = 0;
565 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
566 (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
567 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
568 attr);
569 if (prefix != NULL)
570 xmlFree(prefix);
571 if (name != NULL)
572 xmlFree(name);
573}
574
575/**
576 * xmlSAX2ElementDecl:
577 * @ctx: the user data (XML parser context)
578 * @name: the element name
579 * @type: the element type
580 * @content: the element value tree
581 *
582 * An element definition has been parsed
583 */
584void
585xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
586 xmlElementContentPtr content)
587{
588 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
589 xmlElementPtr elem = NULL;
590
591#ifdef DEBUG_SAX
592 xmlGenericError(xmlGenericErrorContext,
593 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
594#endif
595
596 if (ctxt->inSubset == 1)
597 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
598 name, (xmlElementTypeVal) type, content);
599 else if (ctxt->inSubset == 2)
600 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
601 name, (xmlElementTypeVal) type, content);
602 else {
603 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000604 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000605 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
606 name);
607 return;
608 }
609 if (elem == NULL)
610 ctxt->valid = 0;
611 if (ctxt->validate && ctxt->wellFormed &&
612 ctxt->myDoc && ctxt->myDoc->intSubset)
613 ctxt->valid &=
614 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
615}
616
617/**
618 * xmlSAX2NotationDecl:
619 * @ctx: the user data (XML parser context)
620 * @name: The name of the notation
621 * @publicId: The public ID of the entity
622 * @systemId: The system ID of the entity
623 *
624 * What to do when a notation declaration has been parsed.
625 */
626void
627xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
628 const xmlChar *publicId, const xmlChar *systemId)
629{
630 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
631 xmlNotationPtr nota = NULL;
632
633#ifdef DEBUG_SAX
634 xmlGenericError(xmlGenericErrorContext,
635 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
636#endif
637
638 if ((publicId == NULL) && (systemId == NULL)) {
639 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000640 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000641 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n", name);
642 ctxt->valid = 0;
643 ctxt->wellFormed = 0;
644 return;
645 } else if (ctxt->inSubset == 1)
646 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
647 publicId, systemId);
648 else if (ctxt->inSubset == 2)
649 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
650 publicId, systemId);
651 else {
652 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000653 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000654 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n", name);
655 return;
656 }
657 if (nota == NULL) ctxt->valid = 0;
658 if (ctxt->validate && ctxt->wellFormed &&
659 ctxt->myDoc && ctxt->myDoc->intSubset)
660 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
661 nota);
662}
663
664/**
665 * xmlSAX2UnparsedEntityDecl:
666 * @ctx: the user data (XML parser context)
667 * @name: The name of the entity
668 * @publicId: The public ID of the entity
669 * @systemId: The system ID of the entity
670 * @notationName: the name of the notation
671 *
672 * What to do when an unparsed entity declaration is parsed
673 */
674void
675xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
676 const xmlChar *publicId, const xmlChar *systemId,
677 const xmlChar *notationName)
678{
679 xmlEntityPtr ent;
680 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
681#ifdef DEBUG_SAX
682 xmlGenericError(xmlGenericErrorContext,
683 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
684 name, publicId, systemId, notationName);
685#endif
686 if (ctxt->inSubset == 1) {
687 ent = xmlAddDocEntity(ctxt->myDoc, name,
688 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
689 publicId, systemId, notationName);
690 if ((ent == NULL) && (ctxt->pedantic) &&
691 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000692 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000693 "Entity(%s) already defined in the internal subset\n", name);
694 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
695 xmlChar *URI;
696 const char *base = NULL;
697
698 if (ctxt->input != NULL)
699 base = ctxt->input->filename;
700 if (base == NULL)
701 base = ctxt->directory;
702
703 URI = xmlBuildURI(systemId, (const xmlChar *) base);
704 ent->URI = URI;
705 }
706 } else if (ctxt->inSubset == 2) {
707 ent = xmlAddDtdEntity(ctxt->myDoc, name,
708 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
709 publicId, systemId, notationName);
710 if ((ent == NULL) && (ctxt->pedantic) &&
711 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000712 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000713 "Entity(%s) already defined in the external subset\n", name);
714 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
715 xmlChar *URI;
716 const char *base = NULL;
717
718 if (ctxt->input != NULL)
719 base = ctxt->input->filename;
720 if (base == NULL)
721 base = ctxt->directory;
722
723 URI = xmlBuildURI(systemId, (const xmlChar *) base);
724 ent->URI = URI;
725 }
726 } else {
727 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000728 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000729 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n", name);
730 }
731}
732
733/**
734 * xmlSAX2SetDocumentLocator:
735 * @ctx: the user data (XML parser context)
736 * @loc: A SAX Locator
737 *
738 * Receive the document locator at startup, actually xmlDefaultSAXLocator
739 * Everything is available on the context, so this is useless in our case.
740 */
741void
742xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
743{
744 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
745#ifdef DEBUG_SAX
746 xmlGenericError(xmlGenericErrorContext,
747 "SAX.xmlSAX2SetDocumentLocator()\n");
748#endif
749}
750
751/**
752 * xmlSAX2StartDocument:
753 * @ctx: the user data (XML parser context)
754 *
755 * called when the document start being processed.
756 */
757void
758xmlSAX2StartDocument(void *ctx)
759{
760 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
761 xmlDocPtr doc;
762
763#ifdef DEBUG_SAX
764 xmlGenericError(xmlGenericErrorContext,
765 "SAX.xmlSAX2StartDocument()\n");
766#endif
767 if (ctxt->html) {
768#ifdef LIBXML_HTML_ENABLED
769 if (ctxt->myDoc == NULL)
770 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
771 if (ctxt->myDoc == NULL) {
772 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
773 ctxt->sax->error(ctxt->userData,
774 "SAX.xmlSAX2StartDocument(): out of memory\n");
775 ctxt->errNo = XML_ERR_NO_MEMORY;
776 ctxt->instate = XML_PARSER_EOF;
777 ctxt->disableSAX = 1;
778 return;
779 }
780#else
781 xmlGenericError(xmlGenericErrorContext,
782 "libxml2 built without HTML support\n");
783 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
784 ctxt->instate = XML_PARSER_EOF;
785 ctxt->disableSAX = 1;
786 return;
787#endif
788 } else {
789 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
790 if (doc != NULL) {
791 if (ctxt->encoding != NULL)
792 doc->encoding = xmlStrdup(ctxt->encoding);
793 else
794 doc->encoding = NULL;
795 doc->standalone = ctxt->standalone;
796 } else {
797 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
798 ctxt->sax->error(ctxt->userData,
799 "SAX.xmlSAX2StartDocument(): out of memory\n");
800 ctxt->errNo = XML_ERR_NO_MEMORY;
801 ctxt->instate = XML_PARSER_EOF;
802 ctxt->disableSAX = 1;
803 return;
804 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000805 if ((ctxt->dictNames) && (doc != NULL))
806 doc->dict = ctxt->dict;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000807 }
808 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
809 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
810 ctxt->myDoc->URL = xmlCanonicPath((const xmlChar *) ctxt->input->filename);
811 if (ctxt->myDoc->URL == NULL)
812 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
813 }
814}
815
816/**
817 * xmlSAX2EndDocument:
818 * @ctx: the user data (XML parser context)
819 *
820 * called when the document end has been detected.
821 */
822void
823xmlSAX2EndDocument(void *ctx)
824{
825 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
826#ifdef DEBUG_SAX
827 xmlGenericError(xmlGenericErrorContext,
828 "SAX.xmlSAX2EndDocument()\n");
829#endif
830 if (ctxt->validate && ctxt->wellFormed &&
831 ctxt->myDoc && ctxt->myDoc->intSubset)
832 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
833
834 /*
835 * Grab the encoding if it was added on-the-fly
836 */
837 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
838 (ctxt->myDoc->encoding == NULL)) {
839 ctxt->myDoc->encoding = ctxt->encoding;
840 ctxt->encoding = NULL;
841 }
842 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
843 (ctxt->myDoc->encoding == NULL)) {
844 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
845 }
846 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
847 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
848 ctxt->myDoc->charset = ctxt->charset;
849 }
850}
851
852/**
853 * xmlSAX2AttributeInternal:
854 * @ctx: the user data (XML parser context)
855 * @fullname: The attribute name, including namespace prefix
856 * @value: The attribute value
857 * @prefix: the prefix on the element node
858 *
859 * Handle an attribute that has been read by the parser.
860 * The default handling is to convert the attribute into an
861 * DOM subtree and past it in a new xmlAttr element added to
862 * the element.
863 */
864static void
865xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
866 const xmlChar *value, const xmlChar *prefix)
867{
868 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
869 xmlAttrPtr ret;
870 xmlChar *name;
871 xmlChar *ns;
872 xmlChar *nval;
873 xmlNsPtr namespace;
874
875 /*
876 * Split the full name into a namespace prefix and the tag name
877 */
878 name = xmlSplitQName(ctxt, fullname, &ns);
879 if ((name != NULL) && (name[0] == 0)) {
880 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
881 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
882 ctxt->sax->error(ctxt->userData,
883 "invalid namespace declaration '%s'\n", fullname);
884 } else {
885 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
886 ctxt->sax->warning(ctxt->userData,
887 "Avoid attribute ending with ':' like '%s'\n", fullname);
888 }
889 if (ns != NULL)
890 xmlFree(ns);
891 ns = NULL;
892 xmlFree(name);
893 name = xmlStrdup(fullname);
894 }
895 if (name == NULL) {
896 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
897 ctxt->sax->error(ctxt->userData,
898 "SAX.xmlSAX2StartElement(): out of memory\n");
899 ctxt->errNo = XML_ERR_NO_MEMORY;
900 ctxt->instate = XML_PARSER_EOF;
901 ctxt->disableSAX = 1;
902 if (ns != NULL)
903 xmlFree(ns);
904 return;
905 }
906
907 /*
908 * Do the last stage of the attribute normalization
909 * Needed for HTML too:
910 * http://www.w3.org/TR/html4/types.html#h-6.2
911 */
912 ctxt->vctxt.valid = 1;
913 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
914 ctxt->myDoc, ctxt->node,
915 fullname, value);
916 if (ctxt->vctxt.valid != 1) {
917 ctxt->valid = 0;
918 }
919 if (nval != NULL)
920 value = nval;
921
922 /*
923 * Check whether it's a namespace definition
924 */
925 if ((!ctxt->html) && (ns == NULL) &&
926 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
927 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
928 xmlNsPtr nsret;
929 xmlChar *val;
930
931 if (!ctxt->replaceEntities) {
932 ctxt->depth++;
933 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
934 0,0,0);
935 ctxt->depth--;
936 } else {
937 val = (xmlChar *) value;
938 }
939
940 if (val[0] != 0) {
941 xmlURIPtr uri;
942
943 uri = xmlParseURI((const char *)val);
944 if (uri == NULL) {
945 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
946 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +0000947 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000948 } else {
949 if (uri->scheme == NULL) {
950 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
951 ctxt->sax->warning(ctxt->userData,
952 "xmlns: URI %s is not absolute\n", val);
953 }
954 xmlFreeURI(uri);
955 }
956 }
957
958 /* a default namespace definition */
959 nsret = xmlNewNs(ctxt->node, val, NULL);
960
961 /*
962 * Validate also for namespace decls, they are attributes from
963 * an XML-1.0 perspective
964 */
965 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
966 ctxt->myDoc && ctxt->myDoc->intSubset)
967 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
968 ctxt->node, prefix, nsret, val);
969 if (name != NULL)
970 xmlFree(name);
971 if (nval != NULL)
972 xmlFree(nval);
973 if (val != value)
974 xmlFree(val);
975 return;
976 }
977 if ((!ctxt->html) &&
978 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
979 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
980 xmlNsPtr nsret;
981 xmlChar *val;
982
983 if (!ctxt->replaceEntities) {
984 ctxt->depth++;
985 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
986 0,0,0);
987 ctxt->depth--;
988 if (val == NULL) {
989 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
990 ctxt->sax->error(ctxt->userData,
991 "SAX.xmlSAX2StartElement(): out of memory\n");
992 ctxt->errNo = XML_ERR_NO_MEMORY;
993 ctxt->instate = XML_PARSER_EOF;
994 ctxt->disableSAX = 1;
995 xmlFree(ns);
996 if (name != NULL)
997 xmlFree(name);
998 return;
999 }
1000 } else {
1001 val = (xmlChar *) value;
1002 }
1003
1004 if (val[0] == 0) {
1005 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1006 ctxt->sax->error(ctxt->userData,
1007 "Empty namespace name for prefix %s\n", name);
1008 }
1009 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1010 xmlURIPtr uri;
1011
1012 uri = xmlParseURI((const char *)val);
1013 if (uri == NULL) {
1014 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1015 ctxt->sax->warning(ctxt->userData,
1016 "xmlns:%s: %s not a valid URI\n", name, value);
1017 } else {
1018 if (uri->scheme == NULL) {
1019 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1020 ctxt->sax->warning(ctxt->userData,
1021 "xmlns:%s: URI %s is not absolute\n", name, value);
1022 }
1023 xmlFreeURI(uri);
1024 }
1025 }
1026
1027 /* a standard namespace definition */
1028 nsret = xmlNewNs(ctxt->node, val, name);
1029 xmlFree(ns);
1030 /*
1031 * Validate also for namespace decls, they are attributes from
1032 * an XML-1.0 perspective
1033 */
1034 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1035 ctxt->myDoc && ctxt->myDoc->intSubset)
1036 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1037 ctxt->node, prefix, nsret, value);
1038 if (name != NULL)
1039 xmlFree(name);
1040 if (nval != NULL)
1041 xmlFree(nval);
1042 if (val != value)
1043 xmlFree(val);
1044 return;
1045 }
1046
1047 if (ns != NULL) {
1048 xmlAttrPtr prop;
1049 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillard67906942003-08-28 21:13:25 +00001050 if (namespace == NULL) {
William M. Brack4811ba32003-09-06 18:02:53 +00001051 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1052 ctxt->sax->error(ctxt->userData,
1053 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001054 ns, name);
1055 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001056
1057 prop = ctxt->node->properties;
1058 while (prop != NULL) {
1059 if (prop->ns != NULL) {
1060 if ((xmlStrEqual(name, prop->name)) &&
1061 ((namespace == prop->ns) ||
1062 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1063 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
1064 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1065 ctxt->sax->error(ctxt->userData,
1066 "Attribute %s in %s redefined\n",
1067 name, namespace->href);
1068 ctxt->wellFormed = 0;
1069 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1070 goto error;
1071 }
1072 }
1073 prop = prop->next;
1074 }
1075 } else {
1076 namespace = NULL;
1077 }
1078
1079 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1080 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1081
1082 if (ret != NULL) {
1083 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1084 xmlNodePtr tmp;
1085
1086 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1087 tmp = ret->children;
1088 while (tmp != NULL) {
1089 tmp->parent = (xmlNodePtr) ret;
1090 if (tmp->next == NULL)
1091 ret->last = tmp;
1092 tmp = tmp->next;
1093 }
1094 } else if (value != NULL) {
1095 ret->children = xmlNewDocText(ctxt->myDoc, value);
1096 ret->last = ret->children;
1097 if (ret->children != NULL)
1098 ret->children->parent = (xmlNodePtr) ret;
1099 }
1100 }
1101
1102 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1103 ctxt->myDoc && ctxt->myDoc->intSubset) {
1104
1105 /*
1106 * If we don't substitute entities, the validation should be
1107 * done on a value with replaced entities anyway.
1108 */
1109 if (!ctxt->replaceEntities) {
1110 xmlChar *val;
1111
1112 ctxt->depth++;
1113 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1114 0,0,0);
1115 ctxt->depth--;
1116
1117 if (val == NULL)
1118 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1119 ctxt->myDoc, ctxt->node, ret, value);
1120 else {
1121 xmlChar *nvalnorm;
1122
1123 /*
1124 * Do the last stage of the attribute normalization
1125 * It need to be done twice ... it's an extra burden related
1126 * to the ability to keep xmlSAX2References in attributes
1127 */
1128 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1129 ctxt->node, fullname, val);
1130 if (nvalnorm != NULL) {
1131 xmlFree(val);
1132 val = nvalnorm;
1133 }
1134
1135 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1136 ctxt->myDoc, ctxt->node, ret, val);
1137 xmlFree(val);
1138 }
1139 } else {
1140 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1141 ctxt->node, ret, value);
1142 }
1143 } else if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1144 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1145 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1146 /*
1147 * when validating, the ID registration is done at the attribute
1148 * validation level. Otherwise we have to do specific handling here.
1149 */
1150 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1151 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1152 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1153 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1154 }
1155
1156error:
1157 if (nval != NULL)
1158 xmlFree(nval);
1159 if (ns != NULL)
1160 xmlFree(ns);
1161}
1162
Daniel Veillard1af9a412003-08-20 22:54:39 +00001163/*
1164 * xmlCheckDefaultedAttributes:
1165 *
1166 * Check defaulted attributes from the DTD
1167 */
1168static void
1169xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1170 const xmlChar *prefix, const xmlChar **atts) {
1171 xmlElementPtr elemDecl;
1172 const xmlChar *att;
1173 int internal = 1;
1174 int i;
1175
1176 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1177 if (elemDecl == NULL) {
1178 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1179 internal = 0;
1180 }
1181
1182process_external_subset:
1183
1184 if (elemDecl != NULL) {
1185 xmlAttributePtr attr = elemDecl->attributes;
1186 /*
1187 * Check against defaulted attributes from the external subset
1188 * if the document is stamped as standalone
1189 */
1190 if ((ctxt->myDoc->standalone == 1) &&
1191 (ctxt->myDoc->extSubset != NULL) &&
1192 (ctxt->validate)) {
1193 while (attr != NULL) {
1194 if ((attr->defaultValue != NULL) &&
1195 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1196 attr->elem, attr->name,
1197 attr->prefix) == attr) &&
1198 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1199 attr->elem, attr->name,
1200 attr->prefix) == NULL)) {
1201 xmlChar *fulln;
1202
1203 if (attr->prefix != NULL) {
1204 fulln = xmlStrdup(attr->prefix);
1205 fulln = xmlStrcat(fulln, BAD_CAST ":");
1206 fulln = xmlStrcat(fulln, attr->name);
1207 } else {
1208 fulln = xmlStrdup(attr->name);
1209 }
1210
1211 /*
1212 * Check that the attribute is not declared in the
1213 * serialization
1214 */
1215 att = NULL;
1216 if (atts != NULL) {
1217 i = 0;
1218 att = atts[i];
1219 while (att != NULL) {
1220 if (xmlStrEqual(att, fulln))
1221 break;
1222 i += 2;
1223 att = atts[i];
1224 }
1225 }
1226 if (att == NULL) {
1227 if (ctxt->vctxt.error != NULL)
1228 ctxt->vctxt.error(ctxt->vctxt.userData,
1229 "standalone: attribute %s on %s defaulted from external subset\n",
1230 fulln, attr->elem);
1231 ctxt->valid = 0;
1232 }
1233 }
1234 attr = attr->nexth;
1235 }
1236 }
1237
1238 /*
1239 * Actually insert defaulted values when needed
1240 */
1241 attr = elemDecl->attributes;
1242 while (attr != NULL) {
1243 /*
1244 * Make sure that attributes redefinition occuring in the
1245 * internal subset are not overriden by definitions in the
1246 * external subset.
1247 */
1248 if (attr->defaultValue != NULL) {
1249 /*
1250 * the element should be instantiated in the tree if:
1251 * - this is a namespace prefix
1252 * - the user required for completion in the tree
1253 * like XSLT
1254 * - there isn't already an attribute definition
1255 * in the internal subset overriding it.
1256 */
1257 if (((attr->prefix != NULL) &&
1258 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1259 ((attr->prefix == NULL) &&
1260 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1261 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1262 xmlAttributePtr tst;
1263
1264 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1265 attr->elem, attr->name,
1266 attr->prefix);
1267 if ((tst == attr) || (tst == NULL)) {
1268 xmlChar fn[50];
1269 xmlChar *fulln;
1270
1271 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1272 if (fulln == NULL) {
1273 if ((ctxt->sax != NULL) &&
1274 (ctxt->sax->error != NULL))
1275 ctxt->sax->error(ctxt->userData,
1276 "SAX.xmlSAX2StartElement(): out of memory\n");
1277 ctxt->errNo = XML_ERR_NO_MEMORY;
1278 ctxt->instate = XML_PARSER_EOF;
1279 ctxt->disableSAX = 1;
1280 return;
1281 }
1282
1283 /*
1284 * Check that the attribute is not declared in the
1285 * serialization
1286 */
1287 att = NULL;
1288 if (atts != NULL) {
1289 i = 0;
1290 att = atts[i];
1291 while (att != NULL) {
1292 if (xmlStrEqual(att, fulln))
1293 break;
1294 i += 2;
1295 att = atts[i];
1296 }
1297 }
1298 if (att == NULL) {
1299 xmlSAX2AttributeInternal(ctxt, fulln,
1300 attr->defaultValue, prefix);
1301 }
1302 if ((fulln != fn) && (fulln != attr->name))
1303 xmlFree(fulln);
1304 }
1305 }
1306 }
1307 attr = attr->nexth;
1308 }
1309 if (internal == 1) {
1310 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1311 name, prefix);
1312 internal = 0;
1313 goto process_external_subset;
1314 }
1315 }
1316}
1317
1318/**
1319 * xmlSAX2StartElement:
1320 * @ctx: the user data (XML parser context)
1321 * @fullname: The element name, including namespace prefix
1322 * @atts: An array of name/value attributes pairs, NULL terminated
1323 *
1324 * called when an opening tag has been processed.
1325 */
1326void
1327xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1328{
1329 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1330 xmlNodePtr ret;
1331 xmlNodePtr parent = ctxt->node;
1332 xmlNsPtr ns;
1333 xmlChar *name;
1334 xmlChar *prefix;
1335 const xmlChar *att;
1336 const xmlChar *value;
1337 int i;
1338
1339#ifdef DEBUG_SAX
1340 xmlGenericError(xmlGenericErrorContext,
1341 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1342#endif
1343
1344 /*
1345 * First check on validity:
1346 */
1347 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1348 ((ctxt->myDoc->intSubset == NULL) ||
1349 ((ctxt->myDoc->intSubset->notations == NULL) &&
1350 (ctxt->myDoc->intSubset->elements == NULL) &&
1351 (ctxt->myDoc->intSubset->attributes == NULL) &&
1352 (ctxt->myDoc->intSubset->entities == NULL)))) {
1353 if (ctxt->vctxt.error != NULL) {
1354 ctxt->vctxt.error(ctxt->vctxt.userData,
1355 "Validation failed: no DTD found !\n");
1356 }
1357 ctxt->validate = 0;
1358 ctxt->valid = 0;
1359 ctxt->errNo = XML_ERR_NO_DTD;
1360 }
1361
1362
1363 /*
1364 * Split the full name into a namespace prefix and the tag name
1365 */
1366 name = xmlSplitQName(ctxt, fullname, &prefix);
1367
1368
1369 /*
1370 * Note : the namespace resolution is deferred until the end of the
1371 * attributes parsing, since local namespace can be defined as
1372 * an attribute at this level.
1373 */
1374 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1375 if (ret == NULL) {
1376 if (prefix != NULL)
1377 xmlFree(prefix);
1378 ctxt->errNo = XML_ERR_NO_MEMORY;
1379 ctxt->instate = XML_PARSER_EOF;
1380 ctxt->disableSAX = 1;
1381 return;
1382 }
1383 if (ctxt->myDoc->children == NULL) {
1384#ifdef DEBUG_SAX_TREE
1385 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1386#endif
1387 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1388 } else if (parent == NULL) {
1389 parent = ctxt->myDoc->children;
1390 }
1391 ctxt->nodemem = -1;
1392 if (ctxt->linenumbers) {
1393 if (ctxt->input != NULL)
1394 ret->content = (void *) (long) ctxt->input->line;
1395 }
1396
1397 /*
1398 * We are parsing a new node.
1399 */
1400#ifdef DEBUG_SAX_TREE
1401 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1402#endif
1403 nodePush(ctxt, ret);
1404
1405 /*
1406 * Link the child element
1407 */
1408 if (parent != NULL) {
1409 if (parent->type == XML_ELEMENT_NODE) {
1410#ifdef DEBUG_SAX_TREE
1411 xmlGenericError(xmlGenericErrorContext,
1412 "adding child %s to %s\n", name, parent->name);
1413#endif
1414 xmlAddChild(parent, ret);
1415 } else {
1416#ifdef DEBUG_SAX_TREE
1417 xmlGenericError(xmlGenericErrorContext,
1418 "adding sibling %s to ", name);
1419 xmlDebugDumpOneNode(stderr, parent, 0);
1420#endif
1421 xmlAddSibling(parent, ret);
1422 }
1423 }
1424
1425 /*
1426 * Insert all the defaulted attributes from the DTD especially namespaces
1427 */
1428 if ((!ctxt->html) &&
1429 ((ctxt->myDoc->intSubset != NULL) ||
1430 (ctxt->myDoc->extSubset != NULL))) {
1431 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1432 }
1433
1434 /*
1435 * process all the attributes whose name start with "xmlns"
1436 */
1437 if (atts != NULL) {
1438 i = 0;
1439 att = atts[i++];
1440 value = atts[i++];
1441 if (!ctxt->html) {
1442 while ((att != NULL) && (value != NULL)) {
1443 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1444 (att[3] == 'n') && (att[4] == 's'))
1445 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1446
1447 att = atts[i++];
1448 value = atts[i++];
1449 }
1450 }
1451 }
1452
1453 /*
1454 * Search the namespace, note that since the attributes have been
1455 * processed, the local namespaces are available.
1456 */
1457 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1458 if ((ns == NULL) && (parent != NULL))
1459 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1460 if ((prefix != NULL) && (ns == NULL)) {
1461 ns = xmlNewNs(ret, NULL, prefix);
1462 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1463 ctxt->sax->warning(ctxt->userData,
1464 "Namespace prefix %s is not defined\n", prefix);
1465 }
1466
1467 /*
1468 * set the namespace node, making sure that if the default namspace
1469 * is unbound on a parent we simply kee it NULL
1470 */
1471 if ((ns != NULL) && (ns->href != NULL) &&
1472 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1473 xmlSetNs(ret, ns);
1474
1475 /*
1476 * process all the other attributes
1477 */
1478 if (atts != NULL) {
1479 i = 0;
1480 att = atts[i++];
1481 value = atts[i++];
1482 if (ctxt->html) {
1483 while (att != NULL) {
1484 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1485 att = atts[i++];
1486 value = atts[i++];
1487 }
1488 } else {
1489 while ((att != NULL) && (value != NULL)) {
1490 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1491 (att[3] != 'n') || (att[4] != 's'))
1492 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1493
1494 /*
1495 * Next ones
1496 */
1497 att = atts[i++];
1498 value = atts[i++];
1499 }
1500 }
1501 }
1502
1503 /*
1504 * If it's the Document root, finish the DTD validation and
1505 * check the document root element for validity
1506 */
1507 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
1508 int chk;
1509
1510 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1511 if (chk <= 0)
1512 ctxt->valid = 0;
1513 if (chk < 0)
1514 ctxt->wellFormed = 0;
1515 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1516 ctxt->vctxt.finishDtd = 1;
1517 }
1518
1519 if (prefix != NULL)
1520 xmlFree(prefix);
1521
1522}
1523
1524/**
1525 * xmlSAX2EndElement:
1526 * @ctx: the user data (XML parser context)
1527 * @name: The element name
1528 *
1529 * called when the end of an element has been detected.
1530 */
1531void
1532xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1533{
1534 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1535 xmlParserNodeInfo node_info;
1536 xmlNodePtr cur = ctxt->node;
1537
1538#ifdef DEBUG_SAX
1539 if (name == NULL)
1540 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1541 else
1542 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1543#endif
1544
1545 /* Capture end position and add node */
1546 if (cur != NULL && ctxt->record_info) {
1547 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1548 node_info.end_line = ctxt->input->line;
1549 node_info.node = cur;
1550 xmlParserAddNodeInfo(ctxt, &node_info);
1551 }
1552 ctxt->nodemem = -1;
1553
1554 if (ctxt->validate && ctxt->wellFormed &&
1555 ctxt->myDoc && ctxt->myDoc->intSubset)
1556 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1557 cur);
1558
1559
1560 /*
1561 * end of parsing of this node.
1562 */
1563#ifdef DEBUG_SAX_TREE
1564 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1565#endif
1566 nodePop(ctxt);
1567}
1568
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001569int nb_interned = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001570/*
Daniel Veillard19895052003-09-17 13:59:32 +00001571 * xmlSAX2TextNode:
1572 * @ctxt: the parser context
1573 * @str: the input string
1574 * @len: the string length
1575 *
1576 * Remove the entities from an attribute value
1577 *
1578 * Returns the newly allocated string or NULL if not needed or error
1579 */
1580static xmlNodePtr
1581xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1582 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001583 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001584
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001585 /*
1586 * Allocate
1587 */
Daniel Veillard19895052003-09-17 13:59:32 +00001588 if (ctxt->freeElems != NULL) {
1589 ret = ctxt->freeElems;
1590 ctxt->freeElems = ret->next;
1591 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001592 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001593 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001594 }
1595 if (ret == NULL) {
1596 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1597 ctxt->sax->error(ctxt->userData,
1598 "SAX.xmlSAX2Characters(): out of memory\n");
1599 ctxt->errNo = XML_ERR_NO_MEMORY;
1600 ctxt->instate = XML_PARSER_EOF;
1601 ctxt->disableSAX = 1;
1602 return(NULL);
1603 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001604 /*
1605 * intern the formatting blanks found between tags, or the
1606 * very short strings
1607 */
1608 if (ctxt->dictNames) {
1609 xmlChar cur = str[len];
1610
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001611 if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1612 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001613 intern = xmlDictLookup(ctxt->dict, str, len);
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001614 } else if (IS_BLANK(*str) && (len < 60) && (cur == '<') &&
1615 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001616 int i;
1617
1618 for (i = 1;i < len;i++) {
1619 if (!IS_BLANK(*str)) goto skip;
1620 }
1621 intern = xmlDictLookup(ctxt->dict, str, len);
1622 }
1623 }
1624skip:
1625 memset(ret, 0, sizeof(xmlNode));
1626 ret->type = XML_TEXT_NODE;
1627
1628 ret->name = xmlStringText;
1629 if (intern == NULL)
1630 ret->content = xmlStrndup(str, len);
1631 else
1632 ret->content = (xmlChar *) intern;
1633
1634 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1635 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001636 return(ret);
1637}
1638
1639/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001640 * xmlSAX2DecodeAttrEntities:
1641 * @ctxt: the parser context
1642 * @str: the input string
1643 * @len: the string length
1644 *
1645 * Remove the entities from an attribute value
1646 *
1647 * Returns the newly allocated string or NULL if not needed or error
1648 */
1649static xmlChar *
1650xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1651 const xmlChar *end) {
1652 const xmlChar *in;
1653 xmlChar *ret;
1654
1655 in = str;
1656 while (in < end)
1657 if (*in++ == '&')
1658 goto decode;
1659 return(NULL);
1660decode:
1661 ctxt->depth++;
1662 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1663 XML_SUBSTITUTE_REF, 0,0,0);
1664 ctxt->depth--;
1665 return(ret);
1666}
1667
1668/**
1669 * xmlSAX2AttributeNs:
1670 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001671 * @localname: the local name of the attribute
1672 * @prefix: the attribute namespace prefix if available
1673 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001674 * @value: Start of the attribute value
1675 * @valueend: end of the attribute value
1676 *
1677 * Handle an attribute that has been read by the parser.
1678 * The default handling is to convert the attribute into an
1679 * DOM subtree and past it in a new xmlAttr element added to
1680 * the element.
1681 */
1682static void
1683xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1684 const xmlChar * localname,
1685 const xmlChar * prefix,
1686 const xmlChar * value,
1687 const xmlChar * valueend)
1688{
1689 xmlAttrPtr ret;
1690 xmlNsPtr namespace = NULL;
1691 xmlChar *dup = NULL;
1692
Daniel Veillarde57ec792003-09-10 10:50:59 +00001693 /*
1694 * Note: if prefix == NULL, the attribute is not in the default namespace
1695 */
1696 if (prefix != NULL)
1697 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1698
Daniel Veillard8a44e592003-09-15 14:50:06 +00001699 /*
1700 * allocate the node
1701 */
1702 if (ctxt->freeAttrs != NULL) {
1703 ret = ctxt->freeAttrs;
1704 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001705 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001706 memset(ret, 0, sizeof(xmlAttr));
1707 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001708
Daniel Veillard8a44e592003-09-15 14:50:06 +00001709 ret->parent = ctxt->node;
1710 ret->doc = ctxt->myDoc;
1711 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001712
Daniel Veillard8a44e592003-09-15 14:50:06 +00001713 if (ctxt->dictNames)
1714 ret->name = localname;
1715 else
1716 ret->name = xmlStrdup(localname);
1717
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001718 /* link at the end to preserv order, TODO speed up with a last */
1719 if (ctxt->node->properties == NULL) {
1720 ctxt->node->properties = ret;
1721 } else {
1722 xmlAttrPtr prev = ctxt->node->properties;
1723
1724 while (prev->next != NULL) prev = prev->next;
1725 prev->next = ret;
1726 ret->prev = prev;
1727 }
1728
Daniel Veillard8a44e592003-09-15 14:50:06 +00001729 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1730 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1731 } else {
1732 if (ctxt->dictNames)
1733 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1734 (xmlChar *) localname, NULL);
1735 else
1736 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1737 if (ret == NULL) {
1738 ctxt->errNo = XML_ERR_NO_MEMORY;
1739 ctxt->instate = XML_PARSER_EOF;
1740 ctxt->disableSAX = 1;
1741 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001742 }
1743 }
1744
Daniel Veillard8a44e592003-09-15 14:50:06 +00001745 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1746 xmlNodePtr tmp;
1747
Daniel Veillard19895052003-09-17 13:59:32 +00001748 /*
1749 * We know that if there is an entity reference, then
1750 * the string has been dup'ed and terminates with 0
1751 * otherwise with ' or "
1752 */
1753 if (*valueend != 0) {
1754 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1755 ret->children = tmp;
1756 ret->last = tmp;
1757 if (tmp != NULL) {
1758 tmp->doc = ret->doc;
1759 tmp->parent = (xmlNodePtr) ret;
1760 }
1761 } else {
1762 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1763 valueend - value);
1764 tmp = ret->children;
1765 while (tmp != NULL) {
1766 tmp->parent = (xmlNodePtr) ret;
1767 if (tmp->next == NULL)
1768 ret->last = tmp;
1769 tmp = tmp->next;
1770 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001771 }
1772 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00001773 xmlNodePtr tmp;
1774
1775 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1776 ret->children = tmp;
1777 ret->last = tmp;
1778 if (tmp != NULL) {
1779 tmp->doc = ret->doc;
1780 tmp->parent = (xmlNodePtr) ret;
1781 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001782 }
1783
Daniel Veillarde57ec792003-09-10 10:50:59 +00001784 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1785 ctxt->myDoc && ctxt->myDoc->intSubset) {
1786 /*
1787 * If we don't substitute entities, the validation should be
1788 * done on a value with replaced entities anyway.
1789 */
1790 if (!ctxt->replaceEntities) {
1791 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
1792 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00001793 if (*valueend == 0) {
1794 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1795 ctxt->myDoc, ctxt->node, ret, value);
1796 } else {
1797 /*
1798 * That should already be normalized.
1799 * cheaper to finally allocate here than duplicate
1800 * entry points in the full validation code
1801 */
1802 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001803
Daniel Veillard62998c02003-09-15 12:56:36 +00001804 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1805 ctxt->myDoc, ctxt->node, ret, dup);
1806 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001807 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00001808 /*
1809 * dup now contains a string of the flattened attribute
1810 * content with entities substitued. Check if we need to
1811 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001812 * It need to be done twice ... it's an extra burden related
1813 * to the ability to keep references in attributes
1814 */
Daniel Veillard62998c02003-09-15 12:56:36 +00001815 if (ctxt->attsSpecial != NULL) {
1816 xmlChar *nvalnorm;
1817 xmlChar fn[50];
1818 xmlChar *fullname;
1819
1820 fullname = xmlBuildQName(localname, prefix, fn, 50);
1821 if (fullname != NULL) {
1822 ctxt->vctxt.valid = 1;
1823 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
1824 &ctxt->vctxt, ctxt->myDoc,
1825 ctxt->node, fullname, dup);
1826 if (ctxt->vctxt.valid != 1)
1827 ctxt->valid = 0;
1828
1829 if ((fullname != fn) && (fullname != localname))
1830 xmlFree(fullname);
1831 if (nvalnorm != NULL) {
1832 xmlFree(dup);
1833 dup = nvalnorm;
1834 }
1835 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001836 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001837
1838 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1839 ctxt->myDoc, ctxt->node, ret, dup);
1840 }
1841 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001842 /*
1843 * if entities already have been substitued, then
1844 * the attribute as passed is already normalized
1845 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001846 dup = xmlStrndup(value, valueend - value);
1847
1848 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1849 ctxt->myDoc, ctxt->node, ret, dup);
1850 }
1851 } else if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1852 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1853 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1854 /*
1855 * when validating, the ID registration is done at the attribute
1856 * validation level. Otherwise we have to do specific handling here.
1857 */
1858 if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
1859 /* might be worth duplicate entry points and not copy */
1860 if (dup == NULL)
1861 dup = xmlStrndup(value, valueend - value);
1862 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1863 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
1864 if (dup == NULL)
1865 dup = xmlStrndup(value, valueend - value);
1866 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1867 }
1868 }
1869 if (dup != NULL)
1870 xmlFree(dup);
1871}
1872
1873/**
1874 * xmlSAX2StartElementNs:
1875 * @ctx: the user data (XML parser context)
1876 * @localname: the local name of the element
1877 * @prefix: the element namespace prefix if available
1878 * @URI: the element namespace name if available
1879 * @nb_namespaces: number of namespace definitions on that node
1880 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
1881 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001882 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001883 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
1884 * attribute values.
1885 *
1886 * SAX2 callback when an element start has been detected by the parser.
1887 * It provides the namespace informations for the element, as well as
1888 * the new namespace declarations on the element.
1889 */
1890void
1891xmlSAX2StartElementNs(void *ctx,
1892 const xmlChar *localname,
1893 const xmlChar *prefix,
1894 const xmlChar *URI,
1895 int nb_namespaces,
1896 const xmlChar **namespaces,
1897 int nb_attributes,
1898 int nb_defaulted,
1899 const xmlChar **attributes)
1900{
1901 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1902 xmlNodePtr ret;
1903 xmlNodePtr parent = ctxt->node;
1904 xmlNsPtr last = NULL, ns;
1905 const xmlChar *uri, *pref;
1906 int i, j;
1907
1908 /*
1909 * First check on validity:
1910 */
1911 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1912 ((ctxt->myDoc->intSubset == NULL) ||
1913 ((ctxt->myDoc->intSubset->notations == NULL) &&
1914 (ctxt->myDoc->intSubset->elements == NULL) &&
1915 (ctxt->myDoc->intSubset->attributes == NULL) &&
1916 (ctxt->myDoc->intSubset->entities == NULL)))) {
1917 if (ctxt->vctxt.error != NULL) {
1918 ctxt->vctxt.error(ctxt->vctxt.userData,
1919 "Validation failed: no DTD found !\n");
1920 }
1921 ctxt->validate = 0;
1922 ctxt->valid = 0;
1923 ctxt->errNo = XML_ERR_NO_DTD;
1924 }
1925
Daniel Veillard8a44e592003-09-15 14:50:06 +00001926 /*
1927 * allocate the node
1928 */
1929 if (ctxt->freeElems != NULL) {
1930 ret = ctxt->freeElems;
1931 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001932 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001933 memset(ret, 0, sizeof(xmlNode));
1934 ret->type = XML_ELEMENT_NODE;
1935
1936 if (ctxt->dictNames)
1937 ret->name = localname;
1938 else
1939 ret->name = xmlStrdup(localname);
1940
1941 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1942 xmlRegisterNodeDefaultValue(ret);
1943 } else {
1944 if (ctxt->dictNames)
1945 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
1946 (xmlChar *) localname, NULL);
1947 else
1948 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
1949 if (ret == NULL) {
1950 ctxt->errNo = XML_ERR_NO_MEMORY;
1951 ctxt->instate = XML_PARSER_EOF;
1952 ctxt->disableSAX = 1;
1953 return;
1954 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001955 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00001956 if (ctxt->linenumbers) {
1957 if (ctxt->input != NULL)
1958 ret->content = (void *) (long) ctxt->input->line;
1959 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001960
Daniel Veillarde57ec792003-09-10 10:50:59 +00001961 if (ctxt->myDoc->children == NULL) {
1962 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1963 } else if (parent == NULL) {
1964 parent = ctxt->myDoc->children;
1965 }
1966 /*
1967 * Build the namespace list
1968 */
1969 for (i = 0,j = 0;j < nb_namespaces;j++) {
1970 pref = namespaces[i++];
1971 uri = namespaces[i++];
1972 ns = xmlNewNs(NULL, uri, pref);
1973 if (ns != NULL) {
1974 if (last == NULL) {
1975 ret->nsDef = last = ns;
1976 } else {
1977 last->next = ns;
1978 last = ns;
1979 }
1980 if ((URI != NULL) && (prefix == pref))
1981 ret->ns = ns;
1982 } else {
1983 ctxt->errNo = XML_ERR_NO_MEMORY;
1984 ctxt->instate = XML_PARSER_EOF;
1985 ctxt->disableSAX = 1;
1986 return;
1987 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00001988 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1989 ctxt->myDoc && ctxt->myDoc->intSubset) {
1990 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1991 ret, prefix, ns, uri);
1992 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001993 }
1994 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001995
1996 /*
1997 * We are parsing a new node.
1998 */
1999 nodePush(ctxt, ret);
2000
2001 /*
2002 * Link the child element
2003 */
2004 if (parent != NULL) {
2005 if (parent->type == XML_ELEMENT_NODE) {
2006 xmlAddChild(parent, ret);
2007 } else {
2008 xmlAddSibling(parent, ret);
2009 }
2010 }
2011
2012 /*
2013 * Insert the defaulted attributes from the DTD only if requested:
2014 */
2015 if ((nb_defaulted != 0) &&
2016 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2017 nb_attributes -= nb_defaulted;
2018
2019 /*
2020 * Search the namespace if it wasn't already found
2021 */
2022 if ((URI != NULL) && (ret->ns == NULL)) {
2023 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2024 if (ret->ns == NULL) {
2025 ns = xmlNewNs(ret, NULL, prefix);
2026 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
2027 ctxt->sax->warning(ctxt->userData,
2028 "Namespace prefix %s was not found\n", prefix);
2029 }
2030 }
2031
2032 /*
2033 * process all the other attributes
2034 */
2035 if (nb_attributes > 0) {
2036 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2037 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2038 attributes[j+3], attributes[j+4]);
2039 }
2040 }
2041
2042 /*
2043 * If it's the Document root, finish the DTD validation and
2044 * check the document root element for validity
2045 */
2046 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
2047 int chk;
2048
2049 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2050 if (chk <= 0)
2051 ctxt->valid = 0;
2052 if (chk < 0)
2053 ctxt->wellFormed = 0;
2054 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2055 ctxt->vctxt.finishDtd = 1;
2056 }
2057}
2058
2059/**
2060 * xmlSAX2EndElementNs:
2061 * @ctx: the user data (XML parser context)
2062 * @localname: the local name of the element
2063 * @prefix: the element namespace prefix if available
2064 * @URI: the element namespace name if available
2065 *
2066 * SAX2 callback when an element end has been detected by the parser.
2067 * It provides the namespace informations for the element.
2068 */
2069void
2070xmlSAX2EndElementNs(void *ctx,
2071 const xmlChar * localname ATTRIBUTE_UNUSED,
2072 const xmlChar * prefix ATTRIBUTE_UNUSED,
2073 const xmlChar * URI ATTRIBUTE_UNUSED)
2074{
2075 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2076 xmlParserNodeInfo node_info;
2077 xmlNodePtr cur = ctxt->node;
2078
2079 /* Capture end position and add node */
2080 if ((ctxt->record_info) && (cur != NULL)) {
2081 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2082 node_info.end_line = ctxt->input->line;
2083 node_info.node = cur;
2084 xmlParserAddNodeInfo(ctxt, &node_info);
2085 }
2086 ctxt->nodemem = -1;
2087
2088 if (ctxt->validate && ctxt->wellFormed &&
2089 ctxt->myDoc && ctxt->myDoc->intSubset)
2090 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
2091
2092 /*
2093 * end of parsing of this node.
2094 */
2095 nodePop(ctxt);
2096}
2097
Daniel Veillard1af9a412003-08-20 22:54:39 +00002098/**
2099 * xmlSAX2Reference:
2100 * @ctx: the user data (XML parser context)
2101 * @name: The entity name
2102 *
2103 * called when an entity xmlSAX2Reference is detected.
2104 */
2105void
2106xmlSAX2Reference(void *ctx, const xmlChar *name)
2107{
2108 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2109 xmlNodePtr ret;
2110
2111#ifdef DEBUG_SAX
2112 xmlGenericError(xmlGenericErrorContext,
2113 "SAX.xmlSAX2Reference(%s)\n", name);
2114#endif
2115 if (name[0] == '#')
2116 ret = xmlNewCharRef(ctxt->myDoc, name);
2117 else
2118 ret = xmlNewReference(ctxt->myDoc, name);
2119#ifdef DEBUG_SAX_TREE
2120 xmlGenericError(xmlGenericErrorContext,
2121 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2122#endif
2123 xmlAddChild(ctxt->node, ret);
2124}
2125
2126/**
2127 * xmlSAX2Characters:
2128 * @ctx: the user data (XML parser context)
2129 * @ch: a xmlChar string
2130 * @len: the number of xmlChar
2131 *
2132 * receiving some chars from the parser.
2133 */
2134void
2135xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2136{
2137 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2138 xmlNodePtr lastChild;
2139
2140#ifdef DEBUG_SAX
2141 xmlGenericError(xmlGenericErrorContext,
2142 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2143#endif
2144 /*
2145 * Handle the data if any. If there is no child
2146 * add it as content, otherwise if the last child is text,
2147 * concatenate it, else create a new node of type text.
2148 */
2149
2150 if (ctxt->node == NULL) {
2151#ifdef DEBUG_SAX_TREE
2152 xmlGenericError(xmlGenericErrorContext,
2153 "add chars: ctxt->node == NULL !\n");
2154#endif
2155 return;
2156 }
Daniel Veillard19895052003-09-17 13:59:32 +00002157 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002158#ifdef DEBUG_SAX_TREE
2159 xmlGenericError(xmlGenericErrorContext,
2160 "add chars to %s \n", ctxt->node->name);
2161#endif
2162
2163 /*
2164 * Here we needed an accelerator mechanism in case of very large
2165 * elements. Use an attribute in the structure !!!
2166 */
2167 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002168 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2169 if (lastChild != NULL) {
2170 ctxt->node->children = lastChild;
2171 ctxt->node->last = lastChild;
2172 lastChild->parent = ctxt->node;
2173 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002174 ctxt->nodelen = len;
2175 ctxt->nodemem = len + 1;
2176 }
2177 } else {
2178 int coalesceText = (lastChild != NULL) &&
2179 (lastChild->type == XML_TEXT_NODE) &&
2180 (lastChild->name == xmlStringText);
2181 if ((coalesceText) && (ctxt->nodemem != 0)) {
2182 /*
2183 * The whole point of maintaining nodelen and nodemem,
2184 * xmlTextConcat is too costly, i.e. compute length,
2185 * reallocate a new buffer, move data, append ch. Here
2186 * We try to minimaze realloc() uses and avoid copying
2187 * and recomputing length over and over.
2188 */
2189 if (ctxt->nodelen + len >= ctxt->nodemem) {
2190 xmlChar *newbuf;
2191 int size;
2192
2193 size = ctxt->nodemem + len;
2194 size *= 2;
2195 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2196 if (newbuf == NULL) {
2197 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2198 ctxt->sax->error(ctxt->userData,
2199 "SAX.xmlSAX2Characters(): out of memory\n");
2200 ctxt->errNo = XML_ERR_NO_MEMORY;
2201 ctxt->instate = XML_PARSER_EOF;
2202 ctxt->disableSAX = 1;
2203 return;
2204 }
2205 ctxt->nodemem = size;
2206 lastChild->content = newbuf;
2207 }
2208 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2209 ctxt->nodelen += len;
2210 lastChild->content[ctxt->nodelen] = 0;
2211 } else if (coalesceText) {
2212 if (xmlTextConcat(lastChild, ch, len)) {
2213 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2214 ctxt->sax->error(ctxt->userData,
2215 "SAX.xmlSAX2Characters(): out of memory\n");
2216 ctxt->errNo = XML_ERR_NO_MEMORY;
2217 ctxt->instate = XML_PARSER_EOF;
2218 ctxt->disableSAX = 1;
2219 }
2220 if (ctxt->node->children != NULL) {
2221 ctxt->nodelen = xmlStrlen(lastChild->content);
2222 ctxt->nodemem = ctxt->nodelen + 1;
2223 }
2224 } else {
2225 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002226 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2227 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002228 xmlAddChild(ctxt->node, lastChild);
2229 if (ctxt->node->children != NULL) {
2230 ctxt->nodelen = len;
2231 ctxt->nodemem = len + 1;
2232 }
2233 }
2234 }
2235 }
2236}
2237
2238/**
2239 * xmlSAX2IgnorableWhitespace:
2240 * @ctx: the user data (XML parser context)
2241 * @ch: a xmlChar string
2242 * @len: the number of xmlChar
2243 *
2244 * receiving some ignorable whitespaces from the parser.
2245 * UNUSED: by default the DOM building will use xmlSAX2Characters
2246 */
2247void
2248xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2249{
2250 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2251#ifdef DEBUG_SAX
2252 xmlGenericError(xmlGenericErrorContext,
2253 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2254#endif
2255}
2256
2257/**
2258 * xmlSAX2ProcessingInstruction:
2259 * @ctx: the user data (XML parser context)
2260 * @target: the target name
2261 * @data: the PI data's
2262 *
2263 * A processing instruction has been parsed.
2264 */
2265void
2266xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2267 const xmlChar *data)
2268{
2269 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2270 xmlNodePtr ret;
2271 xmlNodePtr parent = ctxt->node;
2272
2273#ifdef DEBUG_SAX
2274 xmlGenericError(xmlGenericErrorContext,
2275 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2276#endif
2277
2278 ret = xmlNewPI(target, data);
2279 if (ret == NULL) return;
2280 parent = ctxt->node;
2281
2282 if (ctxt->inSubset == 1) {
2283 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2284 return;
2285 } else if (ctxt->inSubset == 2) {
2286 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2287 return;
2288 }
2289 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2290#ifdef DEBUG_SAX_TREE
2291 xmlGenericError(xmlGenericErrorContext,
2292 "Setting PI %s as root\n", target);
2293#endif
2294 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2295 return;
2296 }
2297 if (parent->type == XML_ELEMENT_NODE) {
2298#ifdef DEBUG_SAX_TREE
2299 xmlGenericError(xmlGenericErrorContext,
2300 "adding PI %s child to %s\n", target, parent->name);
2301#endif
2302 xmlAddChild(parent, ret);
2303 } else {
2304#ifdef DEBUG_SAX_TREE
2305 xmlGenericError(xmlGenericErrorContext,
2306 "adding PI %s sibling to ", target);
2307 xmlDebugDumpOneNode(stderr, parent, 0);
2308#endif
2309 xmlAddSibling(parent, ret);
2310 }
2311}
2312
2313/**
2314 * xmlSAX2Comment:
2315 * @ctx: the user data (XML parser context)
2316 * @value: the xmlSAX2Comment content
2317 *
2318 * A xmlSAX2Comment has been parsed.
2319 */
2320void
2321xmlSAX2Comment(void *ctx, const xmlChar *value)
2322{
2323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2324 xmlNodePtr ret;
2325 xmlNodePtr parent = ctxt->node;
2326
2327#ifdef DEBUG_SAX
2328 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2329#endif
2330 ret = xmlNewDocComment(ctxt->myDoc, value);
2331 if (ret == NULL) return;
2332
2333 if (ctxt->inSubset == 1) {
2334 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2335 return;
2336 } else if (ctxt->inSubset == 2) {
2337 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2338 return;
2339 }
2340 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2341#ifdef DEBUG_SAX_TREE
2342 xmlGenericError(xmlGenericErrorContext,
2343 "Setting xmlSAX2Comment as root\n");
2344#endif
2345 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2346 return;
2347 }
2348 if (parent->type == XML_ELEMENT_NODE) {
2349#ifdef DEBUG_SAX_TREE
2350 xmlGenericError(xmlGenericErrorContext,
2351 "adding xmlSAX2Comment child to %s\n", parent->name);
2352#endif
2353 xmlAddChild(parent, ret);
2354 } else {
2355#ifdef DEBUG_SAX_TREE
2356 xmlGenericError(xmlGenericErrorContext,
2357 "adding xmlSAX2Comment sibling to ");
2358 xmlDebugDumpOneNode(stderr, parent, 0);
2359#endif
2360 xmlAddSibling(parent, ret);
2361 }
2362}
2363
2364/**
2365 * xmlSAX2CDataBlock:
2366 * @ctx: the user data (XML parser context)
2367 * @value: The pcdata content
2368 * @len: the block length
2369 *
2370 * called when a pcdata block has been parsed
2371 */
2372void
2373xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2374{
2375 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2376 xmlNodePtr ret, lastChild;
2377
2378#ifdef DEBUG_SAX
2379 xmlGenericError(xmlGenericErrorContext,
2380 "SAX.pcdata(%.10s, %d)\n", value, len);
2381#endif
2382 lastChild = xmlGetLastChild(ctxt->node);
2383#ifdef DEBUG_SAX_TREE
2384 xmlGenericError(xmlGenericErrorContext,
2385 "add chars to %s \n", ctxt->node->name);
2386#endif
2387 if ((lastChild != NULL) &&
2388 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2389 xmlTextConcat(lastChild, value, len);
2390 } else {
2391 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2392 xmlAddChild(ctxt->node, ret);
2393 }
2394}
2395
Daniel Veillard62998c02003-09-15 12:56:36 +00002396static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002397
Daniel Veillarde57ec792003-09-10 10:50:59 +00002398/**
2399 * xmlSAXDefaultVersion:
2400 * @version: the version, 1 or 2
2401 *
2402 * Set the default version of SAX used globally by the library.
2403 * Note that this may not be a good thing to do from a library
2404 * it is better to use xmlSAXVersion() to set up specifically the
2405 * version for a given parsing context.
2406 *
2407 * Returns the previous value in case of success and -1 in case of error.
2408 */
2409int
2410xmlSAXDefaultVersion(int version)
2411{
2412 int ret = xmlSAX2DefaultVersionValue;
2413
2414 if ((version != 1) && (version != 2))
2415 return(-1);
2416 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002417 return(ret);
2418}
2419
2420/**
2421 * xmlSAXVersion:
2422 * @hdlr: the SAX handler
2423 * @version: the version, 1 or 2
2424 *
2425 * Initialize the default XML SAX handler according to the version
2426 *
2427 * Returns 0 in case of success and -1 in case of error.
2428 */
2429int
2430xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2431{
2432 if (hdlr == NULL) return(-1);
2433 if (version == 1) {
2434 hdlr->startElement = xmlSAX2StartElement;
2435 hdlr->endElement = xmlSAX2EndElement;
Daniel Veillard092643b2003-09-25 14:29:29 +00002436 hdlr->initialized = 1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002437 } else if (version == 2) {
2438 hdlr->startElement = NULL;
2439 hdlr->endElement = NULL;
2440 hdlr->startElementNs = xmlSAX2StartElementNs;
2441 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillard092643b2003-09-25 14:29:29 +00002442 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002443 } else
2444 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002445 hdlr->internalSubset = xmlSAX2InternalSubset;
2446 hdlr->externalSubset = xmlSAX2ExternalSubset;
2447 hdlr->isStandalone = xmlSAX2IsStandalone;
2448 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2449 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2450 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2451 hdlr->getEntity = xmlSAX2GetEntity;
2452 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2453 hdlr->entityDecl = xmlSAX2EntityDecl;
2454 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2455 hdlr->elementDecl = xmlSAX2ElementDecl;
2456 hdlr->notationDecl = xmlSAX2NotationDecl;
2457 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2458 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2459 hdlr->startDocument = xmlSAX2StartDocument;
2460 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002461 hdlr->reference = xmlSAX2Reference;
2462 hdlr->characters = xmlSAX2Characters;
2463 hdlr->cdataBlock = xmlSAX2CDataBlock;
2464 hdlr->ignorableWhitespace = xmlSAX2Characters;
2465 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2466 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002467 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002468 hdlr->error = xmlParserError;
2469 hdlr->fatalError = xmlParserError;
2470
Daniel Veillarde57ec792003-09-10 10:50:59 +00002471 return(0);
2472}
2473
2474/**
2475 * xmlSAX2InitDefaultSAXHandler:
2476 * @hdlr: the SAX handler
2477 * @warning: flag if non-zero sets the handler warning procedure
2478 *
2479 * Initialize the default XML SAX2 handler
2480 */
2481void
2482xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2483{
2484 if ((hdlr == NULL) || (hdlr->initialized != 0))
2485 return;
2486
2487 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2488 if (warning == 0)
2489 hdlr->warning = NULL;
2490 else
2491 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002492}
2493
2494/**
2495 * xmlDefaultSAXHandlerInit:
2496 *
2497 * Initialize the default SAX2 handler
2498 */
2499void
2500xmlDefaultSAXHandlerInit(void)
2501{
Daniel Veillard092643b2003-09-25 14:29:29 +00002502 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002503}
2504
2505#ifdef LIBXML_HTML_ENABLED
2506
2507/**
2508 * xmlSAX2InitHtmlDefaultSAXHandler:
2509 * @hdlr: the SAX handler
2510 *
2511 * Initialize the default HTML SAX2 handler
2512 */
2513void
2514xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2515{
2516 if(hdlr->initialized != 0)
2517 return;
2518
2519 hdlr->internalSubset = xmlSAX2InternalSubset;
2520 hdlr->externalSubset = NULL;
2521 hdlr->isStandalone = NULL;
2522 hdlr->hasInternalSubset = NULL;
2523 hdlr->hasExternalSubset = NULL;
2524 hdlr->resolveEntity = NULL;
2525 hdlr->getEntity = xmlSAX2GetEntity;
2526 hdlr->getParameterEntity = NULL;
2527 hdlr->entityDecl = NULL;
2528 hdlr->attributeDecl = NULL;
2529 hdlr->elementDecl = NULL;
2530 hdlr->notationDecl = NULL;
2531 hdlr->unparsedEntityDecl = NULL;
2532 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2533 hdlr->startDocument = xmlSAX2StartDocument;
2534 hdlr->endDocument = xmlSAX2EndDocument;
2535 hdlr->startElement = xmlSAX2StartElement;
2536 hdlr->endElement = xmlSAX2EndElement;
2537 hdlr->reference = NULL;
2538 hdlr->characters = xmlSAX2Characters;
2539 hdlr->cdataBlock = xmlSAX2CDataBlock;
2540 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2541 hdlr->processingInstruction = NULL;
2542 hdlr->comment = xmlSAX2Comment;
2543 hdlr->warning = xmlParserWarning;
2544 hdlr->error = xmlParserError;
2545 hdlr->fatalError = xmlParserError;
2546
Daniel Veillard092643b2003-09-25 14:29:29 +00002547 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002548}
2549
2550/**
2551 * htmlDefaultSAXHandlerInit:
2552 *
2553 * Initialize the default SAX handler
2554 */
2555void
2556htmlDefaultSAXHandlerInit(void)
2557{
Daniel Veillard092643b2003-09-25 14:29:29 +00002558 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002559}
2560
2561#endif /* LIBXML_HTML_ENABLED */
2562
2563#ifdef LIBXML_DOCB_ENABLED
2564
2565/**
2566 * xmlSAX2InitDocbDefaultSAXHandler:
2567 * @hdlr: the SAX handler
2568 *
2569 * Initialize the default DocBook SAX2 handler
2570 */
2571void
2572xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2573{
2574 if(hdlr->initialized != 0)
2575 return;
2576
2577 hdlr->internalSubset = xmlSAX2InternalSubset;
2578 hdlr->externalSubset = NULL;
2579 hdlr->isStandalone = xmlSAX2IsStandalone;
2580 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2581 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2582 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2583 hdlr->getEntity = xmlSAX2GetEntity;
2584 hdlr->getParameterEntity = NULL;
2585 hdlr->entityDecl = xmlSAX2EntityDecl;
2586 hdlr->attributeDecl = NULL;
2587 hdlr->elementDecl = NULL;
2588 hdlr->notationDecl = NULL;
2589 hdlr->unparsedEntityDecl = NULL;
2590 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2591 hdlr->startDocument = xmlSAX2StartDocument;
2592 hdlr->endDocument = xmlSAX2EndDocument;
2593 hdlr->startElement = xmlSAX2StartElement;
2594 hdlr->endElement = xmlSAX2EndElement;
2595 hdlr->reference = xmlSAX2Reference;
2596 hdlr->characters = xmlSAX2Characters;
2597 hdlr->cdataBlock = NULL;
2598 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2599 hdlr->processingInstruction = NULL;
2600 hdlr->comment = xmlSAX2Comment;
2601 hdlr->warning = xmlParserWarning;
2602 hdlr->error = xmlParserError;
2603 hdlr->fatalError = xmlParserError;
2604
2605 hdlr->initialized = XML_SAX2_MAGIC;
2606}
2607
2608/**
2609 * docbDefaultSAXHandlerInit:
2610 *
2611 * Initialize the default SAX handler
2612 */
2613void
2614docbDefaultSAXHandlerInit(void)
2615{
Daniel Veillard092643b2003-09-25 14:29:29 +00002616 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002617}
2618
2619#endif /* LIBXML_DOCB_ENABLED */