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