|
|
| version 1.197, 2020/11/10 22:42:25 | version 1.201, 2021/10/19 16:16:35 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b xdoc parser class. | Parser: @b xdoc parser class. |
| Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 118 private: | Line 118 private: |
| xsltTransformContext *_Ptr; | xsltTransformContext *_Ptr; |
| }; | }; |
| class xsltStylesheet_auto_ptr { | |
| public: | |
| explicit xsltStylesheet_auto_ptr(xsltStylesheet *_APtr = 0) | |
| : _Owns(_APtr != 0), _Ptr(_APtr) {} | |
| xsltStylesheet_auto_ptr(const xsltStylesheet_auto_ptr& _Y) | |
| : _Owns(_Y._Owns), _Ptr(_Y.release()) {} | |
| xsltStylesheet_auto_ptr& operator=(const xsltStylesheet_auto_ptr& _Y) | |
| {if (this != &_Y) | |
| {if (_Ptr != _Y.get()) | |
| {if (_Owns && _Ptr) | |
| xsltFreeStylesheet(_Ptr); | |
| _Owns = _Y._Owns; } | |
| else if (_Y._Owns) | |
| _Owns = true; | |
| _Ptr = _Y.release(); } | |
| return (*this); } | |
| ~xsltStylesheet_auto_ptr() | |
| {if (_Owns && _Ptr) | |
| xsltFreeStylesheet(_Ptr); } | |
| xsltStylesheet& operator*() const | |
| {return (*get()); } | |
| xsltStylesheet *operator->() const | |
| {return (get()); } | |
| xsltStylesheet *get() const | |
| {return (_Ptr); } | |
| xsltStylesheet *release() const | |
| {((xsltStylesheet_auto_ptr *)this)->_Owns = false; | |
| return (_Ptr); } | |
| private: | |
| bool _Owns; | |
| xsltStylesheet *_Ptr; | |
| }; | |
| // methods | // methods |
| static void writeNode(Request& r, VXdoc& xdoc, xmlNode* node) { | static void writeNode(Request& r, VXdoc& xdoc, xmlNode* node) { |
| Line 401 static void _create(Request& r, MethodPa | Line 368 static void _create(Request& r, MethodPa |
| set_encoding=true; | set_encoding=true; |
| // must be last action in if, see after if} | // must be last action in if, see after if} |
| } else { | } else { |
| VFile* vfile=param.as_vfile(String::L_AS_IS); | VFile* vfile=param.as_vfile(); |
| xmldoc=xmlReadMemory(vfile->value_ptr(), vfile->value_size(), NULL, NULL, XML_PARSE_OPTIONS); | xmldoc=xmlReadMemory(vfile->value_ptr(), vfile->value_size(), NULL, NULL, XML_PARSE_OPTIONS); |
| if(!xmldoc || xmlHaveGenericErrors()) | if(!xmldoc || xmlHaveGenericErrors()) |
| throw XmlException(0, r); | throw XmlException(0, r); |
| Line 472 String::C xdoc2buf(Request& r, VXdoc& vd | Line 439 String::C xdoc2buf(Request& r, VXdoc& vd |
| xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer)); | xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer)); |
| xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet()); | xsltStylesheet *stylesheet = xsltNewStylesheet(); |
| if(!stylesheet.get()) | if(!stylesheet) |
| throw Exception(0, | throw Exception(0, 0, "xsltNewStylesheet failed"); |
| 0, | |
| "xsltNewStylesheet failed"); | |
| #define OOSTRING2STYLE(name) \ | #define OOSTRING2STYLE(name) \ |
| stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0 | stylesheet->name=oo.name?BAD_CAST xmlMemStrdup((const char*)r.transcode(*oo.name)):0 |
| Line 497 String::C xdoc2buf(Request& r, VXdoc& vd | Line 462 String::C xdoc2buf(Request& r, VXdoc& vd |
| xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding); | xmldoc.encoding=BAD_CAST xmlMemStrdup(render_encoding); |
| if(header_encoding) | if(header_encoding) |
| stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding); | stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding); |
| if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0 | if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet)<0 |
| || xmlHaveGenericErrors()) | || xmlHaveGenericErrors()) |
| throw XmlException(0, r); | throw XmlException(0, r); |
| Line 594 static void add_xslt_param( | Line 559 static void add_xslt_param( |
| *info->current_transform_param++=(s=info->r->transcode(meaning->as_string())); *info->strings+=s; | *info->current_transform_param++=(s=info->r->transcode(meaning->as_string())); *info->strings+=s; |
| } | } |
| static VXdoc& _transform(Request& r, const String* stylesheet_source, | static VXdoc& _transform(Request& r, const String* stylesheet_source, VXdoc& vdoc, xsltStylesheetPtr stylesheet, const xmlChar** transform_params) |
| VXdoc& vdoc, xsltStylesheetPtr stylesheet, const xmlChar** transform_params) | |
| { | { |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| Line 669 static void _transform(Request& r, Metho | Line 633 static void _transform(Request& r, Metho |
| VXdoc* result; | VXdoc* result; |
| if(Value *vxdoc=params[0].as(VXDOC_TYPE)) { // stylesheet (xdoc) | if(Value *vxdoc=params[0].as(VXDOC_TYPE)) { // stylesheet (xdoc) |
| VXdoc& vstylesheet=static_cast<VXdoc &>(*vxdoc); | VXdoc& vstylesheet=static_cast<VXdoc &>(*vxdoc); |
| xmlDoc& stylesheetdoc=vstylesheet.get_xmldoc(); | |
| if(!vstylesheet.stylesheet){ | // compile xdoc stylesheet |
| xmlDoc& stylesheetdoc=vstylesheet.get_xmldoc(); | xsltStylesheet *stylesheet=xsltParseStylesheetDoc(&stylesheetdoc); |
| if(xmlHaveGenericErrors()) | |
| // compile xdoc stylesheet | throw XmlException(0, r); |
| vstylesheet.stylesheet=xsltParseStylesheetDoc(&stylesheetdoc); | if(!stylesheet) |
| if(xmlHaveGenericErrors()) | throw Exception("xml", 0, "stylesheet failed to compile"); |
| throw XmlException(0, r); | |
| if(!vstylesheet.stylesheet) | |
| throw Exception("xml", 0, "stylesheet failed to compile"); | |
| } | |
| // transform! | // transform! |
| result=&_transform(r, 0, vdoc, vstylesheet.stylesheet, transform_params); | result=&_transform(r, 0, vdoc, stylesheet, transform_params); |
| } else { // stylesheet (file name) | } else { // stylesheet (file name) |
| // extablish stylesheet connection | // extablish stylesheet connection |
| const String& stylesheet_filespec=r.full_disk_path(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)")); | const String& stylesheet_filespec=r.full_disk_path(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)")); |
| Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec); | Stylesheet_connection_ptr connection(stylesheet_manager->get_connection(stylesheet_filespec)); |
| // load and compile file to stylesheet [or get cached if any] | // load and compile file to stylesheet [or get cached if any] |
| // transform! | // transform! |