blob: 82bc4a76e58aa800ddfe9f12e9bd3ed34c3e8c87 [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 }
805 }
806 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
807 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
808 ctxt->myDoc->URL = xmlCanonicPath((const xmlChar *) ctxt->input->filename);
809 if (ctxt->myDoc->URL == NULL)
810 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
811 }
812}
813
814/**
815 * xmlSAX2EndDocument:
816 * @ctx: the user data (XML parser context)
817 *
818 * called when the document end has been detected.
819 */
820void
821xmlSAX2EndDocument(void *ctx)
822{
823 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
824#ifdef DEBUG_SAX
825 xmlGenericError(xmlGenericErrorContext,
826 "SAX.xmlSAX2EndDocument()\n");
827#endif
828 if (ctxt->validate && ctxt->wellFormed &&
829 ctxt->myDoc && ctxt->myDoc->intSubset)
830 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
831
832 /*
833 * Grab the encoding if it was added on-the-fly
834 */
835 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
836 (ctxt->myDoc->encoding == NULL)) {
837 ctxt->myDoc->encoding = ctxt->encoding;
838 ctxt->encoding = NULL;
839 }
840 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
841 (ctxt->myDoc->encoding == NULL)) {
842 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
843 }
844 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
845 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
846 ctxt->myDoc->charset = ctxt->charset;
847 }
848}
849
850/**
851 * xmlSAX2AttributeInternal:
852 * @ctx: the user data (XML parser context)
853 * @fullname: The attribute name, including namespace prefix
854 * @value: The attribute value
855 * @prefix: the prefix on the element node
856 *
857 * Handle an attribute that has been read by the parser.
858 * The default handling is to convert the attribute into an
859 * DOM subtree and past it in a new xmlAttr element added to
860 * the element.
861 */
862static void
863xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
864 const xmlChar *value, const xmlChar *prefix)
865{
866 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
867 xmlAttrPtr ret;
868 xmlChar *name;
869 xmlChar *ns;
870 xmlChar *nval;
871 xmlNsPtr namespace;
872
873 /*
874 * Split the full name into a namespace prefix and the tag name
875 */
876 name = xmlSplitQName(ctxt, fullname, &ns);
877 if ((name != NULL) && (name[0] == 0)) {
878 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
879 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
880 ctxt->sax->error(ctxt->userData,
881 "invalid namespace declaration '%s'\n", fullname);
882 } else {
883 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
884 ctxt->sax->warning(ctxt->userData,
885 "Avoid attribute ending with ':' like '%s'\n", fullname);
886 }
887 if (ns != NULL)
888 xmlFree(ns);
889 ns = NULL;
890 xmlFree(name);
891 name = xmlStrdup(fullname);
892 }
893 if (name == NULL) {
894 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
895 ctxt->sax->error(ctxt->userData,
896 "SAX.xmlSAX2StartElement(): out of memory\n");
897 ctxt->errNo = XML_ERR_NO_MEMORY;
898 ctxt->instate = XML_PARSER_EOF;
899 ctxt->disableSAX = 1;
900 if (ns != NULL)
901 xmlFree(ns);
902 return;
903 }
904
905 /*
906 * Do the last stage of the attribute normalization
907 * Needed for HTML too:
908 * http://www.w3.org/TR/html4/types.html#h-6.2
909 */
910 ctxt->vctxt.valid = 1;
911 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
912 ctxt->myDoc, ctxt->node,
913 fullname, value);
914 if (ctxt->vctxt.valid != 1) {
915 ctxt->valid = 0;
916 }
917 if (nval != NULL)
918 value = nval;
919
920 /*
921 * Check whether it's a namespace definition
922 */
923 if ((!ctxt->html) && (ns == NULL) &&
924 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
925 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
926 xmlNsPtr nsret;
927 xmlChar *val;
928
929 if (!ctxt->replaceEntities) {
930 ctxt->depth++;
931 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
932 0,0,0);
933 ctxt->depth--;
934 } else {
935 val = (xmlChar *) value;
936 }
937
938 if (val[0] != 0) {
939 xmlURIPtr uri;
940
941 uri = xmlParseURI((const char *)val);
942 if (uri == NULL) {
943 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
944 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +0000945 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000946 } else {
947 if (uri->scheme == NULL) {
948 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
949 ctxt->sax->warning(ctxt->userData,
950 "xmlns: URI %s is not absolute\n", val);
951 }
952 xmlFreeURI(uri);
953 }
954 }
955
956 /* a default namespace definition */
957 nsret = xmlNewNs(ctxt->node, val, NULL);
958
959 /*
960 * Validate also for namespace decls, they are attributes from
961 * an XML-1.0 perspective
962 */
963 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
964 ctxt->myDoc && ctxt->myDoc->intSubset)
965 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
966 ctxt->node, prefix, nsret, val);
967 if (name != NULL)
968 xmlFree(name);
969 if (nval != NULL)
970 xmlFree(nval);
971 if (val != value)
972 xmlFree(val);
973 return;
974 }
975 if ((!ctxt->html) &&
976 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
977 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
978 xmlNsPtr nsret;
979 xmlChar *val;
980
981 if (!ctxt->replaceEntities) {
982 ctxt->depth++;
983 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
984 0,0,0);
985 ctxt->depth--;
986 if (val == NULL) {
987 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
988 ctxt->sax->error(ctxt->userData,
989 "SAX.xmlSAX2StartElement(): out of memory\n");
990 ctxt->errNo = XML_ERR_NO_MEMORY;
991 ctxt->instate = XML_PARSER_EOF;
992 ctxt->disableSAX = 1;
993 xmlFree(ns);
994 if (name != NULL)
995 xmlFree(name);
996 return;
997 }
998 } else {
999 val = (xmlChar *) value;
1000 }
1001
1002 if (val[0] == 0) {
1003 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1004 ctxt->sax->error(ctxt->userData,
1005 "Empty namespace name for prefix %s\n", name);
1006 }
1007 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1008 xmlURIPtr uri;
1009
1010 uri = xmlParseURI((const char *)val);
1011 if (uri == NULL) {
1012 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1013 ctxt->sax->warning(ctxt->userData,
1014 "xmlns:%s: %s not a valid URI\n", name, value);
1015 } else {
1016 if (uri->scheme == NULL) {
1017 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1018 ctxt->sax->warning(ctxt->userData,
1019 "xmlns:%s: URI %s is not absolute\n", name, value);
1020 }
1021 xmlFreeURI(uri);
1022 }
1023 }
1024
1025 /* a standard namespace definition */
1026 nsret = xmlNewNs(ctxt->node, val, name);
1027 xmlFree(ns);
1028 /*
1029 * Validate also for namespace decls, they are attributes from
1030 * an XML-1.0 perspective
1031 */
1032 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1033 ctxt->myDoc && ctxt->myDoc->intSubset)
1034 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1035 ctxt->node, prefix, nsret, value);
1036 if (name != NULL)
1037 xmlFree(name);
1038 if (nval != NULL)
1039 xmlFree(nval);
1040 if (val != value)
1041 xmlFree(val);
1042 return;
1043 }
1044
1045 if (ns != NULL) {
1046 xmlAttrPtr prop;
1047 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillard67906942003-08-28 21:13:25 +00001048 if (namespace == NULL) {
William M. Brack4811ba32003-09-06 18:02:53 +00001049 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1050 ctxt->sax->error(ctxt->userData,
1051 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001052 ns, name);
1053 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001054
1055 prop = ctxt->node->properties;
1056 while (prop != NULL) {
1057 if (prop->ns != NULL) {
1058 if ((xmlStrEqual(name, prop->name)) &&
1059 ((namespace == prop->ns) ||
1060 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1061 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
1062 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1063 ctxt->sax->error(ctxt->userData,
1064 "Attribute %s in %s redefined\n",
1065 name, namespace->href);
1066 ctxt->wellFormed = 0;
1067 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1068 goto error;
1069 }
1070 }
1071 prop = prop->next;
1072 }
1073 } else {
1074 namespace = NULL;
1075 }
1076
1077 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1078 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1079
1080 if (ret != NULL) {
1081 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1082 xmlNodePtr tmp;
1083
1084 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1085 tmp = ret->children;
1086 while (tmp != NULL) {
1087 tmp->parent = (xmlNodePtr) ret;
1088 if (tmp->next == NULL)
1089 ret->last = tmp;
1090 tmp = tmp->next;
1091 }
1092 } else if (value != NULL) {
1093 ret->children = xmlNewDocText(ctxt->myDoc, value);
1094 ret->last = ret->children;
1095 if (ret->children != NULL)
1096 ret->children->parent = (xmlNodePtr) ret;
1097 }
1098 }
1099
1100 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1101 ctxt->myDoc && ctxt->myDoc->intSubset) {
1102
1103 /*
1104 * If we don't substitute entities, the validation should be
1105 * done on a value with replaced entities anyway.
1106 */
1107 if (!ctxt->replaceEntities) {
1108 xmlChar *val;
1109
1110 ctxt->depth++;
1111 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1112 0,0,0);
1113 ctxt->depth--;
1114
1115 if (val == NULL)
1116 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1117 ctxt->myDoc, ctxt->node, ret, value);
1118 else {
1119 xmlChar *nvalnorm;
1120
1121 /*
1122 * Do the last stage of the attribute normalization
1123 * It need to be done twice ... it's an extra burden related
1124 * to the ability to keep xmlSAX2References in attributes
1125 */
1126 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1127 ctxt->node, fullname, val);
1128 if (nvalnorm != NULL) {
1129 xmlFree(val);
1130 val = nvalnorm;
1131 }
1132
1133 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1134 ctxt->myDoc, ctxt->node, ret, val);
1135 xmlFree(val);
1136 }
1137 } else {
1138 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1139 ctxt->node, ret, value);
1140 }
1141 } else if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1142 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1143 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1144 /*
1145 * when validating, the ID registration is done at the attribute
1146 * validation level. Otherwise we have to do specific handling here.
1147 */
1148 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1149 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1150 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1151 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1152 }
1153
1154error:
1155 if (nval != NULL)
1156 xmlFree(nval);
1157 if (ns != NULL)
1158 xmlFree(ns);
1159}
1160
Daniel Veillard1af9a412003-08-20 22:54:39 +00001161/*
1162 * xmlCheckDefaultedAttributes:
1163 *
1164 * Check defaulted attributes from the DTD
1165 */
1166static void
1167xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1168 const xmlChar *prefix, const xmlChar **atts) {
1169 xmlElementPtr elemDecl;
1170 const xmlChar *att;
1171 int internal = 1;
1172 int i;
1173
1174 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1175 if (elemDecl == NULL) {
1176 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1177 internal = 0;
1178 }
1179
1180process_external_subset:
1181
1182 if (elemDecl != NULL) {
1183 xmlAttributePtr attr = elemDecl->attributes;
1184 /*
1185 * Check against defaulted attributes from the external subset
1186 * if the document is stamped as standalone
1187 */
1188 if ((ctxt->myDoc->standalone == 1) &&
1189 (ctxt->myDoc->extSubset != NULL) &&
1190 (ctxt->validate)) {
1191 while (attr != NULL) {
1192 if ((attr->defaultValue != NULL) &&
1193 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1194 attr->elem, attr->name,
1195 attr->prefix) == attr) &&
1196 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1197 attr->elem, attr->name,
1198 attr->prefix) == NULL)) {
1199 xmlChar *fulln;
1200
1201 if (attr->prefix != NULL) {
1202 fulln = xmlStrdup(attr->prefix);
1203 fulln = xmlStrcat(fulln, BAD_CAST ":");
1204 fulln = xmlStrcat(fulln, attr->name);
1205 } else {
1206 fulln = xmlStrdup(attr->name);
1207 }
1208
1209 /*
1210 * Check that the attribute is not declared in the
1211 * serialization
1212 */
1213 att = NULL;
1214 if (atts != NULL) {
1215 i = 0;
1216 att = atts[i];
1217 while (att != NULL) {
1218 if (xmlStrEqual(att, fulln))
1219 break;
1220 i += 2;
1221 att = atts[i];
1222 }
1223 }
1224 if (att == NULL) {
1225 if (ctxt->vctxt.error != NULL)
1226 ctxt->vctxt.error(ctxt->vctxt.userData,
1227 "standalone: attribute %s on %s defaulted from external subset\n",
1228 fulln, attr->elem);
1229 ctxt->valid = 0;
1230 }
1231 }
1232 attr = attr->nexth;
1233 }
1234 }
1235
1236 /*
1237 * Actually insert defaulted values when needed
1238 */
1239 attr = elemDecl->attributes;
1240 while (attr != NULL) {
1241 /*
1242 * Make sure that attributes redefinition occuring in the
1243 * internal subset are not overriden by definitions in the
1244 * external subset.
1245 */
1246 if (attr->defaultValue != NULL) {
1247 /*
1248 * the element should be instantiated in the tree if:
1249 * - this is a namespace prefix
1250 * - the user required for completion in the tree
1251 * like XSLT
1252 * - there isn't already an attribute definition
1253 * in the internal subset overriding it.
1254 */
1255 if (((attr->prefix != NULL) &&
1256 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1257 ((attr->prefix == NULL) &&
1258 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1259 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1260 xmlAttributePtr tst;
1261
1262 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1263 attr->elem, attr->name,
1264 attr->prefix);
1265 if ((tst == attr) || (tst == NULL)) {
1266 xmlChar fn[50];
1267 xmlChar *fulln;
1268
1269 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1270 if (fulln == NULL) {
1271 if ((ctxt->sax != NULL) &&
1272 (ctxt->sax->error != NULL))
1273 ctxt->sax->error(ctxt->userData,
1274 "SAX.xmlSAX2StartElement(): out of memory\n");
1275 ctxt->errNo = XML_ERR_NO_MEMORY;
1276 ctxt->instate = XML_PARSER_EOF;
1277 ctxt->disableSAX = 1;
1278 return;
1279 }
1280
1281 /*
1282 * Check that the attribute is not declared in the
1283 * serialization
1284 */
1285 att = NULL;
1286 if (atts != NULL) {
1287 i = 0;
1288 att = atts[i];
1289 while (att != NULL) {
1290 if (xmlStrEqual(att, fulln))
1291 break;
1292 i += 2;
1293 att = atts[i];
1294 }
1295 }
1296 if (att == NULL) {
1297 xmlSAX2AttributeInternal(ctxt, fulln,
1298 attr->defaultValue, prefix);
1299 }
1300 if ((fulln != fn) && (fulln != attr->name))
1301 xmlFree(fulln);
1302 }
1303 }
1304 }
1305 attr = attr->nexth;
1306 }
1307 if (internal == 1) {
1308 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1309 name, prefix);
1310 internal = 0;
1311 goto process_external_subset;
1312 }
1313 }
1314}
1315
1316/**
1317 * xmlSAX2StartElement:
1318 * @ctx: the user data (XML parser context)
1319 * @fullname: The element name, including namespace prefix
1320 * @atts: An array of name/value attributes pairs, NULL terminated
1321 *
1322 * called when an opening tag has been processed.
1323 */
1324void
1325xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1326{
1327 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1328 xmlNodePtr ret;
1329 xmlNodePtr parent = ctxt->node;
1330 xmlNsPtr ns;
1331 xmlChar *name;
1332 xmlChar *prefix;
1333 const xmlChar *att;
1334 const xmlChar *value;
1335 int i;
1336
1337#ifdef DEBUG_SAX
1338 xmlGenericError(xmlGenericErrorContext,
1339 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1340#endif
1341
1342 /*
1343 * First check on validity:
1344 */
1345 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1346 ((ctxt->myDoc->intSubset == NULL) ||
1347 ((ctxt->myDoc->intSubset->notations == NULL) &&
1348 (ctxt->myDoc->intSubset->elements == NULL) &&
1349 (ctxt->myDoc->intSubset->attributes == NULL) &&
1350 (ctxt->myDoc->intSubset->entities == NULL)))) {
1351 if (ctxt->vctxt.error != NULL) {
1352 ctxt->vctxt.error(ctxt->vctxt.userData,
1353 "Validation failed: no DTD found !\n");
1354 }
1355 ctxt->validate = 0;
1356 ctxt->valid = 0;
1357 ctxt->errNo = XML_ERR_NO_DTD;
1358 }
1359
1360
1361 /*
1362 * Split the full name into a namespace prefix and the tag name
1363 */
1364 name = xmlSplitQName(ctxt, fullname, &prefix);
1365
1366
1367 /*
1368 * Note : the namespace resolution is deferred until the end of the
1369 * attributes parsing, since local namespace can be defined as
1370 * an attribute at this level.
1371 */
1372 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1373 if (ret == NULL) {
1374 if (prefix != NULL)
1375 xmlFree(prefix);
1376 ctxt->errNo = XML_ERR_NO_MEMORY;
1377 ctxt->instate = XML_PARSER_EOF;
1378 ctxt->disableSAX = 1;
1379 return;
1380 }
1381 if (ctxt->myDoc->children == NULL) {
1382#ifdef DEBUG_SAX_TREE
1383 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1384#endif
1385 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1386 } else if (parent == NULL) {
1387 parent = ctxt->myDoc->children;
1388 }
1389 ctxt->nodemem = -1;
1390 if (ctxt->linenumbers) {
1391 if (ctxt->input != NULL)
1392 ret->content = (void *) (long) ctxt->input->line;
1393 }
1394
1395 /*
1396 * We are parsing a new node.
1397 */
1398#ifdef DEBUG_SAX_TREE
1399 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1400#endif
1401 nodePush(ctxt, ret);
1402
1403 /*
1404 * Link the child element
1405 */
1406 if (parent != NULL) {
1407 if (parent->type == XML_ELEMENT_NODE) {
1408#ifdef DEBUG_SAX_TREE
1409 xmlGenericError(xmlGenericErrorContext,
1410 "adding child %s to %s\n", name, parent->name);
1411#endif
1412 xmlAddChild(parent, ret);
1413 } else {
1414#ifdef DEBUG_SAX_TREE
1415 xmlGenericError(xmlGenericErrorContext,
1416 "adding sibling %s to ", name);
1417 xmlDebugDumpOneNode(stderr, parent, 0);
1418#endif
1419 xmlAddSibling(parent, ret);
1420 }
1421 }
1422
1423 /*
1424 * Insert all the defaulted attributes from the DTD especially namespaces
1425 */
1426 if ((!ctxt->html) &&
1427 ((ctxt->myDoc->intSubset != NULL) ||
1428 (ctxt->myDoc->extSubset != NULL))) {
1429 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1430 }
1431
1432 /*
1433 * process all the attributes whose name start with "xmlns"
1434 */
1435 if (atts != NULL) {
1436 i = 0;
1437 att = atts[i++];
1438 value = atts[i++];
1439 if (!ctxt->html) {
1440 while ((att != NULL) && (value != NULL)) {
1441 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1442 (att[3] == 'n') && (att[4] == 's'))
1443 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1444
1445 att = atts[i++];
1446 value = atts[i++];
1447 }
1448 }
1449 }
1450
1451 /*
1452 * Search the namespace, note that since the attributes have been
1453 * processed, the local namespaces are available.
1454 */
1455 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1456 if ((ns == NULL) && (parent != NULL))
1457 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1458 if ((prefix != NULL) && (ns == NULL)) {
1459 ns = xmlNewNs(ret, NULL, prefix);
1460 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1461 ctxt->sax->warning(ctxt->userData,
1462 "Namespace prefix %s is not defined\n", prefix);
1463 }
1464
1465 /*
1466 * set the namespace node, making sure that if the default namspace
1467 * is unbound on a parent we simply kee it NULL
1468 */
1469 if ((ns != NULL) && (ns->href != NULL) &&
1470 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1471 xmlSetNs(ret, ns);
1472
1473 /*
1474 * process all the other attributes
1475 */
1476 if (atts != NULL) {
1477 i = 0;
1478 att = atts[i++];
1479 value = atts[i++];
1480 if (ctxt->html) {
1481 while (att != NULL) {
1482 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1483 att = atts[i++];
1484 value = atts[i++];
1485 }
1486 } else {
1487 while ((att != NULL) && (value != NULL)) {
1488 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1489 (att[3] != 'n') || (att[4] != 's'))
1490 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1491
1492 /*
1493 * Next ones
1494 */
1495 att = atts[i++];
1496 value = atts[i++];
1497 }
1498 }
1499 }
1500
1501 /*
1502 * If it's the Document root, finish the DTD validation and
1503 * check the document root element for validity
1504 */
1505 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
1506 int chk;
1507
1508 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1509 if (chk <= 0)
1510 ctxt->valid = 0;
1511 if (chk < 0)
1512 ctxt->wellFormed = 0;
1513 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1514 ctxt->vctxt.finishDtd = 1;
1515 }
1516
1517 if (prefix != NULL)
1518 xmlFree(prefix);
1519
1520}
1521
1522/**
1523 * xmlSAX2EndElement:
1524 * @ctx: the user data (XML parser context)
1525 * @name: The element name
1526 *
1527 * called when the end of an element has been detected.
1528 */
1529void
1530xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1531{
1532 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1533 xmlParserNodeInfo node_info;
1534 xmlNodePtr cur = ctxt->node;
1535
1536#ifdef DEBUG_SAX
1537 if (name == NULL)
1538 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1539 else
1540 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1541#endif
1542
1543 /* Capture end position and add node */
1544 if (cur != NULL && ctxt->record_info) {
1545 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1546 node_info.end_line = ctxt->input->line;
1547 node_info.node = cur;
1548 xmlParserAddNodeInfo(ctxt, &node_info);
1549 }
1550 ctxt->nodemem = -1;
1551
1552 if (ctxt->validate && ctxt->wellFormed &&
1553 ctxt->myDoc && ctxt->myDoc->intSubset)
1554 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1555 cur);
1556
1557
1558 /*
1559 * end of parsing of this node.
1560 */
1561#ifdef DEBUG_SAX_TREE
1562 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1563#endif
1564 nodePop(ctxt);
1565}
1566
Daniel Veillarde57ec792003-09-10 10:50:59 +00001567/*
Daniel Veillard19895052003-09-17 13:59:32 +00001568 * xmlSAX2TextNode:
1569 * @ctxt: the parser context
1570 * @str: the input string
1571 * @len: the string length
1572 *
1573 * Remove the entities from an attribute value
1574 *
1575 * Returns the newly allocated string or NULL if not needed or error
1576 */
1577static xmlNodePtr
1578xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1579 xmlNodePtr ret;
1580
1581 if (ctxt->freeElems != NULL) {
1582 ret = ctxt->freeElems;
1583 ctxt->freeElems = ret->next;
1584 ctxt->freeElemsNr--;
1585 memset(ret, 0, sizeof(xmlNode));
1586 ret->type = XML_TEXT_NODE;
1587
1588 ret->name = xmlStringText;
1589 ret->content = xmlStrndup(str, len);
1590
1591 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1592 xmlRegisterNodeDefaultValue(ret);
1593 } else {
1594 ret = xmlNewTextLen(str, len);
1595 }
1596 if (ret == NULL) {
1597 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1598 ctxt->sax->error(ctxt->userData,
1599 "SAX.xmlSAX2Characters(): out of memory\n");
1600 ctxt->errNo = XML_ERR_NO_MEMORY;
1601 ctxt->instate = XML_PARSER_EOF;
1602 ctxt->disableSAX = 1;
1603 return(NULL);
1604 }
1605 return(ret);
1606}
1607
1608/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001609 * xmlSAX2DecodeAttrEntities:
1610 * @ctxt: the parser context
1611 * @str: the input string
1612 * @len: the string length
1613 *
1614 * Remove the entities from an attribute value
1615 *
1616 * Returns the newly allocated string or NULL if not needed or error
1617 */
1618static xmlChar *
1619xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1620 const xmlChar *end) {
1621 const xmlChar *in;
1622 xmlChar *ret;
1623
1624 in = str;
1625 while (in < end)
1626 if (*in++ == '&')
1627 goto decode;
1628 return(NULL);
1629decode:
1630 ctxt->depth++;
1631 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1632 XML_SUBSTITUTE_REF, 0,0,0);
1633 ctxt->depth--;
1634 return(ret);
1635}
1636
1637/**
1638 * xmlSAX2AttributeNs:
1639 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001640 * @localname: the local name of the attribute
1641 * @prefix: the attribute namespace prefix if available
1642 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001643 * @value: Start of the attribute value
1644 * @valueend: end of the attribute value
1645 *
1646 * Handle an attribute that has been read by the parser.
1647 * The default handling is to convert the attribute into an
1648 * DOM subtree and past it in a new xmlAttr element added to
1649 * the element.
1650 */
1651static void
1652xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1653 const xmlChar * localname,
1654 const xmlChar * prefix,
1655 const xmlChar * value,
1656 const xmlChar * valueend)
1657{
1658 xmlAttrPtr ret;
1659 xmlNsPtr namespace = NULL;
1660 xmlChar *dup = NULL;
1661
Daniel Veillarde57ec792003-09-10 10:50:59 +00001662 /*
1663 * Note: if prefix == NULL, the attribute is not in the default namespace
1664 */
1665 if (prefix != NULL)
1666 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1667
Daniel Veillard8a44e592003-09-15 14:50:06 +00001668 /*
1669 * allocate the node
1670 */
1671 if (ctxt->freeAttrs != NULL) {
1672 ret = ctxt->freeAttrs;
1673 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001674 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001675 memset(ret, 0, sizeof(xmlAttr));
1676 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001677
Daniel Veillard8a44e592003-09-15 14:50:06 +00001678 ret->parent = ctxt->node;
1679 ret->doc = ctxt->myDoc;
1680 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001681
Daniel Veillard8a44e592003-09-15 14:50:06 +00001682 if (ctxt->dictNames)
1683 ret->name = localname;
1684 else
1685 ret->name = xmlStrdup(localname);
1686
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001687 /* link at the end to preserv order, TODO speed up with a last */
1688 if (ctxt->node->properties == NULL) {
1689 ctxt->node->properties = ret;
1690 } else {
1691 xmlAttrPtr prev = ctxt->node->properties;
1692
1693 while (prev->next != NULL) prev = prev->next;
1694 prev->next = ret;
1695 ret->prev = prev;
1696 }
1697
Daniel Veillard8a44e592003-09-15 14:50:06 +00001698 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1699 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1700 } else {
1701 if (ctxt->dictNames)
1702 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1703 (xmlChar *) localname, NULL);
1704 else
1705 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1706 if (ret == NULL) {
1707 ctxt->errNo = XML_ERR_NO_MEMORY;
1708 ctxt->instate = XML_PARSER_EOF;
1709 ctxt->disableSAX = 1;
1710 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001711 }
1712 }
1713
Daniel Veillard8a44e592003-09-15 14:50:06 +00001714 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1715 xmlNodePtr tmp;
1716
Daniel Veillard19895052003-09-17 13:59:32 +00001717 /*
1718 * We know that if there is an entity reference, then
1719 * the string has been dup'ed and terminates with 0
1720 * otherwise with ' or "
1721 */
1722 if (*valueend != 0) {
1723 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1724 ret->children = tmp;
1725 ret->last = tmp;
1726 if (tmp != NULL) {
1727 tmp->doc = ret->doc;
1728 tmp->parent = (xmlNodePtr) ret;
1729 }
1730 } else {
1731 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1732 valueend - value);
1733 tmp = ret->children;
1734 while (tmp != NULL) {
1735 tmp->parent = (xmlNodePtr) ret;
1736 if (tmp->next == NULL)
1737 ret->last = tmp;
1738 tmp = tmp->next;
1739 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001740 }
1741 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00001742 xmlNodePtr tmp;
1743
1744 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1745 ret->children = tmp;
1746 ret->last = tmp;
1747 if (tmp != NULL) {
1748 tmp->doc = ret->doc;
1749 tmp->parent = (xmlNodePtr) ret;
1750 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001751 }
1752
Daniel Veillarde57ec792003-09-10 10:50:59 +00001753 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1754 ctxt->myDoc && ctxt->myDoc->intSubset) {
1755 /*
1756 * If we don't substitute entities, the validation should be
1757 * done on a value with replaced entities anyway.
1758 */
1759 if (!ctxt->replaceEntities) {
1760 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
1761 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00001762 if (*valueend == 0) {
1763 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1764 ctxt->myDoc, ctxt->node, ret, value);
1765 } else {
1766 /*
1767 * That should already be normalized.
1768 * cheaper to finally allocate here than duplicate
1769 * entry points in the full validation code
1770 */
1771 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001772
Daniel Veillard62998c02003-09-15 12:56:36 +00001773 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1774 ctxt->myDoc, ctxt->node, ret, dup);
1775 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001776 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00001777 /*
1778 * dup now contains a string of the flattened attribute
1779 * content with entities substitued. Check if we need to
1780 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001781 * It need to be done twice ... it's an extra burden related
1782 * to the ability to keep references in attributes
1783 */
Daniel Veillard62998c02003-09-15 12:56:36 +00001784 if (ctxt->attsSpecial != NULL) {
1785 xmlChar *nvalnorm;
1786 xmlChar fn[50];
1787 xmlChar *fullname;
1788
1789 fullname = xmlBuildQName(localname, prefix, fn, 50);
1790 if (fullname != NULL) {
1791 ctxt->vctxt.valid = 1;
1792 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
1793 &ctxt->vctxt, ctxt->myDoc,
1794 ctxt->node, fullname, dup);
1795 if (ctxt->vctxt.valid != 1)
1796 ctxt->valid = 0;
1797
1798 if ((fullname != fn) && (fullname != localname))
1799 xmlFree(fullname);
1800 if (nvalnorm != NULL) {
1801 xmlFree(dup);
1802 dup = nvalnorm;
1803 }
1804 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001805 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001806
1807 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1808 ctxt->myDoc, ctxt->node, ret, dup);
1809 }
1810 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001811 /*
1812 * if entities already have been substitued, then
1813 * the attribute as passed is already normalized
1814 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001815 dup = xmlStrndup(value, valueend - value);
1816
1817 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1818 ctxt->myDoc, ctxt->node, ret, dup);
1819 }
1820 } else if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1821 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1822 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1823 /*
1824 * when validating, the ID registration is done at the attribute
1825 * validation level. Otherwise we have to do specific handling here.
1826 */
1827 if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
1828 /* might be worth duplicate entry points and not copy */
1829 if (dup == NULL)
1830 dup = xmlStrndup(value, valueend - value);
1831 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1832 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
1833 if (dup == NULL)
1834 dup = xmlStrndup(value, valueend - value);
1835 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1836 }
1837 }
1838 if (dup != NULL)
1839 xmlFree(dup);
1840}
1841
1842/**
1843 * xmlSAX2StartElementNs:
1844 * @ctx: the user data (XML parser context)
1845 * @localname: the local name of the element
1846 * @prefix: the element namespace prefix if available
1847 * @URI: the element namespace name if available
1848 * @nb_namespaces: number of namespace definitions on that node
1849 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
1850 * @nb_attributes: the number of attributes on that node
1851 * nb_defaulted: the number of defaulted attributes.
1852 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
1853 * attribute values.
1854 *
1855 * SAX2 callback when an element start has been detected by the parser.
1856 * It provides the namespace informations for the element, as well as
1857 * the new namespace declarations on the element.
1858 */
1859void
1860xmlSAX2StartElementNs(void *ctx,
1861 const xmlChar *localname,
1862 const xmlChar *prefix,
1863 const xmlChar *URI,
1864 int nb_namespaces,
1865 const xmlChar **namespaces,
1866 int nb_attributes,
1867 int nb_defaulted,
1868 const xmlChar **attributes)
1869{
1870 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1871 xmlNodePtr ret;
1872 xmlNodePtr parent = ctxt->node;
1873 xmlNsPtr last = NULL, ns;
1874 const xmlChar *uri, *pref;
1875 int i, j;
1876
1877 /*
1878 * First check on validity:
1879 */
1880 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1881 ((ctxt->myDoc->intSubset == NULL) ||
1882 ((ctxt->myDoc->intSubset->notations == NULL) &&
1883 (ctxt->myDoc->intSubset->elements == NULL) &&
1884 (ctxt->myDoc->intSubset->attributes == NULL) &&
1885 (ctxt->myDoc->intSubset->entities == NULL)))) {
1886 if (ctxt->vctxt.error != NULL) {
1887 ctxt->vctxt.error(ctxt->vctxt.userData,
1888 "Validation failed: no DTD found !\n");
1889 }
1890 ctxt->validate = 0;
1891 ctxt->valid = 0;
1892 ctxt->errNo = XML_ERR_NO_DTD;
1893 }
1894
Daniel Veillard8a44e592003-09-15 14:50:06 +00001895 /*
1896 * allocate the node
1897 */
1898 if (ctxt->freeElems != NULL) {
1899 ret = ctxt->freeElems;
1900 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001901 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001902 memset(ret, 0, sizeof(xmlNode));
1903 ret->type = XML_ELEMENT_NODE;
1904
1905 if (ctxt->dictNames)
1906 ret->name = localname;
1907 else
1908 ret->name = xmlStrdup(localname);
1909
1910 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1911 xmlRegisterNodeDefaultValue(ret);
1912 } else {
1913 if (ctxt->dictNames)
1914 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
1915 (xmlChar *) localname, NULL);
1916 else
1917 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
1918 if (ret == NULL) {
1919 ctxt->errNo = XML_ERR_NO_MEMORY;
1920 ctxt->instate = XML_PARSER_EOF;
1921 ctxt->disableSAX = 1;
1922 return;
1923 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001924 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001925
Daniel Veillarde57ec792003-09-10 10:50:59 +00001926 if (ctxt->myDoc->children == NULL) {
1927 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1928 } else if (parent == NULL) {
1929 parent = ctxt->myDoc->children;
1930 }
1931 /*
1932 * Build the namespace list
1933 */
1934 for (i = 0,j = 0;j < nb_namespaces;j++) {
1935 pref = namespaces[i++];
1936 uri = namespaces[i++];
1937 ns = xmlNewNs(NULL, uri, pref);
1938 if (ns != NULL) {
1939 if (last == NULL) {
1940 ret->nsDef = last = ns;
1941 } else {
1942 last->next = ns;
1943 last = ns;
1944 }
1945 if ((URI != NULL) && (prefix == pref))
1946 ret->ns = ns;
1947 } else {
1948 ctxt->errNo = XML_ERR_NO_MEMORY;
1949 ctxt->instate = XML_PARSER_EOF;
1950 ctxt->disableSAX = 1;
1951 return;
1952 }
1953 }
1954 ctxt->nodemem = -1;
1955 if (ctxt->linenumbers) {
1956 if (ctxt->input != NULL)
1957 ret->content = (void *) (long) ctxt->input->line;
1958 }
1959
1960 /*
1961 * We are parsing a new node.
1962 */
1963 nodePush(ctxt, ret);
1964
1965 /*
1966 * Link the child element
1967 */
1968 if (parent != NULL) {
1969 if (parent->type == XML_ELEMENT_NODE) {
1970 xmlAddChild(parent, ret);
1971 } else {
1972 xmlAddSibling(parent, ret);
1973 }
1974 }
1975
1976 /*
1977 * Insert the defaulted attributes from the DTD only if requested:
1978 */
1979 if ((nb_defaulted != 0) &&
1980 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
1981 nb_attributes -= nb_defaulted;
1982
1983 /*
1984 * Search the namespace if it wasn't already found
1985 */
1986 if ((URI != NULL) && (ret->ns == NULL)) {
1987 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1988 if (ret->ns == NULL) {
1989 ns = xmlNewNs(ret, NULL, prefix);
1990 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1991 ctxt->sax->warning(ctxt->userData,
1992 "Namespace prefix %s was not found\n", prefix);
1993 }
1994 }
1995
1996 /*
1997 * process all the other attributes
1998 */
1999 if (nb_attributes > 0) {
2000 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2001 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2002 attributes[j+3], attributes[j+4]);
2003 }
2004 }
2005
2006 /*
2007 * If it's the Document root, finish the DTD validation and
2008 * check the document root element for validity
2009 */
2010 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
2011 int chk;
2012
2013 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2014 if (chk <= 0)
2015 ctxt->valid = 0;
2016 if (chk < 0)
2017 ctxt->wellFormed = 0;
2018 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2019 ctxt->vctxt.finishDtd = 1;
2020 }
2021}
2022
2023/**
2024 * xmlSAX2EndElementNs:
2025 * @ctx: the user data (XML parser context)
2026 * @localname: the local name of the element
2027 * @prefix: the element namespace prefix if available
2028 * @URI: the element namespace name if available
2029 *
2030 * SAX2 callback when an element end has been detected by the parser.
2031 * It provides the namespace informations for the element.
2032 */
2033void
2034xmlSAX2EndElementNs(void *ctx,
2035 const xmlChar * localname ATTRIBUTE_UNUSED,
2036 const xmlChar * prefix ATTRIBUTE_UNUSED,
2037 const xmlChar * URI ATTRIBUTE_UNUSED)
2038{
2039 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2040 xmlParserNodeInfo node_info;
2041 xmlNodePtr cur = ctxt->node;
2042
2043 /* Capture end position and add node */
2044 if ((ctxt->record_info) && (cur != NULL)) {
2045 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2046 node_info.end_line = ctxt->input->line;
2047 node_info.node = cur;
2048 xmlParserAddNodeInfo(ctxt, &node_info);
2049 }
2050 ctxt->nodemem = -1;
2051
2052 if (ctxt->validate && ctxt->wellFormed &&
2053 ctxt->myDoc && ctxt->myDoc->intSubset)
2054 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
2055
2056 /*
2057 * end of parsing of this node.
2058 */
2059 nodePop(ctxt);
2060}
2061
Daniel Veillard1af9a412003-08-20 22:54:39 +00002062/**
2063 * xmlSAX2Reference:
2064 * @ctx: the user data (XML parser context)
2065 * @name: The entity name
2066 *
2067 * called when an entity xmlSAX2Reference is detected.
2068 */
2069void
2070xmlSAX2Reference(void *ctx, const xmlChar *name)
2071{
2072 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2073 xmlNodePtr ret;
2074
2075#ifdef DEBUG_SAX
2076 xmlGenericError(xmlGenericErrorContext,
2077 "SAX.xmlSAX2Reference(%s)\n", name);
2078#endif
2079 if (name[0] == '#')
2080 ret = xmlNewCharRef(ctxt->myDoc, name);
2081 else
2082 ret = xmlNewReference(ctxt->myDoc, name);
2083#ifdef DEBUG_SAX_TREE
2084 xmlGenericError(xmlGenericErrorContext,
2085 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2086#endif
2087 xmlAddChild(ctxt->node, ret);
2088}
2089
2090/**
2091 * xmlSAX2Characters:
2092 * @ctx: the user data (XML parser context)
2093 * @ch: a xmlChar string
2094 * @len: the number of xmlChar
2095 *
2096 * receiving some chars from the parser.
2097 */
2098void
2099xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2100{
2101 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2102 xmlNodePtr lastChild;
2103
2104#ifdef DEBUG_SAX
2105 xmlGenericError(xmlGenericErrorContext,
2106 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2107#endif
2108 /*
2109 * Handle the data if any. If there is no child
2110 * add it as content, otherwise if the last child is text,
2111 * concatenate it, else create a new node of type text.
2112 */
2113
2114 if (ctxt->node == NULL) {
2115#ifdef DEBUG_SAX_TREE
2116 xmlGenericError(xmlGenericErrorContext,
2117 "add chars: ctxt->node == NULL !\n");
2118#endif
2119 return;
2120 }
Daniel Veillard19895052003-09-17 13:59:32 +00002121 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002122#ifdef DEBUG_SAX_TREE
2123 xmlGenericError(xmlGenericErrorContext,
2124 "add chars to %s \n", ctxt->node->name);
2125#endif
2126
2127 /*
2128 * Here we needed an accelerator mechanism in case of very large
2129 * elements. Use an attribute in the structure !!!
2130 */
2131 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002132 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2133 if (lastChild != NULL) {
2134 ctxt->node->children = lastChild;
2135 ctxt->node->last = lastChild;
2136 lastChild->parent = ctxt->node;
2137 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002138 ctxt->nodelen = len;
2139 ctxt->nodemem = len + 1;
2140 }
2141 } else {
2142 int coalesceText = (lastChild != NULL) &&
2143 (lastChild->type == XML_TEXT_NODE) &&
2144 (lastChild->name == xmlStringText);
2145 if ((coalesceText) && (ctxt->nodemem != 0)) {
2146 /*
2147 * The whole point of maintaining nodelen and nodemem,
2148 * xmlTextConcat is too costly, i.e. compute length,
2149 * reallocate a new buffer, move data, append ch. Here
2150 * We try to minimaze realloc() uses and avoid copying
2151 * and recomputing length over and over.
2152 */
2153 if (ctxt->nodelen + len >= ctxt->nodemem) {
2154 xmlChar *newbuf;
2155 int size;
2156
2157 size = ctxt->nodemem + len;
2158 size *= 2;
2159 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2160 if (newbuf == NULL) {
2161 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2162 ctxt->sax->error(ctxt->userData,
2163 "SAX.xmlSAX2Characters(): out of memory\n");
2164 ctxt->errNo = XML_ERR_NO_MEMORY;
2165 ctxt->instate = XML_PARSER_EOF;
2166 ctxt->disableSAX = 1;
2167 return;
2168 }
2169 ctxt->nodemem = size;
2170 lastChild->content = newbuf;
2171 }
2172 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2173 ctxt->nodelen += len;
2174 lastChild->content[ctxt->nodelen] = 0;
2175 } else if (coalesceText) {
2176 if (xmlTextConcat(lastChild, ch, len)) {
2177 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2178 ctxt->sax->error(ctxt->userData,
2179 "SAX.xmlSAX2Characters(): out of memory\n");
2180 ctxt->errNo = XML_ERR_NO_MEMORY;
2181 ctxt->instate = XML_PARSER_EOF;
2182 ctxt->disableSAX = 1;
2183 }
2184 if (ctxt->node->children != NULL) {
2185 ctxt->nodelen = xmlStrlen(lastChild->content);
2186 ctxt->nodemem = ctxt->nodelen + 1;
2187 }
2188 } else {
2189 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002190 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2191 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002192 xmlAddChild(ctxt->node, lastChild);
2193 if (ctxt->node->children != NULL) {
2194 ctxt->nodelen = len;
2195 ctxt->nodemem = len + 1;
2196 }
2197 }
2198 }
2199 }
2200}
2201
2202/**
2203 * xmlSAX2IgnorableWhitespace:
2204 * @ctx: the user data (XML parser context)
2205 * @ch: a xmlChar string
2206 * @len: the number of xmlChar
2207 *
2208 * receiving some ignorable whitespaces from the parser.
2209 * UNUSED: by default the DOM building will use xmlSAX2Characters
2210 */
2211void
2212xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2213{
2214 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2215#ifdef DEBUG_SAX
2216 xmlGenericError(xmlGenericErrorContext,
2217 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2218#endif
2219}
2220
2221/**
2222 * xmlSAX2ProcessingInstruction:
2223 * @ctx: the user data (XML parser context)
2224 * @target: the target name
2225 * @data: the PI data's
2226 *
2227 * A processing instruction has been parsed.
2228 */
2229void
2230xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2231 const xmlChar *data)
2232{
2233 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2234 xmlNodePtr ret;
2235 xmlNodePtr parent = ctxt->node;
2236
2237#ifdef DEBUG_SAX
2238 xmlGenericError(xmlGenericErrorContext,
2239 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2240#endif
2241
2242 ret = xmlNewPI(target, data);
2243 if (ret == NULL) return;
2244 parent = ctxt->node;
2245
2246 if (ctxt->inSubset == 1) {
2247 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2248 return;
2249 } else if (ctxt->inSubset == 2) {
2250 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2251 return;
2252 }
2253 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2254#ifdef DEBUG_SAX_TREE
2255 xmlGenericError(xmlGenericErrorContext,
2256 "Setting PI %s as root\n", target);
2257#endif
2258 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2259 return;
2260 }
2261 if (parent->type == XML_ELEMENT_NODE) {
2262#ifdef DEBUG_SAX_TREE
2263 xmlGenericError(xmlGenericErrorContext,
2264 "adding PI %s child to %s\n", target, parent->name);
2265#endif
2266 xmlAddChild(parent, ret);
2267 } else {
2268#ifdef DEBUG_SAX_TREE
2269 xmlGenericError(xmlGenericErrorContext,
2270 "adding PI %s sibling to ", target);
2271 xmlDebugDumpOneNode(stderr, parent, 0);
2272#endif
2273 xmlAddSibling(parent, ret);
2274 }
2275}
2276
2277/**
2278 * xmlSAX2Comment:
2279 * @ctx: the user data (XML parser context)
2280 * @value: the xmlSAX2Comment content
2281 *
2282 * A xmlSAX2Comment has been parsed.
2283 */
2284void
2285xmlSAX2Comment(void *ctx, const xmlChar *value)
2286{
2287 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2288 xmlNodePtr ret;
2289 xmlNodePtr parent = ctxt->node;
2290
2291#ifdef DEBUG_SAX
2292 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2293#endif
2294 ret = xmlNewDocComment(ctxt->myDoc, value);
2295 if (ret == NULL) return;
2296
2297 if (ctxt->inSubset == 1) {
2298 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2299 return;
2300 } else if (ctxt->inSubset == 2) {
2301 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2302 return;
2303 }
2304 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2305#ifdef DEBUG_SAX_TREE
2306 xmlGenericError(xmlGenericErrorContext,
2307 "Setting xmlSAX2Comment as root\n");
2308#endif
2309 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2310 return;
2311 }
2312 if (parent->type == XML_ELEMENT_NODE) {
2313#ifdef DEBUG_SAX_TREE
2314 xmlGenericError(xmlGenericErrorContext,
2315 "adding xmlSAX2Comment child to %s\n", parent->name);
2316#endif
2317 xmlAddChild(parent, ret);
2318 } else {
2319#ifdef DEBUG_SAX_TREE
2320 xmlGenericError(xmlGenericErrorContext,
2321 "adding xmlSAX2Comment sibling to ");
2322 xmlDebugDumpOneNode(stderr, parent, 0);
2323#endif
2324 xmlAddSibling(parent, ret);
2325 }
2326}
2327
2328/**
2329 * xmlSAX2CDataBlock:
2330 * @ctx: the user data (XML parser context)
2331 * @value: The pcdata content
2332 * @len: the block length
2333 *
2334 * called when a pcdata block has been parsed
2335 */
2336void
2337xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2338{
2339 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2340 xmlNodePtr ret, lastChild;
2341
2342#ifdef DEBUG_SAX
2343 xmlGenericError(xmlGenericErrorContext,
2344 "SAX.pcdata(%.10s, %d)\n", value, len);
2345#endif
2346 lastChild = xmlGetLastChild(ctxt->node);
2347#ifdef DEBUG_SAX_TREE
2348 xmlGenericError(xmlGenericErrorContext,
2349 "add chars to %s \n", ctxt->node->name);
2350#endif
2351 if ((lastChild != NULL) &&
2352 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2353 xmlTextConcat(lastChild, value, len);
2354 } else {
2355 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2356 xmlAddChild(ctxt->node, ret);
2357 }
2358}
2359
Daniel Veillard62998c02003-09-15 12:56:36 +00002360static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002361
Daniel Veillarde57ec792003-09-10 10:50:59 +00002362/**
2363 * xmlSAXDefaultVersion:
2364 * @version: the version, 1 or 2
2365 *
2366 * Set the default version of SAX used globally by the library.
2367 * Note that this may not be a good thing to do from a library
2368 * it is better to use xmlSAXVersion() to set up specifically the
2369 * version for a given parsing context.
2370 *
2371 * Returns the previous value in case of success and -1 in case of error.
2372 */
2373int
2374xmlSAXDefaultVersion(int version)
2375{
2376 int ret = xmlSAX2DefaultVersionValue;
2377
2378 if ((version != 1) && (version != 2))
2379 return(-1);
2380 xmlSAX2DefaultVersionValue = version;
2381 if (version == 1) {
2382 xmlDefaultSAXHandler.startElement = xmlSAX2StartElement;
2383 xmlDefaultSAXHandler.endElement = xmlSAX2EndElement;
2384 xmlDefaultSAXHandler.startElementNs = NULL;
2385 xmlDefaultSAXHandler.endElementNs = NULL;
2386 } else if (version == 2) {
2387 xmlDefaultSAXHandler.startElement = NULL;
2388 xmlDefaultSAXHandler.endElement = NULL;
2389 xmlDefaultSAXHandler.startElementNs = xmlSAX2StartElementNs;
2390 xmlDefaultSAXHandler.endElementNs = xmlSAX2EndElementNs;
2391 }
2392 return(ret);
2393}
2394
2395/**
2396 * xmlSAXVersion:
2397 * @hdlr: the SAX handler
2398 * @version: the version, 1 or 2
2399 *
2400 * Initialize the default XML SAX handler according to the version
2401 *
2402 * Returns 0 in case of success and -1 in case of error.
2403 */
2404int
2405xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2406{
2407 if (hdlr == NULL) return(-1);
2408 if (version == 1) {
2409 hdlr->startElement = xmlSAX2StartElement;
2410 hdlr->endElement = xmlSAX2EndElement;
2411 hdlr->startElementNs = NULL;
2412 hdlr->endElementNs = NULL;
2413 } else if (version == 2) {
2414 hdlr->startElement = NULL;
2415 hdlr->endElement = NULL;
2416 hdlr->startElementNs = xmlSAX2StartElementNs;
2417 hdlr->endElementNs = xmlSAX2EndElementNs;
2418 } else
2419 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002420 hdlr->internalSubset = xmlSAX2InternalSubset;
2421 hdlr->externalSubset = xmlSAX2ExternalSubset;
2422 hdlr->isStandalone = xmlSAX2IsStandalone;
2423 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2424 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2425 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2426 hdlr->getEntity = xmlSAX2GetEntity;
2427 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2428 hdlr->entityDecl = xmlSAX2EntityDecl;
2429 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2430 hdlr->elementDecl = xmlSAX2ElementDecl;
2431 hdlr->notationDecl = xmlSAX2NotationDecl;
2432 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2433 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2434 hdlr->startDocument = xmlSAX2StartDocument;
2435 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002436 hdlr->reference = xmlSAX2Reference;
2437 hdlr->characters = xmlSAX2Characters;
2438 hdlr->cdataBlock = xmlSAX2CDataBlock;
2439 hdlr->ignorableWhitespace = xmlSAX2Characters;
2440 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2441 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002442 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002443 hdlr->error = xmlParserError;
2444 hdlr->fatalError = xmlParserError;
2445
2446 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002447 return(0);
2448}
2449
2450/**
2451 * xmlSAX2InitDefaultSAXHandler:
2452 * @hdlr: the SAX handler
2453 * @warning: flag if non-zero sets the handler warning procedure
2454 *
2455 * Initialize the default XML SAX2 handler
2456 */
2457void
2458xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2459{
2460 if ((hdlr == NULL) || (hdlr->initialized != 0))
2461 return;
2462
2463 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2464 if (warning == 0)
2465 hdlr->warning = NULL;
2466 else
2467 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002468}
2469
2470/**
2471 * xmlDefaultSAXHandlerInit:
2472 *
2473 * Initialize the default SAX2 handler
2474 */
2475void
2476xmlDefaultSAXHandlerInit(void)
2477{
2478 xmlSAX2InitDefaultSAXHandler(&xmlDefaultSAXHandler,
2479 xmlGetWarningsDefaultValue);
2480}
2481
2482#ifdef LIBXML_HTML_ENABLED
2483
2484/**
2485 * xmlSAX2InitHtmlDefaultSAXHandler:
2486 * @hdlr: the SAX handler
2487 *
2488 * Initialize the default HTML SAX2 handler
2489 */
2490void
2491xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2492{
2493 if(hdlr->initialized != 0)
2494 return;
2495
2496 hdlr->internalSubset = xmlSAX2InternalSubset;
2497 hdlr->externalSubset = NULL;
2498 hdlr->isStandalone = NULL;
2499 hdlr->hasInternalSubset = NULL;
2500 hdlr->hasExternalSubset = NULL;
2501 hdlr->resolveEntity = NULL;
2502 hdlr->getEntity = xmlSAX2GetEntity;
2503 hdlr->getParameterEntity = NULL;
2504 hdlr->entityDecl = NULL;
2505 hdlr->attributeDecl = NULL;
2506 hdlr->elementDecl = NULL;
2507 hdlr->notationDecl = NULL;
2508 hdlr->unparsedEntityDecl = NULL;
2509 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2510 hdlr->startDocument = xmlSAX2StartDocument;
2511 hdlr->endDocument = xmlSAX2EndDocument;
2512 hdlr->startElement = xmlSAX2StartElement;
2513 hdlr->endElement = xmlSAX2EndElement;
2514 hdlr->reference = NULL;
2515 hdlr->characters = xmlSAX2Characters;
2516 hdlr->cdataBlock = xmlSAX2CDataBlock;
2517 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2518 hdlr->processingInstruction = NULL;
2519 hdlr->comment = xmlSAX2Comment;
2520 hdlr->warning = xmlParserWarning;
2521 hdlr->error = xmlParserError;
2522 hdlr->fatalError = xmlParserError;
2523
2524 hdlr->initialized = XML_SAX2_MAGIC;
2525}
2526
2527/**
2528 * htmlDefaultSAXHandlerInit:
2529 *
2530 * Initialize the default SAX handler
2531 */
2532void
2533htmlDefaultSAXHandlerInit(void)
2534{
2535 xmlSAX2InitHtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
2536}
2537
2538#endif /* LIBXML_HTML_ENABLED */
2539
2540#ifdef LIBXML_DOCB_ENABLED
2541
2542/**
2543 * xmlSAX2InitDocbDefaultSAXHandler:
2544 * @hdlr: the SAX handler
2545 *
2546 * Initialize the default DocBook SAX2 handler
2547 */
2548void
2549xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2550{
2551 if(hdlr->initialized != 0)
2552 return;
2553
2554 hdlr->internalSubset = xmlSAX2InternalSubset;
2555 hdlr->externalSubset = NULL;
2556 hdlr->isStandalone = xmlSAX2IsStandalone;
2557 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2558 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2559 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2560 hdlr->getEntity = xmlSAX2GetEntity;
2561 hdlr->getParameterEntity = NULL;
2562 hdlr->entityDecl = xmlSAX2EntityDecl;
2563 hdlr->attributeDecl = NULL;
2564 hdlr->elementDecl = NULL;
2565 hdlr->notationDecl = NULL;
2566 hdlr->unparsedEntityDecl = NULL;
2567 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2568 hdlr->startDocument = xmlSAX2StartDocument;
2569 hdlr->endDocument = xmlSAX2EndDocument;
2570 hdlr->startElement = xmlSAX2StartElement;
2571 hdlr->endElement = xmlSAX2EndElement;
2572 hdlr->reference = xmlSAX2Reference;
2573 hdlr->characters = xmlSAX2Characters;
2574 hdlr->cdataBlock = NULL;
2575 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2576 hdlr->processingInstruction = NULL;
2577 hdlr->comment = xmlSAX2Comment;
2578 hdlr->warning = xmlParserWarning;
2579 hdlr->error = xmlParserError;
2580 hdlr->fatalError = xmlParserError;
2581
2582 hdlr->initialized = XML_SAX2_MAGIC;
2583}
2584
2585/**
2586 * docbDefaultSAXHandlerInit:
2587 *
2588 * Initialize the default SAX handler
2589 */
2590void
2591docbDefaultSAXHandlerInit(void)
2592{
2593 xmlSAX2InitDocbDefaultSAXHandler(&docbDefaultSAXHandler);
2594}
2595
2596#endif /* LIBXML_DOCB_ENABLED */