blob: 9cf06a07639c173b279360a7a3b3073978544266 [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"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +0000910 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000911
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);
Owen Taylor3473f882001-02-23 17:55:21 +0000987 if (ns != NULL)
988 xmlFree(ns);
989}
990
Daniel Veillard878eab02002-02-19 13:46:09 +0000991/*
992 * xmlCheckDefaultedAttributes:
993 *
994 * Check defaulted attributes from the DTD
995 */
996static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000997xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +0000998 const xmlChar *prefix, const xmlChar **atts) {
999 xmlElementPtr elemDecl;
1000 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001001 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001002 int i;
1003
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001004 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1005 if (elemDecl == NULL) {
1006 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1007 internal = 0;
1008 }
1009
1010process_external_subset:
1011
Daniel Veillard878eab02002-02-19 13:46:09 +00001012 if (elemDecl != NULL) {
1013 xmlAttributePtr attr = elemDecl->attributes;
1014 /*
1015 * Check against defaulted attributes from the external subset
1016 * if the document is stamped as standalone
1017 */
1018 if ((ctxt->myDoc->standalone == 1) &&
1019 (ctxt->myDoc->extSubset != NULL) &&
1020 (ctxt->validate)) {
1021 while (attr != NULL) {
1022 if ((attr->defaultValue != NULL) &&
1023 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1024 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001025 attr->prefix) == attr) &&
1026 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1027 attr->elem, attr->name,
1028 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001029 xmlChar *fulln;
1030
1031 if (attr->prefix != NULL) {
1032 fulln = xmlStrdup(attr->prefix);
1033 fulln = xmlStrcat(fulln, BAD_CAST ":");
1034 fulln = xmlStrcat(fulln, attr->name);
1035 } else {
1036 fulln = xmlStrdup(attr->name);
1037 }
1038
1039 /*
1040 * Check that the attribute is not declared in the
1041 * serialization
1042 */
1043 att = NULL;
1044 if (atts != NULL) {
1045 i = 0;
1046 att = atts[i];
1047 while (att != NULL) {
1048 if (xmlStrEqual(att, fulln))
1049 break;
1050 i += 2;
1051 att = atts[i];
1052 }
1053 }
1054 if (att == NULL) {
1055 if (ctxt->vctxt.error != NULL)
1056 ctxt->vctxt.error(ctxt->vctxt.userData,
1057 "standalone: attribute %s on %s defaulted from external subset\n",
1058 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001059 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001060 }
1061 }
1062 attr = attr->nexth;
1063 }
1064 }
1065
1066 /*
1067 * Actually insert defaulted values when needed
1068 */
1069 attr = elemDecl->attributes;
1070 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001071 /*
1072 * Make sure that attributes redefinition occuring in the
1073 * internal subset are not overriden by definitions in the
1074 * external subset.
1075 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001076 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001077 /*
1078 * the element should be instantiated in the tree if:
1079 * - this is a namespace prefix
1080 * - the user required for completion in the tree
1081 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001082 * - there isn't already an attribute definition
1083 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001084 */
1085 if (((attr->prefix != NULL) &&
1086 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1087 ((attr->prefix == NULL) &&
1088 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1089 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001090 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001091
Daniel Veillard8aff2472002-02-19 21:50:43 +00001092 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1093 attr->elem, attr->name,
1094 attr->prefix);
1095 if ((tst == attr) || (tst == NULL)) {
1096 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001097
Daniel Veillard8aff2472002-02-19 21:50:43 +00001098 if (attr->prefix != NULL) {
1099 fulln = xmlStrdup(attr->prefix);
1100 fulln = xmlStrcat(fulln, BAD_CAST ":");
1101 fulln = xmlStrcat(fulln, attr->name);
1102 } else {
1103 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001104 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001105
1106 /*
1107 * Check that the attribute is not declared in the
1108 * serialization
1109 */
1110 att = NULL;
1111 if (atts != NULL) {
1112 i = 0;
1113 att = atts[i];
1114 while (att != NULL) {
1115 if (xmlStrEqual(att, fulln))
1116 break;
1117 i += 2;
1118 att = atts[i];
1119 }
1120 }
1121 if (att == NULL) {
1122 attribute(ctxt, fulln, attr->defaultValue);
1123 }
1124 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001125 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001126 }
1127 }
1128 attr = attr->nexth;
1129 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001130 if (internal == 1) {
1131 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1132 name, prefix);
1133 internal = 0;
1134 goto process_external_subset;
1135 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001136 }
1137}
1138
Owen Taylor3473f882001-02-23 17:55:21 +00001139/**
1140 * startElement:
1141 * @ctx: the user data (XML parser context)
1142 * @fullname: The element name, including namespace prefix
1143 * @atts: An array of name/value attributes pairs, NULL terminated
1144 *
1145 * called when an opening tag has been processed.
1146 */
1147void
1148startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1149{
1150 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1151 xmlNodePtr ret;
1152 xmlNodePtr parent = ctxt->node;
1153 xmlNsPtr ns;
1154 xmlChar *name;
1155 xmlChar *prefix;
1156 const xmlChar *att;
1157 const xmlChar *value;
1158 int i;
1159
1160#ifdef DEBUG_SAX
1161 xmlGenericError(xmlGenericErrorContext,
1162 "SAX.startElement(%s)\n", fullname);
1163#endif
1164
1165 /*
1166 * First check on validity:
1167 */
1168 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1169 ((ctxt->myDoc->intSubset == NULL) ||
1170 ((ctxt->myDoc->intSubset->notations == NULL) &&
1171 (ctxt->myDoc->intSubset->elements == NULL) &&
1172 (ctxt->myDoc->intSubset->attributes == NULL) &&
1173 (ctxt->myDoc->intSubset->entities == NULL)))) {
1174 if (ctxt->vctxt.error != NULL) {
1175 ctxt->vctxt.error(ctxt->vctxt.userData,
1176 "Validation failed: no DTD found !\n");
1177 }
1178 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001179 ctxt->valid = 0;
1180 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001181 }
1182
1183
1184 /*
1185 * Split the full name into a namespace prefix and the tag name
1186 */
1187 name = xmlSplitQName(ctxt, fullname, &prefix);
1188
1189
1190 /*
1191 * Note : the namespace resolution is deferred until the end of the
1192 * attributes parsing, since local namespace can be defined as
1193 * an attribute at this level.
1194 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001195 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001196 if (ret == NULL) return;
1197 if (ctxt->myDoc->children == NULL) {
1198#ifdef DEBUG_SAX_TREE
1199 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1200#endif
1201 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1202 } else if (parent == NULL) {
1203 parent = ctxt->myDoc->children;
1204 }
1205 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001206 if (ctxt->linenumbers) {
1207 if (ctxt->input != NULL)
1208 ret->content = (void *) (long) ctxt->input->line;
1209 }
Owen Taylor3473f882001-02-23 17:55:21 +00001210
1211 /*
1212 * We are parsing a new node.
1213 */
1214#ifdef DEBUG_SAX_TREE
1215 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1216#endif
1217 nodePush(ctxt, ret);
1218
1219 /*
1220 * Link the child element
1221 */
1222 if (parent != NULL) {
1223 if (parent->type == XML_ELEMENT_NODE) {
1224#ifdef DEBUG_SAX_TREE
1225 xmlGenericError(xmlGenericErrorContext,
1226 "adding child %s to %s\n", name, parent->name);
1227#endif
1228 xmlAddChild(parent, ret);
1229 } else {
1230#ifdef DEBUG_SAX_TREE
1231 xmlGenericError(xmlGenericErrorContext,
1232 "adding sibling %s to ", name);
1233 xmlDebugDumpOneNode(stderr, parent, 0);
1234#endif
1235 xmlAddSibling(parent, ret);
1236 }
1237 }
1238
1239 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001240 * Insert all the defaulted attributes from the DTD especially namespaces
1241 */
1242 if ((!ctxt->html) &&
1243 ((ctxt->myDoc->intSubset != NULL) ||
1244 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001245 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001246 }
1247
1248 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001249 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001250 */
1251 if (atts != NULL) {
1252 i = 0;
1253 att = atts[i++];
1254 value = atts[i++];
1255 if (!ctxt->html) {
1256 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001257 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1258 (att[3] == 'n') && (att[4] == 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001259 attribute(ctxt, att, value);
1260
1261 att = atts[i++];
1262 value = atts[i++];
1263 }
1264 }
1265 }
1266
1267 /*
1268 * Search the namespace, note that since the attributes have been
1269 * processed, the local namespaces are available.
1270 */
1271 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1272 if ((ns == NULL) && (parent != NULL))
1273 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1274 if ((prefix != NULL) && (ns == NULL)) {
1275 ns = xmlNewNs(ret, NULL, prefix);
1276 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1277 ctxt->sax->warning(ctxt->userData,
1278 "Namespace prefix %s is not defined\n", prefix);
1279 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001280
1281 /*
1282 * set the namespace node, making sure that if the default namspace
1283 * is unbound on a parent we simply kee it NULL
1284 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001285 if ((ns != NULL) && (ns->href != NULL) &&
1286 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001287 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001288
1289 /*
1290 * process all the other attributes
1291 */
1292 if (atts != NULL) {
1293 i = 0;
1294 att = atts[i++];
1295 value = atts[i++];
1296 if (ctxt->html) {
1297 while (att != NULL) {
1298 attribute(ctxt, att, value);
1299 att = atts[i++];
1300 value = atts[i++];
1301 }
1302 } else {
1303 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001304 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1305 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001306 attribute(ctxt, att, value);
1307
1308 /*
1309 * Next ones
1310 */
1311 att = atts[i++];
1312 value = atts[i++];
1313 }
1314 }
1315 }
1316
1317 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001318 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001319 * check the document root element for validity
1320 */
1321 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001322 int chk;
1323
1324 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1325 if (chk <= 0)
1326 ctxt->valid = 0;
1327 if (chk < 0)
1328 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001329 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1330 ctxt->vctxt.finishDtd = 1;
1331 }
1332
1333 if (prefix != NULL)
1334 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001335
1336}
1337
1338/**
1339 * endElement:
1340 * @ctx: the user data (XML parser context)
1341 * @name: The element name
1342 *
1343 * called when the end of an element has been detected.
1344 */
1345void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001346endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001347{
1348 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1349 xmlParserNodeInfo node_info;
1350 xmlNodePtr cur = ctxt->node;
1351
1352#ifdef DEBUG_SAX
1353 if (name == NULL)
1354 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1355 else
1356 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1357#endif
1358
1359 /* Capture end position and add node */
1360 if (cur != NULL && ctxt->record_info) {
1361 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1362 node_info.end_line = ctxt->input->line;
1363 node_info.node = cur;
1364 xmlParserAddNodeInfo(ctxt, &node_info);
1365 }
1366 ctxt->nodemem = -1;
1367
1368 if (ctxt->validate && ctxt->wellFormed &&
1369 ctxt->myDoc && ctxt->myDoc->intSubset)
1370 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1371 cur);
1372
1373
1374 /*
1375 * end of parsing of this node.
1376 */
1377#ifdef DEBUG_SAX_TREE
1378 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1379#endif
1380 nodePop(ctxt);
1381}
1382
1383/**
1384 * reference:
1385 * @ctx: the user data (XML parser context)
1386 * @name: The entity name
1387 *
1388 * called when an entity reference is detected.
1389 */
1390void
1391reference(void *ctx, const xmlChar *name)
1392{
1393 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1394 xmlNodePtr ret;
1395
1396#ifdef DEBUG_SAX
1397 xmlGenericError(xmlGenericErrorContext,
1398 "SAX.reference(%s)\n", name);
1399#endif
1400 if (name[0] == '#')
1401 ret = xmlNewCharRef(ctxt->myDoc, name);
1402 else
1403 ret = xmlNewReference(ctxt->myDoc, name);
1404#ifdef DEBUG_SAX_TREE
1405 xmlGenericError(xmlGenericErrorContext,
1406 "add reference %s to %s \n", name, ctxt->node->name);
1407#endif
1408 xmlAddChild(ctxt->node, ret);
1409}
1410
1411/**
1412 * characters:
1413 * @ctx: the user data (XML parser context)
1414 * @ch: a xmlChar string
1415 * @len: the number of xmlChar
1416 *
1417 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001418 */
1419void
1420characters(void *ctx, const xmlChar *ch, int len)
1421{
1422 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1423 xmlNodePtr lastChild;
1424
1425#ifdef DEBUG_SAX
1426 xmlGenericError(xmlGenericErrorContext,
1427 "SAX.characters(%.30s, %d)\n", ch, len);
1428#endif
1429 /*
1430 * Handle the data if any. If there is no child
1431 * add it as content, otherwise if the last child is text,
1432 * concatenate it, else create a new node of type text.
1433 */
1434
1435 if (ctxt->node == NULL) {
1436#ifdef DEBUG_SAX_TREE
1437 xmlGenericError(xmlGenericErrorContext,
1438 "add chars: ctxt->node == NULL !\n");
1439#endif
1440 return;
1441 }
1442 lastChild = xmlGetLastChild(ctxt->node);
1443#ifdef DEBUG_SAX_TREE
1444 xmlGenericError(xmlGenericErrorContext,
1445 "add chars to %s \n", ctxt->node->name);
1446#endif
1447
1448 /*
1449 * Here we needed an accelerator mechanism in case of very large
1450 * elements. Use an attribute in the structure !!!
1451 */
1452 if (lastChild == NULL) {
1453 /* first node, first time */
1454 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001455 if (ctxt->node->children != NULL) {
1456 ctxt->nodelen = len;
1457 ctxt->nodemem = len + 1;
1458 }
Owen Taylor3473f882001-02-23 17:55:21 +00001459 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001460 int coalesceText = (lastChild != NULL) &&
1461 (lastChild->type == XML_TEXT_NODE) &&
1462 (lastChild->name == xmlStringText);
1463 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001464 /*
1465 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001466 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001467 * reallocate a new buffer, move data, append ch. Here
1468 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001469 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001470 */
1471 if (ctxt->nodelen + len >= ctxt->nodemem) {
1472 xmlChar *newbuf;
1473 int size;
1474
1475 size = ctxt->nodemem + len;
1476 size *= 2;
1477 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1478 if (newbuf == NULL) {
1479 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1480 ctxt->sax->error(ctxt->userData,
1481 "SAX.characters(): out of memory\n");
1482 return;
1483 }
1484 ctxt->nodemem = size;
1485 lastChild->content = newbuf;
1486 }
1487 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1488 ctxt->nodelen += len;
1489 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001490 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001491 xmlTextConcat(lastChild, ch, len);
1492 if (ctxt->node->children != NULL) {
1493 ctxt->nodelen = xmlStrlen(lastChild->content);
1494 ctxt->nodemem = ctxt->nodelen + 1;
1495 }
Owen Taylor3473f882001-02-23 17:55:21 +00001496 } else {
1497 /* Mixed content, first time */
1498 lastChild = xmlNewTextLen(ch, len);
1499 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001500 if (ctxt->node->children != NULL) {
1501 ctxt->nodelen = len;
1502 ctxt->nodemem = len + 1;
1503 }
Owen Taylor3473f882001-02-23 17:55:21 +00001504 }
1505 }
1506}
1507
1508/**
1509 * ignorableWhitespace:
1510 * @ctx: the user data (XML parser context)
1511 * @ch: a xmlChar string
1512 * @len: the number of xmlChar
1513 *
1514 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001515 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001516 */
1517void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001518ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001519{
1520 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1521#ifdef DEBUG_SAX
1522 xmlGenericError(xmlGenericErrorContext,
1523 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1524#endif
1525}
1526
1527/**
1528 * processingInstruction:
1529 * @ctx: the user data (XML parser context)
1530 * @target: the target name
1531 * @data: the PI data's
1532 *
1533 * A processing instruction has been parsed.
1534 */
1535void
1536processingInstruction(void *ctx, const xmlChar *target,
1537 const xmlChar *data)
1538{
1539 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1540 xmlNodePtr ret;
1541 xmlNodePtr parent = ctxt->node;
1542
1543#ifdef DEBUG_SAX
1544 xmlGenericError(xmlGenericErrorContext,
1545 "SAX.processingInstruction(%s, %s)\n", target, data);
1546#endif
1547
1548 ret = xmlNewPI(target, data);
1549 if (ret == NULL) return;
1550 parent = ctxt->node;
1551
1552 if (ctxt->inSubset == 1) {
1553 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1554 return;
1555 } else if (ctxt->inSubset == 2) {
1556 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1557 return;
1558 }
1559 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1560#ifdef DEBUG_SAX_TREE
1561 xmlGenericError(xmlGenericErrorContext,
1562 "Setting PI %s as root\n", target);
1563#endif
1564 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1565 return;
1566 }
1567 if (parent->type == XML_ELEMENT_NODE) {
1568#ifdef DEBUG_SAX_TREE
1569 xmlGenericError(xmlGenericErrorContext,
1570 "adding PI %s child to %s\n", target, parent->name);
1571#endif
1572 xmlAddChild(parent, ret);
1573 } else {
1574#ifdef DEBUG_SAX_TREE
1575 xmlGenericError(xmlGenericErrorContext,
1576 "adding PI %s sibling to ", target);
1577 xmlDebugDumpOneNode(stderr, parent, 0);
1578#endif
1579 xmlAddSibling(parent, ret);
1580 }
1581}
1582
1583/**
1584 * globalNamespace:
1585 * @ctx: the user data (XML parser context)
1586 * @href: the namespace associated URN
1587 * @prefix: the namespace prefix
1588 *
1589 * An old global namespace has been parsed.
1590 */
1591void
1592globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1593{
1594 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1595#ifdef DEBUG_SAX
1596 xmlGenericError(xmlGenericErrorContext,
1597 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1598#endif
1599 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1600}
1601
1602/**
1603 * setNamespace:
1604 * @ctx: the user data (XML parser context)
1605 * @name: the namespace prefix
1606 *
1607 * Set the current element namespace.
1608 */
1609
1610void
1611setNamespace(void *ctx, const xmlChar *name)
1612{
1613 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1614 xmlNsPtr ns;
1615 xmlNodePtr parent;
1616
1617#ifdef DEBUG_SAX
1618 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1619#endif
1620 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1621 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1622 if (ctxt->nodeNr >= 2) {
1623 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1624 if (parent != NULL)
1625 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1626 }
1627 }
1628 xmlSetNs(ctxt->node, ns);
1629}
1630
1631/**
1632 * getNamespace:
1633 * @ctx: the user data (XML parser context)
1634 *
1635 * Get the current element namespace.
1636 *
1637 * Returns the xmlNsPtr or NULL if none
1638 */
1639
1640xmlNsPtr
1641getNamespace(void *ctx)
1642{
1643 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1644 xmlNsPtr ret;
1645
1646#ifdef DEBUG_SAX
1647 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1648#endif
1649 ret = ctxt->node->ns;
1650 return(ret);
1651}
1652
1653/**
1654 * checkNamespace:
1655 * @ctx: the user data (XML parser context)
1656 * @namespace: the namespace to check against
1657 *
1658 * Check that the current element namespace is the same as the
1659 * one read upon parsing.
1660 *
1661 * Returns 1 if true 0 otherwise
1662 */
1663
1664int
1665checkNamespace(void *ctx, xmlChar *namespace)
1666{
1667 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1668 xmlNodePtr cur = ctxt->node;
1669
1670#ifdef DEBUG_SAX
1671 xmlGenericError(xmlGenericErrorContext,
1672 "SAX.checkNamespace(%s)\n", namespace);
1673#endif
1674
1675 /*
1676 * Check that the Name in the ETag is the same as in the STag.
1677 */
1678 if (namespace == NULL) {
1679 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1680 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1681 ctxt->sax->error(ctxt,
1682 "End tags for %s don't hold the namespace %s\n",
1683 cur->name, cur->ns->prefix);
1684 ctxt->wellFormed = 0;
1685 }
1686 } else {
1687 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1688 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1689 ctxt->sax->error(ctxt,
1690 "End tags %s holds a prefix %s not used by the open tag\n",
1691 cur->name, namespace);
1692 ctxt->wellFormed = 0;
1693 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1694 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1695 ctxt->sax->error(ctxt,
1696 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1697 cur->name, cur->ns->prefix, namespace);
1698 ctxt->wellFormed = 0;
1699 } else
1700 return(1);
1701 }
1702 return(0);
1703}
1704
1705/**
1706 * namespaceDecl:
1707 * @ctx: the user data (XML parser context)
1708 * @href: the namespace associated URN
1709 * @prefix: the namespace prefix
1710 *
1711 * A namespace has been parsed.
1712 */
1713void
1714namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1715{
1716 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1717#ifdef DEBUG_SAX
1718 if (prefix == NULL)
1719 xmlGenericError(xmlGenericErrorContext,
1720 "SAX.namespaceDecl(%s, NULL)\n", href);
1721 else
1722 xmlGenericError(xmlGenericErrorContext,
1723 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1724#endif
1725 xmlNewNs(ctxt->node, href, prefix);
1726}
1727
1728/**
1729 * comment:
1730 * @ctx: the user data (XML parser context)
1731 * @value: the comment content
1732 *
1733 * A comment has been parsed.
1734 */
1735void
1736comment(void *ctx, const xmlChar *value)
1737{
1738 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1739 xmlNodePtr ret;
1740 xmlNodePtr parent = ctxt->node;
1741
1742#ifdef DEBUG_SAX
1743 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1744#endif
1745 ret = xmlNewDocComment(ctxt->myDoc, value);
1746 if (ret == NULL) return;
1747
1748 if (ctxt->inSubset == 1) {
1749 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1750 return;
1751 } else if (ctxt->inSubset == 2) {
1752 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1753 return;
1754 }
1755 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1756#ifdef DEBUG_SAX_TREE
1757 xmlGenericError(xmlGenericErrorContext,
1758 "Setting comment as root\n");
1759#endif
1760 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1761 return;
1762 }
1763 if (parent->type == XML_ELEMENT_NODE) {
1764#ifdef DEBUG_SAX_TREE
1765 xmlGenericError(xmlGenericErrorContext,
1766 "adding comment child to %s\n", parent->name);
1767#endif
1768 xmlAddChild(parent, ret);
1769 } else {
1770#ifdef DEBUG_SAX_TREE
1771 xmlGenericError(xmlGenericErrorContext,
1772 "adding comment sibling to ");
1773 xmlDebugDumpOneNode(stderr, parent, 0);
1774#endif
1775 xmlAddSibling(parent, ret);
1776 }
1777}
1778
1779/**
1780 * cdataBlock:
1781 * @ctx: the user data (XML parser context)
1782 * @value: The pcdata content
1783 * @len: the block length
1784 *
1785 * called when a pcdata block has been parsed
1786 */
1787void
1788cdataBlock(void *ctx, const xmlChar *value, int len)
1789{
1790 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1791 xmlNodePtr ret, lastChild;
1792
1793#ifdef DEBUG_SAX
1794 xmlGenericError(xmlGenericErrorContext,
1795 "SAX.pcdata(%.10s, %d)\n", value, len);
1796#endif
1797 lastChild = xmlGetLastChild(ctxt->node);
1798#ifdef DEBUG_SAX_TREE
1799 xmlGenericError(xmlGenericErrorContext,
1800 "add chars to %s \n", ctxt->node->name);
1801#endif
1802 if ((lastChild != NULL) &&
1803 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1804 xmlTextConcat(lastChild, value, len);
1805 } else {
1806 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1807 xmlAddChild(ctxt->node, ret);
1808 }
1809}
1810
Daniel Veillardd0463562001-10-13 09:15:48 +00001811/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001812 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001813 * @hdlr: the SAX handler
1814 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001815 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001816 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001817 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001818void
1819initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1820{
1821 if(hdlr->initialized == 1)
1822 return;
1823
1824 hdlr->internalSubset = internalSubset;
1825 hdlr->externalSubset = externalSubset;
1826 hdlr->isStandalone = isStandalone;
1827 hdlr->hasInternalSubset = hasInternalSubset;
1828 hdlr->hasExternalSubset = hasExternalSubset;
1829 hdlr->resolveEntity = resolveEntity;
1830 hdlr->getEntity = getEntity;
1831 hdlr->getParameterEntity = getParameterEntity;
1832 hdlr->entityDecl = entityDecl;
1833 hdlr->attributeDecl = attributeDecl;
1834 hdlr->elementDecl = elementDecl;
1835 hdlr->notationDecl = notationDecl;
1836 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1837 hdlr->setDocumentLocator = setDocumentLocator;
1838 hdlr->startDocument = startDocument;
1839 hdlr->endDocument = endDocument;
1840 hdlr->startElement = startElement;
1841 hdlr->endElement = endElement;
1842 hdlr->reference = reference;
1843 hdlr->characters = characters;
1844 hdlr->cdataBlock = cdataBlock;
1845 hdlr->ignorableWhitespace = characters;
1846 hdlr->processingInstruction = processingInstruction;
1847 hdlr->comment = comment;
1848 /* if (xmlGetWarningsDefaultValue == 0) */
1849 if (warning == 0)
1850 hdlr->warning = NULL;
1851 else
1852 hdlr->warning = xmlParserWarning;
1853 hdlr->error = xmlParserError;
1854 hdlr->fatalError = xmlParserError;
1855
1856 hdlr->initialized = 1;
1857}
Owen Taylor3473f882001-02-23 17:55:21 +00001858
1859/**
1860 * xmlDefaultSAXHandlerInit:
1861 *
1862 * Initialize the default SAX handler
1863 */
1864void
1865xmlDefaultSAXHandlerInit(void)
1866{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001867 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001868}
1869
Daniel Veillardeae522a2001-04-23 13:41:34 +00001870#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001871
1872/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001873 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001874 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001875 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001876 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001877 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001878void
1879inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1880{
1881 if(hdlr->initialized == 1)
1882 return;
1883
1884 hdlr->internalSubset = internalSubset;
1885 hdlr->externalSubset = NULL;
1886 hdlr->isStandalone = NULL;
1887 hdlr->hasInternalSubset = NULL;
1888 hdlr->hasExternalSubset = NULL;
1889 hdlr->resolveEntity = NULL;
1890 hdlr->getEntity = getEntity;
1891 hdlr->getParameterEntity = NULL;
1892 hdlr->entityDecl = NULL;
1893 hdlr->attributeDecl = NULL;
1894 hdlr->elementDecl = NULL;
1895 hdlr->notationDecl = NULL;
1896 hdlr->unparsedEntityDecl = NULL;
1897 hdlr->setDocumentLocator = setDocumentLocator;
1898 hdlr->startDocument = startDocument;
1899 hdlr->endDocument = endDocument;
1900 hdlr->startElement = startElement;
1901 hdlr->endElement = endElement;
1902 hdlr->reference = NULL;
1903 hdlr->characters = characters;
1904 hdlr->cdataBlock = cdataBlock;
1905 hdlr->ignorableWhitespace = ignorableWhitespace;
1906 hdlr->processingInstruction = NULL;
1907 hdlr->comment = comment;
1908 hdlr->warning = xmlParserWarning;
1909 hdlr->error = xmlParserError;
1910 hdlr->fatalError = xmlParserError;
1911
1912 hdlr->initialized = 1;
1913}
Owen Taylor3473f882001-02-23 17:55:21 +00001914
1915/**
1916 * htmlDefaultSAXHandlerInit:
1917 *
1918 * Initialize the default SAX handler
1919 */
1920void
1921htmlDefaultSAXHandlerInit(void)
1922{
Daniel Veillardd0463562001-10-13 09:15:48 +00001923 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00001924}
Daniel Veillardd0463562001-10-13 09:15:48 +00001925
Daniel Veillardeae522a2001-04-23 13:41:34 +00001926#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001927
Daniel Veillardeae522a2001-04-23 13:41:34 +00001928#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001929
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001930/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001931 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001932 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001933 *
1934 * Initialize the default DocBook SAX handler
1935 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001936void
1937initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
1938{
1939 if(hdlr->initialized == 1)
1940 return;
1941
1942 hdlr->internalSubset = internalSubset;
1943 hdlr->externalSubset = NULL;
1944 hdlr->isStandalone = isStandalone;
1945 hdlr->hasInternalSubset = hasInternalSubset;
1946 hdlr->hasExternalSubset = hasExternalSubset;
1947 hdlr->resolveEntity = resolveEntity;
1948 hdlr->getEntity = getEntity;
1949 hdlr->getParameterEntity = NULL;
1950 hdlr->entityDecl = entityDecl;
1951 hdlr->attributeDecl = NULL;
1952 hdlr->elementDecl = NULL;
1953 hdlr->notationDecl = NULL;
1954 hdlr->unparsedEntityDecl = NULL;
1955 hdlr->setDocumentLocator = setDocumentLocator;
1956 hdlr->startDocument = startDocument;
1957 hdlr->endDocument = endDocument;
1958 hdlr->startElement = startElement;
1959 hdlr->endElement = endElement;
1960 hdlr->reference = reference;
1961 hdlr->characters = characters;
1962 hdlr->cdataBlock = NULL;
1963 hdlr->ignorableWhitespace = ignorableWhitespace;
1964 hdlr->processingInstruction = NULL;
1965 hdlr->comment = comment;
1966 hdlr->warning = xmlParserWarning;
1967 hdlr->error = xmlParserError;
1968 hdlr->fatalError = xmlParserError;
1969
1970 hdlr->initialized = 1;
1971}
Owen Taylor3473f882001-02-23 17:55:21 +00001972
1973/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00001974 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00001975 *
1976 * Initialize the default SAX handler
1977 */
1978void
Daniel Veillardeae522a2001-04-23 13:41:34 +00001979docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00001980{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001981 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00001982}
Daniel Veillardeae522a2001-04-23 13:41:34 +00001983
1984#endif /* LIBXML_DOCB_ENABLED */