|
|
| version 1.19, 2001/12/28 14:06:50 | version 1.31, 2002/01/24 13:57:19 |
|---|---|
| Line 9 | Line 9 |
| #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_vvoid.h" | |
| #include "xnode.h" | #include "xnode.h" |
| extern "C" { | |
| #include "gdomecore/gdome-xml-node.h" | |
| #include "gdomecore/gdome-xml-document.h" | |
| }; | |
| #include "gdome.h" | #include "gdome.h" |
| #include "libxml/xpath.h" | |
| // 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; | |
| }; | |
| // defines | // defines |
| Line 176 static void _getAttribute(Request& r, co | Line 251 static void _getAttribute(Request& r, co |
| GdomeException exc; | GdomeException exc; |
| GdomeDOMString *attribute_value= | GdomeDOMString *attribute_value= |
| gdome_el_getAttribute(element, pool.transcode(name), &exc); | 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 190 static void _setAttribute(Request& r, co | Line 265 static void _setAttribute(Request& r, co |
| GdomeException exc; | GdomeException exc; |
| gdome_el_setAttribute(element, | gdome_el_setAttribute(element, |
| pool.transcode(name), | pool.transcode(name).get(), |
| pool.transcode(attribute_value), | pool.transcode(attribute_value).get(), |
| &exc); | &exc); |
| if(exc) | if(exc) |
| throw Exception(0, 0, | throw Exception(0, 0, |
| Line 206 static void _removeAttribute(Request& r, | Line 281 static void _removeAttribute(Request& r, |
| const String& name=params->as_string(0, "name must be string"); | const String& name=params->as_string(0, "name must be string"); |
| GdomeException exc; | GdomeException exc; |
| gdome_el_removeAttribute(element, pool.transcode(name), &exc); | gdome_el_removeAttribute(element, pool.transcode(name).get(), &exc); |
| if(exc) | if(exc) |
| throw Exception(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| Line 220 static void _getAttributeNode(Request& r | Line 295 static void _getAttributeNode(Request& r |
| const String& name=params->as_string(0, "name must be string"); | const String& name=params->as_string(0, "name must be string"); |
| GdomeException exc; | GdomeException exc; |
| if(GdomeAttr *attr=gdome_el_getAttributeNode(element, pool.transcode(name), &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, (GdomeNode *)attr); | VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)attr); |
| r.write_no_lang(result); | r.write_no_lang(result); |
| Line 257 static void _removeAttributeNode(Request | Line 332 static void _removeAttributeNode(Request |
| gdome_el_removeAttributeNode(element, oldAttr, &exc); | gdome_el_removeAttributeNode(element, oldAttr, &exc); |
| if(exc) | if(exc) |
| throw Exception(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| exc); | exc); |
| } | } |
| // NodeList getElementsByTagName(in DOMString name); | // NodeList getElementsByTagName(in DOMString name); |
| Line 271 static void _getElementsByTagName(Reques | Line 346 static void _getElementsByTagName(Reques |
| VHash& result=*new(pool) VHash(pool); | VHash& result=*new(pool) VHash(pool); |
| GdomeException exc; | GdomeException exc; |
| if(GdomeNodeList *nodes= | if(GdomeNodeList *nodes= |
| gdome_el_getElementsByTagName(element, pool.transcode(name), &exc)) { | gdome_el_getElementsByTagName(element, pool.transcode(name).get(), &exc)) { |
| gulong length=gdome_nl_length(nodes, &exc); | gulong length=gdome_nl_length(nodes, &exc); |
| for(gulong i=0; i<length; i++) { | for(gulong i=0; i<length; i++) { |
| String& skey=*new(pool) String(pool); | String& skey=*new(pool) String(pool); |
| Line 285 static void _getElementsByTagName(Reques | Line 360 static void _getElementsByTagName(Reques |
| } | } |
| } else if(exc) | } else if(exc) |
| throw Exception(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| exc); | exc); |
| // write out result | // write out result |
| r.write_no_lang(result); | r.write_no_lang(result); |
| Line 302 static void _normalize(Request& r, const | Line 377 static void _normalize(Request& r, const |
| gdome_n_normalize(selfNode, &exc); | gdome_n_normalize(selfNode, &exc); |
| if(exc) | if(exc) |
| throw Exception(0, 0, | throw Exception(0, 0, |
| &method_name, | &method_name, |
| exc); | exc); |
| } | } |
| static void _selectX(Request& r, const String& method_name, MethodParams *params, | |
| /* | void (*handler)(Pool& pool, |
| static void _select(Request& r, const String& method_name, MethodParams *params) { | 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 be string"); | const String& expression=params->as_string(0, "expression must be string"); |
| const char *expression_cstr=expression.cstr(); | |
| 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(&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) |
| char *buf=(char *)pool.malloc(MAX_NUMBER); | dome_document=GDOME_DOC(dome_node); |
| snprintf(buf, MAX_NUMBER, "%d", i); | xmlDoc *xml_document=((_Gdome_xml_Document *)dome_document)->n; |
| skey << buf; | xmlXPathContext_auto_ptr ctxt(xmlXPathNewContext(xml_document)); |
| ctxt->node=xmlDocGetRootElement(xml_document); | |
| /*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; | |
| } | |
| hash.put(skey, new(pool) VXnode(pool, | |
| gdome_xml_n_mkref(res->nodesetval->nodeTab[i]))); | |
| } | } |
| } | |
| break; | |
| default: | |
| throw Exception(0, 0, | |
| &expression, | |
| "wrong xmlXPathEvalExpression result type (%d)", res->type); | |
| break; // never | |
| } | |
| } | |
| result.hash(0).put(skey, new(pool) VXnode(pool, list.item(i), false)); | static void selectNodeHandler(Pool& pool, |
| const String& expression, | |
| xmlXPathObject_auto_ptr res, | |
| Value *& result) { | |
| switch(res->type) { | |
| case XPATH_UNDEFINED: | |
| break; | |
| case XPATH_NODESET: | |
| if(res->nodesetval->nodeNr) { // empty result strangly has NODESET res->type | |
| if(res->nodesetval->nodeNr>1) | |
| throw Exception(0, 0, | |
| &expression, | |
| "resulted not in a single node (%d)", res->nodesetval->nodeNr); | |
| result=new(pool) VXnode(pool, gdome_xml_n_mkref(res->nodesetval->nodeTab[0])); | |
| } | } |
| result.set_name(method_name); | break; |
| r.write_no_lang(result); | case XPATH_BOOLEAN: |
| } catch(const XSLException& e) { | result=new(pool) VBool(pool, res->boolval!=0); |
| Exception::provide_source(pool, &expression, e); | 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 _selectSingle(Request& r, const String& method_name, MethodParams *params) { | static void selectBoolHandler(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) { | |
| 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 | |
| } | |
| } | |
| // expression | static void selectNumberHandler(Pool& pool, |
| const String& expression=params->as_string(0, "expression must be string"); | const String& expression, |
| const char *expression_cstr=expression.cstr(); | xmlXPathObject_auto_ptr res, |
| XalanDOMString dstring(expression_cstr); | Value *& result) { |
| const XalanDOMChar *expression_dcstr=dstring.c_str(); | switch(res->type) { |
| case XPATH_NUMBER: | |
| XPathEvaluator evaluator; | result=new(pool) VDouble(pool, res->floatval); |
| // Initialize the XalanSourceTree subsystem... | break; |
| // XalanSourceTreeInit theSourceTreeInit; | case XPATH_NODESET: |
| // We'll use these to parse the XML file. | if(!res->nodesetval->nodeNr) |
| XalanSourceTreeDOMSupport dom_support; | break; |
| // else[nodeset] fall down to default | |
| try { | default: |
| if(GdomeNode *node=evaluator.selectSingleNode(dom_support, | throw Exception(0, 0, |
| &vnode.get_node(&method_name), | &expression, |
| expression_dcstr)) { | "wrong xmlXPathEvalExpression result type (%d)", res->type); |
| break; // never | |
| VXnode& result=*new(pool) VXnode(pool, node, false); | } |
| result.set_name(method_name); | } |
| r.write_no_lang(result); | |
| } | static void selectStringHandler(Pool& pool, |
| } catch(const XSLException& e) { | const String& expression, |
| Exception::provide_source(pool, &expression, e); | 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 418 MXnode::MXnode(Pool& apool) : Methoded(a | Line 621 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, _selectSingle, 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) \ | #define CONST(name) \ |