|
|
| version 1.8, 2001/10/16 14:44:40 | version 1.35, 2002/01/28 10:50:03 |
|---|---|
| Line 2 | Line 2 |
| Parser: @b dom parser class. | Parser: @b dom parser class. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru) |
| $Id$ | $Id$ |
| */ | */ |
| #include "classes.h" | #include "classes.h" |
| #ifdef XML | #ifdef XML |
| #include "pa_charset.h" | |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_vxnode.h" | #include "pa_vxnode.h" |
| #include "pa_vxdoc.h" | |
| #include "pa_vvoid.h" | |
| #include "xnode.h" | #include "xnode.h" |
| #include <util/XMLString.hpp> | extern "C" { |
| #include <XalanSourceTree/XalanSourceTreeDOMSupport.hpp> | #include "gdomecore/gdome-xml-node.h" |
| #include <XPath/XPathEvaluator.hpp> | #include "gdomecore/gdome-xml-document.h" |
| #include <XPath/NodeRefList.hpp> | }; |
| #include <XalanDOM/XalanElement.hpp> | #include "gdome.h" |
| #include <XalanDOM/XalanAttr.hpp> | #include "libxml/xpath.h" |
| #include <XalanDOM/XalanNodeList.hpp> | |
| // defines | // defines |
| #define XNODE_CLASS_NAME "xnode" | #define XNODE_CLASS_NAME "xnode" |
| // classes | |
| class xmlXPathObject_auto_ptr { | |
| public: | |
| 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) | |
| {if (this != &_Y) | |
| {if (_Ptr != _Y.get()) | |
| {if (_Owns && _Ptr) | |
| xmlXPathFreeObject(_Ptr); | |
| _Owns = _Y._Owns; } | |
| else if (_Y._Owns) | |
| _Owns = true; | |
| _Ptr = _Y.release(); } | |
| return (*this); } | |
| ~xmlXPathObject_auto_ptr() | |
| {if (_Owns && _Ptr) | |
| xmlXPathFreeObject(_Ptr); } | |
| xmlXPathObject& operator*() const | |
| {return (*get()); } | |
| xmlXPathObject *operator->() const | |
| {return (get()); } | |
| xmlXPathObject *get() const | |
| {return (_Ptr); } | |
| xmlXPathObject *release() const | |
| {((xmlXPathObject_auto_ptr *)this)->_Owns = false; | |
| return (_Ptr); } | |
| private: | |
| bool _Owns; | |
| xmlXPathObject *_Ptr; | |
| }; | |
| class xmlXPathContext_auto_ptr { | |
| public: | |
| 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) | |
| {if (this != &_Y) | |
| {if (_Ptr != _Y.get()) | |
| {if (_Owns && _Ptr) | |
| xmlXPathFreeContext(_Ptr); | |
| _Owns = _Y._Owns; } | |
| else if (_Y._Owns) | |
| _Owns = true; | |
| _Ptr = _Y.release(); } | |
| return (*this); } | |
| ~xmlXPathContext_auto_ptr() | |
| {if (_Owns && _Ptr) | |
| xmlXPathFreeContext(_Ptr); } | |
| xmlXPathContext& operator*() const | |
| {return (*get()); } | |
| xmlXPathContext *operator->() const | |
| {return (get()); } | |
| xmlXPathContext *get() const | |
| {return (_Ptr); } | |
| xmlXPathContext *release() const | |
| {((xmlXPathContext_auto_ptr *)this)->_Owns = false; | |
| return (_Ptr); } | |
| private: | |
| bool _Owns; | |
| xmlXPathContext *_Ptr; | |
| }; | |
| // helpers | // helpers |
| XalanNode& as_node(Pool& pool, const String& method_name, MethodParams *params, | GdomeNode *as_node(Pool& pool, const String& method_name, MethodParams *params, |
| int index, const char *msg) { | int index, const char *msg) { |
| Value& value=params->as_no_junction(index, msg); | Value& value=params->as_no_junction(index, msg); |
| if(strcmp(value.type(), VXNODE_TYPE)!=0) | if(strcmp(value.type(), VXNODE_TYPE)!=0 |
| PTHROW(0, 0, | || strcmp(value.type(), VXDOC_TYPE)!=0) |
| throw Exception(0, 0, | |
| &method_name, | &method_name, |
| msg); | msg); |
| VXnode& vnode=*static_cast<VXnode *>(&value); | VXnode& vnode=*static_cast<VXnode *>(&value); |
| return vnode.get_node(pool, &method_name); | return vnode.get_node(&method_name); |
| } | } |
| // helpers | // helpers |
| XalanAttr& as_attr(Pool& pool, const String& method_name, MethodParams *params, | GdomeAttr * as_attr(Pool& pool, const String& method_name, MethodParams *params, |
| int index, const char *msg) { | int index, const char *msg) { |
| XalanNode& node=as_node(pool, method_name, params, index, msg); | GdomeNode *node=as_node(pool, method_name, params, index, msg); |
| GdomeException exc; | |
| if(node.getNodeType()!=XalanNode::ATTRIBUTE_NODE) | if(gdome_n_nodeType(node, &exc)!=GDOME_ATTRIBUTE_NODE) |
| PTHROW(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| msg); | msg); |
| return *reinterpret_cast<XalanAttr *>(&node); | return GDOME_A(node); |
| } | } |
| // methods | // methods |
| Line 62 XalanAttr& as_attr(Pool& pool, const Str | Line 133 XalanAttr& as_attr(Pool& pool, const Str |
| static void _insertBefore(Request& r, const String& method_name, MethodParams *params) { | static void _insertBefore(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *selfNode=vnode.get_node(&method_name); |
| GdomeNode *newChild=as_node(pool, method_name, params, 0, "newChild must be node"); | |
| XalanNode& newChild=as_node(pool, method_name, params, 0, "newChild must be node"); | GdomeNode *refChild=as_node(pool, method_name, params, 1, "refChild must be node"); |
| XalanNode& refChild=as_node(pool, method_name, params, 1, "refChild must be node"); | |
| try { | GdomeException exc; |
| node.insertBefore(&newChild, &refChild); | if(GdomeNode *retNode=gdome_n_insertBefore(selfNode, newChild, refChild, &exc)) { |
| } catch(const XalanDOMException& e) { | // write out result |
| pool.exception()._throw(pool, &method_name, e); | VXnode& result=*new(pool) VXnode(pool, retNode); |
| } | r.write_no_lang(result); |
| } else | |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| } | } |
| // Node replaceChild(in Node newChild,in Node oldChild) raises(DOMException); | // Node replaceChild(in Node newChild,in Node oldChild) raises(DOMException); |
| static void _replaceChild(Request& r, const String& method_name, MethodParams *params) { | static void _replaceChild(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *selfNode=vnode.get_node(&method_name); |
| GdomeNode *newChild=as_node(pool, method_name, params, 0, "newChild must be node"); | |
| XalanNode& newChild=as_node(pool, method_name, params, 0, "newChild must be node"); | GdomeNode *refChild=as_node(pool, method_name, params, 1, "refChild must be node"); |
| XalanNode& refChild=as_node(pool, method_name, params, 1, "refChild must be node"); | |
| try { | GdomeException exc; |
| node.replaceChild(&newChild, &refChild); | if(GdomeNode *retNode=gdome_n_replaceChild(selfNode, newChild, refChild, &exc)) { |
| } catch(const XalanDOMException& e) { | // write out result |
| pool.exception()._throw(pool, &method_name, e); | r.write_no_lang(*new(pool) VXnode(pool, retNode)); |
| } | } else |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| } | } |
| // Node removeChild(in Node oldChild) raises(DOMException); | // Node removeChild(in Node oldChild) raises(DOMException); |
| static void _removeChild(Request& r, const String& method_name, MethodParams *params) { | static void _removeChild(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *selfNode=vnode.get_node(&method_name); |
| GdomeNode *oldChild=as_node(pool, method_name, params, 0, "oldChild must be node"); | |
| XalanNode& oldChild=as_node(pool, method_name, params, 0, "oldChild must be node"); | |
| try { | GdomeException exc; |
| node.removeChild(&oldChild); | if(GdomeNode *retNode=gdome_n_removeChild(selfNode, oldChild, &exc)) { |
| } catch(const XalanDOMException& e) { | // write out result |
| pool.exception()._throw(pool, &method_name, e); | r.write_no_lang(*new(pool) VXnode(pool, retNode)); |
| } | } else |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| } | } |
| // Node appendChild(in Node newChild) raises(DOMException); | // Node appendChild(in Node newChild) raises(DOMException); |
| static void _appendChild(Request& r, const String& method_name, MethodParams *params) { | static void _appendChild(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *selfNode=vnode.get_node(&method_name); |
| GdomeNode *newChild=as_node(pool, method_name, params, 0, "newChild must be node"); | |
| XalanNode& newChild=as_node(pool, method_name, params, 0, "newChild must be node"); | |
| try { | GdomeException exc; |
| node.appendChild(&newChild); | if(GdomeNode *retNode=gdome_n_appendChild(selfNode, newChild, &exc)) { |
| } catch(const XalanDOMException& e) { | // write out result |
| pool.exception()._throw(pool, &method_name, e); | r.write_no_lang(*new(pool) VXnode(pool, retNode)); |
| } | } else |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| } | } |
| // boolean hasChildNodes(); | // boolean hasChildNodes(); |
| static void _hasChildNodes(Request& r, const String& method_name, MethodParams *) { | static void _hasChildNodes(Request& r, const String& method_name, MethodParams *) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *node=vnode.get_node(&method_name); |
| GdomeException exc; | |
| // write out result | // write out result |
| VBool& result=*new(pool) VBool(pool, node.hasChildNodes()); | VBool& result=*new(pool) VBool(pool, gdome_n_hasChildNodes(node, &exc)!=0); |
| result.set_name(method_name); | result.set_name(method_name); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| // Node cloneNode(in boolean deep); | // Node cloneNode(in boolean deep); |
| /// @test 1. who frees it up? 2. ownerDocument=? | |
| static void _cloneNode(Request& r, const String& method_name, MethodParams *params) { | static void _cloneNode(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *node=vnode.get_node(&method_name); |
| bool deep=params->as_bool(0, "deep must be bool", r); | bool deep=params->as_bool(0, "deep must be bool", r); |
| GdomeException exc; | |
| // write out result | // write out result |
| VXnode& result=*new(pool) VXnode(pool, node.cloneNode(deep)); | VXnode& result=*new(pool) VXnode(pool, gdome_n_cloneNode(node, deep, &exc)); |
| result.set_name(method_name); | result.set_name(method_name); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } |
| // DOM1 element | // DOM1 element |
| XalanElement& get_self_element(Request& r, const String& method_name) { | GdomeElement *get_self_element(Request& r, const String& method_name) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| XalanNode& node=vnode.get_node(pool, &method_name); | GdomeNode *node=vnode.get_node(&method_name); |
| if(node.getNodeType()!=XalanNode::ELEMENT_NODE) | GdomeException exc; |
| PTHROW(0, 0, | if(gdome_n_nodeType(node, &exc)!=GDOME_ELEMENT_NODE) |
| throw Exception(0, 0, | |
| &method_name, | &method_name, |
| "method can be called on node of ELEMENT type"); | "method can be called on node of ELEMENT type"); |
| return *reinterpret_cast<XalanElement *>(&node); | return GDOME_EL(node); |
| } | } |
| // DOMString getAttribute(in DOMString name); | // DOMString getAttribute(in DOMString name); |
| static void _getAttribute(Request& r, const String& method_name, MethodParams *params) { | static void _getAttribute(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS); | const String& name=params->as_string(0, "name must be string"); |
| const XalanDOMString& attribute_value=element.getAttribute(XalanDOMString(name)); | GdomeException exc; |
| GdomeDOMString *attribute_value= | |
| gdome_el_getAttribute(element, pool.transcode(name).get(), &exc); | |
| // write out result | // write out result |
| r.write_no_lang(*new(pool) VString(pool.transcode(attribute_value))); | r.write_no_lang(*new(pool) VString(pool.transcode(attribute_value))); |
| } | } |
| Line 176 static void _getAttribute(Request& r, co | Line 260 static void _getAttribute(Request& r, co |
| // void setAttribute(in DOMString name, in DOMString value) raises(DOMException); | // void setAttribute(in DOMString name, in DOMString value) raises(DOMException); |
| static void _setAttribute(Request& r, const String& method_name, MethodParams *params) { | static void _setAttribute(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS); | const String& name=params->as_string(0, "name must be string"); |
| const char *attribute_value=params->as_string(1, "value must not be code").cstr(String::UL_AS_IS); | const String& attribute_value=params->as_string(1, "value must be string"); |
| try { | GdomeException exc; |
| element.setAttribute( | gdome_el_setAttribute(element, |
| XalanDOMString(name), | pool.transcode(name).get(), |
| XalanDOMString(attribute_value)); | pool.transcode(attribute_value).get(), |
| } catch(const XalanDOMException& e) { | &exc); |
| pool.exception()._throw(pool, &method_name, e); | if(exc) |
| } | throw Exception(0, 0, |
| &method_name, | |
| exc); | |
| } | } |
| // void removeAttribute(in DOMString name) raises(DOMException); | // void removeAttribute(in DOMString name) raises(DOMException); |
| static void _removeAttribute(Request& r, const String& method_name, MethodParams *params) { | static void _removeAttribute(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS); | const String& name=params->as_string(0, "name must be string"); |
| try { | GdomeException exc; |
| element.removeAttribute(XalanDOMString(name)); | gdome_el_removeAttribute(element, pool.transcode(name).get(), &exc); |
| } catch(const XalanDOMException& e) { | if(exc) |
| pool.exception()._throw(pool, &method_name, e); | throw Exception(0, 0, |
| } | &method_name, |
| exc); | |
| } | } |
| // Attr getAttributeNode(in DOMString name); | // Attr getAttributeNode(in DOMString name); |
| static void _getAttributeNode(Request& r, const String& method_name, MethodParams *params) { | static void _getAttributeNode(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS); | const String& name=params->as_string(0, "name must be string"); |
| if(XalanAttr *attr=element.getAttributeNode(XalanDOMString(name))) { | GdomeException exc; |
| if(GdomeAttr *attr=gdome_el_getAttributeNode(element, pool.transcode(name).get(), &exc)) { | |
| // write out result | // write out result |
| VXnode& result=*new(pool) VXnode(pool, attr); | VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)attr); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } else if(exc) |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| } | } |
| // Attr setAttributeNode(in Attr newAttr) raises(DOMException); | // Attr setAttributeNode(in Attr newAttr) raises(DOMException); |
| static void _setAttributeNode(Request& r, const String& method_name, MethodParams *params) { | static void _setAttributeNode(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| XalanAttr& newAttr=as_attr(pool, method_name, params, 0, "newAttr must be ATTRIBUTE node"); | GdomeAttr * newAttr=as_attr(pool, method_name, params, 0, "newAttr must be ATTRIBUTE node"); |
| try { | GdomeException exc; |
| if(XalanAttr *returnAttr=element.setAttributeNode(&newAttr)) { | if(GdomeAttr *returnAttr=gdome_el_setAttributeNode(element, newAttr, &exc)) { |
| // write out result | // write out result |
| VXnode& result=*new(pool) VXnode(pool, returnAttr); | VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)returnAttr); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| } | } else |
| } catch(const XalanDOMException& e) { | throw Exception(0, 0, |
| pool.exception()._throw(pool, &method_name, e); | &method_name, |
| } | exc); |
| } | } |
| // Attr removeAttributeNode(in Attr oldAttr) raises(DOMException); | // Attr removeAttributeNode(in Attr oldAttr) raises(DOMException); |
| static void _removeAttributeNode(Request& r, const String& method_name, MethodParams *params) { | static void _removeAttributeNode(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| XalanAttr& oldAttr=as_attr(pool, method_name, params, 0, "oldAttr must be ATTRIBUTE node"); | GdomeAttr * oldAttr=as_attr(pool, method_name, params, 0, "oldAttr must be ATTRIBUTE node"); |
| try { | GdomeException exc; |
| /*XalanAttr *returnAttr*/element.removeAttributeNode(&oldAttr); | gdome_el_removeAttributeNode(element, oldAttr, &exc); |
| } catch(const XalanDOMException& e) { | if(exc) |
| pool.exception()._throw(pool, &method_name, e); | throw Exception(0, 0, |
| } | &method_name, |
| exc); | |
| } | } |
| // NodeList getElementsByTagName(in DOMString name); | // NodeList getElementsByTagName(in DOMString name); |
| static void _getElementsByTagName(Request& r, const String& method_name, MethodParams *params) { | static void _getElementsByTagName(Request& r, const String& method_name, MethodParams *params) { |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| XalanElement& element=get_self_element(r, method_name); | GdomeElement *element=get_self_element(r, method_name); |
| const char *name=params->as_string(0, "name must not be code").cstr(String::UL_AS_IS); | const String& name=params->as_string(0, "name must be string"); |
| VHash& result=*new(pool) VHash(pool); | VHash& result=*new(pool) VHash(pool); |
| if(const XalanNodeList *nodes= | GdomeException exc; |
| element.getElementsByTagName(XalanDOMString(name))) { | if(GdomeNodeList *nodes= |
| for(int i=0; i<nodes->getLength(); i++) { | gdome_el_getElementsByTagName(element, pool.transcode(name).get(), &exc)) { |
| gulong length=gdome_nl_length(nodes, &exc); | |
| for(gulong i=0; i<length; i++) { | |
| String& skey=*new(pool) String(pool); | String& skey=*new(pool) String(pool); |
| { | { |
| char *buf=(char *)pool.malloc(MAX_NUMBER); | char *buf=(char *)pool.malloc(MAX_NUMBER); |
| Line 263 static void _getElementsByTagName(Reques | Line 357 static void _getElementsByTagName(Reques |
| skey << buf; | skey << buf; |
| } | } |
| result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i))); | result.hash(0).put(skey, new(pool) VXnode(pool, gdome_nl_item(nodes, i, &exc))); |
| } | } |
| } | } else if(exc) |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| // write out result | // write out result |
| r.write_no_lang(result); | r.write_no_lang(result); |
| Line 273 static void _getElementsByTagName(Reques | Line 370 static void _getElementsByTagName(Reques |
| // void normalize(); | // void normalize(); |
| static void _normalize(Request& r, const String& method_name, MethodParams *) { | static void _normalize(Request& r, const String& method_name, MethodParams *) { |
| XalanElement& element=get_self_element(r, method_name); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | |
| GdomeNode *selfNode=vnode.get_node(&method_name); | |
| element.normalize(); | GdomeException exc; |
| gdome_n_normalize(selfNode, &exc); | |
| if(exc) | |
| throw Exception(0, 0, | |
| &method_name, | |
| exc); | |
| } | } |
| static void _selectX(Request& r, const String& method_name, MethodParams *params, | |
| static void _select(Request& r, const String& method_name, MethodParams *params) { | void (*handler)(Pool& pool, |
| const String& expression, | |
| xmlXPathObject_auto_ptr res, | |
| Value *& result)) { | |
| // _asm int 3; | // _asm int 3; |
| Pool& pool=r.pool(); | Pool& pool=r.pool(); |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | VXnode& vnode=*static_cast<VXnode *>(r.self); |
| // expression | // expression |
| const String& expression=params->as_string(0, "expression must not be code"); | const String& expression=params->as_string(0, "expression must be string"); |
| const char *expression_cstr=expression.cstr(String::UL_AS_IS); | |
| XalanDOMString dstring(expression_cstr); | |
| const XalanDOMChar *expression_dcstr=dstring.c_str(); | |
| XPathEvaluator evaluator; | |
| // We'll use these to parse the XML file. | |
| XalanSourceTreeDOMSupport dom_support; | |
| try { | |
| NodeRefList list=evaluator.selectNodeList(dom_support, | |
| &vnode.get_node(pool, &method_name), | |
| expression_dcstr); | |
| VHash& result=*new(pool) VHash(pool); | GdomeException exc; |
| for(int i=0; i<list.getLength(); i++) { | GdomeNode *dome_node=vnode.get_node(&method_name); |
| String& skey=*new(pool) String(pool); | GdomeDocument *dome_document=gdome_n_ownerDocument(dome_node, &exc); |
| { | if(!dome_document) // document does not own itself, so ownerDocument = 0 |
| char *buf=(char *)pool.malloc(MAX_NUMBER); | dome_document=GDOME_DOC(dome_node); // and we need downcast |
| snprintf(buf, MAX_NUMBER, "%d", i); | xmlDoc *xml_document=gdome_xml_doc_get_xmlDoc(dome_document); |
| skey << buf; | xmlXPathContext_auto_ptr ctxt(xmlXPathNewContext(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); | |
| if(result) { | |
| result->set_name(method_name); | |
| r.write_no_lang(*result); | |
| } | |
| } | |
| static void selectNodesHandler(Pool& pool, | |
| 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) { | |
| Hash& hash=vhash->hash(0); | |
| for(int i=0; i<size; i++) { | |
| String& skey=*new(pool) String(pool); | |
| { | |
| char *buf=(char *)pool.malloc(MAX_NUMBER); | |
| snprintf(buf, MAX_NUMBER, "%d", i); | |
| skey << buf; | |
| } | |
| result.hash().put(skey, new(pool) VXnode(pool, list.item(i))); | hash.put(skey, new(pool) VXnode(pool, |
| gdome_xml_n_mkref(res->nodesetval->nodeTab[i]))); | |
| } | |
| } | } |
| result.set_name(method_name); | break; |
| r.write_no_lang(result); | default: |
| } catch(const XSLException& e) { | throw Exception(0, 0, |
| pool.exception()._throw(pool, &expression, e); | &expression, |
| "wrong xmlXPathEvalExpression result type (%d)", res->type); | |
| break; // never | |
| } | } |
| } | } |
| static void _select_single(Request& r, const String& method_name, MethodParams *params) { | static void selectNodeHandler(Pool& pool, |
| // _asm int 3; | const String& expression, |
| Pool& pool=r.pool(); | xmlXPathObject_auto_ptr res, |
| VXnode& vnode=*static_cast<VXnode *>(r.self); | Value *& result) { |
| switch(res->type) { | |
| // expression | case XPATH_UNDEFINED: |
| const String& expression=params->as_string(0, "expression must not be code"); | break; |
| const char *expression_cstr=expression.cstr(String::UL_AS_IS); | case XPATH_NODESET: |
| XalanDOMString dstring(expression_cstr); | if(res->nodesetval->nodeNr) { // empty result strangly has NODESET res->type |
| const XalanDOMChar *expression_dcstr=dstring.c_str(); | if(res->nodesetval->nodeNr>1) |
| throw Exception(0, 0, | |
| XPathEvaluator evaluator; | &expression, |
| // Initialize the XalanSourceTree subsystem... | "resulted not in a single node (%d)", res->nodesetval->nodeNr); |
| // XalanSourceTreeInit theSourceTreeInit; | |
| // We'll use these to parse the XML file. | result=new(pool) VXnode(pool, gdome_xml_n_mkref(res->nodesetval->nodeTab[0])); |
| XalanSourceTreeDOMSupport dom_support; | |
| try { | |
| if(XalanNode *node=evaluator.selectSingleNode(dom_support, | |
| &vnode.get_node(pool, &method_name), | |
| expression_dcstr)) { | |
| VXnode& result=*new(pool) VXnode(pool, node); | |
| result.set_name(method_name); | |
| r.write_no_lang(result); | |
| } | } |
| } catch(const XSLException& e) { | break; |
| pool.exception()._throw(pool, &expression, e); | case XPATH_BOOLEAN: |
| result=new(pool) VBool(pool, res->boolval!=0); | |
| break; | |
| case XPATH_NUMBER: | |
| result=new(pool) VDouble(pool, res->floatval); | |
| break; | |
| case XPATH_STRING: | |
| result=new(pool) VString( | |
| pool.transcode( | |
| GdomeDOMString_auto_ptr( | |
| gdome_str_mkref_dup((const gchar *)res->stringval)).get())); | |
| break; | |
| default: | |
| throw Exception(0, 0, | |
| &expression, | |
| "wrong xmlXPathEvalExpression result type (%d)", res->type); | |
| break; // never | |
| } | |
| } | |
| static void selectBoolHandler(Pool& pool, | |
| const String& expression, | |
| xmlXPathObject_auto_ptr res, | |
| Value *& result) { | |
| switch(res->type) { | |
| case XPATH_BOOLEAN: | |
| result=new(pool) VBool(pool, res->boolval!=0); | |
| break; | |
| case XPATH_NODESET: | |
| if(!res->nodesetval->nodeNr) | |
| break; | |
| // else[nodeset] fall down to default | |
| default: | |
| throw Exception(0, 0, | |
| &expression, | |
| "wrong xmlXPathEvalExpression result type (%d)", res->type); | |
| break; // never | |
| } | |
| } | |
| static void selectNumberHandler(Pool& pool, | |
| const String& expression, | |
| xmlXPathObject_auto_ptr res, | |
| Value *& result) { | |
| switch(res->type) { | |
| case XPATH_NUMBER: | |
| result=new(pool) VDouble(pool, res->floatval); | |
| break; | |
| case XPATH_NODESET: | |
| if(!res->nodesetval->nodeNr) | |
| break; | |
| // else[nodeset] fall down to default | |
| default: | |
| throw Exception(0, 0, | |
| &expression, | |
| "wrong xmlXPathEvalExpression result type (%d)", res->type); | |
| break; // never | |
| } | |
| } | |
| static void selectStringHandler(Pool& pool, | |
| const String& expression, | |
| xmlXPathObject_auto_ptr res, | |
| Value *& result) { | |
| switch(res->type) { | |
| case XPATH_UNDEFINED: | |
| break; | |
| case XPATH_STRING: | |
| result=new(pool) VString( | |
| pool.transcode( | |
| GdomeDOMString_auto_ptr( | |
| gdome_str_mkref_dup((const gchar *)res->stringval)).get())); | |
| break; | |
| case XPATH_NODESET: | |
| if(!res->nodesetval->nodeNr) | |
| break; | |
| // else[nodeset] fall down to default | |
| default: | |
| throw Exception(0, 0, | |
| &expression, | |
| "wrong xmlXPathEvalExpression result type (%d)", res->type); | |
| break; // never | |
| } | } |
| } | } |
| static void _select(Request& r, const String& method_name, MethodParams *params) { | |
| _selectX(r, method_name, params, | |
| selectNodesHandler); | |
| } | |
| static void _selectSingle(Request& r, const String& method_name, MethodParams *params) { | |
| _selectX(r, method_name, params, | |
| selectNodeHandler); | |
| } | |
| static void _selectBool(Request& r, const String& method_name, MethodParams *params) { | |
| _selectX(r, method_name, params, | |
| selectBoolHandler); | |
| } | |
| static void _selectNumber(Request& r, const String& method_name, MethodParams *params) { | |
| _selectX(r, method_name, params, | |
| selectNumberHandler); | |
| } | |
| static void _selectString(Request& r, const String& method_name, MethodParams *params) { | |
| _selectX(r, method_name, params, | |
| selectStringHandler); | |
| } | |
| // constructor | // constructor |
| MXnode::MXnode(Pool& apool) : Methoded(apool), | MXnode::MXnode(Pool& apool) : Methoded(apool), |
| Line 389 MXnode::MXnode(Pool& apool) : Methoded(a | Line 622 MXnode::MXnode(Pool& apool) : Methoded(a |
| add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0); | add_native_method("normalize", Method::CT_DYNAMIC, _normalize, 0, 0); |
| /// parser | /// parser |
| // ^node.select[/some/xpath/query] = hash $.#[dnode] | // ^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, 1); |
| // ^node.selectSingle[/some/xpath/query] = first dnode | // ^node.selectSingle[/some/xpath/query] = first node [if any] |
| add_native_method("selectSingle", Method::CT_DYNAMIC, _select_single, 1, 1); | add_native_method("selectSingle", Method::CT_DYNAMIC, _selectSingle, 1, 1); |
| // ^node.selectBool[/some/xpath/query] = bool value [if any] | |
| add_native_method("selectBool", Method::CT_DYNAMIC, _selectBool, 1, 1); | |
| // ^node.selectNumber[/some/xpath/query] = double value [if any] | |
| add_native_method("selectNumber", Method::CT_DYNAMIC, _selectNumber, 1, 1); | |
| // ^node.selectString[/some/xpath/query] = strinv value [if any] | |
| add_native_method("selectString", Method::CT_DYNAMIC, _selectString, 1, 1); | |
| // consts | // consts |
| #define CONST(name, value) \ | #define CONST(name) \ |
| consts.put(*new(pool()) String(pool(), #name), new(pool()) VInt(pool(), value)) | consts.put(*new(pool()) String(pool(), #name), new(pool()) VInt(pool(), GDOME_##name)) |
| CONST(ELEMENT_NODE, 1); | CONST(ELEMENT_NODE); |
| CONST(ATTRIBUTE_NODE, 2); | CONST(ATTRIBUTE_NODE); |
| CONST(TEXT_NODE, 3); | CONST(TEXT_NODE); |
| CONST(CDATA_SECTION_NODE, 4); | CONST(CDATA_SECTION_NODE); |
| CONST(ENTITY_REFERENCE_NODE, 5); | CONST(ENTITY_REFERENCE_NODE); |
| CONST(ENTITY_NODE, 6); | CONST(ENTITY_NODE); |
| CONST(PROCESSING_INSTRUCTION_NODE, 7); | CONST(PROCESSING_INSTRUCTION_NODE); |
| CONST(COMMENT_NODE, 8); | CONST(COMMENT_NODE); |
| CONST(DOCUMENT_NODE, 9); | CONST(DOCUMENT_NODE); |
| CONST(DOCUMENT_TYPE_NODE, 10); | CONST(DOCUMENT_TYPE_NODE); |
| CONST(DOCUMENT_FRAGMENT_NODE, 11); | CONST(DOCUMENT_FRAGMENT_NODE); |
| CONST(NOTATION_NODE, 12); | CONST(NOTATION_NODE); |
| } | } |