1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-07-30 22:43:14 +03:00

Introduce xmlNewSAXParserCtxt and htmlNewSAXParserCtxt

Add API functions to create a parser context with a custom SAX handler
without having to mess with ctxt->sax manually.
This commit is contained in:
Nick Wellnhofer
2022-08-24 04:21:58 +02:00
parent 0a04db19fc
commit 9a82b94a94
16 changed files with 253 additions and 176 deletions

View File

@ -5028,6 +5028,8 @@ htmlParseDocument(htmlParserCtxtPtr ctxt) {
/** /**
* htmlInitParserCtxt: * htmlInitParserCtxt:
* @ctxt: an HTML parser context * @ctxt: an HTML parser context
* @sax: SAX handler
* @userData: user data
* *
* Initialize a parser context * Initialize a parser context
* *
@ -5035,10 +5037,8 @@ htmlParseDocument(htmlParserCtxtPtr ctxt) {
*/ */
static int static int
htmlInitParserCtxt(htmlParserCtxtPtr ctxt) htmlInitParserCtxt(htmlParserCtxtPtr ctxt, htmlSAXHandler *sax, void *userData)
{ {
htmlSAXHandler *sax;
if (ctxt == NULL) return(-1); if (ctxt == NULL) return(-1);
memset(ctxt, 0, sizeof(htmlParserCtxt)); memset(ctxt, 0, sizeof(htmlParserCtxt));
@ -5047,12 +5047,21 @@ htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
return(-1); return(-1);
} }
sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
if (sax == NULL) { if (ctxt->sax == NULL)
ctxt->sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
if (ctxt->sax == NULL) {
htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n"); htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
return(-1); return(-1);
} }
memset(sax, 0, sizeof(htmlSAXHandler)); if (sax == NULL) {
memset(ctxt->sax, 0, sizeof(htmlSAXHandler));
xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax);
ctxt->userData = ctxt;
} else {
memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
ctxt->userData = userData ? userData : ctxt;
}
/* Allocate the Input stack */ /* Allocate the Input stack */
ctxt->inputTab = (htmlParserInputPtr *) ctxt->inputTab = (htmlParserInputPtr *)
@ -5111,10 +5120,6 @@ htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
ctxt->nodeInfoNr = 0; ctxt->nodeInfoNr = 0;
ctxt->nodeInfoMax = 0; ctxt->nodeInfoMax = 0;
ctxt->sax = sax;
xmlSAX2InitHtmlDefaultSAXHandler(sax);
ctxt->userData = ctxt;
ctxt->myDoc = NULL; ctxt->myDoc = NULL;
ctxt->wellFormed = 1; ctxt->wellFormed = 1;
ctxt->replaceEntities = 0; ctxt->replaceEntities = 0;
@ -5157,6 +5162,22 @@ htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
htmlParserCtxtPtr htmlParserCtxtPtr
htmlNewParserCtxt(void) htmlNewParserCtxt(void)
{
return(htmlNewSAXParserCtxt(NULL, NULL));
}
/**
* htmlNewSAXParserCtxt:
* @sax: SAX handler
* @userData: user data
*
* Allocate and initialize a new parser context.
*
* Returns the htmlParserCtxtPtr or NULL in case of allocation error
*/
htmlParserCtxtPtr
htmlNewSAXParserCtxt(htmlSAXHandlerPtr sax, void *userData)
{ {
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt;
@ -5166,7 +5187,7 @@ htmlNewParserCtxt(void)
return(NULL); return(NULL);
} }
memset(ctxt, 0, sizeof(xmlParserCtxt)); memset(ctxt, 0, sizeof(xmlParserCtxt));
if (htmlInitParserCtxt(ctxt) < 0) { if (htmlInitParserCtxt(ctxt, sax, userData) < 0) {
htmlFreeParserCtxt(ctxt); htmlFreeParserCtxt(ctxt);
return(NULL); return(NULL);
} }
@ -6326,28 +6347,13 @@ htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
buf = xmlAllocParserInputBuffer(enc); buf = xmlAllocParserInputBuffer(enc);
if (buf == NULL) return(NULL); if (buf == NULL) return(NULL);
ctxt = htmlNewParserCtxt(); ctxt = htmlNewSAXParserCtxt(sax, user_data);
if (ctxt == NULL) { if (ctxt == NULL) {
xmlFreeParserInputBuffer(buf); xmlFreeParserInputBuffer(buf);
return(NULL); return(NULL);
} }
if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder) if(enc==XML_CHAR_ENCODING_UTF8 || buf->encoder)
ctxt->charset=XML_CHAR_ENCODING_UTF8; ctxt->charset=XML_CHAR_ENCODING_UTF8;
if (sax != NULL) {
#ifdef LIBXML_SAX1_ENABLED
if (ctxt->sax != (xmlSAXHandlerPtr) &htmlDefaultSAXHandler)
#endif
xmlFree(ctxt->sax);
ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
if (ctxt->sax == NULL) {
xmlFree(buf);
xmlFree(ctxt);
return(NULL);
}
memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
if (user_data != NULL)
ctxt->userData = user_data;
}
if (filename == NULL) { if (filename == NULL) {
ctxt->directory = NULL; ctxt->directory = NULL;
} else { } else {

View File

@ -78,6 +78,7 @@ int <a href="#htmlHandleOmittedElem">htmlHandleOmittedElem</a> (int val);
int <a href="#htmlIsAutoClosed">htmlIsAutoClosed</a> (<a href="libxml2-HTMLparser.html#htmlDocPtr">htmlDocPtr</a> doc, <br/> <a href="libxml2-HTMLparser.html#htmlNodePtr">htmlNodePtr</a> elem); int <a href="#htmlIsAutoClosed">htmlIsAutoClosed</a> (<a href="libxml2-HTMLparser.html#htmlDocPtr">htmlDocPtr</a> doc, <br/> <a href="libxml2-HTMLparser.html#htmlNodePtr">htmlNodePtr</a> elem);
int <a href="#htmlIsScriptAttribute">htmlIsScriptAttribute</a> (const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * name); int <a href="#htmlIsScriptAttribute">htmlIsScriptAttribute</a> (const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * name);
<a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> <a href="#htmlNewParserCtxt">htmlNewParserCtxt</a> (void); <a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> <a href="#htmlNewParserCtxt">htmlNewParserCtxt</a> (void);
<a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> <a href="#htmlNewSAXParserCtxt">htmlNewSAXParserCtxt</a> (<a href="libxml2-HTMLparser.html#htmlSAXHandlerPtr">htmlSAXHandlerPtr</a> sax, <br/> void * userData);
<a href="libxml2-HTMLparser.html#htmlStatus">htmlStatus</a> <a href="#htmlNodeStatus">htmlNodeStatus</a> (const <a href="libxml2-HTMLparser.html#htmlNodePtr">htmlNodePtr</a> node, <br/> int legacy); <a href="libxml2-HTMLparser.html#htmlStatus">htmlStatus</a> <a href="#htmlNodeStatus">htmlNodeStatus</a> (const <a href="libxml2-HTMLparser.html#htmlNodePtr">htmlNodePtr</a> node, <br/> int legacy);
int <a href="#htmlParseCharRef">htmlParseCharRef</a> (<a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> ctxt); int <a href="#htmlParseCharRef">htmlParseCharRef</a> (<a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> ctxt);
int <a href="#htmlParseChunk">htmlParseChunk</a> (<a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> ctxt, <br/> const char * chunk, <br/> int size, <br/> int terminate); int <a href="#htmlParseChunk">htmlParseChunk</a> (<a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> ctxt, <br/> const char * chunk, <br/> int size, <br/> int terminate);
@ -297,6 +298,10 @@ const <a href="libxml2-HTMLparser.html#htmlElemDesc">htmlElemDesc</a> * <a href=
<div class="refsect2" lang="en"><h3><a name="htmlNewParserCtxt"/>htmlNewParserCtxt ()</h3><pre class="programlisting"><a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> htmlNewParserCtxt (void)<br/> <div class="refsect2" lang="en"><h3><a name="htmlNewParserCtxt"/>htmlNewParserCtxt ()</h3><pre class="programlisting"><a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> htmlNewParserCtxt (void)<br/>
</pre><p>Allocate and initialize a new parser context.</p> </pre><p>Allocate and initialize a new parser context.</p>
<div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>the <a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> or NULL in case of allocation error</td></tr></tbody></table></div></div> <div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>the <a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> or NULL in case of allocation error</td></tr></tbody></table></div></div>
<hr/>
<div class="refsect2" lang="en"><h3><a name="htmlNewSAXParserCtxt"/>htmlNewSAXParserCtxt ()</h3><pre class="programlisting"><a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> htmlNewSAXParserCtxt (<a href="libxml2-HTMLparser.html#htmlSAXHandlerPtr">htmlSAXHandlerPtr</a> sax, <br/> void * userData)<br/>
</pre><p>Allocate and initialize a new parser context.</p>
<div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>sax</tt></i>:</span></td><td>SAX handler</td></tr><tr><td><span class="term"><i><tt>userData</tt></i>:</span></td><td>user data</td></tr><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>the <a href="libxml2-HTMLparser.html#htmlParserCtxtPtr">htmlParserCtxtPtr</a> or NULL in case of allocation error</td></tr></tbody></table></div></div>
<hr/> <hr/>
<div class="refsect2" lang="en"><h3><a name="htmlNodeStatus"/>htmlNodeStatus ()</h3><pre class="programlisting"><a href="libxml2-HTMLparser.html#htmlStatus">htmlStatus</a> htmlNodeStatus (const <a href="libxml2-HTMLparser.html#htmlNodePtr">htmlNodePtr</a> node, <br/> int legacy)<br/> <div class="refsect2" lang="en"><h3><a name="htmlNodeStatus"/>htmlNodeStatus ()</h3><pre class="programlisting"><a href="libxml2-HTMLparser.html#htmlStatus">htmlStatus</a> htmlNodeStatus (const <a href="libxml2-HTMLparser.html#htmlNodePtr">htmlNodePtr</a> node, <br/> int legacy)<br/>
</pre><p>Checks whether the tree node is valid. Experimental (the author only uses the HTML enhancements in a SAX parser)</p> </pre><p>Checks whether the tree node is valid. Experimental (the author only uses the HTML enhancements in a SAX parser)</p>

View File

@ -119,6 +119,7 @@ int <a href="#xmlLineNumbersDefault">xmlLineNumbersDefault</a> (int val);
<a href="libxml2-tree.html#xmlParserInputPtr">xmlParserInputPtr</a> <a href="#xmlLoadExternalEntity">xmlLoadExternalEntity</a> (const char * URL, <br/> const char * ID, <br/> <a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt); <a href="libxml2-tree.html#xmlParserInputPtr">xmlParserInputPtr</a> <a href="#xmlLoadExternalEntity">xmlLoadExternalEntity</a> (const char * URL, <br/> const char * ID, <br/> <a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt);
<a href="libxml2-tree.html#xmlParserInputPtr">xmlParserInputPtr</a> <a href="#xmlNewIOInputStream">xmlNewIOInputStream</a> (<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt, <br/> <a href="libxml2-tree.html#xmlParserInputBufferPtr">xmlParserInputBufferPtr</a> input, <br/> <a href="libxml2-encoding.html#xmlCharEncoding">xmlCharEncoding</a> enc); <a href="libxml2-tree.html#xmlParserInputPtr">xmlParserInputPtr</a> <a href="#xmlNewIOInputStream">xmlNewIOInputStream</a> (<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt, <br/> <a href="libxml2-tree.html#xmlParserInputBufferPtr">xmlParserInputBufferPtr</a> input, <br/> <a href="libxml2-encoding.html#xmlCharEncoding">xmlCharEncoding</a> enc);
<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> <a href="#xmlNewParserCtxt">xmlNewParserCtxt</a> (void); <a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> <a href="#xmlNewParserCtxt">xmlNewParserCtxt</a> (void);
<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> <a href="#xmlNewSAXParserCtxt">xmlNewSAXParserCtxt</a> (<a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * userData);
int <a href="#xmlParseBalancedChunkMemory">xmlParseBalancedChunkMemory</a> (<a href="libxml2-tree.html#xmlDocPtr">xmlDocPtr</a> doc, <br/> <a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * user_data, <br/> int depth, <br/> const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * string, <br/> <a href="libxml2-tree.html#xmlNodePtr">xmlNodePtr</a> * lst); int <a href="#xmlParseBalancedChunkMemory">xmlParseBalancedChunkMemory</a> (<a href="libxml2-tree.html#xmlDocPtr">xmlDocPtr</a> doc, <br/> <a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * user_data, <br/> int depth, <br/> const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * string, <br/> <a href="libxml2-tree.html#xmlNodePtr">xmlNodePtr</a> * lst);
int <a href="#xmlParseBalancedChunkMemoryRecover">xmlParseBalancedChunkMemoryRecover</a> (<a href="libxml2-tree.html#xmlDocPtr">xmlDocPtr</a> doc, <br/> <a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * user_data, <br/> int depth, <br/> const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * string, <br/> <a href="libxml2-tree.html#xmlNodePtr">xmlNodePtr</a> * lst, <br/> int recover); int <a href="#xmlParseBalancedChunkMemoryRecover">xmlParseBalancedChunkMemoryRecover</a> (<a href="libxml2-tree.html#xmlDocPtr">xmlDocPtr</a> doc, <br/> <a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * user_data, <br/> int depth, <br/> const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * string, <br/> <a href="libxml2-tree.html#xmlNodePtr">xmlNodePtr</a> * lst, <br/> int recover);
int <a href="#xmlParseChunk">xmlParseChunk</a> (<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt, <br/> const char * chunk, <br/> int size, <br/> int terminate); int <a href="#xmlParseChunk">xmlParseChunk</a> (<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt, <br/> const char * chunk, <br/> int size, <br/> int terminate);
@ -580,7 +581,7 @@ The content of this structure is not made public by the API.
</div> </div>
<hr/> <hr/>
<div class="refsect2" lang="en"><h3><a name="xmlInitParserCtxt"/>xmlInitParserCtxt ()</h3><pre class="programlisting">int xmlInitParserCtxt (<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt)<br/> <div class="refsect2" lang="en"><h3><a name="xmlInitParserCtxt"/>xmlInitParserCtxt ()</h3><pre class="programlisting">int xmlInitParserCtxt (<a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> ctxt)<br/>
</pre><p>Initialize a parser context</p> </pre><p>DEPRECATED: Internal function which will be made private in a future version. Initialize a parser context</p>
<div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>ctxt</tt></i>:</span></td><td>an XML parser context</td></tr><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>0 in case of success and -1 in case of error</td></tr></tbody></table></div></div> <div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>ctxt</tt></i>:</span></td><td>an XML parser context</td></tr><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>0 in case of success and -1 in case of error</td></tr></tbody></table></div></div>
<hr/> <hr/>
<div class="refsect2" lang="en"><h3><a name="xmlKeepBlanksDefault"/>xmlKeepBlanksDefault ()</h3><pre class="programlisting">int xmlKeepBlanksDefault (int val)<br/> <div class="refsect2" lang="en"><h3><a name="xmlKeepBlanksDefault"/>xmlKeepBlanksDefault ()</h3><pre class="programlisting">int xmlKeepBlanksDefault (int val)<br/>
@ -602,6 +603,10 @@ The content of this structure is not made public by the API.
<div class="refsect2" lang="en"><h3><a name="xmlNewParserCtxt"/>xmlNewParserCtxt ()</h3><pre class="programlisting"><a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> xmlNewParserCtxt (void)<br/> <div class="refsect2" lang="en"><h3><a name="xmlNewParserCtxt"/>xmlNewParserCtxt ()</h3><pre class="programlisting"><a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> xmlNewParserCtxt (void)<br/>
</pre><p>Allocate and initialize a new parser context.</p> </pre><p>Allocate and initialize a new parser context.</p>
<div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>the <a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> or NULL</td></tr></tbody></table></div></div> <div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>the <a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> or NULL</td></tr></tbody></table></div></div>
<hr/>
<div class="refsect2" lang="en"><h3><a name="xmlNewSAXParserCtxt"/>xmlNewSAXParserCtxt ()</h3><pre class="programlisting"><a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> xmlNewSAXParserCtxt (<a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * userData)<br/>
</pre><p>Allocate and initialize a new SAX parser context.</p>
<div class="variablelist"><table border="0"><col align="left"/><tbody><tr><td><span class="term"><i><tt>sax</tt></i>:</span></td><td>SAX handler</td></tr><tr><td><span class="term"><i><tt>userData</tt></i>:</span></td><td>user data</td></tr><tr><td><span class="term"><i><tt>Returns</tt></i>:</span></td><td>the <a href="libxml2-tree.html#xmlParserCtxtPtr">xmlParserCtxtPtr</a> or NULL</td></tr></tbody></table></div></div>
<hr/> <hr/>
<div class="refsect2" lang="en"><h3><a name="xmlParseBalancedChunkMemory"/>xmlParseBalancedChunkMemory ()</h3><pre class="programlisting">int xmlParseBalancedChunkMemory (<a href="libxml2-tree.html#xmlDocPtr">xmlDocPtr</a> doc, <br/> <a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * user_data, <br/> int depth, <br/> const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * string, <br/> <a href="libxml2-tree.html#xmlNodePtr">xmlNodePtr</a> * lst)<br/> <div class="refsect2" lang="en"><h3><a name="xmlParseBalancedChunkMemory"/>xmlParseBalancedChunkMemory ()</h3><pre class="programlisting">int xmlParseBalancedChunkMemory (<a href="libxml2-tree.html#xmlDocPtr">xmlDocPtr</a> doc, <br/> <a href="libxml2-tree.html#xmlSAXHandlerPtr">xmlSAXHandlerPtr</a> sax, <br/> void * user_data, <br/> int depth, <br/> const <a href="libxml2-xmlstring.html#xmlChar">xmlChar</a> * string, <br/> <a href="libxml2-tree.html#xmlNodePtr">xmlNodePtr</a> * lst)<br/>
</pre><p>Parse a well-balanced chunk of an XML document called by the parser The allowed sequence for the Well Balanced Chunk is the one defined by the content production in the XML grammar: [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*</p> </pre><p>Parse a well-balanced chunk of an XML document called by the parser The allowed sequence for the Well Balanced Chunk is the one defined by the content production in the XML grammar: [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*</p>

View File

@ -1997,6 +1997,7 @@
<keyword type="function" name="htmlNewDoc ()" link="libxml2-HTMLtree.html#htmlNewDoc"/> <keyword type="function" name="htmlNewDoc ()" link="libxml2-HTMLtree.html#htmlNewDoc"/>
<keyword type="function" name="htmlNewDocNoDtD ()" link="libxml2-HTMLtree.html#htmlNewDocNoDtD"/> <keyword type="function" name="htmlNewDocNoDtD ()" link="libxml2-HTMLtree.html#htmlNewDocNoDtD"/>
<keyword type="function" name="htmlNewParserCtxt ()" link="libxml2-HTMLparser.html#htmlNewParserCtxt"/> <keyword type="function" name="htmlNewParserCtxt ()" link="libxml2-HTMLparser.html#htmlNewParserCtxt"/>
<keyword type="function" name="htmlNewSAXParserCtxt ()" link="libxml2-HTMLparser.html#htmlNewSAXParserCtxt"/>
<keyword type="function" name="htmlNodeDump ()" link="libxml2-HTMLtree.html#htmlNodeDump"/> <keyword type="function" name="htmlNodeDump ()" link="libxml2-HTMLtree.html#htmlNodeDump"/>
<keyword type="function" name="htmlNodeDumpFile ()" link="libxml2-HTMLtree.html#htmlNodeDumpFile"/> <keyword type="function" name="htmlNodeDumpFile ()" link="libxml2-HTMLtree.html#htmlNodeDumpFile"/>
<keyword type="function" name="htmlNodeDumpFileFormat ()" link="libxml2-HTMLtree.html#htmlNodeDumpFileFormat"/> <keyword type="function" name="htmlNodeDumpFileFormat ()" link="libxml2-HTMLtree.html#htmlNodeDumpFileFormat"/>
@ -2563,6 +2564,7 @@
<keyword type="function" name="xmlNewProp ()" link="libxml2-tree.html#xmlNewProp"/> <keyword type="function" name="xmlNewProp ()" link="libxml2-tree.html#xmlNewProp"/>
<keyword type="function" name="xmlNewRMutex ()" link="libxml2-threads.html#xmlNewRMutex"/> <keyword type="function" name="xmlNewRMutex ()" link="libxml2-threads.html#xmlNewRMutex"/>
<keyword type="function" name="xmlNewReference ()" link="libxml2-tree.html#xmlNewReference"/> <keyword type="function" name="xmlNewReference ()" link="libxml2-tree.html#xmlNewReference"/>
<keyword type="function" name="xmlNewSAXParserCtxt ()" link="libxml2-parser.html#xmlNewSAXParserCtxt"/>
<keyword type="function" name="xmlNewStringInputStream ()" link="libxml2-parserInternals.html#xmlNewStringInputStream"/> <keyword type="function" name="xmlNewStringInputStream ()" link="libxml2-parserInternals.html#xmlNewStringInputStream"/>
<keyword type="function" name="xmlNewText ()" link="libxml2-tree.html#xmlNewText"/> <keyword type="function" name="xmlNewText ()" link="libxml2-tree.html#xmlNewText"/>
<keyword type="function" name="xmlNewTextChild ()" link="libxml2-tree.html#xmlNewTextChild"/> <keyword type="function" name="xmlNewTextChild ()" link="libxml2-tree.html#xmlNewTextChild"/>

View File

@ -62,6 +62,7 @@
<exports symbol='htmlIsAutoClosed' type='function'/> <exports symbol='htmlIsAutoClosed' type='function'/>
<exports symbol='htmlIsScriptAttribute' type='function'/> <exports symbol='htmlIsScriptAttribute' type='function'/>
<exports symbol='htmlNewParserCtxt' type='function'/> <exports symbol='htmlNewParserCtxt' type='function'/>
<exports symbol='htmlNewSAXParserCtxt' type='function'/>
<exports symbol='htmlNodeStatus' type='function'/> <exports symbol='htmlNodeStatus' type='function'/>
<exports symbol='htmlParseCharRef' type='function'/> <exports symbol='htmlParseCharRef' type='function'/>
<exports symbol='htmlParseChunk' type='function'/> <exports symbol='htmlParseChunk' type='function'/>
@ -813,6 +814,7 @@
<exports symbol='xmlLoadExternalEntity' type='function'/> <exports symbol='xmlLoadExternalEntity' type='function'/>
<exports symbol='xmlNewIOInputStream' type='function'/> <exports symbol='xmlNewIOInputStream' type='function'/>
<exports symbol='xmlNewParserCtxt' type='function'/> <exports symbol='xmlNewParserCtxt' type='function'/>
<exports symbol='xmlNewSAXParserCtxt' type='function'/>
<exports symbol='xmlParseBalancedChunkMemory' type='function'/> <exports symbol='xmlParseBalancedChunkMemory' type='function'/>
<exports symbol='xmlParseBalancedChunkMemoryRecover' type='function'/> <exports symbol='xmlParseBalancedChunkMemoryRecover' type='function'/>
<exports symbol='xmlParseChunk' type='function'/> <exports symbol='xmlParseChunk' type='function'/>
@ -7628,6 +7630,13 @@ Could we use @subtypes for this?'/>
<info>Allocate and initialize a new parser context.</info> <info>Allocate and initialize a new parser context.</info>
<return type='htmlParserCtxtPtr' info='the htmlParserCtxtPtr or NULL in case of allocation error'/> <return type='htmlParserCtxtPtr' info='the htmlParserCtxtPtr or NULL in case of allocation error'/>
</function> </function>
<function name='htmlNewSAXParserCtxt' file='HTMLparser' module='HTMLparser'>
<cond>defined(LIBXML_HTML_ENABLED)</cond>
<info>Allocate and initialize a new parser context.</info>
<return type='htmlParserCtxtPtr' info='the htmlParserCtxtPtr or NULL in case of allocation error'/>
<arg name='sax' type='htmlSAXHandlerPtr' info='SAX handler'/>
<arg name='userData' type='void *' info='user data'/>
</function>
<function name='htmlNodeDump' file='HTMLtree' module='HTMLtree'> <function name='htmlNodeDump' file='HTMLtree' module='HTMLtree'>
<cond>defined(LIBXML_HTML_ENABLED) &amp;&amp; defined(LIBXML_OUTPUT_ENABLED)</cond> <cond>defined(LIBXML_HTML_ENABLED) &amp;&amp; defined(LIBXML_OUTPUT_ENABLED)</cond>
<info>Dump an HTML node, recursive behaviour,children are printed too, and formatting returns are added.</info> <info>Dump an HTML node, recursive behaviour,children are printed too, and formatting returns are added.</info>
@ -10582,7 +10591,7 @@ Could we use @subtypes for this?'/>
<return type='void'/> <return type='void'/>
</function> </function>
<function name='xmlInitParserCtxt' file='parser' module='parserInternals'> <function name='xmlInitParserCtxt' file='parser' module='parserInternals'>
<info>Initialize a parser context</info> <info>DEPRECATED: Internal function which will be made private in a future version. Initialize a parser context</info>
<return type='int' info='0 in case of success and -1 in case of error'/> <return type='int' info='0 in case of success and -1 in case of error'/>
<arg name='ctxt' type='xmlParserCtxtPtr' info='an XML parser context'/> <arg name='ctxt' type='xmlParserCtxtPtr' info='an XML parser context'/>
</function> </function>
@ -11596,6 +11605,12 @@ Could we use @subtypes for this?'/>
<arg name='doc' type='const xmlDoc *' info='the document'/> <arg name='doc' type='const xmlDoc *' info='the document'/>
<arg name='name' type='const xmlChar *' info='the reference name, or the reference string with &amp; and ;'/> <arg name='name' type='const xmlChar *' info='the reference name, or the reference string with &amp; and ;'/>
</function> </function>
<function name='xmlNewSAXParserCtxt' file='parser' module='parserInternals'>
<info>Allocate and initialize a new SAX parser context.</info>
<return type='xmlParserCtxtPtr' info='the xmlParserCtxtPtr or NULL'/>
<arg name='sax' type='xmlSAXHandlerPtr' info='SAX handler'/>
<arg name='userData' type='void *' info='user data'/>
</function>
<function name='xmlNewStringInputStream' file='parserInternals' module='parserInternals'> <function name='xmlNewStringInputStream' file='parserInternals' module='parserInternals'>
<info>Create a new input stream based on a memory buffer.</info> <info>Create a new input stream based on a memory buffer.</info>
<return type='xmlParserInputPtr' info='the new input stream'/> <return type='xmlParserInputPtr' info='the new input stream'/>

View File

@ -1771,4 +1771,8 @@
<release version="2.9.11"> <release version="2.9.11">
<symbol file="xmlIO">xmlPopOutputCallbacks</symbol> <symbol file="xmlIO">xmlPopOutputCallbacks</symbol>
</release> </release>
<release version="2.11.0">
<symbol file="HTMLparser">htmlNewSAXParserCtxt</symbol>
<symbol file="parser">xmlNewSAXParserCtxt</symbol>
</release>
</symbols> </symbols>

View File

@ -107,6 +107,9 @@ XMLPUBFUN void XMLCALL
XMLPUBFUN htmlParserCtxtPtr XMLCALL XMLPUBFUN htmlParserCtxtPtr XMLCALL
htmlNewParserCtxt(void); htmlNewParserCtxt(void);
XMLPUBFUN htmlParserCtxtPtr XMLCALL
htmlNewSAXParserCtxt(htmlSAXHandlerPtr sax,
void *userData);
XMLPUBFUN htmlParserCtxtPtr XMLCALL XMLPUBFUN htmlParserCtxtPtr XMLCALL
htmlCreateMemoryParserCtxt(const char *buffer, htmlCreateMemoryParserCtxt(const char *buffer,

View File

@ -975,6 +975,8 @@ XMLPUBFUN int XMLCALL
*/ */
XMLPUBFUN xmlParserCtxtPtr XMLCALL XMLPUBFUN xmlParserCtxtPtr XMLCALL
xmlNewParserCtxt (void); xmlNewParserCtxt (void);
XMLPUBFUN xmlParserCtxtPtr XMLCALL
xmlNewSAXParserCtxt (xmlSAXHandlerPtr sax, void *userData);
XMLPUBFUN int XMLCALL XMLPUBFUN int XMLCALL
xmlInitParserCtxt (xmlParserCtxtPtr ctxt); xmlInitParserCtxt (xmlParserCtxtPtr ctxt);
XMLPUBFUN void XMLCALL XMLPUBFUN void XMLCALL

View File

@ -2293,3 +2293,13 @@ LIBXML2_2.9.11 {
xmlPopOutputCallbacks; xmlPopOutputCallbacks;
} LIBXML2_2.9.8; } LIBXML2_2.9.8;
LIBXML2_2.11.0 {
global:
# HTMLparser
htmlNewSAXParserCtxt;
# parser
xmlNewSAXParserCtxt;
} LIBXML2_2.9.11;

104
parser.c
View File

@ -85,8 +85,9 @@ static void
xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info); xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info);
static xmlParserCtxtPtr static xmlParserCtxtPtr
xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, xmlCreateEntityParserCtxtInternal(xmlSAXHandlerPtr sax, void *userData,
const xmlChar *base, xmlParserCtxtPtr pctx); const xmlChar *URL, const xmlChar *ID, const xmlChar *base,
xmlParserCtxtPtr pctx);
static void xmlHaltParser(xmlParserCtxtPtr ctxt); static void xmlHaltParser(xmlParserCtxtPtr ctxt);
@ -12447,33 +12448,13 @@ xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
buf = xmlAllocParserInputBuffer(enc); buf = xmlAllocParserInputBuffer(enc);
if (buf == NULL) return(NULL); if (buf == NULL) return(NULL);
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(sax, user_data);
if (ctxt == NULL) { if (ctxt == NULL) {
xmlErrMemory(NULL, "creating parser: out of memory\n"); xmlErrMemory(NULL, "creating parser: out of memory\n");
xmlFreeParserInputBuffer(buf); xmlFreeParserInputBuffer(buf);
return(NULL); return(NULL);
} }
ctxt->dictNames = 1; ctxt->dictNames = 1;
if (sax != NULL) {
#ifdef LIBXML_SAX1_ENABLED
if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
#endif /* LIBXML_SAX1_ENABLED */
xmlFree(ctxt->sax);
ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
if (ctxt->sax == NULL) {
xmlErrMemory(ctxt, NULL);
xmlFreeParserInputBuffer(buf);
xmlFreeParserCtxt(ctxt);
return(NULL);
}
memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
if (sax->initialized == XML_SAX2_MAGIC)
memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
else
memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
if (user_data != NULL)
ctxt->userData = user_data;
}
if (filename == NULL) { if (filename == NULL) {
ctxt->directory = NULL; ctxt->directory = NULL;
} else { } else {
@ -12609,31 +12590,11 @@ xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
return (NULL); return (NULL);
} }
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(sax, user_data);
if (ctxt == NULL) { if (ctxt == NULL) {
xmlFreeParserInputBuffer(buf); xmlFreeParserInputBuffer(buf);
return(NULL); return(NULL);
} }
if (sax != NULL) {
#ifdef LIBXML_SAX1_ENABLED
if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
#endif /* LIBXML_SAX1_ENABLED */
xmlFree(ctxt->sax);
ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
if (ctxt->sax == NULL) {
xmlFreeParserInputBuffer(buf);
xmlErrMemory(ctxt, NULL);
xmlFreeParserCtxt(ctxt);
return(NULL);
}
memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
if (sax->initialized == XML_SAX2_MAGIC)
memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
else
memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
if (user_data != NULL)
ctxt->userData = user_data;
}
inputStream = xmlNewIOInputStream(ctxt, buf, enc); inputStream = xmlNewIOInputStream(ctxt, buf, enc);
if (inputStream == NULL) { if (inputStream == NULL) {
@ -12675,7 +12636,7 @@ xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
if (input == NULL) if (input == NULL)
return(NULL); return(NULL);
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(sax, NULL);
if (ctxt == NULL) { if (ctxt == NULL) {
xmlFreeParserInputBuffer(input); xmlFreeParserInputBuffer(input);
return(NULL); return(NULL);
@ -12684,15 +12645,6 @@ xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
/* We are loading a DTD */ /* We are loading a DTD */
ctxt->options |= XML_PARSE_DTDLOAD; ctxt->options |= XML_PARSE_DTDLOAD;
/*
* Set-up the SAX context
*/
if (sax != NULL) {
if (ctxt->sax != NULL)
xmlFree(ctxt->sax);
ctxt->sax = sax;
ctxt->userData = ctxt;
}
xmlDetectSAX2(ctxt); xmlDetectSAX2(ctxt);
/* /*
@ -12701,7 +12653,6 @@ xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
if (pinput == NULL) { if (pinput == NULL) {
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserInputBuffer(input); xmlFreeParserInputBuffer(input);
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(NULL); return(NULL);
@ -12711,7 +12662,6 @@ xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
* plug some encoding conversion routines here. * plug some encoding conversion routines here.
*/ */
if (xmlPushInput(ctxt, pinput) < 0) { if (xmlPushInput(ctxt, pinput) < 0) {
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(NULL); return(NULL);
} }
@ -12778,7 +12728,6 @@ xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
xmlFreeDoc(ctxt->myDoc); xmlFreeDoc(ctxt->myDoc);
ctxt->myDoc = NULL; ctxt->myDoc = NULL;
} }
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(ret); return(ret);
@ -12806,7 +12755,7 @@ xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(sax, NULL);
if (ctxt == NULL) { if (ctxt == NULL) {
return(NULL); return(NULL);
} }
@ -12814,16 +12763,6 @@ xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
/* We are loading a DTD */ /* We are loading a DTD */
ctxt->options |= XML_PARSE_DTDLOAD; ctxt->options |= XML_PARSE_DTDLOAD;
/*
* Set-up the SAX context
*/
if (sax != NULL) {
if (ctxt->sax != NULL)
xmlFree(ctxt->sax);
ctxt->sax = sax;
ctxt->userData = ctxt;
}
/* /*
* Canonicalise the system ID * Canonicalise the system ID
*/ */
@ -12841,7 +12780,6 @@ xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
systemIdCanonic); systemIdCanonic);
if (input == NULL) { if (input == NULL) {
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
if (systemIdCanonic != NULL) if (systemIdCanonic != NULL)
xmlFree(systemIdCanonic); xmlFree(systemIdCanonic);
@ -12852,7 +12790,6 @@ xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
* plug some encoding conversion routines here. * plug some encoding conversion routines here.
*/ */
if (xmlPushInput(ctxt, input) < 0) { if (xmlPushInput(ctxt, input) < 0) {
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
if (systemIdCanonic != NULL) if (systemIdCanonic != NULL)
xmlFree(systemIdCanonic); xmlFree(systemIdCanonic);
@ -12880,7 +12817,6 @@ xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
if (ctxt->myDoc == NULL) { if (ctxt->myDoc == NULL) {
xmlErrMemory(ctxt, "New Doc failed"); xmlErrMemory(ctxt, "New Doc failed");
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(NULL); return(NULL);
} }
@ -12909,7 +12845,6 @@ xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
xmlFreeDoc(ctxt->myDoc); xmlFreeDoc(ctxt->myDoc);
ctxt->myDoc = NULL; ctxt->myDoc = NULL;
} }
if (sax != NULL) ctxt->sax = NULL;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(ret); return(ret);
@ -13000,7 +12935,6 @@ xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt;
xmlDocPtr newDoc; xmlDocPtr newDoc;
xmlNodePtr newRoot; xmlNodePtr newRoot;
xmlSAXHandlerPtr oldsax = NULL;
xmlParserErrors ret = XML_ERR_OK; xmlParserErrors ret = XML_ERR_OK;
xmlChar start[4]; xmlChar start[4];
xmlCharEncoding enc; xmlCharEncoding enc;
@ -13018,17 +12952,11 @@ xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
if (doc == NULL) if (doc == NULL)
return(XML_ERR_INTERNAL_ERROR); return(XML_ERR_INTERNAL_ERROR);
ctxt = xmlCreateEntityParserCtxtInternal(sax, user_data, URL, ID, NULL,
ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, oldctxt); oldctxt);
if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
ctxt->userData = ctxt;
if (sax != NULL) {
oldsax = ctxt->sax;
ctxt->sax = sax;
if (user_data != NULL)
ctxt->userData = user_data;
}
xmlDetectSAX2(ctxt); xmlDetectSAX2(ctxt);
newDoc = xmlNewDoc(BAD_CAST "1.0"); newDoc = xmlNewDoc(BAD_CAST "1.0");
if (newDoc == NULL) { if (newDoc == NULL) {
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
@ -13049,7 +12977,6 @@ xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
if (newRoot == NULL) { if (newRoot == NULL) {
if (sax != NULL) if (sax != NULL)
ctxt->sax = oldsax;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
newDoc->intSubset = NULL; newDoc->intSubset = NULL;
newDoc->extSubset = NULL; newDoc->extSubset = NULL;
@ -13190,8 +13117,6 @@ xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
if ((oldctxt != NULL) && (ctxt->lastError.code != XML_ERR_OK)) if ((oldctxt != NULL) && (ctxt->lastError.code != XML_ERR_OK))
xmlCopyError(&ctxt->lastError, &oldctxt->lastError); xmlCopyError(&ctxt->lastError, &oldctxt->lastError);
if (sax != NULL)
ctxt->sax = oldsax;
if (oldctxt != NULL) { if (oldctxt != NULL) {
ctxt->dict = NULL; ctxt->dict = NULL;
ctxt->attsDefault = NULL; ctxt->attsDefault = NULL;
@ -13942,14 +13867,15 @@ xmlParseEntity(const char *filename) {
* Returns the new parser context or NULL * Returns the new parser context or NULL
*/ */
static xmlParserCtxtPtr static xmlParserCtxtPtr
xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, xmlCreateEntityParserCtxtInternal(xmlSAXHandlerPtr sax, void *userData,
const xmlChar *base, xmlParserCtxtPtr pctx) { const xmlChar *URL, const xmlChar *ID, const xmlChar *base,
xmlParserCtxtPtr pctx) {
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt;
xmlParserInputPtr inputStream; xmlParserInputPtr inputStream;
char *directory = NULL; char *directory = NULL;
xmlChar *uri; xmlChar *uri;
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(sax, userData);
if (ctxt == NULL) { if (ctxt == NULL) {
return(NULL); return(NULL);
} }
@ -14017,7 +13943,7 @@ xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID,
xmlParserCtxtPtr xmlParserCtxtPtr
xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
const xmlChar *base) { const xmlChar *base) {
return xmlCreateEntityParserCtxtInternal(URL, ID, base, NULL); return xmlCreateEntityParserCtxtInternal(NULL, NULL, URL, ID, base, NULL);
} }

View File

@ -1439,16 +1439,19 @@ xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
************************************************************************/ ************************************************************************/
/** /**
* xmlInitParserCtxt: * xmlInitSAXParserCtxt:
* @ctxt: an XML parser context * @ctxt: XML parser context
* @sax: SAX handlert
* @userData: user data
* *
* Initialize a parser context * Initialize a SAX parser context
* *
* Returns 0 in case of success and -1 in case of error * Returns 0 in case of success and -1 in case of error
*/ */
int static int
xmlInitParserCtxt(xmlParserCtxtPtr ctxt) xmlInitSAXParserCtxt(xmlParserCtxtPtr ctxt, xmlSAXHandlerPtr sax,
void *userData)
{ {
xmlParserInputPtr input; xmlParserInputPtr input;
@ -1473,8 +1476,19 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
xmlErrMemory(NULL, "cannot initialize parser context\n"); xmlErrMemory(NULL, "cannot initialize parser context\n");
return(-1); return(-1);
} }
else if (sax == NULL) {
memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
xmlSAXVersion(ctxt->sax, 2); xmlSAXVersion(ctxt->sax, 2);
ctxt->userData = ctxt;
} else {
if (sax->initialized == XML_SAX2_MAGIC) {
memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
} else {
memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
}
ctxt->userData = userData ? userData : ctxt;
}
ctxt->maxatts = 0; ctxt->maxatts = 0;
ctxt->atts = NULL; ctxt->atts = NULL;
@ -1572,7 +1586,6 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
ctxt->spaceMax = 10; ctxt->spaceMax = 10;
ctxt->spaceTab[0] = -1; ctxt->spaceTab[0] = -1;
ctxt->space = &ctxt->spaceTab[0]; ctxt->space = &ctxt->spaceTab[0];
ctxt->userData = ctxt;
ctxt->myDoc = NULL; ctxt->myDoc = NULL;
ctxt->wellFormed = 1; ctxt->wellFormed = 1;
ctxt->nsWellFormed = 1; ctxt->nsWellFormed = 1;
@ -1624,6 +1637,24 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
return(0); return(0);
} }
/**
* xmlInitParserCtxt:
* @ctxt: an XML parser context
*
* DEPRECATED: Internal function which will be made private in a future
* version.
*
* Initialize a parser context
*
* Returns 0 in case of success and -1 in case of error
*/
int
xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
{
return(xmlInitSAXParserCtxt(ctxt, NULL, NULL));
}
/** /**
* xmlFreeParserCtxt: * xmlFreeParserCtxt:
* @ctxt: an XML parser context * @ctxt: an XML parser context
@ -1720,6 +1751,22 @@ xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
xmlParserCtxtPtr xmlParserCtxtPtr
xmlNewParserCtxt(void) xmlNewParserCtxt(void)
{
return(xmlNewSAXParserCtxt(NULL, NULL));
}
/**
* xmlNewSAXParserCtxt:
* @sax: SAX handler
* @userData: user data
*
* Allocate and initialize a new SAX parser context.
*
* Returns the xmlParserCtxtPtr or NULL
*/
xmlParserCtxtPtr
xmlNewSAXParserCtxt(xmlSAXHandlerPtr sax, void *userData)
{ {
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt;
@ -1729,7 +1776,7 @@ xmlNewParserCtxt(void)
return(NULL); return(NULL);
} }
memset(ctxt, 0, sizeof(xmlParserCtxt)); memset(ctxt, 0, sizeof(xmlParserCtxt));
if (xmlInitParserCtxt(ctxt) < 0) { if (xmlInitSAXParserCtxt(ctxt, sax, userData) < 0) {
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(NULL); return(NULL);
} }

View File

@ -464,8 +464,6 @@ static void des_xmlParserCtxtPtr(int no ATTRIBUTE_UNUSED, xmlParserCtxtPtr val,
xmlFreeParserCtxt(val); xmlFreeParserCtxt(val);
} }
#if defined(LIBXML_PUSH_ENABLED) || defined(LIBXML_SAX1_ENABLED) || \
defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_VALID_ENABLED)
#define gen_nb_xmlSAXHandlerPtr 2 #define gen_nb_xmlSAXHandlerPtr 2
static xmlSAXHandlerPtr gen_xmlSAXHandlerPtr(int no, int nr ATTRIBUTE_UNUSED) { static xmlSAXHandlerPtr gen_xmlSAXHandlerPtr(int no, int nr ATTRIBUTE_UNUSED) {
(void) no; (void) no;
@ -476,7 +474,6 @@ static xmlSAXHandlerPtr gen_xmlSAXHandlerPtr(int no, int nr ATTRIBUTE_UNUSED) {
} }
static void des_xmlSAXHandlerPtr(int no ATTRIBUTE_UNUSED, xmlSAXHandlerPtr val ATTRIBUTE_UNUSED, int nr ATTRIBUTE_UNUSED) { static void des_xmlSAXHandlerPtr(int no ATTRIBUTE_UNUSED, xmlSAXHandlerPtr val ATTRIBUTE_UNUSED, int nr ATTRIBUTE_UNUSED) {
} }
#endif
#define gen_nb_xmlValidCtxtPtr 2 #define gen_nb_xmlValidCtxtPtr 2
static xmlValidCtxtPtr gen_xmlValidCtxtPtr(int no, int nr ATTRIBUTE_UNUSED) { static xmlValidCtxtPtr gen_xmlValidCtxtPtr(int no, int nr ATTRIBUTE_UNUSED) {
@ -2181,6 +2178,47 @@ test_htmlNewParserCtxt(void) {
} }
static int
test_htmlNewSAXParserCtxt(void) {
int test_ret = 0;
#if defined(LIBXML_HTML_ENABLED)
int mem_base;
htmlParserCtxtPtr ret_val;
htmlSAXHandlerPtr sax; /* SAX handler */
int n_sax;
void * userData; /* user data */
int n_userData;
for (n_sax = 0;n_sax < gen_nb_htmlSAXHandlerPtr;n_sax++) {
for (n_userData = 0;n_userData < gen_nb_userdata;n_userData++) {
mem_base = xmlMemBlocks();
sax = gen_htmlSAXHandlerPtr(n_sax, 0);
userData = gen_userdata(n_userData, 1);
ret_val = htmlNewSAXParserCtxt(sax, userData);
desret_htmlParserCtxtPtr(ret_val);
call_tests++;
des_htmlSAXHandlerPtr(n_sax, sax, 0);
des_userdata(n_userData, userData, 1);
xmlResetLastError();
if (mem_base != xmlMemBlocks()) {
printf("Leak of %d blocks found in htmlNewSAXParserCtxt",
xmlMemBlocks() - mem_base);
test_ret++;
printf(" %d", n_sax);
printf(" %d", n_userData);
printf("\n");
}
}
}
function_tests++;
#endif
return(test_ret);
}
static int static int
test_htmlNodeStatus(void) { test_htmlNodeStatus(void) {
int test_ret = 0; int test_ret = 0;
@ -2786,7 +2824,7 @@ static int
test_HTMLparser(void) { test_HTMLparser(void) {
int test_ret = 0; int test_ret = 0;
if (quiet == 0) printf("Testing HTMLparser : 32 of 38 functions ...\n"); if (quiet == 0) printf("Testing HTMLparser : 33 of 39 functions ...\n");
test_ret += test_UTF8ToHtml(); test_ret += test_UTF8ToHtml();
test_ret += test_htmlAttrAllowed(); test_ret += test_htmlAttrAllowed();
test_ret += test_htmlAutoCloseTag(); test_ret += test_htmlAutoCloseTag();
@ -2806,6 +2844,7 @@ test_HTMLparser(void) {
test_ret += test_htmlIsAutoClosed(); test_ret += test_htmlIsAutoClosed();
test_ret += test_htmlIsScriptAttribute(); test_ret += test_htmlIsScriptAttribute();
test_ret += test_htmlNewParserCtxt(); test_ret += test_htmlNewParserCtxt();
test_ret += test_htmlNewSAXParserCtxt();
test_ret += test_htmlNodeStatus(); test_ret += test_htmlNodeStatus();
test_ret += test_htmlParseCharRef(); test_ret += test_htmlParseCharRef();
test_ret += test_htmlParseChunk(); test_ret += test_htmlParseChunk();
@ -12764,6 +12803,45 @@ test_xmlNewParserCtxt(void) {
} }
static int
test_xmlNewSAXParserCtxt(void) {
int test_ret = 0;
int mem_base;
xmlParserCtxtPtr ret_val;
xmlSAXHandlerPtr sax; /* SAX handler */
int n_sax;
void * userData; /* user data */
int n_userData;
for (n_sax = 0;n_sax < gen_nb_xmlSAXHandlerPtr;n_sax++) {
for (n_userData = 0;n_userData < gen_nb_userdata;n_userData++) {
mem_base = xmlMemBlocks();
sax = gen_xmlSAXHandlerPtr(n_sax, 0);
userData = gen_userdata(n_userData, 1);
ret_val = xmlNewSAXParserCtxt(sax, userData);
desret_xmlParserCtxtPtr(ret_val);
call_tests++;
des_xmlSAXHandlerPtr(n_sax, sax, 0);
des_userdata(n_userData, userData, 1);
xmlResetLastError();
if (mem_base != xmlMemBlocks()) {
printf("Leak of %d blocks found in xmlNewSAXParserCtxt",
xmlMemBlocks() - mem_base);
test_ret++;
printf(" %d", n_sax);
printf(" %d", n_userData);
printf("\n");
}
}
}
function_tests++;
return(test_ret);
}
#define gen_nb_xmlNodePtr_ptr 1 #define gen_nb_xmlNodePtr_ptr 1
#define gen_xmlNodePtr_ptr(no, nr) NULL #define gen_xmlNodePtr_ptr(no, nr) NULL
#define des_xmlNodePtr_ptr(no, val, nr) #define des_xmlNodePtr_ptr(no, val, nr)
@ -14587,7 +14665,7 @@ static int
test_parser(void) { test_parser(void) {
int test_ret = 0; int test_ret = 0;
if (quiet == 0) printf("Testing parser : 58 of 70 functions ...\n"); if (quiet == 0) printf("Testing parser : 59 of 71 functions ...\n");
test_ret += test_xmlByteConsumed(); test_ret += test_xmlByteConsumed();
test_ret += test_xmlClearNodeInfoSeq(); test_ret += test_xmlClearNodeInfoSeq();
test_ret += test_xmlClearParserCtxt(); test_ret += test_xmlClearParserCtxt();
@ -14610,6 +14688,7 @@ test_parser(void) {
test_ret += test_xmlLoadExternalEntity(); test_ret += test_xmlLoadExternalEntity();
test_ret += test_xmlNewIOInputStream(); test_ret += test_xmlNewIOInputStream();
test_ret += test_xmlNewParserCtxt(); test_ret += test_xmlNewParserCtxt();
test_ret += test_xmlNewSAXParserCtxt();
test_ret += test_xmlParseBalancedChunkMemory(); test_ret += test_xmlParseBalancedChunkMemory();
test_ret += test_xmlParseBalancedChunkMemoryRecover(); test_ret += test_xmlParseBalancedChunkMemoryRecover();
test_ret += test_xmlParseChunk(); test_ret += test_xmlParseChunk();

View File

@ -1261,19 +1261,15 @@ saxTest(const char *filename, size_t limit, int options, int fail) {
int res = 0; int res = 0;
xmlParserCtxtPtr ctxt; xmlParserCtxtPtr ctxt;
xmlDocPtr doc; xmlDocPtr doc;
xmlSAXHandlerPtr old_sax;
nb_tests++; nb_tests++;
maxlen = limit; maxlen = limit;
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(callbackSAX2Handler, NULL);
if (ctxt == NULL) { if (ctxt == NULL) {
fprintf(stderr, "Failed to create parser context\n"); fprintf(stderr, "Failed to create parser context\n");
return(1); return(1);
} }
old_sax = ctxt->sax;
ctxt->sax = callbackSAX2Handler;
ctxt->userData = NULL;
doc = xmlCtxtReadFile(ctxt, filename, NULL, options); doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
if (doc != NULL) { if (doc != NULL) {
@ -1296,7 +1292,6 @@ saxTest(const char *filename, size_t limit, int options, int fail) {
} else } else
res = 0; res = 0;
} }
ctxt->sax = old_sax;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
return(res); return(res);

View File

@ -297,6 +297,9 @@ htmlNewDocNoDtD
htmlNewParserCtxt htmlNewParserCtxt
#endif #endif
#ifdef LIBXML_HTML_ENABLED #ifdef LIBXML_HTML_ENABLED
htmlNewSAXParserCtxt
#endif
#ifdef LIBXML_HTML_ENABLED
htmlNodeDump htmlNodeDump
#endif #endif
#ifdef LIBXML_HTML_ENABLED #ifdef LIBXML_HTML_ENABLED
@ -1214,6 +1217,7 @@ xmlNewParserCtxt
xmlNewProp xmlNewProp
xmlNewRMutex xmlNewRMutex
xmlNewReference xmlNewReference
xmlNewSAXParserCtxt
xmlNewStringInputStream xmlNewStringInputStream
xmlNewText xmlNewText
xmlNewTextChild xmlNewTextChild

View File

@ -1586,10 +1586,6 @@ static void
testSAX(const char *filename) { testSAX(const char *filename) {
xmlSAXHandlerPtr handler; xmlSAXHandlerPtr handler;
const char *user_data = "user_data"; /* mostly for debugging */ const char *user_data = "user_data"; /* mostly for debugging */
xmlParserInputBufferPtr buf = NULL;
xmlParserInputPtr inputStream;
xmlParserCtxtPtr ctxt = NULL;
xmlSAXHandlerPtr old_sax = NULL;
callbacks = 0; callbacks = 0;
@ -1603,24 +1599,22 @@ testSAX(const char *filename) {
handler = debugSAX2Handler; handler = debugSAX2Handler;
} }
/*
* it's not the simplest code but the most generic in term of I/O
*/
buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
if (buf == NULL) {
goto error;
}
#ifdef LIBXML_SCHEMAS_ENABLED #ifdef LIBXML_SCHEMAS_ENABLED
if (wxschemas != NULL) { if (wxschemas != NULL) {
int ret; int ret;
xmlSchemaValidCtxtPtr vctxt; xmlSchemaValidCtxtPtr vctxt;
xmlParserInputBufferPtr buf;
buf = xmlParserInputBufferCreateFilename(filename,
XML_CHAR_ENCODING_NONE);
if (buf == NULL)
return;
vctxt = xmlSchemaNewValidCtxt(wxschemas); vctxt = xmlSchemaNewValidCtxt(wxschemas);
if (vctxt == NULL) { if (vctxt == NULL) {
progresult = XMLLINT_ERR_MEM; progresult = XMLLINT_ERR_MEM;
xmlFreeParserInputBuffer(buf); xmlFreeParserInputBuffer(buf);
goto error; return;
} }
xmlSchemaSetValidErrors(vctxt, xmlGenericError, xmlGenericError, NULL); xmlSchemaSetValidErrors(vctxt, xmlGenericError, xmlGenericError, NULL);
xmlSchemaValidateSetFilename(vctxt, filename); xmlSchemaValidateSetFilename(vctxt, filename);
@ -1645,38 +1639,23 @@ testSAX(const char *filename) {
} else } else
#endif #endif
{ {
xmlParserCtxtPtr ctxt = NULL;
/* /*
* Create the parser context amd hook the input * Create the parser context amd hook the input
*/ */
ctxt = xmlNewParserCtxt(); ctxt = xmlNewSAXParserCtxt(handler, (void *) user_data);
if (ctxt == NULL) { if (ctxt == NULL) {
progresult = XMLLINT_ERR_MEM; progresult = XMLLINT_ERR_MEM;
xmlFreeParserInputBuffer(buf); return;
goto error;
} }
old_sax = ctxt->sax; xmlCtxtReadFile(ctxt, filename, NULL, options);
ctxt->sax = handler;
ctxt->userData = (void *) user_data;
inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
if (inputStream == NULL) {
xmlFreeParserInputBuffer(buf);
goto error;
}
inputPush(ctxt, inputStream);
/* do the parsing */
xmlParseDocument(ctxt);
if (ctxt->myDoc != NULL) { if (ctxt->myDoc != NULL) {
fprintf(stderr, "SAX generated a doc !\n"); fprintf(stderr, "SAX generated a doc !\n");
xmlFreeDoc(ctxt->myDoc); xmlFreeDoc(ctxt->myDoc);
ctxt->myDoc = NULL; ctxt->myDoc = NULL;
} }
}
error:
if (ctxt != NULL) {
ctxt->sax = old_sax;
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
} }
} }

View File

@ -29063,7 +29063,6 @@ xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
xmlSAXHandlerPtr sax, void *user_data) xmlSAXHandlerPtr sax, void *user_data)
{ {
xmlSchemaSAXPlugPtr plug = NULL; xmlSchemaSAXPlugPtr plug = NULL;
xmlSAXHandlerPtr old_sax = NULL;
xmlParserCtxtPtr pctxt = NULL; xmlParserCtxtPtr pctxt = NULL;
xmlParserInputPtr inputStream = NULL; xmlParserInputPtr inputStream = NULL;
int ret; int ret;
@ -29074,12 +29073,9 @@ xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
/* /*
* prepare the parser * prepare the parser
*/ */
pctxt = xmlNewParserCtxt(); pctxt = xmlNewSAXParserCtxt(sax, user_data);
if (pctxt == NULL) if (pctxt == NULL)
return (-1); return (-1);
old_sax = pctxt->sax;
pctxt->sax = sax;
pctxt->userData = user_data;
#if 0 #if 0
if (options) if (options)
xmlCtxtUseOptions(pctxt, options); xmlCtxtUseOptions(pctxt, options);
@ -29125,7 +29121,6 @@ done:
} }
/* cleanup */ /* cleanup */
if (pctxt != NULL) { if (pctxt != NULL) {
pctxt->sax = old_sax;
xmlFreeParserCtxt(pctxt); xmlFreeParserCtxt(pctxt);
} }
return (ret); return (ret);