VirtualBox

source: vbox/trunk/src/libs/libxml2-2.9.14/SAX2.c@ 103285

最後變更 在這個檔案從103285是 95312,由 vboxsync 提交於 2 年 前

libs/{curl,libxml2}: OSE export fixes, bugref:8515

  • 屬性 svn:eol-style 設為 native
檔案大小: 84.4 KB
 
1/*
2 * SAX2.c : Default SAX2 handler to build a tree.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <[email protected]>
7 */
8
9
10#define IN_LIBXML
11#include "libxml.h"
12#include <stdlib.h>
13#include <string.h>
14#include <limits.h>
15#include <stddef.h>
16#include <libxml/xmlmemory.h>
17#include <libxml/tree.h>
18#include <libxml/parser.h>
19#include <libxml/parserInternals.h>
20#include <libxml/valid.h>
21#include <libxml/entities.h>
22#include <libxml/xmlerror.h>
23#include <libxml/debugXML.h>
24#include <libxml/xmlIO.h>
25#include <libxml/SAX.h>
26#include <libxml/uri.h>
27#include <libxml/valid.h>
28#include <libxml/HTMLtree.h>
29#include <libxml/globals.h>
30
31/* Define SIZE_T_MAX unless defined through <limits.h>. */
32#ifndef SIZE_T_MAX
33# define SIZE_T_MAX ((size_t)-1)
34#endif /* !SIZE_T_MAX */
35
36/* #define DEBUG_SAX2 */
37/* #define DEBUG_SAX2_TREE */
38
39/**
40 * TODO:
41 *
42 * macro to flag unimplemented blocks
43 * XML_CATALOG_PREFER user env to select between system/public preferred
44 * option. C.f. Richard Tobin <[email protected]>
45 *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
46 *> values "system" and "public". I have made the default be "system" to
47 *> match yours.
48 */
49#define TODO \
50 xmlGenericError(xmlGenericErrorContext, \
51 "Unimplemented block at %s:%d\n", \
52 __FILE__, __LINE__);
53
54/*
55 * xmlSAX2ErrMemory:
56 * @ctxt: an XML validation parser context
57 * @msg: a string to accompany the error message
58 */
59static void LIBXML_ATTR_FORMAT(2,0)
60xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
61 xmlStructuredErrorFunc schannel = NULL;
62 const char *str1 = "out of memory\n";
63
64 if (ctxt != NULL) {
65 ctxt->errNo = XML_ERR_NO_MEMORY;
66 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
67 schannel = ctxt->sax->serror;
68 __xmlRaiseError(schannel,
69 ctxt->vctxt.error, ctxt->vctxt.userData,
70 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
71 XML_ERR_ERROR, NULL, 0, (const char *) str1,
72 NULL, NULL, 0, 0,
73 msg, (const char *) str1, NULL);
74 ctxt->errNo = XML_ERR_NO_MEMORY;
75 ctxt->instate = XML_PARSER_EOF;
76 ctxt->disableSAX = 1;
77 } else {
78 __xmlRaiseError(schannel,
79 NULL, NULL,
80 ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
81 XML_ERR_ERROR, NULL, 0, (const char *) str1,
82 NULL, NULL, 0, 0,
83 msg, (const char *) str1, NULL);
84 }
85}
86
87/**
88 * xmlValidError:
89 * @ctxt: an XML validation parser context
90 * @error: the error number
91 * @msg: the error message
92 * @str1: extra data
93 * @str2: extra data
94 *
95 * Handle a validation error
96 */
97static void LIBXML_ATTR_FORMAT(3,0)
98xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
99 const char *msg, const char *str1, const char *str2)
100{
101 xmlStructuredErrorFunc schannel = NULL;
102
103 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
104 (ctxt->instate == XML_PARSER_EOF))
105 return;
106 if (ctxt != NULL) {
107 ctxt->errNo = error;
108 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
109 schannel = ctxt->sax->serror;
110 __xmlRaiseError(schannel,
111 ctxt->vctxt.error, ctxt->vctxt.userData,
112 ctxt, NULL, XML_FROM_DTD, error,
113 XML_ERR_ERROR, NULL, 0, (const char *) str1,
114 (const char *) str2, NULL, 0, 0,
115 msg, (const char *) str1, (const char *) str2);
116 ctxt->valid = 0;
117 } else {
118 __xmlRaiseError(schannel,
119 NULL, NULL,
120 ctxt, NULL, XML_FROM_DTD, error,
121 XML_ERR_ERROR, NULL, 0, (const char *) str1,
122 (const char *) str2, NULL, 0, 0,
123 msg, (const char *) str1, (const char *) str2);
124 }
125}
126
127/**
128 * xmlFatalErrMsg:
129 * @ctxt: an XML parser context
130 * @error: the error number
131 * @msg: the error message
132 * @str1: an error string
133 * @str2: an error string
134 *
135 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
136 */
137static void LIBXML_ATTR_FORMAT(3,0)
138xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
139 const char *msg, const xmlChar *str1, const xmlChar *str2)
140{
141 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
142 (ctxt->instate == XML_PARSER_EOF))
143 return;
144 if (ctxt != NULL)
145 ctxt->errNo = error;
146 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
147 XML_ERR_FATAL, NULL, 0,
148 (const char *) str1, (const char *) str2,
149 NULL, 0, 0, msg, str1, str2);
150 if (ctxt != NULL) {
151 ctxt->wellFormed = 0;
152 ctxt->valid = 0;
153 if (ctxt->recovery == 0)
154 ctxt->disableSAX = 1;
155 }
156}
157
158/**
159 * xmlWarnMsg:
160 * @ctxt: an XML parser context
161 * @error: the error number
162 * @msg: the error message
163 * @str1: an error string
164 * @str2: an error string
165 *
166 * Handle a parser warning
167 */
168static void LIBXML_ATTR_FORMAT(3,0)
169xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
170 const char *msg, const xmlChar *str1)
171{
172 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
173 (ctxt->instate == XML_PARSER_EOF))
174 return;
175 if (ctxt != NULL)
176 ctxt->errNo = error;
177 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
178 XML_ERR_WARNING, NULL, 0,
179 (const char *) str1, NULL,
180 NULL, 0, 0, msg, str1);
181}
182
183/**
184 * xmlNsErrMsg:
185 * @ctxt: an XML parser context
186 * @error: the error number
187 * @msg: the error message
188 * @str1: an error string
189 * @str2: an error string
190 *
191 * Handle a namespace error
192 */
193static void LIBXML_ATTR_FORMAT(3,0)
194xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
195 const char *msg, const xmlChar *str1, const xmlChar *str2)
196{
197 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
198 (ctxt->instate == XML_PARSER_EOF))
199 return;
200 if (ctxt != NULL)
201 ctxt->errNo = error;
202 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
203 XML_ERR_ERROR, NULL, 0,
204 (const char *) str1, (const char *) str2,
205 NULL, 0, 0, msg, str1, str2);
206}
207
208/**
209 * xmlNsWarnMsg:
210 * @ctxt: an XML parser context
211 * @error: the error number
212 * @msg: the error message
213 * @str1: an error string
214 *
215 * Handle a namespace warning
216 */
217static void LIBXML_ATTR_FORMAT(3,0)
218xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
219 const char *msg, const xmlChar *str1, const xmlChar *str2)
220{
221 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
222 (ctxt->instate == XML_PARSER_EOF))
223 return;
224 if (ctxt != NULL)
225 ctxt->errNo = error;
226 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
227 XML_ERR_WARNING, NULL, 0,
228 (const char *) str1, (const char *) str2,
229 NULL, 0, 0, msg, str1, str2);
230}
231
232/**
233 * xmlSAX2GetPublicId:
234 * @ctx: the user data (XML parser context)
235 *
236 * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
237 *
238 * Returns a xmlChar *
239 */
240const xmlChar *
241xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
242{
243 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
244 return(NULL);
245}
246
247/**
248 * xmlSAX2GetSystemId:
249 * @ctx: the user data (XML parser context)
250 *
251 * Provides the system ID, basically URL or filename e.g.
252 * http://www.sgmlsource.com/dtds/memo.dtd
253 *
254 * Returns a xmlChar *
255 */
256const xmlChar *
257xmlSAX2GetSystemId(void *ctx)
258{
259 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
260 if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
261 return((const xmlChar *) ctxt->input->filename);
262}
263
264/**
265 * xmlSAX2GetLineNumber:
266 * @ctx: the user data (XML parser context)
267 *
268 * Provide the line number of the current parsing point.
269 *
270 * Returns an int
271 */
272int
273xmlSAX2GetLineNumber(void *ctx)
274{
275 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
276 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
277 return(ctxt->input->line);
278}
279
280/**
281 * xmlSAX2GetColumnNumber:
282 * @ctx: the user data (XML parser context)
283 *
284 * Provide the column number of the current parsing point.
285 *
286 * Returns an int
287 */
288int
289xmlSAX2GetColumnNumber(void *ctx)
290{
291 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
292 if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
293 return(ctxt->input->col);
294}
295
296/**
297 * xmlSAX2IsStandalone:
298 * @ctx: the user data (XML parser context)
299 *
300 * Is this document tagged standalone ?
301 *
302 * Returns 1 if true
303 */
304int
305xmlSAX2IsStandalone(void *ctx)
306{
307 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
308 if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
309 return(ctxt->myDoc->standalone == 1);
310}
311
312/**
313 * xmlSAX2HasInternalSubset:
314 * @ctx: the user data (XML parser context)
315 *
316 * Does this document has an internal subset
317 *
318 * Returns 1 if true
319 */
320int
321xmlSAX2HasInternalSubset(void *ctx)
322{
323 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
324 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
325 return(ctxt->myDoc->intSubset != NULL);
326}
327
328/**
329 * xmlSAX2HasExternalSubset:
330 * @ctx: the user data (XML parser context)
331 *
332 * Does this document has an external subset
333 *
334 * Returns 1 if true
335 */
336int
337xmlSAX2HasExternalSubset(void *ctx)
338{
339 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
340 if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
341 return(ctxt->myDoc->extSubset != NULL);
342}
343
344/**
345 * xmlSAX2InternalSubset:
346 * @ctx: the user data (XML parser context)
347 * @name: the root element name
348 * @ExternalID: the external ID
349 * @SystemID: the SYSTEM ID (e.g. filename or URL)
350 *
351 * Callback on internal subset declaration.
352 */
353void
354xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
355 const xmlChar *ExternalID, const xmlChar *SystemID)
356{
357 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
358 xmlDtdPtr dtd;
359 if (ctx == NULL) return;
360#ifdef DEBUG_SAX
361 xmlGenericError(xmlGenericErrorContext,
362 "SAX.xmlSAX2InternalSubset(%s, %s, %s)\n",
363 name, ExternalID, SystemID);
364#endif
365
366 if (ctxt->myDoc == NULL)
367 return;
368 dtd = xmlGetIntSubset(ctxt->myDoc);
369 if (dtd != NULL) {
370 if (ctxt->html)
371 return;
372 xmlUnlinkNode((xmlNodePtr) dtd);
373 xmlFreeDtd(dtd);
374 ctxt->myDoc->intSubset = NULL;
375 }
376 ctxt->myDoc->intSubset =
377 xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
378 if (ctxt->myDoc->intSubset == NULL)
379 xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
380}
381
382/**
383 * xmlSAX2ExternalSubset:
384 * @ctx: the user data (XML parser context)
385 * @name: the root element name
386 * @ExternalID: the external ID
387 * @SystemID: the SYSTEM ID (e.g. filename or URL)
388 *
389 * Callback on external subset declaration.
390 */
391void
392xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
393 const xmlChar *ExternalID, const xmlChar *SystemID)
394{
395 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
396 if (ctx == NULL) return;
397#ifdef DEBUG_SAX
398 xmlGenericError(xmlGenericErrorContext,
399 "SAX.xmlSAX2ExternalSubset(%s, %s, %s)\n",
400 name, ExternalID, SystemID);
401#endif
402 if (((ExternalID != NULL) || (SystemID != NULL)) &&
403 (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
404 (ctxt->wellFormed && ctxt->myDoc))) {
405 /*
406 * Try to fetch and parse the external subset.
407 */
408 xmlParserInputPtr oldinput;
409 int oldinputNr;
410 int oldinputMax;
411 xmlParserInputPtr *oldinputTab;
412 xmlParserInputPtr input = NULL;
413 xmlCharEncoding enc;
414 int oldcharset;
415 const xmlChar *oldencoding;
416
417 /*
418 * Ask the Entity resolver to load the damn thing
419 */
420 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
421 input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
422 SystemID);
423 if (input == NULL) {
424 return;
425 }
426
427 xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
428
429 /*
430 * make sure we won't destroy the main document context
431 */
432 oldinput = ctxt->input;
433 oldinputNr = ctxt->inputNr;
434 oldinputMax = ctxt->inputMax;
435 oldinputTab = ctxt->inputTab;
436 oldcharset = ctxt->charset;
437 oldencoding = ctxt->encoding;
438 ctxt->encoding = NULL;
439
440 ctxt->inputTab = (xmlParserInputPtr *)
441 xmlMalloc(5 * sizeof(xmlParserInputPtr));
442 if (ctxt->inputTab == NULL) {
443 xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
444 ctxt->input = oldinput;
445 ctxt->inputNr = oldinputNr;
446 ctxt->inputMax = oldinputMax;
447 ctxt->inputTab = oldinputTab;
448 ctxt->charset = oldcharset;
449 ctxt->encoding = oldencoding;
450 return;
451 }
452 ctxt->inputNr = 0;
453 ctxt->inputMax = 5;
454 ctxt->input = NULL;
455 xmlPushInput(ctxt, input);
456
457 /*
458 * On the fly encoding conversion if needed
459 */
460 if (ctxt->input->length >= 4) {
461 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
462 xmlSwitchEncoding(ctxt, enc);
463 }
464
465 if (input->filename == NULL)
466 input->filename = (char *) xmlCanonicPath(SystemID);
467 input->line = 1;
468 input->col = 1;
469 input->base = ctxt->input->cur;
470 input->cur = ctxt->input->cur;
471 input->free = NULL;
472
473 /*
474 * let's parse that entity knowing it's an external subset.
475 */
476 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
477
478 /*
479 * Free up the external entities
480 */
481
482 while (ctxt->inputNr > 1)
483 xmlPopInput(ctxt);
484 xmlFreeInputStream(ctxt->input);
485 xmlFree(ctxt->inputTab);
486
487 /*
488 * Restore the parsing context of the main entity
489 */
490 ctxt->input = oldinput;
491 ctxt->inputNr = oldinputNr;
492 ctxt->inputMax = oldinputMax;
493 ctxt->inputTab = oldinputTab;
494 ctxt->charset = oldcharset;
495 if ((ctxt->encoding != NULL) &&
496 ((ctxt->dict == NULL) ||
497 (!xmlDictOwns(ctxt->dict, ctxt->encoding))))
498 xmlFree((xmlChar *) ctxt->encoding);
499 ctxt->encoding = oldencoding;
500 /* ctxt->wellFormed = oldwellFormed; */
501 }
502}
503
504/**
505 * xmlSAX2ResolveEntity:
506 * @ctx: the user data (XML parser context)
507 * @publicId: The public ID of the entity
508 * @systemId: The system ID of the entity
509 *
510 * The entity loader, to control the loading of external entities,
511 * the application can either:
512 * - override this xmlSAX2ResolveEntity() callback in the SAX block
513 * - or better use the xmlSetExternalEntityLoader() function to
514 * set up it's own entity resolution routine
515 *
516 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
517 */
518xmlParserInputPtr
519xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
520{
521 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
522 xmlParserInputPtr ret;
523 xmlChar *URI;
524 const char *base = NULL;
525
526 if (ctx == NULL) return(NULL);
527 if (ctxt->input != NULL)
528 base = ctxt->input->filename;
529 if (base == NULL)
530 base = ctxt->directory;
531
532 URI = xmlBuildURI(systemId, (const xmlChar *) base);
533
534#ifdef DEBUG_SAX
535 xmlGenericError(xmlGenericErrorContext,
536 "SAX.xmlSAX2ResolveEntity(%s, %s)\n", publicId, systemId);
537#endif
538
539 ret = xmlLoadExternalEntity((const char *) URI,
540 (const char *) publicId, ctxt);
541 if (URI != NULL)
542 xmlFree(URI);
543 return(ret);
544}
545
546/**
547 * xmlSAX2GetEntity:
548 * @ctx: the user data (XML parser context)
549 * @name: The entity name
550 *
551 * Get an entity by name
552 *
553 * Returns the xmlEntityPtr if found.
554 */
555xmlEntityPtr
556xmlSAX2GetEntity(void *ctx, const xmlChar *name)
557{
558 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
559 xmlEntityPtr ret = NULL;
560
561 if (ctx == NULL) return(NULL);
562#ifdef DEBUG_SAX
563 xmlGenericError(xmlGenericErrorContext,
564 "SAX.xmlSAX2GetEntity(%s)\n", name);
565#endif
566
567 if (ctxt->inSubset == 0) {
568 ret = xmlGetPredefinedEntity(name);
569 if (ret != NULL)
570 return(ret);
571 }
572 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
573 if (ctxt->inSubset == 2) {
574 ctxt->myDoc->standalone = 0;
575 ret = xmlGetDocEntity(ctxt->myDoc, name);
576 ctxt->myDoc->standalone = 1;
577 } else {
578 ret = xmlGetDocEntity(ctxt->myDoc, name);
579 if (ret == NULL) {
580 ctxt->myDoc->standalone = 0;
581 ret = xmlGetDocEntity(ctxt->myDoc, name);
582 if (ret != NULL) {
583 xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
584 "Entity(%s) document marked standalone but requires external subset\n",
585 name, NULL);
586 }
587 ctxt->myDoc->standalone = 1;
588 }
589 }
590 } else {
591 ret = xmlGetDocEntity(ctxt->myDoc, name);
592 }
593 return(ret);
594}
595
596/**
597 * xmlSAX2GetParameterEntity:
598 * @ctx: the user data (XML parser context)
599 * @name: The entity name
600 *
601 * Get a parameter entity by name
602 *
603 * Returns the xmlEntityPtr if found.
604 */
605xmlEntityPtr
606xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
607{
608 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
609 xmlEntityPtr ret;
610
611 if (ctx == NULL) return(NULL);
612#ifdef DEBUG_SAX
613 xmlGenericError(xmlGenericErrorContext,
614 "SAX.xmlSAX2GetParameterEntity(%s)\n", name);
615#endif
616
617 ret = xmlGetParameterEntity(ctxt->myDoc, name);
618 return(ret);
619}
620
621
622/**
623 * xmlSAX2EntityDecl:
624 * @ctx: the user data (XML parser context)
625 * @name: the entity name
626 * @type: the entity type
627 * @publicId: The public ID of the entity
628 * @systemId: The system ID of the entity
629 * @content: the entity value (without processing).
630 *
631 * An entity definition has been parsed
632 */
633void
634xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
635 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
636{
637 xmlEntityPtr ent;
638 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
639
640 if (ctx == NULL) return;
641#ifdef DEBUG_SAX
642 xmlGenericError(xmlGenericErrorContext,
643 "SAX.xmlSAX2EntityDecl(%s, %d, %s, %s, %s)\n",
644 name, type, publicId, systemId, content);
645#endif
646 if (ctxt->inSubset == 1) {
647 ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
648 systemId, content);
649 if ((ent == NULL) && (ctxt->pedantic))
650 xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
651 "Entity(%s) already defined in the internal subset\n",
652 name);
653 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
654 xmlChar *URI;
655 const char *base = NULL;
656
657 if (ctxt->input != NULL)
658 base = ctxt->input->filename;
659 if (base == NULL)
660 base = ctxt->directory;
661
662 URI = xmlBuildURI(systemId, (const xmlChar *) base);
663 ent->URI = URI;
664 }
665 } else if (ctxt->inSubset == 2) {
666 ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
667 systemId, content);
668 if ((ent == NULL) && (ctxt->pedantic) &&
669 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
670 ctxt->sax->warning(ctxt->userData,
671 "Entity(%s) already defined in the external subset\n", name);
672 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
673 xmlChar *URI;
674 const char *base = NULL;
675
676 if (ctxt->input != NULL)
677 base = ctxt->input->filename;
678 if (base == NULL)
679 base = ctxt->directory;
680
681 URI = xmlBuildURI(systemId, (const xmlChar *) base);
682 ent->URI = URI;
683 }
684 } else {
685 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
686 "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
687 name, NULL);
688 }
689}
690
691/**
692 * xmlSAX2AttributeDecl:
693 * @ctx: the user data (XML parser context)
694 * @elem: the name of the element
695 * @fullname: the attribute name
696 * @type: the attribute type
697 * @def: the type of default value
698 * @defaultValue: the attribute default value
699 * @tree: the tree of enumerated value set
700 *
701 * An attribute definition has been parsed
702 */
703void
704xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
705 int type, int def, const xmlChar *defaultValue,
706 xmlEnumerationPtr tree)
707{
708 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
709 xmlAttributePtr attr;
710 xmlChar *name = NULL, *prefix = NULL;
711
712 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
713 return;
714
715#ifdef DEBUG_SAX
716 xmlGenericError(xmlGenericErrorContext,
717 "SAX.xmlSAX2AttributeDecl(%s, %s, %d, %d, %s, ...)\n",
718 elem, fullname, type, def, defaultValue);
719#endif
720 if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
721 (type != XML_ATTRIBUTE_ID)) {
722 /*
723 * Raise the error but keep the validity flag
724 */
725 int tmp = ctxt->valid;
726 xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
727 "xml:id : attribute type should be ID\n", NULL, NULL);
728 ctxt->valid = tmp;
729 }
730 /* TODO: optimize name/prefix allocation */
731 name = xmlSplitQName(ctxt, fullname, &prefix);
732 ctxt->vctxt.valid = 1;
733 if (ctxt->inSubset == 1)
734 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
735 name, prefix, (xmlAttributeType) type,
736 (xmlAttributeDefault) def, defaultValue, tree);
737 else if (ctxt->inSubset == 2)
738 attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
739 name, prefix, (xmlAttributeType) type,
740 (xmlAttributeDefault) def, defaultValue, tree);
741 else {
742 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
743 "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
744 name, NULL);
745 xmlFree(name);
746 xmlFreeEnumeration(tree);
747 return;
748 }
749#ifdef LIBXML_VALID_ENABLED
750 if (ctxt->vctxt.valid == 0)
751 ctxt->valid = 0;
752 if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
753 (ctxt->myDoc->intSubset != NULL))
754 ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
755 attr);
756#endif /* LIBXML_VALID_ENABLED */
757 if (prefix != NULL)
758 xmlFree(prefix);
759 if (name != NULL)
760 xmlFree(name);
761}
762
763/**
764 * xmlSAX2ElementDecl:
765 * @ctx: the user data (XML parser context)
766 * @name: the element name
767 * @type: the element type
768 * @content: the element value tree
769 *
770 * An element definition has been parsed
771 */
772void
773xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
774 xmlElementContentPtr content)
775{
776 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
777 xmlElementPtr elem = NULL;
778
779 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
780 return;
781
782#ifdef DEBUG_SAX
783 xmlGenericError(xmlGenericErrorContext,
784 "SAX.xmlSAX2ElementDecl(%s, %d, ...)\n", name, type);
785#endif
786
787 if (ctxt->inSubset == 1)
788 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
789 name, (xmlElementTypeVal) type, content);
790 else if (ctxt->inSubset == 2)
791 elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
792 name, (xmlElementTypeVal) type, content);
793 else {
794 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
795 "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
796 name, NULL);
797 return;
798 }
799#ifdef LIBXML_VALID_ENABLED
800 if (elem == NULL)
801 ctxt->valid = 0;
802 if (ctxt->validate && ctxt->wellFormed &&
803 ctxt->myDoc && ctxt->myDoc->intSubset)
804 ctxt->valid &=
805 xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
806#endif /* LIBXML_VALID_ENABLED */
807}
808
809/**
810 * xmlSAX2NotationDecl:
811 * @ctx: the user data (XML parser context)
812 * @name: The name of the notation
813 * @publicId: The public ID of the entity
814 * @systemId: The system ID of the entity
815 *
816 * What to do when a notation declaration has been parsed.
817 */
818void
819xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
820 const xmlChar *publicId, const xmlChar *systemId)
821{
822 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
823 xmlNotationPtr nota = NULL;
824
825 if ((ctxt == NULL) || (ctxt->myDoc == NULL))
826 return;
827
828#ifdef DEBUG_SAX
829 xmlGenericError(xmlGenericErrorContext,
830 "SAX.xmlSAX2NotationDecl(%s, %s, %s)\n", name, publicId, systemId);
831#endif
832
833 if ((publicId == NULL) && (systemId == NULL)) {
834 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
835 "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
836 name, NULL);
837 return;
838 } else if (ctxt->inSubset == 1)
839 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
840 publicId, systemId);
841 else if (ctxt->inSubset == 2)
842 nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
843 publicId, systemId);
844 else {
845 xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
846 "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
847 name, NULL);
848 return;
849 }
850#ifdef LIBXML_VALID_ENABLED
851 if (nota == NULL) ctxt->valid = 0;
852 if ((ctxt->validate) && (ctxt->wellFormed) &&
853 (ctxt->myDoc->intSubset != NULL))
854 ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
855 nota);
856#endif /* LIBXML_VALID_ENABLED */
857}
858
859/**
860 * xmlSAX2UnparsedEntityDecl:
861 * @ctx: the user data (XML parser context)
862 * @name: The name of the entity
863 * @publicId: The public ID of the entity
864 * @systemId: The system ID of the entity
865 * @notationName: the name of the notation
866 *
867 * What to do when an unparsed entity declaration is parsed
868 */
869void
870xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
871 const xmlChar *publicId, const xmlChar *systemId,
872 const xmlChar *notationName)
873{
874 xmlEntityPtr ent;
875 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
876 if (ctx == NULL) return;
877#ifdef DEBUG_SAX
878 xmlGenericError(xmlGenericErrorContext,
879 "SAX.xmlSAX2UnparsedEntityDecl(%s, %s, %s, %s)\n",
880 name, publicId, systemId, notationName);
881#endif
882 if (ctxt->inSubset == 1) {
883 ent = xmlAddDocEntity(ctxt->myDoc, name,
884 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
885 publicId, systemId, notationName);
886 if ((ent == NULL) && (ctxt->pedantic) &&
887 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
888 ctxt->sax->warning(ctxt->userData,
889 "Entity(%s) already defined in the internal subset\n", name);
890 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
891 xmlChar *URI;
892 const char *base = NULL;
893
894 if (ctxt->input != NULL)
895 base = ctxt->input->filename;
896 if (base == NULL)
897 base = ctxt->directory;
898
899 URI = xmlBuildURI(systemId, (const xmlChar *) base);
900 ent->URI = URI;
901 }
902 } else if (ctxt->inSubset == 2) {
903 ent = xmlAddDtdEntity(ctxt->myDoc, name,
904 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
905 publicId, systemId, notationName);
906 if ((ent == NULL) && (ctxt->pedantic) &&
907 (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
908 ctxt->sax->warning(ctxt->userData,
909 "Entity(%s) already defined in the external subset\n", name);
910 if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
911 xmlChar *URI;
912 const char *base = NULL;
913
914 if (ctxt->input != NULL)
915 base = ctxt->input->filename;
916 if (base == NULL)
917 base = ctxt->directory;
918
919 URI = xmlBuildURI(systemId, (const xmlChar *) base);
920 ent->URI = URI;
921 }
922 } else {
923 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
924 "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
925 name, NULL);
926 }
927}
928
929/**
930 * xmlSAX2SetDocumentLocator:
931 * @ctx: the user data (XML parser context)
932 * @loc: A SAX Locator
933 *
934 * Receive the document locator at startup, actually xmlDefaultSAXLocator
935 * Everything is available on the context, so this is useless in our case.
936 */
937void
938xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
939{
940 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
941#ifdef DEBUG_SAX
942 xmlGenericError(xmlGenericErrorContext,
943 "SAX.xmlSAX2SetDocumentLocator()\n");
944#endif
945}
946
947/**
948 * xmlSAX2StartDocument:
949 * @ctx: the user data (XML parser context)
950 *
951 * called when the document start being processed.
952 */
953void
954xmlSAX2StartDocument(void *ctx)
955{
956 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
957 xmlDocPtr doc;
958
959 if (ctx == NULL) return;
960
961#ifdef DEBUG_SAX
962 xmlGenericError(xmlGenericErrorContext,
963 "SAX.xmlSAX2StartDocument()\n");
964#endif
965 if (ctxt->html) {
966#ifdef LIBXML_HTML_ENABLED
967 if (ctxt->myDoc == NULL)
968 ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
969 if (ctxt->myDoc == NULL) {
970 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
971 return;
972 }
973 ctxt->myDoc->properties = XML_DOC_HTML;
974 ctxt->myDoc->parseFlags = ctxt->options;
975#else
976 xmlGenericError(xmlGenericErrorContext,
977 "libxml2 built without HTML support\n");
978 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
979 ctxt->instate = XML_PARSER_EOF;
980 ctxt->disableSAX = 1;
981 return;
982#endif
983 } else {
984 doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
985 if (doc != NULL) {
986 doc->properties = 0;
987 if (ctxt->options & XML_PARSE_OLD10)
988 doc->properties |= XML_DOC_OLD10;
989 doc->parseFlags = ctxt->options;
990 if (ctxt->encoding != NULL)
991 doc->encoding = xmlStrdup(ctxt->encoding);
992 else
993 doc->encoding = NULL;
994 doc->standalone = ctxt->standalone;
995 } else {
996 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
997 return;
998 }
999 if ((ctxt->dictNames) && (doc != NULL)) {
1000 doc->dict = ctxt->dict;
1001 xmlDictReference(doc->dict);
1002 }
1003 }
1004 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
1005 (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
1006 ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
1007 if (ctxt->myDoc->URL == NULL)
1008 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
1009 }
1010}
1011
1012/**
1013 * xmlSAX2EndDocument:
1014 * @ctx: the user data (XML parser context)
1015 *
1016 * called when the document end has been detected.
1017 */
1018void
1019xmlSAX2EndDocument(void *ctx)
1020{
1021 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1022#ifdef DEBUG_SAX
1023 xmlGenericError(xmlGenericErrorContext,
1024 "SAX.xmlSAX2EndDocument()\n");
1025#endif
1026 if (ctx == NULL) return;
1027#ifdef LIBXML_VALID_ENABLED
1028 if (ctxt->validate && ctxt->wellFormed &&
1029 ctxt->myDoc && ctxt->myDoc->intSubset)
1030 ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
1031#endif /* LIBXML_VALID_ENABLED */
1032
1033 /*
1034 * Grab the encoding if it was added on-the-fly
1035 */
1036 if ((ctxt->encoding != NULL) && (ctxt->myDoc != NULL) &&
1037 (ctxt->myDoc->encoding == NULL)) {
1038 ctxt->myDoc->encoding = ctxt->encoding;
1039 ctxt->encoding = NULL;
1040 }
1041 if ((ctxt->inputTab != NULL) &&
1042 (ctxt->inputNr > 0) && (ctxt->inputTab[0] != NULL) &&
1043 (ctxt->inputTab[0]->encoding != NULL) && (ctxt->myDoc != NULL) &&
1044 (ctxt->myDoc->encoding == NULL)) {
1045 ctxt->myDoc->encoding = xmlStrdup(ctxt->inputTab[0]->encoding);
1046 }
1047 if ((ctxt->charset != XML_CHAR_ENCODING_NONE) && (ctxt->myDoc != NULL) &&
1048 (ctxt->myDoc->charset == XML_CHAR_ENCODING_NONE)) {
1049 ctxt->myDoc->charset = ctxt->charset;
1050 }
1051}
1052
1053#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
1054/**
1055 * xmlSAX2AttributeInternal:
1056 * @ctx: the user data (XML parser context)
1057 * @fullname: The attribute name, including namespace prefix
1058 * @value: The attribute value
1059 * @prefix: the prefix on the element node
1060 *
1061 * Handle an attribute that has been read by the parser.
1062 * The default handling is to convert the attribute into an
1063 * DOM subtree and past it in a new xmlAttr element added to
1064 * the element.
1065 */
1066static void
1067xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
1068 const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
1069{
1070 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1071 xmlAttrPtr ret;
1072 xmlChar *name;
1073 xmlChar *ns;
1074 xmlChar *nval;
1075 xmlNsPtr namespace;
1076
1077 if (ctxt->html) {
1078 name = xmlStrdup(fullname);
1079 ns = NULL;
1080 namespace = NULL;
1081 } else {
1082 /*
1083 * Split the full name into a namespace prefix and the tag name
1084 */
1085 name = xmlSplitQName(ctxt, fullname, &ns);
1086 if ((name != NULL) && (name[0] == 0)) {
1087 if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
1088 xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
1089 "invalid namespace declaration '%s'\n",
1090 fullname, NULL);
1091 } else {
1092 xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
1093 "Avoid attribute ending with ':' like '%s'\n",
1094 fullname, NULL);
1095 }
1096 if (ns != NULL)
1097 xmlFree(ns);
1098 ns = NULL;
1099 xmlFree(name);
1100 name = xmlStrdup(fullname);
1101 }
1102 }
1103 if (name == NULL) {
1104 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1105 if (ns != NULL)
1106 xmlFree(ns);
1107 return;
1108 }
1109
1110#ifdef LIBXML_HTML_ENABLED
1111 if ((ctxt->html) &&
1112 (value == NULL) && (htmlIsBooleanAttr(fullname))) {
1113 nval = xmlStrdup(fullname);
1114 value = (const xmlChar *) nval;
1115 } else
1116#endif
1117 {
1118#ifdef LIBXML_VALID_ENABLED
1119 /*
1120 * Do the last stage of the attribute normalization
1121 * Needed for HTML too:
1122 * http://www.w3.org/TR/html4/types.html#h-6.2
1123 */
1124 ctxt->vctxt.valid = 1;
1125 nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
1126 ctxt->myDoc, ctxt->node,
1127 fullname, value);
1128 if (ctxt->vctxt.valid != 1) {
1129 ctxt->valid = 0;
1130 }
1131 if (nval != NULL)
1132 value = nval;
1133#else
1134 nval = NULL;
1135#endif /* LIBXML_VALID_ENABLED */
1136 }
1137
1138 /*
1139 * Check whether it's a namespace definition
1140 */
1141 if ((!ctxt->html) && (ns == NULL) &&
1142 (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
1143 (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
1144 xmlNsPtr nsret;
1145 xmlChar *val;
1146
1147 if (!ctxt->replaceEntities) {
1148 ctxt->depth++;
1149 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1150 0,0,0);
1151 ctxt->depth--;
1152 if (val == NULL) {
1153 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1154 if (name != NULL)
1155 xmlFree(name);
1156 if (nval != NULL)
1157 xmlFree(nval);
1158 return;
1159 }
1160 } else {
1161 val = (xmlChar *) value;
1162 }
1163
1164 if (val[0] != 0) {
1165 xmlURIPtr uri;
1166
1167 uri = xmlParseURI((const char *)val);
1168 if (uri == NULL) {
1169 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1170 ctxt->sax->warning(ctxt->userData,
1171 "xmlns: %s not a valid URI\n", val);
1172 } else {
1173 if (uri->scheme == NULL) {
1174 if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
1175 ctxt->sax->warning(ctxt->userData,
1176 "xmlns: URI %s is not absolute\n", val);
1177 }
1178 xmlFreeURI(uri);
1179 }
1180 }
1181
1182 /* a default namespace definition */
1183 nsret = xmlNewNs(ctxt->node, val, NULL);
1184
1185#ifdef LIBXML_VALID_ENABLED
1186 /*
1187 * Validate also for namespace decls, they are attributes from
1188 * an XML-1.0 perspective
1189 */
1190 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1191 ctxt->myDoc && ctxt->myDoc->intSubset)
1192 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1193 ctxt->node, prefix, nsret, val);
1194#endif /* LIBXML_VALID_ENABLED */
1195 if (name != NULL)
1196 xmlFree(name);
1197 if (nval != NULL)
1198 xmlFree(nval);
1199 if (val != value)
1200 xmlFree(val);
1201 return;
1202 }
1203 if ((!ctxt->html) &&
1204 (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
1205 (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
1206 xmlNsPtr nsret;
1207 xmlChar *val;
1208
1209 if (!ctxt->replaceEntities) {
1210 ctxt->depth++;
1211 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1212 0,0,0);
1213 ctxt->depth--;
1214 if (val == NULL) {
1215 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1216 xmlFree(ns);
1217 if (name != NULL)
1218 xmlFree(name);
1219 if (nval != NULL)
1220 xmlFree(nval);
1221 return;
1222 }
1223 } else {
1224 val = (xmlChar *) value;
1225 }
1226
1227 if (val[0] == 0) {
1228 xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
1229 "Empty namespace name for prefix %s\n", name, NULL);
1230 }
1231 if ((ctxt->pedantic != 0) && (val[0] != 0)) {
1232 xmlURIPtr uri;
1233
1234 uri = xmlParseURI((const char *)val);
1235 if (uri == NULL) {
1236 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
1237 "xmlns:%s: %s not a valid URI\n", name, value);
1238 } else {
1239 if (uri->scheme == NULL) {
1240 xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
1241 "xmlns:%s: URI %s is not absolute\n", name, value);
1242 }
1243 xmlFreeURI(uri);
1244 }
1245 }
1246
1247 /* a standard namespace definition */
1248 nsret = xmlNewNs(ctxt->node, val, name);
1249 xmlFree(ns);
1250#ifdef LIBXML_VALID_ENABLED
1251 /*
1252 * Validate also for namespace decls, they are attributes from
1253 * an XML-1.0 perspective
1254 */
1255 if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
1256 ctxt->myDoc && ctxt->myDoc->intSubset)
1257 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
1258 ctxt->node, prefix, nsret, value);
1259#endif /* LIBXML_VALID_ENABLED */
1260 if (name != NULL)
1261 xmlFree(name);
1262 if (nval != NULL)
1263 xmlFree(nval);
1264 if (val != value)
1265 xmlFree(val);
1266 return;
1267 }
1268
1269 if (ns != NULL) {
1270 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
1271
1272 if (namespace == NULL) {
1273 xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1274 "Namespace prefix %s of attribute %s is not defined\n",
1275 ns, name);
1276 } else {
1277 xmlAttrPtr prop;
1278
1279 prop = ctxt->node->properties;
1280 while (prop != NULL) {
1281 if (prop->ns != NULL) {
1282 if ((xmlStrEqual(name, prop->name)) &&
1283 ((namespace == prop->ns) ||
1284 (xmlStrEqual(namespace->href, prop->ns->href)))) {
1285 xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
1286 "Attribute %s in %s redefined\n",
1287 name, namespace->href);
1288 ctxt->wellFormed = 0;
1289 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1290 if (name != NULL)
1291 xmlFree(name);
1292 goto error;
1293 }
1294 }
1295 prop = prop->next;
1296 }
1297 }
1298 } else {
1299 namespace = NULL;
1300 }
1301
1302 /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
1303 ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
1304
1305 if (ret != NULL) {
1306 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
1307 xmlNodePtr tmp;
1308
1309 ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
1310 tmp = ret->children;
1311 while (tmp != NULL) {
1312 tmp->parent = (xmlNodePtr) ret;
1313 if (tmp->next == NULL)
1314 ret->last = tmp;
1315 tmp = tmp->next;
1316 }
1317 } else if (value != NULL) {
1318 ret->children = xmlNewDocText(ctxt->myDoc, value);
1319 ret->last = ret->children;
1320 if (ret->children != NULL)
1321 ret->children->parent = (xmlNodePtr) ret;
1322 }
1323 }
1324
1325#ifdef LIBXML_VALID_ENABLED
1326 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
1327 ctxt->myDoc && ctxt->myDoc->intSubset) {
1328
1329 /*
1330 * If we don't substitute entities, the validation should be
1331 * done on a value with replaced entities anyway.
1332 */
1333 if (!ctxt->replaceEntities) {
1334 xmlChar *val;
1335
1336 ctxt->depth++;
1337 val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
1338 0,0,0);
1339 ctxt->depth--;
1340
1341 if (val == NULL)
1342 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1343 ctxt->myDoc, ctxt->node, ret, value);
1344 else {
1345 xmlChar *nvalnorm;
1346
1347 /*
1348 * Do the last stage of the attribute normalization
1349 * It need to be done twice ... it's an extra burden related
1350 * to the ability to keep xmlSAX2References in attributes
1351 */
1352 nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
1353 ctxt->node, fullname, val);
1354 if (nvalnorm != NULL) {
1355 xmlFree(val);
1356 val = nvalnorm;
1357 }
1358
1359 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
1360 ctxt->myDoc, ctxt->node, ret, val);
1361 xmlFree(val);
1362 }
1363 } else {
1364 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
1365 ctxt->node, ret, value);
1366 }
1367 } else
1368#endif /* LIBXML_VALID_ENABLED */
1369 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
1370 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
1371 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
1372 /*
1373 * when validating, the ID registration is done at the attribute
1374 * validation level. Otherwise we have to do specific handling here.
1375 */
1376 if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
1377 /*
1378 * Add the xml:id value
1379 *
1380 * Open issue: normalization of the value.
1381 */
1382 if (xmlValidateNCName(value, 1) != 0) {
1383 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
1384 "xml:id : attribute value %s is not an NCName\n",
1385 (const char *) value, NULL);
1386 }
1387 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1388 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
1389 xmlAddID(&ctxt->vctxt, ctxt->myDoc, value, ret);
1390 else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
1391 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, value, ret);
1392 }
1393
1394error:
1395 if (nval != NULL)
1396 xmlFree(nval);
1397 if (ns != NULL)
1398 xmlFree(ns);
1399}
1400
1401/*
1402 * xmlCheckDefaultedAttributes:
1403 *
1404 * Check defaulted attributes from the DTD
1405 */
1406static void
1407xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
1408 const xmlChar *prefix, const xmlChar **atts) {
1409 xmlElementPtr elemDecl;
1410 const xmlChar *att;
1411 int internal = 1;
1412 int i;
1413
1414 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
1415 if (elemDecl == NULL) {
1416 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
1417 internal = 0;
1418 }
1419
1420process_external_subset:
1421
1422 if (elemDecl != NULL) {
1423 xmlAttributePtr attr = elemDecl->attributes;
1424 /*
1425 * Check against defaulted attributes from the external subset
1426 * if the document is stamped as standalone
1427 */
1428 if ((ctxt->myDoc->standalone == 1) &&
1429 (ctxt->myDoc->extSubset != NULL) &&
1430 (ctxt->validate)) {
1431 while (attr != NULL) {
1432 if ((attr->defaultValue != NULL) &&
1433 (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
1434 attr->elem, attr->name,
1435 attr->prefix) == attr) &&
1436 (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1437 attr->elem, attr->name,
1438 attr->prefix) == NULL)) {
1439 xmlChar *fulln;
1440
1441 if (attr->prefix != NULL) {
1442 fulln = xmlStrdup(attr->prefix);
1443 fulln = xmlStrcat(fulln, BAD_CAST ":");
1444 fulln = xmlStrcat(fulln, attr->name);
1445 } else {
1446 fulln = xmlStrdup(attr->name);
1447 }
1448 if (fulln == NULL) {
1449 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1450 break;
1451 }
1452
1453 /*
1454 * Check that the attribute is not declared in the
1455 * serialization
1456 */
1457 att = NULL;
1458 if (atts != NULL) {
1459 i = 0;
1460 att = atts[i];
1461 while (att != NULL) {
1462 if (xmlStrEqual(att, fulln))
1463 break;
1464 i += 2;
1465 att = atts[i];
1466 }
1467 }
1468 if (att == NULL) {
1469 xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
1470 "standalone: attribute %s on %s defaulted from external subset\n",
1471 (const char *)fulln,
1472 (const char *)attr->elem);
1473 }
1474 xmlFree(fulln);
1475 }
1476 attr = attr->nexth;
1477 }
1478 }
1479
1480 /*
1481 * Actually insert defaulted values when needed
1482 */
1483 attr = elemDecl->attributes;
1484 while (attr != NULL) {
1485 /*
1486 * Make sure that attributes redefinition occurring in the
1487 * internal subset are not overridden by definitions in the
1488 * external subset.
1489 */
1490 if (attr->defaultValue != NULL) {
1491 /*
1492 * the element should be instantiated in the tree if:
1493 * - this is a namespace prefix
1494 * - the user required for completion in the tree
1495 * like XSLT
1496 * - there isn't already an attribute definition
1497 * in the internal subset overriding it.
1498 */
1499 if (((attr->prefix != NULL) &&
1500 (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
1501 ((attr->prefix == NULL) &&
1502 (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
1503 (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
1504 xmlAttributePtr tst;
1505
1506 tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
1507 attr->elem, attr->name,
1508 attr->prefix);
1509 if ((tst == attr) || (tst == NULL)) {
1510 xmlChar fn[50];
1511 xmlChar *fulln;
1512
1513 fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
1514 if (fulln == NULL) {
1515 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1516 return;
1517 }
1518
1519 /*
1520 * Check that the attribute is not declared in the
1521 * serialization
1522 */
1523 att = NULL;
1524 if (atts != NULL) {
1525 i = 0;
1526 att = atts[i];
1527 while (att != NULL) {
1528 if (xmlStrEqual(att, fulln))
1529 break;
1530 i += 2;
1531 att = atts[i];
1532 }
1533 }
1534 if (att == NULL) {
1535 xmlSAX2AttributeInternal(ctxt, fulln,
1536 attr->defaultValue, prefix);
1537 }
1538 if ((fulln != fn) && (fulln != attr->name))
1539 xmlFree(fulln);
1540 }
1541 }
1542 }
1543 attr = attr->nexth;
1544 }
1545 if (internal == 1) {
1546 elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
1547 name, prefix);
1548 internal = 0;
1549 goto process_external_subset;
1550 }
1551 }
1552}
1553
1554/**
1555 * xmlSAX2StartElement:
1556 * @ctx: the user data (XML parser context)
1557 * @fullname: The element name, including namespace prefix
1558 * @atts: An array of name/value attributes pairs, NULL terminated
1559 *
1560 * called when an opening tag has been processed.
1561 */
1562void
1563xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
1564{
1565 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1566 xmlNodePtr ret;
1567 xmlNodePtr parent;
1568 xmlNsPtr ns;
1569 xmlChar *name;
1570 xmlChar *prefix;
1571 const xmlChar *att;
1572 const xmlChar *value;
1573 int i;
1574
1575 if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
1576 parent = ctxt->node;
1577#ifdef DEBUG_SAX
1578 xmlGenericError(xmlGenericErrorContext,
1579 "SAX.xmlSAX2StartElement(%s)\n", fullname);
1580#endif
1581
1582 /*
1583 * First check on validity:
1584 */
1585 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
1586 ((ctxt->myDoc->intSubset == NULL) ||
1587 ((ctxt->myDoc->intSubset->notations == NULL) &&
1588 (ctxt->myDoc->intSubset->elements == NULL) &&
1589 (ctxt->myDoc->intSubset->attributes == NULL) &&
1590 (ctxt->myDoc->intSubset->entities == NULL)))) {
1591 xmlErrValid(ctxt, XML_ERR_NO_DTD,
1592 "Validation failed: no DTD found !", NULL, NULL);
1593 ctxt->validate = 0;
1594 }
1595
1596
1597 /*
1598 * Split the full name into a namespace prefix and the tag name
1599 */
1600 name = xmlSplitQName(ctxt, fullname, &prefix);
1601
1602
1603 /*
1604 * Note : the namespace resolution is deferred until the end of the
1605 * attributes parsing, since local namespace can be defined as
1606 * an attribute at this level.
1607 */
1608 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
1609 if (ret == NULL) {
1610 if (prefix != NULL)
1611 xmlFree(prefix);
1612 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
1613 return;
1614 }
1615 if (ctxt->myDoc->children == NULL) {
1616#ifdef DEBUG_SAX_TREE
1617 xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
1618#endif
1619 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
1620 } else if (parent == NULL) {
1621 parent = ctxt->myDoc->children;
1622 }
1623 ctxt->nodemem = -1;
1624 if (ctxt->linenumbers) {
1625 if (ctxt->input != NULL) {
1626 if (ctxt->input->line < USHRT_MAX)
1627 ret->line = (unsigned short) ctxt->input->line;
1628 else
1629 ret->line = USHRT_MAX;
1630 }
1631 }
1632
1633 /*
1634 * We are parsing a new node.
1635 */
1636#ifdef DEBUG_SAX_TREE
1637 xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
1638#endif
1639 if (nodePush(ctxt, ret) < 0) {
1640 xmlUnlinkNode(ret);
1641 xmlFreeNode(ret);
1642 if (prefix != NULL)
1643 xmlFree(prefix);
1644 return;
1645 }
1646
1647 /*
1648 * Link the child element
1649 */
1650 if (parent != NULL) {
1651 if (parent->type == XML_ELEMENT_NODE) {
1652#ifdef DEBUG_SAX_TREE
1653 xmlGenericError(xmlGenericErrorContext,
1654 "adding child %s to %s\n", name, parent->name);
1655#endif
1656 xmlAddChild(parent, ret);
1657 } else {
1658#ifdef DEBUG_SAX_TREE
1659 xmlGenericError(xmlGenericErrorContext,
1660 "adding sibling %s to ", name);
1661 xmlDebugDumpOneNode(stderr, parent, 0);
1662#endif
1663 xmlAddSibling(parent, ret);
1664 }
1665 }
1666
1667 if (!ctxt->html) {
1668 /*
1669 * Insert all the defaulted attributes from the DTD especially
1670 * namespaces
1671 */
1672 if ((ctxt->myDoc->intSubset != NULL) ||
1673 (ctxt->myDoc->extSubset != NULL)) {
1674 xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
1675 }
1676
1677 /*
1678 * process all the attributes whose name start with "xmlns"
1679 */
1680 if (atts != NULL) {
1681 i = 0;
1682 att = atts[i++];
1683 value = atts[i++];
1684 while ((att != NULL) && (value != NULL)) {
1685 if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
1686 (att[3] == 'n') && (att[4] == 's'))
1687 xmlSAX2AttributeInternal(ctxt, att, value, prefix);
1688
1689 att = atts[i++];
1690 value = atts[i++];
1691 }
1692 }
1693
1694 /*
1695 * Search the namespace, note that since the attributes have been
1696 * processed, the local namespaces are available.
1697 */
1698 ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
1699 if ((ns == NULL) && (parent != NULL))
1700 ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
1701 if ((prefix != NULL) && (ns == NULL)) {
1702 ns = xmlNewNs(ret, NULL, prefix);
1703 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
1704 "Namespace prefix %s is not defined\n",
1705 prefix, NULL);
1706 }
1707
1708 /*
1709 * set the namespace node, making sure that if the default namespace
1710 * is unbound on a parent we simply keep it NULL
1711 */
1712 if ((ns != NULL) && (ns->href != NULL) &&
1713 ((ns->href[0] != 0) || (ns->prefix != NULL)))
1714 xmlSetNs(ret, ns);
1715 }
1716
1717 /*
1718 * process all the other attributes
1719 */
1720 if (atts != NULL) {
1721 i = 0;
1722 att = atts[i++];
1723 value = atts[i++];
1724 if (ctxt->html) {
1725 while (att != NULL) {
1726 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1727 att = atts[i++];
1728 value = atts[i++];
1729 }
1730 } else {
1731 while ((att != NULL) && (value != NULL)) {
1732 if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
1733 (att[3] != 'n') || (att[4] != 's'))
1734 xmlSAX2AttributeInternal(ctxt, att, value, NULL);
1735
1736 /*
1737 * Next ones
1738 */
1739 att = atts[i++];
1740 value = atts[i++];
1741 }
1742 }
1743 }
1744
1745#ifdef LIBXML_VALID_ENABLED
1746 /*
1747 * If it's the Document root, finish the DTD validation and
1748 * check the document root element for validity
1749 */
1750 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
1751 int chk;
1752
1753 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
1754 if (chk <= 0)
1755 ctxt->valid = 0;
1756 if (chk < 0)
1757 ctxt->wellFormed = 0;
1758 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
1759 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
1760 }
1761#endif /* LIBXML_VALID_ENABLED */
1762
1763 if (prefix != NULL)
1764 xmlFree(prefix);
1765
1766}
1767
1768/**
1769 * xmlSAX2EndElement:
1770 * @ctx: the user data (XML parser context)
1771 * @name: The element name
1772 *
1773 * called when the end of an element has been detected.
1774 */
1775void
1776xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
1777{
1778 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
1779 xmlNodePtr cur;
1780
1781 if (ctx == NULL) return;
1782 cur = ctxt->node;
1783#ifdef DEBUG_SAX
1784 if (name == NULL)
1785 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(NULL)\n");
1786 else
1787 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name);
1788#endif
1789
1790 /* Capture end position and add node */
1791 if (cur != NULL && ctxt->record_info) {
1792 ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base;
1793 ctxt->nodeInfo->end_line = ctxt->input->line;
1794 ctxt->nodeInfo->node = cur;
1795 xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
1796 }
1797 ctxt->nodemem = -1;
1798
1799#ifdef LIBXML_VALID_ENABLED
1800 if (ctxt->validate && ctxt->wellFormed &&
1801 ctxt->myDoc && ctxt->myDoc->intSubset)
1802 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
1803 cur);
1804#endif /* LIBXML_VALID_ENABLED */
1805
1806
1807 /*
1808 * end of parsing of this node.
1809 */
1810#ifdef DEBUG_SAX_TREE
1811 xmlGenericError(xmlGenericErrorContext, "popping(%s)\n", cur->name);
1812#endif
1813 nodePop(ctxt);
1814}
1815#endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLED || LIBXML_LEGACY_ENABLED */
1816
1817/*
1818 * xmlSAX2TextNode:
1819 * @ctxt: the parser context
1820 * @str: the input string
1821 * @len: the string length
1822 *
1823 * Callback for a text node
1824 *
1825 * Returns the newly allocated string or NULL if not needed or error
1826 */
1827static xmlNodePtr
1828xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1829 xmlNodePtr ret;
1830 const xmlChar *intern = NULL;
1831
1832 /*
1833 * Allocate
1834 */
1835 if (ctxt->freeElems != NULL) {
1836 ret = ctxt->freeElems;
1837 ctxt->freeElems = ret->next;
1838 ctxt->freeElemsNr--;
1839 } else {
1840 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
1841 }
1842 if (ret == NULL) {
1843 xmlErrMemory(ctxt, "xmlSAX2Characters");
1844 return(NULL);
1845 }
1846 memset(ret, 0, sizeof(xmlNode));
1847 /*
1848 * intern the formatting blanks found between tags, or the
1849 * very short strings
1850 */
1851 if (ctxt->dictNames) {
1852 xmlChar cur = str[len];
1853
1854 if ((len < (int) (2 * sizeof(void *))) &&
1855 (ctxt->options & XML_PARSE_COMPACT)) {
1856 /* store the string in the node overriding properties and nsDef */
1857 xmlChar *tmp = (xmlChar *) &(ret->properties);
1858 memcpy(tmp, str, len);
1859 tmp[len] = 0;
1860 intern = tmp;
1861 } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
1862 ((cur == '<') && (str[len + 1] != '!')))) {
1863 intern = xmlDictLookup(ctxt->dict, str, len);
1864 } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
1865 (str[len + 1] != '!')) {
1866 int i;
1867
1868 for (i = 1;i < len;i++) {
1869 if (!IS_BLANK_CH(str[i])) goto skip;
1870 }
1871 intern = xmlDictLookup(ctxt->dict, str, len);
1872 }
1873 }
1874skip:
1875 ret->type = XML_TEXT_NODE;
1876
1877 ret->name = xmlStringText;
1878 if (intern == NULL) {
1879 ret->content = xmlStrndup(str, len);
1880 if (ret->content == NULL) {
1881 xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
1882 xmlFree(ret);
1883 return(NULL);
1884 }
1885 } else
1886 ret->content = (xmlChar *) intern;
1887
1888 if (ctxt->linenumbers) {
1889 if (ctxt->input != NULL) {
1890 if (ctxt->input->line < USHRT_MAX)
1891 ret->line = (unsigned short) ctxt->input->line;
1892 else {
1893 ret->line = USHRT_MAX;
1894 if (ctxt->options & XML_PARSE_BIG_LINES)
1895 ret->psvi = (void *) (ptrdiff_t) ctxt->input->line;
1896 }
1897 }
1898 }
1899
1900 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1901 xmlRegisterNodeDefaultValue(ret);
1902 return(ret);
1903}
1904
1905#ifdef LIBXML_VALID_ENABLED
1906/*
1907 * xmlSAX2DecodeAttrEntities:
1908 * @ctxt: the parser context
1909 * @str: the input string
1910 * @len: the string length
1911 *
1912 * Remove the entities from an attribute value
1913 *
1914 * Returns the newly allocated string or NULL if not needed or error
1915 */
1916static xmlChar *
1917xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
1918 const xmlChar *end) {
1919 const xmlChar *in;
1920 xmlChar *ret;
1921
1922 in = str;
1923 while (in < end)
1924 if (*in++ == '&')
1925 goto decode;
1926 return(NULL);
1927decode:
1928 ctxt->depth++;
1929 ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
1930 XML_SUBSTITUTE_REF, 0,0,0);
1931 ctxt->depth--;
1932 return(ret);
1933}
1934#endif /* LIBXML_VALID_ENABLED */
1935
1936/**
1937 * xmlSAX2AttributeNs:
1938 * @ctx: the user data (XML parser context)
1939 * @localname: the local name of the attribute
1940 * @prefix: the attribute namespace prefix if available
1941 * @URI: the attribute namespace name if available
1942 * @value: Start of the attribute value
1943 * @valueend: end of the attribute value
1944 *
1945 * Handle an attribute that has been read by the parser.
1946 * The default handling is to convert the attribute into an
1947 * DOM subtree and past it in a new xmlAttr element added to
1948 * the element.
1949 */
1950static void
1951xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
1952 const xmlChar * localname,
1953 const xmlChar * prefix,
1954 const xmlChar * value,
1955 const xmlChar * valueend)
1956{
1957 xmlAttrPtr ret;
1958 xmlNsPtr namespace = NULL;
1959 xmlChar *dup = NULL;
1960
1961 /*
1962 * Note: if prefix == NULL, the attribute is not in the default namespace
1963 */
1964 if (prefix != NULL)
1965 namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
1966
1967 /*
1968 * allocate the node
1969 */
1970 if (ctxt->freeAttrs != NULL) {
1971 ret = ctxt->freeAttrs;
1972 ctxt->freeAttrs = ret->next;
1973 ctxt->freeAttrsNr--;
1974 memset(ret, 0, sizeof(xmlAttr));
1975 ret->type = XML_ATTRIBUTE_NODE;
1976
1977 ret->parent = ctxt->node;
1978 ret->doc = ctxt->myDoc;
1979 ret->ns = namespace;
1980
1981 if (ctxt->dictNames)
1982 ret->name = localname;
1983 else
1984 ret->name = xmlStrdup(localname);
1985
1986 /* link at the end to preserve order, TODO speed up with a last */
1987 if (ctxt->node->properties == NULL) {
1988 ctxt->node->properties = ret;
1989 } else {
1990 xmlAttrPtr prev = ctxt->node->properties;
1991
1992 while (prev->next != NULL) prev = prev->next;
1993 prev->next = ret;
1994 ret->prev = prev;
1995 }
1996
1997 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
1998 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
1999 } else {
2000 if (ctxt->dictNames)
2001 ret = xmlNewNsPropEatName(ctxt->node, namespace,
2002 (xmlChar *) localname, NULL);
2003 else
2004 ret = xmlNewNsProp(ctxt->node, namespace, localname, NULL);
2005 if (ret == NULL) {
2006 xmlErrMemory(ctxt, "xmlSAX2AttributeNs");
2007 return;
2008 }
2009 }
2010
2011 if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
2012 xmlNodePtr tmp;
2013
2014 /*
2015 * We know that if there is an entity reference, then
2016 * the string has been dup'ed and terminates with 0
2017 * otherwise with ' or "
2018 */
2019 if (*valueend != 0) {
2020 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2021 ret->children = tmp;
2022 ret->last = tmp;
2023 if (tmp != NULL) {
2024 tmp->doc = ret->doc;
2025 tmp->parent = (xmlNodePtr) ret;
2026 }
2027 } else {
2028 ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
2029 valueend - value);
2030 tmp = ret->children;
2031 while (tmp != NULL) {
2032 tmp->doc = ret->doc;
2033 tmp->parent = (xmlNodePtr) ret;
2034 if (tmp->next == NULL)
2035 ret->last = tmp;
2036 tmp = tmp->next;
2037 }
2038 }
2039 } else if (value != NULL) {
2040 xmlNodePtr tmp;
2041
2042 tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
2043 ret->children = tmp;
2044 ret->last = tmp;
2045 if (tmp != NULL) {
2046 tmp->doc = ret->doc;
2047 tmp->parent = (xmlNodePtr) ret;
2048 }
2049 }
2050
2051#ifdef LIBXML_VALID_ENABLED
2052 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2053 ctxt->myDoc && ctxt->myDoc->intSubset) {
2054 /*
2055 * If we don't substitute entities, the validation should be
2056 * done on a value with replaced entities anyway.
2057 */
2058 if (!ctxt->replaceEntities) {
2059 dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
2060 if (dup == NULL) {
2061 if (*valueend == 0) {
2062 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2063 ctxt->myDoc, ctxt->node, ret, value);
2064 } else {
2065 /*
2066 * That should already be normalized.
2067 * cheaper to finally allocate here than duplicate
2068 * entry points in the full validation code
2069 */
2070 dup = xmlStrndup(value, valueend - value);
2071
2072 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2073 ctxt->myDoc, ctxt->node, ret, dup);
2074 }
2075 } else {
2076 /*
2077 * dup now contains a string of the flattened attribute
2078 * content with entities substituted. Check if we need to
2079 * apply an extra layer of normalization.
2080 * It need to be done twice ... it's an extra burden related
2081 * to the ability to keep references in attributes
2082 */
2083 if (ctxt->attsSpecial != NULL) {
2084 xmlChar *nvalnorm;
2085 xmlChar fn[50];
2086 xmlChar *fullname;
2087
2088 fullname = xmlBuildQName(localname, prefix, fn, 50);
2089 if (fullname != NULL) {
2090 ctxt->vctxt.valid = 1;
2091 nvalnorm = xmlValidCtxtNormalizeAttributeValue(
2092 &ctxt->vctxt, ctxt->myDoc,
2093 ctxt->node, fullname, dup);
2094 if (ctxt->vctxt.valid != 1)
2095 ctxt->valid = 0;
2096
2097 if ((fullname != fn) && (fullname != localname))
2098 xmlFree(fullname);
2099 if (nvalnorm != NULL) {
2100 xmlFree(dup);
2101 dup = nvalnorm;
2102 }
2103 }
2104 }
2105
2106 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2107 ctxt->myDoc, ctxt->node, ret, dup);
2108 }
2109 } else {
2110 /*
2111 * if entities already have been substituted, then
2112 * the attribute as passed is already normalized
2113 */
2114 dup = xmlStrndup(value, valueend - value);
2115
2116 ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
2117 ctxt->myDoc, ctxt->node, ret, dup);
2118 }
2119 } else
2120#endif /* LIBXML_VALID_ENABLED */
2121 if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
2122 (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
2123 ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0)))) {
2124 /*
2125 * when validating, the ID registration is done at the attribute
2126 * validation level. Otherwise we have to do specific handling here.
2127 */
2128 if ((prefix == ctxt->str_xml) &&
2129 (localname[0] == 'i') && (localname[1] == 'd') &&
2130 (localname[2] == 0)) {
2131 /*
2132 * Add the xml:id value
2133 *
2134 * Open issue: normalization of the value.
2135 */
2136 if (dup == NULL)
2137 dup = xmlStrndup(value, valueend - value);
2138#if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_DOCB_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
2139#ifdef LIBXML_VALID_ENABLED
2140 if (xmlValidateNCName(dup, 1) != 0) {
2141 xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
2142 "xml:id : attribute value %s is not an NCName\n",
2143 (const char *) dup, NULL);
2144 }
2145#endif
2146#endif
2147 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2148 } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
2149 /* might be worth duplicate entry points and not copy */
2150 if (dup == NULL)
2151 dup = xmlStrndup(value, valueend - value);
2152 xmlAddID(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2153 } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
2154 if (dup == NULL)
2155 dup = xmlStrndup(value, valueend - value);
2156 xmlAddRef(&ctxt->vctxt, ctxt->myDoc, dup, ret);
2157 }
2158 }
2159 if (dup != NULL)
2160 xmlFree(dup);
2161}
2162
2163/**
2164 * xmlSAX2StartElementNs:
2165 * @ctx: the user data (XML parser context)
2166 * @localname: the local name of the element
2167 * @prefix: the element namespace prefix if available
2168 * @URI: the element namespace name if available
2169 * @nb_namespaces: number of namespace definitions on that node
2170 * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
2171 * @nb_attributes: the number of attributes on that node
2172 * @nb_defaulted: the number of defaulted attributes.
2173 * @attributes: pointer to the array of (localname/prefix/URI/value/end)
2174 * attribute values.
2175 *
2176 * SAX2 callback when an element start has been detected by the parser.
2177 * It provides the namespace information for the element, as well as
2178 * the new namespace declarations on the element.
2179 */
2180void
2181xmlSAX2StartElementNs(void *ctx,
2182 const xmlChar *localname,
2183 const xmlChar *prefix,
2184 const xmlChar *URI,
2185 int nb_namespaces,
2186 const xmlChar **namespaces,
2187 int nb_attributes,
2188 int nb_defaulted,
2189 const xmlChar **attributes)
2190{
2191 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2192 xmlNodePtr ret;
2193 xmlNodePtr parent;
2194 xmlNsPtr last = NULL, ns;
2195 const xmlChar *uri, *pref;
2196 xmlChar *lname = NULL;
2197 int i, j;
2198
2199 if (ctx == NULL) return;
2200 parent = ctxt->node;
2201 /*
2202 * First check on validity:
2203 */
2204 if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
2205 ((ctxt->myDoc->intSubset == NULL) ||
2206 ((ctxt->myDoc->intSubset->notations == NULL) &&
2207 (ctxt->myDoc->intSubset->elements == NULL) &&
2208 (ctxt->myDoc->intSubset->attributes == NULL) &&
2209 (ctxt->myDoc->intSubset->entities == NULL)))) {
2210 xmlErrValid(ctxt, XML_DTD_NO_DTD,
2211 "Validation failed: no DTD found !", NULL, NULL);
2212 ctxt->validate = 0;
2213 }
2214
2215 /*
2216 * Take care of the rare case of an undefined namespace prefix
2217 */
2218 if ((prefix != NULL) && (URI == NULL)) {
2219 if (ctxt->dictNames) {
2220 const xmlChar *fullname;
2221
2222 fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
2223 if (fullname != NULL)
2224 localname = fullname;
2225 } else {
2226 lname = xmlBuildQName(localname, prefix, NULL, 0);
2227 }
2228 }
2229 /*
2230 * allocate the node
2231 */
2232 if (ctxt->freeElems != NULL) {
2233 ret = ctxt->freeElems;
2234 ctxt->freeElems = ret->next;
2235 ctxt->freeElemsNr--;
2236 memset(ret, 0, sizeof(xmlNode));
2237 ret->doc = ctxt->myDoc;
2238 ret->type = XML_ELEMENT_NODE;
2239
2240 if (ctxt->dictNames)
2241 ret->name = localname;
2242 else {
2243 if (lname == NULL)
2244 ret->name = xmlStrdup(localname);
2245 else
2246 ret->name = lname;
2247 if (ret->name == NULL) {
2248 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2249 return;
2250 }
2251 }
2252 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
2253 xmlRegisterNodeDefaultValue(ret);
2254 } else {
2255 if (ctxt->dictNames)
2256 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2257 (xmlChar *) localname, NULL);
2258 else if (lname == NULL)
2259 ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
2260 else
2261 ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
2262 (xmlChar *) lname, NULL);
2263 if (ret == NULL) {
2264 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2265 return;
2266 }
2267 }
2268 if (ctxt->linenumbers) {
2269 if (ctxt->input != NULL) {
2270 if (ctxt->input->line < USHRT_MAX)
2271 ret->line = (unsigned short) ctxt->input->line;
2272 else
2273 ret->line = USHRT_MAX;
2274 }
2275 }
2276
2277 if (parent == NULL) {
2278 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2279 }
2280 /*
2281 * Build the namespace list
2282 */
2283 for (i = 0,j = 0;j < nb_namespaces;j++) {
2284 pref = namespaces[i++];
2285 uri = namespaces[i++];
2286 ns = xmlNewNs(NULL, uri, pref);
2287 if (ns != NULL) {
2288 if (last == NULL) {
2289 ret->nsDef = last = ns;
2290 } else {
2291 last->next = ns;
2292 last = ns;
2293 }
2294 if ((URI != NULL) && (prefix == pref))
2295 ret->ns = ns;
2296 } else {
2297 /*
2298 * any out of memory error would already have been raised
2299 * but we can't be guaranteed it's the actual error due to the
2300 * API, best is to skip in this case
2301 */
2302 continue;
2303 }
2304#ifdef LIBXML_VALID_ENABLED
2305 if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
2306 ctxt->myDoc && ctxt->myDoc->intSubset) {
2307 ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
2308 ret, prefix, ns, uri);
2309 }
2310#endif /* LIBXML_VALID_ENABLED */
2311 }
2312 ctxt->nodemem = -1;
2313
2314 /*
2315 * We are parsing a new node.
2316 */
2317 if (nodePush(ctxt, ret) < 0) {
2318 xmlUnlinkNode(ret);
2319 xmlFreeNode(ret);
2320 return;
2321 }
2322
2323 /*
2324 * Link the child element
2325 */
2326 if (parent != NULL) {
2327 if (parent->type == XML_ELEMENT_NODE) {
2328 xmlAddChild(parent, ret);
2329 } else {
2330 xmlAddSibling(parent, ret);
2331 }
2332 }
2333
2334 /*
2335 * Insert the defaulted attributes from the DTD only if requested:
2336 */
2337 if ((nb_defaulted != 0) &&
2338 ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
2339 nb_attributes -= nb_defaulted;
2340
2341 /*
2342 * Search the namespace if it wasn't already found
2343 * Note that, if prefix is NULL, this searches for the default Ns
2344 */
2345 if ((URI != NULL) && (ret->ns == NULL)) {
2346 ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
2347 if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
2348 ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
2349 }
2350 if (ret->ns == NULL) {
2351 ns = xmlNewNs(ret, NULL, prefix);
2352 if (ns == NULL) {
2353
2354 xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
2355 return;
2356 }
2357 if (prefix != NULL)
2358 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2359 "Namespace prefix %s was not found\n",
2360 prefix, NULL);
2361 else
2362 xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
2363 "Namespace default prefix was not found\n",
2364 NULL, NULL);
2365 }
2366 }
2367
2368 /*
2369 * process all the other attributes
2370 */
2371 if (nb_attributes > 0) {
2372 for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
2373 /*
2374 * Handle the rare case of an undefined attribute prefix
2375 */
2376 if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
2377 if (ctxt->dictNames) {
2378 const xmlChar *fullname;
2379
2380 fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
2381 attributes[j]);
2382 if (fullname != NULL) {
2383 xmlSAX2AttributeNs(ctxt, fullname, NULL,
2384 attributes[j+3], attributes[j+4]);
2385 continue;
2386 }
2387 } else {
2388 lname = xmlBuildQName(attributes[j], attributes[j+1],
2389 NULL, 0);
2390 if (lname != NULL) {
2391 xmlSAX2AttributeNs(ctxt, lname, NULL,
2392 attributes[j+3], attributes[j+4]);
2393 xmlFree(lname);
2394 continue;
2395 }
2396 }
2397 }
2398 xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
2399 attributes[j+3], attributes[j+4]);
2400 }
2401 }
2402
2403#ifdef LIBXML_VALID_ENABLED
2404 /*
2405 * If it's the Document root, finish the DTD validation and
2406 * check the document root element for validity
2407 */
2408 if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
2409 int chk;
2410
2411 chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
2412 if (chk <= 0)
2413 ctxt->valid = 0;
2414 if (chk < 0)
2415 ctxt->wellFormed = 0;
2416 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
2417 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
2418 }
2419#endif /* LIBXML_VALID_ENABLED */
2420}
2421
2422/**
2423 * xmlSAX2EndElementNs:
2424 * @ctx: the user data (XML parser context)
2425 * @localname: the local name of the element
2426 * @prefix: the element namespace prefix if available
2427 * @URI: the element namespace name if available
2428 *
2429 * SAX2 callback when an element end has been detected by the parser.
2430 * It provides the namespace information for the element.
2431 */
2432void
2433xmlSAX2EndElementNs(void *ctx,
2434 const xmlChar * localname ATTRIBUTE_UNUSED,
2435 const xmlChar * prefix ATTRIBUTE_UNUSED,
2436 const xmlChar * URI ATTRIBUTE_UNUSED)
2437{
2438 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2439 xmlParserNodeInfo node_info;
2440 xmlNodePtr cur;
2441
2442 if (ctx == NULL) return;
2443 cur = ctxt->node;
2444 /* Capture end position and add node */
2445 if ((ctxt->record_info) && (cur != NULL)) {
2446 node_info.end_pos = ctxt->input->cur - ctxt->input->base;
2447 node_info.end_line = ctxt->input->line;
2448 node_info.node = cur;
2449 xmlParserAddNodeInfo(ctxt, &node_info);
2450 }
2451 ctxt->nodemem = -1;
2452
2453#ifdef LIBXML_VALID_ENABLED
2454 if (ctxt->validate && ctxt->wellFormed &&
2455 ctxt->myDoc && ctxt->myDoc->intSubset)
2456 ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur);
2457#endif /* LIBXML_VALID_ENABLED */
2458
2459 /*
2460 * end of parsing of this node.
2461 */
2462 nodePop(ctxt);
2463}
2464
2465/**
2466 * xmlSAX2Reference:
2467 * @ctx: the user data (XML parser context)
2468 * @name: The entity name
2469 *
2470 * called when an entity xmlSAX2Reference is detected.
2471 */
2472void
2473xmlSAX2Reference(void *ctx, const xmlChar *name)
2474{
2475 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2476 xmlNodePtr ret;
2477
2478 if (ctx == NULL) return;
2479#ifdef DEBUG_SAX
2480 xmlGenericError(xmlGenericErrorContext,
2481 "SAX.xmlSAX2Reference(%s)\n", name);
2482#endif
2483 if (name[0] == '#')
2484 ret = xmlNewCharRef(ctxt->myDoc, name);
2485 else
2486 ret = xmlNewReference(ctxt->myDoc, name);
2487#ifdef DEBUG_SAX_TREE
2488 xmlGenericError(xmlGenericErrorContext,
2489 "add xmlSAX2Reference %s to %s \n", name, ctxt->node->name);
2490#endif
2491 if (xmlAddChild(ctxt->node, ret) == NULL) {
2492 xmlFreeNode(ret);
2493 }
2494}
2495
2496/**
2497 * xmlSAX2Text:
2498 * @ctx: the user data (XML parser context)
2499 * @ch: a xmlChar string
2500 * @len: the number of xmlChar
2501 * @type: text or cdata
2502 *
2503 * Append characters.
2504 */
2505static void
2506xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
2507 xmlElementType type)
2508{
2509 xmlNodePtr lastChild;
2510
2511 if (ctxt == NULL) return;
2512#ifdef DEBUG_SAX
2513 xmlGenericError(xmlGenericErrorContext,
2514 "SAX.xmlSAX2Characters(%.30s, %d)\n", ch, len);
2515#endif
2516 /*
2517 * Handle the data if any. If there is no child
2518 * add it as content, otherwise if the last child is text,
2519 * concatenate it, else create a new node of type text.
2520 */
2521
2522 if (ctxt->node == NULL) {
2523#ifdef DEBUG_SAX_TREE
2524 xmlGenericError(xmlGenericErrorContext,
2525 "add chars: ctxt->node == NULL !\n");
2526#endif
2527 return;
2528 }
2529 lastChild = ctxt->node->last;
2530#ifdef DEBUG_SAX_TREE
2531 xmlGenericError(xmlGenericErrorContext,
2532 "add chars to %s \n", ctxt->node->name);
2533#endif
2534
2535 /*
2536 * Here we needed an accelerator mechanism in case of very large
2537 * elements. Use an attribute in the structure !!!
2538 */
2539 if (lastChild == NULL) {
2540 if (type == XML_TEXT_NODE)
2541 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2542 else
2543 lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
2544 if (lastChild != NULL) {
2545 ctxt->node->children = lastChild;
2546 ctxt->node->last = lastChild;
2547 lastChild->parent = ctxt->node;
2548 lastChild->doc = ctxt->node->doc;
2549 ctxt->nodelen = len;
2550 ctxt->nodemem = len + 1;
2551 } else {
2552 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2553 return;
2554 }
2555 } else {
2556 int coalesceText = (lastChild != NULL) &&
2557 (lastChild->type == type) &&
2558 ((type != XML_TEXT_NODE) ||
2559 (lastChild->name == xmlStringText));
2560 if ((coalesceText) && (ctxt->nodemem != 0)) {
2561 /*
2562 * The whole point of maintaining nodelen and nodemem,
2563 * xmlTextConcat is too costly, i.e. compute length,
2564 * reallocate a new buffer, move data, append ch. Here
2565 * We try to minimize realloc() uses and avoid copying
2566 * and recomputing length over and over.
2567 */
2568 if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
2569 lastChild->content = xmlStrdup(lastChild->content);
2570 lastChild->properties = NULL;
2571 } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
2572 (xmlDictOwns(ctxt->dict, lastChild->content))) {
2573 lastChild->content = xmlStrdup(lastChild->content);
2574 }
2575 if (lastChild->content == NULL) {
2576 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: xmlStrdup returned NULL");
2577 return;
2578 }
2579 if (((size_t)ctxt->nodelen + (size_t)len > XML_MAX_TEXT_LENGTH) &&
2580 ((ctxt->options & XML_PARSE_HUGE) == 0)) {
2581 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
2582 return;
2583 }
2584 if ((size_t)ctxt->nodelen > SIZE_T_MAX - (size_t)len ||
2585 (size_t)ctxt->nodemem + (size_t)len > SIZE_T_MAX / 2) {
2586 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
2587 return;
2588 }
2589 if (ctxt->nodelen + len >= ctxt->nodemem) {
2590 xmlChar *newbuf;
2591 size_t size;
2592
2593 size = ctxt->nodemem + len;
2594 size *= 2;
2595 newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
2596 if (newbuf == NULL) {
2597 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2598 return;
2599 }
2600 ctxt->nodemem = size;
2601 lastChild->content = newbuf;
2602 }
2603 memcpy(&lastChild->content[ctxt->nodelen], ch, len);
2604 ctxt->nodelen += len;
2605 lastChild->content[ctxt->nodelen] = 0;
2606 } else if (coalesceText) {
2607 if (xmlTextConcat(lastChild, ch, len)) {
2608 xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
2609 }
2610 if (ctxt->node->children != NULL) {
2611 ctxt->nodelen = xmlStrlen(lastChild->content);
2612 ctxt->nodemem = ctxt->nodelen + 1;
2613 }
2614 } else {
2615 /* Mixed content, first time */
2616 if (type == XML_TEXT_NODE)
2617 lastChild = xmlSAX2TextNode(ctxt, ch, len);
2618 else
2619 lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
2620 if (lastChild != NULL) {
2621 xmlAddChild(ctxt->node, lastChild);
2622 if (ctxt->node->children != NULL) {
2623 ctxt->nodelen = len;
2624 ctxt->nodemem = len + 1;
2625 }
2626 }
2627 }
2628 }
2629}
2630
2631/**
2632 * xmlSAX2Characters:
2633 * @ctx: the user data (XML parser context)
2634 * @ch: a xmlChar string
2635 * @len: the number of xmlChar
2636 *
2637 * receiving some chars from the parser.
2638 */
2639void
2640xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
2641{
2642 xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE);
2643}
2644
2645/**
2646 * xmlSAX2IgnorableWhitespace:
2647 * @ctx: the user data (XML parser context)
2648 * @ch: a xmlChar string
2649 * @len: the number of xmlChar
2650 *
2651 * receiving some ignorable whitespaces from the parser.
2652 * UNUSED: by default the DOM building will use xmlSAX2Characters
2653 */
2654void
2655xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
2656{
2657 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
2658#ifdef DEBUG_SAX
2659 xmlGenericError(xmlGenericErrorContext,
2660 "SAX.xmlSAX2IgnorableWhitespace(%.30s, %d)\n", ch, len);
2661#endif
2662}
2663
2664/**
2665 * xmlSAX2ProcessingInstruction:
2666 * @ctx: the user data (XML parser context)
2667 * @target: the target name
2668 * @data: the PI data's
2669 *
2670 * A processing instruction has been parsed.
2671 */
2672void
2673xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
2674 const xmlChar *data)
2675{
2676 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2677 xmlNodePtr ret;
2678 xmlNodePtr parent;
2679
2680 if (ctx == NULL) return;
2681 parent = ctxt->node;
2682#ifdef DEBUG_SAX
2683 xmlGenericError(xmlGenericErrorContext,
2684 "SAX.xmlSAX2ProcessingInstruction(%s, %s)\n", target, data);
2685#endif
2686
2687 ret = xmlNewDocPI(ctxt->myDoc, target, data);
2688 if (ret == NULL) return;
2689
2690 if (ctxt->linenumbers) {
2691 if (ctxt->input != NULL) {
2692 if (ctxt->input->line < USHRT_MAX)
2693 ret->line = (unsigned short) ctxt->input->line;
2694 else
2695 ret->line = USHRT_MAX;
2696 }
2697 }
2698 if (ctxt->inSubset == 1) {
2699 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2700 return;
2701 } else if (ctxt->inSubset == 2) {
2702 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2703 return;
2704 }
2705 if (parent == NULL) {
2706#ifdef DEBUG_SAX_TREE
2707 xmlGenericError(xmlGenericErrorContext,
2708 "Setting PI %s as root\n", target);
2709#endif
2710 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2711 return;
2712 }
2713 if (parent->type == XML_ELEMENT_NODE) {
2714#ifdef DEBUG_SAX_TREE
2715 xmlGenericError(xmlGenericErrorContext,
2716 "adding PI %s child to %s\n", target, parent->name);
2717#endif
2718 xmlAddChild(parent, ret);
2719 } else {
2720#ifdef DEBUG_SAX_TREE
2721 xmlGenericError(xmlGenericErrorContext,
2722 "adding PI %s sibling to ", target);
2723 xmlDebugDumpOneNode(stderr, parent, 0);
2724#endif
2725 xmlAddSibling(parent, ret);
2726 }
2727}
2728
2729/**
2730 * xmlSAX2Comment:
2731 * @ctx: the user data (XML parser context)
2732 * @value: the xmlSAX2Comment content
2733 *
2734 * A xmlSAX2Comment has been parsed.
2735 */
2736void
2737xmlSAX2Comment(void *ctx, const xmlChar *value)
2738{
2739 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
2740 xmlNodePtr ret;
2741 xmlNodePtr parent;
2742
2743 if (ctx == NULL) return;
2744 parent = ctxt->node;
2745#ifdef DEBUG_SAX
2746 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2Comment(%s)\n", value);
2747#endif
2748 ret = xmlNewDocComment(ctxt->myDoc, value);
2749 if (ret == NULL) return;
2750 if (ctxt->linenumbers) {
2751 if (ctxt->input != NULL) {
2752 if (ctxt->input->line < USHRT_MAX)
2753 ret->line = (unsigned short) ctxt->input->line;
2754 else
2755 ret->line = USHRT_MAX;
2756 }
2757 }
2758
2759 if (ctxt->inSubset == 1) {
2760 xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
2761 return;
2762 } else if (ctxt->inSubset == 2) {
2763 xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
2764 return;
2765 }
2766 if (parent == NULL) {
2767#ifdef DEBUG_SAX_TREE
2768 xmlGenericError(xmlGenericErrorContext,
2769 "Setting xmlSAX2Comment as root\n");
2770#endif
2771 xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
2772 return;
2773 }
2774 if (parent->type == XML_ELEMENT_NODE) {
2775#ifdef DEBUG_SAX_TREE
2776 xmlGenericError(xmlGenericErrorContext,
2777 "adding xmlSAX2Comment child to %s\n", parent->name);
2778#endif
2779 xmlAddChild(parent, ret);
2780 } else {
2781#ifdef DEBUG_SAX_TREE
2782 xmlGenericError(xmlGenericErrorContext,
2783 "adding xmlSAX2Comment sibling to ");
2784 xmlDebugDumpOneNode(stderr, parent, 0);
2785#endif
2786 xmlAddSibling(parent, ret);
2787 }
2788}
2789
2790/**
2791 * xmlSAX2CDataBlock:
2792 * @ctx: the user data (XML parser context)
2793 * @value: The pcdata content
2794 * @len: the block length
2795 *
2796 * called when a pcdata block has been parsed
2797 */
2798void
2799xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
2800{
2801 xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE);
2802}
2803
2804static int xmlSAX2DefaultVersionValue = 2;
2805
2806#ifdef LIBXML_SAX1_ENABLED
2807/**
2808 * xmlSAXDefaultVersion:
2809 * @version: the version, 1 or 2
2810 *
2811 * Set the default version of SAX used globally by the library.
2812 * By default, during initialization the default is set to 2.
2813 * Note that it is generally a better coding style to use
2814 * xmlSAXVersion() to set up the version explicitly for a given
2815 * parsing context.
2816 *
2817 * Returns the previous value in case of success and -1 in case of error.
2818 */
2819int
2820xmlSAXDefaultVersion(int version)
2821{
2822 int ret = xmlSAX2DefaultVersionValue;
2823
2824 if ((version != 1) && (version != 2))
2825 return(-1);
2826 xmlSAX2DefaultVersionValue = version;
2827 return(ret);
2828}
2829#endif /* LIBXML_SAX1_ENABLED */
2830
2831/**
2832 * xmlSAXVersion:
2833 * @hdlr: the SAX handler
2834 * @version: the version, 1 or 2
2835 *
2836 * Initialize the default XML SAX handler according to the version
2837 *
2838 * Returns 0 in case of success and -1 in case of error.
2839 */
2840int
2841xmlSAXVersion(xmlSAXHandler *hdlr, int version)
2842{
2843 if (hdlr == NULL) return(-1);
2844 if (version == 2) {
2845 hdlr->startElement = NULL;
2846 hdlr->endElement = NULL;
2847 hdlr->startElementNs = xmlSAX2StartElementNs;
2848 hdlr->endElementNs = xmlSAX2EndElementNs;
2849 hdlr->serror = NULL;
2850 hdlr->initialized = XML_SAX2_MAGIC;
2851#ifdef LIBXML_SAX1_ENABLED
2852 } else if (version == 1) {
2853 hdlr->startElement = xmlSAX2StartElement;
2854 hdlr->endElement = xmlSAX2EndElement;
2855 hdlr->initialized = 1;
2856#endif /* LIBXML_SAX1_ENABLED */
2857 } else
2858 return(-1);
2859 hdlr->internalSubset = xmlSAX2InternalSubset;
2860 hdlr->externalSubset = xmlSAX2ExternalSubset;
2861 hdlr->isStandalone = xmlSAX2IsStandalone;
2862 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2863 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2864 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2865 hdlr->getEntity = xmlSAX2GetEntity;
2866 hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
2867 hdlr->entityDecl = xmlSAX2EntityDecl;
2868 hdlr->attributeDecl = xmlSAX2AttributeDecl;
2869 hdlr->elementDecl = xmlSAX2ElementDecl;
2870 hdlr->notationDecl = xmlSAX2NotationDecl;
2871 hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
2872 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2873 hdlr->startDocument = xmlSAX2StartDocument;
2874 hdlr->endDocument = xmlSAX2EndDocument;
2875 hdlr->reference = xmlSAX2Reference;
2876 hdlr->characters = xmlSAX2Characters;
2877 hdlr->cdataBlock = xmlSAX2CDataBlock;
2878 hdlr->ignorableWhitespace = xmlSAX2Characters;
2879 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2880 hdlr->comment = xmlSAX2Comment;
2881 hdlr->warning = xmlParserWarning;
2882 hdlr->error = xmlParserError;
2883 hdlr->fatalError = xmlParserError;
2884
2885 return(0);
2886}
2887
2888/**
2889 * xmlSAX2InitDefaultSAXHandler:
2890 * @hdlr: the SAX handler
2891 * @warning: flag if non-zero sets the handler warning procedure
2892 *
2893 * Initialize the default XML SAX2 handler
2894 */
2895void
2896xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
2897{
2898 if ((hdlr == NULL) || (hdlr->initialized != 0))
2899 return;
2900
2901 xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
2902 if (warning == 0)
2903 hdlr->warning = NULL;
2904 else
2905 hdlr->warning = xmlParserWarning;
2906}
2907
2908/**
2909 * xmlDefaultSAXHandlerInit:
2910 *
2911 * Initialize the default SAX2 handler
2912 */
2913void
2914xmlDefaultSAXHandlerInit(void)
2915{
2916#ifdef LIBXML_SAX1_ENABLED
2917 xmlSAXVersion((xmlSAXHandlerPtr) &xmlDefaultSAXHandler, 1);
2918#endif /* LIBXML_SAX1_ENABLED */
2919}
2920
2921#ifdef LIBXML_HTML_ENABLED
2922
2923/**
2924 * xmlSAX2InitHtmlDefaultSAXHandler:
2925 * @hdlr: the SAX handler
2926 *
2927 * Initialize the default HTML SAX2 handler
2928 */
2929void
2930xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
2931{
2932 if ((hdlr == NULL) || (hdlr->initialized != 0))
2933 return;
2934
2935 hdlr->internalSubset = xmlSAX2InternalSubset;
2936 hdlr->externalSubset = NULL;
2937 hdlr->isStandalone = NULL;
2938 hdlr->hasInternalSubset = NULL;
2939 hdlr->hasExternalSubset = NULL;
2940 hdlr->resolveEntity = NULL;
2941 hdlr->getEntity = xmlSAX2GetEntity;
2942 hdlr->getParameterEntity = NULL;
2943 hdlr->entityDecl = NULL;
2944 hdlr->attributeDecl = NULL;
2945 hdlr->elementDecl = NULL;
2946 hdlr->notationDecl = NULL;
2947 hdlr->unparsedEntityDecl = NULL;
2948 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
2949 hdlr->startDocument = xmlSAX2StartDocument;
2950 hdlr->endDocument = xmlSAX2EndDocument;
2951 hdlr->startElement = xmlSAX2StartElement;
2952 hdlr->endElement = xmlSAX2EndElement;
2953 hdlr->reference = NULL;
2954 hdlr->characters = xmlSAX2Characters;
2955 hdlr->cdataBlock = xmlSAX2CDataBlock;
2956 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
2957 hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
2958 hdlr->comment = xmlSAX2Comment;
2959 hdlr->warning = xmlParserWarning;
2960 hdlr->error = xmlParserError;
2961 hdlr->fatalError = xmlParserError;
2962
2963 hdlr->initialized = 1;
2964}
2965
2966/**
2967 * htmlDefaultSAXHandlerInit:
2968 *
2969 * Initialize the default SAX handler
2970 */
2971void
2972htmlDefaultSAXHandlerInit(void)
2973{
2974 xmlSAX2InitHtmlDefaultSAXHandler((xmlSAXHandlerPtr) &htmlDefaultSAXHandler);
2975}
2976
2977#endif /* LIBXML_HTML_ENABLED */
2978
2979#ifdef LIBXML_DOCB_ENABLED
2980
2981/**
2982 * xmlSAX2InitDocbDefaultSAXHandler:
2983 * @hdlr: the SAX handler
2984 *
2985 * Initialize the default DocBook SAX2 handler
2986 */
2987void
2988xmlSAX2InitDocbDefaultSAXHandler(xmlSAXHandler *hdlr)
2989{
2990 if ((hdlr == NULL) || (hdlr->initialized != 0))
2991 return;
2992
2993 hdlr->internalSubset = xmlSAX2InternalSubset;
2994 hdlr->externalSubset = NULL;
2995 hdlr->isStandalone = xmlSAX2IsStandalone;
2996 hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
2997 hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
2998 hdlr->resolveEntity = xmlSAX2ResolveEntity;
2999 hdlr->getEntity = xmlSAX2GetEntity;
3000 hdlr->getParameterEntity = NULL;
3001 hdlr->entityDecl = xmlSAX2EntityDecl;
3002 hdlr->attributeDecl = NULL;
3003 hdlr->elementDecl = NULL;
3004 hdlr->notationDecl = NULL;
3005 hdlr->unparsedEntityDecl = NULL;
3006 hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
3007 hdlr->startDocument = xmlSAX2StartDocument;
3008 hdlr->endDocument = xmlSAX2EndDocument;
3009 hdlr->startElement = xmlSAX2StartElement;
3010 hdlr->endElement = xmlSAX2EndElement;
3011 hdlr->reference = xmlSAX2Reference;
3012 hdlr->characters = xmlSAX2Characters;
3013 hdlr->cdataBlock = NULL;
3014 hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
3015 hdlr->processingInstruction = NULL;
3016 hdlr->comment = xmlSAX2Comment;
3017 hdlr->warning = xmlParserWarning;
3018 hdlr->error = xmlParserError;
3019 hdlr->fatalError = xmlParserError;
3020
3021 hdlr->initialized = 1;
3022}
3023
3024/**
3025 * docbDefaultSAXHandlerInit:
3026 *
3027 * Initialize the default SAX handler
3028 */
3029void
3030docbDefaultSAXHandlerInit(void)
3031{
3032 xmlSAX2InitDocbDefaultSAXHandler((xmlSAXHandlerPtr) &docbDefaultSAXHandler);
3033}
3034
3035#endif /* LIBXML_DOCB_ENABLED */
3036#define bottom_SAX2
3037#include "elfgcchack.h"
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette