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