blob: 1bcd841b794b6fa03b4adbed8678814a98e59a3a [file] [log] [blame]
Daniel Veillard1af9a412003-08-20 22:54:39 +00001/*
2 * SAX2.c : Default SAX2 handler to build a tree.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <daniel@veillard.com>
7 */
8
9
10#define IN_LIBXML
11#include "libxml.h"
12#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>
25#include <libxml/valid.h>
26#include <libxml/HTMLtree.h>
27#include <libxml/globals.h>
28
29/* #define DEBUG_SAX2 */
30/* #define DEBUG_SAX2_TREE */
31
32/**
Daniel Veillarde57ec792003-09-10 10:50:59 +000033 * TODO:
34 *
35 * macro to flag unimplemented blocks
36 * XML_CATALOG_PREFER user env to select between system/public prefered
37 * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
38 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
39 *> values "system" and "public". I have made the default be "system" to
40 *> match yours.
41 */
42#define TODO \
43 xmlGenericError(xmlGenericErrorContext, \
44 "Unimplemented block at %s:%d\n", \
45 __FILE__, __LINE__);
46
47/**
Daniel Veillard1af9a412003-08-20 22:54:39 +000048 * xmlSAX2GetPublicId:
49 * @ctx: the user data (XML parser context)
50 *
51 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
52 *
53 * Returns a xmlChar *
54 */
55const xmlChar *
56xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
57{
58 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
59 return(NULL);
60}
61
62/**
63 * xmlSAX2GetSystemId:
64 * @ctx: the user data (XML parser context)
65 *
66 * Provides the system ID, basically URL or filename e.g.
67 * http://www.sgmlsource.com/dtds/memo.dtd
68 *
69 * Returns a xmlChar *
70 */
71const xmlChar *
72xmlSAX2GetSystemId(void *ctx)
73{
74 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
75 return((const xmlChar *) ctxt->input->filename);
76}
77
78/**
79 * xmlSAX2GetLineNumber:
80 * @ctx: the user data (XML parser context)
81 *
82 * Provide the line number of the current parsing point.
83 *
84 * Returns an int
85 */
86int
87xmlSAX2GetLineNumber(void *ctx)
88{
89 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
90 return(ctxt->input->line);
91}
92
93/**
94 * xmlSAX2GetColumnNumber:
95 * @ctx: the user data (XML parser context)
96 *
97 * Provide the column number of the current parsing point.
98 *
99 * Returns an int
100 */
101int
102xmlSAX2GetColumnNumber(void *ctx)
103{
104 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
105 return(ctxt->input->col);
106}
107
108/**
109 * xmlSAX2IsStandalone:
110 * @ctx: the user data (XML parser context)
111 *
112 * Is this document tagged standalone ?
113 *
114 * Returns 1 if true
115 */
116int
117xmlSAX2IsStandalone(void *ctx)
118{
119 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
120 return(ctxt->myDoc->standalone == 1);
121}
122
123/**
124 * xmlSAX2HasInternalSubset:
125 * @ctx: the user data (XML parser context)
126 *
127 * Does this document has an internal subset
128 *
129 * Returns 1 if true
130 */
131int
132xmlSAX2HasInternalSubset(void *ctx)
133{
134 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
135 return(ctxt->myDoc->intSubset != NULL);
136}
137
138/**
139 * xmlSAX2HasExternalSubset:
140 * @ctx: the user data (XML parser context)
141 *
142 * Does this document has an external subset
143 *
144 * Returns 1 if true
145 */
146int
147xmlSAX2HasExternalSubset(void *ctx)
148{
149 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
150 return(ctxt->myDoc->extSubset != NULL);
151}
152
153/**
154 * xmlSAX2InternalSubset:
155 * @ctx: the user data (XML parser context)
156 * @name: the root element name
157 * @ExternalID: the external ID
158 * @SystemID: the SYSTEM ID (e.g. filename or URL)
159 *
160 * Callback on internal subset declaration.
161 */
162void
163xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
164 const xmlChar *ExternalID, const xmlChar *SystemID)
165{
166 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
167 xmlDtdPtr dtd;
168#ifdef DEBUG_SAX
169 xmlGenericError(xmlGenericErrorContext,
170 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
171 name, ExternalID, SystemID);
172#endif
173
174 if (ctxt->myDoc == NULL)
175 return;
176 dtd = xmlGetIntSubset(ctxt->myDoc);
177 if (dtd != NULL) {
178 if (ctxt->html)
179 return;
180 xmlUnlinkNode((xmlNodePtr) dtd);
181 xmlFreeDtd(dtd);
182 ctxt->myDoc->intSubset = NULL;
183 }
184 ctxt->myDoc->intSubset =
185 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
186}
187
188/**
189 * xmlSAX2ExternalSubset:
190 * @ctx: the user data (XML parser context)
191 * @name: the root element name
192 * @ExternalID: the external ID
193 * @SystemID: the SYSTEM ID (e.g. filename or URL)
194 *
195 * Callback on external subset declaration.
196 */
197void
198xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
199 const xmlChar *ExternalID, const xmlChar *SystemID)
200{
201 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
202#ifdef DEBUG_SAX
203 xmlGenericError(xmlGenericErrorContext,
204 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
205 name, ExternalID, SystemID);
206#endif
207 if (((ExternalID != NULL) || (SystemID != NULL)) &&
208 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
209 (ctxt->wellFormed && ctxt->myDoc))) {
210 /*
211 * Try to fetch and parse the external subset.
212 */
213 xmlParserInputPtr oldinput;
214 int oldinputNr;
215 int oldinputMax;
216 xmlParserInputPtr *oldinputTab;
217 xmlParserInputPtr input = NULL;
218 xmlCharEncoding enc;
219 int oldcharset;
220
221 /*
222 * Ask the Entity resolver to load the damn thing
223 */
224 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
225 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
226 SystemID);
227 if (input == NULL) {
228 return;
229 }
230
231 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
232
233 /*
234 * make sure we won't destroy the main document context
235 */
236 oldinput = ctxt->input;
237 oldinputNr = ctxt->inputNr;
238 oldinputMax = ctxt->inputMax;
239 oldinputTab = ctxt->inputTab;
240 oldcharset = ctxt->charset;
241
242 ctxt->inputTab = (xmlParserInputPtr *)
243 xmlMalloc(5 * sizeof(xmlParserInputPtr));
244 if (ctxt->inputTab == NULL) {
245 ctxt->errNo = XML_ERR_NO_MEMORY;
246 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
247 ctxt->sax->error(ctxt->userData,
248 "xmlSAX2ExternalSubset: out of memory\n");
249 ctxt->errNo = XML_ERR_NO_MEMORY;
250 ctxt->instate = XML_PARSER_EOF;
251 ctxt->disableSAX = 1;
252 ctxt->input = oldinput;
253 ctxt->inputNr = oldinputNr;
254 ctxt->inputMax = oldinputMax;
255 ctxt->inputTab = oldinputTab;
256 ctxt->charset = oldcharset;
257 return;
258 }
259 ctxt->inputNr = 0;
260 ctxt->inputMax = 5;
261 ctxt->input = NULL;
262 xmlPushInput(ctxt, input);
263
264 /*
265 * On the fly encoding conversion if needed
266 */
267 if (ctxt->input->length >= 4) {
268 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
269 xmlSwitchEncoding(ctxt, enc);
270 }
271
272 if (input->filename == NULL)
273 input->filename = (char *) xmlCanonicPath(SystemID);
274 input->line = 1;
275 input->col = 1;
276 input->base = ctxt->input->cur;
277 input->cur = ctxt->input->cur;
278 input->free = NULL;
279
280 /*
281 * let's parse that entity knowing it's an external subset.
282 */
283 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
284
285 /*
286 * Free up the external entities
287 */
288
289 while (ctxt->inputNr > 1)
290 xmlPopInput(ctxt);
291 xmlFreeInputStream(ctxt->input);
292 xmlFree(ctxt->inputTab);
293
294 /*
295 * Restore the parsing context of the main entity
296 */
297 ctxt->input = oldinput;
298 ctxt->inputNr = oldinputNr;
299 ctxt->inputMax = oldinputMax;
300 ctxt->inputTab = oldinputTab;
301 ctxt->charset = oldcharset;
302 /* ctxt->wellFormed = oldwellFormed; */
303 }
304}
305
306/**
307 * xmlSAX2ResolveEntity:
308 * @ctx: the user data (XML parser context)
309 * @publicId: The public ID of the entity
310 * @systemId: The system ID of the entity
311 *
312 * The entity loader, to control the loading of external entities,
313 * the application can either:
314 * - override this xmlSAX2ResolveEntity() callback in the SAX block
315 * - or better use the xmlSetExternalEntityLoader() function to
316 * set up it's own entity resolution routine
317 *
318 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
319 */
320xmlParserInputPtr
321xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
322{
323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
324 xmlParserInputPtr ret;
325 xmlChar *URI;
326 const char *base = NULL;
327
328 if (ctxt->input != NULL)
329 base = ctxt->input->filename;
330 if (base == NULL)
331 base = ctxt->directory;
332
333 URI = xmlBuildURI(systemId, (const xmlChar *) base);
334
335#ifdef DEBUG_SAX
336 xmlGenericError(xmlGenericErrorContext,
337 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
338#endif
339
340 ret = xmlLoadExternalEntity((const char *) URI,
341 (const char *) publicId, ctxt);
342 if (URI != NULL)
343 xmlFree(URI);
344 return(ret);
345}
346
347/**
348 * xmlSAX2GetEntity:
349 * @ctx: the user data (XML parser context)
350 * @name: The entity name
351 *
352 * Get an entity by name
353 *
354 * Returns the xmlEntityPtr if found.
355 */
356xmlEntityPtr
357xmlSAX2GetEntity(void *ctx, const xmlChar *name)
358{
359 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
360 xmlEntityPtr ret = NULL;
361
362#ifdef DEBUG_SAX
363 xmlGenericError(xmlGenericErrorContext,
364 "SAX.xmlSAX2GetEntity(%s)\n", name);
365#endif
366
367 if (ctxt->inSubset == 0) {
368 ret = xmlGetPredefinedEntity(name);
369 if (ret != NULL)
370 return(ret);
371 }
372 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
373 if (ctxt->inSubset == 2) {
374 ctxt->myDoc->standalone = 0;
375 ret = xmlGetDocEntity(ctxt->myDoc, name);
376 ctxt->myDoc->standalone = 1;
377 } else {
378 ret = xmlGetDocEntity(ctxt->myDoc, name);
379 if (ret == NULL) {
380 ctxt->myDoc->standalone = 0;
381 ret = xmlGetDocEntity(ctxt->myDoc, name);
382 if (ret != NULL) {
383 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000384 ctxt->sax->error(ctxt->userData,
385 "Entity(%s) document marked standalone but requires external subset\n",
Daniel Veillard1af9a412003-08-20 22:54:39 +0000386 name);
387 ctxt->valid = 0;
388 ctxt->wellFormed = 0;
389 }
390 ctxt->myDoc->standalone = 1;
391 }
392 }
393 } else {
394 ret = xmlGetDocEntity(ctxt->myDoc, name);
395 }
396 if ((ret != NULL) &&
397 ((ctxt->validate) || (ctxt->replaceEntities)) &&
398 (ret->children == NULL) &&
399 (ret->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
400 int val;
401
402 /*
403 * for validation purposes we really need to fetch and
404 * parse the external entity
405 */
406 xmlNodePtr children;
407
408 val = xmlParseCtxtExternalEntity(ctxt, ret->URI,
409 ret->ExternalID, &children);
410 if (val == 0) {
411 xmlAddChildList((xmlNodePtr) ret, children);
412 } else {
William M. Brack4811ba32003-09-06 18:02:53 +0000413 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
414 ctxt->sax->error(ctxt->userData,
415 "Failure to process entity %s\n", name);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000416 ctxt->wellFormed = 0;
417 ctxt->valid = 0;
418 ctxt->validate = 0;
419 return(NULL);
420 }
421 ret->owner = 1;
422 }
423 return(ret);
424}
425
426/**
427 * xmlSAX2GetParameterEntity:
428 * @ctx: the user data (XML parser context)
429 * @name: The entity name
430 *
431 * Get a parameter entity by name
432 *
433 * Returns the xmlEntityPtr if found.
434 */
435xmlEntityPtr
436xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
437{
438 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
439 xmlEntityPtr ret;
440
441#ifdef DEBUG_SAX
442 xmlGenericError(xmlGenericErrorContext,
443 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
444#endif
445
446 ret = xmlGetParameterEntity(ctxt->myDoc, name);
447 return(ret);
448}
449
450
451/**
452 * xmlSAX2EntityDecl:
453 * @ctx: the user data (XML parser context)
454 * @name: the entity name
455 * @type: the entity type
456 * @publicId: The public ID of the entity
457 * @systemId: The system ID of the entity
458 * @content: the entity value (without processing).
459 *
460 * An entity definition has been parsed
461 */
462void
463xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
464 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
465{
466 xmlEntityPtr ent;
467 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
468
469#ifdef DEBUG_SAX
470 xmlGenericError(xmlGenericErrorContext,
471 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
472 name, type, publicId, systemId, content);
473#endif
474 if (ctxt->inSubset == 1) {
475 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
476 systemId, content);
477 if ((ent == NULL) && (ctxt->pedantic) &&
478 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000479 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000480 "Entity(%s) already defined in the internal subset\n", name);
481 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
482 xmlChar *URI;
483 const char *base = NULL;
484
485 if (ctxt->input != NULL)
486 base = ctxt->input->filename;
487 if (base == NULL)
488 base = ctxt->directory;
489
490 URI = xmlBuildURI(systemId, (const xmlChar *) base);
491 ent->URI = URI;
492 }
493 } else if (ctxt->inSubset == 2) {
494 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
495 systemId, content);
496 if ((ent == NULL) && (ctxt->pedantic) &&
497 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000498 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000499 "Entity(%s) already defined in the external subset\n", name);
500 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
501 xmlChar *URI;
502 const char *base = NULL;
503
504 if (ctxt->input != NULL)
505 base = ctxt->input->filename;
506 if (base == NULL)
507 base = ctxt->directory;
508
509 URI = xmlBuildURI(systemId, (const xmlChar *) base);
510 ent->URI = URI;
511 }
512 } else {
513 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000514 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000515 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n", name);
516 }
517}
518
519/**
520 * xmlSAX2AttributeDecl:
521 * @ctx: the user data (XML parser context)
522 * @elem: the name of the element
523 * @fullname: the attribute name
524 * @type: the attribute type
525 * @def: the type of default value
526 * @defaultValue: the attribute default value
527 * @tree: the tree of enumerated value set
528 *
529 * An attribute definition has been parsed
530 */
531void
532xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
533 int type, int def, const xmlChar *defaultValue,
534 xmlEnumerationPtr tree)
535{
536 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
537 xmlAttributePtr attr;
538 xmlChar *name = NULL, *prefix = NULL;
539
540#ifdef DEBUG_SAX
541 xmlGenericError(xmlGenericErrorContext,
542 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
543 elem, fullname, type, def, defaultValue);
544#endif
Daniel Veillarde57ec792003-09-10 10:50:59 +0000545 /* TODO: optimize name/prefix allocation */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000546 name = xmlSplitQName(ctxt, fullname, &prefix);
547 ctxt->vctxt.valid = 1;
548 if (ctxt->inSubset == 1)
549 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
550 name, prefix, (xmlAttributeType) type,
551 (xmlAttributeDefault) def, defaultValue, tree);
552 else if (ctxt->inSubset == 2)
553 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
554 name, prefix, (xmlAttributeType) type,
555 (xmlAttributeDefault) def, defaultValue, tree);
556 else {
557 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000558 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000559 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n", name);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000560 xmlFreeEnumeration(tree);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000561 return;
562 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000563#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000564 if (ctxt->vctxt.valid == 0)
565 ctxt->valid = 0;
566 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
567 (ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset != NULL))
568 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
569 attr);
Daniel Veillard4432df22003-09-28 18:58:27 +0000570#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000571 if (prefix != NULL)
572 xmlFree(prefix);
573 if (name != NULL)
574 xmlFree(name);
575}
576
577/**
578 * xmlSAX2ElementDecl:
579 * @ctx: the user data (XML parser context)
580 * @name: the element name
581 * @type: the element type
582 * @content: the element value tree
583 *
584 * An element definition has been parsed
585 */
586void
587xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
588 xmlElementContentPtr content)
589{
590 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
591 xmlElementPtr elem = NULL;
592
593#ifdef DEBUG_SAX
594 xmlGenericError(xmlGenericErrorContext,
595 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
596#endif
597
598 if (ctxt->inSubset == 1)
599 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
600 name, (xmlElementTypeVal) type, content);
601 else if (ctxt->inSubset == 2)
602 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
603 name, (xmlElementTypeVal) type, content);
604 else {
605 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000606 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000607 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
608 name);
609 return;
610 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000611#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000612 if (elem == NULL)
613 ctxt->valid = 0;
614 if (ctxt->validate && ctxt->wellFormed &&
615 ctxt->myDoc && ctxt->myDoc->intSubset)
616 ctxt->valid &=
617 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
Daniel Veillard4432df22003-09-28 18:58:27 +0000618#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000619}
620
621/**
622 * xmlSAX2NotationDecl:
623 * @ctx: the user data (XML parser context)
624 * @name: The name of the notation
625 * @publicId: The public ID of the entity
626 * @systemId: The system ID of the entity
627 *
628 * What to do when a notation declaration has been parsed.
629 */
630void
631xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
632 const xmlChar *publicId, const xmlChar *systemId)
633{
634 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
635 xmlNotationPtr nota = NULL;
636
637#ifdef DEBUG_SAX
638 xmlGenericError(xmlGenericErrorContext,
639 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
640#endif
641
642 if ((publicId == NULL) && (systemId == NULL)) {
643 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000644 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000645 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n", name);
646 ctxt->valid = 0;
647 ctxt->wellFormed = 0;
648 return;
649 } else if (ctxt->inSubset == 1)
650 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
651 publicId, systemId);
652 else if (ctxt->inSubset == 2)
653 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
654 publicId, systemId);
655 else {
656 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000657 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000658 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n", name);
659 return;
660 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000661#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000662 if (nota == NULL) ctxt->valid = 0;
663 if (ctxt->validate && ctxt->wellFormed &&
664 ctxt->myDoc && ctxt->myDoc->intSubset)
665 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
666 nota);
Daniel Veillard4432df22003-09-28 18:58:27 +0000667#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000668}
669
670/**
671 * xmlSAX2UnparsedEntityDecl:
672 * @ctx: the user data (XML parser context)
673 * @name: The name of the entity
674 * @publicId: The public ID of the entity
675 * @systemId: The system ID of the entity
676 * @notationName: the name of the notation
677 *
678 * What to do when an unparsed entity declaration is parsed
679 */
680void
681xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
682 const xmlChar *publicId, const xmlChar *systemId,
683 const xmlChar *notationName)
684{
685 xmlEntityPtr ent;
686 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
687#ifdef DEBUG_SAX
688 xmlGenericError(xmlGenericErrorContext,
689 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
690 name, publicId, systemId, notationName);
691#endif
692 if (ctxt->inSubset == 1) {
693 ent = xmlAddDocEntity(ctxt->myDoc, name,
694 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
695 publicId, systemId, notationName);
696 if ((ent == NULL) && (ctxt->pedantic) &&
697 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000698 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000699 "Entity(%s) already defined in the internal subset\n", name);
700 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
701 xmlChar *URI;
702 const char *base = NULL;
703
704 if (ctxt->input != NULL)
705 base = ctxt->input->filename;
706 if (base == NULL)
707 base = ctxt->directory;
708
709 URI = xmlBuildURI(systemId, (const xmlChar *) base);
710 ent->URI = URI;
711 }
712 } else if (ctxt->inSubset == 2) {
713 ent = xmlAddDtdEntity(ctxt->myDoc, name,
714 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
715 publicId, systemId, notationName);
716 if ((ent == NULL) && (ctxt->pedantic) &&
717 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000718 ctxt->sax->warning(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000719 "Entity(%s) already defined in the external subset\n", name);
720 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
721 xmlChar *URI;
722 const char *base = NULL;
723
724 if (ctxt->input != NULL)
725 base = ctxt->input->filename;
726 if (base == NULL)
727 base = ctxt->directory;
728
729 URI = xmlBuildURI(systemId, (const xmlChar *) base);
730 ent->URI = URI;
731 }
732 } else {
733 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
William M. Brack4811ba32003-09-06 18:02:53 +0000734 ctxt->sax->error(ctxt->userData,
Daniel Veillard1af9a412003-08-20 22:54:39 +0000735 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n", name);
736 }
737}
738
739/**
740 * xmlSAX2SetDocumentLocator:
741 * @ctx: the user data (XML parser context)
742 * @loc: A SAX Locator
743 *
744 * Receive the document locator at startup, actually xmlDefaultSAXLocator
745 * Everything is available on the context, so this is useless in our case.
746 */
747void
748xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
749{
750 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
751#ifdef DEBUG_SAX
752 xmlGenericError(xmlGenericErrorContext,
753 "SAX.xmlSAX2SetDocumentLocator()\n");
754#endif
755}
756
757/**
758 * xmlSAX2StartDocument:
759 * @ctx: the user data (XML parser context)
760 *
761 * called when the document start being processed.
762 */
763void
764xmlSAX2StartDocument(void *ctx)
765{
766 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
767 xmlDocPtr doc;
768
769#ifdef DEBUG_SAX
770 xmlGenericError(xmlGenericErrorContext,
771 "SAX.xmlSAX2StartDocument()\n");
772#endif
773 if (ctxt->html) {
774#ifdef LIBXML_HTML_ENABLED
775 if (ctxt->myDoc == NULL)
776 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
777 if (ctxt->myDoc == NULL) {
778 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
779 ctxt->sax->error(ctxt->userData,
780 "SAX.xmlSAX2StartDocument(): out of memory\n");
781 ctxt->errNo = XML_ERR_NO_MEMORY;
782 ctxt->instate = XML_PARSER_EOF;
783 ctxt->disableSAX = 1;
784 return;
785 }
786#else
787 xmlGenericError(xmlGenericErrorContext,
788 "libxml2 built without HTML support\n");
789 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
790 ctxt->instate = XML_PARSER_EOF;
791 ctxt->disableSAX = 1;
792 return;
793#endif
794 } else {
795 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
796 if (doc != NULL) {
797 if (ctxt->encoding != NULL)
798 doc->encoding = xmlStrdup(ctxt->encoding);
799 else
800 doc->encoding = NULL;
801 doc->standalone = ctxt->standalone;
802 } else {
803 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
804 ctxt->sax->error(ctxt->userData,
805 "SAX.xmlSAX2StartDocument(): out of memory\n");
806 ctxt->errNo = XML_ERR_NO_MEMORY;
807 ctxt->instate = XML_PARSER_EOF;
808 ctxt->disableSAX = 1;
809 return;
810 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000811 if ((ctxt->dictNames) && (doc != NULL))
812 doc->dict = ctxt->dict;
Daniel Veillard1af9a412003-08-20 22:54:39 +0000813 }
814 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
815 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
816 ctxt->myDoc->URL = xmlCanonicPath((const xmlChar *) ctxt->input->filename);
817 if (ctxt->myDoc->URL == NULL)
818 ctxt->myDoc->URL = xmlStrdup((const xmlChar *) ctxt->input->filename);
819 }
820}
821
822/**
823 * xmlSAX2EndDocument:
824 * @ctx: the user data (XML parser context)
825 *
826 * called when the document end has been detected.
827 */
828void
829xmlSAX2EndDocument(void *ctx)
830{
831 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
832#ifdef DEBUG_SAX
833 xmlGenericError(xmlGenericErrorContext,
834 "SAX.xmlSAX2EndDocument()\n");
835#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000836#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000837 if (ctxt->validate && ctxt->wellFormed &&
838 ctxt->myDoc && ctxt->myDoc->intSubset)
839 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +0000840#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000841
842 /*
843 * Grab the encoding if it was added on-the-fly
844 */
845 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
846 (ctxt->myDoc->encoding == NULL)) {
847 ctxt->myDoc->encoding = ctxt->encoding;
848 ctxt->encoding = NULL;
849 }
850 if ((ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
851 (ctxt->myDoc->encoding == NULL)) {
852 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
853 }
854 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
855 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
856 ctxt->myDoc->charset = ctxt->charset;
857 }
858}
859
Daniel Veillard81273902003-09-30 00:43:48 +0000860#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED)
Daniel Veillard1af9a412003-08-20 22:54:39 +0000861/**
862 * xmlSAX2AttributeInternal:
863 * @ctx: the user data (XML parser context)
864 * @fullname: The attribute name, including namespace prefix
865 * @value: The attribute value
866 * @prefix: the prefix on the element node
867 *
868 * Handle an attribute that has been read by the parser.
869 * The default handling is to convert the attribute into an
870 * DOM subtree and past it in a new xmlAttr element added to
871 * the element.
872 */
873static void
874xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000875 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
Daniel Veillard1af9a412003-08-20 22:54:39 +0000876{
877 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
878 xmlAttrPtr ret;
879 xmlChar *name;
880 xmlChar *ns;
881 xmlChar *nval;
882 xmlNsPtr namespace;
883
884 /*
885 * Split the full name into a namespace prefix and the tag name
886 */
887 name = xmlSplitQName(ctxt, fullname, &ns);
888 if ((name != NULL) && (name[0] == 0)) {
889 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
890 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
891 ctxt->sax->error(ctxt->userData,
892 "invalid namespace declaration '%s'\n", fullname);
893 } else {
894 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
895 ctxt->sax->warning(ctxt->userData,
896 "Avoid attribute ending with ':' like '%s'\n", fullname);
897 }
898 if (ns != NULL)
899 xmlFree(ns);
900 ns = NULL;
901 xmlFree(name);
902 name = xmlStrdup(fullname);
903 }
904 if (name == NULL) {
905 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
906 ctxt->sax->error(ctxt->userData,
907 "SAX.xmlSAX2StartElement(): out of memory\n");
908 ctxt->errNo = XML_ERR_NO_MEMORY;
909 ctxt->instate = XML_PARSER_EOF;
910 ctxt->disableSAX = 1;
911 if (ns != NULL)
912 xmlFree(ns);
913 return;
914 }
915
Daniel Veillard4432df22003-09-28 18:58:27 +0000916#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000917 /*
918 * Do the last stage of the attribute normalization
919 * Needed for HTML too:
920 * http://www.w3.org/TR/html4/types.html#h-6.2
921 */
922 ctxt->vctxt.valid = 1;
923 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
924 ctxt->myDoc, ctxt->node,
925 fullname, value);
926 if (ctxt->vctxt.valid != 1) {
927 ctxt->valid = 0;
928 }
929 if (nval != NULL)
930 value = nval;
Daniel Veillard4432df22003-09-28 18:58:27 +0000931#else
932 nval = NULL;
933#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000934
935 /*
936 * Check whether it's a namespace definition
937 */
938 if ((!ctxt->html) && (ns == NULL) &&
939 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
940 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
941 xmlNsPtr nsret;
942 xmlChar *val;
943
944 if (!ctxt->replaceEntities) {
945 ctxt->depth++;
946 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
947 0,0,0);
948 ctxt->depth--;
949 } else {
950 val = (xmlChar *) value;
951 }
952
953 if (val[0] != 0) {
954 xmlURIPtr uri;
955
956 uri = xmlParseURI((const char *)val);
957 if (uri == NULL) {
958 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
959 ctxt->sax->warning(ctxt->userData,
William M. Brack4811ba32003-09-06 18:02:53 +0000960 "xmlns: %s not a valid URI\n", val);
Daniel Veillard1af9a412003-08-20 22:54:39 +0000961 } else {
962 if (uri->scheme == NULL) {
963 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
964 ctxt->sax->warning(ctxt->userData,
965 "xmlns: URI %s is not absolute\n", val);
966 }
967 xmlFreeURI(uri);
968 }
969 }
970
971 /* a default namespace definition */
972 nsret = xmlNewNs(ctxt->node, val, NULL);
973
Daniel Veillard4432df22003-09-28 18:58:27 +0000974#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +0000975 /*
976 * Validate also for namespace decls, they are attributes from
977 * an XML-1.0 perspective
978 */
979 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
980 ctxt->myDoc && ctxt->myDoc->intSubset)
981 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
982 ctxt->node, prefix, nsret, val);
Daniel Veillard4432df22003-09-28 18:58:27 +0000983#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +0000984 if (name != NULL)
985 xmlFree(name);
986 if (nval != NULL)
987 xmlFree(nval);
988 if (val != value)
989 xmlFree(val);
990 return;
991 }
992 if ((!ctxt->html) &&
993 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
994 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
995 xmlNsPtr nsret;
996 xmlChar *val;
997
998 if (!ctxt->replaceEntities) {
999 ctxt->depth++;
1000 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1001 0,0,0);
1002 ctxt->depth--;
1003 if (val == NULL) {
1004 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1005 ctxt->sax->error(ctxt->userData,
1006 "SAX.xmlSAX2StartElement(): out of memory\n");
1007 ctxt->errNo = XML_ERR_NO_MEMORY;
1008 ctxt->instate = XML_PARSER_EOF;
1009 ctxt->disableSAX = 1;
1010 xmlFree(ns);
1011 if (name != NULL)
1012 xmlFree(name);
1013 return;
1014 }
1015 } else {
1016 val = (xmlChar *) value;
1017 }
1018
1019 if (val[0] == 0) {
1020 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1021 ctxt->sax->error(ctxt->userData,
1022 "Empty namespace name for prefix %s\n", name);
1023 }
1024 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1025 xmlURIPtr uri;
1026
1027 uri = xmlParseURI((const char *)val);
1028 if (uri == NULL) {
1029 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1030 ctxt->sax->warning(ctxt->userData,
1031 "xmlns:%s: %s not a valid URI\n", name, value);
1032 } else {
1033 if (uri->scheme == NULL) {
1034 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1035 ctxt->sax->warning(ctxt->userData,
1036 "xmlns:%s: URI %s is not absolute\n", name, value);
1037 }
1038 xmlFreeURI(uri);
1039 }
1040 }
1041
1042 /* a standard namespace definition */
1043 nsret = xmlNewNs(ctxt->node, val, name);
1044 xmlFree(ns);
Daniel Veillard4432df22003-09-28 18:58:27 +00001045#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001046 /*
1047 * Validate also for namespace decls, they are attributes from
1048 * an XML-1.0 perspective
1049 */
1050 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1051 ctxt->myDoc && ctxt->myDoc->intSubset)
1052 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1053 ctxt->node, prefix, nsret, value);
Daniel Veillard4432df22003-09-28 18:58:27 +00001054#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001055 if (name != NULL)
1056 xmlFree(name);
1057 if (nval != NULL)
1058 xmlFree(nval);
1059 if (val != value)
1060 xmlFree(val);
1061 return;
1062 }
1063
1064 if (ns != NULL) {
1065 xmlAttrPtr prop;
1066 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
Daniel Veillard67906942003-08-28 21:13:25 +00001067 if (namespace == NULL) {
William M. Brack4811ba32003-09-06 18:02:53 +00001068 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1069 ctxt->sax->error(ctxt->userData,
1070 "Namespace prefix %s of attribute %s is not defined\n",
Daniel Veillard67906942003-08-28 21:13:25 +00001071 ns, name);
1072 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001073
1074 prop = ctxt->node->properties;
1075 while (prop != NULL) {
1076 if (prop->ns != NULL) {
1077 if ((xmlStrEqual(name, prop->name)) &&
1078 ((namespace == prop->ns) ||
1079 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1080 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
1081 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1082 ctxt->sax->error(ctxt->userData,
1083 "Attribute %s in %s redefined\n",
1084 name, namespace->href);
1085 ctxt->wellFormed = 0;
1086 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1087 goto error;
1088 }
1089 }
1090 prop = prop->next;
1091 }
1092 } else {
1093 namespace = NULL;
1094 }
1095
1096 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1097 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1098
1099 if (ret != NULL) {
1100 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1101 xmlNodePtr tmp;
1102
1103 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1104 tmp = ret->children;
1105 while (tmp != NULL) {
1106 tmp->parent = (xmlNodePtr) ret;
1107 if (tmp->next == NULL)
1108 ret->last = tmp;
1109 tmp = tmp->next;
1110 }
1111 } else if (value != NULL) {
1112 ret->children = xmlNewDocText(ctxt->myDoc, value);
1113 ret->last = ret->children;
1114 if (ret->children != NULL)
1115 ret->children->parent = (xmlNodePtr) ret;
1116 }
1117 }
1118
Daniel Veillard4432df22003-09-28 18:58:27 +00001119#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001120 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1121 ctxt->myDoc && ctxt->myDoc->intSubset) {
1122
1123 /*
1124 * If we don't substitute entities, the validation should be
1125 * done on a value with replaced entities anyway.
1126 */
1127 if (!ctxt->replaceEntities) {
1128 xmlChar *val;
1129
1130 ctxt->depth++;
1131 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1132 0,0,0);
1133 ctxt->depth--;
1134
1135 if (val == NULL)
1136 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1137 ctxt->myDoc, ctxt->node, ret, value);
1138 else {
1139 xmlChar *nvalnorm;
1140
1141 /*
1142 * Do the last stage of the attribute normalization
1143 * It need to be done twice ... it's an extra burden related
1144 * to the ability to keep xmlSAX2References in attributes
1145 */
1146 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1147 ctxt->node, fullname, val);
1148 if (nvalnorm != NULL) {
1149 xmlFree(val);
1150 val = nvalnorm;
1151 }
1152
1153 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1154 ctxt->myDoc, ctxt->node, ret, val);
1155 xmlFree(val);
1156 }
1157 } else {
1158 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1159 ctxt->node, ret, value);
1160 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001161 } else
1162#endif /* LIBXML_VALID_ENABLED */
1163 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillard1af9a412003-08-20 22:54:39 +00001164 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1165 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1166 /*
1167 * when validating, the ID registration is done at the attribute
1168 * validation level. Otherwise we have to do specific handling here.
1169 */
1170 if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1171 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1172 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1173 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1174 }
1175
1176error:
1177 if (nval != NULL)
1178 xmlFree(nval);
1179 if (ns != NULL)
1180 xmlFree(ns);
1181}
1182
Daniel Veillard1af9a412003-08-20 22:54:39 +00001183/*
1184 * xmlCheckDefaultedAttributes:
1185 *
1186 * Check defaulted attributes from the DTD
1187 */
1188static void
1189xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1190 const xmlChar *prefix, const xmlChar **atts) {
1191 xmlElementPtr elemDecl;
1192 const xmlChar *att;
1193 int internal = 1;
1194 int i;
1195
1196 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1197 if (elemDecl == NULL) {
1198 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1199 internal = 0;
1200 }
1201
1202process_external_subset:
1203
1204 if (elemDecl != NULL) {
1205 xmlAttributePtr attr = elemDecl->attributes;
1206 /*
1207 * Check against defaulted attributes from the external subset
1208 * if the document is stamped as standalone
1209 */
1210 if ((ctxt->myDoc->standalone == 1) &&
1211 (ctxt->myDoc->extSubset != NULL) &&
1212 (ctxt->validate)) {
1213 while (attr != NULL) {
1214 if ((attr->defaultValue != NULL) &&
1215 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1216 attr->elem, attr->name,
1217 attr->prefix) == attr) &&
1218 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1219 attr->elem, attr->name,
1220 attr->prefix) == NULL)) {
1221 xmlChar *fulln;
1222
1223 if (attr->prefix != NULL) {
1224 fulln = xmlStrdup(attr->prefix);
1225 fulln = xmlStrcat(fulln, BAD_CAST ":");
1226 fulln = xmlStrcat(fulln, attr->name);
1227 } else {
1228 fulln = xmlStrdup(attr->name);
1229 }
1230
1231 /*
1232 * Check that the attribute is not declared in the
1233 * serialization
1234 */
1235 att = NULL;
1236 if (atts != NULL) {
1237 i = 0;
1238 att = atts[i];
1239 while (att != NULL) {
1240 if (xmlStrEqual(att, fulln))
1241 break;
1242 i += 2;
1243 att = atts[i];
1244 }
1245 }
1246 if (att == NULL) {
1247 if (ctxt->vctxt.error != NULL)
1248 ctxt->vctxt.error(ctxt->vctxt.userData,
1249 "standalone: attribute %s on %s defaulted from external subset\n",
1250 fulln, attr->elem);
1251 ctxt->valid = 0;
1252 }
1253 }
1254 attr = attr->nexth;
1255 }
1256 }
1257
1258 /*
1259 * Actually insert defaulted values when needed
1260 */
1261 attr = elemDecl->attributes;
1262 while (attr != NULL) {
1263 /*
1264 * Make sure that attributes redefinition occuring in the
1265 * internal subset are not overriden by definitions in the
1266 * external subset.
1267 */
1268 if (attr->defaultValue != NULL) {
1269 /*
1270 * the element should be instantiated in the tree if:
1271 * - this is a namespace prefix
1272 * - the user required for completion in the tree
1273 * like XSLT
1274 * - there isn't already an attribute definition
1275 * in the internal subset overriding it.
1276 */
1277 if (((attr->prefix != NULL) &&
1278 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1279 ((attr->prefix == NULL) &&
1280 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1281 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1282 xmlAttributePtr tst;
1283
1284 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1285 attr->elem, attr->name,
1286 attr->prefix);
1287 if ((tst == attr) || (tst == NULL)) {
1288 xmlChar fn[50];
1289 xmlChar *fulln;
1290
1291 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1292 if (fulln == NULL) {
1293 if ((ctxt->sax != NULL) &&
1294 (ctxt->sax->error != NULL))
1295 ctxt->sax->error(ctxt->userData,
1296 "SAX.xmlSAX2StartElement(): out of memory\n");
1297 ctxt->errNo = XML_ERR_NO_MEMORY;
1298 ctxt->instate = XML_PARSER_EOF;
1299 ctxt->disableSAX = 1;
1300 return;
1301 }
1302
1303 /*
1304 * Check that the attribute is not declared in the
1305 * serialization
1306 */
1307 att = NULL;
1308 if (atts != NULL) {
1309 i = 0;
1310 att = atts[i];
1311 while (att != NULL) {
1312 if (xmlStrEqual(att, fulln))
1313 break;
1314 i += 2;
1315 att = atts[i];
1316 }
1317 }
1318 if (att == NULL) {
1319 xmlSAX2AttributeInternal(ctxt, fulln,
1320 attr->defaultValue, prefix);
1321 }
1322 if ((fulln != fn) && (fulln != attr->name))
1323 xmlFree(fulln);
1324 }
1325 }
1326 }
1327 attr = attr->nexth;
1328 }
1329 if (internal == 1) {
1330 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1331 name, prefix);
1332 internal = 0;
1333 goto process_external_subset;
1334 }
1335 }
1336}
1337
1338/**
1339 * xmlSAX2StartElement:
1340 * @ctx: the user data (XML parser context)
1341 * @fullname: The element name, including namespace prefix
1342 * @atts: An array of name/value attributes pairs, NULL terminated
1343 *
1344 * called when an opening tag has been processed.
1345 */
1346void
1347xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1348{
1349 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1350 xmlNodePtr ret;
1351 xmlNodePtr parent = ctxt->node;
1352 xmlNsPtr ns;
1353 xmlChar *name;
1354 xmlChar *prefix;
1355 const xmlChar *att;
1356 const xmlChar *value;
1357 int i;
1358
1359#ifdef DEBUG_SAX
1360 xmlGenericError(xmlGenericErrorContext,
1361 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1362#endif
1363
1364 /*
1365 * First check on validity:
1366 */
1367 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1368 ((ctxt->myDoc->intSubset == NULL) ||
1369 ((ctxt->myDoc->intSubset->notations == NULL) &&
1370 (ctxt->myDoc->intSubset->elements == NULL) &&
1371 (ctxt->myDoc->intSubset->attributes == NULL) &&
1372 (ctxt->myDoc->intSubset->entities == NULL)))) {
1373 if (ctxt->vctxt.error != NULL) {
1374 ctxt->vctxt.error(ctxt->vctxt.userData,
1375 "Validation failed: no DTD found !\n");
1376 }
1377 ctxt->validate = 0;
1378 ctxt->valid = 0;
1379 ctxt->errNo = XML_ERR_NO_DTD;
1380 }
1381
1382
1383 /*
1384 * Split the full name into a namespace prefix and the tag name
1385 */
1386 name = xmlSplitQName(ctxt, fullname, &prefix);
1387
1388
1389 /*
1390 * Note : the namespace resolution is deferred until the end of the
1391 * attributes parsing, since local namespace can be defined as
1392 * an attribute at this level.
1393 */
1394 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1395 if (ret == NULL) {
1396 if (prefix != NULL)
1397 xmlFree(prefix);
1398 ctxt->errNo = XML_ERR_NO_MEMORY;
1399 ctxt->instate = XML_PARSER_EOF;
1400 ctxt->disableSAX = 1;
1401 return;
1402 }
1403 if (ctxt->myDoc->children == NULL) {
1404#ifdef DEBUG_SAX_TREE
1405 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1406#endif
1407 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1408 } else if (parent == NULL) {
1409 parent = ctxt->myDoc->children;
1410 }
1411 ctxt->nodemem = -1;
1412 if (ctxt->linenumbers) {
1413 if (ctxt->input != NULL)
1414 ret->content = (void *) (long) ctxt->input->line;
1415 }
1416
1417 /*
1418 * We are parsing a new node.
1419 */
1420#ifdef DEBUG_SAX_TREE
1421 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1422#endif
1423 nodePush(ctxt, ret);
1424
1425 /*
1426 * Link the child element
1427 */
1428 if (parent != NULL) {
1429 if (parent->type == XML_ELEMENT_NODE) {
1430#ifdef DEBUG_SAX_TREE
1431 xmlGenericError(xmlGenericErrorContext,
1432 "adding child %s to %s\n", name, parent->name);
1433#endif
1434 xmlAddChild(parent, ret);
1435 } else {
1436#ifdef DEBUG_SAX_TREE
1437 xmlGenericError(xmlGenericErrorContext,
1438 "adding sibling %s to ", name);
1439 xmlDebugDumpOneNode(stderr, parent, 0);
1440#endif
1441 xmlAddSibling(parent, ret);
1442 }
1443 }
1444
1445 /*
1446 * Insert all the defaulted attributes from the DTD especially namespaces
1447 */
1448 if ((!ctxt->html) &&
1449 ((ctxt->myDoc->intSubset != NULL) ||
1450 (ctxt->myDoc->extSubset != NULL))) {
1451 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1452 }
1453
1454 /*
1455 * process all the attributes whose name start with "xmlns"
1456 */
1457 if (atts != NULL) {
1458 i = 0;
1459 att = atts[i++];
1460 value = atts[i++];
1461 if (!ctxt->html) {
1462 while ((att != NULL) && (value != NULL)) {
1463 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1464 (att[3] == 'n') && (att[4] == 's'))
1465 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1466
1467 att = atts[i++];
1468 value = atts[i++];
1469 }
1470 }
1471 }
1472
1473 /*
1474 * Search the namespace, note that since the attributes have been
1475 * processed, the local namespaces are available.
1476 */
1477 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1478 if ((ns == NULL) && (parent != NULL))
1479 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1480 if ((prefix != NULL) && (ns == NULL)) {
1481 ns = xmlNewNs(ret, NULL, prefix);
1482 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1483 ctxt->sax->warning(ctxt->userData,
1484 "Namespace prefix %s is not defined\n", prefix);
1485 }
1486
1487 /*
1488 * set the namespace node, making sure that if the default namspace
1489 * is unbound on a parent we simply kee it NULL
1490 */
1491 if ((ns != NULL) && (ns->href != NULL) &&
1492 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1493 xmlSetNs(ret, ns);
1494
1495 /*
1496 * process all the other attributes
1497 */
1498 if (atts != NULL) {
1499 i = 0;
1500 att = atts[i++];
1501 value = atts[i++];
1502 if (ctxt->html) {
1503 while (att != NULL) {
1504 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1505 att = atts[i++];
1506 value = atts[i++];
1507 }
1508 } else {
1509 while ((att != NULL) && (value != NULL)) {
1510 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1511 (att[3] != 'n') || (att[4] != 's'))
1512 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1513
1514 /*
1515 * Next ones
1516 */
1517 att = atts[i++];
1518 value = atts[i++];
1519 }
1520 }
1521 }
1522
Daniel Veillard4432df22003-09-28 18:58:27 +00001523#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001524 /*
1525 * If it's the Document root, finish the DTD validation and
1526 * check the document root element for validity
1527 */
1528 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
1529 int chk;
1530
1531 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1532 if (chk <= 0)
1533 ctxt->valid = 0;
1534 if (chk < 0)
1535 ctxt->wellFormed = 0;
1536 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1537 ctxt->vctxt.finishDtd = 1;
1538 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001539#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001540
1541 if (prefix != NULL)
1542 xmlFree(prefix);
1543
1544}
1545
1546/**
1547 * xmlSAX2EndElement:
1548 * @ctx: the user data (XML parser context)
1549 * @name: The element name
1550 *
1551 * called when the end of an element has been detected.
1552 */
1553void
1554xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1555{
1556 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1557 xmlParserNodeInfo node_info;
1558 xmlNodePtr cur = ctxt->node;
1559
1560#ifdef DEBUG_SAX
1561 if (name == NULL)
1562 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1563 else
1564 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1565#endif
1566
1567 /* Capture end position and add node */
1568 if (cur != NULL && ctxt->record_info) {
1569 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1570 node_info.end_line = ctxt->input->line;
1571 node_info.node = cur;
1572 xmlParserAddNodeInfo(ctxt, &node_info);
1573 }
1574 ctxt->nodemem = -1;
1575
Daniel Veillard4432df22003-09-28 18:58:27 +00001576#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001577 if (ctxt->validate && ctxt->wellFormed &&
1578 ctxt->myDoc && ctxt->myDoc->intSubset)
1579 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1580 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001581#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001582
1583
1584 /*
1585 * end of parsing of this node.
1586 */
1587#ifdef DEBUG_SAX_TREE
1588 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1589#endif
1590 nodePop(ctxt);
1591}
Daniel Veillard81273902003-09-30 00:43:48 +00001592#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001593
Daniel Veillarde57ec792003-09-10 10:50:59 +00001594/*
Daniel Veillard19895052003-09-17 13:59:32 +00001595 * xmlSAX2TextNode:
1596 * @ctxt: the parser context
1597 * @str: the input string
1598 * @len: the string length
1599 *
1600 * Remove the entities from an attribute value
1601 *
1602 * Returns the newly allocated string or NULL if not needed or error
1603 */
1604static xmlNodePtr
1605xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1606 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001607 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001608
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001609 /*
1610 * Allocate
1611 */
Daniel Veillard19895052003-09-17 13:59:32 +00001612 if (ctxt->freeElems != NULL) {
1613 ret = ctxt->freeElems;
1614 ctxt->freeElems = ret->next;
1615 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001616 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001617 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001618 }
1619 if (ret == NULL) {
1620 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1621 ctxt->sax->error(ctxt->userData,
1622 "SAX.xmlSAX2Characters(): out of memory\n");
1623 ctxt->errNo = XML_ERR_NO_MEMORY;
1624 ctxt->instate = XML_PARSER_EOF;
1625 ctxt->disableSAX = 1;
1626 return(NULL);
1627 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001628 /*
1629 * intern the formatting blanks found between tags, or the
1630 * very short strings
1631 */
1632 if (ctxt->dictNames) {
1633 xmlChar cur = str[len];
1634
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001635 if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1636 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001637 intern = xmlDictLookup(ctxt->dict, str, len);
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001638 } else if (IS_BLANK(*str) && (len < 60) && (cur == '<') &&
1639 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001640 int i;
1641
1642 for (i = 1;i < len;i++) {
1643 if (!IS_BLANK(*str)) goto skip;
1644 }
1645 intern = xmlDictLookup(ctxt->dict, str, len);
1646 }
1647 }
1648skip:
1649 memset(ret, 0, sizeof(xmlNode));
1650 ret->type = XML_TEXT_NODE;
1651
1652 ret->name = xmlStringText;
1653 if (intern == NULL)
1654 ret->content = xmlStrndup(str, len);
1655 else
1656 ret->content = (xmlChar *) intern;
1657
1658 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1659 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001660 return(ret);
1661}
1662
Daniel Veillard4432df22003-09-28 18:58:27 +00001663#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001664/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001665 * xmlSAX2DecodeAttrEntities:
1666 * @ctxt: the parser context
1667 * @str: the input string
1668 * @len: the string length
1669 *
1670 * Remove the entities from an attribute value
1671 *
1672 * Returns the newly allocated string or NULL if not needed or error
1673 */
1674static xmlChar *
1675xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1676 const xmlChar *end) {
1677 const xmlChar *in;
1678 xmlChar *ret;
1679
1680 in = str;
1681 while (in < end)
1682 if (*in++ == '&')
1683 goto decode;
1684 return(NULL);
1685decode:
1686 ctxt->depth++;
1687 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1688 XML_SUBSTITUTE_REF, 0,0,0);
1689 ctxt->depth--;
1690 return(ret);
1691}
Daniel Veillard4432df22003-09-28 18:58:27 +00001692#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001693
1694/**
1695 * xmlSAX2AttributeNs:
1696 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001697 * @localname: the local name of the attribute
1698 * @prefix: the attribute namespace prefix if available
1699 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001700 * @value: Start of the attribute value
1701 * @valueend: end of the attribute value
1702 *
1703 * Handle an attribute that has been read by the parser.
1704 * The default handling is to convert the attribute into an
1705 * DOM subtree and past it in a new xmlAttr element added to
1706 * the element.
1707 */
1708static void
1709xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1710 const xmlChar * localname,
1711 const xmlChar * prefix,
1712 const xmlChar * value,
1713 const xmlChar * valueend)
1714{
1715 xmlAttrPtr ret;
1716 xmlNsPtr namespace = NULL;
1717 xmlChar *dup = NULL;
1718
Daniel Veillarde57ec792003-09-10 10:50:59 +00001719 /*
1720 * Note: if prefix == NULL, the attribute is not in the default namespace
1721 */
1722 if (prefix != NULL)
1723 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1724
Daniel Veillard8a44e592003-09-15 14:50:06 +00001725 /*
1726 * allocate the node
1727 */
1728 if (ctxt->freeAttrs != NULL) {
1729 ret = ctxt->freeAttrs;
1730 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001731 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001732 memset(ret, 0, sizeof(xmlAttr));
1733 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001734
Daniel Veillard8a44e592003-09-15 14:50:06 +00001735 ret->parent = ctxt->node;
1736 ret->doc = ctxt->myDoc;
1737 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001738
Daniel Veillard8a44e592003-09-15 14:50:06 +00001739 if (ctxt->dictNames)
1740 ret->name = localname;
1741 else
1742 ret->name = xmlStrdup(localname);
1743
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001744 /* link at the end to preserv order, TODO speed up with a last */
1745 if (ctxt->node->properties == NULL) {
1746 ctxt->node->properties = ret;
1747 } else {
1748 xmlAttrPtr prev = ctxt->node->properties;
1749
1750 while (prev->next != NULL) prev = prev->next;
1751 prev->next = ret;
1752 ret->prev = prev;
1753 }
1754
Daniel Veillard8a44e592003-09-15 14:50:06 +00001755 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1756 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1757 } else {
1758 if (ctxt->dictNames)
1759 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1760 (xmlChar *) localname, NULL);
1761 else
1762 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1763 if (ret == NULL) {
1764 ctxt->errNo = XML_ERR_NO_MEMORY;
1765 ctxt->instate = XML_PARSER_EOF;
1766 ctxt->disableSAX = 1;
1767 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001768 }
1769 }
1770
Daniel Veillard8a44e592003-09-15 14:50:06 +00001771 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1772 xmlNodePtr tmp;
1773
Daniel Veillard19895052003-09-17 13:59:32 +00001774 /*
1775 * We know that if there is an entity reference, then
1776 * the string has been dup'ed and terminates with 0
1777 * otherwise with ' or "
1778 */
1779 if (*valueend != 0) {
1780 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1781 ret->children = tmp;
1782 ret->last = tmp;
1783 if (tmp != NULL) {
1784 tmp->doc = ret->doc;
1785 tmp->parent = (xmlNodePtr) ret;
1786 }
1787 } else {
1788 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1789 valueend - value);
1790 tmp = ret->children;
1791 while (tmp != NULL) {
1792 tmp->parent = (xmlNodePtr) ret;
1793 if (tmp->next == NULL)
1794 ret->last = tmp;
1795 tmp = tmp->next;
1796 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001797 }
1798 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00001799 xmlNodePtr tmp;
1800
1801 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1802 ret->children = tmp;
1803 ret->last = tmp;
1804 if (tmp != NULL) {
1805 tmp->doc = ret->doc;
1806 tmp->parent = (xmlNodePtr) ret;
1807 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001808 }
1809
Daniel Veillard4432df22003-09-28 18:58:27 +00001810#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00001811 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1812 ctxt->myDoc && ctxt->myDoc->intSubset) {
1813 /*
1814 * If we don't substitute entities, the validation should be
1815 * done on a value with replaced entities anyway.
1816 */
1817 if (!ctxt->replaceEntities) {
1818 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
1819 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00001820 if (*valueend == 0) {
1821 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1822 ctxt->myDoc, ctxt->node, ret, value);
1823 } else {
1824 /*
1825 * That should already be normalized.
1826 * cheaper to finally allocate here than duplicate
1827 * entry points in the full validation code
1828 */
1829 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001830
Daniel Veillard62998c02003-09-15 12:56:36 +00001831 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1832 ctxt->myDoc, ctxt->node, ret, dup);
1833 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001834 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00001835 /*
1836 * dup now contains a string of the flattened attribute
1837 * content with entities substitued. Check if we need to
1838 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001839 * It need to be done twice ... it's an extra burden related
1840 * to the ability to keep references in attributes
1841 */
Daniel Veillard62998c02003-09-15 12:56:36 +00001842 if (ctxt->attsSpecial != NULL) {
1843 xmlChar *nvalnorm;
1844 xmlChar fn[50];
1845 xmlChar *fullname;
1846
1847 fullname = xmlBuildQName(localname, prefix, fn, 50);
1848 if (fullname != NULL) {
1849 ctxt->vctxt.valid = 1;
1850 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
1851 &ctxt->vctxt, ctxt->myDoc,
1852 ctxt->node, fullname, dup);
1853 if (ctxt->vctxt.valid != 1)
1854 ctxt->valid = 0;
1855
1856 if ((fullname != fn) && (fullname != localname))
1857 xmlFree(fullname);
1858 if (nvalnorm != NULL) {
1859 xmlFree(dup);
1860 dup = nvalnorm;
1861 }
1862 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001863 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001864
1865 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1866 ctxt->myDoc, ctxt->node, ret, dup);
1867 }
1868 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001869 /*
1870 * if entities already have been substitued, then
1871 * the attribute as passed is already normalized
1872 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001873 dup = xmlStrndup(value, valueend - value);
1874
1875 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1876 ctxt->myDoc, ctxt->node, ret, dup);
1877 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001878 } else
1879#endif /* LIBXML_VALID_ENABLED */
1880 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00001881 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1882 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1883 /*
1884 * when validating, the ID registration is done at the attribute
1885 * validation level. Otherwise we have to do specific handling here.
1886 */
1887 if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
1888 /* might be worth duplicate entry points and not copy */
1889 if (dup == NULL)
1890 dup = xmlStrndup(value, valueend - value);
1891 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1892 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
1893 if (dup == NULL)
1894 dup = xmlStrndup(value, valueend - value);
1895 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1896 }
1897 }
1898 if (dup != NULL)
1899 xmlFree(dup);
1900}
1901
1902/**
1903 * xmlSAX2StartElementNs:
1904 * @ctx: the user data (XML parser context)
1905 * @localname: the local name of the element
1906 * @prefix: the element namespace prefix if available
1907 * @URI: the element namespace name if available
1908 * @nb_namespaces: number of namespace definitions on that node
1909 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
1910 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001911 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001912 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
1913 * attribute values.
1914 *
1915 * SAX2 callback when an element start has been detected by the parser.
1916 * It provides the namespace informations for the element, as well as
1917 * the new namespace declarations on the element.
1918 */
1919void
1920xmlSAX2StartElementNs(void *ctx,
1921 const xmlChar *localname,
1922 const xmlChar *prefix,
1923 const xmlChar *URI,
1924 int nb_namespaces,
1925 const xmlChar **namespaces,
1926 int nb_attributes,
1927 int nb_defaulted,
1928 const xmlChar **attributes)
1929{
1930 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1931 xmlNodePtr ret;
1932 xmlNodePtr parent = ctxt->node;
1933 xmlNsPtr last = NULL, ns;
1934 const xmlChar *uri, *pref;
1935 int i, j;
1936
1937 /*
1938 * First check on validity:
1939 */
1940 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1941 ((ctxt->myDoc->intSubset == NULL) ||
1942 ((ctxt->myDoc->intSubset->notations == NULL) &&
1943 (ctxt->myDoc->intSubset->elements == NULL) &&
1944 (ctxt->myDoc->intSubset->attributes == NULL) &&
1945 (ctxt->myDoc->intSubset->entities == NULL)))) {
1946 if (ctxt->vctxt.error != NULL) {
1947 ctxt->vctxt.error(ctxt->vctxt.userData,
1948 "Validation failed: no DTD found !\n");
1949 }
1950 ctxt->validate = 0;
1951 ctxt->valid = 0;
1952 ctxt->errNo = XML_ERR_NO_DTD;
1953 }
1954
Daniel Veillard8a44e592003-09-15 14:50:06 +00001955 /*
1956 * allocate the node
1957 */
1958 if (ctxt->freeElems != NULL) {
1959 ret = ctxt->freeElems;
1960 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001961 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001962 memset(ret, 0, sizeof(xmlNode));
1963 ret->type = XML_ELEMENT_NODE;
1964
1965 if (ctxt->dictNames)
1966 ret->name = localname;
1967 else
1968 ret->name = xmlStrdup(localname);
1969
1970 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1971 xmlRegisterNodeDefaultValue(ret);
1972 } else {
1973 if (ctxt->dictNames)
1974 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
1975 (xmlChar *) localname, NULL);
1976 else
1977 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
1978 if (ret == NULL) {
1979 ctxt->errNo = XML_ERR_NO_MEMORY;
1980 ctxt->instate = XML_PARSER_EOF;
1981 ctxt->disableSAX = 1;
1982 return;
1983 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001984 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00001985 if (ctxt->linenumbers) {
1986 if (ctxt->input != NULL)
1987 ret->content = (void *) (long) ctxt->input->line;
1988 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001989
Daniel Veillarde57ec792003-09-10 10:50:59 +00001990 if (ctxt->myDoc->children == NULL) {
1991 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1992 } else if (parent == NULL) {
1993 parent = ctxt->myDoc->children;
1994 }
1995 /*
1996 * Build the namespace list
1997 */
1998 for (i = 0,j = 0;j < nb_namespaces;j++) {
1999 pref = namespaces[i++];
2000 uri = namespaces[i++];
2001 ns = xmlNewNs(NULL, uri, pref);
2002 if (ns != NULL) {
2003 if (last == NULL) {
2004 ret->nsDef = last = ns;
2005 } else {
2006 last->next = ns;
2007 last = ns;
2008 }
2009 if ((URI != NULL) && (prefix == pref))
2010 ret->ns = ns;
2011 } else {
2012 ctxt->errNo = XML_ERR_NO_MEMORY;
2013 ctxt->instate = XML_PARSER_EOF;
2014 ctxt->disableSAX = 1;
2015 return;
2016 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002017#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002018 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2019 ctxt->myDoc && ctxt->myDoc->intSubset) {
2020 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2021 ret, prefix, ns, uri);
2022 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002023#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002024 }
2025 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002026
2027 /*
2028 * We are parsing a new node.
2029 */
2030 nodePush(ctxt, ret);
2031
2032 /*
2033 * Link the child element
2034 */
2035 if (parent != NULL) {
2036 if (parent->type == XML_ELEMENT_NODE) {
2037 xmlAddChild(parent, ret);
2038 } else {
2039 xmlAddSibling(parent, ret);
2040 }
2041 }
2042
2043 /*
2044 * Insert the defaulted attributes from the DTD only if requested:
2045 */
2046 if ((nb_defaulted != 0) &&
2047 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2048 nb_attributes -= nb_defaulted;
2049
2050 /*
2051 * Search the namespace if it wasn't already found
2052 */
2053 if ((URI != NULL) && (ret->ns == NULL)) {
2054 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2055 if (ret->ns == NULL) {
2056 ns = xmlNewNs(ret, NULL, prefix);
2057 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
2058 ctxt->sax->warning(ctxt->userData,
2059 "Namespace prefix %s was not found\n", prefix);
2060 }
2061 }
2062
2063 /*
2064 * process all the other attributes
2065 */
2066 if (nb_attributes > 0) {
2067 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2068 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2069 attributes[j+3], attributes[j+4]);
2070 }
2071 }
2072
Daniel Veillard4432df22003-09-28 18:58:27 +00002073#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002074 /*
2075 * If it's the Document root, finish the DTD validation and
2076 * check the document root element for validity
2077 */
2078 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
2079 int chk;
2080
2081 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2082 if (chk <= 0)
2083 ctxt->valid = 0;
2084 if (chk < 0)
2085 ctxt->wellFormed = 0;
2086 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2087 ctxt->vctxt.finishDtd = 1;
2088 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002089#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002090}
2091
2092/**
2093 * xmlSAX2EndElementNs:
2094 * @ctx: the user data (XML parser context)
2095 * @localname: the local name of the element
2096 * @prefix: the element namespace prefix if available
2097 * @URI: the element namespace name if available
2098 *
2099 * SAX2 callback when an element end has been detected by the parser.
2100 * It provides the namespace informations for the element.
2101 */
2102void
2103xmlSAX2EndElementNs(void *ctx,
2104 const xmlChar * localname ATTRIBUTE_UNUSED,
2105 const xmlChar * prefix ATTRIBUTE_UNUSED,
2106 const xmlChar * URI ATTRIBUTE_UNUSED)
2107{
2108 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2109 xmlParserNodeInfo node_info;
2110 xmlNodePtr cur = ctxt->node;
2111
2112 /* Capture end position and add node */
2113 if ((ctxt->record_info) && (cur != NULL)) {
2114 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2115 node_info.end_line = ctxt->input->line;
2116 node_info.node = cur;
2117 xmlParserAddNodeInfo(ctxt, &node_info);
2118 }
2119 ctxt->nodemem = -1;
2120
Daniel Veillard4432df22003-09-28 18:58:27 +00002121#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002122 if (ctxt->validate && ctxt->wellFormed &&
2123 ctxt->myDoc && ctxt->myDoc->intSubset)
2124 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002125#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002126
2127 /*
2128 * end of parsing of this node.
2129 */
2130 nodePop(ctxt);
2131}
2132
Daniel Veillard1af9a412003-08-20 22:54:39 +00002133/**
2134 * xmlSAX2Reference:
2135 * @ctx: the user data (XML parser context)
2136 * @name: The entity name
2137 *
2138 * called when an entity xmlSAX2Reference is detected.
2139 */
2140void
2141xmlSAX2Reference(void *ctx, const xmlChar *name)
2142{
2143 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2144 xmlNodePtr ret;
2145
2146#ifdef DEBUG_SAX
2147 xmlGenericError(xmlGenericErrorContext,
2148 "SAX.xmlSAX2Reference(%s)\n", name);
2149#endif
2150 if (name[0] == '#')
2151 ret = xmlNewCharRef(ctxt->myDoc, name);
2152 else
2153 ret = xmlNewReference(ctxt->myDoc, name);
2154#ifdef DEBUG_SAX_TREE
2155 xmlGenericError(xmlGenericErrorContext,
2156 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2157#endif
2158 xmlAddChild(ctxt->node, ret);
2159}
2160
2161/**
2162 * xmlSAX2Characters:
2163 * @ctx: the user data (XML parser context)
2164 * @ch: a xmlChar string
2165 * @len: the number of xmlChar
2166 *
2167 * receiving some chars from the parser.
2168 */
2169void
2170xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2171{
2172 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2173 xmlNodePtr lastChild;
2174
2175#ifdef DEBUG_SAX
2176 xmlGenericError(xmlGenericErrorContext,
2177 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2178#endif
2179 /*
2180 * Handle the data if any. If there is no child
2181 * add it as content, otherwise if the last child is text,
2182 * concatenate it, else create a new node of type text.
2183 */
2184
2185 if (ctxt->node == NULL) {
2186#ifdef DEBUG_SAX_TREE
2187 xmlGenericError(xmlGenericErrorContext,
2188 "add chars: ctxt->node == NULL !\n");
2189#endif
2190 return;
2191 }
Daniel Veillard19895052003-09-17 13:59:32 +00002192 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002193#ifdef DEBUG_SAX_TREE
2194 xmlGenericError(xmlGenericErrorContext,
2195 "add chars to %s \n", ctxt->node->name);
2196#endif
2197
2198 /*
2199 * Here we needed an accelerator mechanism in case of very large
2200 * elements. Use an attribute in the structure !!!
2201 */
2202 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002203 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2204 if (lastChild != NULL) {
2205 ctxt->node->children = lastChild;
2206 ctxt->node->last = lastChild;
2207 lastChild->parent = ctxt->node;
2208 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002209 ctxt->nodelen = len;
2210 ctxt->nodemem = len + 1;
2211 }
2212 } else {
2213 int coalesceText = (lastChild != NULL) &&
2214 (lastChild->type == XML_TEXT_NODE) &&
2215 (lastChild->name == xmlStringText);
2216 if ((coalesceText) && (ctxt->nodemem != 0)) {
2217 /*
2218 * The whole point of maintaining nodelen and nodemem,
2219 * xmlTextConcat is too costly, i.e. compute length,
2220 * reallocate a new buffer, move data, append ch. Here
2221 * We try to minimaze realloc() uses and avoid copying
2222 * and recomputing length over and over.
2223 */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002224 if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2225 (xmlDictOwns(ctxt->dict, lastChild->content))) {
2226 lastChild->content = xmlStrdup(lastChild->content);
2227 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002228 if (ctxt->nodelen + len >= ctxt->nodemem) {
2229 xmlChar *newbuf;
2230 int size;
2231
2232 size = ctxt->nodemem + len;
2233 size *= 2;
2234 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2235 if (newbuf == NULL) {
2236 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2237 ctxt->sax->error(ctxt->userData,
2238 "SAX.xmlSAX2Characters(): out of memory\n");
2239 ctxt->errNo = XML_ERR_NO_MEMORY;
2240 ctxt->instate = XML_PARSER_EOF;
2241 ctxt->disableSAX = 1;
2242 return;
2243 }
2244 ctxt->nodemem = size;
2245 lastChild->content = newbuf;
2246 }
2247 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2248 ctxt->nodelen += len;
2249 lastChild->content[ctxt->nodelen] = 0;
2250 } else if (coalesceText) {
2251 if (xmlTextConcat(lastChild, ch, len)) {
2252 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2253 ctxt->sax->error(ctxt->userData,
2254 "SAX.xmlSAX2Characters(): out of memory\n");
2255 ctxt->errNo = XML_ERR_NO_MEMORY;
2256 ctxt->instate = XML_PARSER_EOF;
2257 ctxt->disableSAX = 1;
2258 }
2259 if (ctxt->node->children != NULL) {
2260 ctxt->nodelen = xmlStrlen(lastChild->content);
2261 ctxt->nodemem = ctxt->nodelen + 1;
2262 }
2263 } else {
2264 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002265 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2266 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002267 xmlAddChild(ctxt->node, lastChild);
2268 if (ctxt->node->children != NULL) {
2269 ctxt->nodelen = len;
2270 ctxt->nodemem = len + 1;
2271 }
2272 }
2273 }
2274 }
2275}
2276
2277/**
2278 * xmlSAX2IgnorableWhitespace:
2279 * @ctx: the user data (XML parser context)
2280 * @ch: a xmlChar string
2281 * @len: the number of xmlChar
2282 *
2283 * receiving some ignorable whitespaces from the parser.
2284 * UNUSED: by default the DOM building will use xmlSAX2Characters
2285 */
2286void
2287xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2288{
2289 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2290#ifdef DEBUG_SAX
2291 xmlGenericError(xmlGenericErrorContext,
2292 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2293#endif
2294}
2295
2296/**
2297 * xmlSAX2ProcessingInstruction:
2298 * @ctx: the user data (XML parser context)
2299 * @target: the target name
2300 * @data: the PI data's
2301 *
2302 * A processing instruction has been parsed.
2303 */
2304void
2305xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2306 const xmlChar *data)
2307{
2308 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2309 xmlNodePtr ret;
2310 xmlNodePtr parent = ctxt->node;
2311
2312#ifdef DEBUG_SAX
2313 xmlGenericError(xmlGenericErrorContext,
2314 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2315#endif
2316
2317 ret = xmlNewPI(target, data);
2318 if (ret == NULL) return;
2319 parent = ctxt->node;
2320
2321 if (ctxt->inSubset == 1) {
2322 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2323 return;
2324 } else if (ctxt->inSubset == 2) {
2325 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2326 return;
2327 }
2328 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2329#ifdef DEBUG_SAX_TREE
2330 xmlGenericError(xmlGenericErrorContext,
2331 "Setting PI %s as root\n", target);
2332#endif
2333 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2334 return;
2335 }
2336 if (parent->type == XML_ELEMENT_NODE) {
2337#ifdef DEBUG_SAX_TREE
2338 xmlGenericError(xmlGenericErrorContext,
2339 "adding PI %s child to %s\n", target, parent->name);
2340#endif
2341 xmlAddChild(parent, ret);
2342 } else {
2343#ifdef DEBUG_SAX_TREE
2344 xmlGenericError(xmlGenericErrorContext,
2345 "adding PI %s sibling to ", target);
2346 xmlDebugDumpOneNode(stderr, parent, 0);
2347#endif
2348 xmlAddSibling(parent, ret);
2349 }
2350}
2351
2352/**
2353 * xmlSAX2Comment:
2354 * @ctx: the user data (XML parser context)
2355 * @value: the xmlSAX2Comment content
2356 *
2357 * A xmlSAX2Comment has been parsed.
2358 */
2359void
2360xmlSAX2Comment(void *ctx, const xmlChar *value)
2361{
2362 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2363 xmlNodePtr ret;
2364 xmlNodePtr parent = ctxt->node;
2365
2366#ifdef DEBUG_SAX
2367 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2368#endif
2369 ret = xmlNewDocComment(ctxt->myDoc, value);
2370 if (ret == NULL) return;
2371
2372 if (ctxt->inSubset == 1) {
2373 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2374 return;
2375 } else if (ctxt->inSubset == 2) {
2376 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2377 return;
2378 }
2379 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2380#ifdef DEBUG_SAX_TREE
2381 xmlGenericError(xmlGenericErrorContext,
2382 "Setting xmlSAX2Comment as root\n");
2383#endif
2384 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2385 return;
2386 }
2387 if (parent->type == XML_ELEMENT_NODE) {
2388#ifdef DEBUG_SAX_TREE
2389 xmlGenericError(xmlGenericErrorContext,
2390 "adding xmlSAX2Comment child to %s\n", parent->name);
2391#endif
2392 xmlAddChild(parent, ret);
2393 } else {
2394#ifdef DEBUG_SAX_TREE
2395 xmlGenericError(xmlGenericErrorContext,
2396 "adding xmlSAX2Comment sibling to ");
2397 xmlDebugDumpOneNode(stderr, parent, 0);
2398#endif
2399 xmlAddSibling(parent, ret);
2400 }
2401}
2402
2403/**
2404 * xmlSAX2CDataBlock:
2405 * @ctx: the user data (XML parser context)
2406 * @value: The pcdata content
2407 * @len: the block length
2408 *
2409 * called when a pcdata block has been parsed
2410 */
2411void
2412xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2413{
2414 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2415 xmlNodePtr ret, lastChild;
2416
2417#ifdef DEBUG_SAX
2418 xmlGenericError(xmlGenericErrorContext,
2419 "SAX.pcdata(%.10s, %d)\n", value, len);
2420#endif
2421 lastChild = xmlGetLastChild(ctxt->node);
2422#ifdef DEBUG_SAX_TREE
2423 xmlGenericError(xmlGenericErrorContext,
2424 "add chars to %s \n", ctxt->node->name);
2425#endif
2426 if ((lastChild != NULL) &&
2427 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2428 xmlTextConcat(lastChild, value, len);
2429 } else {
2430 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2431 xmlAddChild(ctxt->node, ret);
2432 }
2433}
2434
Daniel Veillard62998c02003-09-15 12:56:36 +00002435static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002436
Daniel Veillard81273902003-09-30 00:43:48 +00002437#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002438/**
2439 * xmlSAXDefaultVersion:
2440 * @version: the version, 1 or 2
2441 *
2442 * Set the default version of SAX used globally by the library.
2443 * Note that this may not be a good thing to do from a library
2444 * it is better to use xmlSAXVersion() to set up specifically the
2445 * version for a given parsing context.
2446 *
2447 * Returns the previous value in case of success and -1 in case of error.
2448 */
2449int
2450xmlSAXDefaultVersion(int version)
2451{
2452 int ret = xmlSAX2DefaultVersionValue;
2453
2454 if ((version != 1) && (version != 2))
2455 return(-1);
2456 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002457 return(ret);
2458}
Daniel Veillard81273902003-09-30 00:43:48 +00002459#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002460
2461/**
2462 * xmlSAXVersion:
2463 * @hdlr: the SAX handler
2464 * @version: the version, 1 or 2
2465 *
2466 * Initialize the default XML SAX handler according to the version
2467 *
2468 * Returns 0 in case of success and -1 in case of error.
2469 */
2470int
2471xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2472{
2473 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002474 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002475 hdlr->startElement = NULL;
2476 hdlr->endElement = NULL;
2477 hdlr->startElementNs = xmlSAX2StartElementNs;
2478 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002479 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002480 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002481#ifdef LIBXML_SAX1_ENABLED
2482 } else if (version == 1) {
2483 hdlr->startElement = xmlSAX2StartElement;
2484 hdlr->endElement = xmlSAX2EndElement;
2485 hdlr->initialized = 1;
2486#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002487 } else
2488 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002489 hdlr->internalSubset = xmlSAX2InternalSubset;
2490 hdlr->externalSubset = xmlSAX2ExternalSubset;
2491 hdlr->isStandalone = xmlSAX2IsStandalone;
2492 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2493 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2494 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2495 hdlr->getEntity = xmlSAX2GetEntity;
2496 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2497 hdlr->entityDecl = xmlSAX2EntityDecl;
2498 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2499 hdlr->elementDecl = xmlSAX2ElementDecl;
2500 hdlr->notationDecl = xmlSAX2NotationDecl;
2501 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2502 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2503 hdlr->startDocument = xmlSAX2StartDocument;
2504 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002505 hdlr->reference = xmlSAX2Reference;
2506 hdlr->characters = xmlSAX2Characters;
2507 hdlr->cdataBlock = xmlSAX2CDataBlock;
2508 hdlr->ignorableWhitespace = xmlSAX2Characters;
2509 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2510 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002511 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002512 hdlr->error = xmlParserError;
2513 hdlr->fatalError = xmlParserError;
2514
Daniel Veillarde57ec792003-09-10 10:50:59 +00002515 return(0);
2516}
2517
2518/**
2519 * xmlSAX2InitDefaultSAXHandler:
2520 * @hdlr: the SAX handler
2521 * @warning: flag if non-zero sets the handler warning procedure
2522 *
2523 * Initialize the default XML SAX2 handler
2524 */
2525void
2526xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2527{
2528 if ((hdlr == NULL) || (hdlr->initialized != 0))
2529 return;
2530
2531 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2532 if (warning == 0)
2533 hdlr->warning = NULL;
2534 else
2535 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002536}
2537
2538/**
2539 * xmlDefaultSAXHandlerInit:
2540 *
2541 * Initialize the default SAX2 handler
2542 */
2543void
2544xmlDefaultSAXHandlerInit(void)
2545{
Daniel Veillard81273902003-09-30 00:43:48 +00002546#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002547 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002548#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002549}
2550
2551#ifdef LIBXML_HTML_ENABLED
2552
2553/**
2554 * xmlSAX2InitHtmlDefaultSAXHandler:
2555 * @hdlr: the SAX handler
2556 *
2557 * Initialize the default HTML SAX2 handler
2558 */
2559void
2560xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2561{
2562 if(hdlr->initialized != 0)
2563 return;
2564
2565 hdlr->internalSubset = xmlSAX2InternalSubset;
2566 hdlr->externalSubset = NULL;
2567 hdlr->isStandalone = NULL;
2568 hdlr->hasInternalSubset = NULL;
2569 hdlr->hasExternalSubset = NULL;
2570 hdlr->resolveEntity = NULL;
2571 hdlr->getEntity = xmlSAX2GetEntity;
2572 hdlr->getParameterEntity = NULL;
2573 hdlr->entityDecl = NULL;
2574 hdlr->attributeDecl = NULL;
2575 hdlr->elementDecl = NULL;
2576 hdlr->notationDecl = NULL;
2577 hdlr->unparsedEntityDecl = NULL;
2578 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2579 hdlr->startDocument = xmlSAX2StartDocument;
2580 hdlr->endDocument = xmlSAX2EndDocument;
2581 hdlr->startElement = xmlSAX2StartElement;
2582 hdlr->endElement = xmlSAX2EndElement;
2583 hdlr->reference = NULL;
2584 hdlr->characters = xmlSAX2Characters;
2585 hdlr->cdataBlock = xmlSAX2CDataBlock;
2586 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2587 hdlr->processingInstruction = NULL;
2588 hdlr->comment = xmlSAX2Comment;
2589 hdlr->warning = xmlParserWarning;
2590 hdlr->error = xmlParserError;
2591 hdlr->fatalError = xmlParserError;
2592
Daniel Veillard092643b2003-09-25 14:29:29 +00002593 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002594}
2595
2596/**
2597 * htmlDefaultSAXHandlerInit:
2598 *
2599 * Initialize the default SAX handler
2600 */
2601void
2602htmlDefaultSAXHandlerInit(void)
2603{
Daniel Veillard092643b2003-09-25 14:29:29 +00002604 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002605}
2606
2607#endif /* LIBXML_HTML_ENABLED */
2608
2609#ifdef LIBXML_DOCB_ENABLED
2610
2611/**
2612 * xmlSAX2InitDocbDefaultSAXHandler:
2613 * @hdlr: the SAX handler
2614 *
2615 * Initialize the default DocBook SAX2 handler
2616 */
2617void
2618xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2619{
2620 if(hdlr->initialized != 0)
2621 return;
2622
2623 hdlr->internalSubset = xmlSAX2InternalSubset;
2624 hdlr->externalSubset = NULL;
2625 hdlr->isStandalone = xmlSAX2IsStandalone;
2626 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2627 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2628 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2629 hdlr->getEntity = xmlSAX2GetEntity;
2630 hdlr->getParameterEntity = NULL;
2631 hdlr->entityDecl = xmlSAX2EntityDecl;
2632 hdlr->attributeDecl = NULL;
2633 hdlr->elementDecl = NULL;
2634 hdlr->notationDecl = NULL;
2635 hdlr->unparsedEntityDecl = NULL;
2636 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2637 hdlr->startDocument = xmlSAX2StartDocument;
2638 hdlr->endDocument = xmlSAX2EndDocument;
2639 hdlr->startElement = xmlSAX2StartElement;
2640 hdlr->endElement = xmlSAX2EndElement;
2641 hdlr->reference = xmlSAX2Reference;
2642 hdlr->characters = xmlSAX2Characters;
2643 hdlr->cdataBlock = NULL;
2644 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2645 hdlr->processingInstruction = NULL;
2646 hdlr->comment = xmlSAX2Comment;
2647 hdlr->warning = xmlParserWarning;
2648 hdlr->error = xmlParserError;
2649 hdlr->fatalError = xmlParserError;
2650
Daniel Veillardffbbed42003-10-10 14:46:54 +00002651 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002652}
2653
2654/**
2655 * docbDefaultSAXHandlerInit:
2656 *
2657 * Initialize the default SAX handler
2658 */
2659void
2660docbDefaultSAXHandlerInit(void)
2661{
Daniel Veillard092643b2003-09-25 14:29:29 +00002662 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002663}
2664
2665#endif /* LIBXML_DOCB_ENABLED */