blob: 1841e50bbf4f57727595e6d8c54bf53aa2b96a8d [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) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001413 if (ctxt->input != NULL) {
1414 if (ctxt->input->line < 65535)
1415 ret->line = (short) ctxt->input->line;
1416 else
1417 ret->line = 65535;
1418 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00001419 }
1420
1421 /*
1422 * We are parsing a new node.
1423 */
1424#ifdef DEBUG_SAX_TREE
1425 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1426#endif
1427 nodePush(ctxt, ret);
1428
1429 /*
1430 * Link the child element
1431 */
1432 if (parent != NULL) {
1433 if (parent->type == XML_ELEMENT_NODE) {
1434#ifdef DEBUG_SAX_TREE
1435 xmlGenericError(xmlGenericErrorContext,
1436 "adding child %s to %s\n", name, parent->name);
1437#endif
1438 xmlAddChild(parent, ret);
1439 } else {
1440#ifdef DEBUG_SAX_TREE
1441 xmlGenericError(xmlGenericErrorContext,
1442 "adding sibling %s to ", name);
1443 xmlDebugDumpOneNode(stderr, parent, 0);
1444#endif
1445 xmlAddSibling(parent, ret);
1446 }
1447 }
1448
1449 /*
1450 * Insert all the defaulted attributes from the DTD especially namespaces
1451 */
1452 if ((!ctxt->html) &&
1453 ((ctxt->myDoc->intSubset != NULL) ||
1454 (ctxt->myDoc->extSubset != NULL))) {
1455 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1456 }
1457
1458 /*
1459 * process all the attributes whose name start with "xmlns"
1460 */
1461 if (atts != NULL) {
1462 i = 0;
1463 att = atts[i++];
1464 value = atts[i++];
1465 if (!ctxt->html) {
1466 while ((att != NULL) && (value != NULL)) {
1467 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1468 (att[3] == 'n') && (att[4] == 's'))
1469 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1470
1471 att = atts[i++];
1472 value = atts[i++];
1473 }
1474 }
1475 }
1476
1477 /*
1478 * Search the namespace, note that since the attributes have been
1479 * processed, the local namespaces are available.
1480 */
1481 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1482 if ((ns == NULL) && (parent != NULL))
1483 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1484 if ((prefix != NULL) && (ns == NULL)) {
1485 ns = xmlNewNs(ret, NULL, prefix);
1486 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1487 ctxt->sax->warning(ctxt->userData,
1488 "Namespace prefix %s is not defined\n", prefix);
1489 }
1490
1491 /*
1492 * set the namespace node, making sure that if the default namspace
1493 * is unbound on a parent we simply kee it NULL
1494 */
1495 if ((ns != NULL) && (ns->href != NULL) &&
1496 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1497 xmlSetNs(ret, ns);
1498
1499 /*
1500 * process all the other attributes
1501 */
1502 if (atts != NULL) {
1503 i = 0;
1504 att = atts[i++];
1505 value = atts[i++];
1506 if (ctxt->html) {
1507 while (att != NULL) {
1508 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1509 att = atts[i++];
1510 value = atts[i++];
1511 }
1512 } else {
1513 while ((att != NULL) && (value != NULL)) {
1514 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1515 (att[3] != 'n') || (att[4] != 's'))
1516 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1517
1518 /*
1519 * Next ones
1520 */
1521 att = atts[i++];
1522 value = atts[i++];
1523 }
1524 }
1525 }
1526
Daniel Veillard4432df22003-09-28 18:58:27 +00001527#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001528 /*
1529 * If it's the Document root, finish the DTD validation and
1530 * check the document root element for validity
1531 */
1532 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
1533 int chk;
1534
1535 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1536 if (chk <= 0)
1537 ctxt->valid = 0;
1538 if (chk < 0)
1539 ctxt->wellFormed = 0;
1540 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1541 ctxt->vctxt.finishDtd = 1;
1542 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001543#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001544
1545 if (prefix != NULL)
1546 xmlFree(prefix);
1547
1548}
1549
1550/**
1551 * xmlSAX2EndElement:
1552 * @ctx: the user data (XML parser context)
1553 * @name: The element name
1554 *
1555 * called when the end of an element has been detected.
1556 */
1557void
1558xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1559{
1560 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1561 xmlParserNodeInfo node_info;
1562 xmlNodePtr cur = ctxt->node;
1563
1564#ifdef DEBUG_SAX
1565 if (name == NULL)
1566 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1567 else
1568 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1569#endif
1570
1571 /* Capture end position and add node */
1572 if (cur != NULL && ctxt->record_info) {
1573 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
1574 node_info.end_line = ctxt->input->line;
1575 node_info.node = cur;
1576 xmlParserAddNodeInfo(ctxt, &node_info);
1577 }
1578 ctxt->nodemem = -1;
1579
Daniel Veillard4432df22003-09-28 18:58:27 +00001580#ifdef LIBXML_VALID_ENABLED
Daniel Veillard1af9a412003-08-20 22:54:39 +00001581 if (ctxt->validate && ctxt->wellFormed &&
1582 ctxt->myDoc && ctxt->myDoc->intSubset)
1583 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1584 cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00001585#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001586
1587
1588 /*
1589 * end of parsing of this node.
1590 */
1591#ifdef DEBUG_SAX_TREE
1592 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1593#endif
1594 nodePop(ctxt);
1595}
Daniel Veillard81273902003-09-30 00:43:48 +00001596#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLE */
Daniel Veillard1af9a412003-08-20 22:54:39 +00001597
Daniel Veillarde57ec792003-09-10 10:50:59 +00001598/*
Daniel Veillard19895052003-09-17 13:59:32 +00001599 * xmlSAX2TextNode:
1600 * @ctxt: the parser context
1601 * @str: the input string
1602 * @len: the string length
1603 *
1604 * Remove the entities from an attribute value
1605 *
1606 * Returns the newly allocated string or NULL if not needed or error
1607 */
1608static xmlNodePtr
1609xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1610 xmlNodePtr ret;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001611 const xmlChar *intern = NULL;
Daniel Veillard19895052003-09-17 13:59:32 +00001612
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001613 /*
1614 * Allocate
1615 */
Daniel Veillard19895052003-09-17 13:59:32 +00001616 if (ctxt->freeElems != NULL) {
1617 ret = ctxt->freeElems;
1618 ctxt->freeElems = ret->next;
1619 ctxt->freeElemsNr--;
Daniel Veillard19895052003-09-17 13:59:32 +00001620 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001621 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
Daniel Veillard19895052003-09-17 13:59:32 +00001622 }
1623 if (ret == NULL) {
1624 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1625 ctxt->sax->error(ctxt->userData,
1626 "SAX.xmlSAX2Characters(): out of memory\n");
1627 ctxt->errNo = XML_ERR_NO_MEMORY;
1628 ctxt->instate = XML_PARSER_EOF;
1629 ctxt->disableSAX = 1;
1630 return(NULL);
1631 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001632 /*
1633 * intern the formatting blanks found between tags, or the
1634 * very short strings
1635 */
1636 if (ctxt->dictNames) {
1637 xmlChar cur = str[len];
1638
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001639 if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1640 ((cur == '<') && (str[len + 1] != '!')))) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001641 intern = xmlDictLookup(ctxt->dict, str, len);
William M. Brack76e95df2003-10-18 16:20:14 +00001642 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001643 (str[len + 1] != '!')) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001644 int i;
1645
1646 for (i = 1;i < len;i++) {
William M. Brack76e95df2003-10-18 16:20:14 +00001647 if (!IS_BLANK_CH(*str)) goto skip;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001648 }
1649 intern = xmlDictLookup(ctxt->dict, str, len);
1650 }
1651 }
1652skip:
1653 memset(ret, 0, sizeof(xmlNode));
1654 ret->type = XML_TEXT_NODE;
1655
1656 ret->name = xmlStringText;
1657 if (intern == NULL)
1658 ret->content = xmlStrndup(str, len);
1659 else
1660 ret->content = (xmlChar *) intern;
1661
1662 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1663 xmlRegisterNodeDefaultValue(ret);
Daniel Veillard19895052003-09-17 13:59:32 +00001664 return(ret);
1665}
1666
Daniel Veillard4432df22003-09-28 18:58:27 +00001667#ifdef LIBXML_VALID_ENABLED
Daniel Veillard19895052003-09-17 13:59:32 +00001668/*
Daniel Veillarde57ec792003-09-10 10:50:59 +00001669 * xmlSAX2DecodeAttrEntities:
1670 * @ctxt: the parser context
1671 * @str: the input string
1672 * @len: the string length
1673 *
1674 * Remove the entities from an attribute value
1675 *
1676 * Returns the newly allocated string or NULL if not needed or error
1677 */
1678static xmlChar *
1679xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1680 const xmlChar *end) {
1681 const xmlChar *in;
1682 xmlChar *ret;
1683
1684 in = str;
1685 while (in < end)
1686 if (*in++ == '&')
1687 goto decode;
1688 return(NULL);
1689decode:
1690 ctxt->depth++;
1691 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1692 XML_SUBSTITUTE_REF, 0,0,0);
1693 ctxt->depth--;
1694 return(ret);
1695}
Daniel Veillard4432df22003-09-28 18:58:27 +00001696#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001697
1698/**
1699 * xmlSAX2AttributeNs:
1700 * @ctx: the user data (XML parser context)
Daniel Veillard62998c02003-09-15 12:56:36 +00001701 * @localname: the local name of the attribute
1702 * @prefix: the attribute namespace prefix if available
1703 * @URI: the attribute namespace name if available
Daniel Veillarde57ec792003-09-10 10:50:59 +00001704 * @value: Start of the attribute value
1705 * @valueend: end of the attribute value
1706 *
1707 * Handle an attribute that has been read by the parser.
1708 * The default handling is to convert the attribute into an
1709 * DOM subtree and past it in a new xmlAttr element added to
1710 * the element.
1711 */
1712static void
1713xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1714 const xmlChar * localname,
1715 const xmlChar * prefix,
1716 const xmlChar * value,
1717 const xmlChar * valueend)
1718{
1719 xmlAttrPtr ret;
1720 xmlNsPtr namespace = NULL;
1721 xmlChar *dup = NULL;
1722
Daniel Veillarde57ec792003-09-10 10:50:59 +00001723 /*
1724 * Note: if prefix == NULL, the attribute is not in the default namespace
1725 */
1726 if (prefix != NULL)
1727 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1728
Daniel Veillard8a44e592003-09-15 14:50:06 +00001729 /*
1730 * allocate the node
1731 */
1732 if (ctxt->freeAttrs != NULL) {
1733 ret = ctxt->freeAttrs;
1734 ctxt->freeAttrs = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001735 ctxt->freeAttrsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001736 memset(ret, 0, sizeof(xmlAttr));
1737 ret->type = XML_ATTRIBUTE_NODE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001738
Daniel Veillard8a44e592003-09-15 14:50:06 +00001739 ret->parent = ctxt->node;
1740 ret->doc = ctxt->myDoc;
1741 ret->ns = namespace;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001742
Daniel Veillard8a44e592003-09-15 14:50:06 +00001743 if (ctxt->dictNames)
1744 ret->name = localname;
1745 else
1746 ret->name = xmlStrdup(localname);
1747
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001748 /* link at the end to preserv order, TODO speed up with a last */
1749 if (ctxt->node->properties == NULL) {
1750 ctxt->node->properties = ret;
1751 } else {
1752 xmlAttrPtr prev = ctxt->node->properties;
1753
1754 while (prev->next != NULL) prev = prev->next;
1755 prev->next = ret;
1756 ret->prev = prev;
1757 }
1758
Daniel Veillard8a44e592003-09-15 14:50:06 +00001759 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1760 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1761 } else {
1762 if (ctxt->dictNames)
1763 ret = xmlNewNsPropEatName(ctxt->node, namespace,
1764 (xmlChar *) localname, NULL);
1765 else
1766 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
1767 if (ret == NULL) {
1768 ctxt->errNo = XML_ERR_NO_MEMORY;
1769 ctxt->instate = XML_PARSER_EOF;
1770 ctxt->disableSAX = 1;
1771 return;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001772 }
1773 }
1774
Daniel Veillard8a44e592003-09-15 14:50:06 +00001775 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1776 xmlNodePtr tmp;
1777
Daniel Veillard19895052003-09-17 13:59:32 +00001778 /*
1779 * We know that if there is an entity reference, then
1780 * the string has been dup'ed and terminates with 0
1781 * otherwise with ' or "
1782 */
1783 if (*valueend != 0) {
1784 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1785 ret->children = tmp;
1786 ret->last = tmp;
1787 if (tmp != NULL) {
1788 tmp->doc = ret->doc;
1789 tmp->parent = (xmlNodePtr) ret;
1790 }
1791 } else {
1792 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
1793 valueend - value);
1794 tmp = ret->children;
1795 while (tmp != NULL) {
1796 tmp->parent = (xmlNodePtr) ret;
1797 if (tmp->next == NULL)
1798 ret->last = tmp;
1799 tmp = tmp->next;
1800 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001801 }
1802 } else if (value != NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00001803 xmlNodePtr tmp;
1804
1805 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
1806 ret->children = tmp;
1807 ret->last = tmp;
1808 if (tmp != NULL) {
1809 tmp->doc = ret->doc;
1810 tmp->parent = (xmlNodePtr) ret;
1811 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001812 }
1813
Daniel Veillard4432df22003-09-28 18:58:27 +00001814#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00001815 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1816 ctxt->myDoc && ctxt->myDoc->intSubset) {
1817 /*
1818 * If we don't substitute entities, the validation should be
1819 * done on a value with replaced entities anyway.
1820 */
1821 if (!ctxt->replaceEntities) {
1822 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
1823 if (dup == NULL) {
Daniel Veillard62998c02003-09-15 12:56:36 +00001824 if (*valueend == 0) {
1825 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1826 ctxt->myDoc, ctxt->node, ret, value);
1827 } else {
1828 /*
1829 * That should already be normalized.
1830 * cheaper to finally allocate here than duplicate
1831 * entry points in the full validation code
1832 */
1833 dup = xmlStrndup(value, valueend - value);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001834
Daniel Veillard62998c02003-09-15 12:56:36 +00001835 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1836 ctxt->myDoc, ctxt->node, ret, dup);
1837 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001838 } else {
Daniel Veillard62998c02003-09-15 12:56:36 +00001839 /*
1840 * dup now contains a string of the flattened attribute
1841 * content with entities substitued. Check if we need to
1842 * apply an extra layer of normalization.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001843 * It need to be done twice ... it's an extra burden related
1844 * to the ability to keep references in attributes
1845 */
Daniel Veillard62998c02003-09-15 12:56:36 +00001846 if (ctxt->attsSpecial != NULL) {
1847 xmlChar *nvalnorm;
1848 xmlChar fn[50];
1849 xmlChar *fullname;
1850
1851 fullname = xmlBuildQName(localname, prefix, fn, 50);
1852 if (fullname != NULL) {
1853 ctxt->vctxt.valid = 1;
1854 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
1855 &ctxt->vctxt, ctxt->myDoc,
1856 ctxt->node, fullname, dup);
1857 if (ctxt->vctxt.valid != 1)
1858 ctxt->valid = 0;
1859
1860 if ((fullname != fn) && (fullname != localname))
1861 xmlFree(fullname);
1862 if (nvalnorm != NULL) {
1863 xmlFree(dup);
1864 dup = nvalnorm;
1865 }
1866 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001867 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001868
1869 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1870 ctxt->myDoc, ctxt->node, ret, dup);
1871 }
1872 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001873 /*
1874 * if entities already have been substitued, then
1875 * the attribute as passed is already normalized
1876 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001877 dup = xmlStrndup(value, valueend - value);
1878
1879 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1880 ctxt->myDoc, ctxt->node, ret, dup);
1881 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001882 } else
1883#endif /* LIBXML_VALID_ENABLED */
1884 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
Daniel Veillarde57ec792003-09-10 10:50:59 +00001885 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1886 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1887 /*
1888 * when validating, the ID registration is done at the attribute
1889 * validation level. Otherwise we have to do specific handling here.
1890 */
1891 if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
1892 /* might be worth duplicate entry points and not copy */
1893 if (dup == NULL)
1894 dup = xmlStrndup(value, valueend - value);
1895 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1896 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
1897 if (dup == NULL)
1898 dup = xmlStrndup(value, valueend - value);
1899 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
1900 }
1901 }
1902 if (dup != NULL)
1903 xmlFree(dup);
1904}
1905
1906/**
1907 * xmlSAX2StartElementNs:
1908 * @ctx: the user data (XML parser context)
1909 * @localname: the local name of the element
1910 * @prefix: the element namespace prefix if available
1911 * @URI: the element namespace name if available
1912 * @nb_namespaces: number of namespace definitions on that node
1913 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
1914 * @nb_attributes: the number of attributes on that node
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001915 * @nb_defaulted: the number of defaulted attributes.
Daniel Veillarde57ec792003-09-10 10:50:59 +00001916 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
1917 * attribute values.
1918 *
1919 * SAX2 callback when an element start has been detected by the parser.
1920 * It provides the namespace informations for the element, as well as
1921 * the new namespace declarations on the element.
1922 */
1923void
1924xmlSAX2StartElementNs(void *ctx,
1925 const xmlChar *localname,
1926 const xmlChar *prefix,
1927 const xmlChar *URI,
1928 int nb_namespaces,
1929 const xmlChar **namespaces,
1930 int nb_attributes,
1931 int nb_defaulted,
1932 const xmlChar **attributes)
1933{
1934 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1935 xmlNodePtr ret;
1936 xmlNodePtr parent = ctxt->node;
1937 xmlNsPtr last = NULL, ns;
1938 const xmlChar *uri, *pref;
1939 int i, j;
1940
1941 /*
1942 * First check on validity:
1943 */
1944 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1945 ((ctxt->myDoc->intSubset == NULL) ||
1946 ((ctxt->myDoc->intSubset->notations == NULL) &&
1947 (ctxt->myDoc->intSubset->elements == NULL) &&
1948 (ctxt->myDoc->intSubset->attributes == NULL) &&
1949 (ctxt->myDoc->intSubset->entities == NULL)))) {
1950 if (ctxt->vctxt.error != NULL) {
1951 ctxt->vctxt.error(ctxt->vctxt.userData,
1952 "Validation failed: no DTD found !\n");
1953 }
1954 ctxt->validate = 0;
1955 ctxt->valid = 0;
1956 ctxt->errNo = XML_ERR_NO_DTD;
1957 }
1958
Daniel Veillard8a44e592003-09-15 14:50:06 +00001959 /*
1960 * allocate the node
1961 */
1962 if (ctxt->freeElems != NULL) {
1963 ret = ctxt->freeElems;
1964 ctxt->freeElems = ret->next;
Daniel Veillard19895052003-09-17 13:59:32 +00001965 ctxt->freeElemsNr--;
Daniel Veillard8a44e592003-09-15 14:50:06 +00001966 memset(ret, 0, sizeof(xmlNode));
1967 ret->type = XML_ELEMENT_NODE;
1968
1969 if (ctxt->dictNames)
1970 ret->name = localname;
1971 else
1972 ret->name = xmlStrdup(localname);
1973
1974 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1975 xmlRegisterNodeDefaultValue(ret);
1976 } else {
1977 if (ctxt->dictNames)
1978 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
1979 (xmlChar *) localname, NULL);
1980 else
1981 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
1982 if (ret == NULL) {
1983 ctxt->errNo = XML_ERR_NO_MEMORY;
1984 ctxt->instate = XML_PARSER_EOF;
1985 ctxt->disableSAX = 1;
1986 return;
1987 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001988 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00001989 if (ctxt->linenumbers) {
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00001990 if (ctxt->input != NULL) {
1991 if (ctxt->input->line < 65535)
1992 ret->line = (short) ctxt->input->line;
1993 else
1994 ret->line = 65535;
1995 }
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00001996 }
Daniel Veillard8a44e592003-09-15 14:50:06 +00001997
Daniel Veillarde57ec792003-09-10 10:50:59 +00001998 if (ctxt->myDoc->children == NULL) {
1999 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2000 } else if (parent == NULL) {
2001 parent = ctxt->myDoc->children;
2002 }
2003 /*
2004 * Build the namespace list
2005 */
2006 for (i = 0,j = 0;j < nb_namespaces;j++) {
2007 pref = namespaces[i++];
2008 uri = namespaces[i++];
2009 ns = xmlNewNs(NULL, uri, pref);
2010 if (ns != NULL) {
2011 if (last == NULL) {
2012 ret->nsDef = last = ns;
2013 } else {
2014 last->next = ns;
2015 last = ns;
2016 }
2017 if ((URI != NULL) && (prefix == pref))
2018 ret->ns = ns;
2019 } else {
2020 ctxt->errNo = XML_ERR_NO_MEMORY;
2021 ctxt->instate = XML_PARSER_EOF;
2022 ctxt->disableSAX = 1;
2023 return;
2024 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002025#ifdef LIBXML_VALID_ENABLED
Daniel Veillardd9e9c9d2003-09-18 22:03:46 +00002026 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2027 ctxt->myDoc && ctxt->myDoc->intSubset) {
2028 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2029 ret, prefix, ns, uri);
2030 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002031#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002032 }
2033 ctxt->nodemem = -1;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002034
2035 /*
2036 * We are parsing a new node.
2037 */
2038 nodePush(ctxt, ret);
2039
2040 /*
2041 * Link the child element
2042 */
2043 if (parent != NULL) {
2044 if (parent->type == XML_ELEMENT_NODE) {
2045 xmlAddChild(parent, ret);
2046 } else {
2047 xmlAddSibling(parent, ret);
2048 }
2049 }
2050
2051 /*
2052 * Insert the defaulted attributes from the DTD only if requested:
2053 */
2054 if ((nb_defaulted != 0) &&
2055 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2056 nb_attributes -= nb_defaulted;
2057
2058 /*
2059 * Search the namespace if it wasn't already found
2060 */
2061 if ((URI != NULL) && (ret->ns == NULL)) {
2062 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2063 if (ret->ns == NULL) {
2064 ns = xmlNewNs(ret, NULL, prefix);
2065 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
2066 ctxt->sax->warning(ctxt->userData,
2067 "Namespace prefix %s was not found\n", prefix);
2068 }
2069 }
2070
2071 /*
2072 * process all the other attributes
2073 */
2074 if (nb_attributes > 0) {
2075 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2076 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2077 attributes[j+3], attributes[j+4]);
2078 }
2079 }
2080
Daniel Veillard4432df22003-09-28 18:58:27 +00002081#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002082 /*
2083 * If it's the Document root, finish the DTD validation and
2084 * check the document root element for validity
2085 */
2086 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == 0)) {
2087 int chk;
2088
2089 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2090 if (chk <= 0)
2091 ctxt->valid = 0;
2092 if (chk < 0)
2093 ctxt->wellFormed = 0;
2094 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2095 ctxt->vctxt.finishDtd = 1;
2096 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002097#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002098}
2099
2100/**
2101 * xmlSAX2EndElementNs:
2102 * @ctx: the user data (XML parser context)
2103 * @localname: the local name of the element
2104 * @prefix: the element namespace prefix if available
2105 * @URI: the element namespace name if available
2106 *
2107 * SAX2 callback when an element end has been detected by the parser.
2108 * It provides the namespace informations for the element.
2109 */
2110void
2111xmlSAX2EndElementNs(void *ctx,
2112 const xmlChar * localname ATTRIBUTE_UNUSED,
2113 const xmlChar * prefix ATTRIBUTE_UNUSED,
2114 const xmlChar * URI ATTRIBUTE_UNUSED)
2115{
2116 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2117 xmlParserNodeInfo node_info;
2118 xmlNodePtr cur = ctxt->node;
2119
2120 /* Capture end position and add node */
2121 if ((ctxt->record_info) && (cur != NULL)) {
2122 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2123 node_info.end_line = ctxt->input->line;
2124 node_info.node = cur;
2125 xmlParserAddNodeInfo(ctxt, &node_info);
2126 }
2127 ctxt->nodemem = -1;
2128
Daniel Veillard4432df22003-09-28 18:58:27 +00002129#ifdef LIBXML_VALID_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002130 if (ctxt->validate && ctxt->wellFormed &&
2131 ctxt->myDoc && ctxt->myDoc->intSubset)
2132 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
Daniel Veillard4432df22003-09-28 18:58:27 +00002133#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002134
2135 /*
2136 * end of parsing of this node.
2137 */
2138 nodePop(ctxt);
2139}
2140
Daniel Veillard1af9a412003-08-20 22:54:39 +00002141/**
2142 * xmlSAX2Reference:
2143 * @ctx: the user data (XML parser context)
2144 * @name: The entity name
2145 *
2146 * called when an entity xmlSAX2Reference is detected.
2147 */
2148void
2149xmlSAX2Reference(void *ctx, const xmlChar *name)
2150{
2151 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2152 xmlNodePtr ret;
2153
2154#ifdef DEBUG_SAX
2155 xmlGenericError(xmlGenericErrorContext,
2156 "SAX.xmlSAX2Reference(%s)\n", name);
2157#endif
2158 if (name[0] == '#')
2159 ret = xmlNewCharRef(ctxt->myDoc, name);
2160 else
2161 ret = xmlNewReference(ctxt->myDoc, name);
2162#ifdef DEBUG_SAX_TREE
2163 xmlGenericError(xmlGenericErrorContext,
2164 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2165#endif
2166 xmlAddChild(ctxt->node, ret);
2167}
2168
2169/**
2170 * xmlSAX2Characters:
2171 * @ctx: the user data (XML parser context)
2172 * @ch: a xmlChar string
2173 * @len: the number of xmlChar
2174 *
2175 * receiving some chars from the parser.
2176 */
2177void
2178xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2179{
2180 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2181 xmlNodePtr lastChild;
2182
2183#ifdef DEBUG_SAX
2184 xmlGenericError(xmlGenericErrorContext,
2185 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2186#endif
2187 /*
2188 * Handle the data if any. If there is no child
2189 * add it as content, otherwise if the last child is text,
2190 * concatenate it, else create a new node of type text.
2191 */
2192
2193 if (ctxt->node == NULL) {
2194#ifdef DEBUG_SAX_TREE
2195 xmlGenericError(xmlGenericErrorContext,
2196 "add chars: ctxt->node == NULL !\n");
2197#endif
2198 return;
2199 }
Daniel Veillard19895052003-09-17 13:59:32 +00002200 lastChild = ctxt->node->last;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002201#ifdef DEBUG_SAX_TREE
2202 xmlGenericError(xmlGenericErrorContext,
2203 "add chars to %s \n", ctxt->node->name);
2204#endif
2205
2206 /*
2207 * Here we needed an accelerator mechanism in case of very large
2208 * elements. Use an attribute in the structure !!!
2209 */
2210 if (lastChild == NULL) {
Daniel Veillard19895052003-09-17 13:59:32 +00002211 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2212 if (lastChild != NULL) {
2213 ctxt->node->children = lastChild;
2214 ctxt->node->last = lastChild;
2215 lastChild->parent = ctxt->node;
2216 lastChild->doc = ctxt->node->doc;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002217 ctxt->nodelen = len;
2218 ctxt->nodemem = len + 1;
2219 }
2220 } else {
2221 int coalesceText = (lastChild != NULL) &&
2222 (lastChild->type == XML_TEXT_NODE) &&
2223 (lastChild->name == xmlStringText);
2224 if ((coalesceText) && (ctxt->nodemem != 0)) {
2225 /*
2226 * The whole point of maintaining nodelen and nodemem,
2227 * xmlTextConcat is too costly, i.e. compute length,
2228 * reallocate a new buffer, move data, append ch. Here
2229 * We try to minimaze realloc() uses and avoid copying
2230 * and recomputing length over and over.
2231 */
Daniel Veillard2b0f8792003-10-10 19:36:36 +00002232 if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2233 (xmlDictOwns(ctxt->dict, lastChild->content))) {
2234 lastChild->content = xmlStrdup(lastChild->content);
2235 }
Daniel Veillard1af9a412003-08-20 22:54:39 +00002236 if (ctxt->nodelen + len >= ctxt->nodemem) {
2237 xmlChar *newbuf;
2238 int size;
2239
2240 size = ctxt->nodemem + len;
2241 size *= 2;
2242 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2243 if (newbuf == NULL) {
2244 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2245 ctxt->sax->error(ctxt->userData,
2246 "SAX.xmlSAX2Characters(): out of memory\n");
2247 ctxt->errNo = XML_ERR_NO_MEMORY;
2248 ctxt->instate = XML_PARSER_EOF;
2249 ctxt->disableSAX = 1;
2250 return;
2251 }
2252 ctxt->nodemem = size;
2253 lastChild->content = newbuf;
2254 }
2255 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2256 ctxt->nodelen += len;
2257 lastChild->content[ctxt->nodelen] = 0;
2258 } else if (coalesceText) {
2259 if (xmlTextConcat(lastChild, ch, len)) {
2260 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2261 ctxt->sax->error(ctxt->userData,
2262 "SAX.xmlSAX2Characters(): out of memory\n");
2263 ctxt->errNo = XML_ERR_NO_MEMORY;
2264 ctxt->instate = XML_PARSER_EOF;
2265 ctxt->disableSAX = 1;
2266 }
2267 if (ctxt->node->children != NULL) {
2268 ctxt->nodelen = xmlStrlen(lastChild->content);
2269 ctxt->nodemem = ctxt->nodelen + 1;
2270 }
2271 } else {
2272 /* Mixed content, first time */
Daniel Veillard19895052003-09-17 13:59:32 +00002273 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2274 if (lastChild != NULL) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00002275 xmlAddChild(ctxt->node, lastChild);
2276 if (ctxt->node->children != NULL) {
2277 ctxt->nodelen = len;
2278 ctxt->nodemem = len + 1;
2279 }
2280 }
2281 }
2282 }
2283}
2284
2285/**
2286 * xmlSAX2IgnorableWhitespace:
2287 * @ctx: the user data (XML parser context)
2288 * @ch: a xmlChar string
2289 * @len: the number of xmlChar
2290 *
2291 * receiving some ignorable whitespaces from the parser.
2292 * UNUSED: by default the DOM building will use xmlSAX2Characters
2293 */
2294void
2295xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2296{
2297 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2298#ifdef DEBUG_SAX
2299 xmlGenericError(xmlGenericErrorContext,
2300 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2301#endif
2302}
2303
2304/**
2305 * xmlSAX2ProcessingInstruction:
2306 * @ctx: the user data (XML parser context)
2307 * @target: the target name
2308 * @data: the PI data's
2309 *
2310 * A processing instruction has been parsed.
2311 */
2312void
2313xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2314 const xmlChar *data)
2315{
2316 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2317 xmlNodePtr ret;
2318 xmlNodePtr parent = ctxt->node;
2319
2320#ifdef DEBUG_SAX
2321 xmlGenericError(xmlGenericErrorContext,
2322 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2323#endif
2324
2325 ret = xmlNewPI(target, data);
2326 if (ret == NULL) return;
2327 parent = ctxt->node;
2328
2329 if (ctxt->inSubset == 1) {
2330 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2331 return;
2332 } else if (ctxt->inSubset == 2) {
2333 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2334 return;
2335 }
2336 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2337#ifdef DEBUG_SAX_TREE
2338 xmlGenericError(xmlGenericErrorContext,
2339 "Setting PI %s as root\n", target);
2340#endif
2341 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2342 return;
2343 }
2344 if (parent->type == XML_ELEMENT_NODE) {
2345#ifdef DEBUG_SAX_TREE
2346 xmlGenericError(xmlGenericErrorContext,
2347 "adding PI %s child to %s\n", target, parent->name);
2348#endif
2349 xmlAddChild(parent, ret);
2350 } else {
2351#ifdef DEBUG_SAX_TREE
2352 xmlGenericError(xmlGenericErrorContext,
2353 "adding PI %s sibling to ", target);
2354 xmlDebugDumpOneNode(stderr, parent, 0);
2355#endif
2356 xmlAddSibling(parent, ret);
2357 }
2358}
2359
2360/**
2361 * xmlSAX2Comment:
2362 * @ctx: the user data (XML parser context)
2363 * @value: the xmlSAX2Comment content
2364 *
2365 * A xmlSAX2Comment has been parsed.
2366 */
2367void
2368xmlSAX2Comment(void *ctx, const xmlChar *value)
2369{
2370 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2371 xmlNodePtr ret;
2372 xmlNodePtr parent = ctxt->node;
2373
2374#ifdef DEBUG_SAX
2375 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2376#endif
2377 ret = xmlNewDocComment(ctxt->myDoc, value);
2378 if (ret == NULL) return;
2379
2380 if (ctxt->inSubset == 1) {
2381 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2382 return;
2383 } else if (ctxt->inSubset == 2) {
2384 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2385 return;
2386 }
2387 if ((ctxt->myDoc->children == NULL) || (parent == NULL)) {
2388#ifdef DEBUG_SAX_TREE
2389 xmlGenericError(xmlGenericErrorContext,
2390 "Setting xmlSAX2Comment as root\n");
2391#endif
2392 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2393 return;
2394 }
2395 if (parent->type == XML_ELEMENT_NODE) {
2396#ifdef DEBUG_SAX_TREE
2397 xmlGenericError(xmlGenericErrorContext,
2398 "adding xmlSAX2Comment child to %s\n", parent->name);
2399#endif
2400 xmlAddChild(parent, ret);
2401 } else {
2402#ifdef DEBUG_SAX_TREE
2403 xmlGenericError(xmlGenericErrorContext,
2404 "adding xmlSAX2Comment sibling to ");
2405 xmlDebugDumpOneNode(stderr, parent, 0);
2406#endif
2407 xmlAddSibling(parent, ret);
2408 }
2409}
2410
2411/**
2412 * xmlSAX2CDataBlock:
2413 * @ctx: the user data (XML parser context)
2414 * @value: The pcdata content
2415 * @len: the block length
2416 *
2417 * called when a pcdata block has been parsed
2418 */
2419void
2420xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2421{
2422 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2423 xmlNodePtr ret, lastChild;
2424
2425#ifdef DEBUG_SAX
2426 xmlGenericError(xmlGenericErrorContext,
2427 "SAX.pcdata(%.10s, %d)\n", value, len);
2428#endif
2429 lastChild = xmlGetLastChild(ctxt->node);
2430#ifdef DEBUG_SAX_TREE
2431 xmlGenericError(xmlGenericErrorContext,
2432 "add chars to %s \n", ctxt->node->name);
2433#endif
2434 if ((lastChild != NULL) &&
2435 (lastChild->type == XML_CDATA_SECTION_NODE)) {
2436 xmlTextConcat(lastChild, value, len);
2437 } else {
2438 ret = xmlNewCDataBlock(ctxt->myDoc, value, len);
2439 xmlAddChild(ctxt->node, ret);
2440 }
2441}
2442
Daniel Veillard62998c02003-09-15 12:56:36 +00002443static int xmlSAX2DefaultVersionValue = 2;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002444
Daniel Veillard81273902003-09-30 00:43:48 +00002445#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00002446/**
2447 * xmlSAXDefaultVersion:
2448 * @version: the version, 1 or 2
2449 *
2450 * Set the default version of SAX used globally by the library.
2451 * Note that this may not be a good thing to do from a library
2452 * it is better to use xmlSAXVersion() to set up specifically the
2453 * version for a given parsing context.
2454 *
2455 * Returns the previous value in case of success and -1 in case of error.
2456 */
2457int
2458xmlSAXDefaultVersion(int version)
2459{
2460 int ret = xmlSAX2DefaultVersionValue;
2461
2462 if ((version != 1) && (version != 2))
2463 return(-1);
2464 xmlSAX2DefaultVersionValue = version;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002465 return(ret);
2466}
Daniel Veillard81273902003-09-30 00:43:48 +00002467#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002468
2469/**
2470 * xmlSAXVersion:
2471 * @hdlr: the SAX handler
2472 * @version: the version, 1 or 2
2473 *
2474 * Initialize the default XML SAX handler according to the version
2475 *
2476 * Returns 0 in case of success and -1 in case of error.
2477 */
2478int
2479xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2480{
2481 if (hdlr == NULL) return(-1);
Daniel Veillard81273902003-09-30 00:43:48 +00002482 if (version == 2) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00002483 hdlr->startElement = NULL;
2484 hdlr->endElement = NULL;
2485 hdlr->startElementNs = xmlSAX2StartElementNs;
2486 hdlr->endElementNs = xmlSAX2EndElementNs;
Daniel Veillardffbbed42003-10-10 14:46:54 +00002487 hdlr->serror = NULL;
Daniel Veillard092643b2003-09-25 14:29:29 +00002488 hdlr->initialized = XML_SAX2_MAGIC;
Daniel Veillard81273902003-09-30 00:43:48 +00002489#ifdef LIBXML_SAX1_ENABLED
2490 } else if (version == 1) {
2491 hdlr->startElement = xmlSAX2StartElement;
2492 hdlr->endElement = xmlSAX2EndElement;
2493 hdlr->initialized = 1;
2494#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00002495 } else
2496 return(-1);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002497 hdlr->internalSubset = xmlSAX2InternalSubset;
2498 hdlr->externalSubset = xmlSAX2ExternalSubset;
2499 hdlr->isStandalone = xmlSAX2IsStandalone;
2500 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2501 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2502 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2503 hdlr->getEntity = xmlSAX2GetEntity;
2504 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2505 hdlr->entityDecl = xmlSAX2EntityDecl;
2506 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2507 hdlr->elementDecl = xmlSAX2ElementDecl;
2508 hdlr->notationDecl = xmlSAX2NotationDecl;
2509 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2510 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2511 hdlr->startDocument = xmlSAX2StartDocument;
2512 hdlr->endDocument = xmlSAX2EndDocument;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002513 hdlr->reference = xmlSAX2Reference;
2514 hdlr->characters = xmlSAX2Characters;
2515 hdlr->cdataBlock = xmlSAX2CDataBlock;
2516 hdlr->ignorableWhitespace = xmlSAX2Characters;
2517 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2518 hdlr->comment = xmlSAX2Comment;
Daniel Veillarde57ec792003-09-10 10:50:59 +00002519 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002520 hdlr->error = xmlParserError;
2521 hdlr->fatalError = xmlParserError;
2522
Daniel Veillarde57ec792003-09-10 10:50:59 +00002523 return(0);
2524}
2525
2526/**
2527 * xmlSAX2InitDefaultSAXHandler:
2528 * @hdlr: the SAX handler
2529 * @warning: flag if non-zero sets the handler warning procedure
2530 *
2531 * Initialize the default XML SAX2 handler
2532 */
2533void
2534xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2535{
2536 if ((hdlr == NULL) || (hdlr->initialized != 0))
2537 return;
2538
2539 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2540 if (warning == 0)
2541 hdlr->warning = NULL;
2542 else
2543 hdlr->warning = xmlParserWarning;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002544}
2545
2546/**
2547 * xmlDefaultSAXHandlerInit:
2548 *
2549 * Initialize the default SAX2 handler
2550 */
2551void
2552xmlDefaultSAXHandlerInit(void)
2553{
Daniel Veillard81273902003-09-30 00:43:48 +00002554#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002555 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
Daniel Veillard81273902003-09-30 00:43:48 +00002556#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard1af9a412003-08-20 22:54:39 +00002557}
2558
2559#ifdef LIBXML_HTML_ENABLED
2560
2561/**
2562 * xmlSAX2InitHtmlDefaultSAXHandler:
2563 * @hdlr: the SAX handler
2564 *
2565 * Initialize the default HTML SAX2 handler
2566 */
2567void
2568xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2569{
2570 if(hdlr->initialized != 0)
2571 return;
2572
2573 hdlr->internalSubset = xmlSAX2InternalSubset;
2574 hdlr->externalSubset = NULL;
2575 hdlr->isStandalone = NULL;
2576 hdlr->hasInternalSubset = NULL;
2577 hdlr->hasExternalSubset = NULL;
2578 hdlr->resolveEntity = NULL;
2579 hdlr->getEntity = xmlSAX2GetEntity;
2580 hdlr->getParameterEntity = NULL;
2581 hdlr->entityDecl = NULL;
2582 hdlr->attributeDecl = NULL;
2583 hdlr->elementDecl = NULL;
2584 hdlr->notationDecl = NULL;
2585 hdlr->unparsedEntityDecl = NULL;
2586 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2587 hdlr->startDocument = xmlSAX2StartDocument;
2588 hdlr->endDocument = xmlSAX2EndDocument;
2589 hdlr->startElement = xmlSAX2StartElement;
2590 hdlr->endElement = xmlSAX2EndElement;
2591 hdlr->reference = NULL;
2592 hdlr->characters = xmlSAX2Characters;
2593 hdlr->cdataBlock = xmlSAX2CDataBlock;
2594 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2595 hdlr->processingInstruction = NULL;
2596 hdlr->comment = xmlSAX2Comment;
2597 hdlr->warning = xmlParserWarning;
2598 hdlr->error = xmlParserError;
2599 hdlr->fatalError = xmlParserError;
2600
Daniel Veillard092643b2003-09-25 14:29:29 +00002601 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002602}
2603
2604/**
2605 * htmlDefaultSAXHandlerInit:
2606 *
2607 * Initialize the default SAX handler
2608 */
2609void
2610htmlDefaultSAXHandlerInit(void)
2611{
Daniel Veillard092643b2003-09-25 14:29:29 +00002612 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002613}
2614
2615#endif /* LIBXML_HTML_ENABLED */
2616
2617#ifdef LIBXML_DOCB_ENABLED
2618
2619/**
2620 * xmlSAX2InitDocbDefaultSAXHandler:
2621 * @hdlr: the SAX handler
2622 *
2623 * Initialize the default DocBook SAX2 handler
2624 */
2625void
2626xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2627{
2628 if(hdlr->initialized != 0)
2629 return;
2630
2631 hdlr->internalSubset = xmlSAX2InternalSubset;
2632 hdlr->externalSubset = NULL;
2633 hdlr->isStandalone = xmlSAX2IsStandalone;
2634 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2635 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2636 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2637 hdlr->getEntity = xmlSAX2GetEntity;
2638 hdlr->getParameterEntity = NULL;
2639 hdlr->entityDecl = xmlSAX2EntityDecl;
2640 hdlr->attributeDecl = NULL;
2641 hdlr->elementDecl = NULL;
2642 hdlr->notationDecl = NULL;
2643 hdlr->unparsedEntityDecl = NULL;
2644 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2645 hdlr->startDocument = xmlSAX2StartDocument;
2646 hdlr->endDocument = xmlSAX2EndDocument;
2647 hdlr->startElement = xmlSAX2StartElement;
2648 hdlr->endElement = xmlSAX2EndElement;
2649 hdlr->reference = xmlSAX2Reference;
2650 hdlr->characters = xmlSAX2Characters;
2651 hdlr->cdataBlock = NULL;
2652 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2653 hdlr->processingInstruction = NULL;
2654 hdlr->comment = xmlSAX2Comment;
2655 hdlr->warning = xmlParserWarning;
2656 hdlr->error = xmlParserError;
2657 hdlr->fatalError = xmlParserError;
2658
Daniel Veillardffbbed42003-10-10 14:46:54 +00002659 hdlr->initialized = 1;
Daniel Veillard1af9a412003-08-20 22:54:39 +00002660}
2661
2662/**
2663 * docbDefaultSAXHandlerInit:
2664 *
2665 * Initialize the default SAX handler
2666 */
2667void
2668docbDefaultSAXHandlerInit(void)
2669{
Daniel Veillard092643b2003-09-25 14:29:29 +00002670 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
Daniel Veillard1af9a412003-08-20 22:54:39 +00002671}
2672
2673#endif /* LIBXML_DOCB_ENABLED */