blob: b6375075e4f72a08a587332186156f7f99c01b14 [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 }
Daniel Veillarde2830f12003-01-08 17:47:49 +0000377 if ((ret != NULL) &&
378 ((ctxt->validate) || (ctxt->replaceEntities)) &&
379 (ret->children == NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000380 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
381 /*
382 * for validation purposes we really need to fetch and
383 * parse the external entity
384 */
Owen Taylor3473f882001-02-23 17:55:21 +0000385 xmlNodePtr children;
386
Daniel Veillard5015b712001-08-17 09:37:52 +0000387 xmlParseCtxtExternalEntity(ctxt, ret->URI, ret->ExternalID, &children);
Owen Taylor3473f882001-02-23 17:55:21 +0000388 xmlAddChildList((xmlNodePtr) ret, children);
Daniel Veillard8bf70b92003-01-07 23:14:24 +0000389 ret->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000390 }
391 return(ret);
392}
393
394/**
395 * getParameterEntity:
396 * @ctx: the user data (XML parser context)
397 * @name: The entity name
398 *
399 * Get a parameter entity by name
400 *
401 * Returns the xmlEntityPtr if found.
402 */
403xmlEntityPtr
404getParameterEntity(void *ctx, const xmlChar *name)
405{
406 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
407 xmlEntityPtr ret;
408
409#ifdef DEBUG_SAX
410 xmlGenericError(xmlGenericErrorContext,
411 "SAX.getParameterEntity(%s)\n", name);
412#endif
413
414 ret = xmlGetParameterEntity(ctxt->myDoc, name);
415 return(ret);
416}
417
418
419/**
420 * entityDecl:
421 * @ctx: the user data (XML parser context)
422 * @name: the entity name
423 * @type: the entity type
424 * @publicId: The public ID of the entity
425 * @systemId: The system ID of the entity
426 * @content: the entity value (without processing).
427 *
428 * An entity definition has been parsed
429 */
430void
431entityDecl(void *ctx, const xmlChar *name, int type,
432 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
433{
434 xmlEntityPtr ent;
435 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
436
437#ifdef DEBUG_SAX
438 xmlGenericError(xmlGenericErrorContext,
439 "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
440 name, type, publicId, systemId, content);
441#endif
442 if (ctxt->inSubset == 1) {
443 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
444 systemId, content);
445 if ((ent == NULL) && (ctxt->pedantic) &&
446 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
447 ctxt->sax->warning(ctxt,
448 "Entity(%s) already defined in the internal subset\n", name);
449 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
450 xmlChar *URI;
451 const char *base = NULL;
452
453 if (ctxt->input != NULL)
454 base = ctxt->input->filename;
455 if (base == NULL)
456 base = ctxt->directory;
457
458 URI = xmlBuildURI(systemId, (const xmlChar *) base);
459 ent->URI = URI;
460 }
461 } else if (ctxt->inSubset == 2) {
462 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
463 systemId, content);
464 if ((ent == NULL) && (ctxt->pedantic) &&
465 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
466 ctxt->sax->warning(ctxt,
467 "Entity(%s) already defined in the external subset\n", name);
468 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
469 xmlChar *URI;
470 const char *base = NULL;
471
472 if (ctxt->input != NULL)
473 base = ctxt->input->filename;
474 if (base == NULL)
475 base = ctxt->directory;
476
477 URI = xmlBuildURI(systemId, (const xmlChar *) base);
478 ent->URI = URI;
479 }
480 } else {
481 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
482 ctxt->sax->error(ctxt,
483 "SAX.entityDecl(%s) called while not in subset\n", name);
484 }
485}
486
487/**
488 * attributeDecl:
489 * @ctx: the user data (XML parser context)
490 * @elem: the name of the element
491 * @fullname: the attribute name
492 * @type: the attribute type
493 * @def: the type of default value
494 * @defaultValue: the attribute default value
495 * @tree: the tree of enumerated value set
496 *
497 * An attribute definition has been parsed
498 */
499void
500attributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
501 int type, int def, const xmlChar *defaultValue,
502 xmlEnumerationPtr tree)
503{
504 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
505 xmlAttributePtr attr;
506 xmlChar *name = NULL, *prefix = NULL;
507
508#ifdef DEBUG_SAX
509 xmlGenericError(xmlGenericErrorContext,
510 "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
511 elem, fullname, type, def, defaultValue);
512#endif
513 name = xmlSplitQName(ctxt, fullname, &prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +0000514 ctxt->vctxt.valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000515 if (ctxt->inSubset == 1)
516 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
517 name, prefix, (xmlAttributeType) type,
518 (xmlAttributeDefault) def, defaultValue, tree);
519 else if (ctxt->inSubset == 2)
520 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
521 name, prefix, (xmlAttributeType) type,
522 (xmlAttributeDefault) def, defaultValue, tree);
523 else {
524 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
525 ctxt->sax->error(ctxt,
526 "SAX.attributeDecl(%s) called while not in subset\n", name);
527 return;
528 }
Daniel Veillardc7612992002-02-17 22:47:37 +0000529 if (ctxt->vctxt.valid == 0)
530 ctxt->valid = 0;
Daniel Veillardd85f4f42002-03-25 10:48:46 +0000531 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
532 (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000533 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
534 attr);
535 if (prefix != NULL)
536 xmlFree(prefix);
537 if (name != NULL)
538 xmlFree(name);
539}
540
541/**
542 * elementDecl:
543 * @ctx: the user data (XML parser context)
544 * @name: the element name
545 * @type: the element type
546 * @content: the element value tree
547 *
548 * An element definition has been parsed
549 */
550void
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000551elementDecl(void *ctx, const xmlChar * name, int type,
552 xmlElementContentPtr content)
Owen Taylor3473f882001-02-23 17:55:21 +0000553{
554 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
555 xmlElementPtr elem = NULL;
556
557#ifdef DEBUG_SAX
558 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000559 "SAX.elementDecl(%s, %d, ...)\n", name, type);
Owen Taylor3473f882001-02-23 17:55:21 +0000560#endif
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000561
Owen Taylor3473f882001-02-23 17:55:21 +0000562 if (ctxt->inSubset == 1)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000563 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
564 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000565 else if (ctxt->inSubset == 2)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000566 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
567 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000568 else {
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000569 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
570 ctxt->sax->error(ctxt,
571 "SAX.elementDecl(%s) called while not in subset\n",
572 name);
573 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000574 }
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000575 if (elem == NULL)
576 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000577 if (ctxt->validate && ctxt->wellFormed &&
578 ctxt->myDoc && ctxt->myDoc->intSubset)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000579 ctxt->valid &=
580 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Owen Taylor3473f882001-02-23 17:55:21 +0000581}
582
583/**
584 * notationDecl:
585 * @ctx: the user data (XML parser context)
586 * @name: The name of the notation
587 * @publicId: The public ID of the entity
588 * @systemId: The system ID of the entity
589 *
590 * What to do when a notation declaration has been parsed.
591 */
592void
593notationDecl(void *ctx, const xmlChar *name,
594 const xmlChar *publicId, const xmlChar *systemId)
595{
596 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
597 xmlNotationPtr nota = NULL;
598
599#ifdef DEBUG_SAX
600 xmlGenericError(xmlGenericErrorContext,
601 "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
602#endif
603
Daniel Veillard7aea52d2002-02-17 23:07:47 +0000604 if ((publicId == NULL) && (systemId == NULL)) {
605 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
606 ctxt->sax->error(ctxt,
607 "SAX.notationDecl(%s) externalID or PublicID missing\n", name);
608 ctxt->valid = 0;
609 ctxt->wellFormed = 0;
610 return;
611 } else if (ctxt->inSubset == 1)
Owen Taylor3473f882001-02-23 17:55:21 +0000612 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
613 publicId, systemId);
614 else if (ctxt->inSubset == 2)
Daniel Veillard25239c12001-03-14 13:56:48 +0000615 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
Owen Taylor3473f882001-02-23 17:55:21 +0000616 publicId, systemId);
617 else {
618 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
619 ctxt->sax->error(ctxt,
620 "SAX.notationDecl(%s) called while not in subset\n", name);
621 return;
622 }
623 if (nota == NULL) ctxt->valid = 0;
624 if (ctxt->validate && ctxt->wellFormed &&
625 ctxt->myDoc && ctxt->myDoc->intSubset)
626 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
627 nota);
628}
629
630/**
631 * unparsedEntityDecl:
632 * @ctx: the user data (XML parser context)
633 * @name: The name of the entity
634 * @publicId: The public ID of the entity
635 * @systemId: The system ID of the entity
636 * @notationName: the name of the notation
637 *
638 * What to do when an unparsed entity declaration is parsed
639 */
640void
641unparsedEntityDecl(void *ctx, const xmlChar *name,
642 const xmlChar *publicId, const xmlChar *systemId,
643 const xmlChar *notationName)
644{
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000645 xmlEntityPtr ent;
Owen Taylor3473f882001-02-23 17:55:21 +0000646 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
647#ifdef DEBUG_SAX
648 xmlGenericError(xmlGenericErrorContext,
649 "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
650 name, publicId, systemId, notationName);
651#endif
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000652#if 0
653 Done in xmlValidateDtdFinal now.
Daniel Veillard28757702002-02-18 11:19:30 +0000654 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc) {
655 int ret;
656 ret = xmlValidateNotationUse(&ctxt->vctxt, ctxt->myDoc,
Owen Taylor3473f882001-02-23 17:55:21 +0000657 notationName);
Daniel Veillard28757702002-02-18 11:19:30 +0000658 if (ret == 0) {
659 ctxt->wellFormed = 0;
660 ctxt->valid = 0;
661 }
662 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000663#endif
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000664 if (ctxt->inSubset == 1) {
665 ent = xmlAddDocEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000666 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
667 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000668 if ((ent == NULL) && (ctxt->pedantic) &&
669 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
670 ctxt->sax->warning(ctxt,
671 "Entity(%s) already defined in the internal subset\n", name);
672 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
673 xmlChar *URI;
674 const char *base = NULL;
675
676 if (ctxt->input != NULL)
677 base = ctxt->input->filename;
678 if (base == NULL)
679 base = ctxt->directory;
680
681 URI = xmlBuildURI(systemId, (const xmlChar *) base);
682 ent->URI = URI;
683 }
684 } else if (ctxt->inSubset == 2) {
685 ent = xmlAddDtdEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000686 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
687 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000688 if ((ent == NULL) && (ctxt->pedantic) &&
689 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
690 ctxt->sax->warning(ctxt,
691 "Entity(%s) already defined in the external subset\n", name);
692 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
693 xmlChar *URI;
694 const char *base = NULL;
695
696 if (ctxt->input != NULL)
697 base = ctxt->input->filename;
698 if (base == NULL)
699 base = ctxt->directory;
700
701 URI = xmlBuildURI(systemId, (const xmlChar *) base);
702 ent->URI = URI;
703 }
704 } else {
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000705 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
706 ctxt->sax->error(ctxt,
707 "SAX.unparsedEntityDecl(%s) called while not in subset\n", name);
708 }
Owen Taylor3473f882001-02-23 17:55:21 +0000709}
710
711/**
712 * setDocumentLocator:
713 * @ctx: the user data (XML parser context)
714 * @loc: A SAX Locator
715 *
716 * Receive the document locator at startup, actually xmlDefaultSAXLocator
717 * Everything is available on the context, so this is useless in our case.
718 */
719void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000720setDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +0000721{
722 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
723#ifdef DEBUG_SAX
724 xmlGenericError(xmlGenericErrorContext,
725 "SAX.setDocumentLocator()\n");
726#endif
727}
728
729/**
730 * startDocument:
731 * @ctx: the user data (XML parser context)
732 *
733 * called when the document start being processed.
734 */
735void
736startDocument(void *ctx)
737{
738 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
739 xmlDocPtr doc;
740
741#ifdef DEBUG_SAX
742 xmlGenericError(xmlGenericErrorContext,
743 "SAX.startDocument()\n");
744#endif
745 if (ctxt->html) {
746 if (ctxt->myDoc == NULL)
747#ifdef LIBXML_HTML_ENABLED
748 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
749#else
750 xmlGenericError(xmlGenericErrorContext,
751 "libxml2 built without HTML support\n");
752#endif
753 } else {
754 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
755 if (doc != NULL) {
756 if (ctxt->encoding != NULL)
757 doc->encoding = xmlStrdup(ctxt->encoding);
758 else
759 doc->encoding = NULL;
760 doc->standalone = ctxt->standalone;
761 }
762 }
763 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
764 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000765 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
Owen Taylor3473f882001-02-23 17:55:21 +0000766 }
767}
768
769/**
770 * endDocument:
771 * @ctx: the user data (XML parser context)
772 *
773 * called when the document end has been detected.
774 */
775void
776endDocument(void *ctx)
777{
778 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
779#ifdef DEBUG_SAX
780 xmlGenericError(xmlGenericErrorContext,
781 "SAX.endDocument()\n");
782#endif
783 if (ctxt->validate && ctxt->wellFormed &&
784 ctxt->myDoc && ctxt->myDoc->intSubset)
785 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
786
787 /*
788 * Grab the encoding if it was added on-the-fly
789 */
790 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
791 (ctxt->myDoc->encoding == NULL)) {
792 ctxt->myDoc->encoding = ctxt->encoding;
793 ctxt->encoding = NULL;
794 }
795 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
796 (ctxt->myDoc->encoding == NULL)) {
797 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
798 }
799 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
800 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
801 ctxt->myDoc->charset = ctxt->charset;
802 }
803}
804
805/**
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000806 * my_attribute:
Owen Taylor3473f882001-02-23 17:55:21 +0000807 * @ctx: the user data (XML parser context)
808 * @fullname: The attribute name, including namespace prefix
809 * @value: The attribute value
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000810 * @prefix: the prefix on the element node
Owen Taylor3473f882001-02-23 17:55:21 +0000811 *
812 * Handle an attribute that has been read by the parser.
813 * The default handling is to convert the attribute into an
814 * DOM subtree and past it in a new xmlAttr element added to
815 * the element.
816 */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000817static void
818my_attribute(void *ctx, const xmlChar *fullname, const xmlChar *value,
819 const xmlChar *prefix)
Owen Taylor3473f882001-02-23 17:55:21 +0000820{
821 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
822 xmlAttrPtr ret;
823 xmlChar *name;
824 xmlChar *ns;
825 xmlChar *nval;
826 xmlNsPtr namespace;
827
828/****************
829#ifdef DEBUG_SAX
830 xmlGenericError(xmlGenericErrorContext,
831 "SAX.attribute(%s, %s)\n", fullname, value);
832#endif
833 ****************/
834 /*
835 * Split the full name into a namespace prefix and the tag name
836 */
837 name = xmlSplitQName(ctxt, fullname, &ns);
838
839 /*
840 * Do the last stage of the attribute normalization
841 * Needed for HTML too:
842 * http://www.w3.org/TR/html4/types.html#h-6.2
843 */
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000844 ctxt->vctxt.valid = 1;
845 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
846 ctxt->myDoc, ctxt->node,
Owen Taylor3473f882001-02-23 17:55:21 +0000847 fullname, value);
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000848 if (ctxt->vctxt.valid != 1) {
849 ctxt->valid = 0;
850 }
Owen Taylor3473f882001-02-23 17:55:21 +0000851 if (nval != NULL)
852 value = nval;
853
854 /*
855 * Check whether it's a namespace definition
856 */
857 if ((!ctxt->html) && (ns == NULL) &&
858 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
859 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000860 xmlNsPtr nsret;
861
Owen Taylor3473f882001-02-23 17:55:21 +0000862 if (value[0] != 0) {
863 xmlURIPtr uri;
864
865 uri = xmlParseURI((const char *)value);
866 if (uri == NULL) {
867 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
868 ctxt->sax->warning(ctxt->userData,
869 "nmlns: %s not a valid URI\n", value);
870 } else {
871 if (uri->scheme == NULL) {
872 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
873 ctxt->sax->warning(ctxt->userData,
Daniel Veillardecaba492002-12-30 10:55:29 +0000874 "xmlns: URI %s is not absolute\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +0000875 }
876 xmlFreeURI(uri);
877 }
878 }
879
880 /* a default namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000881 nsret = xmlNewNs(ctxt->node, value, NULL);
882
883 /*
884 * Validate also for namespace decls, they are attributes from
885 * an XML-1.0 perspective
886 */
887 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
888 ctxt->myDoc && ctxt->myDoc->intSubset)
889 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
890 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000891 if (name != NULL)
892 xmlFree(name);
893 if (nval != NULL)
894 xmlFree(nval);
895 return;
896 }
897 if ((!ctxt->html) &&
898 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
899 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000900 xmlNsPtr nsret;
901
Daniel Veillardc0fef772002-03-01 16:16:31 +0000902 if (value[0] == 0) {
903 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
904 ctxt->sax->error(ctxt->userData,
905 "Empty namespace name for prefix %s\n", name);
906 }
Daniel Veillard7b4b2f92003-01-06 13:11:20 +0000907 if ((ctxt->pedantic != 0) && (value[0] != 0)) {
Daniel Veillardecaba492002-12-30 10:55:29 +0000908 xmlURIPtr uri;
909
910 uri = xmlParseURI((const char *)value);
911 if (uri == NULL) {
912 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
913 ctxt->sax->warning(ctxt->userData,
914 "xmlns:%s: %s not a valid URI\n", name, value);
915 } else {
916 if (uri->scheme == NULL) {
917 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
918 ctxt->sax->warning(ctxt->userData,
919 "xmlns:%s: URI %s is not absolute\n", name, value);
920 }
921 xmlFreeURI(uri);
922 }
923 }
924
Owen Taylor3473f882001-02-23 17:55:21 +0000925 /* a standard namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000926 nsret = xmlNewNs(ctxt->node, value, name);
Owen Taylor3473f882001-02-23 17:55:21 +0000927 xmlFree(ns);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000928 /*
929 * Validate also for namespace decls, they are attributes from
930 * an XML-1.0 perspective
931 */
932 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
933 ctxt->myDoc && ctxt->myDoc->intSubset)
934 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
935 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000936 if (name != NULL)
937 xmlFree(name);
938 if (nval != NULL)
939 xmlFree(nval);
940 return;
941 }
942
Daniel Veillardde590ca2003-02-05 10:45:26 +0000943 if (ns != NULL) {
944 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +0000945 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardde590ca2003-02-05 10:45:26 +0000946
947 prop = ctxt->node->properties;
948 while (prop != NULL) {
949 if (prop->ns != NULL) {
950 if ((xmlStrEqual(name, prop->name)) &&
951 ((namespace == prop->ns) ||
952 (xmlStrEqual(namespace->href, prop->ns->href)))) {
953 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
954 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
955 ctxt->sax->error(ctxt->userData,
956 "Attribute %s in %s redefined\n",
957 name, namespace->href);
958 ctxt->wellFormed = 0;
959 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
960 goto error;
961 }
962 }
963 prop = prop->next;
964 }
965 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000966 namespace = NULL;
967 }
968
969 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +0000970 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000971
972 if (ret != NULL) {
973 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
974 xmlNodePtr tmp;
975
976 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
977 tmp = ret->children;
978 while (tmp != NULL) {
979 tmp->parent = (xmlNodePtr) ret;
980 if (tmp->next == NULL)
981 ret->last = tmp;
982 tmp = tmp->next;
983 }
984 } else if (value != NULL) {
985 ret->children = xmlNewDocText(ctxt->myDoc, value);
986 ret->last = ret->children;
987 if (ret->children != NULL)
988 ret->children->parent = (xmlNodePtr) ret;
989 }
990 }
991
992 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
993 ctxt->myDoc && ctxt->myDoc->intSubset) {
994
995 /*
996 * If we don't substitute entities, the validation should be
997 * done on a value with replaced entities anyway.
998 */
999 if (!ctxt->replaceEntities) {
1000 xmlChar *val;
1001
1002 ctxt->depth++;
1003 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1004 0,0,0);
1005 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +00001006
Owen Taylor3473f882001-02-23 17:55:21 +00001007 if (val == NULL)
1008 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1009 ctxt->myDoc, ctxt->node, ret, value);
1010 else {
Daniel Veillardc7612992002-02-17 22:47:37 +00001011 xmlChar *nvalnorm;
1012
1013 /*
1014 * Do the last stage of the attribute normalization
1015 * It need to be done twice ... it's an extra burden related
1016 * to the ability to keep references in attributes
1017 */
1018 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1019 ctxt->node, fullname, val);
1020 if (nvalnorm != NULL) {
1021 xmlFree(val);
1022 val = nvalnorm;
1023 }
1024
Owen Taylor3473f882001-02-23 17:55:21 +00001025 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1026 ctxt->myDoc, ctxt->node, ret, val);
1027 xmlFree(val);
1028 }
1029 } else {
1030 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1031 ctxt->node, ret, value);
1032 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00001033 } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1034 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001035 /*
1036 * when validating, the ID registration is done at the attribute
1037 * validation level. Otherwise we have to do specific handling here.
1038 */
1039 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1040 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1041 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1042 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1043 }
1044
Daniel Veillardde590ca2003-02-05 10:45:26 +00001045error:
Owen Taylor3473f882001-02-23 17:55:21 +00001046 if (nval != NULL)
1047 xmlFree(nval);
Owen Taylor3473f882001-02-23 17:55:21 +00001048 if (ns != NULL)
1049 xmlFree(ns);
1050}
1051
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001052/**
1053 * attribute:
1054 * @ctx: the user data (XML parser context)
1055 * @fullname: The attribute name, including namespace prefix
1056 * @value: The attribute value
1057 *
1058 * Handle an attribute that has been read by the parser.
1059 * The default handling is to convert the attribute into an
1060 * DOM subtree and past it in a new xmlAttr element added to
1061 * the element.
1062 */
1063void
1064attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1065{
1066 my_attribute(ctx, fullname, value, NULL);
1067}
1068
Daniel Veillard878eab02002-02-19 13:46:09 +00001069/*
1070 * xmlCheckDefaultedAttributes:
1071 *
1072 * Check defaulted attributes from the DTD
1073 */
1074static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001075xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001076 const xmlChar *prefix, const xmlChar **atts) {
1077 xmlElementPtr elemDecl;
1078 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001079 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001080 int i;
1081
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001082 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1083 if (elemDecl == NULL) {
1084 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1085 internal = 0;
1086 }
1087
1088process_external_subset:
1089
Daniel Veillard878eab02002-02-19 13:46:09 +00001090 if (elemDecl != NULL) {
1091 xmlAttributePtr attr = elemDecl->attributes;
1092 /*
1093 * Check against defaulted attributes from the external subset
1094 * if the document is stamped as standalone
1095 */
1096 if ((ctxt->myDoc->standalone == 1) &&
1097 (ctxt->myDoc->extSubset != NULL) &&
1098 (ctxt->validate)) {
1099 while (attr != NULL) {
1100 if ((attr->defaultValue != NULL) &&
1101 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1102 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001103 attr->prefix) == attr) &&
1104 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1105 attr->elem, attr->name,
1106 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001107 xmlChar *fulln;
1108
1109 if (attr->prefix != NULL) {
1110 fulln = xmlStrdup(attr->prefix);
1111 fulln = xmlStrcat(fulln, BAD_CAST ":");
1112 fulln = xmlStrcat(fulln, attr->name);
1113 } else {
1114 fulln = xmlStrdup(attr->name);
1115 }
1116
1117 /*
1118 * Check that the attribute is not declared in the
1119 * serialization
1120 */
1121 att = NULL;
1122 if (atts != NULL) {
1123 i = 0;
1124 att = atts[i];
1125 while (att != NULL) {
1126 if (xmlStrEqual(att, fulln))
1127 break;
1128 i += 2;
1129 att = atts[i];
1130 }
1131 }
1132 if (att == NULL) {
1133 if (ctxt->vctxt.error != NULL)
1134 ctxt->vctxt.error(ctxt->vctxt.userData,
1135 "standalone: attribute %s on %s defaulted from external subset\n",
1136 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001137 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001138 }
1139 }
1140 attr = attr->nexth;
1141 }
1142 }
1143
1144 /*
1145 * Actually insert defaulted values when needed
1146 */
1147 attr = elemDecl->attributes;
1148 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001149 /*
1150 * Make sure that attributes redefinition occuring in the
1151 * internal subset are not overriden by definitions in the
1152 * external subset.
1153 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001154 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001155 /*
1156 * the element should be instantiated in the tree if:
1157 * - this is a namespace prefix
1158 * - the user required for completion in the tree
1159 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001160 * - there isn't already an attribute definition
1161 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001162 */
1163 if (((attr->prefix != NULL) &&
1164 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1165 ((attr->prefix == NULL) &&
1166 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1167 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001168 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001169
Daniel Veillard8aff2472002-02-19 21:50:43 +00001170 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1171 attr->elem, attr->name,
1172 attr->prefix);
1173 if ((tst == attr) || (tst == NULL)) {
1174 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001175
Daniel Veillard8aff2472002-02-19 21:50:43 +00001176 if (attr->prefix != NULL) {
1177 fulln = xmlStrdup(attr->prefix);
1178 fulln = xmlStrcat(fulln, BAD_CAST ":");
1179 fulln = xmlStrcat(fulln, attr->name);
1180 } else {
1181 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001182 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001183
1184 /*
1185 * Check that the attribute is not declared in the
1186 * serialization
1187 */
1188 att = NULL;
1189 if (atts != NULL) {
1190 i = 0;
1191 att = atts[i];
1192 while (att != NULL) {
1193 if (xmlStrEqual(att, fulln))
1194 break;
1195 i += 2;
1196 att = atts[i];
1197 }
1198 }
1199 if (att == NULL) {
1200 attribute(ctxt, fulln, attr->defaultValue);
1201 }
1202 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001203 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001204 }
1205 }
1206 attr = attr->nexth;
1207 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001208 if (internal == 1) {
1209 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1210 name, prefix);
1211 internal = 0;
1212 goto process_external_subset;
1213 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001214 }
1215}
1216
Owen Taylor3473f882001-02-23 17:55:21 +00001217/**
1218 * startElement:
1219 * @ctx: the user data (XML parser context)
1220 * @fullname: The element name, including namespace prefix
1221 * @atts: An array of name/value attributes pairs, NULL terminated
1222 *
1223 * called when an opening tag has been processed.
1224 */
1225void
1226startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1227{
1228 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1229 xmlNodePtr ret;
1230 xmlNodePtr parent = ctxt->node;
1231 xmlNsPtr ns;
1232 xmlChar *name;
1233 xmlChar *prefix;
1234 const xmlChar *att;
1235 const xmlChar *value;
1236 int i;
1237
1238#ifdef DEBUG_SAX
1239 xmlGenericError(xmlGenericErrorContext,
1240 "SAX.startElement(%s)\n", fullname);
1241#endif
1242
1243 /*
1244 * First check on validity:
1245 */
1246 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1247 ((ctxt->myDoc->intSubset == NULL) ||
1248 ((ctxt->myDoc->intSubset->notations == NULL) &&
1249 (ctxt->myDoc->intSubset->elements == NULL) &&
1250 (ctxt->myDoc->intSubset->attributes == NULL) &&
1251 (ctxt->myDoc->intSubset->entities == NULL)))) {
1252 if (ctxt->vctxt.error != NULL) {
1253 ctxt->vctxt.error(ctxt->vctxt.userData,
1254 "Validation failed: no DTD found !\n");
1255 }
1256 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001257 ctxt->valid = 0;
1258 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001259 }
1260
1261
1262 /*
1263 * Split the full name into a namespace prefix and the tag name
1264 */
1265 name = xmlSplitQName(ctxt, fullname, &prefix);
1266
1267
1268 /*
1269 * Note : the namespace resolution is deferred until the end of the
1270 * attributes parsing, since local namespace can be defined as
1271 * an attribute at this level.
1272 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001273 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001274 if (ret == NULL) return;
1275 if (ctxt->myDoc->children == NULL) {
1276#ifdef DEBUG_SAX_TREE
1277 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1278#endif
1279 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1280 } else if (parent == NULL) {
1281 parent = ctxt->myDoc->children;
1282 }
1283 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001284 if (ctxt->linenumbers) {
1285 if (ctxt->input != NULL)
1286 ret->content = (void *) (long) ctxt->input->line;
1287 }
Owen Taylor3473f882001-02-23 17:55:21 +00001288
1289 /*
1290 * We are parsing a new node.
1291 */
1292#ifdef DEBUG_SAX_TREE
1293 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1294#endif
1295 nodePush(ctxt, ret);
1296
1297 /*
1298 * Link the child element
1299 */
1300 if (parent != NULL) {
1301 if (parent->type == XML_ELEMENT_NODE) {
1302#ifdef DEBUG_SAX_TREE
1303 xmlGenericError(xmlGenericErrorContext,
1304 "adding child %s to %s\n", name, parent->name);
1305#endif
1306 xmlAddChild(parent, ret);
1307 } else {
1308#ifdef DEBUG_SAX_TREE
1309 xmlGenericError(xmlGenericErrorContext,
1310 "adding sibling %s to ", name);
1311 xmlDebugDumpOneNode(stderr, parent, 0);
1312#endif
1313 xmlAddSibling(parent, ret);
1314 }
1315 }
1316
1317 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001318 * Insert all the defaulted attributes from the DTD especially namespaces
1319 */
1320 if ((!ctxt->html) &&
1321 ((ctxt->myDoc->intSubset != NULL) ||
1322 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001323 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001324 }
1325
1326 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001327 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001328 */
1329 if (atts != NULL) {
1330 i = 0;
1331 att = atts[i++];
1332 value = atts[i++];
1333 if (!ctxt->html) {
1334 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001335 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1336 (att[3] == 'n') && (att[4] == 's'))
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001337 my_attribute(ctxt, att, value, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001338
1339 att = atts[i++];
1340 value = atts[i++];
1341 }
1342 }
1343 }
1344
1345 /*
1346 * Search the namespace, note that since the attributes have been
1347 * processed, the local namespaces are available.
1348 */
1349 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1350 if ((ns == NULL) && (parent != NULL))
1351 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1352 if ((prefix != NULL) && (ns == NULL)) {
1353 ns = xmlNewNs(ret, NULL, prefix);
1354 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1355 ctxt->sax->warning(ctxt->userData,
1356 "Namespace prefix %s is not defined\n", prefix);
1357 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001358
1359 /*
1360 * set the namespace node, making sure that if the default namspace
1361 * is unbound on a parent we simply kee it NULL
1362 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001363 if ((ns != NULL) && (ns->href != NULL) &&
1364 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001365 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001366
1367 /*
1368 * process all the other attributes
1369 */
1370 if (atts != NULL) {
1371 i = 0;
1372 att = atts[i++];
1373 value = atts[i++];
1374 if (ctxt->html) {
1375 while (att != NULL) {
1376 attribute(ctxt, att, value);
1377 att = atts[i++];
1378 value = atts[i++];
1379 }
1380 } else {
1381 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001382 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1383 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001384 attribute(ctxt, att, value);
1385
1386 /*
1387 * Next ones
1388 */
1389 att = atts[i++];
1390 value = atts[i++];
1391 }
1392 }
1393 }
1394
1395 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001396 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001397 * check the document root element for validity
1398 */
1399 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001400 int chk;
1401
1402 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1403 if (chk <= 0)
1404 ctxt->valid = 0;
1405 if (chk < 0)
1406 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001407 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1408 ctxt->vctxt.finishDtd = 1;
1409 }
1410
1411 if (prefix != NULL)
1412 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001413
1414}
1415
1416/**
1417 * endElement:
1418 * @ctx: the user data (XML parser context)
1419 * @name: The element name
1420 *
1421 * called when the end of an element has been detected.
1422 */
1423void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001424endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001425{
1426 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1427 xmlParserNodeInfo node_info;
1428 xmlNodePtr cur = ctxt->node;
1429
1430#ifdef DEBUG_SAX
1431 if (name == NULL)
1432 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1433 else
1434 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1435#endif
1436
1437 /* Capture end position and add node */
1438 if (cur != NULL && ctxt->record_info) {
1439 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1440 node_info.end_line = ctxt->input->line;
1441 node_info.node = cur;
1442 xmlParserAddNodeInfo(ctxt, &node_info);
1443 }
1444 ctxt->nodemem = -1;
1445
1446 if (ctxt->validate && ctxt->wellFormed &&
1447 ctxt->myDoc && ctxt->myDoc->intSubset)
1448 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1449 cur);
1450
1451
1452 /*
1453 * end of parsing of this node.
1454 */
1455#ifdef DEBUG_SAX_TREE
1456 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1457#endif
1458 nodePop(ctxt);
1459}
1460
1461/**
1462 * reference:
1463 * @ctx: the user data (XML parser context)
1464 * @name: The entity name
1465 *
1466 * called when an entity reference is detected.
1467 */
1468void
1469reference(void *ctx, const xmlChar *name)
1470{
1471 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1472 xmlNodePtr ret;
1473
1474#ifdef DEBUG_SAX
1475 xmlGenericError(xmlGenericErrorContext,
1476 "SAX.reference(%s)\n", name);
1477#endif
1478 if (name[0] == '#')
1479 ret = xmlNewCharRef(ctxt->myDoc, name);
1480 else
1481 ret = xmlNewReference(ctxt->myDoc, name);
1482#ifdef DEBUG_SAX_TREE
1483 xmlGenericError(xmlGenericErrorContext,
1484 "add reference %s to %s \n", name, ctxt->node->name);
1485#endif
1486 xmlAddChild(ctxt->node, ret);
1487}
1488
1489/**
1490 * characters:
1491 * @ctx: the user data (XML parser context)
1492 * @ch: a xmlChar string
1493 * @len: the number of xmlChar
1494 *
1495 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001496 */
1497void
1498characters(void *ctx, const xmlChar *ch, int len)
1499{
1500 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1501 xmlNodePtr lastChild;
1502
1503#ifdef DEBUG_SAX
1504 xmlGenericError(xmlGenericErrorContext,
1505 "SAX.characters(%.30s, %d)\n", ch, len);
1506#endif
1507 /*
1508 * Handle the data if any. If there is no child
1509 * add it as content, otherwise if the last child is text,
1510 * concatenate it, else create a new node of type text.
1511 */
1512
1513 if (ctxt->node == NULL) {
1514#ifdef DEBUG_SAX_TREE
1515 xmlGenericError(xmlGenericErrorContext,
1516 "add chars: ctxt->node == NULL !\n");
1517#endif
1518 return;
1519 }
1520 lastChild = xmlGetLastChild(ctxt->node);
1521#ifdef DEBUG_SAX_TREE
1522 xmlGenericError(xmlGenericErrorContext,
1523 "add chars to %s \n", ctxt->node->name);
1524#endif
1525
1526 /*
1527 * Here we needed an accelerator mechanism in case of very large
1528 * elements. Use an attribute in the structure !!!
1529 */
1530 if (lastChild == NULL) {
1531 /* first node, first time */
1532 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001533 if (ctxt->node->children != NULL) {
1534 ctxt->nodelen = len;
1535 ctxt->nodemem = len + 1;
1536 }
Owen Taylor3473f882001-02-23 17:55:21 +00001537 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001538 int coalesceText = (lastChild != NULL) &&
1539 (lastChild->type == XML_TEXT_NODE) &&
1540 (lastChild->name == xmlStringText);
1541 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001542 /*
1543 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001544 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001545 * reallocate a new buffer, move data, append ch. Here
1546 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001547 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001548 */
1549 if (ctxt->nodelen + len >= ctxt->nodemem) {
1550 xmlChar *newbuf;
1551 int size;
1552
1553 size = ctxt->nodemem + len;
1554 size *= 2;
1555 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1556 if (newbuf == NULL) {
1557 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1558 ctxt->sax->error(ctxt->userData,
1559 "SAX.characters(): out of memory\n");
1560 return;
1561 }
1562 ctxt->nodemem = size;
1563 lastChild->content = newbuf;
1564 }
1565 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1566 ctxt->nodelen += len;
1567 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001568 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001569 xmlTextConcat(lastChild, ch, len);
1570 if (ctxt->node->children != NULL) {
1571 ctxt->nodelen = xmlStrlen(lastChild->content);
1572 ctxt->nodemem = ctxt->nodelen + 1;
1573 }
Owen Taylor3473f882001-02-23 17:55:21 +00001574 } else {
1575 /* Mixed content, first time */
1576 lastChild = xmlNewTextLen(ch, len);
1577 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001578 if (ctxt->node->children != NULL) {
1579 ctxt->nodelen = len;
1580 ctxt->nodemem = len + 1;
1581 }
Owen Taylor3473f882001-02-23 17:55:21 +00001582 }
1583 }
1584}
1585
1586/**
1587 * ignorableWhitespace:
1588 * @ctx: the user data (XML parser context)
1589 * @ch: a xmlChar string
1590 * @len: the number of xmlChar
1591 *
1592 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001593 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001594 */
1595void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001596ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001597{
1598 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1599#ifdef DEBUG_SAX
1600 xmlGenericError(xmlGenericErrorContext,
1601 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1602#endif
1603}
1604
1605/**
1606 * processingInstruction:
1607 * @ctx: the user data (XML parser context)
1608 * @target: the target name
1609 * @data: the PI data's
1610 *
1611 * A processing instruction has been parsed.
1612 */
1613void
1614processingInstruction(void *ctx, const xmlChar *target,
1615 const xmlChar *data)
1616{
1617 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1618 xmlNodePtr ret;
1619 xmlNodePtr parent = ctxt->node;
1620
1621#ifdef DEBUG_SAX
1622 xmlGenericError(xmlGenericErrorContext,
1623 "SAX.processingInstruction(%s, %s)\n", target, data);
1624#endif
1625
1626 ret = xmlNewPI(target, data);
1627 if (ret == NULL) return;
1628 parent = ctxt->node;
1629
1630 if (ctxt->inSubset == 1) {
1631 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1632 return;
1633 } else if (ctxt->inSubset == 2) {
1634 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1635 return;
1636 }
1637 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1638#ifdef DEBUG_SAX_TREE
1639 xmlGenericError(xmlGenericErrorContext,
1640 "Setting PI %s as root\n", target);
1641#endif
1642 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1643 return;
1644 }
1645 if (parent->type == XML_ELEMENT_NODE) {
1646#ifdef DEBUG_SAX_TREE
1647 xmlGenericError(xmlGenericErrorContext,
1648 "adding PI %s child to %s\n", target, parent->name);
1649#endif
1650 xmlAddChild(parent, ret);
1651 } else {
1652#ifdef DEBUG_SAX_TREE
1653 xmlGenericError(xmlGenericErrorContext,
1654 "adding PI %s sibling to ", target);
1655 xmlDebugDumpOneNode(stderr, parent, 0);
1656#endif
1657 xmlAddSibling(parent, ret);
1658 }
1659}
1660
1661/**
1662 * globalNamespace:
1663 * @ctx: the user data (XML parser context)
1664 * @href: the namespace associated URN
1665 * @prefix: the namespace prefix
1666 *
1667 * An old global namespace has been parsed.
1668 */
1669void
1670globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1671{
1672 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1673#ifdef DEBUG_SAX
1674 xmlGenericError(xmlGenericErrorContext,
1675 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1676#endif
1677 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1678}
1679
1680/**
1681 * setNamespace:
1682 * @ctx: the user data (XML parser context)
1683 * @name: the namespace prefix
1684 *
1685 * Set the current element namespace.
1686 */
1687
1688void
1689setNamespace(void *ctx, const xmlChar *name)
1690{
1691 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1692 xmlNsPtr ns;
1693 xmlNodePtr parent;
1694
1695#ifdef DEBUG_SAX
1696 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1697#endif
1698 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1699 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1700 if (ctxt->nodeNr >= 2) {
1701 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1702 if (parent != NULL)
1703 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1704 }
1705 }
1706 xmlSetNs(ctxt->node, ns);
1707}
1708
1709/**
1710 * getNamespace:
1711 * @ctx: the user data (XML parser context)
1712 *
1713 * Get the current element namespace.
1714 *
1715 * Returns the xmlNsPtr or NULL if none
1716 */
1717
1718xmlNsPtr
1719getNamespace(void *ctx)
1720{
1721 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1722 xmlNsPtr ret;
1723
1724#ifdef DEBUG_SAX
1725 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1726#endif
1727 ret = ctxt->node->ns;
1728 return(ret);
1729}
1730
1731/**
1732 * checkNamespace:
1733 * @ctx: the user data (XML parser context)
1734 * @namespace: the namespace to check against
1735 *
1736 * Check that the current element namespace is the same as the
1737 * one read upon parsing.
1738 *
1739 * Returns 1 if true 0 otherwise
1740 */
1741
1742int
1743checkNamespace(void *ctx, xmlChar *namespace)
1744{
1745 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1746 xmlNodePtr cur = ctxt->node;
1747
1748#ifdef DEBUG_SAX
1749 xmlGenericError(xmlGenericErrorContext,
1750 "SAX.checkNamespace(%s)\n", namespace);
1751#endif
1752
1753 /*
1754 * Check that the Name in the ETag is the same as in the STag.
1755 */
1756 if (namespace == NULL) {
1757 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1758 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1759 ctxt->sax->error(ctxt,
1760 "End tags for %s don't hold the namespace %s\n",
1761 cur->name, cur->ns->prefix);
1762 ctxt->wellFormed = 0;
1763 }
1764 } else {
1765 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1766 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1767 ctxt->sax->error(ctxt,
1768 "End tags %s holds a prefix %s not used by the open tag\n",
1769 cur->name, namespace);
1770 ctxt->wellFormed = 0;
1771 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1772 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1773 ctxt->sax->error(ctxt,
1774 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1775 cur->name, cur->ns->prefix, namespace);
1776 ctxt->wellFormed = 0;
1777 } else
1778 return(1);
1779 }
1780 return(0);
1781}
1782
1783/**
1784 * namespaceDecl:
1785 * @ctx: the user data (XML parser context)
1786 * @href: the namespace associated URN
1787 * @prefix: the namespace prefix
1788 *
1789 * A namespace has been parsed.
1790 */
1791void
1792namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1793{
1794 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1795#ifdef DEBUG_SAX
1796 if (prefix == NULL)
1797 xmlGenericError(xmlGenericErrorContext,
1798 "SAX.namespaceDecl(%s, NULL)\n", href);
1799 else
1800 xmlGenericError(xmlGenericErrorContext,
1801 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1802#endif
1803 xmlNewNs(ctxt->node, href, prefix);
1804}
1805
1806/**
1807 * comment:
1808 * @ctx: the user data (XML parser context)
1809 * @value: the comment content
1810 *
1811 * A comment has been parsed.
1812 */
1813void
1814comment(void *ctx, const xmlChar *value)
1815{
1816 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1817 xmlNodePtr ret;
1818 xmlNodePtr parent = ctxt->node;
1819
1820#ifdef DEBUG_SAX
1821 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1822#endif
1823 ret = xmlNewDocComment(ctxt->myDoc, value);
1824 if (ret == NULL) return;
1825
1826 if (ctxt->inSubset == 1) {
1827 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1828 return;
1829 } else if (ctxt->inSubset == 2) {
1830 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1831 return;
1832 }
1833 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1834#ifdef DEBUG_SAX_TREE
1835 xmlGenericError(xmlGenericErrorContext,
1836 "Setting comment as root\n");
1837#endif
1838 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1839 return;
1840 }
1841 if (parent->type == XML_ELEMENT_NODE) {
1842#ifdef DEBUG_SAX_TREE
1843 xmlGenericError(xmlGenericErrorContext,
1844 "adding comment child to %s\n", parent->name);
1845#endif
1846 xmlAddChild(parent, ret);
1847 } else {
1848#ifdef DEBUG_SAX_TREE
1849 xmlGenericError(xmlGenericErrorContext,
1850 "adding comment sibling to ");
1851 xmlDebugDumpOneNode(stderr, parent, 0);
1852#endif
1853 xmlAddSibling(parent, ret);
1854 }
1855}
1856
1857/**
1858 * cdataBlock:
1859 * @ctx: the user data (XML parser context)
1860 * @value: The pcdata content
1861 * @len: the block length
1862 *
1863 * called when a pcdata block has been parsed
1864 */
1865void
1866cdataBlock(void *ctx, const xmlChar *value, int len)
1867{
1868 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1869 xmlNodePtr ret, lastChild;
1870
1871#ifdef DEBUG_SAX
1872 xmlGenericError(xmlGenericErrorContext,
1873 "SAX.pcdata(%.10s, %d)\n", value, len);
1874#endif
1875 lastChild = xmlGetLastChild(ctxt->node);
1876#ifdef DEBUG_SAX_TREE
1877 xmlGenericError(xmlGenericErrorContext,
1878 "add chars to %s \n", ctxt->node->name);
1879#endif
1880 if ((lastChild != NULL) &&
1881 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1882 xmlTextConcat(lastChild, value, len);
1883 } else {
1884 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1885 xmlAddChild(ctxt->node, ret);
1886 }
1887}
1888
Daniel Veillardd0463562001-10-13 09:15:48 +00001889/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001890 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001891 * @hdlr: the SAX handler
1892 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001893 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001894 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001895 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001896void
1897initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1898{
1899 if(hdlr->initialized == 1)
1900 return;
1901
1902 hdlr->internalSubset = internalSubset;
1903 hdlr->externalSubset = externalSubset;
1904 hdlr->isStandalone = isStandalone;
1905 hdlr->hasInternalSubset = hasInternalSubset;
1906 hdlr->hasExternalSubset = hasExternalSubset;
1907 hdlr->resolveEntity = resolveEntity;
1908 hdlr->getEntity = getEntity;
1909 hdlr->getParameterEntity = getParameterEntity;
1910 hdlr->entityDecl = entityDecl;
1911 hdlr->attributeDecl = attributeDecl;
1912 hdlr->elementDecl = elementDecl;
1913 hdlr->notationDecl = notationDecl;
1914 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1915 hdlr->setDocumentLocator = setDocumentLocator;
1916 hdlr->startDocument = startDocument;
1917 hdlr->endDocument = endDocument;
1918 hdlr->startElement = startElement;
1919 hdlr->endElement = endElement;
1920 hdlr->reference = reference;
1921 hdlr->characters = characters;
1922 hdlr->cdataBlock = cdataBlock;
1923 hdlr->ignorableWhitespace = characters;
1924 hdlr->processingInstruction = processingInstruction;
1925 hdlr->comment = comment;
1926 /* if (xmlGetWarningsDefaultValue == 0) */
1927 if (warning == 0)
1928 hdlr->warning = NULL;
1929 else
1930 hdlr->warning = xmlParserWarning;
1931 hdlr->error = xmlParserError;
1932 hdlr->fatalError = xmlParserError;
1933
1934 hdlr->initialized = 1;
1935}
Owen Taylor3473f882001-02-23 17:55:21 +00001936
1937/**
1938 * xmlDefaultSAXHandlerInit:
1939 *
1940 * Initialize the default SAX handler
1941 */
1942void
1943xmlDefaultSAXHandlerInit(void)
1944{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001945 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001946}
1947
Daniel Veillardeae522a2001-04-23 13:41:34 +00001948#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001949
1950/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001951 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001952 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001953 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001954 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001955 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001956void
1957inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1958{
1959 if(hdlr->initialized == 1)
1960 return;
1961
1962 hdlr->internalSubset = internalSubset;
1963 hdlr->externalSubset = NULL;
1964 hdlr->isStandalone = NULL;
1965 hdlr->hasInternalSubset = NULL;
1966 hdlr->hasExternalSubset = NULL;
1967 hdlr->resolveEntity = NULL;
1968 hdlr->getEntity = getEntity;
1969 hdlr->getParameterEntity = NULL;
1970 hdlr->entityDecl = NULL;
1971 hdlr->attributeDecl = NULL;
1972 hdlr->elementDecl = NULL;
1973 hdlr->notationDecl = NULL;
1974 hdlr->unparsedEntityDecl = NULL;
1975 hdlr->setDocumentLocator = setDocumentLocator;
1976 hdlr->startDocument = startDocument;
1977 hdlr->endDocument = endDocument;
1978 hdlr->startElement = startElement;
1979 hdlr->endElement = endElement;
1980 hdlr->reference = NULL;
1981 hdlr->characters = characters;
1982 hdlr->cdataBlock = cdataBlock;
1983 hdlr->ignorableWhitespace = ignorableWhitespace;
1984 hdlr->processingInstruction = NULL;
1985 hdlr->comment = comment;
1986 hdlr->warning = xmlParserWarning;
1987 hdlr->error = xmlParserError;
1988 hdlr->fatalError = xmlParserError;
1989
1990 hdlr->initialized = 1;
1991}
Owen Taylor3473f882001-02-23 17:55:21 +00001992
1993/**
1994 * htmlDefaultSAXHandlerInit:
1995 *
1996 * Initialize the default SAX handler
1997 */
1998void
1999htmlDefaultSAXHandlerInit(void)
2000{
Daniel Veillardd0463562001-10-13 09:15:48 +00002001 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002002}
Daniel Veillardd0463562001-10-13 09:15:48 +00002003
Daniel Veillardeae522a2001-04-23 13:41:34 +00002004#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002005
Daniel Veillardeae522a2001-04-23 13:41:34 +00002006#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00002007
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002008/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00002009 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002010 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002011 *
2012 * Initialize the default DocBook SAX handler
2013 */
Daniel Veillardd0463562001-10-13 09:15:48 +00002014void
2015initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2016{
2017 if(hdlr->initialized == 1)
2018 return;
2019
2020 hdlr->internalSubset = internalSubset;
2021 hdlr->externalSubset = NULL;
2022 hdlr->isStandalone = isStandalone;
2023 hdlr->hasInternalSubset = hasInternalSubset;
2024 hdlr->hasExternalSubset = hasExternalSubset;
2025 hdlr->resolveEntity = resolveEntity;
2026 hdlr->getEntity = getEntity;
2027 hdlr->getParameterEntity = NULL;
2028 hdlr->entityDecl = entityDecl;
2029 hdlr->attributeDecl = NULL;
2030 hdlr->elementDecl = NULL;
2031 hdlr->notationDecl = NULL;
2032 hdlr->unparsedEntityDecl = NULL;
2033 hdlr->setDocumentLocator = setDocumentLocator;
2034 hdlr->startDocument = startDocument;
2035 hdlr->endDocument = endDocument;
2036 hdlr->startElement = startElement;
2037 hdlr->endElement = endElement;
2038 hdlr->reference = reference;
2039 hdlr->characters = characters;
2040 hdlr->cdataBlock = NULL;
2041 hdlr->ignorableWhitespace = ignorableWhitespace;
2042 hdlr->processingInstruction = NULL;
2043 hdlr->comment = comment;
2044 hdlr->warning = xmlParserWarning;
2045 hdlr->error = xmlParserError;
2046 hdlr->fatalError = xmlParserError;
2047
2048 hdlr->initialized = 1;
2049}
Owen Taylor3473f882001-02-23 17:55:21 +00002050
2051/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00002052 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00002053 *
2054 * Initialize the default SAX handler
2055 */
2056void
Daniel Veillardeae522a2001-04-23 13:41:34 +00002057docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00002058{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002059 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002060}
Daniel Veillardeae522a2001-04-23 13:41:34 +00002061
2062#endif /* LIBXML_DOCB_ENABLED */