--- parser3/src/classes/xnode.C 2003/09/25 09:15:02 1.59 +++ parser3/src/classes/xnode.C 2004/03/10 10:04:30 1.66 @@ -1,13 +1,13 @@ /** @file Parser: @b dom parser class. - Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #include "classes.h" #ifdef XML -static const char* IDENT_XNODE_C="$Date: 2003/09/25 09:15:02 $"; +static const char * const IDENT_XNODE_C="$Date: 2004/03/10 10:04:30 $"; #include "pa_vmethod_frame.h" @@ -16,6 +16,7 @@ static const char* IDENT_XNODE_C="$Date: #include "pa_vxnode.h" #include "pa_vxdoc.h" #include "pa_vvoid.h" +#include "pa_xml_exception.h" #include "xnode.h" @@ -25,6 +26,7 @@ extern "C" { }; #include "gdome.h" #include "libxml/xpath.h" +#include "libxml/xpathInternals.h" // global variable @@ -110,7 +112,6 @@ GdomeNode* as_node(MethodParams& params, throw Exception("parser.runtime", 0, msg); - result=0; // calm, compiler } return result; @@ -146,7 +147,7 @@ static void _insertBefore(Request& r, Me // write out result r.write_no_lang(*new VXnode(&r.charsets, retNode)); } else - throw Exception(0, exc); + throw XmlException(0, exc); } // Node replaceChild(in Node newChild,in Node oldChild) raises(DOMException); @@ -154,14 +155,14 @@ static void _replaceChild(Request& r, Me VXnode& vnode=GET_SELF(r, VXnode); GdomeNode* selfNode=vnode.get_node(); GdomeNode* newChild=as_node(params, 0, "newChild must be node"); - GdomeNode* refChild=as_node(params, 1, "refChild must be node"); + GdomeNode* oldChild=as_node(params, 1, "oldChild must be node"); GdomeException exc; - if(GdomeNode* retNode=gdome_n_replaceChild(selfNode, newChild, refChild, &exc)) { + if(GdomeNode* retNode=gdome_n_replaceChild(selfNode, newChild, oldChild, &exc)) { // write out result r.write_no_lang(*new VXnode(&r.charsets, retNode)); } else - throw Exception(0, exc); + throw XmlException(0, exc); } // Node removeChild(in Node oldChild) raises(DOMException); @@ -175,7 +176,7 @@ static void _removeChild(Request& r, Met // write out result r.write_no_lang(*new VXnode(&r.charsets, retNode)); } else - throw Exception(0, exc); + throw XmlException(0, exc); } // Node appendChild(in Node newChild) raises(DOMException); @@ -189,7 +190,7 @@ static void _appendChild(Request& r, Met // write out result r.write_no_lang(*new VXnode(&r.charsets, retNode)); } else - throw Exception(0, exc); + throw XmlException(0, exc); } // boolean hasChildNodes(); @@ -232,7 +233,7 @@ GdomeElement* get_self_element(Request& -// DOMString getAttribute(in DOMString name); +/// @bug attribute_value must be freed! [// DOMString getAttribute(in DOMString name); static void _getAttribute(Request& r, MethodParams& params) { GdomeElement* element=get_self_element(r); const String& name=params.as_string(0, "name must be string"); @@ -256,7 +257,7 @@ static void _setAttribute(Request& r, Me r.transcode(attribute_value).use(), &exc); if(exc) - throw Exception(0, exc); + throw XmlException(0, exc); } // void removeAttribute(in DOMString name) raises(DOMException); @@ -267,7 +268,7 @@ static void _removeAttribute(Request& r, GdomeException exc; gdome_el_removeAttribute(element, r.transcode(name).use(), &exc); if(exc) - throw Exception(0, exc); + throw XmlException(0, exc); } // Attr getAttributeNode(in DOMString name); @@ -281,9 +282,7 @@ static void _getAttributeNode(Request& r // write out result r.write_no_lang(*new VXnode(&r.charsets, (GdomeNode* )attr)); } else if(exc) - throw Exception( - 0, - exc); + throw XmlException(0, exc); } // Attr setAttributeNode(in Attr newAttr) raises(DOMException); @@ -296,9 +295,7 @@ static void _setAttributeNode(Request& r // write out result r.write_no_lang(*new VXnode(&r.charsets, (GdomeNode* )returnAttr)); } else - throw Exception( - 0, - exc); + throw XmlException(0, exc); } // Attr removeAttributeNode(in Attr oldAttr) raises(DOMException); @@ -309,9 +306,7 @@ static void _removeAttributeNode(Request GdomeException exc; gdome_el_removeAttributeNode(element, oldAttr, &exc); if(exc) - throw Exception( - 0, - exc); + throw XmlException(0, exc); } // NodeList getElementsByTagName(in DOMString name); @@ -330,9 +325,7 @@ static void _getElementsByTagName(Reques String::Body::Format(i), new VXnode(&r.charsets, gdome_nl_item(nodes, i, &exc))); } else if(exc) - throw Exception( - 0, - exc); + throw XmlException(0, exc); // write out result r.write_no_lang(result); @@ -370,7 +363,7 @@ static void _setAttributeNS(Request& r, r.transcode(attribute_value).use(), &exc); if(exc) - throw Exception(0, exc); + throw XmlException(0, exc); } // void removeAttributeNS(in DOMString namespaceURI, in DOMString localName) raises(DOMException); @@ -385,7 +378,7 @@ static void _removeAttributeNS(Request& r.transcode(localName).use(), &exc); if(exc) - throw Exception(0, exc); + throw XmlException(0, exc); } // Attr getAttributeNodeNS(in DOMString namespaceURI, in DOMString localName); @@ -400,9 +393,7 @@ static void _getAttributeNodeNS(Request& // write out result r.write_no_lang(*new VXnode(&r.charsets, (GdomeNode* )attr)); } else if(exc) - throw Exception( - 0, - exc); + throw XmlException(0, exc); } // Attr setAttributeNodeNS(in Attr newAttr) raises(DOMException); @@ -415,9 +406,7 @@ static void _setAttributeNodeNS(Request& // write out result r.write_no_lang(*new VXnode(&r.charsets, (GdomeNode* )returnAttr)); } else - throw Exception( - 0, - exc); + throw XmlException(0, exc); } // boolean hasAttribute(in DOMString name) raises(DOMException); @@ -484,11 +473,28 @@ static void _normalize(Request& r, Metho GdomeException exc; gdome_n_normalize(selfNode, &exc); if(exc) - throw Exception( - 0, - exc); + throw XmlException(0, exc); } +#ifndef DOXYGEN +struct Register_one_ns_info { + Request* r; + xmlXPathContextPtr ctxt; +}; +#endif +static void register_one_ns( + HashStringValue::key_type key, + HashStringValue::value_type value, + Register_one_ns_info* info) { + if(const String* svalue=value->get_string()) + xmlXPathRegisterNs(info->ctxt, + BAD_CAST info->r->transcode(key)->str, + BAD_CAST info->r->transcode(*svalue)->str); + else + throw Exception("parser.runtime", + new String(key, String::L_TAINTED), + "value is %s, must be string or number", value->type()); +} static void _selectX(Request& r, MethodParams& params, void (*handler)(Request& r, const String& expression, @@ -498,6 +504,15 @@ static void _selectX(Request& r, MethodP // expression const String& expression=params.as_string(0, "expression must be string"); + HashStringValue* namespaces=0; + if(params.count()>1) { + Value* vnamespaces=params.get(1); + namespaces=vnamespaces->get_hash(); + if(!namespaces && !vnamespaces->is_string()) + throw Exception("parser.runtime", + 0, + "namespaces parameter must be hash"); + } GdomeException exc; GdomeNode* dome_node=vnode.get_node(); @@ -506,6 +521,10 @@ static void _selectX(Request& r, MethodP 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)); + if(namespaces) { + Register_one_ns_info info={&r, ctxt.get()}; + namespaces->for_each(register_one_ns, &info); + } ctxt->node=gdome_xml_n_get_xmlNode(dome_node); /*error to stderr for now*/ xmlXPathObject_auto_ptr res( @@ -513,7 +532,7 @@ static void _selectX(Request& r, MethodP if(xmlHaveGenericErrors()) { GdomeException exc=0; - throw Exception(&expression, exc); + throw XmlException(&expression, exc); } Value* result=0; @@ -588,7 +607,7 @@ static void selectNodeHandler(Request& r } } -static void selectBoolHandler(Request& r, +static void selectBoolHandler(Request&, const String& expression, xmlXPathObject_auto_ptr res, Value*& result) { @@ -608,7 +627,7 @@ static void selectBoolHandler(Request& r } } -static void selectNumberHandler(Request& r, +static void selectNumberHandler(Request&, const String& expression, xmlXPathObject_auto_ptr res, Value*& result) { @@ -736,16 +755,16 @@ MXnode::MXnode(const char* aname, VState /// parser // ^node.select[/some/xpath/query] = hash $.#[dnode] - add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1); + add_native_method("select", Method::CT_DYNAMIC, _select, 1, 2); // ^node.selectSingle[/some/xpath/query] = first node [if any] - add_native_method("selectSingle", Method::CT_DYNAMIC, _selectSingle, 1, 1); + add_native_method("selectSingle", Method::CT_DYNAMIC, _selectSingle, 1, 2); // ^node.selectBool[/some/xpath/query] = bool value [if any] - add_native_method("selectBool", Method::CT_DYNAMIC, _selectBool, 1, 1); + add_native_method("selectBool", Method::CT_DYNAMIC, _selectBool, 1, 2); // ^node.selectNumber[/some/xpath/query] = double value [if any] - add_native_method("selectNumber", Method::CT_DYNAMIC, _selectNumber, 1, 1); + add_native_method("selectNumber", Method::CT_DYNAMIC, _selectNumber, 1, 2); // ^node.selectString[/some/xpath/query] = strinv value [if any] - add_native_method("selectString", Method::CT_DYNAMIC, _selectString, 1, 1); + add_native_method("selectString", Method::CT_DYNAMIC, _selectString, 1, 2); // consts