blob: 1b3f1f500b8d0b152d6740dd8b849d493beaf5db [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 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)) {
Igor Zlatkovic18fb2782003-02-19 14:49:48 +0000765 ctxt->myDoc->URL = xmlCanonicPath((const xmlChar *) ctxt->input->filename);
766 if (ctxt->myDoc->URL == NULL)
767 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
Owen Taylor3473f882001-02-23 17:55:21 +0000768 }
769}
770
771/**
772 * endDocument:
773 * @ctx: the user data (XML parser context)
774 *
775 * called when the document end has been detected.
776 */
777void
778endDocument(void *ctx)
779{
780 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
781#ifdef DEBUG_SAX
782 xmlGenericError(xmlGenericErrorContext,
783 "SAX.endDocument()\n");
784#endif
785 if (ctxt->validate && ctxt->wellFormed &&
786 ctxt->myDoc && ctxt->myDoc->intSubset)
787 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
788
789 /*
790 * Grab the encoding if it was added on-the-fly
791 */
792 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
793 (ctxt->myDoc->encoding == NULL)) {
794 ctxt->myDoc->encoding = ctxt->encoding;
795 ctxt->encoding = NULL;
796 }
797 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
798 (ctxt->myDoc->encoding == NULL)) {
799 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
800 }
801 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
802 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
803 ctxt->myDoc->charset = ctxt->charset;
804 }
805}
806
807/**
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000808 * my_attribute:
Owen Taylor3473f882001-02-23 17:55:21 +0000809 * @ctx: the user data (XML parser context)
810 * @fullname: The attribute name, including namespace prefix
811 * @value: The attribute value
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000812 * @prefix: the prefix on the element node
Owen Taylor3473f882001-02-23 17:55:21 +0000813 *
814 * Handle an attribute that has been read by the parser.
815 * The default handling is to convert the attribute into an
816 * DOM subtree and past it in a new xmlAttr element added to
817 * the element.
818 */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000819static void
820my_attribute(void *ctx, const xmlChar *fullname, const xmlChar *value,
821 const xmlChar *prefix)
Owen Taylor3473f882001-02-23 17:55:21 +0000822{
823 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
824 xmlAttrPtr ret;
825 xmlChar *name;
826 xmlChar *ns;
827 xmlChar *nval;
828 xmlNsPtr namespace;
829
830/****************
831#ifdef DEBUG_SAX
832 xmlGenericError(xmlGenericErrorContext,
833 "SAX.attribute(%s, %s)\n", fullname, value);
834#endif
835 ****************/
836 /*
837 * Split the full name into a namespace prefix and the tag name
838 */
839 name = xmlSplitQName(ctxt, fullname, &ns);
840
841 /*
842 * Do the last stage of the attribute normalization
843 * Needed for HTML too:
844 * http://www.w3.org/TR/html4/types.html#h-6.2
845 */
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000846 ctxt->vctxt.valid = 1;
847 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
848 ctxt->myDoc, ctxt->node,
Owen Taylor3473f882001-02-23 17:55:21 +0000849 fullname, value);
Daniel Veillard8dc16a62002-02-19 21:08:48 +0000850 if (ctxt->vctxt.valid != 1) {
851 ctxt->valid = 0;
852 }
Owen Taylor3473f882001-02-23 17:55:21 +0000853 if (nval != NULL)
854 value = nval;
855
856 /*
857 * Check whether it's a namespace definition
858 */
859 if ((!ctxt->html) && (ns == NULL) &&
860 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
861 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000862 xmlNsPtr nsret;
Daniel Veillard99737f52003-03-22 14:55:50 +0000863 xmlChar *val;
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000864
Daniel Veillard99737f52003-03-22 14:55:50 +0000865 if (!ctxt->replaceEntities) {
866 ctxt->depth++;
867 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
868 0,0,0);
869 ctxt->depth--;
870 } else {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000871 val = (xmlChar *) value;
Daniel Veillard99737f52003-03-22 14:55:50 +0000872 }
873
874 if (val[0] != 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000875 xmlURIPtr uri;
876
Daniel Veillard99737f52003-03-22 14:55:50 +0000877 uri = xmlParseURI((const char *)val);
Owen Taylor3473f882001-02-23 17:55:21 +0000878 if (uri == NULL) {
879 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
880 ctxt->sax->warning(ctxt->userData,
Daniel Veillard99737f52003-03-22 14:55:50 +0000881 "nmlns: %s not a valid URI\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000882 } else {
883 if (uri->scheme == NULL) {
884 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
885 ctxt->sax->warning(ctxt->userData,
Daniel Veillard99737f52003-03-22 14:55:50 +0000886 "xmlns: URI %s is not absolute\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000887 }
888 xmlFreeURI(uri);
889 }
890 }
891
892 /* a default namespace definition */
Daniel Veillard99737f52003-03-22 14:55:50 +0000893 nsret = xmlNewNs(ctxt->node, val, NULL);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000894
895 /*
896 * Validate also for namespace decls, they are attributes from
897 * an XML-1.0 perspective
898 */
899 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
900 ctxt->myDoc && ctxt->myDoc->intSubset)
901 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
Daniel Veillard99737f52003-03-22 14:55:50 +0000902 ctxt->node, prefix, nsret, val);
Owen Taylor3473f882001-02-23 17:55:21 +0000903 if (name != NULL)
904 xmlFree(name);
905 if (nval != NULL)
906 xmlFree(nval);
Daniel Veillard99737f52003-03-22 14:55:50 +0000907 if (val != value)
908 xmlFree(val);
Owen Taylor3473f882001-02-23 17:55:21 +0000909 return;
910 }
911 if ((!ctxt->html) &&
912 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
913 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000914 xmlNsPtr nsret;
Daniel Veillard99737f52003-03-22 14:55:50 +0000915 xmlChar *val;
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000916
Daniel Veillard99737f52003-03-22 14:55:50 +0000917 if (!ctxt->replaceEntities) {
918 ctxt->depth++;
919 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
920 0,0,0);
921 ctxt->depth--;
922 } else {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +0000923 val = (xmlChar *) value;
Daniel Veillard99737f52003-03-22 14:55:50 +0000924 }
925
926 if (val[0] == 0) {
Daniel Veillardc0fef772002-03-01 16:16:31 +0000927 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
928 ctxt->sax->error(ctxt->userData,
929 "Empty namespace name for prefix %s\n", name);
930 }
Daniel Veillard99737f52003-03-22 14:55:50 +0000931 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
Daniel Veillardecaba492002-12-30 10:55:29 +0000932 xmlURIPtr uri;
933
Daniel Veillard99737f52003-03-22 14:55:50 +0000934 uri = xmlParseURI((const char *)val);
Daniel Veillardecaba492002-12-30 10:55:29 +0000935 if (uri == NULL) {
936 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
937 ctxt->sax->warning(ctxt->userData,
938 "xmlns:%s: %s not a valid URI\n", name, value);
939 } else {
940 if (uri->scheme == NULL) {
941 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
942 ctxt->sax->warning(ctxt->userData,
943 "xmlns:%s: URI %s is not absolute\n", name, value);
944 }
945 xmlFreeURI(uri);
946 }
947 }
948
Owen Taylor3473f882001-02-23 17:55:21 +0000949 /* a standard namespace definition */
Daniel Veillard99737f52003-03-22 14:55:50 +0000950 nsret = xmlNewNs(ctxt->node, val, name);
Owen Taylor3473f882001-02-23 17:55:21 +0000951 xmlFree(ns);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000952 /*
953 * Validate also for namespace decls, they are attributes from
954 * an XML-1.0 perspective
955 */
956 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
957 ctxt->myDoc && ctxt->myDoc->intSubset)
958 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
Daniel Veillardfcc822e2003-02-24 17:52:08 +0000959 ctxt->node, name, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000960 if (name != NULL)
961 xmlFree(name);
962 if (nval != NULL)
963 xmlFree(nval);
Daniel Veillard99737f52003-03-22 14:55:50 +0000964 if (val != value)
965 xmlFree(val);
Owen Taylor3473f882001-02-23 17:55:21 +0000966 return;
967 }
968
Daniel Veillardde590ca2003-02-05 10:45:26 +0000969 if (ns != NULL) {
970 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +0000971 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardde590ca2003-02-05 10:45:26 +0000972
973 prop = ctxt->node->properties;
974 while (prop != NULL) {
975 if (prop->ns != NULL) {
976 if ((xmlStrEqual(name, prop->name)) &&
977 ((namespace == prop->ns) ||
978 (xmlStrEqual(namespace->href, prop->ns->href)))) {
979 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
980 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
981 ctxt->sax->error(ctxt->userData,
982 "Attribute %s in %s redefined\n",
983 name, namespace->href);
984 ctxt->wellFormed = 0;
985 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
986 goto error;
987 }
988 }
989 prop = prop->next;
990 }
991 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000992 namespace = NULL;
993 }
994
995 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +0000996 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000997
998 if (ret != NULL) {
999 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1000 xmlNodePtr tmp;
1001
1002 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1003 tmp = ret->children;
1004 while (tmp != NULL) {
1005 tmp->parent = (xmlNodePtr) ret;
1006 if (tmp->next == NULL)
1007 ret->last = tmp;
1008 tmp = tmp->next;
1009 }
1010 } else if (value != NULL) {
1011 ret->children = xmlNewDocText(ctxt->myDoc, value);
1012 ret->last = ret->children;
1013 if (ret->children != NULL)
1014 ret->children->parent = (xmlNodePtr) ret;
1015 }
1016 }
1017
1018 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1019 ctxt->myDoc && ctxt->myDoc->intSubset) {
1020
1021 /*
1022 * If we don't substitute entities, the validation should be
1023 * done on a value with replaced entities anyway.
1024 */
1025 if (!ctxt->replaceEntities) {
1026 xmlChar *val;
1027
1028 ctxt->depth++;
1029 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1030 0,0,0);
1031 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +00001032
Owen Taylor3473f882001-02-23 17:55:21 +00001033 if (val == NULL)
1034 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1035 ctxt->myDoc, ctxt->node, ret, value);
1036 else {
Daniel Veillardc7612992002-02-17 22:47:37 +00001037 xmlChar *nvalnorm;
1038
1039 /*
1040 * Do the last stage of the attribute normalization
1041 * It need to be done twice ... it's an extra burden related
1042 * to the ability to keep references in attributes
1043 */
1044 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1045 ctxt->node, fullname, val);
1046 if (nvalnorm != NULL) {
1047 xmlFree(val);
1048 val = nvalnorm;
1049 }
1050
Owen Taylor3473f882001-02-23 17:55:21 +00001051 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1052 ctxt->myDoc, ctxt->node, ret, val);
1053 xmlFree(val);
1054 }
1055 } else {
1056 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1057 ctxt->node, ret, value);
1058 }
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00001059 } else if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1060 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1061 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001062 /*
1063 * when validating, the ID registration is done at the attribute
1064 * validation level. Otherwise we have to do specific handling here.
1065 */
1066 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1067 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1068 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1069 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1070 }
1071
Daniel Veillardde590ca2003-02-05 10:45:26 +00001072error:
Owen Taylor3473f882001-02-23 17:55:21 +00001073 if (nval != NULL)
1074 xmlFree(nval);
Owen Taylor3473f882001-02-23 17:55:21 +00001075 if (ns != NULL)
1076 xmlFree(ns);
1077}
1078
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001079/**
1080 * attribute:
1081 * @ctx: the user data (XML parser context)
1082 * @fullname: The attribute name, including namespace prefix
1083 * @value: The attribute value
1084 *
1085 * Handle an attribute that has been read by the parser.
1086 * The default handling is to convert the attribute into an
1087 * DOM subtree and past it in a new xmlAttr element added to
1088 * the element.
1089 */
1090void
1091attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1092{
1093 my_attribute(ctx, fullname, value, NULL);
1094}
1095
Daniel Veillard878eab02002-02-19 13:46:09 +00001096/*
1097 * xmlCheckDefaultedAttributes:
1098 *
1099 * Check defaulted attributes from the DTD
1100 */
1101static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001102xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001103 const xmlChar *prefix, const xmlChar **atts) {
1104 xmlElementPtr elemDecl;
1105 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001106 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001107 int i;
1108
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001109 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1110 if (elemDecl == NULL) {
1111 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1112 internal = 0;
1113 }
1114
1115process_external_subset:
1116
Daniel Veillard878eab02002-02-19 13:46:09 +00001117 if (elemDecl != NULL) {
1118 xmlAttributePtr attr = elemDecl->attributes;
1119 /*
1120 * Check against defaulted attributes from the external subset
1121 * if the document is stamped as standalone
1122 */
1123 if ((ctxt->myDoc->standalone == 1) &&
1124 (ctxt->myDoc->extSubset != NULL) &&
1125 (ctxt->validate)) {
1126 while (attr != NULL) {
1127 if ((attr->defaultValue != NULL) &&
1128 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1129 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001130 attr->prefix) == attr) &&
1131 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1132 attr->elem, attr->name,
1133 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001134 xmlChar *fulln;
1135
1136 if (attr->prefix != NULL) {
1137 fulln = xmlStrdup(attr->prefix);
1138 fulln = xmlStrcat(fulln, BAD_CAST ":");
1139 fulln = xmlStrcat(fulln, attr->name);
1140 } else {
1141 fulln = xmlStrdup(attr->name);
1142 }
1143
1144 /*
1145 * Check that the attribute is not declared in the
1146 * serialization
1147 */
1148 att = NULL;
1149 if (atts != NULL) {
1150 i = 0;
1151 att = atts[i];
1152 while (att != NULL) {
1153 if (xmlStrEqual(att, fulln))
1154 break;
1155 i += 2;
1156 att = atts[i];
1157 }
1158 }
1159 if (att == NULL) {
1160 if (ctxt->vctxt.error != NULL)
1161 ctxt->vctxt.error(ctxt->vctxt.userData,
1162 "standalone: attribute %s on %s defaulted from external subset\n",
1163 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001164 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001165 }
1166 }
1167 attr = attr->nexth;
1168 }
1169 }
1170
1171 /*
1172 * Actually insert defaulted values when needed
1173 */
1174 attr = elemDecl->attributes;
1175 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001176 /*
1177 * Make sure that attributes redefinition occuring in the
1178 * internal subset are not overriden by definitions in the
1179 * external subset.
1180 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001181 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001182 /*
1183 * the element should be instantiated in the tree if:
1184 * - this is a namespace prefix
1185 * - the user required for completion in the tree
1186 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001187 * - there isn't already an attribute definition
1188 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001189 */
1190 if (((attr->prefix != NULL) &&
1191 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1192 ((attr->prefix == NULL) &&
1193 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1194 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001195 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001196
Daniel Veillard8aff2472002-02-19 21:50:43 +00001197 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1198 attr->elem, attr->name,
1199 attr->prefix);
1200 if ((tst == attr) || (tst == NULL)) {
1201 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001202
Daniel Veillard8aff2472002-02-19 21:50:43 +00001203 if (attr->prefix != NULL) {
1204 fulln = xmlStrdup(attr->prefix);
1205 fulln = xmlStrcat(fulln, BAD_CAST ":");
1206 fulln = xmlStrcat(fulln, attr->name);
1207 } else {
1208 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001209 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001210
1211 /*
1212 * Check that the attribute is not declared in the
1213 * serialization
1214 */
1215 att = NULL;
1216 if (atts != NULL) {
1217 i = 0;
1218 att = atts[i];
1219 while (att != NULL) {
1220 if (xmlStrEqual(att, fulln))
1221 break;
1222 i += 2;
1223 att = atts[i];
1224 }
1225 }
1226 if (att == NULL) {
1227 attribute(ctxt, fulln, attr->defaultValue);
1228 }
1229 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001230 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001231 }
1232 }
1233 attr = attr->nexth;
1234 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001235 if (internal == 1) {
1236 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1237 name, prefix);
1238 internal = 0;
1239 goto process_external_subset;
1240 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001241 }
1242}
1243
Owen Taylor3473f882001-02-23 17:55:21 +00001244/**
1245 * startElement:
1246 * @ctx: the user data (XML parser context)
1247 * @fullname: The element name, including namespace prefix
1248 * @atts: An array of name/value attributes pairs, NULL terminated
1249 *
1250 * called when an opening tag has been processed.
1251 */
1252void
1253startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1254{
1255 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1256 xmlNodePtr ret;
1257 xmlNodePtr parent = ctxt->node;
1258 xmlNsPtr ns;
1259 xmlChar *name;
1260 xmlChar *prefix;
1261 const xmlChar *att;
1262 const xmlChar *value;
1263 int i;
1264
1265#ifdef DEBUG_SAX
1266 xmlGenericError(xmlGenericErrorContext,
1267 "SAX.startElement(%s)\n", fullname);
1268#endif
1269
1270 /*
1271 * First check on validity:
1272 */
1273 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1274 ((ctxt->myDoc->intSubset == NULL) ||
1275 ((ctxt->myDoc->intSubset->notations == NULL) &&
1276 (ctxt->myDoc->intSubset->elements == NULL) &&
1277 (ctxt->myDoc->intSubset->attributes == NULL) &&
1278 (ctxt->myDoc->intSubset->entities == NULL)))) {
1279 if (ctxt->vctxt.error != NULL) {
1280 ctxt->vctxt.error(ctxt->vctxt.userData,
1281 "Validation failed: no DTD found !\n");
1282 }
1283 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001284 ctxt->valid = 0;
1285 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001286 }
1287
1288
1289 /*
1290 * Split the full name into a namespace prefix and the tag name
1291 */
1292 name = xmlSplitQName(ctxt, fullname, &prefix);
1293
1294
1295 /*
1296 * Note : the namespace resolution is deferred until the end of the
1297 * attributes parsing, since local namespace can be defined as
1298 * an attribute at this level.
1299 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001300 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001301 if (ret == NULL) return;
1302 if (ctxt->myDoc->children == NULL) {
1303#ifdef DEBUG_SAX_TREE
1304 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1305#endif
1306 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1307 } else if (parent == NULL) {
1308 parent = ctxt->myDoc->children;
1309 }
1310 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001311 if (ctxt->linenumbers) {
1312 if (ctxt->input != NULL)
1313 ret->content = (void *) (long) ctxt->input->line;
1314 }
Owen Taylor3473f882001-02-23 17:55:21 +00001315
1316 /*
1317 * We are parsing a new node.
1318 */
1319#ifdef DEBUG_SAX_TREE
1320 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1321#endif
1322 nodePush(ctxt, ret);
1323
1324 /*
1325 * Link the child element
1326 */
1327 if (parent != NULL) {
1328 if (parent->type == XML_ELEMENT_NODE) {
1329#ifdef DEBUG_SAX_TREE
1330 xmlGenericError(xmlGenericErrorContext,
1331 "adding child %s to %s\n", name, parent->name);
1332#endif
1333 xmlAddChild(parent, ret);
1334 } else {
1335#ifdef DEBUG_SAX_TREE
1336 xmlGenericError(xmlGenericErrorContext,
1337 "adding sibling %s to ", name);
1338 xmlDebugDumpOneNode(stderr, parent, 0);
1339#endif
1340 xmlAddSibling(parent, ret);
1341 }
1342 }
1343
1344 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001345 * Insert all the defaulted attributes from the DTD especially namespaces
1346 */
1347 if ((!ctxt->html) &&
1348 ((ctxt->myDoc->intSubset != NULL) ||
1349 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001350 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001351 }
1352
1353 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001354 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001355 */
1356 if (atts != NULL) {
1357 i = 0;
1358 att = atts[i++];
1359 value = atts[i++];
1360 if (!ctxt->html) {
1361 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001362 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1363 (att[3] == 'n') && (att[4] == 's'))
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001364 my_attribute(ctxt, att, value, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001365
1366 att = atts[i++];
1367 value = atts[i++];
1368 }
1369 }
1370 }
1371
1372 /*
1373 * Search the namespace, note that since the attributes have been
1374 * processed, the local namespaces are available.
1375 */
1376 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1377 if ((ns == NULL) && (parent != NULL))
1378 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1379 if ((prefix != NULL) && (ns == NULL)) {
1380 ns = xmlNewNs(ret, NULL, prefix);
1381 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1382 ctxt->sax->warning(ctxt->userData,
1383 "Namespace prefix %s is not defined\n", prefix);
1384 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001385
1386 /*
1387 * set the namespace node, making sure that if the default namspace
1388 * is unbound on a parent we simply kee it NULL
1389 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001390 if ((ns != NULL) && (ns->href != NULL) &&
1391 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001392 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001393
1394 /*
1395 * process all the other attributes
1396 */
1397 if (atts != NULL) {
1398 i = 0;
1399 att = atts[i++];
1400 value = atts[i++];
1401 if (ctxt->html) {
1402 while (att != NULL) {
1403 attribute(ctxt, att, value);
1404 att = atts[i++];
1405 value = atts[i++];
1406 }
1407 } else {
1408 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001409 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1410 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001411 attribute(ctxt, att, value);
1412
1413 /*
1414 * Next ones
1415 */
1416 att = atts[i++];
1417 value = atts[i++];
1418 }
1419 }
1420 }
1421
1422 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001423 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001424 * check the document root element for validity
1425 */
1426 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001427 int chk;
1428
1429 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1430 if (chk <= 0)
1431 ctxt->valid = 0;
1432 if (chk < 0)
1433 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001434 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1435 ctxt->vctxt.finishDtd = 1;
1436 }
1437
1438 if (prefix != NULL)
1439 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001440
1441}
1442
1443/**
1444 * endElement:
1445 * @ctx: the user data (XML parser context)
1446 * @name: The element name
1447 *
1448 * called when the end of an element has been detected.
1449 */
1450void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001451endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001452{
1453 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1454 xmlParserNodeInfo node_info;
1455 xmlNodePtr cur = ctxt->node;
1456
1457#ifdef DEBUG_SAX
1458 if (name == NULL)
1459 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1460 else
1461 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1462#endif
1463
1464 /* Capture end position and add node */
1465 if (cur != NULL && ctxt->record_info) {
1466 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1467 node_info.end_line = ctxt->input->line;
1468 node_info.node = cur;
1469 xmlParserAddNodeInfo(ctxt, &node_info);
1470 }
1471 ctxt->nodemem = -1;
1472
1473 if (ctxt->validate && ctxt->wellFormed &&
1474 ctxt->myDoc && ctxt->myDoc->intSubset)
1475 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1476 cur);
1477
1478
1479 /*
1480 * end of parsing of this node.
1481 */
1482#ifdef DEBUG_SAX_TREE
1483 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1484#endif
1485 nodePop(ctxt);
1486}
1487
1488/**
1489 * reference:
1490 * @ctx: the user data (XML parser context)
1491 * @name: The entity name
1492 *
1493 * called when an entity reference is detected.
1494 */
1495void
1496reference(void *ctx, const xmlChar *name)
1497{
1498 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1499 xmlNodePtr ret;
1500
1501#ifdef DEBUG_SAX
1502 xmlGenericError(xmlGenericErrorContext,
1503 "SAX.reference(%s)\n", name);
1504#endif
1505 if (name[0] == '#')
1506 ret = xmlNewCharRef(ctxt->myDoc, name);
1507 else
1508 ret = xmlNewReference(ctxt->myDoc, name);
1509#ifdef DEBUG_SAX_TREE
1510 xmlGenericError(xmlGenericErrorContext,
1511 "add reference %s to %s \n", name, ctxt->node->name);
1512#endif
1513 xmlAddChild(ctxt->node, ret);
1514}
1515
1516/**
1517 * characters:
1518 * @ctx: the user data (XML parser context)
1519 * @ch: a xmlChar string
1520 * @len: the number of xmlChar
1521 *
1522 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001523 */
1524void
1525characters(void *ctx, const xmlChar *ch, int len)
1526{
1527 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1528 xmlNodePtr lastChild;
1529
1530#ifdef DEBUG_SAX
1531 xmlGenericError(xmlGenericErrorContext,
1532 "SAX.characters(%.30s, %d)\n", ch, len);
1533#endif
1534 /*
1535 * Handle the data if any. If there is no child
1536 * add it as content, otherwise if the last child is text,
1537 * concatenate it, else create a new node of type text.
1538 */
1539
1540 if (ctxt->node == NULL) {
1541#ifdef DEBUG_SAX_TREE
1542 xmlGenericError(xmlGenericErrorContext,
1543 "add chars: ctxt->node == NULL !\n");
1544#endif
1545 return;
1546 }
1547 lastChild = xmlGetLastChild(ctxt->node);
1548#ifdef DEBUG_SAX_TREE
1549 xmlGenericError(xmlGenericErrorContext,
1550 "add chars to %s \n", ctxt->node->name);
1551#endif
1552
1553 /*
1554 * Here we needed an accelerator mechanism in case of very large
1555 * elements. Use an attribute in the structure !!!
1556 */
1557 if (lastChild == NULL) {
1558 /* first node, first time */
1559 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001560 if (ctxt->node->children != NULL) {
1561 ctxt->nodelen = len;
1562 ctxt->nodemem = len + 1;
1563 }
Owen Taylor3473f882001-02-23 17:55:21 +00001564 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001565 int coalesceText = (lastChild != NULL) &&
1566 (lastChild->type == XML_TEXT_NODE) &&
1567 (lastChild->name == xmlStringText);
1568 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001569 /*
1570 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001571 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001572 * reallocate a new buffer, move data, append ch. Here
1573 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001574 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001575 */
1576 if (ctxt->nodelen + len >= ctxt->nodemem) {
1577 xmlChar *newbuf;
1578 int size;
1579
1580 size = ctxt->nodemem + len;
1581 size *= 2;
1582 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1583 if (newbuf == NULL) {
1584 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1585 ctxt->sax->error(ctxt->userData,
1586 "SAX.characters(): out of memory\n");
1587 return;
1588 }
1589 ctxt->nodemem = size;
1590 lastChild->content = newbuf;
1591 }
1592 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1593 ctxt->nodelen += len;
1594 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001595 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001596 xmlTextConcat(lastChild, ch, len);
1597 if (ctxt->node->children != NULL) {
1598 ctxt->nodelen = xmlStrlen(lastChild->content);
1599 ctxt->nodemem = ctxt->nodelen + 1;
1600 }
Owen Taylor3473f882001-02-23 17:55:21 +00001601 } else {
1602 /* Mixed content, first time */
1603 lastChild = xmlNewTextLen(ch, len);
1604 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001605 if (ctxt->node->children != NULL) {
1606 ctxt->nodelen = len;
1607 ctxt->nodemem = len + 1;
1608 }
Owen Taylor3473f882001-02-23 17:55:21 +00001609 }
1610 }
1611}
1612
1613/**
1614 * ignorableWhitespace:
1615 * @ctx: the user data (XML parser context)
1616 * @ch: a xmlChar string
1617 * @len: the number of xmlChar
1618 *
1619 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001620 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001621 */
1622void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001623ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001624{
1625 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1626#ifdef DEBUG_SAX
1627 xmlGenericError(xmlGenericErrorContext,
1628 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1629#endif
1630}
1631
1632/**
1633 * processingInstruction:
1634 * @ctx: the user data (XML parser context)
1635 * @target: the target name
1636 * @data: the PI data's
1637 *
1638 * A processing instruction has been parsed.
1639 */
1640void
1641processingInstruction(void *ctx, const xmlChar *target,
1642 const xmlChar *data)
1643{
1644 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1645 xmlNodePtr ret;
1646 xmlNodePtr parent = ctxt->node;
1647
1648#ifdef DEBUG_SAX
1649 xmlGenericError(xmlGenericErrorContext,
1650 "SAX.processingInstruction(%s, %s)\n", target, data);
1651#endif
1652
1653 ret = xmlNewPI(target, data);
1654 if (ret == NULL) return;
1655 parent = ctxt->node;
1656
1657 if (ctxt->inSubset == 1) {
1658 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1659 return;
1660 } else if (ctxt->inSubset == 2) {
1661 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1662 return;
1663 }
1664 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1665#ifdef DEBUG_SAX_TREE
1666 xmlGenericError(xmlGenericErrorContext,
1667 "Setting PI %s as root\n", target);
1668#endif
1669 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1670 return;
1671 }
1672 if (parent->type == XML_ELEMENT_NODE) {
1673#ifdef DEBUG_SAX_TREE
1674 xmlGenericError(xmlGenericErrorContext,
1675 "adding PI %s child to %s\n", target, parent->name);
1676#endif
1677 xmlAddChild(parent, ret);
1678 } else {
1679#ifdef DEBUG_SAX_TREE
1680 xmlGenericError(xmlGenericErrorContext,
1681 "adding PI %s sibling to ", target);
1682 xmlDebugDumpOneNode(stderr, parent, 0);
1683#endif
1684 xmlAddSibling(parent, ret);
1685 }
1686}
1687
1688/**
1689 * globalNamespace:
1690 * @ctx: the user data (XML parser context)
1691 * @href: the namespace associated URN
1692 * @prefix: the namespace prefix
1693 *
1694 * An old global namespace has been parsed.
1695 */
1696void
1697globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1698{
1699 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1700#ifdef DEBUG_SAX
1701 xmlGenericError(xmlGenericErrorContext,
1702 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1703#endif
1704 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1705}
1706
1707/**
1708 * setNamespace:
1709 * @ctx: the user data (XML parser context)
1710 * @name: the namespace prefix
1711 *
1712 * Set the current element namespace.
1713 */
1714
1715void
1716setNamespace(void *ctx, const xmlChar *name)
1717{
1718 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1719 xmlNsPtr ns;
1720 xmlNodePtr parent;
1721
1722#ifdef DEBUG_SAX
1723 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1724#endif
1725 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1726 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1727 if (ctxt->nodeNr >= 2) {
1728 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1729 if (parent != NULL)
1730 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1731 }
1732 }
1733 xmlSetNs(ctxt->node, ns);
1734}
1735
1736/**
1737 * getNamespace:
1738 * @ctx: the user data (XML parser context)
1739 *
1740 * Get the current element namespace.
1741 *
1742 * Returns the xmlNsPtr or NULL if none
1743 */
1744
1745xmlNsPtr
1746getNamespace(void *ctx)
1747{
1748 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1749 xmlNsPtr ret;
1750
1751#ifdef DEBUG_SAX
1752 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1753#endif
1754 ret = ctxt->node->ns;
1755 return(ret);
1756}
1757
1758/**
1759 * checkNamespace:
1760 * @ctx: the user data (XML parser context)
1761 * @namespace: the namespace to check against
1762 *
1763 * Check that the current element namespace is the same as the
1764 * one read upon parsing.
1765 *
1766 * Returns 1 if true 0 otherwise
1767 */
1768
1769int
1770checkNamespace(void *ctx, xmlChar *namespace)
1771{
1772 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1773 xmlNodePtr cur = ctxt->node;
1774
1775#ifdef DEBUG_SAX
1776 xmlGenericError(xmlGenericErrorContext,
1777 "SAX.checkNamespace(%s)\n", namespace);
1778#endif
1779
1780 /*
1781 * Check that the Name in the ETag is the same as in the STag.
1782 */
1783 if (namespace == NULL) {
1784 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1785 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1786 ctxt->sax->error(ctxt,
1787 "End tags for %s don't hold the namespace %s\n",
1788 cur->name, cur->ns->prefix);
1789 ctxt->wellFormed = 0;
1790 }
1791 } else {
1792 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1793 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1794 ctxt->sax->error(ctxt,
1795 "End tags %s holds a prefix %s not used by the open tag\n",
1796 cur->name, namespace);
1797 ctxt->wellFormed = 0;
1798 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1799 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1800 ctxt->sax->error(ctxt,
1801 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1802 cur->name, cur->ns->prefix, namespace);
1803 ctxt->wellFormed = 0;
1804 } else
1805 return(1);
1806 }
1807 return(0);
1808}
1809
1810/**
1811 * namespaceDecl:
1812 * @ctx: the user data (XML parser context)
1813 * @href: the namespace associated URN
1814 * @prefix: the namespace prefix
1815 *
1816 * A namespace has been parsed.
1817 */
1818void
1819namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1820{
1821 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1822#ifdef DEBUG_SAX
1823 if (prefix == NULL)
1824 xmlGenericError(xmlGenericErrorContext,
1825 "SAX.namespaceDecl(%s, NULL)\n", href);
1826 else
1827 xmlGenericError(xmlGenericErrorContext,
1828 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1829#endif
1830 xmlNewNs(ctxt->node, href, prefix);
1831}
1832
1833/**
1834 * comment:
1835 * @ctx: the user data (XML parser context)
1836 * @value: the comment content
1837 *
1838 * A comment has been parsed.
1839 */
1840void
1841comment(void *ctx, const xmlChar *value)
1842{
1843 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1844 xmlNodePtr ret;
1845 xmlNodePtr parent = ctxt->node;
1846
1847#ifdef DEBUG_SAX
1848 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1849#endif
1850 ret = xmlNewDocComment(ctxt->myDoc, value);
1851 if (ret == NULL) return;
1852
1853 if (ctxt->inSubset == 1) {
1854 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1855 return;
1856 } else if (ctxt->inSubset == 2) {
1857 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1858 return;
1859 }
1860 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1861#ifdef DEBUG_SAX_TREE
1862 xmlGenericError(xmlGenericErrorContext,
1863 "Setting comment as root\n");
1864#endif
1865 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1866 return;
1867 }
1868 if (parent->type == XML_ELEMENT_NODE) {
1869#ifdef DEBUG_SAX_TREE
1870 xmlGenericError(xmlGenericErrorContext,
1871 "adding comment child to %s\n", parent->name);
1872#endif
1873 xmlAddChild(parent, ret);
1874 } else {
1875#ifdef DEBUG_SAX_TREE
1876 xmlGenericError(xmlGenericErrorContext,
1877 "adding comment sibling to ");
1878 xmlDebugDumpOneNode(stderr, parent, 0);
1879#endif
1880 xmlAddSibling(parent, ret);
1881 }
1882}
1883
1884/**
1885 * cdataBlock:
1886 * @ctx: the user data (XML parser context)
1887 * @value: The pcdata content
1888 * @len: the block length
1889 *
1890 * called when a pcdata block has been parsed
1891 */
1892void
1893cdataBlock(void *ctx, const xmlChar *value, int len)
1894{
1895 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1896 xmlNodePtr ret, lastChild;
1897
1898#ifdef DEBUG_SAX
1899 xmlGenericError(xmlGenericErrorContext,
1900 "SAX.pcdata(%.10s, %d)\n", value, len);
1901#endif
1902 lastChild = xmlGetLastChild(ctxt->node);
1903#ifdef DEBUG_SAX_TREE
1904 xmlGenericError(xmlGenericErrorContext,
1905 "add chars to %s \n", ctxt->node->name);
1906#endif
1907 if ((lastChild != NULL) &&
1908 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1909 xmlTextConcat(lastChild, value, len);
1910 } else {
1911 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1912 xmlAddChild(ctxt->node, ret);
1913 }
1914}
1915
Daniel Veillardd0463562001-10-13 09:15:48 +00001916/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001917 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001918 * @hdlr: the SAX handler
1919 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001920 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001921 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001922 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001923void
1924initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1925{
1926 if(hdlr->initialized == 1)
1927 return;
1928
1929 hdlr->internalSubset = internalSubset;
1930 hdlr->externalSubset = externalSubset;
1931 hdlr->isStandalone = isStandalone;
1932 hdlr->hasInternalSubset = hasInternalSubset;
1933 hdlr->hasExternalSubset = hasExternalSubset;
1934 hdlr->resolveEntity = resolveEntity;
1935 hdlr->getEntity = getEntity;
1936 hdlr->getParameterEntity = getParameterEntity;
1937 hdlr->entityDecl = entityDecl;
1938 hdlr->attributeDecl = attributeDecl;
1939 hdlr->elementDecl = elementDecl;
1940 hdlr->notationDecl = notationDecl;
1941 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1942 hdlr->setDocumentLocator = setDocumentLocator;
1943 hdlr->startDocument = startDocument;
1944 hdlr->endDocument = endDocument;
1945 hdlr->startElement = startElement;
1946 hdlr->endElement = endElement;
1947 hdlr->reference = reference;
1948 hdlr->characters = characters;
1949 hdlr->cdataBlock = cdataBlock;
1950 hdlr->ignorableWhitespace = characters;
1951 hdlr->processingInstruction = processingInstruction;
1952 hdlr->comment = comment;
1953 /* if (xmlGetWarningsDefaultValue == 0) */
1954 if (warning == 0)
1955 hdlr->warning = NULL;
1956 else
1957 hdlr->warning = xmlParserWarning;
1958 hdlr->error = xmlParserError;
1959 hdlr->fatalError = xmlParserError;
1960
1961 hdlr->initialized = 1;
1962}
Owen Taylor3473f882001-02-23 17:55:21 +00001963
1964/**
1965 * xmlDefaultSAXHandlerInit:
1966 *
1967 * Initialize the default SAX handler
1968 */
1969void
1970xmlDefaultSAXHandlerInit(void)
1971{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001972 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001973}
1974
Daniel Veillardeae522a2001-04-23 13:41:34 +00001975#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001976
1977/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001978 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001979 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001980 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001981 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001982 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001983void
1984inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1985{
1986 if(hdlr->initialized == 1)
1987 return;
1988
1989 hdlr->internalSubset = internalSubset;
1990 hdlr->externalSubset = NULL;
1991 hdlr->isStandalone = NULL;
1992 hdlr->hasInternalSubset = NULL;
1993 hdlr->hasExternalSubset = NULL;
1994 hdlr->resolveEntity = NULL;
1995 hdlr->getEntity = getEntity;
1996 hdlr->getParameterEntity = NULL;
1997 hdlr->entityDecl = NULL;
1998 hdlr->attributeDecl = NULL;
1999 hdlr->elementDecl = NULL;
2000 hdlr->notationDecl = NULL;
2001 hdlr->unparsedEntityDecl = NULL;
2002 hdlr->setDocumentLocator = setDocumentLocator;
2003 hdlr->startDocument = startDocument;
2004 hdlr->endDocument = endDocument;
2005 hdlr->startElement = startElement;
2006 hdlr->endElement = endElement;
2007 hdlr->reference = NULL;
2008 hdlr->characters = characters;
2009 hdlr->cdataBlock = cdataBlock;
2010 hdlr->ignorableWhitespace = ignorableWhitespace;
2011 hdlr->processingInstruction = NULL;
2012 hdlr->comment = comment;
2013 hdlr->warning = xmlParserWarning;
2014 hdlr->error = xmlParserError;
2015 hdlr->fatalError = xmlParserError;
2016
2017 hdlr->initialized = 1;
2018}
Owen Taylor3473f882001-02-23 17:55:21 +00002019
2020/**
2021 * htmlDefaultSAXHandlerInit:
2022 *
2023 * Initialize the default SAX handler
2024 */
2025void
2026htmlDefaultSAXHandlerInit(void)
2027{
Daniel Veillardd0463562001-10-13 09:15:48 +00002028 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002029}
Daniel Veillardd0463562001-10-13 09:15:48 +00002030
Daniel Veillardeae522a2001-04-23 13:41:34 +00002031#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002032
Daniel Veillardeae522a2001-04-23 13:41:34 +00002033#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00002034
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002035/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00002036 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002037 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002038 *
2039 * Initialize the default DocBook SAX handler
2040 */
Daniel Veillardd0463562001-10-13 09:15:48 +00002041void
2042initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2043{
2044 if(hdlr->initialized == 1)
2045 return;
2046
2047 hdlr->internalSubset = internalSubset;
2048 hdlr->externalSubset = NULL;
2049 hdlr->isStandalone = isStandalone;
2050 hdlr->hasInternalSubset = hasInternalSubset;
2051 hdlr->hasExternalSubset = hasExternalSubset;
2052 hdlr->resolveEntity = resolveEntity;
2053 hdlr->getEntity = getEntity;
2054 hdlr->getParameterEntity = NULL;
2055 hdlr->entityDecl = entityDecl;
2056 hdlr->attributeDecl = NULL;
2057 hdlr->elementDecl = NULL;
2058 hdlr->notationDecl = NULL;
2059 hdlr->unparsedEntityDecl = NULL;
2060 hdlr->setDocumentLocator = setDocumentLocator;
2061 hdlr->startDocument = startDocument;
2062 hdlr->endDocument = endDocument;
2063 hdlr->startElement = startElement;
2064 hdlr->endElement = endElement;
2065 hdlr->reference = reference;
2066 hdlr->characters = characters;
2067 hdlr->cdataBlock = NULL;
2068 hdlr->ignorableWhitespace = ignorableWhitespace;
2069 hdlr->processingInstruction = NULL;
2070 hdlr->comment = comment;
2071 hdlr->warning = xmlParserWarning;
2072 hdlr->error = xmlParserError;
2073 hdlr->fatalError = xmlParserError;
2074
2075 hdlr->initialized = 1;
2076}
Owen Taylor3473f882001-02-23 17:55:21 +00002077
2078/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00002079 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00002080 *
2081 * Initialize the default SAX handler
2082 */
2083void
Daniel Veillardeae522a2001-04-23 13:41:34 +00002084docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00002085{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002086 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002087}
Daniel Veillardeae522a2001-04-23 13:41:34 +00002088
2089#endif /* LIBXML_DOCB_ENABLED */