--- parser3/src/classes/xnode.C 2002/01/14 15:29:41 1.26 +++ parser3/src/classes/xnode.C 2002/03/27 15:30:35 1.41 @@ -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.41 2002/03/27 15:30:35 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,17 +96,14 @@ 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) - throw Exception(0, 0, + if(!(strcmp(value.type(), VXNODE_TYPE)==0 + || strcmp(value.type(), VXDOC_TYPE)==0)) + throw Exception("parser.runtime", &method_name, msg); @@ -116,7 +118,7 @@ GdomeAttr * as_attr(Pool& pool, const St GdomeNode *node=as_node(pool, method_name, params, index, msg); GdomeException exc; if(gdome_n_nodeType(node, &exc)!=GDOME_ATTRIBUTE_NODE) - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, msg); @@ -141,7 +143,7 @@ static void _insertBefore(Request& r, co VXnode& result=*new(pool) VXnode(pool, retNode); r.write_no_lang(result); } else - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -159,7 +161,7 @@ static void _replaceChild(Request& r, co // write out result r.write_no_lang(*new(pool) VXnode(pool, retNode)); } else - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -176,7 +178,7 @@ static void _removeChild(Request& r, con // write out result r.write_no_lang(*new(pool) VXnode(pool, retNode)); } else - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -193,7 +195,7 @@ static void _appendChild(Request& r, con // write out result r.write_no_lang(*new(pool) VXnode(pool, retNode)); } else - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -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); @@ -236,7 +237,7 @@ GdomeElement *get_self_element(Request& GdomeException exc; if(gdome_n_nodeType(node, &exc)!=GDOME_ELEMENT_NODE) - throw Exception(0, 0, + throw Exception("parser.runtime", &method_name, "method can be called on node of ELEMENT type"); @@ -269,7 +270,7 @@ static void _setAttribute(Request& r, co pool.transcode(attribute_value).get(), &exc); if(exc) - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -283,7 +284,7 @@ static void _removeAttribute(Request& r, GdomeException exc; gdome_el_removeAttribute(element, pool.transcode(name).get(), &exc); if(exc) - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -300,7 +301,7 @@ static void _getAttributeNode(Request& r VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)attr); r.write_no_lang(result); } else if(exc) - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -317,7 +318,7 @@ static void _setAttributeNode(Request& r VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)returnAttr); r.write_no_lang(result); } else - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -331,7 +332,7 @@ static void _removeAttributeNode(Request GdomeException exc; gdome_el_removeAttributeNode(element, oldAttr, &exc); if(exc) - throw Exception(0, 0, + throw Exception( &method_name, exc); } @@ -359,7 +360,7 @@ static void _getElementsByTagName(Reques result.hash(0).put(skey, new(pool) VXnode(pool, gdome_nl_item(nodes, i, &exc))); } } else if(exc) - throw Exception(0, 0, + throw Exception( &method_name, exc); @@ -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(&expression, exc); + } + Value *result=0; if(res.get()) handler(pool, expression, res, result); @@ -418,29 +457,29 @@ 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) + if(int size=res->nodesetval->nodeNr) { + Hash& hash=vhash->hash(0); + for(int i=0; inodesetval->nodeTab[0]))); + hash.put(skey, new(pool) VXnode(pool, + gdome_xml_n_mkref(res->nodesetval->nodeTab[i]))); + } } - result=vhash; - } break; default: - throw Exception(0, 0, + throw Exception(0, &expression, "wrong xmlXPathEvalExpression result type (%d)", res->type); break; // never @@ -455,9 +494,9 @@ static void selectNodeHandler(Pool& pool case XPATH_UNDEFINED: break; case XPATH_NODESET: - if(res->nodesetval->nodeNr) { // empty result strangly has NODESET res->type + if(res->nodesetval && res->nodesetval->nodeNr) { // empty result strangly has NODESET res->type if(res->nodesetval->nodeNr>1) - throw Exception(0, 0, + throw Exception("parser.runtime", &expression, "resulted not in a single node (%d)", res->nodesetval->nodeNr); @@ -477,7 +516,7 @@ static void selectNodeHandler(Pool& pool gdome_str_mkref_dup((const gchar *)res->stringval)).get())); break; default: - throw Exception(0, 0, + throw Exception("parser.runtime", &expression, "wrong xmlXPathEvalExpression result type (%d)", res->type); break; // never @@ -493,11 +532,11 @@ static void selectBoolHandler(Pool& pool result=new(pool) VBool(pool, res->boolval!=0); break; case XPATH_NODESET: - if(!res->nodesetval->nodeNr) + if(!(res->nodesetval && res->nodesetval->nodeNr)) break; // else[nodeset] fall down to default default: - throw Exception(0, 0, + throw Exception("parser.runtime", &expression, "wrong xmlXPathEvalExpression result type (%d)", res->type); break; // never @@ -513,11 +552,11 @@ static void selectNumberHandler(Pool& po result=new(pool) VDouble(pool, res->floatval); break; case XPATH_NODESET: - if(!res->nodesetval->nodeNr) + if(!(res->nodesetval && res->nodesetval->nodeNr)) break; // else[nodeset] fall down to default default: - throw Exception(0, 0, + throw Exception("parser.runtime", &expression, "wrong xmlXPathEvalExpression result type (%d)", res->type); break; // never @@ -538,11 +577,11 @@ static void selectStringHandler(Pool& po gdome_str_mkref_dup((const gchar *)res->stringval)).get())); break; case XPATH_NODESET: - if(!res->nodesetval->nodeNr) + if(!(res->nodesetval && res->nodesetval->nodeNr)) break; // else[nodeset] fall down to default default: - throw Exception(0, 0, + throw Exception("parser.runtime", &expression, "wrong xmlXPathEvalExpression result type (%d)", res->type); break; // never @@ -611,6 +650,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);