blob: e48b1493b52b80620bd2880e184267aaa91ec2ca [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * SAX.c : Default SAX handler to build a tree.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * Daniel Veillard <daniel@veillard.com>
Owen Taylor3473f882001-02-23 17:55:21 +00007 */
8
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012#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>
Daniel Veillard48da9102001-08-07 01:10:10 +000025#include <libxml/valid.h>
Owen Taylor3473f882001-02-23 17:55:21 +000026#include <libxml/HTMLtree.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000027#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000028
29/* #define DEBUG_SAX */
30/* #define DEBUG_SAX_TREE */
31
32/**
33 * getPublicId:
34 * @ctx: the user data (XML parser context)
35 *
36 * Return the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
37 *
38 * Returns a xmlChar *
39 */
40const xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +000041getPublicId(void *ctx ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +000042{
43 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
44 return(NULL);
45}
46
47/**
48 * getSystemId:
49 * @ctx: the user data (XML parser context)
50 *
51 * Return the system ID, basically URL or filename e.g.
52 * http://www.sgmlsource.com/dtds/memo.dtd
53 *
54 * Returns a xmlChar *
55 */
56const xmlChar *
57getSystemId(void *ctx)
58{
59 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard56a4cb82001-03-24 17:00:36 +000060 return((const xmlChar *) ctxt->input->filename);
Owen Taylor3473f882001-02-23 17:55:21 +000061}
62
63/**
64 * getLineNumber:
65 * @ctx: the user data (XML parser context)
66 *
67 * Return the line number of the current parsing point.
68 *
69 * Returns an int
70 */
71int
72getLineNumber(void *ctx)
73{
74 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
75 return(ctxt->input->line);
76}
77
78/**
79 * getColumnNumber:
80 * @ctx: the user data (XML parser context)
81 *
82 * Return the column number of the current parsing point.
83 *
84 * Returns an int
85 */
86int
87getColumnNumber(void *ctx)
88{
89 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
90 return(ctxt->input->col);
91}
92
Owen Taylor3473f882001-02-23 17:55:21 +000093/**
94 * isStandalone:
95 * @ctx: the user data (XML parser context)
96 *
97 * Is this document tagged standalone ?
98 *
99 * Returns 1 if true
100 */
101int
102isStandalone(void *ctx)
103{
104 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
105 return(ctxt->myDoc->standalone == 1);
106}
107
108/**
109 * hasInternalSubset:
110 * @ctx: the user data (XML parser context)
111 *
112 * Does this document has an internal subset
113 *
114 * Returns 1 if true
115 */
116int
117hasInternalSubset(void *ctx)
118{
119 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
120 return(ctxt->myDoc->intSubset != NULL);
121}
122
123/**
124 * hasExternalSubset:
125 * @ctx: the user data (XML parser context)
126 *
127 * Does this document has an external subset
128 *
129 * Returns 1 if true
130 */
131int
132hasExternalSubset(void *ctx)
133{
134 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
135 return(ctxt->myDoc->extSubset != NULL);
136}
137
138/**
139 * internalSubset:
140 * @ctx: the user data (XML parser context)
141 * @name: the root element name
142 * @ExternalID: the external ID
143 * @SystemID: the SYSTEM ID (e.g. filename or URL)
144 *
145 * Callback on internal subset declaration.
146 */
147void
148internalSubset(void *ctx, const xmlChar *name,
149 const xmlChar *ExternalID, const xmlChar *SystemID)
150{
151 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
152 xmlDtdPtr dtd;
153#ifdef DEBUG_SAX
154 xmlGenericError(xmlGenericErrorContext,
155 "SAX.internalSubset(%s, %s, %s)\n",
156 name, ExternalID, SystemID);
157#endif
158
159 if (ctxt->myDoc == NULL)
160 return;
161 dtd = xmlGetIntSubset(ctxt->myDoc);
162 if (dtd != NULL) {
163 if (ctxt->html)
164 return;
165 xmlUnlinkNode((xmlNodePtr) dtd);
166 xmlFreeDtd(dtd);
167 ctxt->myDoc->intSubset = NULL;
168 }
169 ctxt->myDoc->intSubset =
170 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
171}
172
173/**
174 * externalSubset:
175 * @ctx: the user data (XML parser context)
176 * @name: the root element name
177 * @ExternalID: the external ID
178 * @SystemID: the SYSTEM ID (e.g. filename or URL)
179 *
180 * Callback on external subset declaration.
181 */
182void
183externalSubset(void *ctx, const xmlChar *name,
184 const xmlChar *ExternalID, const xmlChar *SystemID)
185{
186 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
187#ifdef DEBUG_SAX
188 xmlGenericError(xmlGenericErrorContext,
189 "SAX.externalSubset(%s, %s, %s)\n",
190 name, ExternalID, SystemID);
191#endif
192 if (((ExternalID != NULL) || (SystemID != NULL)) &&
Daniel Veillard9403a042001-05-28 11:00:53 +0000193 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000194 (ctxt->wellFormed && ctxt->myDoc))) {
195 /*
196 * Try to fetch and parse the external subset.
197 */
198 xmlParserInputPtr oldinput;
199 int oldinputNr;
200 int oldinputMax;
201 xmlParserInputPtr *oldinputTab;
Owen Taylor3473f882001-02-23 17:55:21 +0000202 xmlParserInputPtr input = NULL;
203 xmlCharEncoding enc;
204 int oldcharset;
205
206 /*
207 * Ask the Entity resolver to load the damn thing
208 */
209 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
210 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
211 SystemID);
212 if (input == NULL) {
213 return;
214 }
215
216 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
217
218 /*
219 * make sure we won't destroy the main document context
220 */
221 oldinput = ctxt->input;
222 oldinputNr = ctxt->inputNr;
223 oldinputMax = ctxt->inputMax;
224 oldinputTab = ctxt->inputTab;
Owen Taylor3473f882001-02-23 17:55:21 +0000225 oldcharset = ctxt->charset;
226
227 ctxt->inputTab = (xmlParserInputPtr *)
228 xmlMalloc(5 * sizeof(xmlParserInputPtr));
229 if (ctxt->inputTab == NULL) {
230 ctxt->errNo = XML_ERR_NO_MEMORY;
231 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
232 ctxt->sax->error(ctxt->userData,
233 "externalSubset: out of memory\n");
234 ctxt->errNo = XML_ERR_NO_MEMORY;
235 ctxt->input = oldinput;
236 ctxt->inputNr = oldinputNr;
237 ctxt->inputMax = oldinputMax;
238 ctxt->inputTab = oldinputTab;
239 ctxt->charset = oldcharset;
240 return;
241 }
242 ctxt->inputNr = 0;
243 ctxt->inputMax = 5;
244 ctxt->input = NULL;
245 xmlPushInput(ctxt, input);
246
247 /*
248 * On the fly encoding conversion if needed
249 */
250 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
251 xmlSwitchEncoding(ctxt, enc);
252
253 if (input->filename == NULL)
254 input->filename = (char *) xmlStrdup(SystemID);
255 input->line = 1;
256 input->col = 1;
257 input->base = ctxt->input->cur;
258 input->cur = ctxt->input->cur;
259 input->free = NULL;
260
261 /*
262 * let's parse that entity knowing it's an external subset.
263 */
264 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
265
266 /*
267 * Free up the external entities
268 */
269
270 while (ctxt->inputNr > 1)
271 xmlPopInput(ctxt);
272 xmlFreeInputStream(ctxt->input);
273 xmlFree(ctxt->inputTab);
274
275 /*
276 * Restore the parsing context of the main entity
277 */
278 ctxt->input = oldinput;
279 ctxt->inputNr = oldinputNr;
280 ctxt->inputMax = oldinputMax;
281 ctxt->inputTab = oldinputTab;
282 ctxt->charset = oldcharset;
283 /* ctxt->wellFormed = oldwellFormed; */
284 }
285}
286
287/**
288 * resolveEntity:
289 * @ctx: the user data (XML parser context)
290 * @publicId: The public ID of the entity
291 * @systemId: The system ID of the entity
292 *
293 * The entity loader, to control the loading of external entities,
294 * the application can either:
295 * - override this resolveEntity() callback in the SAX block
296 * - or better use the xmlSetExternalEntityLoader() function to
297 * set up it's own entity resolution routine
298 *
299 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
300 */
301xmlParserInputPtr
302resolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
303{
304 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
305 xmlParserInputPtr ret;
306 xmlChar *URI;
307 const char *base = NULL;
308
309 if (ctxt->input != NULL)
310 base = ctxt->input->filename;
311 if (base == NULL)
312 base = ctxt->directory;
313
314 URI = xmlBuildURI(systemId, (const xmlChar *) base);
315
316#ifdef DEBUG_SAX
317 xmlGenericError(xmlGenericErrorContext,
318 "SAX.resolveEntity(%s, %s)\n", publicId, systemId);
319#endif
320
321 ret = xmlLoadExternalEntity((const char *) URI,
322 (const char *) publicId, ctxt);
323 if (URI != NULL)
324 xmlFree(URI);
325 return(ret);
326}
327
328/**
329 * getEntity:
330 * @ctx: the user data (XML parser context)
331 * @name: The entity name
332 *
333 * Get an entity by name
334 *
335 * Returns the xmlEntityPtr if found.
336 */
337xmlEntityPtr
338getEntity(void *ctx, const xmlChar *name)
339{
340 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
341 xmlEntityPtr ret;
342
343#ifdef DEBUG_SAX
344 xmlGenericError(xmlGenericErrorContext,
345 "SAX.getEntity(%s)\n", name);
346#endif
347
Daniel Veillard28757702002-02-18 11:19:30 +0000348 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
349 if (ctxt->inSubset == 2) {
350 ctxt->myDoc->standalone = 0;
351 ret = xmlGetDocEntity(ctxt->myDoc, name);
352 ctxt->myDoc->standalone = 1;
353 } else {
354 ret = xmlGetDocEntity(ctxt->myDoc, name);
355 if (ret == NULL) {
356 ctxt->myDoc->standalone = 0;
357 ret = xmlGetDocEntity(ctxt->myDoc, name);
358 if (ret != NULL) {
359 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
360 ctxt->sax->error(ctxt,
361 "Entity(%s) document marked standalone but require external subset\n",
362 name);
363 ctxt->valid = 0;
364 ctxt->wellFormed = 0;
365 }
366 ctxt->myDoc->standalone = 1;
367 }
368 }
369 } else {
370 ret = xmlGetDocEntity(ctxt->myDoc, name);
371 }
Owen Taylor3473f882001-02-23 17:55:21 +0000372 if ((ret != NULL) && (ctxt->validate) && (ret->children == NULL) &&
373 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
374 /*
375 * for validation purposes we really need to fetch and
376 * parse the external entity
377 */
Owen Taylor3473f882001-02-23 17:55:21 +0000378 xmlNodePtr children;
379
Daniel Veillard5015b712001-08-17 09:37:52 +0000380 xmlParseCtxtExternalEntity(ctxt, ret->URI, ret->ExternalID, &children);
Owen Taylor3473f882001-02-23 17:55:21 +0000381 xmlAddChildList((xmlNodePtr) ret, children);
382 }
383 return(ret);
384}
385
386/**
387 * getParameterEntity:
388 * @ctx: the user data (XML parser context)
389 * @name: The entity name
390 *
391 * Get a parameter entity by name
392 *
393 * Returns the xmlEntityPtr if found.
394 */
395xmlEntityPtr
396getParameterEntity(void *ctx, const xmlChar *name)
397{
398 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
399 xmlEntityPtr ret;
400
401#ifdef DEBUG_SAX
402 xmlGenericError(xmlGenericErrorContext,
403 "SAX.getParameterEntity(%s)\n", name);
404#endif
405
406 ret = xmlGetParameterEntity(ctxt->myDoc, name);
407 return(ret);
408}
409
410
411/**
412 * entityDecl:
413 * @ctx: the user data (XML parser context)
414 * @name: the entity name
415 * @type: the entity type
416 * @publicId: The public ID of the entity
417 * @systemId: The system ID of the entity
418 * @content: the entity value (without processing).
419 *
420 * An entity definition has been parsed
421 */
422void
423entityDecl(void *ctx, const xmlChar *name, int type,
424 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
425{
426 xmlEntityPtr ent;
427 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
428
429#ifdef DEBUG_SAX
430 xmlGenericError(xmlGenericErrorContext,
431 "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
432 name, type, publicId, systemId, content);
433#endif
434 if (ctxt->inSubset == 1) {
435 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
436 systemId, content);
437 if ((ent == NULL) && (ctxt->pedantic) &&
438 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
439 ctxt->sax->warning(ctxt,
440 "Entity(%s) already defined in the internal subset\n", name);
441 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
442 xmlChar *URI;
443 const char *base = NULL;
444
445 if (ctxt->input != NULL)
446 base = ctxt->input->filename;
447 if (base == NULL)
448 base = ctxt->directory;
449
450 URI = xmlBuildURI(systemId, (const xmlChar *) base);
451 ent->URI = URI;
452 }
453 } else if (ctxt->inSubset == 2) {
454 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
455 systemId, content);
456 if ((ent == NULL) && (ctxt->pedantic) &&
457 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
458 ctxt->sax->warning(ctxt,
459 "Entity(%s) already defined in the external subset\n", name);
460 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
461 xmlChar *URI;
462 const char *base = NULL;
463
464 if (ctxt->input != NULL)
465 base = ctxt->input->filename;
466 if (base == NULL)
467 base = ctxt->directory;
468
469 URI = xmlBuildURI(systemId, (const xmlChar *) base);
470 ent->URI = URI;
471 }
472 } else {
473 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
474 ctxt->sax->error(ctxt,
475 "SAX.entityDecl(%s) called while not in subset\n", name);
476 }
477}
478
479/**
480 * attributeDecl:
481 * @ctx: the user data (XML parser context)
482 * @elem: the name of the element
483 * @fullname: the attribute name
484 * @type: the attribute type
485 * @def: the type of default value
486 * @defaultValue: the attribute default value
487 * @tree: the tree of enumerated value set
488 *
489 * An attribute definition has been parsed
490 */
491void
492attributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
493 int type, int def, const xmlChar *defaultValue,
494 xmlEnumerationPtr tree)
495{
496 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
497 xmlAttributePtr attr;
498 xmlChar *name = NULL, *prefix = NULL;
499
500#ifdef DEBUG_SAX
501 xmlGenericError(xmlGenericErrorContext,
502 "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
503 elem, fullname, type, def, defaultValue);
504#endif
505 name = xmlSplitQName(ctxt, fullname, &prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +0000506 ctxt->vctxt.valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000507 if (ctxt->inSubset == 1)
508 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
509 name, prefix, (xmlAttributeType) type,
510 (xmlAttributeDefault) def, defaultValue, tree);
511 else if (ctxt->inSubset == 2)
512 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
513 name, prefix, (xmlAttributeType) type,
514 (xmlAttributeDefault) def, defaultValue, tree);
515 else {
516 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
517 ctxt->sax->error(ctxt,
518 "SAX.attributeDecl(%s) called while not in subset\n", name);
519 return;
520 }
Daniel Veillardc7612992002-02-17 22:47:37 +0000521 if (ctxt->vctxt.valid == 0)
522 ctxt->valid = 0;
Daniel Veillardd85f4f42002-03-25 10:48:46 +0000523 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
524 (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000525 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
526 attr);
527 if (prefix != NULL)
528 xmlFree(prefix);
529 if (name != NULL)
530 xmlFree(name);
531}
532
533/**
534 * elementDecl:
535 * @ctx: the user data (XML parser context)
536 * @name: the element name
537 * @type: the element type
538 * @content: the element value tree
539 *
540 * An element definition has been parsed
541 */
542void
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000543elementDecl(void *ctx, const xmlChar * name, int type,
544 xmlElementContentPtr content)
Owen Taylor3473f882001-02-23 17:55:21 +0000545{
546 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
547 xmlElementPtr elem = NULL;
548
549#ifdef DEBUG_SAX
550 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000551 "SAX.elementDecl(%s, %d, ...)\n", name, type);
Owen Taylor3473f882001-02-23 17:55:21 +0000552#endif
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000553
Owen Taylor3473f882001-02-23 17:55:21 +0000554 if (ctxt->inSubset == 1)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000555 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
556 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000557 else if (ctxt->inSubset == 2)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000558 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
559 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000560 else {
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000561 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
562 ctxt->sax->error(ctxt,
563 "SAX.elementDecl(%s) called while not in subset\n",
564 name);
565 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000566 }
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000567 if (elem == NULL)
568 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000569 if (ctxt->validate && ctxt->wellFormed &&
570 ctxt->myDoc && ctxt->myDoc->intSubset)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000571 ctxt->valid &=
572 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Owen Taylor3473f882001-02-23 17:55:21 +0000573}
574
575/**
576 * notationDecl:
577 * @ctx: the user data (XML parser context)
578 * @name: The name of the notation
579 * @publicId: The public ID of the entity
580 * @systemId: The system ID of the entity
581 *
582 * What to do when a notation declaration has been parsed.
583 */
584void
585notationDecl(void *ctx, const xmlChar *name,
586 const xmlChar *publicId, const xmlChar *systemId)
587{
588 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
589 xmlNotationPtr nota = NULL;
590
591#ifdef DEBUG_SAX
592 xmlGenericError(xmlGenericErrorContext,
593 "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
594#endif
595
Daniel Veillard7aea52d2002-02-17 23:07:47 +0000596 if ((publicId == NULL) && (systemId == NULL)) {
597 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
598 ctxt->sax->error(ctxt,
599 "SAX.notationDecl(%s) externalID or PublicID missing\n", name);
600 ctxt->valid = 0;
601 ctxt->wellFormed = 0;
602 return;
603 } else if (ctxt->inSubset == 1)
Owen Taylor3473f882001-02-23 17:55:21 +0000604 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
605 publicId, systemId);
606 else if (ctxt->inSubset == 2)
Daniel Veillard25239c12001-03-14 13:56:48 +0000607 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
Owen Taylor3473f882001-02-23 17:55:21 +0000608 publicId, systemId);
609 else {
610 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
611 ctxt->sax->error(ctxt,
612 "SAX.notationDecl(%s) called while not in subset\n", name);
613 return;
614 }
615 if (nota == NULL) ctxt->valid = 0;
616 if (ctxt->validate && ctxt->wellFormed &&
617 ctxt->myDoc && ctxt->myDoc->intSubset)
618 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
619 nota);
620}
621
622/**
623 * unparsedEntityDecl:
624 * @ctx: the user data (XML parser context)
625 * @name: The name of the entity
626 * @publicId: The public ID of the entity
627 * @systemId: The system ID of the entity
628 * @notationName: the name of the notation
629 *
630 * What to do when an unparsed entity declaration is parsed
631 */
632void
633unparsedEntityDecl(void *ctx, const xmlChar *name,
634 const xmlChar *publicId, const xmlChar *systemId,
635 const xmlChar *notationName)
636{
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000637 xmlEntityPtr ent;
Owen Taylor3473f882001-02-23 17:55:21 +0000638 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
639#ifdef DEBUG_SAX
640 xmlGenericError(xmlGenericErrorContext,
641 "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
642 name, publicId, systemId, notationName);
643#endif
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000644#if 0
645 Done in xmlValidateDtdFinal now.
Daniel Veillard28757702002-02-18 11:19:30 +0000646 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc) {
647 int ret;
648 ret = xmlValidateNotationUse(&ctxt->vctxt, ctxt->myDoc,
Owen Taylor3473f882001-02-23 17:55:21 +0000649 notationName);
Daniel Veillard28757702002-02-18 11:19:30 +0000650 if (ret == 0) {
651 ctxt->wellFormed = 0;
652 ctxt->valid = 0;
653 }
654 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000655#endif
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000656 if (ctxt->inSubset == 1) {
657 ent = xmlAddDocEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000658 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
659 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000660 if ((ent == NULL) && (ctxt->pedantic) &&
661 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
662 ctxt->sax->warning(ctxt,
663 "Entity(%s) already defined in the internal subset\n", name);
664 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
665 xmlChar *URI;
666 const char *base = NULL;
667
668 if (ctxt->input != NULL)
669 base = ctxt->input->filename;
670 if (base == NULL)
671 base = ctxt->directory;
672
673 URI = xmlBuildURI(systemId, (const xmlChar *) base);
674 ent->URI = URI;
675 }
676 } else if (ctxt->inSubset == 2) {
677 ent = xmlAddDtdEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000678 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
679 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000680 if ((ent == NULL) && (ctxt->pedantic) &&
681 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
682 ctxt->sax->warning(ctxt,
683 "Entity(%s) already defined in the external subset\n", name);
684 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
685 xmlChar *URI;
686 const char *base = NULL;
687
688 if (ctxt->input != NULL)
689 base = ctxt->input->filename;
690 if (base == NULL)
691 base = ctxt->directory;
692
693 URI = xmlBuildURI(systemId, (const xmlChar *) base);
694 ent->URI = URI;
695 }
696 } else {
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000697 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
698 ctxt->sax->error(ctxt,
699 "SAX.unparsedEntityDecl(%s) called while not in subset\n", name);
700 }
Owen Taylor3473f882001-02-23 17:55:21 +0000701}
702
703/**
704 * setDocumentLocator:
705 * @ctx: the user data (XML parser context)
706 * @loc: A SAX Locator
707 *
708 * Receive the document locator at startup, actually xmlDefaultSAXLocator
709 * Everything is available on the context, so this is useless in our case.
710 */
711void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000712setDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +0000713{
714 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
715#ifdef DEBUG_SAX
716 xmlGenericError(xmlGenericErrorContext,
717 "SAX.setDocumentLocator()\n");
718#endif
719}
720
721/**
722 * startDocument:
723 * @ctx: the user data (XML parser context)
724 *
725 * called when the document start being processed.
726 */
727void
728startDocument(void *ctx)
729{
730 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
731 xmlDocPtr doc;
732
733#ifdef DEBUG_SAX
734 xmlGenericError(xmlGenericErrorContext,
735 "SAX.startDocument()\n");
736#endif
737 if (ctxt->html) {
738 if (ctxt->myDoc == NULL)
739#ifdef LIBXML_HTML_ENABLED
740 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
741#else
742 xmlGenericError(xmlGenericErrorContext,
743 "libxml2 built without HTML support\n");
744#endif
745 } else {
746 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
747 if (doc != NULL) {
748 if (ctxt->encoding != NULL)
749 doc->encoding = xmlStrdup(ctxt->encoding);
750 else
751 doc->encoding = NULL;
752 doc->standalone = ctxt->standalone;
753 }
754 }
755 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
756 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000757 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
Owen Taylor3473f882001-02-23 17:55:21 +0000758 }
759}
760
761/**
762 * endDocument:
763 * @ctx: the user data (XML parser context)
764 *
765 * called when the document end has been detected.
766 */
767void
768endDocument(void *ctx)
769{
770 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
771#ifdef DEBUG_SAX
772 xmlGenericError(xmlGenericErrorContext,
773 "SAX.endDocument()\n");
774#endif
775 if (ctxt->validate && ctxt->wellFormed &&
776 ctxt->myDoc && ctxt->myDoc->intSubset)
777 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
778
779 /*
780 * Grab the encoding if it was added on-the-fly
781 */
782 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
783 (ctxt->myDoc->encoding == NULL)) {
784 ctxt->myDoc->encoding = ctxt->encoding;
785 ctxt->encoding = NULL;
786 }
787 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
788 (ctxt->myDoc->encoding == NULL)) {
789 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
790 }
791 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
792 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
793 ctxt->myDoc->charset = ctxt->charset;
794 }
795}
796
797/**
798 * attribute:
799 * @ctx: the user data (XML parser context)
800 * @fullname: The attribute name, including namespace prefix
801 * @value: The attribute value
802 *
803 * Handle an attribute that has been read by the parser.
804 * The default handling is to convert the attribute into an
805 * DOM subtree and past it in a new xmlAttr element added to
806 * the element.
807 */
808void
809attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
810{
811 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
812 xmlAttrPtr ret;
813 xmlChar *name;
814 xmlChar *ns;
815 xmlChar *nval;
816 xmlNsPtr namespace;
817
818/****************
819#ifdef DEBUG_SAX
820 xmlGenericError(xmlGenericErrorContext,
821 "SAX.attribute(%s, %s)\n", fullname, value);
822#endif
823 ****************/
824 /*
825 * Split the full name into a namespace prefix and the tag name
826 */
827 name = xmlSplitQName(ctxt, fullname, &ns);
828
829 /*
830 * Do the last stage of the attribute normalization
831 * Needed for HTML too:
832 * http://www.w3.org/TR/html4/types.html#h-6.2
833 */
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000834 ctxt->vctxt.valid = 1;
835 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
836 ctxt->myDoc, ctxt->node,
Owen Taylor3473f882001-02-23 17:55:21 +0000837 fullname, value);
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000838 if (ctxt->vctxt.valid != 1) {
839 ctxt->valid = 0;
840 }
Owen Taylor3473f882001-02-23 17:55:21 +0000841 if (nval != NULL)
842 value = nval;
843
844 /*
845 * Check whether it's a namespace definition
846 */
847 if ((!ctxt->html) && (ns == NULL) &&
848 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
849 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
850 if (value[0] != 0) {
851 xmlURIPtr uri;
852
853 uri = xmlParseURI((const char *)value);
854 if (uri == NULL) {
855 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
856 ctxt->sax->warning(ctxt->userData,
857 "nmlns: %s not a valid URI\n", value);
858 } else {
859 if (uri->scheme == NULL) {
860 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
861 ctxt->sax->warning(ctxt->userData,
862 "nmlns: URI %s is not absolute\n", value);
863 }
864 xmlFreeURI(uri);
865 }
866 }
867
868 /* a default namespace definition */
869 xmlNewNs(ctxt->node, value, NULL);
870 if (name != NULL)
871 xmlFree(name);
872 if (nval != NULL)
873 xmlFree(nval);
874 return;
875 }
876 if ((!ctxt->html) &&
877 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
878 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
879 /*
880 * Validate also for namespace decls, they are attributes from
881 * an XML-1.0 perspective
882 TODO ... doesn't map well with current API
883 if (ctxt->validate && ctxt->wellFormed &&
884 ctxt->myDoc && ctxt->myDoc->intSubset)
885 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
886 ctxt->node, ret, value);
887 */
Daniel Veillardc0fef772002-03-01 16:16:31 +0000888 if (value[0] == 0) {
889 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
890 ctxt->sax->error(ctxt->userData,
891 "Empty namespace name for prefix %s\n", name);
892 }
Owen Taylor3473f882001-02-23 17:55:21 +0000893 /* a standard namespace definition */
894 xmlNewNs(ctxt->node, value, name);
895 xmlFree(ns);
896 if (name != NULL)
897 xmlFree(name);
898 if (nval != NULL)
899 xmlFree(nval);
900 return;
901 }
902
903 if (ns != NULL)
904 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
905 else {
906 namespace = NULL;
907 }
908
909 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
910 ret = xmlNewNsProp(ctxt->node, namespace, name, NULL);
911
912 if (ret != NULL) {
913 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
914 xmlNodePtr tmp;
915
916 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
917 tmp = ret->children;
918 while (tmp != NULL) {
919 tmp->parent = (xmlNodePtr) ret;
920 if (tmp->next == NULL)
921 ret->last = tmp;
922 tmp = tmp->next;
923 }
924 } else if (value != NULL) {
925 ret->children = xmlNewDocText(ctxt->myDoc, value);
926 ret->last = ret->children;
927 if (ret->children != NULL)
928 ret->children->parent = (xmlNodePtr) ret;
929 }
930 }
931
932 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
933 ctxt->myDoc && ctxt->myDoc->intSubset) {
934
935 /*
936 * If we don't substitute entities, the validation should be
937 * done on a value with replaced entities anyway.
938 */
939 if (!ctxt->replaceEntities) {
940 xmlChar *val;
941
942 ctxt->depth++;
943 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
944 0,0,0);
945 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +0000946
Owen Taylor3473f882001-02-23 17:55:21 +0000947 if (val == NULL)
948 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
949 ctxt->myDoc, ctxt->node, ret, value);
950 else {
Daniel Veillardc7612992002-02-17 22:47:37 +0000951 xmlChar *nvalnorm;
952
953 /*
954 * Do the last stage of the attribute normalization
955 * It need to be done twice ... it's an extra burden related
956 * to the ability to keep references in attributes
957 */
958 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
959 ctxt->node, fullname, val);
960 if (nvalnorm != NULL) {
961 xmlFree(val);
962 val = nvalnorm;
963 }
964
Owen Taylor3473f882001-02-23 17:55:21 +0000965 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
966 ctxt->myDoc, ctxt->node, ret, val);
967 xmlFree(val);
968 }
969 } else {
970 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
971 ctxt->node, ret, value);
972 }
Daniel Veillard62f313b2001-07-04 19:49:14 +0000973 } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
974 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000975 /*
976 * when validating, the ID registration is done at the attribute
977 * validation level. Otherwise we have to do specific handling here.
978 */
979 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
980 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
981 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
982 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
983 }
984
985 if (nval != NULL)
986 xmlFree(nval);
987 if (name != NULL)
988 xmlFree(name);
989 if (ns != NULL)
990 xmlFree(ns);
991}
992
Daniel Veillard878eab02002-02-19 13:46:09 +0000993/*
994 * xmlCheckDefaultedAttributes:
995 *
996 * Check defaulted attributes from the DTD
997 */
998static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000999xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001000 const xmlChar *prefix, const xmlChar **atts) {
1001 xmlElementPtr elemDecl;
1002 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001003 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001004 int i;
1005
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001006 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1007 if (elemDecl == NULL) {
1008 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1009 internal = 0;
1010 }
1011
1012process_external_subset:
1013
Daniel Veillard878eab02002-02-19 13:46:09 +00001014 if (elemDecl != NULL) {
1015 xmlAttributePtr attr = elemDecl->attributes;
1016 /*
1017 * Check against defaulted attributes from the external subset
1018 * if the document is stamped as standalone
1019 */
1020 if ((ctxt->myDoc->standalone == 1) &&
1021 (ctxt->myDoc->extSubset != NULL) &&
1022 (ctxt->validate)) {
1023 while (attr != NULL) {
1024 if ((attr->defaultValue != NULL) &&
1025 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1026 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001027 attr->prefix) == attr) &&
1028 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1029 attr->elem, attr->name,
1030 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001031 xmlChar *fulln;
1032
1033 if (attr->prefix != NULL) {
1034 fulln = xmlStrdup(attr->prefix);
1035 fulln = xmlStrcat(fulln, BAD_CAST ":");
1036 fulln = xmlStrcat(fulln, attr->name);
1037 } else {
1038 fulln = xmlStrdup(attr->name);
1039 }
1040
1041 /*
1042 * Check that the attribute is not declared in the
1043 * serialization
1044 */
1045 att = NULL;
1046 if (atts != NULL) {
1047 i = 0;
1048 att = atts[i];
1049 while (att != NULL) {
1050 if (xmlStrEqual(att, fulln))
1051 break;
1052 i += 2;
1053 att = atts[i];
1054 }
1055 }
1056 if (att == NULL) {
1057 if (ctxt->vctxt.error != NULL)
1058 ctxt->vctxt.error(ctxt->vctxt.userData,
1059 "standalone: attribute %s on %s defaulted from external subset\n",
1060 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001061 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001062 }
1063 }
1064 attr = attr->nexth;
1065 }
1066 }
1067
1068 /*
1069 * Actually insert defaulted values when needed
1070 */
1071 attr = elemDecl->attributes;
1072 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001073 /*
1074 * Make sure that attributes redefinition occuring in the
1075 * internal subset are not overriden by definitions in the
1076 * external subset.
1077 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001078 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001079 /*
1080 * the element should be instantiated in the tree if:
1081 * - this is a namespace prefix
1082 * - the user required for completion in the tree
1083 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001084 * - there isn't already an attribute definition
1085 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001086 */
1087 if (((attr->prefix != NULL) &&
1088 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1089 ((attr->prefix == NULL) &&
1090 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1091 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001092 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001093
Daniel Veillard8aff2472002-02-19 21:50:43 +00001094 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1095 attr->elem, attr->name,
1096 attr->prefix);
1097 if ((tst == attr) || (tst == NULL)) {
1098 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001099
Daniel Veillard8aff2472002-02-19 21:50:43 +00001100 if (attr->prefix != NULL) {
1101 fulln = xmlStrdup(attr->prefix);
1102 fulln = xmlStrcat(fulln, BAD_CAST ":");
1103 fulln = xmlStrcat(fulln, attr->name);
1104 } else {
1105 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001106 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001107
1108 /*
1109 * Check that the attribute is not declared in the
1110 * serialization
1111 */
1112 att = NULL;
1113 if (atts != NULL) {
1114 i = 0;
1115 att = atts[i];
1116 while (att != NULL) {
1117 if (xmlStrEqual(att, fulln))
1118 break;
1119 i += 2;
1120 att = atts[i];
1121 }
1122 }
1123 if (att == NULL) {
1124 attribute(ctxt, fulln, attr->defaultValue);
1125 }
1126 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001127 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001128 }
1129 }
1130 attr = attr->nexth;
1131 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001132 if (internal == 1) {
1133 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1134 name, prefix);
1135 internal = 0;
1136 goto process_external_subset;
1137 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001138 }
1139}
1140
Owen Taylor3473f882001-02-23 17:55:21 +00001141/**
1142 * startElement:
1143 * @ctx: the user data (XML parser context)
1144 * @fullname: The element name, including namespace prefix
1145 * @atts: An array of name/value attributes pairs, NULL terminated
1146 *
1147 * called when an opening tag has been processed.
1148 */
1149void
1150startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1151{
1152 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1153 xmlNodePtr ret;
1154 xmlNodePtr parent = ctxt->node;
1155 xmlNsPtr ns;
1156 xmlChar *name;
1157 xmlChar *prefix;
1158 const xmlChar *att;
1159 const xmlChar *value;
1160 int i;
1161
1162#ifdef DEBUG_SAX
1163 xmlGenericError(xmlGenericErrorContext,
1164 "SAX.startElement(%s)\n", fullname);
1165#endif
1166
1167 /*
1168 * First check on validity:
1169 */
1170 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1171 ((ctxt->myDoc->intSubset == NULL) ||
1172 ((ctxt->myDoc->intSubset->notations == NULL) &&
1173 (ctxt->myDoc->intSubset->elements == NULL) &&
1174 (ctxt->myDoc->intSubset->attributes == NULL) &&
1175 (ctxt->myDoc->intSubset->entities == NULL)))) {
1176 if (ctxt->vctxt.error != NULL) {
1177 ctxt->vctxt.error(ctxt->vctxt.userData,
1178 "Validation failed: no DTD found !\n");
1179 }
1180 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001181 ctxt->valid = 0;
1182 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001183 }
1184
1185
1186 /*
1187 * Split the full name into a namespace prefix and the tag name
1188 */
1189 name = xmlSplitQName(ctxt, fullname, &prefix);
1190
1191
1192 /*
1193 * Note : the namespace resolution is deferred until the end of the
1194 * attributes parsing, since local namespace can be defined as
1195 * an attribute at this level.
1196 */
1197 ret = xmlNewDocNode(ctxt->myDoc, NULL, name, NULL);
1198 if (ret == NULL) return;
1199 if (ctxt->myDoc->children == NULL) {
1200#ifdef DEBUG_SAX_TREE
1201 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1202#endif
1203 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1204 } else if (parent == NULL) {
1205 parent = ctxt->myDoc->children;
1206 }
1207 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001208 if (ctxt->linenumbers) {
1209 if (ctxt->input != NULL)
1210 ret->content = (void *) (long) ctxt->input->line;
1211 }
Owen Taylor3473f882001-02-23 17:55:21 +00001212
1213 /*
1214 * We are parsing a new node.
1215 */
1216#ifdef DEBUG_SAX_TREE
1217 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1218#endif
1219 nodePush(ctxt, ret);
1220
1221 /*
1222 * Link the child element
1223 */
1224 if (parent != NULL) {
1225 if (parent->type == XML_ELEMENT_NODE) {
1226#ifdef DEBUG_SAX_TREE
1227 xmlGenericError(xmlGenericErrorContext,
1228 "adding child %s to %s\n", name, parent->name);
1229#endif
1230 xmlAddChild(parent, ret);
1231 } else {
1232#ifdef DEBUG_SAX_TREE
1233 xmlGenericError(xmlGenericErrorContext,
1234 "adding sibling %s to ", name);
1235 xmlDebugDumpOneNode(stderr, parent, 0);
1236#endif
1237 xmlAddSibling(parent, ret);
1238 }
1239 }
1240
1241 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001242 * Insert all the defaulted attributes from the DTD especially namespaces
1243 */
1244 if ((!ctxt->html) &&
1245 ((ctxt->myDoc->intSubset != NULL) ||
1246 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001247 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001248 }
1249
1250 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001251 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001252 */
1253 if (atts != NULL) {
1254 i = 0;
1255 att = atts[i++];
1256 value = atts[i++];
1257 if (!ctxt->html) {
1258 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001259 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1260 (att[3] == 'n') && (att[4] == 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001261 attribute(ctxt, att, value);
1262
1263 att = atts[i++];
1264 value = atts[i++];
1265 }
1266 }
1267 }
1268
1269 /*
1270 * Search the namespace, note that since the attributes have been
1271 * processed, the local namespaces are available.
1272 */
1273 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1274 if ((ns == NULL) && (parent != NULL))
1275 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1276 if ((prefix != NULL) && (ns == NULL)) {
1277 ns = xmlNewNs(ret, NULL, prefix);
1278 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1279 ctxt->sax->warning(ctxt->userData,
1280 "Namespace prefix %s is not defined\n", prefix);
1281 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001282
1283 /*
1284 * set the namespace node, making sure that if the default namspace
1285 * is unbound on a parent we simply kee it NULL
1286 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001287 if ((ns != NULL) && (ns->href != NULL) &&
1288 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001289 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001290
1291 /*
1292 * process all the other attributes
1293 */
1294 if (atts != NULL) {
1295 i = 0;
1296 att = atts[i++];
1297 value = atts[i++];
1298 if (ctxt->html) {
1299 while (att != NULL) {
1300 attribute(ctxt, att, value);
1301 att = atts[i++];
1302 value = atts[i++];
1303 }
1304 } else {
1305 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001306 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1307 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001308 attribute(ctxt, att, value);
1309
1310 /*
1311 * Next ones
1312 */
1313 att = atts[i++];
1314 value = atts[i++];
1315 }
1316 }
1317 }
1318
1319 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001320 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001321 * check the document root element for validity
1322 */
1323 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001324 int chk;
1325
1326 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1327 if (chk <= 0)
1328 ctxt->valid = 0;
1329 if (chk < 0)
1330 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001331 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1332 ctxt->vctxt.finishDtd = 1;
1333 }
1334
1335 if (prefix != NULL)
1336 xmlFree(prefix);
1337 if (name != NULL)
1338 xmlFree(name);
1339
1340}
1341
1342/**
1343 * endElement:
1344 * @ctx: the user data (XML parser context)
1345 * @name: The element name
1346 *
1347 * called when the end of an element has been detected.
1348 */
1349void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001350endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001351{
1352 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1353 xmlParserNodeInfo node_info;
1354 xmlNodePtr cur = ctxt->node;
1355
1356#ifdef DEBUG_SAX
1357 if (name == NULL)
1358 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1359 else
1360 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1361#endif
1362
1363 /* Capture end position and add node */
1364 if (cur != NULL && ctxt->record_info) {
1365 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1366 node_info.end_line = ctxt->input->line;
1367 node_info.node = cur;
1368 xmlParserAddNodeInfo(ctxt, &node_info);
1369 }
1370 ctxt->nodemem = -1;
1371
1372 if (ctxt->validate && ctxt->wellFormed &&
1373 ctxt->myDoc && ctxt->myDoc->intSubset)
1374 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1375 cur);
1376
1377
1378 /*
1379 * end of parsing of this node.
1380 */
1381#ifdef DEBUG_SAX_TREE
1382 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1383#endif
1384 nodePop(ctxt);
1385}
1386
1387/**
1388 * reference:
1389 * @ctx: the user data (XML parser context)
1390 * @name: The entity name
1391 *
1392 * called when an entity reference is detected.
1393 */
1394void
1395reference(void *ctx, const xmlChar *name)
1396{
1397 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1398 xmlNodePtr ret;
1399
1400#ifdef DEBUG_SAX
1401 xmlGenericError(xmlGenericErrorContext,
1402 "SAX.reference(%s)\n", name);
1403#endif
1404 if (name[0] == '#')
1405 ret = xmlNewCharRef(ctxt->myDoc, name);
1406 else
1407 ret = xmlNewReference(ctxt->myDoc, name);
1408#ifdef DEBUG_SAX_TREE
1409 xmlGenericError(xmlGenericErrorContext,
1410 "add reference %s to %s \n", name, ctxt->node->name);
1411#endif
1412 xmlAddChild(ctxt->node, ret);
1413}
1414
1415/**
1416 * characters:
1417 * @ctx: the user data (XML parser context)
1418 * @ch: a xmlChar string
1419 * @len: the number of xmlChar
1420 *
1421 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001422 */
1423void
1424characters(void *ctx, const xmlChar *ch, int len)
1425{
1426 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1427 xmlNodePtr lastChild;
1428
1429#ifdef DEBUG_SAX
1430 xmlGenericError(xmlGenericErrorContext,
1431 "SAX.characters(%.30s, %d)\n", ch, len);
1432#endif
1433 /*
1434 * Handle the data if any. If there is no child
1435 * add it as content, otherwise if the last child is text,
1436 * concatenate it, else create a new node of type text.
1437 */
1438
1439 if (ctxt->node == NULL) {
1440#ifdef DEBUG_SAX_TREE
1441 xmlGenericError(xmlGenericErrorContext,
1442 "add chars: ctxt->node == NULL !\n");
1443#endif
1444 return;
1445 }
1446 lastChild = xmlGetLastChild(ctxt->node);
1447#ifdef DEBUG_SAX_TREE
1448 xmlGenericError(xmlGenericErrorContext,
1449 "add chars to %s \n", ctxt->node->name);
1450#endif
1451
1452 /*
1453 * Here we needed an accelerator mechanism in case of very large
1454 * elements. Use an attribute in the structure !!!
1455 */
1456 if (lastChild == NULL) {
1457 /* first node, first time */
1458 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001459 if (ctxt->node->children != NULL) {
1460 ctxt->nodelen = len;
1461 ctxt->nodemem = len + 1;
1462 }
Owen Taylor3473f882001-02-23 17:55:21 +00001463 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001464 int coalesceText = (lastChild != NULL) &&
1465 (lastChild->type == XML_TEXT_NODE) &&
1466 (lastChild->name == xmlStringText);
1467 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001468 /*
1469 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001470 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001471 * reallocate a new buffer, move data, append ch. Here
1472 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001473 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001474 */
1475 if (ctxt->nodelen + len >= ctxt->nodemem) {
1476 xmlChar *newbuf;
1477 int size;
1478
1479 size = ctxt->nodemem + len;
1480 size *= 2;
1481 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1482 if (newbuf == NULL) {
1483 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1484 ctxt->sax->error(ctxt->userData,
1485 "SAX.characters(): out of memory\n");
1486 return;
1487 }
1488 ctxt->nodemem = size;
1489 lastChild->content = newbuf;
1490 }
1491 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1492 ctxt->nodelen += len;
1493 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001494 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001495 xmlTextConcat(lastChild, ch, len);
1496 if (ctxt->node->children != NULL) {
1497 ctxt->nodelen = xmlStrlen(lastChild->content);
1498 ctxt->nodemem = ctxt->nodelen + 1;
1499 }
Owen Taylor3473f882001-02-23 17:55:21 +00001500 } else {
1501 /* Mixed content, first time */
1502 lastChild = xmlNewTextLen(ch, len);
1503 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001504 if (ctxt->node->children != NULL) {
1505 ctxt->nodelen = len;
1506 ctxt->nodemem = len + 1;
1507 }
Owen Taylor3473f882001-02-23 17:55:21 +00001508 }
1509 }
1510}
1511
1512/**
1513 * ignorableWhitespace:
1514 * @ctx: the user data (XML parser context)
1515 * @ch: a xmlChar string
1516 * @len: the number of xmlChar
1517 *
1518 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001519 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001520 */
1521void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001522ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001523{
1524 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1525#ifdef DEBUG_SAX
1526 xmlGenericError(xmlGenericErrorContext,
1527 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1528#endif
1529}
1530
1531/**
1532 * processingInstruction:
1533 * @ctx: the user data (XML parser context)
1534 * @target: the target name
1535 * @data: the PI data's
1536 *
1537 * A processing instruction has been parsed.
1538 */
1539void
1540processingInstruction(void *ctx, const xmlChar *target,
1541 const xmlChar *data)
1542{
1543 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1544 xmlNodePtr ret;
1545 xmlNodePtr parent = ctxt->node;
1546
1547#ifdef DEBUG_SAX
1548 xmlGenericError(xmlGenericErrorContext,
1549 "SAX.processingInstruction(%s, %s)\n", target, data);
1550#endif
1551
1552 ret = xmlNewPI(target, data);
1553 if (ret == NULL) return;
1554 parent = ctxt->node;
1555
1556 if (ctxt->inSubset == 1) {
1557 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1558 return;
1559 } else if (ctxt->inSubset == 2) {
1560 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1561 return;
1562 }
1563 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1564#ifdef DEBUG_SAX_TREE
1565 xmlGenericError(xmlGenericErrorContext,
1566 "Setting PI %s as root\n", target);
1567#endif
1568 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1569 return;
1570 }
1571 if (parent->type == XML_ELEMENT_NODE) {
1572#ifdef DEBUG_SAX_TREE
1573 xmlGenericError(xmlGenericErrorContext,
1574 "adding PI %s child to %s\n", target, parent->name);
1575#endif
1576 xmlAddChild(parent, ret);
1577 } else {
1578#ifdef DEBUG_SAX_TREE
1579 xmlGenericError(xmlGenericErrorContext,
1580 "adding PI %s sibling to ", target);
1581 xmlDebugDumpOneNode(stderr, parent, 0);
1582#endif
1583 xmlAddSibling(parent, ret);
1584 }
1585}
1586
1587/**
1588 * globalNamespace:
1589 * @ctx: the user data (XML parser context)
1590 * @href: the namespace associated URN
1591 * @prefix: the namespace prefix
1592 *
1593 * An old global namespace has been parsed.
1594 */
1595void
1596globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1597{
1598 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1599#ifdef DEBUG_SAX
1600 xmlGenericError(xmlGenericErrorContext,
1601 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1602#endif
1603 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1604}
1605
1606/**
1607 * setNamespace:
1608 * @ctx: the user data (XML parser context)
1609 * @name: the namespace prefix
1610 *
1611 * Set the current element namespace.
1612 */
1613
1614void
1615setNamespace(void *ctx, const xmlChar *name)
1616{
1617 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1618 xmlNsPtr ns;
1619 xmlNodePtr parent;
1620
1621#ifdef DEBUG_SAX
1622 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1623#endif
1624 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1625 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1626 if (ctxt->nodeNr >= 2) {
1627 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1628 if (parent != NULL)
1629 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1630 }
1631 }
1632 xmlSetNs(ctxt->node, ns);
1633}
1634
1635/**
1636 * getNamespace:
1637 * @ctx: the user data (XML parser context)
1638 *
1639 * Get the current element namespace.
1640 *
1641 * Returns the xmlNsPtr or NULL if none
1642 */
1643
1644xmlNsPtr
1645getNamespace(void *ctx)
1646{
1647 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1648 xmlNsPtr ret;
1649
1650#ifdef DEBUG_SAX
1651 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1652#endif
1653 ret = ctxt->node->ns;
1654 return(ret);
1655}
1656
1657/**
1658 * checkNamespace:
1659 * @ctx: the user data (XML parser context)
1660 * @namespace: the namespace to check against
1661 *
1662 * Check that the current element namespace is the same as the
1663 * one read upon parsing.
1664 *
1665 * Returns 1 if true 0 otherwise
1666 */
1667
1668int
1669checkNamespace(void *ctx, xmlChar *namespace)
1670{
1671 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1672 xmlNodePtr cur = ctxt->node;
1673
1674#ifdef DEBUG_SAX
1675 xmlGenericError(xmlGenericErrorContext,
1676 "SAX.checkNamespace(%s)\n", namespace);
1677#endif
1678
1679 /*
1680 * Check that the Name in the ETag is the same as in the STag.
1681 */
1682 if (namespace == NULL) {
1683 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1684 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1685 ctxt->sax->error(ctxt,
1686 "End tags for %s don't hold the namespace %s\n",
1687 cur->name, cur->ns->prefix);
1688 ctxt->wellFormed = 0;
1689 }
1690 } else {
1691 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1692 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1693 ctxt->sax->error(ctxt,
1694 "End tags %s holds a prefix %s not used by the open tag\n",
1695 cur->name, namespace);
1696 ctxt->wellFormed = 0;
1697 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1698 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1699 ctxt->sax->error(ctxt,
1700 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1701 cur->name, cur->ns->prefix, namespace);
1702 ctxt->wellFormed = 0;
1703 } else
1704 return(1);
1705 }
1706 return(0);
1707}
1708
1709/**
1710 * namespaceDecl:
1711 * @ctx: the user data (XML parser context)
1712 * @href: the namespace associated URN
1713 * @prefix: the namespace prefix
1714 *
1715 * A namespace has been parsed.
1716 */
1717void
1718namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1719{
1720 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1721#ifdef DEBUG_SAX
1722 if (prefix == NULL)
1723 xmlGenericError(xmlGenericErrorContext,
1724 "SAX.namespaceDecl(%s, NULL)\n", href);
1725 else
1726 xmlGenericError(xmlGenericErrorContext,
1727 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1728#endif
1729 xmlNewNs(ctxt->node, href, prefix);
1730}
1731
1732/**
1733 * comment:
1734 * @ctx: the user data (XML parser context)
1735 * @value: the comment content
1736 *
1737 * A comment has been parsed.
1738 */
1739void
1740comment(void *ctx, const xmlChar *value)
1741{
1742 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1743 xmlNodePtr ret;
1744 xmlNodePtr parent = ctxt->node;
1745
1746#ifdef DEBUG_SAX
1747 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1748#endif
1749 ret = xmlNewDocComment(ctxt->myDoc, value);
1750 if (ret == NULL) return;
1751
1752 if (ctxt->inSubset == 1) {
1753 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1754 return;
1755 } else if (ctxt->inSubset == 2) {
1756 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1757 return;
1758 }
1759 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1760#ifdef DEBUG_SAX_TREE
1761 xmlGenericError(xmlGenericErrorContext,
1762 "Setting comment as root\n");
1763#endif
1764 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1765 return;
1766 }
1767 if (parent->type == XML_ELEMENT_NODE) {
1768#ifdef DEBUG_SAX_TREE
1769 xmlGenericError(xmlGenericErrorContext,
1770 "adding comment child to %s\n", parent->name);
1771#endif
1772 xmlAddChild(parent, ret);
1773 } else {
1774#ifdef DEBUG_SAX_TREE
1775 xmlGenericError(xmlGenericErrorContext,
1776 "adding comment sibling to ");
1777 xmlDebugDumpOneNode(stderr, parent, 0);
1778#endif
1779 xmlAddSibling(parent, ret);
1780 }
1781}
1782
1783/**
1784 * cdataBlock:
1785 * @ctx: the user data (XML parser context)
1786 * @value: The pcdata content
1787 * @len: the block length
1788 *
1789 * called when a pcdata block has been parsed
1790 */
1791void
1792cdataBlock(void *ctx, const xmlChar *value, int len)
1793{
1794 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1795 xmlNodePtr ret, lastChild;
1796
1797#ifdef DEBUG_SAX
1798 xmlGenericError(xmlGenericErrorContext,
1799 "SAX.pcdata(%.10s, %d)\n", value, len);
1800#endif
1801 lastChild = xmlGetLastChild(ctxt->node);
1802#ifdef DEBUG_SAX_TREE
1803 xmlGenericError(xmlGenericErrorContext,
1804 "add chars to %s \n", ctxt->node->name);
1805#endif
1806 if ((lastChild != NULL) &&
1807 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1808 xmlTextConcat(lastChild, value, len);
1809 } else {
1810 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1811 xmlAddChild(ctxt->node, ret);
1812 }
1813}
1814
Daniel Veillardd0463562001-10-13 09:15:48 +00001815/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001816 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001817 * @hdlr: the SAX handler
1818 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001819 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001820 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001821 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001822void
1823initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1824{
1825 if(hdlr->initialized == 1)
1826 return;
1827
1828 hdlr->internalSubset = internalSubset;
1829 hdlr->externalSubset = externalSubset;
1830 hdlr->isStandalone = isStandalone;
1831 hdlr->hasInternalSubset = hasInternalSubset;
1832 hdlr->hasExternalSubset = hasExternalSubset;
1833 hdlr->resolveEntity = resolveEntity;
1834 hdlr->getEntity = getEntity;
1835 hdlr->getParameterEntity = getParameterEntity;
1836 hdlr->entityDecl = entityDecl;
1837 hdlr->attributeDecl = attributeDecl;
1838 hdlr->elementDecl = elementDecl;
1839 hdlr->notationDecl = notationDecl;
1840 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1841 hdlr->setDocumentLocator = setDocumentLocator;
1842 hdlr->startDocument = startDocument;
1843 hdlr->endDocument = endDocument;
1844 hdlr->startElement = startElement;
1845 hdlr->endElement = endElement;
1846 hdlr->reference = reference;
1847 hdlr->characters = characters;
1848 hdlr->cdataBlock = cdataBlock;
1849 hdlr->ignorableWhitespace = characters;
1850 hdlr->processingInstruction = processingInstruction;
1851 hdlr->comment = comment;
1852 /* if (xmlGetWarningsDefaultValue == 0) */
1853 if (warning == 0)
1854 hdlr->warning = NULL;
1855 else
1856 hdlr->warning = xmlParserWarning;
1857 hdlr->error = xmlParserError;
1858 hdlr->fatalError = xmlParserError;
1859
1860 hdlr->initialized = 1;
1861}
Owen Taylor3473f882001-02-23 17:55:21 +00001862
1863/**
1864 * xmlDefaultSAXHandlerInit:
1865 *
1866 * Initialize the default SAX handler
1867 */
1868void
1869xmlDefaultSAXHandlerInit(void)
1870{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001871 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001872}
1873
Daniel Veillardeae522a2001-04-23 13:41:34 +00001874#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001875
1876/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001877 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001878 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001879 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001880 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001881 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001882void
1883inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1884{
1885 if(hdlr->initialized == 1)
1886 return;
1887
1888 hdlr->internalSubset = internalSubset;
1889 hdlr->externalSubset = NULL;
1890 hdlr->isStandalone = NULL;
1891 hdlr->hasInternalSubset = NULL;
1892 hdlr->hasExternalSubset = NULL;
1893 hdlr->resolveEntity = NULL;
1894 hdlr->getEntity = getEntity;
1895 hdlr->getParameterEntity = NULL;
1896 hdlr->entityDecl = NULL;
1897 hdlr->attributeDecl = NULL;
1898 hdlr->elementDecl = NULL;
1899 hdlr->notationDecl = NULL;
1900 hdlr->unparsedEntityDecl = NULL;
1901 hdlr->setDocumentLocator = setDocumentLocator;
1902 hdlr->startDocument = startDocument;
1903 hdlr->endDocument = endDocument;
1904 hdlr->startElement = startElement;
1905 hdlr->endElement = endElement;
1906 hdlr->reference = NULL;
1907 hdlr->characters = characters;
1908 hdlr->cdataBlock = cdataBlock;
1909 hdlr->ignorableWhitespace = ignorableWhitespace;
1910 hdlr->processingInstruction = NULL;
1911 hdlr->comment = comment;
1912 hdlr->warning = xmlParserWarning;
1913 hdlr->error = xmlParserError;
1914 hdlr->fatalError = xmlParserError;
1915
1916 hdlr->initialized = 1;
1917}
Owen Taylor3473f882001-02-23 17:55:21 +00001918
1919/**
1920 * htmlDefaultSAXHandlerInit:
1921 *
1922 * Initialize the default SAX handler
1923 */
1924void
1925htmlDefaultSAXHandlerInit(void)
1926{
Daniel Veillardd0463562001-10-13 09:15:48 +00001927 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00001928}
Daniel Veillardd0463562001-10-13 09:15:48 +00001929
Daniel Veillardeae522a2001-04-23 13:41:34 +00001930#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001931
Daniel Veillardeae522a2001-04-23 13:41:34 +00001932#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001933
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001934/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001935 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001936 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001937 *
1938 * Initialize the default DocBook SAX handler
1939 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001940void
1941initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
1942{
1943 if(hdlr->initialized == 1)
1944 return;
1945
1946 hdlr->internalSubset = internalSubset;
1947 hdlr->externalSubset = NULL;
1948 hdlr->isStandalone = isStandalone;
1949 hdlr->hasInternalSubset = hasInternalSubset;
1950 hdlr->hasExternalSubset = hasExternalSubset;
1951 hdlr->resolveEntity = resolveEntity;
1952 hdlr->getEntity = getEntity;
1953 hdlr->getParameterEntity = NULL;
1954 hdlr->entityDecl = entityDecl;
1955 hdlr->attributeDecl = NULL;
1956 hdlr->elementDecl = NULL;
1957 hdlr->notationDecl = NULL;
1958 hdlr->unparsedEntityDecl = NULL;
1959 hdlr->setDocumentLocator = setDocumentLocator;
1960 hdlr->startDocument = startDocument;
1961 hdlr->endDocument = endDocument;
1962 hdlr->startElement = startElement;
1963 hdlr->endElement = endElement;
1964 hdlr->reference = reference;
1965 hdlr->characters = characters;
1966 hdlr->cdataBlock = NULL;
1967 hdlr->ignorableWhitespace = ignorableWhitespace;
1968 hdlr->processingInstruction = NULL;
1969 hdlr->comment = comment;
1970 hdlr->warning = xmlParserWarning;
1971 hdlr->error = xmlParserError;
1972 hdlr->fatalError = xmlParserError;
1973
1974 hdlr->initialized = 1;
1975}
Owen Taylor3473f882001-02-23 17:55:21 +00001976
1977/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00001978 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00001979 *
1980 * Initialize the default SAX handler
1981 */
1982void
Daniel Veillardeae522a2001-04-23 13:41:34 +00001983docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00001984{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001985 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00001986}
Daniel Veillardeae522a2001-04-23 13:41:34 +00001987
1988#endif /* LIBXML_DOCB_ENABLED */