blob: 6192faab2d27eedfe594e65a9a1d7f58acd01e13 [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 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +000036 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
Owen Taylor3473f882001-02-23 17:55:21 +000037 *
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 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +000051 * Provides the system ID, basically URL or filename e.g.
Owen Taylor3473f882001-02-23 17:55:21 +000052 * 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 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +000067 * Provide the line number of the current parsing point.
Owen Taylor3473f882001-02-23 17:55:21 +000068 *
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 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +000082 * Provide the column number of the current parsing point.
Owen Taylor3473f882001-02-23 17:55:21 +000083 *
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;
Daniel Veillard8dbd4952002-12-27 11:34:48 +0000341 xmlEntityPtr ret = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +0000342
343#ifdef DEBUG_SAX
344 xmlGenericError(xmlGenericErrorContext,
345 "SAX.getEntity(%s)\n", name);
346#endif
347
Daniel Veillard8dbd4952002-12-27 11:34:48 +0000348 if (ctxt->inSubset == 0) {
349 ret = xmlGetPredefinedEntity(name);
350 if (ret != NULL)
351 return(ret);
352 }
Daniel Veillard28757702002-02-18 11:19:30 +0000353 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
354 if (ctxt->inSubset == 2) {
355 ctxt->myDoc->standalone = 0;
356 ret = xmlGetDocEntity(ctxt->myDoc, name);
357 ctxt->myDoc->standalone = 1;
358 } else {
359 ret = xmlGetDocEntity(ctxt->myDoc, name);
360 if (ret == NULL) {
361 ctxt->myDoc->standalone = 0;
362 ret = xmlGetDocEntity(ctxt->myDoc, name);
363 if (ret != NULL) {
364 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
365 ctxt->sax->error(ctxt,
366 "Entity(%s) document marked standalone but require external subset\n",
367 name);
368 ctxt->valid = 0;
369 ctxt->wellFormed = 0;
370 }
371 ctxt->myDoc->standalone = 1;
372 }
373 }
374 } else {
375 ret = xmlGetDocEntity(ctxt->myDoc, name);
376 }
Owen Taylor3473f882001-02-23 17:55:21 +0000377 if ((ret != NULL) && (ctxt->validate) && (ret->children == NULL) &&
378 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
379 /*
380 * for validation purposes we really need to fetch and
381 * parse the external entity
382 */
Owen Taylor3473f882001-02-23 17:55:21 +0000383 xmlNodePtr children;
384
Daniel Veillard5015b712001-08-17 09:37:52 +0000385 xmlParseCtxtExternalEntity(ctxt, ret->URI, ret->ExternalID, &children);
Owen Taylor3473f882001-02-23 17:55:21 +0000386 xmlAddChildList((xmlNodePtr) ret, children);
387 }
388 return(ret);
389}
390
391/**
392 * getParameterEntity:
393 * @ctx: the user data (XML parser context)
394 * @name: The entity name
395 *
396 * Get a parameter entity by name
397 *
398 * Returns the xmlEntityPtr if found.
399 */
400xmlEntityPtr
401getParameterEntity(void *ctx, const xmlChar *name)
402{
403 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
404 xmlEntityPtr ret;
405
406#ifdef DEBUG_SAX
407 xmlGenericError(xmlGenericErrorContext,
408 "SAX.getParameterEntity(%s)\n", name);
409#endif
410
411 ret = xmlGetParameterEntity(ctxt->myDoc, name);
412 return(ret);
413}
414
415
416/**
417 * entityDecl:
418 * @ctx: the user data (XML parser context)
419 * @name: the entity name
420 * @type: the entity type
421 * @publicId: The public ID of the entity
422 * @systemId: The system ID of the entity
423 * @content: the entity value (without processing).
424 *
425 * An entity definition has been parsed
426 */
427void
428entityDecl(void *ctx, const xmlChar *name, int type,
429 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
430{
431 xmlEntityPtr ent;
432 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
433
434#ifdef DEBUG_SAX
435 xmlGenericError(xmlGenericErrorContext,
436 "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
437 name, type, publicId, systemId, content);
438#endif
439 if (ctxt->inSubset == 1) {
440 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
441 systemId, content);
442 if ((ent == NULL) && (ctxt->pedantic) &&
443 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
444 ctxt->sax->warning(ctxt,
445 "Entity(%s) already defined in the internal subset\n", name);
446 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
447 xmlChar *URI;
448 const char *base = NULL;
449
450 if (ctxt->input != NULL)
451 base = ctxt->input->filename;
452 if (base == NULL)
453 base = ctxt->directory;
454
455 URI = xmlBuildURI(systemId, (const xmlChar *) base);
456 ent->URI = URI;
457 }
458 } else if (ctxt->inSubset == 2) {
459 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
460 systemId, content);
461 if ((ent == NULL) && (ctxt->pedantic) &&
462 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
463 ctxt->sax->warning(ctxt,
464 "Entity(%s) already defined in the external subset\n", name);
465 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
466 xmlChar *URI;
467 const char *base = NULL;
468
469 if (ctxt->input != NULL)
470 base = ctxt->input->filename;
471 if (base == NULL)
472 base = ctxt->directory;
473
474 URI = xmlBuildURI(systemId, (const xmlChar *) base);
475 ent->URI = URI;
476 }
477 } else {
478 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
479 ctxt->sax->error(ctxt,
480 "SAX.entityDecl(%s) called while not in subset\n", name);
481 }
482}
483
484/**
485 * attributeDecl:
486 * @ctx: the user data (XML parser context)
487 * @elem: the name of the element
488 * @fullname: the attribute name
489 * @type: the attribute type
490 * @def: the type of default value
491 * @defaultValue: the attribute default value
492 * @tree: the tree of enumerated value set
493 *
494 * An attribute definition has been parsed
495 */
496void
497attributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
498 int type, int def, const xmlChar *defaultValue,
499 xmlEnumerationPtr tree)
500{
501 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
502 xmlAttributePtr attr;
503 xmlChar *name = NULL, *prefix = NULL;
504
505#ifdef DEBUG_SAX
506 xmlGenericError(xmlGenericErrorContext,
507 "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
508 elem, fullname, type, def, defaultValue);
509#endif
510 name = xmlSplitQName(ctxt, fullname, &prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +0000511 ctxt->vctxt.valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000512 if (ctxt->inSubset == 1)
513 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
514 name, prefix, (xmlAttributeType) type,
515 (xmlAttributeDefault) def, defaultValue, tree);
516 else if (ctxt->inSubset == 2)
517 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
518 name, prefix, (xmlAttributeType) type,
519 (xmlAttributeDefault) def, defaultValue, tree);
520 else {
521 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
522 ctxt->sax->error(ctxt,
523 "SAX.attributeDecl(%s) called while not in subset\n", name);
524 return;
525 }
Daniel Veillardc7612992002-02-17 22:47:37 +0000526 if (ctxt->vctxt.valid == 0)
527 ctxt->valid = 0;
Daniel Veillardd85f4f42002-03-25 10:48:46 +0000528 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
529 (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000530 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
531 attr);
532 if (prefix != NULL)
533 xmlFree(prefix);
534 if (name != NULL)
535 xmlFree(name);
536}
537
538/**
539 * elementDecl:
540 * @ctx: the user data (XML parser context)
541 * @name: the element name
542 * @type: the element type
543 * @content: the element value tree
544 *
545 * An element definition has been parsed
546 */
547void
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000548elementDecl(void *ctx, const xmlChar * name, int type,
549 xmlElementContentPtr content)
Owen Taylor3473f882001-02-23 17:55:21 +0000550{
551 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
552 xmlElementPtr elem = NULL;
553
554#ifdef DEBUG_SAX
555 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000556 "SAX.elementDecl(%s, %d, ...)\n", name, type);
Owen Taylor3473f882001-02-23 17:55:21 +0000557#endif
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000558
Owen Taylor3473f882001-02-23 17:55:21 +0000559 if (ctxt->inSubset == 1)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000560 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
561 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000562 else if (ctxt->inSubset == 2)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000563 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
564 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000565 else {
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000566 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
567 ctxt->sax->error(ctxt,
568 "SAX.elementDecl(%s) called while not in subset\n",
569 name);
570 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000571 }
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000572 if (elem == NULL)
573 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000574 if (ctxt->validate && ctxt->wellFormed &&
575 ctxt->myDoc && ctxt->myDoc->intSubset)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000576 ctxt->valid &=
577 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Owen Taylor3473f882001-02-23 17:55:21 +0000578}
579
580/**
581 * notationDecl:
582 * @ctx: the user data (XML parser context)
583 * @name: The name of the notation
584 * @publicId: The public ID of the entity
585 * @systemId: The system ID of the entity
586 *
587 * What to do when a notation declaration has been parsed.
588 */
589void
590notationDecl(void *ctx, const xmlChar *name,
591 const xmlChar *publicId, const xmlChar *systemId)
592{
593 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
594 xmlNotationPtr nota = NULL;
595
596#ifdef DEBUG_SAX
597 xmlGenericError(xmlGenericErrorContext,
598 "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
599#endif
600
Daniel Veillard7aea52d2002-02-17 23:07:47 +0000601 if ((publicId == NULL) && (systemId == NULL)) {
602 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
603 ctxt->sax->error(ctxt,
604 "SAX.notationDecl(%s) externalID or PublicID missing\n", name);
605 ctxt->valid = 0;
606 ctxt->wellFormed = 0;
607 return;
608 } else if (ctxt->inSubset == 1)
Owen Taylor3473f882001-02-23 17:55:21 +0000609 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
610 publicId, systemId);
611 else if (ctxt->inSubset == 2)
Daniel Veillard25239c12001-03-14 13:56:48 +0000612 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
Owen Taylor3473f882001-02-23 17:55:21 +0000613 publicId, systemId);
614 else {
615 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
616 ctxt->sax->error(ctxt,
617 "SAX.notationDecl(%s) called while not in subset\n", name);
618 return;
619 }
620 if (nota == NULL) ctxt->valid = 0;
621 if (ctxt->validate && ctxt->wellFormed &&
622 ctxt->myDoc && ctxt->myDoc->intSubset)
623 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
624 nota);
625}
626
627/**
628 * unparsedEntityDecl:
629 * @ctx: the user data (XML parser context)
630 * @name: The name of the entity
631 * @publicId: The public ID of the entity
632 * @systemId: The system ID of the entity
633 * @notationName: the name of the notation
634 *
635 * What to do when an unparsed entity declaration is parsed
636 */
637void
638unparsedEntityDecl(void *ctx, const xmlChar *name,
639 const xmlChar *publicId, const xmlChar *systemId,
640 const xmlChar *notationName)
641{
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000642 xmlEntityPtr ent;
Owen Taylor3473f882001-02-23 17:55:21 +0000643 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
644#ifdef DEBUG_SAX
645 xmlGenericError(xmlGenericErrorContext,
646 "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
647 name, publicId, systemId, notationName);
648#endif
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000649#if 0
650 Done in xmlValidateDtdFinal now.
Daniel Veillard28757702002-02-18 11:19:30 +0000651 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc) {
652 int ret;
653 ret = xmlValidateNotationUse(&ctxt->vctxt, ctxt->myDoc,
Owen Taylor3473f882001-02-23 17:55:21 +0000654 notationName);
Daniel Veillard28757702002-02-18 11:19:30 +0000655 if (ret == 0) {
656 ctxt->wellFormed = 0;
657 ctxt->valid = 0;
658 }
659 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000660#endif
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000661 if (ctxt->inSubset == 1) {
662 ent = xmlAddDocEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000663 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
664 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000665 if ((ent == NULL) && (ctxt->pedantic) &&
666 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
667 ctxt->sax->warning(ctxt,
668 "Entity(%s) already defined in the internal subset\n", name);
669 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
670 xmlChar *URI;
671 const char *base = NULL;
672
673 if (ctxt->input != NULL)
674 base = ctxt->input->filename;
675 if (base == NULL)
676 base = ctxt->directory;
677
678 URI = xmlBuildURI(systemId, (const xmlChar *) base);
679 ent->URI = URI;
680 }
681 } else if (ctxt->inSubset == 2) {
682 ent = xmlAddDtdEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000683 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
684 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000685 if ((ent == NULL) && (ctxt->pedantic) &&
686 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
687 ctxt->sax->warning(ctxt,
688 "Entity(%s) already defined in the external subset\n", name);
689 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
690 xmlChar *URI;
691 const char *base = NULL;
692
693 if (ctxt->input != NULL)
694 base = ctxt->input->filename;
695 if (base == NULL)
696 base = ctxt->directory;
697
698 URI = xmlBuildURI(systemId, (const xmlChar *) base);
699 ent->URI = URI;
700 }
701 } else {
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000702 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
703 ctxt->sax->error(ctxt,
704 "SAX.unparsedEntityDecl(%s) called while not in subset\n", name);
705 }
Owen Taylor3473f882001-02-23 17:55:21 +0000706}
707
708/**
709 * setDocumentLocator:
710 * @ctx: the user data (XML parser context)
711 * @loc: A SAX Locator
712 *
713 * Receive the document locator at startup, actually xmlDefaultSAXLocator
714 * Everything is available on the context, so this is useless in our case.
715 */
716void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000717setDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +0000718{
719 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
720#ifdef DEBUG_SAX
721 xmlGenericError(xmlGenericErrorContext,
722 "SAX.setDocumentLocator()\n");
723#endif
724}
725
726/**
727 * startDocument:
728 * @ctx: the user data (XML parser context)
729 *
730 * called when the document start being processed.
731 */
732void
733startDocument(void *ctx)
734{
735 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
736 xmlDocPtr doc;
737
738#ifdef DEBUG_SAX
739 xmlGenericError(xmlGenericErrorContext,
740 "SAX.startDocument()\n");
741#endif
742 if (ctxt->html) {
743 if (ctxt->myDoc == NULL)
744#ifdef LIBXML_HTML_ENABLED
745 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
746#else
747 xmlGenericError(xmlGenericErrorContext,
748 "libxml2 built without HTML support\n");
749#endif
750 } else {
751 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
752 if (doc != NULL) {
753 if (ctxt->encoding != NULL)
754 doc->encoding = xmlStrdup(ctxt->encoding);
755 else
756 doc->encoding = NULL;
757 doc->standalone = ctxt->standalone;
758 }
759 }
760 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
761 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000762 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
Owen Taylor3473f882001-02-23 17:55:21 +0000763 }
764}
765
766/**
767 * endDocument:
768 * @ctx: the user data (XML parser context)
769 *
770 * called when the document end has been detected.
771 */
772void
773endDocument(void *ctx)
774{
775 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
776#ifdef DEBUG_SAX
777 xmlGenericError(xmlGenericErrorContext,
778 "SAX.endDocument()\n");
779#endif
780 if (ctxt->validate && ctxt->wellFormed &&
781 ctxt->myDoc && ctxt->myDoc->intSubset)
782 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
783
784 /*
785 * Grab the encoding if it was added on-the-fly
786 */
787 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
788 (ctxt->myDoc->encoding == NULL)) {
789 ctxt->myDoc->encoding = ctxt->encoding;
790 ctxt->encoding = NULL;
791 }
792 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
793 (ctxt->myDoc->encoding == NULL)) {
794 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
795 }
796 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
797 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
798 ctxt->myDoc->charset = ctxt->charset;
799 }
800}
801
802/**
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000803 * my_attribute:
Owen Taylor3473f882001-02-23 17:55:21 +0000804 * @ctx: the user data (XML parser context)
805 * @fullname: The attribute name, including namespace prefix
806 * @value: The attribute value
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000807 * @prefix: the prefix on the element node
Owen Taylor3473f882001-02-23 17:55:21 +0000808 *
809 * Handle an attribute that has been read by the parser.
810 * The default handling is to convert the attribute into an
811 * DOM subtree and past it in a new xmlAttr element added to
812 * the element.
813 */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000814static void
815my_attribute(void *ctx, const xmlChar *fullname, const xmlChar *value,
816 const xmlChar *prefix)
Owen Taylor3473f882001-02-23 17:55:21 +0000817{
818 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
819 xmlAttrPtr ret;
820 xmlChar *name;
821 xmlChar *ns;
822 xmlChar *nval;
823 xmlNsPtr namespace;
824
825/****************
826#ifdef DEBUG_SAX
827 xmlGenericError(xmlGenericErrorContext,
828 "SAX.attribute(%s, %s)\n", fullname, value);
829#endif
830 ****************/
831 /*
832 * Split the full name into a namespace prefix and the tag name
833 */
834 name = xmlSplitQName(ctxt, fullname, &ns);
835
836 /*
837 * Do the last stage of the attribute normalization
838 * Needed for HTML too:
839 * http://www.w3.org/TR/html4/types.html#h-6.2
840 */
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000841 ctxt->vctxt.valid = 1;
842 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
843 ctxt->myDoc, ctxt->node,
Owen Taylor3473f882001-02-23 17:55:21 +0000844 fullname, value);
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000845 if (ctxt->vctxt.valid != 1) {
846 ctxt->valid = 0;
847 }
Owen Taylor3473f882001-02-23 17:55:21 +0000848 if (nval != NULL)
849 value = nval;
850
851 /*
852 * Check whether it's a namespace definition
853 */
854 if ((!ctxt->html) && (ns == NULL) &&
855 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
856 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000857 xmlNsPtr nsret;
858
Owen Taylor3473f882001-02-23 17:55:21 +0000859 if (value[0] != 0) {
860 xmlURIPtr uri;
861
862 uri = xmlParseURI((const char *)value);
863 if (uri == NULL) {
864 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
865 ctxt->sax->warning(ctxt->userData,
866 "nmlns: %s not a valid URI\n", value);
867 } else {
868 if (uri->scheme == NULL) {
869 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
870 ctxt->sax->warning(ctxt->userData,
Daniel Veillardecaba492002-12-30 10:55:29 +0000871 "xmlns: URI %s is not absolute\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +0000872 }
873 xmlFreeURI(uri);
874 }
875 }
876
877 /* a default namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000878 nsret = xmlNewNs(ctxt->node, value, NULL);
879
880 /*
881 * Validate also for namespace decls, they are attributes from
882 * an XML-1.0 perspective
883 */
884 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
885 ctxt->myDoc && ctxt->myDoc->intSubset)
886 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
887 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000888 if (name != NULL)
889 xmlFree(name);
890 if (nval != NULL)
891 xmlFree(nval);
892 return;
893 }
894 if ((!ctxt->html) &&
895 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
896 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000897 xmlNsPtr nsret;
898
Daniel Veillardc0fef772002-03-01 16:16:31 +0000899 if (value[0] == 0) {
900 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
901 ctxt->sax->error(ctxt->userData,
902 "Empty namespace name for prefix %s\n", name);
903 }
Daniel Veillard7b4b2f92003-01-06 13:11:20 +0000904 if ((ctxt->pedantic != 0) && (value[0] != 0)) {
Daniel Veillardecaba492002-12-30 10:55:29 +0000905 xmlURIPtr uri;
906
907 uri = xmlParseURI((const char *)value);
908 if (uri == NULL) {
909 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
910 ctxt->sax->warning(ctxt->userData,
911 "xmlns:%s: %s not a valid URI\n", name, value);
912 } else {
913 if (uri->scheme == NULL) {
914 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
915 ctxt->sax->warning(ctxt->userData,
916 "xmlns:%s: URI %s is not absolute\n", name, value);
917 }
918 xmlFreeURI(uri);
919 }
920 }
921
Owen Taylor3473f882001-02-23 17:55:21 +0000922 /* a standard namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000923 nsret = xmlNewNs(ctxt->node, value, name);
Owen Taylor3473f882001-02-23 17:55:21 +0000924 xmlFree(ns);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000925 /*
926 * Validate also for namespace decls, they are attributes from
927 * an XML-1.0 perspective
928 */
929 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
930 ctxt->myDoc && ctxt->myDoc->intSubset)
931 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
932 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000933 if (name != NULL)
934 xmlFree(name);
935 if (nval != NULL)
936 xmlFree(nval);
937 return;
938 }
939
940 if (ns != NULL)
941 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
942 else {
943 namespace = NULL;
944 }
945
946 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +0000947 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000948
949 if (ret != NULL) {
950 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
951 xmlNodePtr tmp;
952
953 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
954 tmp = ret->children;
955 while (tmp != NULL) {
956 tmp->parent = (xmlNodePtr) ret;
957 if (tmp->next == NULL)
958 ret->last = tmp;
959 tmp = tmp->next;
960 }
961 } else if (value != NULL) {
962 ret->children = xmlNewDocText(ctxt->myDoc, value);
963 ret->last = ret->children;
964 if (ret->children != NULL)
965 ret->children->parent = (xmlNodePtr) ret;
966 }
967 }
968
969 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
970 ctxt->myDoc && ctxt->myDoc->intSubset) {
971
972 /*
973 * If we don't substitute entities, the validation should be
974 * done on a value with replaced entities anyway.
975 */
976 if (!ctxt->replaceEntities) {
977 xmlChar *val;
978
979 ctxt->depth++;
980 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
981 0,0,0);
982 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +0000983
Owen Taylor3473f882001-02-23 17:55:21 +0000984 if (val == NULL)
985 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
986 ctxt->myDoc, ctxt->node, ret, value);
987 else {
Daniel Veillardc7612992002-02-17 22:47:37 +0000988 xmlChar *nvalnorm;
989
990 /*
991 * Do the last stage of the attribute normalization
992 * It need to be done twice ... it's an extra burden related
993 * to the ability to keep references in attributes
994 */
995 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
996 ctxt->node, fullname, val);
997 if (nvalnorm != NULL) {
998 xmlFree(val);
999 val = nvalnorm;
1000 }
1001
Owen Taylor3473f882001-02-23 17:55:21 +00001002 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1003 ctxt->myDoc, ctxt->node, ret, val);
1004 xmlFree(val);
1005 }
1006 } else {
1007 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1008 ctxt->node, ret, value);
1009 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00001010 } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1011 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001012 /*
1013 * when validating, the ID registration is done at the attribute
1014 * validation level. Otherwise we have to do specific handling here.
1015 */
1016 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1017 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1018 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1019 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1020 }
1021
1022 if (nval != NULL)
1023 xmlFree(nval);
Owen Taylor3473f882001-02-23 17:55:21 +00001024 if (ns != NULL)
1025 xmlFree(ns);
1026}
1027
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001028/**
1029 * attribute:
1030 * @ctx: the user data (XML parser context)
1031 * @fullname: The attribute name, including namespace prefix
1032 * @value: The attribute value
1033 *
1034 * Handle an attribute that has been read by the parser.
1035 * The default handling is to convert the attribute into an
1036 * DOM subtree and past it in a new xmlAttr element added to
1037 * the element.
1038 */
1039void
1040attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1041{
1042 my_attribute(ctx, fullname, value, NULL);
1043}
1044
Daniel Veillard878eab02002-02-19 13:46:09 +00001045/*
1046 * xmlCheckDefaultedAttributes:
1047 *
1048 * Check defaulted attributes from the DTD
1049 */
1050static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001051xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001052 const xmlChar *prefix, const xmlChar **atts) {
1053 xmlElementPtr elemDecl;
1054 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001055 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001056 int i;
1057
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001058 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1059 if (elemDecl == NULL) {
1060 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1061 internal = 0;
1062 }
1063
1064process_external_subset:
1065
Daniel Veillard878eab02002-02-19 13:46:09 +00001066 if (elemDecl != NULL) {
1067 xmlAttributePtr attr = elemDecl->attributes;
1068 /*
1069 * Check against defaulted attributes from the external subset
1070 * if the document is stamped as standalone
1071 */
1072 if ((ctxt->myDoc->standalone == 1) &&
1073 (ctxt->myDoc->extSubset != NULL) &&
1074 (ctxt->validate)) {
1075 while (attr != NULL) {
1076 if ((attr->defaultValue != NULL) &&
1077 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1078 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001079 attr->prefix) == attr) &&
1080 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1081 attr->elem, attr->name,
1082 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001083 xmlChar *fulln;
1084
1085 if (attr->prefix != NULL) {
1086 fulln = xmlStrdup(attr->prefix);
1087 fulln = xmlStrcat(fulln, BAD_CAST ":");
1088 fulln = xmlStrcat(fulln, attr->name);
1089 } else {
1090 fulln = xmlStrdup(attr->name);
1091 }
1092
1093 /*
1094 * Check that the attribute is not declared in the
1095 * serialization
1096 */
1097 att = NULL;
1098 if (atts != NULL) {
1099 i = 0;
1100 att = atts[i];
1101 while (att != NULL) {
1102 if (xmlStrEqual(att, fulln))
1103 break;
1104 i += 2;
1105 att = atts[i];
1106 }
1107 }
1108 if (att == NULL) {
1109 if (ctxt->vctxt.error != NULL)
1110 ctxt->vctxt.error(ctxt->vctxt.userData,
1111 "standalone: attribute %s on %s defaulted from external subset\n",
1112 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001113 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001114 }
1115 }
1116 attr = attr->nexth;
1117 }
1118 }
1119
1120 /*
1121 * Actually insert defaulted values when needed
1122 */
1123 attr = elemDecl->attributes;
1124 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001125 /*
1126 * Make sure that attributes redefinition occuring in the
1127 * internal subset are not overriden by definitions in the
1128 * external subset.
1129 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001130 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001131 /*
1132 * the element should be instantiated in the tree if:
1133 * - this is a namespace prefix
1134 * - the user required for completion in the tree
1135 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001136 * - there isn't already an attribute definition
1137 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001138 */
1139 if (((attr->prefix != NULL) &&
1140 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1141 ((attr->prefix == NULL) &&
1142 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1143 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001144 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001145
Daniel Veillard8aff2472002-02-19 21:50:43 +00001146 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1147 attr->elem, attr->name,
1148 attr->prefix);
1149 if ((tst == attr) || (tst == NULL)) {
1150 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001151
Daniel Veillard8aff2472002-02-19 21:50:43 +00001152 if (attr->prefix != NULL) {
1153 fulln = xmlStrdup(attr->prefix);
1154 fulln = xmlStrcat(fulln, BAD_CAST ":");
1155 fulln = xmlStrcat(fulln, attr->name);
1156 } else {
1157 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001158 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001159
1160 /*
1161 * Check that the attribute is not declared in the
1162 * serialization
1163 */
1164 att = NULL;
1165 if (atts != NULL) {
1166 i = 0;
1167 att = atts[i];
1168 while (att != NULL) {
1169 if (xmlStrEqual(att, fulln))
1170 break;
1171 i += 2;
1172 att = atts[i];
1173 }
1174 }
1175 if (att == NULL) {
1176 attribute(ctxt, fulln, attr->defaultValue);
1177 }
1178 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001179 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001180 }
1181 }
1182 attr = attr->nexth;
1183 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001184 if (internal == 1) {
1185 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1186 name, prefix);
1187 internal = 0;
1188 goto process_external_subset;
1189 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001190 }
1191}
1192
Owen Taylor3473f882001-02-23 17:55:21 +00001193/**
1194 * startElement:
1195 * @ctx: the user data (XML parser context)
1196 * @fullname: The element name, including namespace prefix
1197 * @atts: An array of name/value attributes pairs, NULL terminated
1198 *
1199 * called when an opening tag has been processed.
1200 */
1201void
1202startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1203{
1204 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1205 xmlNodePtr ret;
1206 xmlNodePtr parent = ctxt->node;
1207 xmlNsPtr ns;
1208 xmlChar *name;
1209 xmlChar *prefix;
1210 const xmlChar *att;
1211 const xmlChar *value;
1212 int i;
1213
1214#ifdef DEBUG_SAX
1215 xmlGenericError(xmlGenericErrorContext,
1216 "SAX.startElement(%s)\n", fullname);
1217#endif
1218
1219 /*
1220 * First check on validity:
1221 */
1222 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1223 ((ctxt->myDoc->intSubset == NULL) ||
1224 ((ctxt->myDoc->intSubset->notations == NULL) &&
1225 (ctxt->myDoc->intSubset->elements == NULL) &&
1226 (ctxt->myDoc->intSubset->attributes == NULL) &&
1227 (ctxt->myDoc->intSubset->entities == NULL)))) {
1228 if (ctxt->vctxt.error != NULL) {
1229 ctxt->vctxt.error(ctxt->vctxt.userData,
1230 "Validation failed: no DTD found !\n");
1231 }
1232 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001233 ctxt->valid = 0;
1234 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001235 }
1236
1237
1238 /*
1239 * Split the full name into a namespace prefix and the tag name
1240 */
1241 name = xmlSplitQName(ctxt, fullname, &prefix);
1242
1243
1244 /*
1245 * Note : the namespace resolution is deferred until the end of the
1246 * attributes parsing, since local namespace can be defined as
1247 * an attribute at this level.
1248 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001249 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001250 if (ret == NULL) return;
1251 if (ctxt->myDoc->children == NULL) {
1252#ifdef DEBUG_SAX_TREE
1253 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1254#endif
1255 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1256 } else if (parent == NULL) {
1257 parent = ctxt->myDoc->children;
1258 }
1259 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001260 if (ctxt->linenumbers) {
1261 if (ctxt->input != NULL)
1262 ret->content = (void *) (long) ctxt->input->line;
1263 }
Owen Taylor3473f882001-02-23 17:55:21 +00001264
1265 /*
1266 * We are parsing a new node.
1267 */
1268#ifdef DEBUG_SAX_TREE
1269 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1270#endif
1271 nodePush(ctxt, ret);
1272
1273 /*
1274 * Link the child element
1275 */
1276 if (parent != NULL) {
1277 if (parent->type == XML_ELEMENT_NODE) {
1278#ifdef DEBUG_SAX_TREE
1279 xmlGenericError(xmlGenericErrorContext,
1280 "adding child %s to %s\n", name, parent->name);
1281#endif
1282 xmlAddChild(parent, ret);
1283 } else {
1284#ifdef DEBUG_SAX_TREE
1285 xmlGenericError(xmlGenericErrorContext,
1286 "adding sibling %s to ", name);
1287 xmlDebugDumpOneNode(stderr, parent, 0);
1288#endif
1289 xmlAddSibling(parent, ret);
1290 }
1291 }
1292
1293 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001294 * Insert all the defaulted attributes from the DTD especially namespaces
1295 */
1296 if ((!ctxt->html) &&
1297 ((ctxt->myDoc->intSubset != NULL) ||
1298 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001299 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001300 }
1301
1302 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001303 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001304 */
1305 if (atts != NULL) {
1306 i = 0;
1307 att = atts[i++];
1308 value = atts[i++];
1309 if (!ctxt->html) {
1310 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001311 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1312 (att[3] == 'n') && (att[4] == 's'))
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001313 my_attribute(ctxt, att, value, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001314
1315 att = atts[i++];
1316 value = atts[i++];
1317 }
1318 }
1319 }
1320
1321 /*
1322 * Search the namespace, note that since the attributes have been
1323 * processed, the local namespaces are available.
1324 */
1325 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1326 if ((ns == NULL) && (parent != NULL))
1327 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1328 if ((prefix != NULL) && (ns == NULL)) {
1329 ns = xmlNewNs(ret, NULL, prefix);
1330 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1331 ctxt->sax->warning(ctxt->userData,
1332 "Namespace prefix %s is not defined\n", prefix);
1333 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001334
1335 /*
1336 * set the namespace node, making sure that if the default namspace
1337 * is unbound on a parent we simply kee it NULL
1338 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001339 if ((ns != NULL) && (ns->href != NULL) &&
1340 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001341 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001342
1343 /*
1344 * process all the other attributes
1345 */
1346 if (atts != NULL) {
1347 i = 0;
1348 att = atts[i++];
1349 value = atts[i++];
1350 if (ctxt->html) {
1351 while (att != NULL) {
1352 attribute(ctxt, att, value);
1353 att = atts[i++];
1354 value = atts[i++];
1355 }
1356 } else {
1357 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001358 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1359 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001360 attribute(ctxt, att, value);
1361
1362 /*
1363 * Next ones
1364 */
1365 att = atts[i++];
1366 value = atts[i++];
1367 }
1368 }
1369 }
1370
1371 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001372 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001373 * check the document root element for validity
1374 */
1375 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001376 int chk;
1377
1378 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1379 if (chk <= 0)
1380 ctxt->valid = 0;
1381 if (chk < 0)
1382 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001383 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1384 ctxt->vctxt.finishDtd = 1;
1385 }
1386
1387 if (prefix != NULL)
1388 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001389
1390}
1391
1392/**
1393 * endElement:
1394 * @ctx: the user data (XML parser context)
1395 * @name: The element name
1396 *
1397 * called when the end of an element has been detected.
1398 */
1399void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001400endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001401{
1402 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1403 xmlParserNodeInfo node_info;
1404 xmlNodePtr cur = ctxt->node;
1405
1406#ifdef DEBUG_SAX
1407 if (name == NULL)
1408 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1409 else
1410 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1411#endif
1412
1413 /* Capture end position and add node */
1414 if (cur != NULL && ctxt->record_info) {
1415 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1416 node_info.end_line = ctxt->input->line;
1417 node_info.node = cur;
1418 xmlParserAddNodeInfo(ctxt, &node_info);
1419 }
1420 ctxt->nodemem = -1;
1421
1422 if (ctxt->validate && ctxt->wellFormed &&
1423 ctxt->myDoc && ctxt->myDoc->intSubset)
1424 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1425 cur);
1426
1427
1428 /*
1429 * end of parsing of this node.
1430 */
1431#ifdef DEBUG_SAX_TREE
1432 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1433#endif
1434 nodePop(ctxt);
1435}
1436
1437/**
1438 * reference:
1439 * @ctx: the user data (XML parser context)
1440 * @name: The entity name
1441 *
1442 * called when an entity reference is detected.
1443 */
1444void
1445reference(void *ctx, const xmlChar *name)
1446{
1447 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1448 xmlNodePtr ret;
1449
1450#ifdef DEBUG_SAX
1451 xmlGenericError(xmlGenericErrorContext,
1452 "SAX.reference(%s)\n", name);
1453#endif
1454 if (name[0] == '#')
1455 ret = xmlNewCharRef(ctxt->myDoc, name);
1456 else
1457 ret = xmlNewReference(ctxt->myDoc, name);
1458#ifdef DEBUG_SAX_TREE
1459 xmlGenericError(xmlGenericErrorContext,
1460 "add reference %s to %s \n", name, ctxt->node->name);
1461#endif
1462 xmlAddChild(ctxt->node, ret);
1463}
1464
1465/**
1466 * characters:
1467 * @ctx: the user data (XML parser context)
1468 * @ch: a xmlChar string
1469 * @len: the number of xmlChar
1470 *
1471 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001472 */
1473void
1474characters(void *ctx, const xmlChar *ch, int len)
1475{
1476 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1477 xmlNodePtr lastChild;
1478
1479#ifdef DEBUG_SAX
1480 xmlGenericError(xmlGenericErrorContext,
1481 "SAX.characters(%.30s, %d)\n", ch, len);
1482#endif
1483 /*
1484 * Handle the data if any. If there is no child
1485 * add it as content, otherwise if the last child is text,
1486 * concatenate it, else create a new node of type text.
1487 */
1488
1489 if (ctxt->node == NULL) {
1490#ifdef DEBUG_SAX_TREE
1491 xmlGenericError(xmlGenericErrorContext,
1492 "add chars: ctxt->node == NULL !\n");
1493#endif
1494 return;
1495 }
1496 lastChild = xmlGetLastChild(ctxt->node);
1497#ifdef DEBUG_SAX_TREE
1498 xmlGenericError(xmlGenericErrorContext,
1499 "add chars to %s \n", ctxt->node->name);
1500#endif
1501
1502 /*
1503 * Here we needed an accelerator mechanism in case of very large
1504 * elements. Use an attribute in the structure !!!
1505 */
1506 if (lastChild == NULL) {
1507 /* first node, first time */
1508 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001509 if (ctxt->node->children != NULL) {
1510 ctxt->nodelen = len;
1511 ctxt->nodemem = len + 1;
1512 }
Owen Taylor3473f882001-02-23 17:55:21 +00001513 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001514 int coalesceText = (lastChild != NULL) &&
1515 (lastChild->type == XML_TEXT_NODE) &&
1516 (lastChild->name == xmlStringText);
1517 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001518 /*
1519 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001520 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001521 * reallocate a new buffer, move data, append ch. Here
1522 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001523 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001524 */
1525 if (ctxt->nodelen + len >= ctxt->nodemem) {
1526 xmlChar *newbuf;
1527 int size;
1528
1529 size = ctxt->nodemem + len;
1530 size *= 2;
1531 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1532 if (newbuf == NULL) {
1533 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1534 ctxt->sax->error(ctxt->userData,
1535 "SAX.characters(): out of memory\n");
1536 return;
1537 }
1538 ctxt->nodemem = size;
1539 lastChild->content = newbuf;
1540 }
1541 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1542 ctxt->nodelen += len;
1543 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001544 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001545 xmlTextConcat(lastChild, ch, len);
1546 if (ctxt->node->children != NULL) {
1547 ctxt->nodelen = xmlStrlen(lastChild->content);
1548 ctxt->nodemem = ctxt->nodelen + 1;
1549 }
Owen Taylor3473f882001-02-23 17:55:21 +00001550 } else {
1551 /* Mixed content, first time */
1552 lastChild = xmlNewTextLen(ch, len);
1553 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001554 if (ctxt->node->children != NULL) {
1555 ctxt->nodelen = len;
1556 ctxt->nodemem = len + 1;
1557 }
Owen Taylor3473f882001-02-23 17:55:21 +00001558 }
1559 }
1560}
1561
1562/**
1563 * ignorableWhitespace:
1564 * @ctx: the user data (XML parser context)
1565 * @ch: a xmlChar string
1566 * @len: the number of xmlChar
1567 *
1568 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001569 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001570 */
1571void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001572ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001573{
1574 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1575#ifdef DEBUG_SAX
1576 xmlGenericError(xmlGenericErrorContext,
1577 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1578#endif
1579}
1580
1581/**
1582 * processingInstruction:
1583 * @ctx: the user data (XML parser context)
1584 * @target: the target name
1585 * @data: the PI data's
1586 *
1587 * A processing instruction has been parsed.
1588 */
1589void
1590processingInstruction(void *ctx, const xmlChar *target,
1591 const xmlChar *data)
1592{
1593 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1594 xmlNodePtr ret;
1595 xmlNodePtr parent = ctxt->node;
1596
1597#ifdef DEBUG_SAX
1598 xmlGenericError(xmlGenericErrorContext,
1599 "SAX.processingInstruction(%s, %s)\n", target, data);
1600#endif
1601
1602 ret = xmlNewPI(target, data);
1603 if (ret == NULL) return;
1604 parent = ctxt->node;
1605
1606 if (ctxt->inSubset == 1) {
1607 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1608 return;
1609 } else if (ctxt->inSubset == 2) {
1610 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1611 return;
1612 }
1613 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1614#ifdef DEBUG_SAX_TREE
1615 xmlGenericError(xmlGenericErrorContext,
1616 "Setting PI %s as root\n", target);
1617#endif
1618 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1619 return;
1620 }
1621 if (parent->type == XML_ELEMENT_NODE) {
1622#ifdef DEBUG_SAX_TREE
1623 xmlGenericError(xmlGenericErrorContext,
1624 "adding PI %s child to %s\n", target, parent->name);
1625#endif
1626 xmlAddChild(parent, ret);
1627 } else {
1628#ifdef DEBUG_SAX_TREE
1629 xmlGenericError(xmlGenericErrorContext,
1630 "adding PI %s sibling to ", target);
1631 xmlDebugDumpOneNode(stderr, parent, 0);
1632#endif
1633 xmlAddSibling(parent, ret);
1634 }
1635}
1636
1637/**
1638 * globalNamespace:
1639 * @ctx: the user data (XML parser context)
1640 * @href: the namespace associated URN
1641 * @prefix: the namespace prefix
1642 *
1643 * An old global namespace has been parsed.
1644 */
1645void
1646globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1647{
1648 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1649#ifdef DEBUG_SAX
1650 xmlGenericError(xmlGenericErrorContext,
1651 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1652#endif
1653 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1654}
1655
1656/**
1657 * setNamespace:
1658 * @ctx: the user data (XML parser context)
1659 * @name: the namespace prefix
1660 *
1661 * Set the current element namespace.
1662 */
1663
1664void
1665setNamespace(void *ctx, const xmlChar *name)
1666{
1667 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1668 xmlNsPtr ns;
1669 xmlNodePtr parent;
1670
1671#ifdef DEBUG_SAX
1672 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1673#endif
1674 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1675 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1676 if (ctxt->nodeNr >= 2) {
1677 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1678 if (parent != NULL)
1679 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1680 }
1681 }
1682 xmlSetNs(ctxt->node, ns);
1683}
1684
1685/**
1686 * getNamespace:
1687 * @ctx: the user data (XML parser context)
1688 *
1689 * Get the current element namespace.
1690 *
1691 * Returns the xmlNsPtr or NULL if none
1692 */
1693
1694xmlNsPtr
1695getNamespace(void *ctx)
1696{
1697 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1698 xmlNsPtr ret;
1699
1700#ifdef DEBUG_SAX
1701 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1702#endif
1703 ret = ctxt->node->ns;
1704 return(ret);
1705}
1706
1707/**
1708 * checkNamespace:
1709 * @ctx: the user data (XML parser context)
1710 * @namespace: the namespace to check against
1711 *
1712 * Check that the current element namespace is the same as the
1713 * one read upon parsing.
1714 *
1715 * Returns 1 if true 0 otherwise
1716 */
1717
1718int
1719checkNamespace(void *ctx, xmlChar *namespace)
1720{
1721 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1722 xmlNodePtr cur = ctxt->node;
1723
1724#ifdef DEBUG_SAX
1725 xmlGenericError(xmlGenericErrorContext,
1726 "SAX.checkNamespace(%s)\n", namespace);
1727#endif
1728
1729 /*
1730 * Check that the Name in the ETag is the same as in the STag.
1731 */
1732 if (namespace == NULL) {
1733 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1734 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1735 ctxt->sax->error(ctxt,
1736 "End tags for %s don't hold the namespace %s\n",
1737 cur->name, cur->ns->prefix);
1738 ctxt->wellFormed = 0;
1739 }
1740 } else {
1741 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1742 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1743 ctxt->sax->error(ctxt,
1744 "End tags %s holds a prefix %s not used by the open tag\n",
1745 cur->name, namespace);
1746 ctxt->wellFormed = 0;
1747 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1748 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1749 ctxt->sax->error(ctxt,
1750 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1751 cur->name, cur->ns->prefix, namespace);
1752 ctxt->wellFormed = 0;
1753 } else
1754 return(1);
1755 }
1756 return(0);
1757}
1758
1759/**
1760 * namespaceDecl:
1761 * @ctx: the user data (XML parser context)
1762 * @href: the namespace associated URN
1763 * @prefix: the namespace prefix
1764 *
1765 * A namespace has been parsed.
1766 */
1767void
1768namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1769{
1770 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1771#ifdef DEBUG_SAX
1772 if (prefix == NULL)
1773 xmlGenericError(xmlGenericErrorContext,
1774 "SAX.namespaceDecl(%s, NULL)\n", href);
1775 else
1776 xmlGenericError(xmlGenericErrorContext,
1777 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1778#endif
1779 xmlNewNs(ctxt->node, href, prefix);
1780}
1781
1782/**
1783 * comment:
1784 * @ctx: the user data (XML parser context)
1785 * @value: the comment content
1786 *
1787 * A comment has been parsed.
1788 */
1789void
1790comment(void *ctx, const xmlChar *value)
1791{
1792 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1793 xmlNodePtr ret;
1794 xmlNodePtr parent = ctxt->node;
1795
1796#ifdef DEBUG_SAX
1797 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1798#endif
1799 ret = xmlNewDocComment(ctxt->myDoc, value);
1800 if (ret == NULL) return;
1801
1802 if (ctxt->inSubset == 1) {
1803 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1804 return;
1805 } else if (ctxt->inSubset == 2) {
1806 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1807 return;
1808 }
1809 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1810#ifdef DEBUG_SAX_TREE
1811 xmlGenericError(xmlGenericErrorContext,
1812 "Setting comment as root\n");
1813#endif
1814 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1815 return;
1816 }
1817 if (parent->type == XML_ELEMENT_NODE) {
1818#ifdef DEBUG_SAX_TREE
1819 xmlGenericError(xmlGenericErrorContext,
1820 "adding comment child to %s\n", parent->name);
1821#endif
1822 xmlAddChild(parent, ret);
1823 } else {
1824#ifdef DEBUG_SAX_TREE
1825 xmlGenericError(xmlGenericErrorContext,
1826 "adding comment sibling to ");
1827 xmlDebugDumpOneNode(stderr, parent, 0);
1828#endif
1829 xmlAddSibling(parent, ret);
1830 }
1831}
1832
1833/**
1834 * cdataBlock:
1835 * @ctx: the user data (XML parser context)
1836 * @value: The pcdata content
1837 * @len: the block length
1838 *
1839 * called when a pcdata block has been parsed
1840 */
1841void
1842cdataBlock(void *ctx, const xmlChar *value, int len)
1843{
1844 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1845 xmlNodePtr ret, lastChild;
1846
1847#ifdef DEBUG_SAX
1848 xmlGenericError(xmlGenericErrorContext,
1849 "SAX.pcdata(%.10s, %d)\n", value, len);
1850#endif
1851 lastChild = xmlGetLastChild(ctxt->node);
1852#ifdef DEBUG_SAX_TREE
1853 xmlGenericError(xmlGenericErrorContext,
1854 "add chars to %s \n", ctxt->node->name);
1855#endif
1856 if ((lastChild != NULL) &&
1857 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1858 xmlTextConcat(lastChild, value, len);
1859 } else {
1860 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1861 xmlAddChild(ctxt->node, ret);
1862 }
1863}
1864
Daniel Veillardd0463562001-10-13 09:15:48 +00001865/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001866 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001867 * @hdlr: the SAX handler
1868 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001869 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001870 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001871 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001872void
1873initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1874{
1875 if(hdlr->initialized == 1)
1876 return;
1877
1878 hdlr->internalSubset = internalSubset;
1879 hdlr->externalSubset = externalSubset;
1880 hdlr->isStandalone = isStandalone;
1881 hdlr->hasInternalSubset = hasInternalSubset;
1882 hdlr->hasExternalSubset = hasExternalSubset;
1883 hdlr->resolveEntity = resolveEntity;
1884 hdlr->getEntity = getEntity;
1885 hdlr->getParameterEntity = getParameterEntity;
1886 hdlr->entityDecl = entityDecl;
1887 hdlr->attributeDecl = attributeDecl;
1888 hdlr->elementDecl = elementDecl;
1889 hdlr->notationDecl = notationDecl;
1890 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1891 hdlr->setDocumentLocator = setDocumentLocator;
1892 hdlr->startDocument = startDocument;
1893 hdlr->endDocument = endDocument;
1894 hdlr->startElement = startElement;
1895 hdlr->endElement = endElement;
1896 hdlr->reference = reference;
1897 hdlr->characters = characters;
1898 hdlr->cdataBlock = cdataBlock;
1899 hdlr->ignorableWhitespace = characters;
1900 hdlr->processingInstruction = processingInstruction;
1901 hdlr->comment = comment;
1902 /* if (xmlGetWarningsDefaultValue == 0) */
1903 if (warning == 0)
1904 hdlr->warning = NULL;
1905 else
1906 hdlr->warning = xmlParserWarning;
1907 hdlr->error = xmlParserError;
1908 hdlr->fatalError = xmlParserError;
1909
1910 hdlr->initialized = 1;
1911}
Owen Taylor3473f882001-02-23 17:55:21 +00001912
1913/**
1914 * xmlDefaultSAXHandlerInit:
1915 *
1916 * Initialize the default SAX handler
1917 */
1918void
1919xmlDefaultSAXHandlerInit(void)
1920{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001921 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001922}
1923
Daniel Veillardeae522a2001-04-23 13:41:34 +00001924#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001925
1926/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001927 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001928 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001929 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001930 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001931 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001932void
1933inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1934{
1935 if(hdlr->initialized == 1)
1936 return;
1937
1938 hdlr->internalSubset = internalSubset;
1939 hdlr->externalSubset = NULL;
1940 hdlr->isStandalone = NULL;
1941 hdlr->hasInternalSubset = NULL;
1942 hdlr->hasExternalSubset = NULL;
1943 hdlr->resolveEntity = NULL;
1944 hdlr->getEntity = getEntity;
1945 hdlr->getParameterEntity = NULL;
1946 hdlr->entityDecl = NULL;
1947 hdlr->attributeDecl = NULL;
1948 hdlr->elementDecl = NULL;
1949 hdlr->notationDecl = NULL;
1950 hdlr->unparsedEntityDecl = NULL;
1951 hdlr->setDocumentLocator = setDocumentLocator;
1952 hdlr->startDocument = startDocument;
1953 hdlr->endDocument = endDocument;
1954 hdlr->startElement = startElement;
1955 hdlr->endElement = endElement;
1956 hdlr->reference = NULL;
1957 hdlr->characters = characters;
1958 hdlr->cdataBlock = cdataBlock;
1959 hdlr->ignorableWhitespace = ignorableWhitespace;
1960 hdlr->processingInstruction = NULL;
1961 hdlr->comment = comment;
1962 hdlr->warning = xmlParserWarning;
1963 hdlr->error = xmlParserError;
1964 hdlr->fatalError = xmlParserError;
1965
1966 hdlr->initialized = 1;
1967}
Owen Taylor3473f882001-02-23 17:55:21 +00001968
1969/**
1970 * htmlDefaultSAXHandlerInit:
1971 *
1972 * Initialize the default SAX handler
1973 */
1974void
1975htmlDefaultSAXHandlerInit(void)
1976{
Daniel Veillardd0463562001-10-13 09:15:48 +00001977 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00001978}
Daniel Veillardd0463562001-10-13 09:15:48 +00001979
Daniel Veillardeae522a2001-04-23 13:41:34 +00001980#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001981
Daniel Veillardeae522a2001-04-23 13:41:34 +00001982#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001983
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001984/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001985 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001986 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001987 *
1988 * Initialize the default DocBook SAX handler
1989 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001990void
1991initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
1992{
1993 if(hdlr->initialized == 1)
1994 return;
1995
1996 hdlr->internalSubset = internalSubset;
1997 hdlr->externalSubset = NULL;
1998 hdlr->isStandalone = isStandalone;
1999 hdlr->hasInternalSubset = hasInternalSubset;
2000 hdlr->hasExternalSubset = hasExternalSubset;
2001 hdlr->resolveEntity = resolveEntity;
2002 hdlr->getEntity = getEntity;
2003 hdlr->getParameterEntity = NULL;
2004 hdlr->entityDecl = entityDecl;
2005 hdlr->attributeDecl = NULL;
2006 hdlr->elementDecl = NULL;
2007 hdlr->notationDecl = NULL;
2008 hdlr->unparsedEntityDecl = NULL;
2009 hdlr->setDocumentLocator = setDocumentLocator;
2010 hdlr->startDocument = startDocument;
2011 hdlr->endDocument = endDocument;
2012 hdlr->startElement = startElement;
2013 hdlr->endElement = endElement;
2014 hdlr->reference = reference;
2015 hdlr->characters = characters;
2016 hdlr->cdataBlock = NULL;
2017 hdlr->ignorableWhitespace = ignorableWhitespace;
2018 hdlr->processingInstruction = NULL;
2019 hdlr->comment = comment;
2020 hdlr->warning = xmlParserWarning;
2021 hdlr->error = xmlParserError;
2022 hdlr->fatalError = xmlParserError;
2023
2024 hdlr->initialized = 1;
2025}
Owen Taylor3473f882001-02-23 17:55:21 +00002026
2027/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00002028 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00002029 *
2030 * Initialize the default SAX handler
2031 */
2032void
Daniel Veillardeae522a2001-04-23 13:41:34 +00002033docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00002034{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002035 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002036}
Daniel Veillardeae522a2001-04-23 13:41:34 +00002037
2038#endif /* LIBXML_DOCB_ENABLED */