blob: 6e3cba1fb1e7dcfd1e447b94482a6df1dfa16210 [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)) {
Daniel Veillard39eb88b2003-03-11 11:21:28 +0000381 int val;
382
Owen Taylor3473f882001-02-23 17:55:21 +0000383 /*
384 * for validation purposes we really need to fetch and
385 * parse the external entity
386 */
Owen Taylor3473f882001-02-23 17:55:21 +0000387 xmlNodePtr children;
388
Daniel Veillard39eb88b2003-03-11 11:21:28 +0000389 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
390 ret->ExternalID, &children);
391 if (val == 0) {
392 xmlAddChildList((xmlNodePtr) ret, children);
393 } else {
394 ctxt->sax->error(ctxt,
395 "Failure to process entity %s\n", name);
396 ctxt->wellFormed = 0;
397 ctxt->valid = 0;
398 ctxt->validate = 0;
399 return(NULL);
400 }
Daniel Veillard8bf70b92003-01-07 23:14:24 +0000401 ret->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000402 }
403 return(ret);
404}
405
406/**
407 * getParameterEntity:
408 * @ctx: the user data (XML parser context)
409 * @name: The entity name
410 *
411 * Get a parameter entity by name
412 *
413 * Returns the xmlEntityPtr if found.
414 */
415xmlEntityPtr
416getParameterEntity(void *ctx, const xmlChar *name)
417{
418 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
419 xmlEntityPtr ret;
420
421#ifdef DEBUG_SAX
422 xmlGenericError(xmlGenericErrorContext,
423 "SAX.getParameterEntity(%s)\n", name);
424#endif
425
426 ret = xmlGetParameterEntity(ctxt->myDoc, name);
427 return(ret);
428}
429
430
431/**
432 * entityDecl:
433 * @ctx: the user data (XML parser context)
434 * @name: the entity name
435 * @type: the entity type
436 * @publicId: The public ID of the entity
437 * @systemId: The system ID of the entity
438 * @content: the entity value (without processing).
439 *
440 * An entity definition has been parsed
441 */
442void
443entityDecl(void *ctx, const xmlChar *name, int type,
444 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
445{
446 xmlEntityPtr ent;
447 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
448
449#ifdef DEBUG_SAX
450 xmlGenericError(xmlGenericErrorContext,
451 "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
452 name, type, publicId, systemId, content);
453#endif
454 if (ctxt->inSubset == 1) {
455 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
456 systemId, content);
457 if ((ent == NULL) && (ctxt->pedantic) &&
458 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
459 ctxt->sax->warning(ctxt,
460 "Entity(%s) already defined in the internal subset\n", name);
461 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
462 xmlChar *URI;
463 const char *base = NULL;
464
465 if (ctxt->input != NULL)
466 base = ctxt->input->filename;
467 if (base == NULL)
468 base = ctxt->directory;
469
470 URI = xmlBuildURI(systemId, (const xmlChar *) base);
471 ent->URI = URI;
472 }
473 } else if (ctxt->inSubset == 2) {
474 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
475 systemId, content);
476 if ((ent == NULL) && (ctxt->pedantic) &&
477 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
478 ctxt->sax->warning(ctxt,
479 "Entity(%s) already defined in the external subset\n", name);
480 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
481 xmlChar *URI;
482 const char *base = NULL;
483
484 if (ctxt->input != NULL)
485 base = ctxt->input->filename;
486 if (base == NULL)
487 base = ctxt->directory;
488
489 URI = xmlBuildURI(systemId, (const xmlChar *) base);
490 ent->URI = URI;
491 }
492 } else {
493 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
494 ctxt->sax->error(ctxt,
495 "SAX.entityDecl(%s) called while not in subset\n", name);
496 }
497}
498
499/**
500 * attributeDecl:
501 * @ctx: the user data (XML parser context)
502 * @elem: the name of the element
503 * @fullname: the attribute name
504 * @type: the attribute type
505 * @def: the type of default value
506 * @defaultValue: the attribute default value
507 * @tree: the tree of enumerated value set
508 *
509 * An attribute definition has been parsed
510 */
511void
512attributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
513 int type, int def, const xmlChar *defaultValue,
514 xmlEnumerationPtr tree)
515{
516 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
517 xmlAttributePtr attr;
518 xmlChar *name = NULL, *prefix = NULL;
519
520#ifdef DEBUG_SAX
521 xmlGenericError(xmlGenericErrorContext,
522 "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
523 elem, fullname, type, def, defaultValue);
524#endif
525 name = xmlSplitQName(ctxt, fullname, &prefix);
Daniel Veillardc7612992002-02-17 22:47:37 +0000526 ctxt->vctxt.valid = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000527 if (ctxt->inSubset == 1)
528 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
529 name, prefix, (xmlAttributeType) type,
530 (xmlAttributeDefault) def, defaultValue, tree);
531 else if (ctxt->inSubset == 2)
532 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
533 name, prefix, (xmlAttributeType) type,
534 (xmlAttributeDefault) def, defaultValue, tree);
535 else {
536 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
537 ctxt->sax->error(ctxt,
538 "SAX.attributeDecl(%s) called while not in subset\n", name);
539 return;
540 }
Daniel Veillardc7612992002-02-17 22:47:37 +0000541 if (ctxt->vctxt.valid == 0)
542 ctxt->valid = 0;
Daniel Veillardd85f4f42002-03-25 10:48:46 +0000543 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
544 (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000545 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
546 attr);
547 if (prefix != NULL)
548 xmlFree(prefix);
549 if (name != NULL)
550 xmlFree(name);
551}
552
553/**
554 * elementDecl:
555 * @ctx: the user data (XML parser context)
556 * @name: the element name
557 * @type: the element type
558 * @content: the element value tree
559 *
560 * An element definition has been parsed
561 */
562void
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000563elementDecl(void *ctx, const xmlChar * name, int type,
564 xmlElementContentPtr content)
Owen Taylor3473f882001-02-23 17:55:21 +0000565{
566 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
567 xmlElementPtr elem = NULL;
568
569#ifdef DEBUG_SAX
570 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000571 "SAX.elementDecl(%s, %d, ...)\n", name, type);
Owen Taylor3473f882001-02-23 17:55:21 +0000572#endif
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000573
Owen Taylor3473f882001-02-23 17:55:21 +0000574 if (ctxt->inSubset == 1)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000575 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
576 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000577 else if (ctxt->inSubset == 2)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000578 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
579 name, (xmlElementTypeVal) type, content);
Owen Taylor3473f882001-02-23 17:55:21 +0000580 else {
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000581 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
582 ctxt->sax->error(ctxt,
583 "SAX.elementDecl(%s) called while not in subset\n",
584 name);
585 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000586 }
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000587 if (elem == NULL)
588 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000589 if (ctxt->validate && ctxt->wellFormed &&
590 ctxt->myDoc && ctxt->myDoc->intSubset)
Daniel Veillard1fd36d22001-07-04 22:54:28 +0000591 ctxt->valid &=
592 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Owen Taylor3473f882001-02-23 17:55:21 +0000593}
594
595/**
596 * notationDecl:
597 * @ctx: the user data (XML parser context)
598 * @name: The name of the notation
599 * @publicId: The public ID of the entity
600 * @systemId: The system ID of the entity
601 *
602 * What to do when a notation declaration has been parsed.
603 */
604void
605notationDecl(void *ctx, const xmlChar *name,
606 const xmlChar *publicId, const xmlChar *systemId)
607{
608 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
609 xmlNotationPtr nota = NULL;
610
611#ifdef DEBUG_SAX
612 xmlGenericError(xmlGenericErrorContext,
613 "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
614#endif
615
Daniel Veillard7aea52d2002-02-17 23:07:47 +0000616 if ((publicId == NULL) && (systemId == NULL)) {
617 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
618 ctxt->sax->error(ctxt,
619 "SAX.notationDecl(%s) externalID or PublicID missing\n", name);
620 ctxt->valid = 0;
621 ctxt->wellFormed = 0;
622 return;
623 } else if (ctxt->inSubset == 1)
Owen Taylor3473f882001-02-23 17:55:21 +0000624 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
625 publicId, systemId);
626 else if (ctxt->inSubset == 2)
Daniel Veillard25239c12001-03-14 13:56:48 +0000627 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
Owen Taylor3473f882001-02-23 17:55:21 +0000628 publicId, systemId);
629 else {
630 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
631 ctxt->sax->error(ctxt,
632 "SAX.notationDecl(%s) called while not in subset\n", name);
633 return;
634 }
635 if (nota == NULL) ctxt->valid = 0;
636 if (ctxt->validate && ctxt->wellFormed &&
637 ctxt->myDoc && ctxt->myDoc->intSubset)
638 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
639 nota);
640}
641
642/**
643 * unparsedEntityDecl:
644 * @ctx: the user data (XML parser context)
645 * @name: The name of the entity
646 * @publicId: The public ID of the entity
647 * @systemId: The system ID of the entity
648 * @notationName: the name of the notation
649 *
650 * What to do when an unparsed entity declaration is parsed
651 */
652void
653unparsedEntityDecl(void *ctx, const xmlChar *name,
654 const xmlChar *publicId, const xmlChar *systemId,
655 const xmlChar *notationName)
656{
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000657 xmlEntityPtr ent;
Owen Taylor3473f882001-02-23 17:55:21 +0000658 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
659#ifdef DEBUG_SAX
660 xmlGenericError(xmlGenericErrorContext,
661 "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
662 name, publicId, systemId, notationName);
663#endif
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000664#if 0
665 Done in xmlValidateDtdFinal now.
Daniel Veillard28757702002-02-18 11:19:30 +0000666 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc) {
667 int ret;
668 ret = xmlValidateNotationUse(&ctxt->vctxt, ctxt->myDoc,
Owen Taylor3473f882001-02-23 17:55:21 +0000669 notationName);
Daniel Veillard28757702002-02-18 11:19:30 +0000670 if (ret == 0) {
671 ctxt->wellFormed = 0;
672 ctxt->valid = 0;
673 }
674 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000675#endif
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000676 if (ctxt->inSubset == 1) {
677 ent = xmlAddDocEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000678 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
679 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000680 if ((ent == NULL) && (ctxt->pedantic) &&
681 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
682 ctxt->sax->warning(ctxt,
683 "Entity(%s) already defined in the internal subset\n", name);
684 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
685 xmlChar *URI;
686 const char *base = NULL;
687
688 if (ctxt->input != NULL)
689 base = ctxt->input->filename;
690 if (base == NULL)
691 base = ctxt->directory;
692
693 URI = xmlBuildURI(systemId, (const xmlChar *) base);
694 ent->URI = URI;
695 }
696 } else if (ctxt->inSubset == 2) {
697 ent = xmlAddDtdEntity(ctxt->myDoc, name,
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000698 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
699 publicId, systemId, notationName);
Daniel Veillard9f4eb912001-08-01 21:22:27 +0000700 if ((ent == NULL) && (ctxt->pedantic) &&
701 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
702 ctxt->sax->warning(ctxt,
703 "Entity(%s) already defined in the external subset\n", name);
704 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
705 xmlChar *URI;
706 const char *base = NULL;
707
708 if (ctxt->input != NULL)
709 base = ctxt->input->filename;
710 if (base == NULL)
711 base = ctxt->directory;
712
713 URI = xmlBuildURI(systemId, (const xmlChar *) base);
714 ent->URI = URI;
715 }
716 } else {
Daniel Veillarde020c3a2001-03-21 18:06:15 +0000717 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
718 ctxt->sax->error(ctxt,
719 "SAX.unparsedEntityDecl(%s) called while not in subset\n", name);
720 }
Owen Taylor3473f882001-02-23 17:55:21 +0000721}
722
723/**
724 * setDocumentLocator:
725 * @ctx: the user data (XML parser context)
726 * @loc: A SAX Locator
727 *
728 * Receive the document locator at startup, actually xmlDefaultSAXLocator
729 * Everything is available on the context, so this is useless in our case.
730 */
731void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000732setDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +0000733{
734 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
735#ifdef DEBUG_SAX
736 xmlGenericError(xmlGenericErrorContext,
737 "SAX.setDocumentLocator()\n");
738#endif
739}
740
741/**
742 * startDocument:
743 * @ctx: the user data (XML parser context)
744 *
745 * called when the document start being processed.
746 */
747void
748startDocument(void *ctx)
749{
750 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
751 xmlDocPtr doc;
752
753#ifdef DEBUG_SAX
754 xmlGenericError(xmlGenericErrorContext,
755 "SAX.startDocument()\n");
756#endif
757 if (ctxt->html) {
758 if (ctxt->myDoc == NULL)
759#ifdef LIBXML_HTML_ENABLED
760 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
761#else
762 xmlGenericError(xmlGenericErrorContext,
763 "libxml2 built without HTML support\n");
764#endif
765 } else {
766 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
767 if (doc != NULL) {
768 if (ctxt->encoding != NULL)
769 doc->encoding = xmlStrdup(ctxt->encoding);
770 else
771 doc->encoding = NULL;
772 doc->standalone = ctxt->standalone;
773 }
774 }
775 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
776 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
Igor Zlatkovic18fb2782003-02-19 14:49:48 +0000777 ctxt->myDoc->URL = xmlCanonicPath((const xmlChar *) ctxt->input->filename);
778 if (ctxt->myDoc->URL == NULL)
779 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
Owen Taylor3473f882001-02-23 17:55:21 +0000780 }
781}
782
783/**
784 * endDocument:
785 * @ctx: the user data (XML parser context)
786 *
787 * called when the document end has been detected.
788 */
789void
790endDocument(void *ctx)
791{
792 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
793#ifdef DEBUG_SAX
794 xmlGenericError(xmlGenericErrorContext,
795 "SAX.endDocument()\n");
796#endif
797 if (ctxt->validate && ctxt->wellFormed &&
798 ctxt->myDoc && ctxt->myDoc->intSubset)
799 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
800
801 /*
802 * Grab the encoding if it was added on-the-fly
803 */
804 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
805 (ctxt->myDoc->encoding == NULL)) {
806 ctxt->myDoc->encoding = ctxt->encoding;
807 ctxt->encoding = NULL;
808 }
809 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
810 (ctxt->myDoc->encoding == NULL)) {
811 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
812 }
813 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
814 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
815 ctxt->myDoc->charset = ctxt->charset;
816 }
817}
818
819/**
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000820 * my_attribute:
Owen Taylor3473f882001-02-23 17:55:21 +0000821 * @ctx: the user data (XML parser context)
822 * @fullname: The attribute name, including namespace prefix
823 * @value: The attribute value
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000824 * @prefix: the prefix on the element node
Owen Taylor3473f882001-02-23 17:55:21 +0000825 *
826 * Handle an attribute that has been read by the parser.
827 * The default handling is to convert the attribute into an
828 * DOM subtree and past it in a new xmlAttr element added to
829 * the element.
830 */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000831static void
832my_attribute(void *ctx, const xmlChar *fullname, const xmlChar *value,
833 const xmlChar *prefix)
Owen Taylor3473f882001-02-23 17:55:21 +0000834{
835 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
836 xmlAttrPtr ret;
837 xmlChar *name;
838 xmlChar *ns;
839 xmlChar *nval;
840 xmlNsPtr namespace;
841
842/****************
843#ifdef DEBUG_SAX
844 xmlGenericError(xmlGenericErrorContext,
845 "SAX.attribute(%s, %s)\n", fullname, value);
846#endif
847 ****************/
848 /*
849 * Split the full name into a namespace prefix and the tag name
850 */
851 name = xmlSplitQName(ctxt, fullname, &ns);
852
853 /*
854 * Do the last stage of the attribute normalization
855 * Needed for HTML too:
856 * http://www.w3.org/TR/html4/types.html#h-6.2
857 */
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000858 ctxt->vctxt.valid = 1;
859 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
860 ctxt->myDoc, ctxt->node,
Owen Taylor3473f882001-02-23 17:55:21 +0000861 fullname, value);
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000862 if (ctxt->vctxt.valid != 1) {
863 ctxt->valid = 0;
864 }
Owen Taylor3473f882001-02-23 17:55:21 +0000865 if (nval != NULL)
866 value = nval;
867
868 /*
869 * Check whether it's a namespace definition
870 */
871 if ((!ctxt->html) && (ns == NULL) &&
872 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
873 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000874 xmlNsPtr nsret;
Daniel Veillard99737f52003-03-22 14:55:50 +0000875 xmlChar *val;
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000876
Daniel Veillard99737f52003-03-22 14:55:50 +0000877 if (!ctxt->replaceEntities) {
878 ctxt->depth++;
879 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
880 0,0,0);
881 ctxt->depth--;
882 } else {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000883 val = (xmlChar *) value;
Daniel Veillard99737f52003-03-22 14:55:50 +0000884 }
885
886 if (val[0] != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000887 xmlURIPtr uri;
888
Daniel Veillard99737f52003-03-22 14:55:50 +0000889 uri = xmlParseURI((const char *)val);
Owen Taylor3473f882001-02-23 17:55:21 +0000890 if (uri == NULL) {
891 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
892 ctxt->sax->warning(ctxt->userData,
Daniel Veillard99737f52003-03-22 14:55:50 +0000893 "nmlns: %s not a valid URI\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000894 } else {
895 if (uri->scheme == NULL) {
896 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
897 ctxt->sax->warning(ctxt->userData,
Daniel Veillard99737f52003-03-22 14:55:50 +0000898 "xmlns: URI %s is not absolute\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000899 }
900 xmlFreeURI(uri);
901 }
902 }
903
904 /* a default namespace definition */
Daniel Veillard99737f52003-03-22 14:55:50 +0000905 nsret = xmlNewNs(ctxt->node, val, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000906
907 /*
908 * Validate also for namespace decls, they are attributes from
909 * an XML-1.0 perspective
910 */
911 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
912 ctxt->myDoc && ctxt->myDoc->intSubset)
913 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
Daniel Veillard99737f52003-03-22 14:55:50 +0000914 ctxt->node, prefix, nsret, val);
Owen Taylor3473f882001-02-23 17:55:21 +0000915 if (name != NULL)
916 xmlFree(name);
917 if (nval != NULL)
918 xmlFree(nval);
Daniel Veillard99737f52003-03-22 14:55:50 +0000919 if (val != value)
920 xmlFree(val);
Owen Taylor3473f882001-02-23 17:55:21 +0000921 return;
922 }
923 if ((!ctxt->html) &&
924 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
925 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000926 xmlNsPtr nsret;
Daniel Veillard99737f52003-03-22 14:55:50 +0000927 xmlChar *val;
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000928
Daniel Veillard99737f52003-03-22 14:55:50 +0000929 if (!ctxt->replaceEntities) {
930 ctxt->depth++;
931 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
932 0,0,0);
933 ctxt->depth--;
934 } else {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000935 val = (xmlChar *) value;
Daniel Veillard99737f52003-03-22 14:55:50 +0000936 }
937
938 if (val[0] == 0) {
Daniel Veillardc0fef772002-03-01 16:16:31 +0000939 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
940 ctxt->sax->error(ctxt->userData,
941 "Empty namespace name for prefix %s\n", name);
942 }
Daniel Veillard99737f52003-03-22 14:55:50 +0000943 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
Daniel Veillardecaba492002-12-30 10:55:29 +0000944 xmlURIPtr uri;
945
Daniel Veillard99737f52003-03-22 14:55:50 +0000946 uri = xmlParseURI((const char *)val);
Daniel Veillardecaba492002-12-30 10:55:29 +0000947 if (uri == NULL) {
948 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
949 ctxt->sax->warning(ctxt->userData,
950 "xmlns:%s: %s not a valid URI\n", name, value);
951 } else {
952 if (uri->scheme == NULL) {
953 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
954 ctxt->sax->warning(ctxt->userData,
955 "xmlns:%s: URI %s is not absolute\n", name, value);
956 }
957 xmlFreeURI(uri);
958 }
959 }
960
Owen Taylor3473f882001-02-23 17:55:21 +0000961 /* a standard namespace definition */
Daniel Veillard99737f52003-03-22 14:55:50 +0000962 nsret = xmlNewNs(ctxt->node, val, name);
Owen Taylor3473f882001-02-23 17:55:21 +0000963 xmlFree(ns);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000964 /*
965 * Validate also for namespace decls, they are attributes from
966 * an XML-1.0 perspective
967 */
968 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
969 ctxt->myDoc && ctxt->myDoc->intSubset)
970 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
Daniel Veillardfcc822e2003-02-24 17:52:08 +0000971 ctxt->node, name, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000972 if (name != NULL)
973 xmlFree(name);
974 if (nval != NULL)
975 xmlFree(nval);
Daniel Veillard99737f52003-03-22 14:55:50 +0000976 if (val != value)
977 xmlFree(val);
Owen Taylor3473f882001-02-23 17:55:21 +0000978 return;
979 }
980
Daniel Veillardde590ca2003-02-05 10:45:26 +0000981 if (ns != NULL) {
982 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +0000983 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardde590ca2003-02-05 10:45:26 +0000984
985 prop = ctxt->node->properties;
986 while (prop != NULL) {
987 if (prop->ns != NULL) {
988 if ((xmlStrEqual(name, prop->name)) &&
989 ((namespace == prop->ns) ||
990 (xmlStrEqual(namespace->href, prop->ns->href)))) {
991 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
992 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
993 ctxt->sax->error(ctxt->userData,
994 "Attribute %s in %s redefined\n",
995 name, namespace->href);
996 ctxt->wellFormed = 0;
997 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
998 goto error;
999 }
1000 }
1001 prop = prop->next;
1002 }
1003 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00001004 namespace = NULL;
1005 }
1006
1007 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001008 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001009
1010 if (ret != NULL) {
1011 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1012 xmlNodePtr tmp;
1013
1014 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1015 tmp = ret->children;
1016 while (tmp != NULL) {
1017 tmp->parent = (xmlNodePtr) ret;
1018 if (tmp->next == NULL)
1019 ret->last = tmp;
1020 tmp = tmp->next;
1021 }
1022 } else if (value != NULL) {
1023 ret->children = xmlNewDocText(ctxt->myDoc, value);
1024 ret->last = ret->children;
1025 if (ret->children != NULL)
1026 ret->children->parent = (xmlNodePtr) ret;
1027 }
1028 }
1029
1030 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1031 ctxt->myDoc && ctxt->myDoc->intSubset) {
1032
1033 /*
1034 * If we don't substitute entities, the validation should be
1035 * done on a value with replaced entities anyway.
1036 */
1037 if (!ctxt->replaceEntities) {
1038 xmlChar *val;
1039
1040 ctxt->depth++;
1041 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1042 0,0,0);
1043 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +00001044
Owen Taylor3473f882001-02-23 17:55:21 +00001045 if (val == NULL)
1046 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1047 ctxt->myDoc, ctxt->node, ret, value);
1048 else {
Daniel Veillardc7612992002-02-17 22:47:37 +00001049 xmlChar *nvalnorm;
1050
1051 /*
1052 * Do the last stage of the attribute normalization
1053 * It need to be done twice ... it's an extra burden related
1054 * to the ability to keep references in attributes
1055 */
1056 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1057 ctxt->node, fullname, val);
1058 if (nvalnorm != NULL) {
1059 xmlFree(val);
1060 val = nvalnorm;
1061 }
1062
Owen Taylor3473f882001-02-23 17:55:21 +00001063 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1064 ctxt->myDoc, ctxt->node, ret, val);
1065 xmlFree(val);
1066 }
1067 } else {
1068 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1069 ctxt->node, ret, value);
1070 }
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001071 } else if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1072 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1073 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001074 /*
1075 * when validating, the ID registration is done at the attribute
1076 * validation level. Otherwise we have to do specific handling here.
1077 */
1078 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1079 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1080 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1081 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1082 }
1083
Daniel Veillardde590ca2003-02-05 10:45:26 +00001084error:
Owen Taylor3473f882001-02-23 17:55:21 +00001085 if (nval != NULL)
1086 xmlFree(nval);
Owen Taylor3473f882001-02-23 17:55:21 +00001087 if (ns != NULL)
1088 xmlFree(ns);
1089}
1090
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001091/**
1092 * attribute:
1093 * @ctx: the user data (XML parser context)
1094 * @fullname: The attribute name, including namespace prefix
1095 * @value: The attribute value
1096 *
1097 * Handle an attribute that has been read by the parser.
1098 * The default handling is to convert the attribute into an
1099 * DOM subtree and past it in a new xmlAttr element added to
1100 * the element.
1101 */
1102void
1103attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1104{
1105 my_attribute(ctx, fullname, value, NULL);
1106}
1107
Daniel Veillard878eab02002-02-19 13:46:09 +00001108/*
1109 * xmlCheckDefaultedAttributes:
1110 *
1111 * Check defaulted attributes from the DTD
1112 */
1113static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001114xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001115 const xmlChar *prefix, const xmlChar **atts) {
1116 xmlElementPtr elemDecl;
1117 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001118 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001119 int i;
1120
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001121 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1122 if (elemDecl == NULL) {
1123 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1124 internal = 0;
1125 }
1126
1127process_external_subset:
1128
Daniel Veillard878eab02002-02-19 13:46:09 +00001129 if (elemDecl != NULL) {
1130 xmlAttributePtr attr = elemDecl->attributes;
1131 /*
1132 * Check against defaulted attributes from the external subset
1133 * if the document is stamped as standalone
1134 */
1135 if ((ctxt->myDoc->standalone == 1) &&
1136 (ctxt->myDoc->extSubset != NULL) &&
1137 (ctxt->validate)) {
1138 while (attr != NULL) {
1139 if ((attr->defaultValue != NULL) &&
1140 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1141 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001142 attr->prefix) == attr) &&
1143 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1144 attr->elem, attr->name,
1145 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001146 xmlChar *fulln;
1147
1148 if (attr->prefix != NULL) {
1149 fulln = xmlStrdup(attr->prefix);
1150 fulln = xmlStrcat(fulln, BAD_CAST ":");
1151 fulln = xmlStrcat(fulln, attr->name);
1152 } else {
1153 fulln = xmlStrdup(attr->name);
1154 }
1155
1156 /*
1157 * Check that the attribute is not declared in the
1158 * serialization
1159 */
1160 att = NULL;
1161 if (atts != NULL) {
1162 i = 0;
1163 att = atts[i];
1164 while (att != NULL) {
1165 if (xmlStrEqual(att, fulln))
1166 break;
1167 i += 2;
1168 att = atts[i];
1169 }
1170 }
1171 if (att == NULL) {
1172 if (ctxt->vctxt.error != NULL)
1173 ctxt->vctxt.error(ctxt->vctxt.userData,
1174 "standalone: attribute %s on %s defaulted from external subset\n",
1175 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001176 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001177 }
1178 }
1179 attr = attr->nexth;
1180 }
1181 }
1182
1183 /*
1184 * Actually insert defaulted values when needed
1185 */
1186 attr = elemDecl->attributes;
1187 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001188 /*
1189 * Make sure that attributes redefinition occuring in the
1190 * internal subset are not overriden by definitions in the
1191 * external subset.
1192 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001193 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001194 /*
1195 * the element should be instantiated in the tree if:
1196 * - this is a namespace prefix
1197 * - the user required for completion in the tree
1198 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001199 * - there isn't already an attribute definition
1200 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001201 */
1202 if (((attr->prefix != NULL) &&
1203 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1204 ((attr->prefix == NULL) &&
1205 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1206 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001207 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001208
Daniel Veillard8aff2472002-02-19 21:50:43 +00001209 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1210 attr->elem, attr->name,
1211 attr->prefix);
1212 if ((tst == attr) || (tst == NULL)) {
1213 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001214
Daniel Veillard8aff2472002-02-19 21:50:43 +00001215 if (attr->prefix != NULL) {
1216 fulln = xmlStrdup(attr->prefix);
1217 fulln = xmlStrcat(fulln, BAD_CAST ":");
1218 fulln = xmlStrcat(fulln, attr->name);
1219 } else {
1220 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001221 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001222
1223 /*
1224 * Check that the attribute is not declared in the
1225 * serialization
1226 */
1227 att = NULL;
1228 if (atts != NULL) {
1229 i = 0;
1230 att = atts[i];
1231 while (att != NULL) {
1232 if (xmlStrEqual(att, fulln))
1233 break;
1234 i += 2;
1235 att = atts[i];
1236 }
1237 }
1238 if (att == NULL) {
1239 attribute(ctxt, fulln, attr->defaultValue);
1240 }
1241 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001242 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001243 }
1244 }
1245 attr = attr->nexth;
1246 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001247 if (internal == 1) {
1248 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1249 name, prefix);
1250 internal = 0;
1251 goto process_external_subset;
1252 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001253 }
1254}
1255
Owen Taylor3473f882001-02-23 17:55:21 +00001256/**
1257 * startElement:
1258 * @ctx: the user data (XML parser context)
1259 * @fullname: The element name, including namespace prefix
1260 * @atts: An array of name/value attributes pairs, NULL terminated
1261 *
1262 * called when an opening tag has been processed.
1263 */
1264void
1265startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1266{
1267 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1268 xmlNodePtr ret;
1269 xmlNodePtr parent = ctxt->node;
1270 xmlNsPtr ns;
1271 xmlChar *name;
1272 xmlChar *prefix;
1273 const xmlChar *att;
1274 const xmlChar *value;
1275 int i;
1276
1277#ifdef DEBUG_SAX
1278 xmlGenericError(xmlGenericErrorContext,
1279 "SAX.startElement(%s)\n", fullname);
1280#endif
1281
1282 /*
1283 * First check on validity:
1284 */
1285 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1286 ((ctxt->myDoc->intSubset == NULL) ||
1287 ((ctxt->myDoc->intSubset->notations == NULL) &&
1288 (ctxt->myDoc->intSubset->elements == NULL) &&
1289 (ctxt->myDoc->intSubset->attributes == NULL) &&
1290 (ctxt->myDoc->intSubset->entities == NULL)))) {
1291 if (ctxt->vctxt.error != NULL) {
1292 ctxt->vctxt.error(ctxt->vctxt.userData,
1293 "Validation failed: no DTD found !\n");
1294 }
1295 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001296 ctxt->valid = 0;
1297 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001298 }
1299
1300
1301 /*
1302 * Split the full name into a namespace prefix and the tag name
1303 */
1304 name = xmlSplitQName(ctxt, fullname, &prefix);
1305
1306
1307 /*
1308 * Note : the namespace resolution is deferred until the end of the
1309 * attributes parsing, since local namespace can be defined as
1310 * an attribute at this level.
1311 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001312 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001313 if (ret == NULL) return;
1314 if (ctxt->myDoc->children == NULL) {
1315#ifdef DEBUG_SAX_TREE
1316 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1317#endif
1318 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1319 } else if (parent == NULL) {
1320 parent = ctxt->myDoc->children;
1321 }
1322 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001323 if (ctxt->linenumbers) {
1324 if (ctxt->input != NULL)
1325 ret->content = (void *) (long) ctxt->input->line;
1326 }
Owen Taylor3473f882001-02-23 17:55:21 +00001327
1328 /*
1329 * We are parsing a new node.
1330 */
1331#ifdef DEBUG_SAX_TREE
1332 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1333#endif
1334 nodePush(ctxt, ret);
1335
1336 /*
1337 * Link the child element
1338 */
1339 if (parent != NULL) {
1340 if (parent->type == XML_ELEMENT_NODE) {
1341#ifdef DEBUG_SAX_TREE
1342 xmlGenericError(xmlGenericErrorContext,
1343 "adding child %s to %s\n", name, parent->name);
1344#endif
1345 xmlAddChild(parent, ret);
1346 } else {
1347#ifdef DEBUG_SAX_TREE
1348 xmlGenericError(xmlGenericErrorContext,
1349 "adding sibling %s to ", name);
1350 xmlDebugDumpOneNode(stderr, parent, 0);
1351#endif
1352 xmlAddSibling(parent, ret);
1353 }
1354 }
1355
1356 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001357 * Insert all the defaulted attributes from the DTD especially namespaces
1358 */
1359 if ((!ctxt->html) &&
1360 ((ctxt->myDoc->intSubset != NULL) ||
1361 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001362 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001363 }
1364
1365 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001366 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001367 */
1368 if (atts != NULL) {
1369 i = 0;
1370 att = atts[i++];
1371 value = atts[i++];
1372 if (!ctxt->html) {
1373 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001374 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1375 (att[3] == 'n') && (att[4] == 's'))
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001376 my_attribute(ctxt, att, value, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001377
1378 att = atts[i++];
1379 value = atts[i++];
1380 }
1381 }
1382 }
1383
1384 /*
1385 * Search the namespace, note that since the attributes have been
1386 * processed, the local namespaces are available.
1387 */
1388 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1389 if ((ns == NULL) && (parent != NULL))
1390 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1391 if ((prefix != NULL) && (ns == NULL)) {
1392 ns = xmlNewNs(ret, NULL, prefix);
1393 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1394 ctxt->sax->warning(ctxt->userData,
1395 "Namespace prefix %s is not defined\n", prefix);
1396 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001397
1398 /*
1399 * set the namespace node, making sure that if the default namspace
1400 * is unbound on a parent we simply kee it NULL
1401 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001402 if ((ns != NULL) && (ns->href != NULL) &&
1403 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001404 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001405
1406 /*
1407 * process all the other attributes
1408 */
1409 if (atts != NULL) {
1410 i = 0;
1411 att = atts[i++];
1412 value = atts[i++];
1413 if (ctxt->html) {
1414 while (att != NULL) {
1415 attribute(ctxt, att, value);
1416 att = atts[i++];
1417 value = atts[i++];
1418 }
1419 } else {
1420 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001421 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1422 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001423 attribute(ctxt, att, value);
1424
1425 /*
1426 * Next ones
1427 */
1428 att = atts[i++];
1429 value = atts[i++];
1430 }
1431 }
1432 }
1433
1434 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001435 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001436 * check the document root element for validity
1437 */
1438 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001439 int chk;
1440
1441 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1442 if (chk <= 0)
1443 ctxt->valid = 0;
1444 if (chk < 0)
1445 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001446 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1447 ctxt->vctxt.finishDtd = 1;
1448 }
1449
1450 if (prefix != NULL)
1451 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001452
1453}
1454
1455/**
1456 * endElement:
1457 * @ctx: the user data (XML parser context)
1458 * @name: The element name
1459 *
1460 * called when the end of an element has been detected.
1461 */
1462void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001463endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001464{
1465 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1466 xmlParserNodeInfo node_info;
1467 xmlNodePtr cur = ctxt->node;
1468
1469#ifdef DEBUG_SAX
1470 if (name == NULL)
1471 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1472 else
1473 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1474#endif
1475
1476 /* Capture end position and add node */
1477 if (cur != NULL && ctxt->record_info) {
1478 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1479 node_info.end_line = ctxt->input->line;
1480 node_info.node = cur;
1481 xmlParserAddNodeInfo(ctxt, &node_info);
1482 }
1483 ctxt->nodemem = -1;
1484
1485 if (ctxt->validate && ctxt->wellFormed &&
1486 ctxt->myDoc && ctxt->myDoc->intSubset)
1487 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1488 cur);
1489
1490
1491 /*
1492 * end of parsing of this node.
1493 */
1494#ifdef DEBUG_SAX_TREE
1495 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1496#endif
1497 nodePop(ctxt);
1498}
1499
1500/**
1501 * reference:
1502 * @ctx: the user data (XML parser context)
1503 * @name: The entity name
1504 *
1505 * called when an entity reference is detected.
1506 */
1507void
1508reference(void *ctx, const xmlChar *name)
1509{
1510 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1511 xmlNodePtr ret;
1512
1513#ifdef DEBUG_SAX
1514 xmlGenericError(xmlGenericErrorContext,
1515 "SAX.reference(%s)\n", name);
1516#endif
1517 if (name[0] == '#')
1518 ret = xmlNewCharRef(ctxt->myDoc, name);
1519 else
1520 ret = xmlNewReference(ctxt->myDoc, name);
1521#ifdef DEBUG_SAX_TREE
1522 xmlGenericError(xmlGenericErrorContext,
1523 "add reference %s to %s \n", name, ctxt->node->name);
1524#endif
1525 xmlAddChild(ctxt->node, ret);
1526}
1527
1528/**
1529 * characters:
1530 * @ctx: the user data (XML parser context)
1531 * @ch: a xmlChar string
1532 * @len: the number of xmlChar
1533 *
1534 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001535 */
1536void
1537characters(void *ctx, const xmlChar *ch, int len)
1538{
1539 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1540 xmlNodePtr lastChild;
1541
1542#ifdef DEBUG_SAX
1543 xmlGenericError(xmlGenericErrorContext,
1544 "SAX.characters(%.30s, %d)\n", ch, len);
1545#endif
1546 /*
1547 * Handle the data if any. If there is no child
1548 * add it as content, otherwise if the last child is text,
1549 * concatenate it, else create a new node of type text.
1550 */
1551
1552 if (ctxt->node == NULL) {
1553#ifdef DEBUG_SAX_TREE
1554 xmlGenericError(xmlGenericErrorContext,
1555 "add chars: ctxt->node == NULL !\n");
1556#endif
1557 return;
1558 }
1559 lastChild = xmlGetLastChild(ctxt->node);
1560#ifdef DEBUG_SAX_TREE
1561 xmlGenericError(xmlGenericErrorContext,
1562 "add chars to %s \n", ctxt->node->name);
1563#endif
1564
1565 /*
1566 * Here we needed an accelerator mechanism in case of very large
1567 * elements. Use an attribute in the structure !!!
1568 */
1569 if (lastChild == NULL) {
1570 /* first node, first time */
1571 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001572 if (ctxt->node->children != NULL) {
1573 ctxt->nodelen = len;
1574 ctxt->nodemem = len + 1;
1575 }
Owen Taylor3473f882001-02-23 17:55:21 +00001576 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001577 int coalesceText = (lastChild != NULL) &&
1578 (lastChild->type == XML_TEXT_NODE) &&
1579 (lastChild->name == xmlStringText);
1580 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001581 /*
1582 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001583 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001584 * reallocate a new buffer, move data, append ch. Here
1585 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001586 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001587 */
1588 if (ctxt->nodelen + len >= ctxt->nodemem) {
1589 xmlChar *newbuf;
1590 int size;
1591
1592 size = ctxt->nodemem + len;
1593 size *= 2;
1594 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1595 if (newbuf == NULL) {
1596 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1597 ctxt->sax->error(ctxt->userData,
1598 "SAX.characters(): out of memory\n");
1599 return;
1600 }
1601 ctxt->nodemem = size;
1602 lastChild->content = newbuf;
1603 }
1604 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1605 ctxt->nodelen += len;
1606 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001607 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001608 xmlTextConcat(lastChild, ch, len);
1609 if (ctxt->node->children != NULL) {
1610 ctxt->nodelen = xmlStrlen(lastChild->content);
1611 ctxt->nodemem = ctxt->nodelen + 1;
1612 }
Owen Taylor3473f882001-02-23 17:55:21 +00001613 } else {
1614 /* Mixed content, first time */
1615 lastChild = xmlNewTextLen(ch, len);
1616 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001617 if (ctxt->node->children != NULL) {
1618 ctxt->nodelen = len;
1619 ctxt->nodemem = len + 1;
1620 }
Owen Taylor3473f882001-02-23 17:55:21 +00001621 }
1622 }
1623}
1624
1625/**
1626 * ignorableWhitespace:
1627 * @ctx: the user data (XML parser context)
1628 * @ch: a xmlChar string
1629 * @len: the number of xmlChar
1630 *
1631 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001632 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001633 */
1634void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001635ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001636{
1637 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1638#ifdef DEBUG_SAX
1639 xmlGenericError(xmlGenericErrorContext,
1640 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1641#endif
1642}
1643
1644/**
1645 * processingInstruction:
1646 * @ctx: the user data (XML parser context)
1647 * @target: the target name
1648 * @data: the PI data's
1649 *
1650 * A processing instruction has been parsed.
1651 */
1652void
1653processingInstruction(void *ctx, const xmlChar *target,
1654 const xmlChar *data)
1655{
1656 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1657 xmlNodePtr ret;
1658 xmlNodePtr parent = ctxt->node;
1659
1660#ifdef DEBUG_SAX
1661 xmlGenericError(xmlGenericErrorContext,
1662 "SAX.processingInstruction(%s, %s)\n", target, data);
1663#endif
1664
1665 ret = xmlNewPI(target, data);
1666 if (ret == NULL) return;
1667 parent = ctxt->node;
1668
1669 if (ctxt->inSubset == 1) {
1670 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1671 return;
1672 } else if (ctxt->inSubset == 2) {
1673 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1674 return;
1675 }
1676 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1677#ifdef DEBUG_SAX_TREE
1678 xmlGenericError(xmlGenericErrorContext,
1679 "Setting PI %s as root\n", target);
1680#endif
1681 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1682 return;
1683 }
1684 if (parent->type == XML_ELEMENT_NODE) {
1685#ifdef DEBUG_SAX_TREE
1686 xmlGenericError(xmlGenericErrorContext,
1687 "adding PI %s child to %s\n", target, parent->name);
1688#endif
1689 xmlAddChild(parent, ret);
1690 } else {
1691#ifdef DEBUG_SAX_TREE
1692 xmlGenericError(xmlGenericErrorContext,
1693 "adding PI %s sibling to ", target);
1694 xmlDebugDumpOneNode(stderr, parent, 0);
1695#endif
1696 xmlAddSibling(parent, ret);
1697 }
1698}
1699
1700/**
1701 * globalNamespace:
1702 * @ctx: the user data (XML parser context)
1703 * @href: the namespace associated URN
1704 * @prefix: the namespace prefix
1705 *
1706 * An old global namespace has been parsed.
1707 */
1708void
1709globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1710{
1711 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1712#ifdef DEBUG_SAX
1713 xmlGenericError(xmlGenericErrorContext,
1714 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1715#endif
1716 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1717}
1718
1719/**
1720 * setNamespace:
1721 * @ctx: the user data (XML parser context)
1722 * @name: the namespace prefix
1723 *
1724 * Set the current element namespace.
1725 */
1726
1727void
1728setNamespace(void *ctx, const xmlChar *name)
1729{
1730 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1731 xmlNsPtr ns;
1732 xmlNodePtr parent;
1733
1734#ifdef DEBUG_SAX
1735 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1736#endif
1737 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1738 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1739 if (ctxt->nodeNr >= 2) {
1740 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1741 if (parent != NULL)
1742 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1743 }
1744 }
1745 xmlSetNs(ctxt->node, ns);
1746}
1747
1748/**
1749 * getNamespace:
1750 * @ctx: the user data (XML parser context)
1751 *
1752 * Get the current element namespace.
1753 *
1754 * Returns the xmlNsPtr or NULL if none
1755 */
1756
1757xmlNsPtr
1758getNamespace(void *ctx)
1759{
1760 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1761 xmlNsPtr ret;
1762
1763#ifdef DEBUG_SAX
1764 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1765#endif
1766 ret = ctxt->node->ns;
1767 return(ret);
1768}
1769
1770/**
1771 * checkNamespace:
1772 * @ctx: the user data (XML parser context)
1773 * @namespace: the namespace to check against
1774 *
1775 * Check that the current element namespace is the same as the
1776 * one read upon parsing.
1777 *
1778 * Returns 1 if true 0 otherwise
1779 */
1780
1781int
1782checkNamespace(void *ctx, xmlChar *namespace)
1783{
1784 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1785 xmlNodePtr cur = ctxt->node;
1786
1787#ifdef DEBUG_SAX
1788 xmlGenericError(xmlGenericErrorContext,
1789 "SAX.checkNamespace(%s)\n", namespace);
1790#endif
1791
1792 /*
1793 * Check that the Name in the ETag is the same as in the STag.
1794 */
1795 if (namespace == NULL) {
1796 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1797 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1798 ctxt->sax->error(ctxt,
1799 "End tags for %s don't hold the namespace %s\n",
1800 cur->name, cur->ns->prefix);
1801 ctxt->wellFormed = 0;
1802 }
1803 } else {
1804 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1805 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1806 ctxt->sax->error(ctxt,
1807 "End tags %s holds a prefix %s not used by the open tag\n",
1808 cur->name, namespace);
1809 ctxt->wellFormed = 0;
1810 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1811 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1812 ctxt->sax->error(ctxt,
1813 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1814 cur->name, cur->ns->prefix, namespace);
1815 ctxt->wellFormed = 0;
1816 } else
1817 return(1);
1818 }
1819 return(0);
1820}
1821
1822/**
1823 * namespaceDecl:
1824 * @ctx: the user data (XML parser context)
1825 * @href: the namespace associated URN
1826 * @prefix: the namespace prefix
1827 *
1828 * A namespace has been parsed.
1829 */
1830void
1831namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1832{
1833 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1834#ifdef DEBUG_SAX
1835 if (prefix == NULL)
1836 xmlGenericError(xmlGenericErrorContext,
1837 "SAX.namespaceDecl(%s, NULL)\n", href);
1838 else
1839 xmlGenericError(xmlGenericErrorContext,
1840 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1841#endif
1842 xmlNewNs(ctxt->node, href, prefix);
1843}
1844
1845/**
1846 * comment:
1847 * @ctx: the user data (XML parser context)
1848 * @value: the comment content
1849 *
1850 * A comment has been parsed.
1851 */
1852void
1853comment(void *ctx, const xmlChar *value)
1854{
1855 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1856 xmlNodePtr ret;
1857 xmlNodePtr parent = ctxt->node;
1858
1859#ifdef DEBUG_SAX
1860 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1861#endif
1862 ret = xmlNewDocComment(ctxt->myDoc, value);
1863 if (ret == NULL) return;
1864
1865 if (ctxt->inSubset == 1) {
1866 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1867 return;
1868 } else if (ctxt->inSubset == 2) {
1869 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1870 return;
1871 }
1872 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1873#ifdef DEBUG_SAX_TREE
1874 xmlGenericError(xmlGenericErrorContext,
1875 "Setting comment as root\n");
1876#endif
1877 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1878 return;
1879 }
1880 if (parent->type == XML_ELEMENT_NODE) {
1881#ifdef DEBUG_SAX_TREE
1882 xmlGenericError(xmlGenericErrorContext,
1883 "adding comment child to %s\n", parent->name);
1884#endif
1885 xmlAddChild(parent, ret);
1886 } else {
1887#ifdef DEBUG_SAX_TREE
1888 xmlGenericError(xmlGenericErrorContext,
1889 "adding comment sibling to ");
1890 xmlDebugDumpOneNode(stderr, parent, 0);
1891#endif
1892 xmlAddSibling(parent, ret);
1893 }
1894}
1895
1896/**
1897 * cdataBlock:
1898 * @ctx: the user data (XML parser context)
1899 * @value: The pcdata content
1900 * @len: the block length
1901 *
1902 * called when a pcdata block has been parsed
1903 */
1904void
1905cdataBlock(void *ctx, const xmlChar *value, int len)
1906{
1907 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1908 xmlNodePtr ret, lastChild;
1909
1910#ifdef DEBUG_SAX
1911 xmlGenericError(xmlGenericErrorContext,
1912 "SAX.pcdata(%.10s, %d)\n", value, len);
1913#endif
1914 lastChild = xmlGetLastChild(ctxt->node);
1915#ifdef DEBUG_SAX_TREE
1916 xmlGenericError(xmlGenericErrorContext,
1917 "add chars to %s \n", ctxt->node->name);
1918#endif
1919 if ((lastChild != NULL) &&
1920 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1921 xmlTextConcat(lastChild, value, len);
1922 } else {
1923 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1924 xmlAddChild(ctxt->node, ret);
1925 }
1926}
1927
Daniel Veillardd0463562001-10-13 09:15:48 +00001928/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001929 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001930 * @hdlr: the SAX handler
1931 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001932 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001933 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001934 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001935void
1936initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1937{
1938 if(hdlr->initialized == 1)
1939 return;
1940
1941 hdlr->internalSubset = internalSubset;
1942 hdlr->externalSubset = externalSubset;
1943 hdlr->isStandalone = isStandalone;
1944 hdlr->hasInternalSubset = hasInternalSubset;
1945 hdlr->hasExternalSubset = hasExternalSubset;
1946 hdlr->resolveEntity = resolveEntity;
1947 hdlr->getEntity = getEntity;
1948 hdlr->getParameterEntity = getParameterEntity;
1949 hdlr->entityDecl = entityDecl;
1950 hdlr->attributeDecl = attributeDecl;
1951 hdlr->elementDecl = elementDecl;
1952 hdlr->notationDecl = notationDecl;
1953 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1954 hdlr->setDocumentLocator = setDocumentLocator;
1955 hdlr->startDocument = startDocument;
1956 hdlr->endDocument = endDocument;
1957 hdlr->startElement = startElement;
1958 hdlr->endElement = endElement;
1959 hdlr->reference = reference;
1960 hdlr->characters = characters;
1961 hdlr->cdataBlock = cdataBlock;
1962 hdlr->ignorableWhitespace = characters;
1963 hdlr->processingInstruction = processingInstruction;
1964 hdlr->comment = comment;
1965 /* if (xmlGetWarningsDefaultValue == 0) */
1966 if (warning == 0)
1967 hdlr->warning = NULL;
1968 else
1969 hdlr->warning = xmlParserWarning;
1970 hdlr->error = xmlParserError;
1971 hdlr->fatalError = xmlParserError;
1972
1973 hdlr->initialized = 1;
1974}
Owen Taylor3473f882001-02-23 17:55:21 +00001975
1976/**
1977 * xmlDefaultSAXHandlerInit:
1978 *
1979 * Initialize the default SAX handler
1980 */
1981void
1982xmlDefaultSAXHandlerInit(void)
1983{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001984 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001985}
1986
Daniel Veillardeae522a2001-04-23 13:41:34 +00001987#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001988
1989/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001990 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001991 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001992 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001993 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001994 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001995void
1996inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1997{
1998 if(hdlr->initialized == 1)
1999 return;
2000
2001 hdlr->internalSubset = internalSubset;
2002 hdlr->externalSubset = NULL;
2003 hdlr->isStandalone = NULL;
2004 hdlr->hasInternalSubset = NULL;
2005 hdlr->hasExternalSubset = NULL;
2006 hdlr->resolveEntity = NULL;
2007 hdlr->getEntity = getEntity;
2008 hdlr->getParameterEntity = NULL;
2009 hdlr->entityDecl = NULL;
2010 hdlr->attributeDecl = NULL;
2011 hdlr->elementDecl = NULL;
2012 hdlr->notationDecl = NULL;
2013 hdlr->unparsedEntityDecl = NULL;
2014 hdlr->setDocumentLocator = setDocumentLocator;
2015 hdlr->startDocument = startDocument;
2016 hdlr->endDocument = endDocument;
2017 hdlr->startElement = startElement;
2018 hdlr->endElement = endElement;
2019 hdlr->reference = NULL;
2020 hdlr->characters = characters;
2021 hdlr->cdataBlock = cdataBlock;
2022 hdlr->ignorableWhitespace = ignorableWhitespace;
2023 hdlr->processingInstruction = NULL;
2024 hdlr->comment = comment;
2025 hdlr->warning = xmlParserWarning;
2026 hdlr->error = xmlParserError;
2027 hdlr->fatalError = xmlParserError;
2028
2029 hdlr->initialized = 1;
2030}
Owen Taylor3473f882001-02-23 17:55:21 +00002031
2032/**
2033 * htmlDefaultSAXHandlerInit:
2034 *
2035 * Initialize the default SAX handler
2036 */
2037void
2038htmlDefaultSAXHandlerInit(void)
2039{
Daniel Veillardd0463562001-10-13 09:15:48 +00002040 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002041}
Daniel Veillardd0463562001-10-13 09:15:48 +00002042
Daniel Veillardeae522a2001-04-23 13:41:34 +00002043#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002044
Daniel Veillardeae522a2001-04-23 13:41:34 +00002045#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00002046
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002047/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00002048 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002049 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002050 *
2051 * Initialize the default DocBook SAX handler
2052 */
Daniel Veillardd0463562001-10-13 09:15:48 +00002053void
2054initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2055{
2056 if(hdlr->initialized == 1)
2057 return;
2058
2059 hdlr->internalSubset = internalSubset;
2060 hdlr->externalSubset = NULL;
2061 hdlr->isStandalone = isStandalone;
2062 hdlr->hasInternalSubset = hasInternalSubset;
2063 hdlr->hasExternalSubset = hasExternalSubset;
2064 hdlr->resolveEntity = resolveEntity;
2065 hdlr->getEntity = getEntity;
2066 hdlr->getParameterEntity = NULL;
2067 hdlr->entityDecl = entityDecl;
2068 hdlr->attributeDecl = NULL;
2069 hdlr->elementDecl = NULL;
2070 hdlr->notationDecl = NULL;
2071 hdlr->unparsedEntityDecl = NULL;
2072 hdlr->setDocumentLocator = setDocumentLocator;
2073 hdlr->startDocument = startDocument;
2074 hdlr->endDocument = endDocument;
2075 hdlr->startElement = startElement;
2076 hdlr->endElement = endElement;
2077 hdlr->reference = reference;
2078 hdlr->characters = characters;
2079 hdlr->cdataBlock = NULL;
2080 hdlr->ignorableWhitespace = ignorableWhitespace;
2081 hdlr->processingInstruction = NULL;
2082 hdlr->comment = comment;
2083 hdlr->warning = xmlParserWarning;
2084 hdlr->error = xmlParserError;
2085 hdlr->fatalError = xmlParserError;
2086
2087 hdlr->initialized = 1;
2088}
Owen Taylor3473f882001-02-23 17:55:21 +00002089
2090/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00002091 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00002092 *
2093 * Initialize the default SAX handler
2094 */
2095void
Daniel Veillardeae522a2001-04-23 13:41:34 +00002096docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00002097{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002098 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002099}
Daniel Veillardeae522a2001-04-23 13:41:34 +00002100
2101#endif /* LIBXML_DOCB_ENABLED */