From: Ilya M. <m_...@us...> - 2003-03-20 10:16:03
|
Update of /cvsroot/perl-xml/XML-LibXML-XPathContext In directory sc8-pr-cvs1:/tmp/cvs-serv14801 Modified Files: XPathContext.xs Log Message: Preserve current node and reset context structure before each method call Index: XPathContext.xs =================================================================== RCS file: /cvsroot/perl-xml/XML-LibXML-XPathContext/XPathContext.xs,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- XPathContext.xs 20 Mar 2003 09:02:27 -0000 1.10 +++ XPathContext.xs 20 Mar 2003 10:15:59 -0000 1.11 @@ -407,6 +407,36 @@ LEAVE; } +static void +LibXML_configure_namespaces( xmlXPathContextPtr ctxt ) { + xmlNodePtr node = ctxt->node; + + if (ctxt->namespaces != NULL) { + xmlFree( ctxt->namespaces ); + } + if (node->type == XML_DOCUMENT_NODE) { + ctxt->namespaces = xmlGetNsList( node->doc, + xmlDocGetRootElement( node->doc ) ); + } else { + ctxt->namespaces = xmlGetNsList(node->doc, node); + } + ctxt->nsNr = 0; + if (ctxt->namespaces != NULL) { + while (ctxt->namespaces[ctxt->nsNr] != NULL) + ctxt->nsNr++; + } +} + +static void +LibXML_configure_xpathcontext( xmlXPathContextPtr ctxt ) { + xmlNodePtr node = PmmSvNode(ctxt->user); + + ctxt->doc = node->doc; + ctxt->node = node; + + LibXML_configure_namespaces(ctxt); +} + MODULE = XML::LibXML::XPathContext PACKAGE = XML::LibXML::XPathContext SV* @@ -414,28 +444,11 @@ const char * CLASS SV * pnode INIT: - xmlNodePtr node = PmmSvNode(pnode); xmlXPathContextPtr ctxt; CODE: - ctxt = xmlXPathNewContext( node->doc ); - ctxt->node = node; - - /* we want the node and doc live as long as ctxt does */ - PmmREFCNT_inc(SvPROXYNODE(pnode)); - PmmREFCNT_inc(PmmNewNode((xmlNodePtr)node->doc)); - - /* get the namespace information */ - if (node->type == XML_DOCUMENT_NODE) { - ctxt->namespaces = xmlGetNsList( node->doc, - xmlDocGetRootElement( node->doc ) ); - } else { - ctxt->namespaces = xmlGetNsList(node->doc, node); - } - ctxt->nsNr = 0; - if (ctxt->namespaces != NULL) { - while (ctxt->namespaces[ctxt->nsNr] != NULL) - ctxt->nsNr++; - } + ctxt = xmlXPathNewContext( NULL ); + ctxt->user = pnode; + SvREFCNT_inc(pnode); xmlXPathRegisterFunc(ctxt, (const xmlChar *) "document", @@ -456,11 +469,8 @@ CODE: xs_warn( "DESTROY XPATH CONTEXT" ); if (ctxt) { - if (ctxt->node) { - PmmREFCNT_dec(ctxt->node->_private); - } - if (ctxt->doc) { - PmmREFCNT_dec(ctxt->doc->_private); + if (ctxt->user) { + SvREFCNT_dec(ctxt->user); } if (ctxt->namespaces != NULL) { @@ -483,6 +493,10 @@ SV * self INIT: xmlXPathContextPtr ctxt = (xmlXPathContextPtr)SvIV(SvRV(self)); + if ( ctxt == NULL ) { + croak( "missing xpath context" ); + } + LibXML_configure_xpathcontext(ctxt); CODE: if (ctxt->node != NULL) { RETVAL = PmmNodeToSv(ctxt->node, @@ -500,46 +514,16 @@ SV * self SV * pnode INIT: - xmlNodePtr node = PmmSvNode(pnode); xmlXPathContextPtr ctxt = (xmlXPathContextPtr)SvIV(SvRV(self)); + if ( ctxt == NULL ) { + croak( "missing xpath context" ); + } PPCODE: - if (node != NULL) { - /* we want the node and doc live as long as ctxt does */ - PmmREFCNT_inc(SvPROXYNODE(pnode)); - PmmREFCNT_inc(PmmNewNode((xmlNodePtr)node->doc)); - - if (ctxt->node != NULL) { - PmmREFCNT_dec(ctxt->node->_private); - } else { - warn("XPathContext: Lost previous context node"); - } - if (ctxt->doc != NULL) { - PmmREFCNT_dec(ctxt->doc->_private); - } else { - warn("XPathContext: Lost previous document node"); - } - ctxt->node = node; - ctxt->doc = node->doc; - - /* free old namespace information */ - if (ctxt->namespaces != NULL) { - xmlFree( ctxt->namespaces ); - } - /* get new namespace information */ - if (node->type == XML_DOCUMENT_NODE) { - ctxt->namespaces = xmlGetNsList( node->doc, - xmlDocGetRootElement( node->doc ) ); - } else { - ctxt->namespaces = xmlGetNsList(node->doc, node); - } - ctxt->nsNr = 0; - if (ctxt->namespaces != NULL) { - while (ctxt->namespaces[ctxt->nsNr] != NULL) - ctxt->nsNr++; - } - } else { - croak("XPathContext: Cannot set an undefined context node"); + if (ctxt->user) { + SvREFCNT_dec(ctxt->user); } + ctxt->user = pnode; + SvREFCNT_inc(pnode); void registerNs( pxpath_context, prefix, ns_uri ) @@ -554,6 +538,7 @@ if ( ctxt == NULL ) { croak( "missing xpath context" ); } + LibXML_configure_xpathcontext(ctxt); PPCODE: ret = xmlXPathRegisterNs(ctxt, prefix, ns_uri); if(ret == -1) { @@ -566,6 +551,10 @@ INIT: SV ** lookup_data; xmlXPathContextPtr ctxt = (xmlXPathContextPtr)SvIV(SvRV(self)); + if ( ctxt == NULL ) { + croak( "missing xpath context" ); + } + LibXML_configure_xpathcontext(ctxt); CODE: if (ctxt->varLookupData != NULL && SvROK((SV*)(ctxt->varLookupData)) && @@ -599,6 +588,7 @@ if ( ctxt == NULL ) { croak( "missing xpath context" ); } + LibXML_configure_xpathcontext(ctxt); if ( SvROK(lookup_func) && SvTYPE(SvRV(lookup_func)) == SVt_PVCV ) { pfdr = newRV_inc((SV*) newAV()); av_push((AV *)SvRV(pfdr), SvREFCNT_inc(lookup_func)); @@ -639,6 +629,7 @@ if ( ctxt == NULL ) { croak( "missing xpath context" ); } + LibXML_configure_xpathcontext(ctxt); if ( SvTYPE(SvRV(func)) == SVt_PVCV ) { if (ctxt->funcLookupData == NULL) { pfdr = newRV_inc((SV*) newHV()); @@ -690,6 +681,7 @@ if ( ctxt == NULL ) { croak( "missing xpath context" ); } + LibXML_configure_xpathcontext(ctxt); if ( ctxt->node == NULL ) { croak( "lost node" ); } @@ -775,6 +767,7 @@ if ( ctxt == NULL ) { croak( "missing xpath context" ); } + LibXML_configure_xpathcontext(ctxt); if ( ctxt->node == NULL ) { croak( "lost node" ); } |