--- parser3/src/classes/xnode.C 2002/01/14 15:29:41 1.26 +++ parser3/src/classes/xnode.C 2002/02/19 15:03:10 1.39 @@ -1,10 +1,10 @@ /** @file Parser: @b dom parser class. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: xnode.C,v 1.26 2002/01/14 15:29:41 paf Exp $ + $Id: xnode.C,v 1.39 2002/02/19 15:03:10 paf Exp $ */ #include "classes.h" #ifdef XML @@ -12,23 +12,28 @@ #include "pa_charset.h" #include "pa_request.h" #include "pa_vxnode.h" +#include "pa_vxdoc.h" #include "pa_vvoid.h" #include "xnode.h" extern "C" { #include "gdomecore/gdome-xml-node.h" -}; #include "gdomecore/gdome-xml-document.h" +}; #include "gdome.h" #include "libxml/xpath.h" +// defines + +#define XNODE_CLASS_NAME "xnode" + // classes class xmlXPathObject_auto_ptr { public: - explicit xmlXPathObject_auto_ptr(xmlXPathObject *_P = 0) - : _Owns(_P != 0), _Ptr(_P) {} + explicit xmlXPathObject_auto_ptr(xmlXPathObject *_APtr = 0) + : _Owns(_APtr != 0), _Ptr(_APtr) {} xmlXPathObject_auto_ptr(const xmlXPathObject_auto_ptr& _Y) : _Owns(_Y._Owns), _Ptr(_Y.release()) {} xmlXPathObject_auto_ptr& operator=(const xmlXPathObject_auto_ptr& _Y) @@ -60,8 +65,8 @@ private: class xmlXPathContext_auto_ptr { public: - explicit xmlXPathContext_auto_ptr(xmlXPathContext *_P = 0) - : _Owns(_P != 0), _Ptr(_P) {} + explicit xmlXPathContext_auto_ptr(xmlXPathContext *_APtr = 0) + : _Owns(_APtr != 0), _Ptr(_APtr) {} xmlXPathContext_auto_ptr(const xmlXPathContext_auto_ptr& _Y) : _Owns(_Y._Owns), _Ptr(_Y.release()) {} xmlXPathContext_auto_ptr& operator=(const xmlXPathContext_auto_ptr& _Y) @@ -91,16 +96,13 @@ private: xmlXPathContext *_Ptr; }; -// defines - -#define XNODE_CLASS_NAME "xnode" - // helpers GdomeNode *as_node(Pool& pool, const String& method_name, MethodParams *params, int index, const char *msg) { Value& value=params->as_no_junction(index, msg); - if(strcmp(value.type(), VXNODE_TYPE)!=0) + if(!(strcmp(value.type(), VXNODE_TYPE)==0 + || strcmp(value.type(), VXDOC_TYPE)==0)) throw Exception(0, 0, &method_name, msg); @@ -212,7 +214,6 @@ static void _hasChildNodes(Request& r, c } // Node cloneNode(in boolean deep); -/// @test ownerDocument=? static void _cloneNode(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); VXnode& vnode=*static_cast(r.self); @@ -367,6 +368,39 @@ static void _getElementsByTagName(Reques r.write_no_lang(result); } +static void _getElementsByTagNameNS(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + GdomeElement *element=get_self_element(r, method_name); + + // namespaceURI;localName + const String& namespaceURI=params->as_string(0, "namespaceURI must be string"); + const String& localName=params->as_string(1, "localName must be string"); + + GdomeException exc; + VHash& result=*new(pool) VHash(pool); + if(GdomeNodeList *nodes= + gdome_el_getElementsByTagNameNS( + element, + pool.transcode(namespaceURI).get(), + pool.transcode(localName).get(), + &exc)) { + gulong length=gdome_nl_length(nodes, &exc); + for(gulong i=0; in; + if(!dome_document) // document does not own itself, so ownerDocument = 0 + dome_document=GDOME_DOC(dome_node); // and we need downcast + xmlDoc *xml_document=gdome_xml_doc_get_xmlDoc(dome_document); xmlXPathContext_auto_ptr ctxt(xmlXPathNewContext(xml_document)); - ctxt->node=xmlDocGetRootElement(xml_document); + ctxt->node=gdome_xml_n_get_xmlNode(dome_node); /*error to stderr for now*/ xmlXPathObject_auto_ptr res( xmlXPathEvalExpression(BAD_CAST pool.transcode(expression)->str, ctxt.get())); + if(xmlHaveGenericErrors()) { + GdomeException exc=0; + throw Exception(0, 0, + &expression, + exc); + } + Value *result=0; if(res.get()) handler(pool, expression, res, result); @@ -418,12 +459,12 @@ static void selectNodesHandler(Pool& poo const String& expression, xmlXPathObject_auto_ptr res, Value *& result) { + VHash *vhash=new(pool) VHash(pool); result=vhash; switch(res->type) { case XPATH_UNDEFINED: break; case XPATH_NODESET: if(int size=res->nodesetval->nodeNr) { - VHash *vhash=new(pool) VHash(pool); Hash& hash=vhash->hash(0); for(int i=0; inodesetval->nodeTab[0]))); + gdome_xml_n_mkref(res->nodesetval->nodeTab[i]))); } - result=vhash; } break; default: @@ -611,6 +651,8 @@ MXnode::MXnode(Pool& apool) : Methoded(a add_native_method("removeAttributeNode", Method::CT_DYNAMIC, _removeAttributeNode, 1, 1); // NodeList getElementsByTagName(in DOMString name); add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1); + // NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName); + add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2); // void normalize(); add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0);