blob: c8452ebf912710f1d85269585851158eebc58ff4 [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/**
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000798 * my_attribute:
Owen Taylor3473f882001-02-23 17:55:21 +0000799 * @ctx: the user data (XML parser context)
800 * @fullname: The attribute name, including namespace prefix
801 * @value: The attribute value
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000802 * @prefix: the prefix on the element node
Owen Taylor3473f882001-02-23 17:55:21 +0000803 *
804 * Handle an attribute that has been read by the parser.
805 * The default handling is to convert the attribute into an
806 * DOM subtree and past it in a new xmlAttr element added to
807 * the element.
808 */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000809static void
810my_attribute(void *ctx, const xmlChar *fullname, const xmlChar *value,
811 const xmlChar *prefix)
Owen Taylor3473f882001-02-23 17:55:21 +0000812{
813 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
814 xmlAttrPtr ret;
815 xmlChar *name;
816 xmlChar *ns;
817 xmlChar *nval;
818 xmlNsPtr namespace;
819
820/****************
821#ifdef DEBUG_SAX
822 xmlGenericError(xmlGenericErrorContext,
823 "SAX.attribute(%s, %s)\n", fullname, value);
824#endif
825 ****************/
826 /*
827 * Split the full name into a namespace prefix and the tag name
828 */
829 name = xmlSplitQName(ctxt, fullname, &ns);
830
831 /*
832 * Do the last stage of the attribute normalization
833 * Needed for HTML too:
834 * http://www.w3.org/TR/html4/types.html#h-6.2
835 */
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000836 ctxt->vctxt.valid = 1;
837 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
838 ctxt->myDoc, ctxt->node,
Owen Taylor3473f882001-02-23 17:55:21 +0000839 fullname, value);
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000840 if (ctxt->vctxt.valid != 1) {
841 ctxt->valid = 0;
842 }
Owen Taylor3473f882001-02-23 17:55:21 +0000843 if (nval != NULL)
844 value = nval;
845
846 /*
847 * Check whether it's a namespace definition
848 */
849 if ((!ctxt->html) && (ns == NULL) &&
850 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
851 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000852 xmlNsPtr nsret;
853
Owen Taylor3473f882001-02-23 17:55:21 +0000854 if (value[0] != 0) {
855 xmlURIPtr uri;
856
857 uri = xmlParseURI((const char *)value);
858 if (uri == NULL) {
859 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
860 ctxt->sax->warning(ctxt->userData,
861 "nmlns: %s not a valid URI\n", value);
862 } else {
863 if (uri->scheme == NULL) {
864 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
865 ctxt->sax->warning(ctxt->userData,
866 "nmlns: URI %s is not absolute\n", value);
867 }
868 xmlFreeURI(uri);
869 }
870 }
871
872 /* a default namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000873 nsret = xmlNewNs(ctxt->node, value, NULL);
874
875 /*
876 * Validate also for namespace decls, they are attributes from
877 * an XML-1.0 perspective
878 */
879 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
880 ctxt->myDoc && ctxt->myDoc->intSubset)
881 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
882 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000883 if (name != NULL)
884 xmlFree(name);
885 if (nval != NULL)
886 xmlFree(nval);
887 return;
888 }
889 if ((!ctxt->html) &&
890 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
891 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000892 xmlNsPtr nsret;
893
Daniel Veillardc0fef772002-03-01 16:16:31 +0000894 if (value[0] == 0) {
895 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
896 ctxt->sax->error(ctxt->userData,
897 "Empty namespace name for prefix %s\n", name);
898 }
Owen Taylor3473f882001-02-23 17:55:21 +0000899 /* a standard namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000900 nsret = xmlNewNs(ctxt->node, value, name);
Owen Taylor3473f882001-02-23 17:55:21 +0000901 xmlFree(ns);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000902 /*
903 * Validate also for namespace decls, they are attributes from
904 * an XML-1.0 perspective
905 */
906 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
907 ctxt->myDoc && ctxt->myDoc->intSubset)
908 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
909 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000910 if (name != NULL)
911 xmlFree(name);
912 if (nval != NULL)
913 xmlFree(nval);
914 return;
915 }
916
917 if (ns != NULL)
918 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
919 else {
920 namespace = NULL;
921 }
922
923 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +0000924 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000925
926 if (ret != NULL) {
927 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
928 xmlNodePtr tmp;
929
930 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
931 tmp = ret->children;
932 while (tmp != NULL) {
933 tmp->parent = (xmlNodePtr) ret;
934 if (tmp->next == NULL)
935 ret->last = tmp;
936 tmp = tmp->next;
937 }
938 } else if (value != NULL) {
939 ret->children = xmlNewDocText(ctxt->myDoc, value);
940 ret->last = ret->children;
941 if (ret->children != NULL)
942 ret->children->parent = (xmlNodePtr) ret;
943 }
944 }
945
946 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
947 ctxt->myDoc && ctxt->myDoc->intSubset) {
948
949 /*
950 * If we don't substitute entities, the validation should be
951 * done on a value with replaced entities anyway.
952 */
953 if (!ctxt->replaceEntities) {
954 xmlChar *val;
955
956 ctxt->depth++;
957 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
958 0,0,0);
959 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +0000960
Owen Taylor3473f882001-02-23 17:55:21 +0000961 if (val == NULL)
962 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
963 ctxt->myDoc, ctxt->node, ret, value);
964 else {
Daniel Veillardc7612992002-02-17 22:47:37 +0000965 xmlChar *nvalnorm;
966
967 /*
968 * Do the last stage of the attribute normalization
969 * It need to be done twice ... it's an extra burden related
970 * to the ability to keep references in attributes
971 */
972 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
973 ctxt->node, fullname, val);
974 if (nvalnorm != NULL) {
975 xmlFree(val);
976 val = nvalnorm;
977 }
978
Owen Taylor3473f882001-02-23 17:55:21 +0000979 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
980 ctxt->myDoc, ctxt->node, ret, val);
981 xmlFree(val);
982 }
983 } else {
984 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
985 ctxt->node, ret, value);
986 }
Daniel Veillard62f313b2001-07-04 19:49:14 +0000987 } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
988 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +0000989 /*
990 * when validating, the ID registration is done at the attribute
991 * validation level. Otherwise we have to do specific handling here.
992 */
993 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
994 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
995 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
996 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
997 }
998
999 if (nval != NULL)
1000 xmlFree(nval);
Owen Taylor3473f882001-02-23 17:55:21 +00001001 if (ns != NULL)
1002 xmlFree(ns);
1003}
1004
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001005/**
1006 * attribute:
1007 * @ctx: the user data (XML parser context)
1008 * @fullname: The attribute name, including namespace prefix
1009 * @value: The attribute value
1010 *
1011 * Handle an attribute that has been read by the parser.
1012 * The default handling is to convert the attribute into an
1013 * DOM subtree and past it in a new xmlAttr element added to
1014 * the element.
1015 */
1016void
1017attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1018{
1019 my_attribute(ctx, fullname, value, NULL);
1020}
1021
Daniel Veillard878eab02002-02-19 13:46:09 +00001022/*
1023 * xmlCheckDefaultedAttributes:
1024 *
1025 * Check defaulted attributes from the DTD
1026 */
1027static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001028xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001029 const xmlChar *prefix, const xmlChar **atts) {
1030 xmlElementPtr elemDecl;
1031 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001032 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001033 int i;
1034
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001035 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1036 if (elemDecl == NULL) {
1037 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1038 internal = 0;
1039 }
1040
1041process_external_subset:
1042
Daniel Veillard878eab02002-02-19 13:46:09 +00001043 if (elemDecl != NULL) {
1044 xmlAttributePtr attr = elemDecl->attributes;
1045 /*
1046 * Check against defaulted attributes from the external subset
1047 * if the document is stamped as standalone
1048 */
1049 if ((ctxt->myDoc->standalone == 1) &&
1050 (ctxt->myDoc->extSubset != NULL) &&
1051 (ctxt->validate)) {
1052 while (attr != NULL) {
1053 if ((attr->defaultValue != NULL) &&
1054 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1055 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001056 attr->prefix) == attr) &&
1057 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1058 attr->elem, attr->name,
1059 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001060 xmlChar *fulln;
1061
1062 if (attr->prefix != NULL) {
1063 fulln = xmlStrdup(attr->prefix);
1064 fulln = xmlStrcat(fulln, BAD_CAST ":");
1065 fulln = xmlStrcat(fulln, attr->name);
1066 } else {
1067 fulln = xmlStrdup(attr->name);
1068 }
1069
1070 /*
1071 * Check that the attribute is not declared in the
1072 * serialization
1073 */
1074 att = NULL;
1075 if (atts != NULL) {
1076 i = 0;
1077 att = atts[i];
1078 while (att != NULL) {
1079 if (xmlStrEqual(att, fulln))
1080 break;
1081 i += 2;
1082 att = atts[i];
1083 }
1084 }
1085 if (att == NULL) {
1086 if (ctxt->vctxt.error != NULL)
1087 ctxt->vctxt.error(ctxt->vctxt.userData,
1088 "standalone: attribute %s on %s defaulted from external subset\n",
1089 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001090 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001091 }
1092 }
1093 attr = attr->nexth;
1094 }
1095 }
1096
1097 /*
1098 * Actually insert defaulted values when needed
1099 */
1100 attr = elemDecl->attributes;
1101 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001102 /*
1103 * Make sure that attributes redefinition occuring in the
1104 * internal subset are not overriden by definitions in the
1105 * external subset.
1106 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001107 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001108 /*
1109 * the element should be instantiated in the tree if:
1110 * - this is a namespace prefix
1111 * - the user required for completion in the tree
1112 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001113 * - there isn't already an attribute definition
1114 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001115 */
1116 if (((attr->prefix != NULL) &&
1117 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1118 ((attr->prefix == NULL) &&
1119 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1120 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001121 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001122
Daniel Veillard8aff2472002-02-19 21:50:43 +00001123 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1124 attr->elem, attr->name,
1125 attr->prefix);
1126 if ((tst == attr) || (tst == NULL)) {
1127 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001128
Daniel Veillard8aff2472002-02-19 21:50:43 +00001129 if (attr->prefix != NULL) {
1130 fulln = xmlStrdup(attr->prefix);
1131 fulln = xmlStrcat(fulln, BAD_CAST ":");
1132 fulln = xmlStrcat(fulln, attr->name);
1133 } else {
1134 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001135 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001136
1137 /*
1138 * Check that the attribute is not declared in the
1139 * serialization
1140 */
1141 att = NULL;
1142 if (atts != NULL) {
1143 i = 0;
1144 att = atts[i];
1145 while (att != NULL) {
1146 if (xmlStrEqual(att, fulln))
1147 break;
1148 i += 2;
1149 att = atts[i];
1150 }
1151 }
1152 if (att == NULL) {
1153 attribute(ctxt, fulln, attr->defaultValue);
1154 }
1155 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001156 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001157 }
1158 }
1159 attr = attr->nexth;
1160 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001161 if (internal == 1) {
1162 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1163 name, prefix);
1164 internal = 0;
1165 goto process_external_subset;
1166 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001167 }
1168}
1169
Owen Taylor3473f882001-02-23 17:55:21 +00001170/**
1171 * startElement:
1172 * @ctx: the user data (XML parser context)
1173 * @fullname: The element name, including namespace prefix
1174 * @atts: An array of name/value attributes pairs, NULL terminated
1175 *
1176 * called when an opening tag has been processed.
1177 */
1178void
1179startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1180{
1181 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1182 xmlNodePtr ret;
1183 xmlNodePtr parent = ctxt->node;
1184 xmlNsPtr ns;
1185 xmlChar *name;
1186 xmlChar *prefix;
1187 const xmlChar *att;
1188 const xmlChar *value;
1189 int i;
1190
1191#ifdef DEBUG_SAX
1192 xmlGenericError(xmlGenericErrorContext,
1193 "SAX.startElement(%s)\n", fullname);
1194#endif
1195
1196 /*
1197 * First check on validity:
1198 */
1199 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1200 ((ctxt->myDoc->intSubset == NULL) ||
1201 ((ctxt->myDoc->intSubset->notations == NULL) &&
1202 (ctxt->myDoc->intSubset->elements == NULL) &&
1203 (ctxt->myDoc->intSubset->attributes == NULL) &&
1204 (ctxt->myDoc->intSubset->entities == NULL)))) {
1205 if (ctxt->vctxt.error != NULL) {
1206 ctxt->vctxt.error(ctxt->vctxt.userData,
1207 "Validation failed: no DTD found !\n");
1208 }
1209 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001210 ctxt->valid = 0;
1211 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001212 }
1213
1214
1215 /*
1216 * Split the full name into a namespace prefix and the tag name
1217 */
1218 name = xmlSplitQName(ctxt, fullname, &prefix);
1219
1220
1221 /*
1222 * Note : the namespace resolution is deferred until the end of the
1223 * attributes parsing, since local namespace can be defined as
1224 * an attribute at this level.
1225 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001226 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001227 if (ret == NULL) return;
1228 if (ctxt->myDoc->children == NULL) {
1229#ifdef DEBUG_SAX_TREE
1230 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1231#endif
1232 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1233 } else if (parent == NULL) {
1234 parent = ctxt->myDoc->children;
1235 }
1236 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001237 if (ctxt->linenumbers) {
1238 if (ctxt->input != NULL)
1239 ret->content = (void *) (long) ctxt->input->line;
1240 }
Owen Taylor3473f882001-02-23 17:55:21 +00001241
1242 /*
1243 * We are parsing a new node.
1244 */
1245#ifdef DEBUG_SAX_TREE
1246 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1247#endif
1248 nodePush(ctxt, ret);
1249
1250 /*
1251 * Link the child element
1252 */
1253 if (parent != NULL) {
1254 if (parent->type == XML_ELEMENT_NODE) {
1255#ifdef DEBUG_SAX_TREE
1256 xmlGenericError(xmlGenericErrorContext,
1257 "adding child %s to %s\n", name, parent->name);
1258#endif
1259 xmlAddChild(parent, ret);
1260 } else {
1261#ifdef DEBUG_SAX_TREE
1262 xmlGenericError(xmlGenericErrorContext,
1263 "adding sibling %s to ", name);
1264 xmlDebugDumpOneNode(stderr, parent, 0);
1265#endif
1266 xmlAddSibling(parent, ret);
1267 }
1268 }
1269
1270 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001271 * Insert all the defaulted attributes from the DTD especially namespaces
1272 */
1273 if ((!ctxt->html) &&
1274 ((ctxt->myDoc->intSubset != NULL) ||
1275 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001276 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001277 }
1278
1279 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001280 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001281 */
1282 if (atts != NULL) {
1283 i = 0;
1284 att = atts[i++];
1285 value = atts[i++];
1286 if (!ctxt->html) {
1287 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001288 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1289 (att[3] == 'n') && (att[4] == 's'))
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001290 my_attribute(ctxt, att, value, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001291
1292 att = atts[i++];
1293 value = atts[i++];
1294 }
1295 }
1296 }
1297
1298 /*
1299 * Search the namespace, note that since the attributes have been
1300 * processed, the local namespaces are available.
1301 */
1302 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1303 if ((ns == NULL) && (parent != NULL))
1304 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1305 if ((prefix != NULL) && (ns == NULL)) {
1306 ns = xmlNewNs(ret, NULL, prefix);
1307 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1308 ctxt->sax->warning(ctxt->userData,
1309 "Namespace prefix %s is not defined\n", prefix);
1310 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001311
1312 /*
1313 * set the namespace node, making sure that if the default namspace
1314 * is unbound on a parent we simply kee it NULL
1315 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001316 if ((ns != NULL) && (ns->href != NULL) &&
1317 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001318 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001319
1320 /*
1321 * process all the other attributes
1322 */
1323 if (atts != NULL) {
1324 i = 0;
1325 att = atts[i++];
1326 value = atts[i++];
1327 if (ctxt->html) {
1328 while (att != NULL) {
1329 attribute(ctxt, att, value);
1330 att = atts[i++];
1331 value = atts[i++];
1332 }
1333 } else {
1334 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001335 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1336 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001337 attribute(ctxt, att, value);
1338
1339 /*
1340 * Next ones
1341 */
1342 att = atts[i++];
1343 value = atts[i++];
1344 }
1345 }
1346 }
1347
1348 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001349 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001350 * check the document root element for validity
1351 */
1352 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001353 int chk;
1354
1355 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1356 if (chk <= 0)
1357 ctxt->valid = 0;
1358 if (chk < 0)
1359 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001360 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1361 ctxt->vctxt.finishDtd = 1;
1362 }
1363
1364 if (prefix != NULL)
1365 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001366
1367}
1368
1369/**
1370 * endElement:
1371 * @ctx: the user data (XML parser context)
1372 * @name: The element name
1373 *
1374 * called when the end of an element has been detected.
1375 */
1376void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001377endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001378{
1379 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1380 xmlParserNodeInfo node_info;
1381 xmlNodePtr cur = ctxt->node;
1382
1383#ifdef DEBUG_SAX
1384 if (name == NULL)
1385 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1386 else
1387 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1388#endif
1389
1390 /* Capture end position and add node */
1391 if (cur != NULL && ctxt->record_info) {
1392 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1393 node_info.end_line = ctxt->input->line;
1394 node_info.node = cur;
1395 xmlParserAddNodeInfo(ctxt, &node_info);
1396 }
1397 ctxt->nodemem = -1;
1398
1399 if (ctxt->validate && ctxt->wellFormed &&
1400 ctxt->myDoc && ctxt->myDoc->intSubset)
1401 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1402 cur);
1403
1404
1405 /*
1406 * end of parsing of this node.
1407 */
1408#ifdef DEBUG_SAX_TREE
1409 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1410#endif
1411 nodePop(ctxt);
1412}
1413
1414/**
1415 * reference:
1416 * @ctx: the user data (XML parser context)
1417 * @name: The entity name
1418 *
1419 * called when an entity reference is detected.
1420 */
1421void
1422reference(void *ctx, const xmlChar *name)
1423{
1424 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1425 xmlNodePtr ret;
1426
1427#ifdef DEBUG_SAX
1428 xmlGenericError(xmlGenericErrorContext,
1429 "SAX.reference(%s)\n", name);
1430#endif
1431 if (name[0] == '#')
1432 ret = xmlNewCharRef(ctxt->myDoc, name);
1433 else
1434 ret = xmlNewReference(ctxt->myDoc, name);
1435#ifdef DEBUG_SAX_TREE
1436 xmlGenericError(xmlGenericErrorContext,
1437 "add reference %s to %s \n", name, ctxt->node->name);
1438#endif
1439 xmlAddChild(ctxt->node, ret);
1440}
1441
1442/**
1443 * characters:
1444 * @ctx: the user data (XML parser context)
1445 * @ch: a xmlChar string
1446 * @len: the number of xmlChar
1447 *
1448 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001449 */
1450void
1451characters(void *ctx, const xmlChar *ch, int len)
1452{
1453 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1454 xmlNodePtr lastChild;
1455
1456#ifdef DEBUG_SAX
1457 xmlGenericError(xmlGenericErrorContext,
1458 "SAX.characters(%.30s, %d)\n", ch, len);
1459#endif
1460 /*
1461 * Handle the data if any. If there is no child
1462 * add it as content, otherwise if the last child is text,
1463 * concatenate it, else create a new node of type text.
1464 */
1465
1466 if (ctxt->node == NULL) {
1467#ifdef DEBUG_SAX_TREE
1468 xmlGenericError(xmlGenericErrorContext,
1469 "add chars: ctxt->node == NULL !\n");
1470#endif
1471 return;
1472 }
1473 lastChild = xmlGetLastChild(ctxt->node);
1474#ifdef DEBUG_SAX_TREE
1475 xmlGenericError(xmlGenericErrorContext,
1476 "add chars to %s \n", ctxt->node->name);
1477#endif
1478
1479 /*
1480 * Here we needed an accelerator mechanism in case of very large
1481 * elements. Use an attribute in the structure !!!
1482 */
1483 if (lastChild == NULL) {
1484 /* first node, first time */
1485 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001486 if (ctxt->node->children != NULL) {
1487 ctxt->nodelen = len;
1488 ctxt->nodemem = len + 1;
1489 }
Owen Taylor3473f882001-02-23 17:55:21 +00001490 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001491 int coalesceText = (lastChild != NULL) &&
1492 (lastChild->type == XML_TEXT_NODE) &&
1493 (lastChild->name == xmlStringText);
1494 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001495 /*
1496 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001497 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001498 * reallocate a new buffer, move data, append ch. Here
1499 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001500 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001501 */
1502 if (ctxt->nodelen + len >= ctxt->nodemem) {
1503 xmlChar *newbuf;
1504 int size;
1505
1506 size = ctxt->nodemem + len;
1507 size *= 2;
1508 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1509 if (newbuf == NULL) {
1510 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1511 ctxt->sax->error(ctxt->userData,
1512 "SAX.characters(): out of memory\n");
1513 return;
1514 }
1515 ctxt->nodemem = size;
1516 lastChild->content = newbuf;
1517 }
1518 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1519 ctxt->nodelen += len;
1520 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001521 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001522 xmlTextConcat(lastChild, ch, len);
1523 if (ctxt->node->children != NULL) {
1524 ctxt->nodelen = xmlStrlen(lastChild->content);
1525 ctxt->nodemem = ctxt->nodelen + 1;
1526 }
Owen Taylor3473f882001-02-23 17:55:21 +00001527 } else {
1528 /* Mixed content, first time */
1529 lastChild = xmlNewTextLen(ch, len);
1530 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001531 if (ctxt->node->children != NULL) {
1532 ctxt->nodelen = len;
1533 ctxt->nodemem = len + 1;
1534 }
Owen Taylor3473f882001-02-23 17:55:21 +00001535 }
1536 }
1537}
1538
1539/**
1540 * ignorableWhitespace:
1541 * @ctx: the user data (XML parser context)
1542 * @ch: a xmlChar string
1543 * @len: the number of xmlChar
1544 *
1545 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001546 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001547 */
1548void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001549ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001550{
1551 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1552#ifdef DEBUG_SAX
1553 xmlGenericError(xmlGenericErrorContext,
1554 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1555#endif
1556}
1557
1558/**
1559 * processingInstruction:
1560 * @ctx: the user data (XML parser context)
1561 * @target: the target name
1562 * @data: the PI data's
1563 *
1564 * A processing instruction has been parsed.
1565 */
1566void
1567processingInstruction(void *ctx, const xmlChar *target,
1568 const xmlChar *data)
1569{
1570 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1571 xmlNodePtr ret;
1572 xmlNodePtr parent = ctxt->node;
1573
1574#ifdef DEBUG_SAX
1575 xmlGenericError(xmlGenericErrorContext,
1576 "SAX.processingInstruction(%s, %s)\n", target, data);
1577#endif
1578
1579 ret = xmlNewPI(target, data);
1580 if (ret == NULL) return;
1581 parent = ctxt->node;
1582
1583 if (ctxt->inSubset == 1) {
1584 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1585 return;
1586 } else if (ctxt->inSubset == 2) {
1587 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1588 return;
1589 }
1590 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1591#ifdef DEBUG_SAX_TREE
1592 xmlGenericError(xmlGenericErrorContext,
1593 "Setting PI %s as root\n", target);
1594#endif
1595 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1596 return;
1597 }
1598 if (parent->type == XML_ELEMENT_NODE) {
1599#ifdef DEBUG_SAX_TREE
1600 xmlGenericError(xmlGenericErrorContext,
1601 "adding PI %s child to %s\n", target, parent->name);
1602#endif
1603 xmlAddChild(parent, ret);
1604 } else {
1605#ifdef DEBUG_SAX_TREE
1606 xmlGenericError(xmlGenericErrorContext,
1607 "adding PI %s sibling to ", target);
1608 xmlDebugDumpOneNode(stderr, parent, 0);
1609#endif
1610 xmlAddSibling(parent, ret);
1611 }
1612}
1613
1614/**
1615 * globalNamespace:
1616 * @ctx: the user data (XML parser context)
1617 * @href: the namespace associated URN
1618 * @prefix: the namespace prefix
1619 *
1620 * An old global namespace has been parsed.
1621 */
1622void
1623globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1624{
1625 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1626#ifdef DEBUG_SAX
1627 xmlGenericError(xmlGenericErrorContext,
1628 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1629#endif
1630 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1631}
1632
1633/**
1634 * setNamespace:
1635 * @ctx: the user data (XML parser context)
1636 * @name: the namespace prefix
1637 *
1638 * Set the current element namespace.
1639 */
1640
1641void
1642setNamespace(void *ctx, const xmlChar *name)
1643{
1644 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1645 xmlNsPtr ns;
1646 xmlNodePtr parent;
1647
1648#ifdef DEBUG_SAX
1649 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1650#endif
1651 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1652 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1653 if (ctxt->nodeNr >= 2) {
1654 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1655 if (parent != NULL)
1656 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1657 }
1658 }
1659 xmlSetNs(ctxt->node, ns);
1660}
1661
1662/**
1663 * getNamespace:
1664 * @ctx: the user data (XML parser context)
1665 *
1666 * Get the current element namespace.
1667 *
1668 * Returns the xmlNsPtr or NULL if none
1669 */
1670
1671xmlNsPtr
1672getNamespace(void *ctx)
1673{
1674 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1675 xmlNsPtr ret;
1676
1677#ifdef DEBUG_SAX
1678 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1679#endif
1680 ret = ctxt->node->ns;
1681 return(ret);
1682}
1683
1684/**
1685 * checkNamespace:
1686 * @ctx: the user data (XML parser context)
1687 * @namespace: the namespace to check against
1688 *
1689 * Check that the current element namespace is the same as the
1690 * one read upon parsing.
1691 *
1692 * Returns 1 if true 0 otherwise
1693 */
1694
1695int
1696checkNamespace(void *ctx, xmlChar *namespace)
1697{
1698 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1699 xmlNodePtr cur = ctxt->node;
1700
1701#ifdef DEBUG_SAX
1702 xmlGenericError(xmlGenericErrorContext,
1703 "SAX.checkNamespace(%s)\n", namespace);
1704#endif
1705
1706 /*
1707 * Check that the Name in the ETag is the same as in the STag.
1708 */
1709 if (namespace == NULL) {
1710 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1711 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1712 ctxt->sax->error(ctxt,
1713 "End tags for %s don't hold the namespace %s\n",
1714 cur->name, cur->ns->prefix);
1715 ctxt->wellFormed = 0;
1716 }
1717 } else {
1718 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1719 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1720 ctxt->sax->error(ctxt,
1721 "End tags %s holds a prefix %s not used by the open tag\n",
1722 cur->name, namespace);
1723 ctxt->wellFormed = 0;
1724 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1725 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1726 ctxt->sax->error(ctxt,
1727 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1728 cur->name, cur->ns->prefix, namespace);
1729 ctxt->wellFormed = 0;
1730 } else
1731 return(1);
1732 }
1733 return(0);
1734}
1735
1736/**
1737 * namespaceDecl:
1738 * @ctx: the user data (XML parser context)
1739 * @href: the namespace associated URN
1740 * @prefix: the namespace prefix
1741 *
1742 * A namespace has been parsed.
1743 */
1744void
1745namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1746{
1747 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1748#ifdef DEBUG_SAX
1749 if (prefix == NULL)
1750 xmlGenericError(xmlGenericErrorContext,
1751 "SAX.namespaceDecl(%s, NULL)\n", href);
1752 else
1753 xmlGenericError(xmlGenericErrorContext,
1754 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1755#endif
1756 xmlNewNs(ctxt->node, href, prefix);
1757}
1758
1759/**
1760 * comment:
1761 * @ctx: the user data (XML parser context)
1762 * @value: the comment content
1763 *
1764 * A comment has been parsed.
1765 */
1766void
1767comment(void *ctx, const xmlChar *value)
1768{
1769 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1770 xmlNodePtr ret;
1771 xmlNodePtr parent = ctxt->node;
1772
1773#ifdef DEBUG_SAX
1774 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1775#endif
1776 ret = xmlNewDocComment(ctxt->myDoc, value);
1777 if (ret == NULL) return;
1778
1779 if (ctxt->inSubset == 1) {
1780 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1781 return;
1782 } else if (ctxt->inSubset == 2) {
1783 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1784 return;
1785 }
1786 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1787#ifdef DEBUG_SAX_TREE
1788 xmlGenericError(xmlGenericErrorContext,
1789 "Setting comment as root\n");
1790#endif
1791 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1792 return;
1793 }
1794 if (parent->type == XML_ELEMENT_NODE) {
1795#ifdef DEBUG_SAX_TREE
1796 xmlGenericError(xmlGenericErrorContext,
1797 "adding comment child to %s\n", parent->name);
1798#endif
1799 xmlAddChild(parent, ret);
1800 } else {
1801#ifdef DEBUG_SAX_TREE
1802 xmlGenericError(xmlGenericErrorContext,
1803 "adding comment sibling to ");
1804 xmlDebugDumpOneNode(stderr, parent, 0);
1805#endif
1806 xmlAddSibling(parent, ret);
1807 }
1808}
1809
1810/**
1811 * cdataBlock:
1812 * @ctx: the user data (XML parser context)
1813 * @value: The pcdata content
1814 * @len: the block length
1815 *
1816 * called when a pcdata block has been parsed
1817 */
1818void
1819cdataBlock(void *ctx, const xmlChar *value, int len)
1820{
1821 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1822 xmlNodePtr ret, lastChild;
1823
1824#ifdef DEBUG_SAX
1825 xmlGenericError(xmlGenericErrorContext,
1826 "SAX.pcdata(%.10s, %d)\n", value, len);
1827#endif
1828 lastChild = xmlGetLastChild(ctxt->node);
1829#ifdef DEBUG_SAX_TREE
1830 xmlGenericError(xmlGenericErrorContext,
1831 "add chars to %s \n", ctxt->node->name);
1832#endif
1833 if ((lastChild != NULL) &&
1834 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1835 xmlTextConcat(lastChild, value, len);
1836 } else {
1837 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1838 xmlAddChild(ctxt->node, ret);
1839 }
1840}
1841
Daniel Veillardd0463562001-10-13 09:15:48 +00001842/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001843 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001844 * @hdlr: the SAX handler
1845 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001846 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001847 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001848 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001849void
1850initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1851{
1852 if(hdlr->initialized == 1)
1853 return;
1854
1855 hdlr->internalSubset = internalSubset;
1856 hdlr->externalSubset = externalSubset;
1857 hdlr->isStandalone = isStandalone;
1858 hdlr->hasInternalSubset = hasInternalSubset;
1859 hdlr->hasExternalSubset = hasExternalSubset;
1860 hdlr->resolveEntity = resolveEntity;
1861 hdlr->getEntity = getEntity;
1862 hdlr->getParameterEntity = getParameterEntity;
1863 hdlr->entityDecl = entityDecl;
1864 hdlr->attributeDecl = attributeDecl;
1865 hdlr->elementDecl = elementDecl;
1866 hdlr->notationDecl = notationDecl;
1867 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1868 hdlr->setDocumentLocator = setDocumentLocator;
1869 hdlr->startDocument = startDocument;
1870 hdlr->endDocument = endDocument;
1871 hdlr->startElement = startElement;
1872 hdlr->endElement = endElement;
1873 hdlr->reference = reference;
1874 hdlr->characters = characters;
1875 hdlr->cdataBlock = cdataBlock;
1876 hdlr->ignorableWhitespace = characters;
1877 hdlr->processingInstruction = processingInstruction;
1878 hdlr->comment = comment;
1879 /* if (xmlGetWarningsDefaultValue == 0) */
1880 if (warning == 0)
1881 hdlr->warning = NULL;
1882 else
1883 hdlr->warning = xmlParserWarning;
1884 hdlr->error = xmlParserError;
1885 hdlr->fatalError = xmlParserError;
1886
1887 hdlr->initialized = 1;
1888}
Owen Taylor3473f882001-02-23 17:55:21 +00001889
1890/**
1891 * xmlDefaultSAXHandlerInit:
1892 *
1893 * Initialize the default SAX handler
1894 */
1895void
1896xmlDefaultSAXHandlerInit(void)
1897{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001898 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001899}
1900
Daniel Veillardeae522a2001-04-23 13:41:34 +00001901#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001902
1903/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001904 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001905 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001906 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001907 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001908 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001909void
1910inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1911{
1912 if(hdlr->initialized == 1)
1913 return;
1914
1915 hdlr->internalSubset = internalSubset;
1916 hdlr->externalSubset = NULL;
1917 hdlr->isStandalone = NULL;
1918 hdlr->hasInternalSubset = NULL;
1919 hdlr->hasExternalSubset = NULL;
1920 hdlr->resolveEntity = NULL;
1921 hdlr->getEntity = getEntity;
1922 hdlr->getParameterEntity = NULL;
1923 hdlr->entityDecl = NULL;
1924 hdlr->attributeDecl = NULL;
1925 hdlr->elementDecl = NULL;
1926 hdlr->notationDecl = NULL;
1927 hdlr->unparsedEntityDecl = NULL;
1928 hdlr->setDocumentLocator = setDocumentLocator;
1929 hdlr->startDocument = startDocument;
1930 hdlr->endDocument = endDocument;
1931 hdlr->startElement = startElement;
1932 hdlr->endElement = endElement;
1933 hdlr->reference = NULL;
1934 hdlr->characters = characters;
1935 hdlr->cdataBlock = cdataBlock;
1936 hdlr->ignorableWhitespace = ignorableWhitespace;
1937 hdlr->processingInstruction = NULL;
1938 hdlr->comment = comment;
1939 hdlr->warning = xmlParserWarning;
1940 hdlr->error = xmlParserError;
1941 hdlr->fatalError = xmlParserError;
1942
1943 hdlr->initialized = 1;
1944}
Owen Taylor3473f882001-02-23 17:55:21 +00001945
1946/**
1947 * htmlDefaultSAXHandlerInit:
1948 *
1949 * Initialize the default SAX handler
1950 */
1951void
1952htmlDefaultSAXHandlerInit(void)
1953{
Daniel Veillardd0463562001-10-13 09:15:48 +00001954 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00001955}
Daniel Veillardd0463562001-10-13 09:15:48 +00001956
Daniel Veillardeae522a2001-04-23 13:41:34 +00001957#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001958
Daniel Veillardeae522a2001-04-23 13:41:34 +00001959#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001960
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001961/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001962 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001963 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001964 *
1965 * Initialize the default DocBook SAX handler
1966 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001967void
1968initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
1969{
1970 if(hdlr->initialized == 1)
1971 return;
1972
1973 hdlr->internalSubset = internalSubset;
1974 hdlr->externalSubset = NULL;
1975 hdlr->isStandalone = isStandalone;
1976 hdlr->hasInternalSubset = hasInternalSubset;
1977 hdlr->hasExternalSubset = hasExternalSubset;
1978 hdlr->resolveEntity = resolveEntity;
1979 hdlr->getEntity = getEntity;
1980 hdlr->getParameterEntity = NULL;
1981 hdlr->entityDecl = entityDecl;
1982 hdlr->attributeDecl = NULL;
1983 hdlr->elementDecl = NULL;
1984 hdlr->notationDecl = NULL;
1985 hdlr->unparsedEntityDecl = NULL;
1986 hdlr->setDocumentLocator = setDocumentLocator;
1987 hdlr->startDocument = startDocument;
1988 hdlr->endDocument = endDocument;
1989 hdlr->startElement = startElement;
1990 hdlr->endElement = endElement;
1991 hdlr->reference = reference;
1992 hdlr->characters = characters;
1993 hdlr->cdataBlock = NULL;
1994 hdlr->ignorableWhitespace = ignorableWhitespace;
1995 hdlr->processingInstruction = NULL;
1996 hdlr->comment = comment;
1997 hdlr->warning = xmlParserWarning;
1998 hdlr->error = xmlParserError;
1999 hdlr->fatalError = xmlParserError;
2000
2001 hdlr->initialized = 1;
2002}
Owen Taylor3473f882001-02-23 17:55:21 +00002003
2004/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00002005 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00002006 *
2007 * Initialize the default SAX handler
2008 */
2009void
Daniel Veillardeae522a2001-04-23 13:41:34 +00002010docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00002011{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002012 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002013}
Daniel Veillardeae522a2001-04-23 13:41:34 +00002014
2015#endif /* LIBXML_DOCB_ENABLED */