blob: 0f2a029150187332a092cf8a2a2c641db3371ea9 [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;
875
Owen Taylor3473f882001-02-23 17:55:21 +0000876 if (value[0] != 0) {
877 xmlURIPtr uri;
878
879 uri = xmlParseURI((const char *)value);
880 if (uri == NULL) {
881 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
882 ctxt->sax->warning(ctxt->userData,
883 "nmlns: %s not a valid URI\n", value);
884 } else {
885 if (uri->scheme == NULL) {
886 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
887 ctxt->sax->warning(ctxt->userData,
Daniel Veillardecaba492002-12-30 10:55:29 +0000888 "xmlns: URI %s is not absolute\n", value);
Owen Taylor3473f882001-02-23 17:55:21 +0000889 }
890 xmlFreeURI(uri);
891 }
892 }
893
894 /* a default namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000895 nsret = xmlNewNs(ctxt->node, value, NULL);
896
897 /*
898 * Validate also for namespace decls, they are attributes from
899 * an XML-1.0 perspective
900 */
901 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
902 ctxt->myDoc && ctxt->myDoc->intSubset)
903 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
904 ctxt->node, prefix, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000905 if (name != NULL)
906 xmlFree(name);
907 if (nval != NULL)
908 xmlFree(nval);
909 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;
915
Daniel Veillardc0fef772002-03-01 16:16:31 +0000916 if (value[0] == 0) {
917 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
918 ctxt->sax->error(ctxt->userData,
919 "Empty namespace name for prefix %s\n", name);
920 }
Daniel Veillard7b4b2f92003-01-06 13:11:20 +0000921 if ((ctxt->pedantic != 0) && (value[0] != 0)) {
Daniel Veillardecaba492002-12-30 10:55:29 +0000922 xmlURIPtr uri;
923
924 uri = xmlParseURI((const char *)value);
925 if (uri == NULL) {
926 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
927 ctxt->sax->warning(ctxt->userData,
928 "xmlns:%s: %s not a valid URI\n", name, value);
929 } else {
930 if (uri->scheme == NULL) {
931 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
932 ctxt->sax->warning(ctxt->userData,
933 "xmlns:%s: URI %s is not absolute\n", name, value);
934 }
935 xmlFreeURI(uri);
936 }
937 }
938
Owen Taylor3473f882001-02-23 17:55:21 +0000939 /* a standard namespace definition */
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000940 nsret = xmlNewNs(ctxt->node, value, name);
Owen Taylor3473f882001-02-23 17:55:21 +0000941 xmlFree(ns);
Daniel Veillard90d68fb2002-09-26 16:10:21 +0000942 /*
943 * Validate also for namespace decls, they are attributes from
944 * an XML-1.0 perspective
945 */
946 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
947 ctxt->myDoc && ctxt->myDoc->intSubset)
948 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
Daniel Veillardfcc822e2003-02-24 17:52:08 +0000949 ctxt->node, name, nsret, value);
Owen Taylor3473f882001-02-23 17:55:21 +0000950 if (name != NULL)
951 xmlFree(name);
952 if (nval != NULL)
953 xmlFree(nval);
954 return;
955 }
956
Daniel Veillardde590ca2003-02-05 10:45:26 +0000957 if (ns != NULL) {
958 xmlAttrPtr prop;
Owen Taylor3473f882001-02-23 17:55:21 +0000959 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillardde590ca2003-02-05 10:45:26 +0000960
961 prop = ctxt->node->properties;
962 while (prop != NULL) {
963 if (prop->ns != NULL) {
964 if ((xmlStrEqual(name, prop->name)) &&
965 ((namespace == prop->ns) ||
966 (xmlStrEqual(namespace->href, prop->ns->href)))) {
967 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
968 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
969 ctxt->sax->error(ctxt->userData,
970 "Attribute %s in %s redefined\n",
971 name, namespace->href);
972 ctxt->wellFormed = 0;
973 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
974 goto error;
975 }
976 }
977 prop = prop->next;
978 }
979 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000980 namespace = NULL;
981 }
982
983 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
Daniel Veillard46de64e2002-05-29 08:21:33 +0000984 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000985
986 if (ret != NULL) {
987 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
988 xmlNodePtr tmp;
989
990 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
991 tmp = ret->children;
992 while (tmp != NULL) {
993 tmp->parent = (xmlNodePtr) ret;
994 if (tmp->next == NULL)
995 ret->last = tmp;
996 tmp = tmp->next;
997 }
998 } else if (value != NULL) {
999 ret->children = xmlNewDocText(ctxt->myDoc, value);
1000 ret->last = ret->children;
1001 if (ret->children != NULL)
1002 ret->children->parent = (xmlNodePtr) ret;
1003 }
1004 }
1005
1006 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1007 ctxt->myDoc && ctxt->myDoc->intSubset) {
1008
1009 /*
1010 * If we don't substitute entities, the validation should be
1011 * done on a value with replaced entities anyway.
1012 */
1013 if (!ctxt->replaceEntities) {
1014 xmlChar *val;
1015
1016 ctxt->depth++;
1017 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1018 0,0,0);
1019 ctxt->depth--;
Daniel Veillardc7612992002-02-17 22:47:37 +00001020
Owen Taylor3473f882001-02-23 17:55:21 +00001021 if (val == NULL)
1022 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1023 ctxt->myDoc, ctxt->node, ret, value);
1024 else {
Daniel Veillardc7612992002-02-17 22:47:37 +00001025 xmlChar *nvalnorm;
1026
1027 /*
1028 * Do the last stage of the attribute normalization
1029 * It need to be done twice ... it's an extra burden related
1030 * to the ability to keep references in attributes
1031 */
1032 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1033 ctxt->node, fullname, val);
1034 if (nvalnorm != NULL) {
1035 xmlFree(val);
1036 val = nvalnorm;
1037 }
1038
Owen Taylor3473f882001-02-23 17:55:21 +00001039 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1040 ctxt->myDoc, ctxt->node, ret, val);
1041 xmlFree(val);
1042 }
1043 } else {
1044 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1045 ctxt->node, ret, value);
1046 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00001047 } else if (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1048 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001049 /*
1050 * when validating, the ID registration is done at the attribute
1051 * validation level. Otherwise we have to do specific handling here.
1052 */
1053 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1054 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1055 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1056 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1057 }
1058
Daniel Veillardde590ca2003-02-05 10:45:26 +00001059error:
Owen Taylor3473f882001-02-23 17:55:21 +00001060 if (nval != NULL)
1061 xmlFree(nval);
Owen Taylor3473f882001-02-23 17:55:21 +00001062 if (ns != NULL)
1063 xmlFree(ns);
1064}
1065
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001066/**
1067 * attribute:
1068 * @ctx: the user data (XML parser context)
1069 * @fullname: The attribute name, including namespace prefix
1070 * @value: The attribute value
1071 *
1072 * Handle an attribute that has been read by the parser.
1073 * The default handling is to convert the attribute into an
1074 * DOM subtree and past it in a new xmlAttr element added to
1075 * the element.
1076 */
1077void
1078attribute(void *ctx, const xmlChar *fullname, const xmlChar *value)
1079{
1080 my_attribute(ctx, fullname, value, NULL);
1081}
1082
Daniel Veillard878eab02002-02-19 13:46:09 +00001083/*
1084 * xmlCheckDefaultedAttributes:
1085 *
1086 * Check defaulted attributes from the DTD
1087 */
1088static void
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001089xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
Daniel Veillard878eab02002-02-19 13:46:09 +00001090 const xmlChar *prefix, const xmlChar **atts) {
1091 xmlElementPtr elemDecl;
1092 const xmlChar *att;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001093 int internal = 1;
Daniel Veillard878eab02002-02-19 13:46:09 +00001094 int i;
1095
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001096 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1097 if (elemDecl == NULL) {
1098 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1099 internal = 0;
1100 }
1101
1102process_external_subset:
1103
Daniel Veillard878eab02002-02-19 13:46:09 +00001104 if (elemDecl != NULL) {
1105 xmlAttributePtr attr = elemDecl->attributes;
1106 /*
1107 * Check against defaulted attributes from the external subset
1108 * if the document is stamped as standalone
1109 */
1110 if ((ctxt->myDoc->standalone == 1) &&
1111 (ctxt->myDoc->extSubset != NULL) &&
1112 (ctxt->validate)) {
1113 while (attr != NULL) {
1114 if ((attr->defaultValue != NULL) &&
1115 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1116 attr->elem, attr->name,
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001117 attr->prefix) == attr) &&
1118 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1119 attr->elem, attr->name,
1120 attr->prefix) == NULL)) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001121 xmlChar *fulln;
1122
1123 if (attr->prefix != NULL) {
1124 fulln = xmlStrdup(attr->prefix);
1125 fulln = xmlStrcat(fulln, BAD_CAST ":");
1126 fulln = xmlStrcat(fulln, attr->name);
1127 } else {
1128 fulln = xmlStrdup(attr->name);
1129 }
1130
1131 /*
1132 * Check that the attribute is not declared in the
1133 * serialization
1134 */
1135 att = NULL;
1136 if (atts != NULL) {
1137 i = 0;
1138 att = atts[i];
1139 while (att != NULL) {
1140 if (xmlStrEqual(att, fulln))
1141 break;
1142 i += 2;
1143 att = atts[i];
1144 }
1145 }
1146 if (att == NULL) {
1147 if (ctxt->vctxt.error != NULL)
1148 ctxt->vctxt.error(ctxt->vctxt.userData,
1149 "standalone: attribute %s on %s defaulted from external subset\n",
1150 fulln, attr->elem);
Daniel Veillard878eab02002-02-19 13:46:09 +00001151 ctxt->valid = 0;
Daniel Veillard878eab02002-02-19 13:46:09 +00001152 }
1153 }
1154 attr = attr->nexth;
1155 }
1156 }
1157
1158 /*
1159 * Actually insert defaulted values when needed
1160 */
1161 attr = elemDecl->attributes;
1162 while (attr != NULL) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001163 /*
1164 * Make sure that attributes redefinition occuring in the
1165 * internal subset are not overriden by definitions in the
1166 * external subset.
1167 */
Daniel Veillard8aff2472002-02-19 21:50:43 +00001168 if (attr->defaultValue != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001169 /*
1170 * the element should be instantiated in the tree if:
1171 * - this is a namespace prefix
1172 * - the user required for completion in the tree
1173 * like XSLT
Daniel Veillard8aff2472002-02-19 21:50:43 +00001174 * - there isn't already an attribute definition
1175 * in the internal subset overriding it.
Daniel Veillard878eab02002-02-19 13:46:09 +00001176 */
1177 if (((attr->prefix != NULL) &&
1178 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1179 ((attr->prefix == NULL) &&
1180 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1181 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
Daniel Veillard8aff2472002-02-19 21:50:43 +00001182 xmlAttributePtr tst;
Daniel Veillard878eab02002-02-19 13:46:09 +00001183
Daniel Veillard8aff2472002-02-19 21:50:43 +00001184 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1185 attr->elem, attr->name,
1186 attr->prefix);
1187 if ((tst == attr) || (tst == NULL)) {
1188 xmlChar *fulln;
Daniel Veillard878eab02002-02-19 13:46:09 +00001189
Daniel Veillard8aff2472002-02-19 21:50:43 +00001190 if (attr->prefix != NULL) {
1191 fulln = xmlStrdup(attr->prefix);
1192 fulln = xmlStrcat(fulln, BAD_CAST ":");
1193 fulln = xmlStrcat(fulln, attr->name);
1194 } else {
1195 fulln = xmlStrdup(attr->name);
Daniel Veillard878eab02002-02-19 13:46:09 +00001196 }
Daniel Veillard8aff2472002-02-19 21:50:43 +00001197
1198 /*
1199 * Check that the attribute is not declared in the
1200 * serialization
1201 */
1202 att = NULL;
1203 if (atts != NULL) {
1204 i = 0;
1205 att = atts[i];
1206 while (att != NULL) {
1207 if (xmlStrEqual(att, fulln))
1208 break;
1209 i += 2;
1210 att = atts[i];
1211 }
1212 }
1213 if (att == NULL) {
1214 attribute(ctxt, fulln, attr->defaultValue);
1215 }
1216 xmlFree(fulln);
Daniel Veillard878eab02002-02-19 13:46:09 +00001217 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001218 }
1219 }
1220 attr = attr->nexth;
1221 }
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001222 if (internal == 1) {
1223 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1224 name, prefix);
1225 internal = 0;
1226 goto process_external_subset;
1227 }
Daniel Veillard878eab02002-02-19 13:46:09 +00001228 }
1229}
1230
Owen Taylor3473f882001-02-23 17:55:21 +00001231/**
1232 * startElement:
1233 * @ctx: the user data (XML parser context)
1234 * @fullname: The element name, including namespace prefix
1235 * @atts: An array of name/value attributes pairs, NULL terminated
1236 *
1237 * called when an opening tag has been processed.
1238 */
1239void
1240startElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1241{
1242 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1243 xmlNodePtr ret;
1244 xmlNodePtr parent = ctxt->node;
1245 xmlNsPtr ns;
1246 xmlChar *name;
1247 xmlChar *prefix;
1248 const xmlChar *att;
1249 const xmlChar *value;
1250 int i;
1251
1252#ifdef DEBUG_SAX
1253 xmlGenericError(xmlGenericErrorContext,
1254 "SAX.startElement(%s)\n", fullname);
1255#endif
1256
1257 /*
1258 * First check on validity:
1259 */
1260 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1261 ((ctxt->myDoc->intSubset == NULL) ||
1262 ((ctxt->myDoc->intSubset->notations == NULL) &&
1263 (ctxt->myDoc->intSubset->elements == NULL) &&
1264 (ctxt->myDoc->intSubset->attributes == NULL) &&
1265 (ctxt->myDoc->intSubset->entities == NULL)))) {
1266 if (ctxt->vctxt.error != NULL) {
1267 ctxt->vctxt.error(ctxt->vctxt.userData,
1268 "Validation failed: no DTD found !\n");
1269 }
1270 ctxt->validate = 0;
Daniel Veillard7a51d6d2001-09-10 14:40:43 +00001271 ctxt->valid = 0;
1272 ctxt->errNo = XML_ERR_NO_DTD;
Owen Taylor3473f882001-02-23 17:55:21 +00001273 }
1274
1275
1276 /*
1277 * Split the full name into a namespace prefix and the tag name
1278 */
1279 name = xmlSplitQName(ctxt, fullname, &prefix);
1280
1281
1282 /*
1283 * Note : the namespace resolution is deferred until the end of the
1284 * attributes parsing, since local namespace can be defined as
1285 * an attribute at this level.
1286 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00001287 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001288 if (ret == NULL) return;
1289 if (ctxt->myDoc->children == NULL) {
1290#ifdef DEBUG_SAX_TREE
1291 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1292#endif
1293 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1294 } else if (parent == NULL) {
1295 parent = ctxt->myDoc->children;
1296 }
1297 ctxt->nodemem = -1;
Daniel Veillardd9bad132001-07-23 19:39:43 +00001298 if (ctxt->linenumbers) {
1299 if (ctxt->input != NULL)
1300 ret->content = (void *) (long) ctxt->input->line;
1301 }
Owen Taylor3473f882001-02-23 17:55:21 +00001302
1303 /*
1304 * We are parsing a new node.
1305 */
1306#ifdef DEBUG_SAX_TREE
1307 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1308#endif
1309 nodePush(ctxt, ret);
1310
1311 /*
1312 * Link the child element
1313 */
1314 if (parent != NULL) {
1315 if (parent->type == XML_ELEMENT_NODE) {
1316#ifdef DEBUG_SAX_TREE
1317 xmlGenericError(xmlGenericErrorContext,
1318 "adding child %s to %s\n", name, parent->name);
1319#endif
1320 xmlAddChild(parent, ret);
1321 } else {
1322#ifdef DEBUG_SAX_TREE
1323 xmlGenericError(xmlGenericErrorContext,
1324 "adding sibling %s to ", name);
1325 xmlDebugDumpOneNode(stderr, parent, 0);
1326#endif
1327 xmlAddSibling(parent, ret);
1328 }
1329 }
1330
1331 /*
Daniel Veillard48da9102001-08-07 01:10:10 +00001332 * Insert all the defaulted attributes from the DTD especially namespaces
1333 */
1334 if ((!ctxt->html) &&
1335 ((ctxt->myDoc->intSubset != NULL) ||
1336 (ctxt->myDoc->extSubset != NULL))) {
Daniel Veillard8dc16a62002-02-19 21:08:48 +00001337 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
Daniel Veillard48da9102001-08-07 01:10:10 +00001338 }
1339
1340 /*
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001341 * process all the attributes whose name start with "xmlns"
Owen Taylor3473f882001-02-23 17:55:21 +00001342 */
1343 if (atts != NULL) {
1344 i = 0;
1345 att = atts[i++];
1346 value = atts[i++];
1347 if (!ctxt->html) {
1348 while ((att != NULL) && (value != NULL)) {
Daniel Veillardd85f4f42002-03-25 10:48:46 +00001349 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1350 (att[3] == 'n') && (att[4] == 's'))
Daniel Veillard90d68fb2002-09-26 16:10:21 +00001351 my_attribute(ctxt, att, value, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001352
1353 att = atts[i++];
1354 value = atts[i++];
1355 }
1356 }
1357 }
1358
1359 /*
1360 * Search the namespace, note that since the attributes have been
1361 * processed, the local namespaces are available.
1362 */
1363 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1364 if ((ns == NULL) && (parent != NULL))
1365 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1366 if ((prefix != NULL) && (ns == NULL)) {
1367 ns = xmlNewNs(ret, NULL, prefix);
1368 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1369 ctxt->sax->warning(ctxt->userData,
1370 "Namespace prefix %s is not defined\n", prefix);
1371 }
Daniel Veillard143b04f2001-09-10 18:14:14 +00001372
1373 /*
1374 * set the namespace node, making sure that if the default namspace
1375 * is unbound on a parent we simply kee it NULL
1376 */
Daniel Veillardc0fef772002-03-01 16:16:31 +00001377 if ((ns != NULL) && (ns->href != NULL) &&
1378 ((ns->href[0] != 0) || (ns->prefix != NULL)))
Daniel Veillard143b04f2001-09-10 18:14:14 +00001379 xmlSetNs(ret, ns);
Owen Taylor3473f882001-02-23 17:55:21 +00001380
1381 /*
1382 * process all the other attributes
1383 */
1384 if (atts != NULL) {
1385 i = 0;
1386 att = atts[i++];
1387 value = atts[i++];
1388 if (ctxt->html) {
1389 while (att != NULL) {
1390 attribute(ctxt, att, value);
1391 att = atts[i++];
1392 value = atts[i++];
1393 }
1394 } else {
1395 while ((att != NULL) && (value != NULL)) {
Daniel Veillard6f4561a2002-03-25 12:10:14 +00001396 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1397 (att[3] != 'n') || (att[4] != 's'))
Owen Taylor3473f882001-02-23 17:55:21 +00001398 attribute(ctxt, att, value);
1399
1400 /*
1401 * Next ones
1402 */
1403 att = atts[i++];
1404 value = atts[i++];
1405 }
1406 }
1407 }
1408
1409 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001410 * If it's the Document root, finish the DTD validation and
Owen Taylor3473f882001-02-23 17:55:21 +00001411 * check the document root element for validity
1412 */
1413 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001414 int chk;
1415
1416 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1417 if (chk <= 0)
1418 ctxt->valid = 0;
1419 if (chk < 0)
1420 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001421 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1422 ctxt->vctxt.finishDtd = 1;
1423 }
1424
1425 if (prefix != NULL)
1426 xmlFree(prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00001427
1428}
1429
1430/**
1431 * endElement:
1432 * @ctx: the user data (XML parser context)
1433 * @name: The element name
1434 *
1435 * called when the end of an element has been detected.
1436 */
1437void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001438endElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001439{
1440 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1441 xmlParserNodeInfo node_info;
1442 xmlNodePtr cur = ctxt->node;
1443
1444#ifdef DEBUG_SAX
1445 if (name == NULL)
1446 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(NULL)\n");
1447 else
1448 xmlGenericError(xmlGenericErrorContext, "SAX.endElement(%s)\n", name);
1449#endif
1450
1451 /* Capture end position and add node */
1452 if (cur != NULL && ctxt->record_info) {
1453 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1454 node_info.end_line = ctxt->input->line;
1455 node_info.node = cur;
1456 xmlParserAddNodeInfo(ctxt, &node_info);
1457 }
1458 ctxt->nodemem = -1;
1459
1460 if (ctxt->validate && ctxt->wellFormed &&
1461 ctxt->myDoc && ctxt->myDoc->intSubset)
1462 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1463 cur);
1464
1465
1466 /*
1467 * end of parsing of this node.
1468 */
1469#ifdef DEBUG_SAX_TREE
1470 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1471#endif
1472 nodePop(ctxt);
1473}
1474
1475/**
1476 * reference:
1477 * @ctx: the user data (XML parser context)
1478 * @name: The entity name
1479 *
1480 * called when an entity reference is detected.
1481 */
1482void
1483reference(void *ctx, const xmlChar *name)
1484{
1485 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1486 xmlNodePtr ret;
1487
1488#ifdef DEBUG_SAX
1489 xmlGenericError(xmlGenericErrorContext,
1490 "SAX.reference(%s)\n", name);
1491#endif
1492 if (name[0] == '#')
1493 ret = xmlNewCharRef(ctxt->myDoc, name);
1494 else
1495 ret = xmlNewReference(ctxt->myDoc, name);
1496#ifdef DEBUG_SAX_TREE
1497 xmlGenericError(xmlGenericErrorContext,
1498 "add reference %s to %s \n", name, ctxt->node->name);
1499#endif
1500 xmlAddChild(ctxt->node, ret);
1501}
1502
1503/**
1504 * characters:
1505 * @ctx: the user data (XML parser context)
1506 * @ch: a xmlChar string
1507 * @len: the number of xmlChar
1508 *
1509 * receiving some chars from the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001510 */
1511void
1512characters(void *ctx, const xmlChar *ch, int len)
1513{
1514 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1515 xmlNodePtr lastChild;
1516
1517#ifdef DEBUG_SAX
1518 xmlGenericError(xmlGenericErrorContext,
1519 "SAX.characters(%.30s, %d)\n", ch, len);
1520#endif
1521 /*
1522 * Handle the data if any. If there is no child
1523 * add it as content, otherwise if the last child is text,
1524 * concatenate it, else create a new node of type text.
1525 */
1526
1527 if (ctxt->node == NULL) {
1528#ifdef DEBUG_SAX_TREE
1529 xmlGenericError(xmlGenericErrorContext,
1530 "add chars: ctxt->node == NULL !\n");
1531#endif
1532 return;
1533 }
1534 lastChild = xmlGetLastChild(ctxt->node);
1535#ifdef DEBUG_SAX_TREE
1536 xmlGenericError(xmlGenericErrorContext,
1537 "add chars to %s \n", ctxt->node->name);
1538#endif
1539
1540 /*
1541 * Here we needed an accelerator mechanism in case of very large
1542 * elements. Use an attribute in the structure !!!
1543 */
1544 if (lastChild == NULL) {
1545 /* first node, first time */
1546 xmlNodeAddContentLen(ctxt->node, ch, len);
Owen Taylor3473f882001-02-23 17:55:21 +00001547 if (ctxt->node->children != NULL) {
1548 ctxt->nodelen = len;
1549 ctxt->nodemem = len + 1;
1550 }
Owen Taylor3473f882001-02-23 17:55:21 +00001551 } else {
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001552 int coalesceText = (lastChild != NULL) &&
1553 (lastChild->type == XML_TEXT_NODE) &&
1554 (lastChild->name == xmlStringText);
1555 if ((coalesceText) && (ctxt->nodemem != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001556 /*
1557 * The whole point of maintaining nodelen and nodemem,
Daniel Veillard60087f32001-10-10 09:45:09 +00001558 * xmlTextConcat is too costly, i.e. compute length,
Owen Taylor3473f882001-02-23 17:55:21 +00001559 * reallocate a new buffer, move data, append ch. Here
1560 * We try to minimaze realloc() uses and avoid copying
Daniel Veillard60087f32001-10-10 09:45:09 +00001561 * and recomputing length over and over.
Owen Taylor3473f882001-02-23 17:55:21 +00001562 */
1563 if (ctxt->nodelen + len >= ctxt->nodemem) {
1564 xmlChar *newbuf;
1565 int size;
1566
1567 size = ctxt->nodemem + len;
1568 size *= 2;
1569 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
1570 if (newbuf == NULL) {
1571 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1572 ctxt->sax->error(ctxt->userData,
1573 "SAX.characters(): out of memory\n");
1574 return;
1575 }
1576 ctxt->nodemem = size;
1577 lastChild->content = newbuf;
1578 }
1579 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
1580 ctxt->nodelen += len;
1581 lastChild->content[ctxt->nodelen] = 0;
Daniel Veillardf300b7e2001-08-13 10:43:15 +00001582 } else if (coalesceText) {
Daniel Veillard80f32572001-03-07 19:45:40 +00001583 xmlTextConcat(lastChild, ch, len);
1584 if (ctxt->node->children != NULL) {
1585 ctxt->nodelen = xmlStrlen(lastChild->content);
1586 ctxt->nodemem = ctxt->nodelen + 1;
1587 }
Owen Taylor3473f882001-02-23 17:55:21 +00001588 } else {
1589 /* Mixed content, first time */
1590 lastChild = xmlNewTextLen(ch, len);
1591 xmlAddChild(ctxt->node, lastChild);
Owen Taylor3473f882001-02-23 17:55:21 +00001592 if (ctxt->node->children != NULL) {
1593 ctxt->nodelen = len;
1594 ctxt->nodemem = len + 1;
1595 }
Owen Taylor3473f882001-02-23 17:55:21 +00001596 }
1597 }
1598}
1599
1600/**
1601 * ignorableWhitespace:
1602 * @ctx: the user data (XML parser context)
1603 * @ch: a xmlChar string
1604 * @len: the number of xmlChar
1605 *
1606 * receiving some ignorable whitespaces from the parser.
Daniel Veillard05c13a22001-09-09 08:38:09 +00001607 * UNUSED: by default the DOM building will use characters
Owen Taylor3473f882001-02-23 17:55:21 +00001608 */
1609void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001610ignorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
Owen Taylor3473f882001-02-23 17:55:21 +00001611{
1612 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1613#ifdef DEBUG_SAX
1614 xmlGenericError(xmlGenericErrorContext,
1615 "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
1616#endif
1617}
1618
1619/**
1620 * processingInstruction:
1621 * @ctx: the user data (XML parser context)
1622 * @target: the target name
1623 * @data: the PI data's
1624 *
1625 * A processing instruction has been parsed.
1626 */
1627void
1628processingInstruction(void *ctx, const xmlChar *target,
1629 const xmlChar *data)
1630{
1631 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1632 xmlNodePtr ret;
1633 xmlNodePtr parent = ctxt->node;
1634
1635#ifdef DEBUG_SAX
1636 xmlGenericError(xmlGenericErrorContext,
1637 "SAX.processingInstruction(%s, %s)\n", target, data);
1638#endif
1639
1640 ret = xmlNewPI(target, data);
1641 if (ret == NULL) return;
1642 parent = ctxt->node;
1643
1644 if (ctxt->inSubset == 1) {
1645 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1646 return;
1647 } else if (ctxt->inSubset == 2) {
1648 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1649 return;
1650 }
1651 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1652#ifdef DEBUG_SAX_TREE
1653 xmlGenericError(xmlGenericErrorContext,
1654 "Setting PI %s as root\n", target);
1655#endif
1656 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1657 return;
1658 }
1659 if (parent->type == XML_ELEMENT_NODE) {
1660#ifdef DEBUG_SAX_TREE
1661 xmlGenericError(xmlGenericErrorContext,
1662 "adding PI %s child to %s\n", target, parent->name);
1663#endif
1664 xmlAddChild(parent, ret);
1665 } else {
1666#ifdef DEBUG_SAX_TREE
1667 xmlGenericError(xmlGenericErrorContext,
1668 "adding PI %s sibling to ", target);
1669 xmlDebugDumpOneNode(stderr, parent, 0);
1670#endif
1671 xmlAddSibling(parent, ret);
1672 }
1673}
1674
1675/**
1676 * globalNamespace:
1677 * @ctx: the user data (XML parser context)
1678 * @href: the namespace associated URN
1679 * @prefix: the namespace prefix
1680 *
1681 * An old global namespace has been parsed.
1682 */
1683void
1684globalNamespace(void *ctx, const xmlChar *href, const xmlChar *prefix)
1685{
1686 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1687#ifdef DEBUG_SAX
1688 xmlGenericError(xmlGenericErrorContext,
1689 "SAX.globalNamespace(%s, %s)\n", href, prefix);
1690#endif
1691 xmlNewGlobalNs(ctxt->myDoc, href, prefix);
1692}
1693
1694/**
1695 * setNamespace:
1696 * @ctx: the user data (XML parser context)
1697 * @name: the namespace prefix
1698 *
1699 * Set the current element namespace.
1700 */
1701
1702void
1703setNamespace(void *ctx, const xmlChar *name)
1704{
1705 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1706 xmlNsPtr ns;
1707 xmlNodePtr parent;
1708
1709#ifdef DEBUG_SAX
1710 xmlGenericError(xmlGenericErrorContext, "SAX.setNamespace(%s)\n", name);
1711#endif
1712 ns = xmlSearchNs(ctxt->myDoc, ctxt->node, name);
1713 if (ns == NULL) { /* ctxt->node may not have a parent yet ! */
1714 if (ctxt->nodeNr >= 2) {
1715 parent = ctxt->nodeTab[ctxt->nodeNr - 2];
1716 if (parent != NULL)
1717 ns = xmlSearchNs(ctxt->myDoc, parent, name);
1718 }
1719 }
1720 xmlSetNs(ctxt->node, ns);
1721}
1722
1723/**
1724 * getNamespace:
1725 * @ctx: the user data (XML parser context)
1726 *
1727 * Get the current element namespace.
1728 *
1729 * Returns the xmlNsPtr or NULL if none
1730 */
1731
1732xmlNsPtr
1733getNamespace(void *ctx)
1734{
1735 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1736 xmlNsPtr ret;
1737
1738#ifdef DEBUG_SAX
1739 xmlGenericError(xmlGenericErrorContext, "SAX.getNamespace()\n");
1740#endif
1741 ret = ctxt->node->ns;
1742 return(ret);
1743}
1744
1745/**
1746 * checkNamespace:
1747 * @ctx: the user data (XML parser context)
1748 * @namespace: the namespace to check against
1749 *
1750 * Check that the current element namespace is the same as the
1751 * one read upon parsing.
1752 *
1753 * Returns 1 if true 0 otherwise
1754 */
1755
1756int
1757checkNamespace(void *ctx, xmlChar *namespace)
1758{
1759 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1760 xmlNodePtr cur = ctxt->node;
1761
1762#ifdef DEBUG_SAX
1763 xmlGenericError(xmlGenericErrorContext,
1764 "SAX.checkNamespace(%s)\n", namespace);
1765#endif
1766
1767 /*
1768 * Check that the Name in the ETag is the same as in the STag.
1769 */
1770 if (namespace == NULL) {
1771 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
1772 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1773 ctxt->sax->error(ctxt,
1774 "End tags for %s don't hold the namespace %s\n",
1775 cur->name, cur->ns->prefix);
1776 ctxt->wellFormed = 0;
1777 }
1778 } else {
1779 if ((cur->ns == NULL) || (cur->ns->prefix == NULL)) {
1780 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1781 ctxt->sax->error(ctxt,
1782 "End tags %s holds a prefix %s not used by the open tag\n",
1783 cur->name, namespace);
1784 ctxt->wellFormed = 0;
1785 } else if (!xmlStrEqual(namespace, cur->ns->prefix)) {
1786 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1787 ctxt->sax->error(ctxt,
1788 "Start and End tags for %s don't use the same namespaces: %s and %s\n",
1789 cur->name, cur->ns->prefix, namespace);
1790 ctxt->wellFormed = 0;
1791 } else
1792 return(1);
1793 }
1794 return(0);
1795}
1796
1797/**
1798 * namespaceDecl:
1799 * @ctx: the user data (XML parser context)
1800 * @href: the namespace associated URN
1801 * @prefix: the namespace prefix
1802 *
1803 * A namespace has been parsed.
1804 */
1805void
1806namespaceDecl(void *ctx, const xmlChar *href, const xmlChar *prefix)
1807{
1808 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1809#ifdef DEBUG_SAX
1810 if (prefix == NULL)
1811 xmlGenericError(xmlGenericErrorContext,
1812 "SAX.namespaceDecl(%s, NULL)\n", href);
1813 else
1814 xmlGenericError(xmlGenericErrorContext,
1815 "SAX.namespaceDecl(%s, %s)\n", href, prefix);
1816#endif
1817 xmlNewNs(ctxt->node, href, prefix);
1818}
1819
1820/**
1821 * comment:
1822 * @ctx: the user data (XML parser context)
1823 * @value: the comment content
1824 *
1825 * A comment has been parsed.
1826 */
1827void
1828comment(void *ctx, const xmlChar *value)
1829{
1830 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1831 xmlNodePtr ret;
1832 xmlNodePtr parent = ctxt->node;
1833
1834#ifdef DEBUG_SAX
1835 xmlGenericError(xmlGenericErrorContext, "SAX.comment(%s)\n", value);
1836#endif
1837 ret = xmlNewDocComment(ctxt->myDoc, value);
1838 if (ret == NULL) return;
1839
1840 if (ctxt->inSubset == 1) {
1841 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
1842 return;
1843 } else if (ctxt->inSubset == 2) {
1844 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
1845 return;
1846 }
1847 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
1848#ifdef DEBUG_SAX_TREE
1849 xmlGenericError(xmlGenericErrorContext,
1850 "Setting comment as root\n");
1851#endif
1852 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1853 return;
1854 }
1855 if (parent->type == XML_ELEMENT_NODE) {
1856#ifdef DEBUG_SAX_TREE
1857 xmlGenericError(xmlGenericErrorContext,
1858 "adding comment child to %s\n", parent->name);
1859#endif
1860 xmlAddChild(parent, ret);
1861 } else {
1862#ifdef DEBUG_SAX_TREE
1863 xmlGenericError(xmlGenericErrorContext,
1864 "adding comment sibling to ");
1865 xmlDebugDumpOneNode(stderr, parent, 0);
1866#endif
1867 xmlAddSibling(parent, ret);
1868 }
1869}
1870
1871/**
1872 * cdataBlock:
1873 * @ctx: the user data (XML parser context)
1874 * @value: The pcdata content
1875 * @len: the block length
1876 *
1877 * called when a pcdata block has been parsed
1878 */
1879void
1880cdataBlock(void *ctx, const xmlChar *value, int len)
1881{
1882 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1883 xmlNodePtr ret, lastChild;
1884
1885#ifdef DEBUG_SAX
1886 xmlGenericError(xmlGenericErrorContext,
1887 "SAX.pcdata(%.10s, %d)\n", value, len);
1888#endif
1889 lastChild = xmlGetLastChild(ctxt->node);
1890#ifdef DEBUG_SAX_TREE
1891 xmlGenericError(xmlGenericErrorContext,
1892 "add chars to %s \n", ctxt->node->name);
1893#endif
1894 if ((lastChild != NULL) &&
1895 (lastChild->type == XML_CDATA_SECTION_NODE)) {
1896 xmlTextConcat(lastChild, value, len);
1897 } else {
1898 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
1899 xmlAddChild(ctxt->node, ret);
1900 }
1901}
1902
Daniel Veillardd0463562001-10-13 09:15:48 +00001903/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001904 * initxmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001905 * @hdlr: the SAX handler
1906 * @warning: flag if non-zero sets the handler warning procedure
Daniel Veillardd0463562001-10-13 09:15:48 +00001907 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001908 * Initialize the default XML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001909 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001910void
1911initxmlDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
1912{
1913 if(hdlr->initialized == 1)
1914 return;
1915
1916 hdlr->internalSubset = internalSubset;
1917 hdlr->externalSubset = externalSubset;
1918 hdlr->isStandalone = isStandalone;
1919 hdlr->hasInternalSubset = hasInternalSubset;
1920 hdlr->hasExternalSubset = hasExternalSubset;
1921 hdlr->resolveEntity = resolveEntity;
1922 hdlr->getEntity = getEntity;
1923 hdlr->getParameterEntity = getParameterEntity;
1924 hdlr->entityDecl = entityDecl;
1925 hdlr->attributeDecl = attributeDecl;
1926 hdlr->elementDecl = elementDecl;
1927 hdlr->notationDecl = notationDecl;
1928 hdlr->unparsedEntityDecl = unparsedEntityDecl;
1929 hdlr->setDocumentLocator = setDocumentLocator;
1930 hdlr->startDocument = startDocument;
1931 hdlr->endDocument = endDocument;
1932 hdlr->startElement = startElement;
1933 hdlr->endElement = endElement;
1934 hdlr->reference = reference;
1935 hdlr->characters = characters;
1936 hdlr->cdataBlock = cdataBlock;
1937 hdlr->ignorableWhitespace = characters;
1938 hdlr->processingInstruction = processingInstruction;
1939 hdlr->comment = comment;
1940 /* if (xmlGetWarningsDefaultValue == 0) */
1941 if (warning == 0)
1942 hdlr->warning = NULL;
1943 else
1944 hdlr->warning = xmlParserWarning;
1945 hdlr->error = xmlParserError;
1946 hdlr->fatalError = xmlParserError;
1947
1948 hdlr->initialized = 1;
1949}
Owen Taylor3473f882001-02-23 17:55:21 +00001950
1951/**
1952 * xmlDefaultSAXHandlerInit:
1953 *
1954 * Initialize the default SAX handler
1955 */
1956void
1957xmlDefaultSAXHandlerInit(void)
1958{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00001959 initxmlDefaultSAXHandler(&xmlDefaultSAXHandler, xmlGetWarningsDefaultValue);
Owen Taylor3473f882001-02-23 17:55:21 +00001960}
1961
Daniel Veillardeae522a2001-04-23 13:41:34 +00001962#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00001963
1964/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00001965 * inithtmlDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001966 * @hdlr: the SAX handler
Daniel Veillardd0463562001-10-13 09:15:48 +00001967 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001968 * Initialize the default HTML SAX handler
Owen Taylor3473f882001-02-23 17:55:21 +00001969 */
Daniel Veillardd0463562001-10-13 09:15:48 +00001970void
1971inithtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
1972{
1973 if(hdlr->initialized == 1)
1974 return;
1975
1976 hdlr->internalSubset = internalSubset;
1977 hdlr->externalSubset = NULL;
1978 hdlr->isStandalone = NULL;
1979 hdlr->hasInternalSubset = NULL;
1980 hdlr->hasExternalSubset = NULL;
1981 hdlr->resolveEntity = NULL;
1982 hdlr->getEntity = getEntity;
1983 hdlr->getParameterEntity = NULL;
1984 hdlr->entityDecl = NULL;
1985 hdlr->attributeDecl = NULL;
1986 hdlr->elementDecl = NULL;
1987 hdlr->notationDecl = NULL;
1988 hdlr->unparsedEntityDecl = NULL;
1989 hdlr->setDocumentLocator = setDocumentLocator;
1990 hdlr->startDocument = startDocument;
1991 hdlr->endDocument = endDocument;
1992 hdlr->startElement = startElement;
1993 hdlr->endElement = endElement;
1994 hdlr->reference = NULL;
1995 hdlr->characters = characters;
1996 hdlr->cdataBlock = cdataBlock;
1997 hdlr->ignorableWhitespace = ignorableWhitespace;
1998 hdlr->processingInstruction = NULL;
1999 hdlr->comment = comment;
2000 hdlr->warning = xmlParserWarning;
2001 hdlr->error = xmlParserError;
2002 hdlr->fatalError = xmlParserError;
2003
2004 hdlr->initialized = 1;
2005}
Owen Taylor3473f882001-02-23 17:55:21 +00002006
2007/**
2008 * htmlDefaultSAXHandlerInit:
2009 *
2010 * Initialize the default SAX handler
2011 */
2012void
2013htmlDefaultSAXHandlerInit(void)
2014{
Daniel Veillardd0463562001-10-13 09:15:48 +00002015 inithtmlDefaultSAXHandler(&htmlDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002016}
Daniel Veillardd0463562001-10-13 09:15:48 +00002017
Daniel Veillardeae522a2001-04-23 13:41:34 +00002018#endif /* LIBXML_HTML_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002019
Daniel Veillardeae522a2001-04-23 13:41:34 +00002020#ifdef LIBXML_DOCB_ENABLED
Daniel Veillardd0463562001-10-13 09:15:48 +00002021
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002022/**
Daniel Veillard9d06d302002-01-22 18:15:52 +00002023 * initdocbDefaultSAXHandler:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002024 * @hdlr: the SAX handler
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002025 *
2026 * Initialize the default DocBook SAX handler
2027 */
Daniel Veillardd0463562001-10-13 09:15:48 +00002028void
2029initdocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2030{
2031 if(hdlr->initialized == 1)
2032 return;
2033
2034 hdlr->internalSubset = internalSubset;
2035 hdlr->externalSubset = NULL;
2036 hdlr->isStandalone = isStandalone;
2037 hdlr->hasInternalSubset = hasInternalSubset;
2038 hdlr->hasExternalSubset = hasExternalSubset;
2039 hdlr->resolveEntity = resolveEntity;
2040 hdlr->getEntity = getEntity;
2041 hdlr->getParameterEntity = NULL;
2042 hdlr->entityDecl = entityDecl;
2043 hdlr->attributeDecl = NULL;
2044 hdlr->elementDecl = NULL;
2045 hdlr->notationDecl = NULL;
2046 hdlr->unparsedEntityDecl = NULL;
2047 hdlr->setDocumentLocator = setDocumentLocator;
2048 hdlr->startDocument = startDocument;
2049 hdlr->endDocument = endDocument;
2050 hdlr->startElement = startElement;
2051 hdlr->endElement = endElement;
2052 hdlr->reference = reference;
2053 hdlr->characters = characters;
2054 hdlr->cdataBlock = NULL;
2055 hdlr->ignorableWhitespace = ignorableWhitespace;
2056 hdlr->processingInstruction = NULL;
2057 hdlr->comment = comment;
2058 hdlr->warning = xmlParserWarning;
2059 hdlr->error = xmlParserError;
2060 hdlr->fatalError = xmlParserError;
2061
2062 hdlr->initialized = 1;
2063}
Owen Taylor3473f882001-02-23 17:55:21 +00002064
2065/**
Daniel Veillardeae522a2001-04-23 13:41:34 +00002066 * docbDefaultSAXHandlerInit:
Owen Taylor3473f882001-02-23 17:55:21 +00002067 *
2068 * Initialize the default SAX handler
2069 */
2070void
Daniel Veillardeae522a2001-04-23 13:41:34 +00002071docbDefaultSAXHandlerInit(void)
Owen Taylor3473f882001-02-23 17:55:21 +00002072{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002073 initdocbDefaultSAXHandler(&docbDefaultSAXHandler);
Owen Taylor3473f882001-02-23 17:55:21 +00002074}
Daniel Veillardeae522a2001-04-23 13:41:34 +00002075
2076#endif /* LIBXML_DOCB_ENABLED */