--- parser3/src/classes/xdoc.C 2002/01/15 13:18:42 1.61 +++ parser3/src/classes/xdoc.C 2002/01/24 15:11:59 1.76 @@ -4,11 +4,21 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://paf.design.ru) - $Id: xdoc.C,v 1.61 2002/01/15 13:18:42 paf Exp $ + $Id: xdoc.C,v 1.76 2002/01/24 15:11:59 paf Exp $ */ #include "pa_types.h" #ifdef XML +#include "gdome.h" +#include "libxml/tree.h" +extern "C" { +#include "gdomecore/gdome-xml-node.h" +#include "gdomecore/gdome-xml-document.h" +}; +#include "libxslt/xsltInternals.h" +#include "libxslt/transform.h" +#include "libxslt/xsltutils.h" + #include "pa_stylesheet_connection.h" #include "classes.h" #include "pa_request.h" @@ -17,12 +27,6 @@ #include "pa_vfile.h" #include "xnode.h" -extern "C" { -#include "gdomecore/gdome-xml-node.h" -#include "gdomecore/gdome-xml-document.h" -}; -#include "libxslt/transform.h" -#include "libxslt/xsltutils.h" // defines @@ -32,7 +36,6 @@ extern "C" { #define XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "xml" #define XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "html" #define XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "text" -#define XDOC_OUTPUT_DEFAULT_INDENT 4 // class @@ -52,8 +55,8 @@ public: // Methoded class xmlOutputBuffer_auto_ptr { public: - explicit xmlOutputBuffer_auto_ptr(xmlOutputBuffer *_P = 0) - : _Owns(_P != 0), _Ptr(_P) {} + explicit xmlOutputBuffer_auto_ptr(xmlOutputBuffer *_APtr = 0) + : _Owns(_APtr != 0), _Ptr(_APtr) {} xmlOutputBuffer_auto_ptr(const xmlOutputBuffer_auto_ptr& _Y) : _Owns(_Y._Owns), _Ptr(_Y.release()) {} xmlOutputBuffer_auto_ptr& operator=(const xmlOutputBuffer_auto_ptr& _Y) @@ -83,7 +86,71 @@ private: xmlOutputBuffer *_Ptr; }; +class xsltTransformContext_auto_ptr { +public: + explicit xsltTransformContext_auto_ptr(xsltTransformContext *_APtr = 0) + : _Owns(_APtr != 0), _Ptr(_APtr) {} + xsltTransformContext_auto_ptr(const xsltTransformContext_auto_ptr& _Y) + : _Owns(_Y._Owns), _Ptr(_Y.release()) {} + xsltTransformContext_auto_ptr& operator=(const xsltTransformContext_auto_ptr& _Y) + {if (this != &_Y) + {if (_Ptr != _Y.get()) + {if (_Owns && _Ptr) + xsltFreeTransformContext(_Ptr); + _Owns = _Y._Owns; } + else if (_Y._Owns) + _Owns = true; + _Ptr = _Y.release(); } + return (*this); } + ~xsltTransformContext_auto_ptr() + {if (_Owns && _Ptr) + xsltFreeTransformContext(_Ptr); } + xsltTransformContext& operator*() const + {return (*get()); } + xsltTransformContext *operator->() const + {return (get()); } + xsltTransformContext *get() const + {return (_Ptr); } + xsltTransformContext *release() const + {((xsltTransformContext_auto_ptr *)this)->_Owns = false; + return (_Ptr); } +private: + bool _Owns; + 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 @@ -299,7 +366,7 @@ static void _getElementById(Request& r, // write out result VXnode& result=*new(pool) VXnode(pool, node); r.write_no_lang(result); - } else if(exc) + } else if(exc || xmlHaveGenericErrors()) throw Exception(0, 0, &method_name, exc); @@ -326,7 +393,7 @@ static void _create(Request& r, const St 0/*publicId* /, 0/*systemId* /, &exc); - if(!documentType || exc) + if(!documentType || exc || xmlHaveGenericErrors()) throw Exception(0, 0, &method_name, exc); @@ -336,7 +403,7 @@ static void _create(Request& r, const St pool.transcode(qualifiedName).get(), 0/*doctype*/, &exc); - if(!document || exc) + if(!document || exc || xmlHaveGenericErrors()) throw Exception(0, 0, &method_name, exc); @@ -347,7 +414,6 @@ static void _create(Request& r, const St vdoc.set_document(document); } -/// @test xmlSAXParseMemory(NULL<as_string(0, "uri must be string"); const String& uri=r.absolute(file_name); - GdomeException exc; - GdomeDocument *document=gdome_di_createDocFromURI(domimpl, - uri.cstr(), - GDOME_LOAD_PARSING - /* GDOME_LOAD_VALIDATING pending until kill warning of no-dtd*/ - /*|GDOME_LOAD_SUBSTITUTE_ENTITIES */, - &exc); - if(!document || exc) + GdomeDocument *document=(GdomeDocument *) + gdome_xml_n_mkref((xmlNode *)xmlParseFile(uri.cstr())); + if(!document || xmlHaveGenericErrors()) { + GdomeException exc=0; throw Exception(0, 0, - &method_name, + &uri, exc); + } // replace any previous parsed source vdoc.set_document(document); @@ -485,143 +547,132 @@ static void prepare_output_options( } } - -static void _save(Request& r, const String& method_name, MethodParams *params) { - Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); - - const String& file_name=params->as_string(0, "file name must be string"); - const String& filespec=r.absolute(file_name); - - GdomeException exc; - if(!gdome_di_saveDocToFile(domimpl, - vdoc.get_document(&method_name), - filespec.cstr(String::UL_FILE_SPEC), - GDOME_SAVE_LIBXML_INDENT /*GDOME_SAVE_STANDARD */, - &exc)) - throw Exception(0, 0, - &method_name, - exc); -} - -int xmlOutputWriteToParserString(void *context, const char *buffer, int len) { - String& string=*static_cast(context); - char *copy=(char *)string.malloc((size_t)len); - memcpy(copy, buffer, (size_t)len); - string.APPEND_CLEAN(copy, (size_t)len, "xdoc", 0); - return len; -} - -static void _string(Request& r, const String& method_name, MethodParams *params) { - Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); - - VXdoc::Output_options oo(vdoc.output_options); - prepare_output_options(pool, method_name, params, 0, +static void xdoc2buf(Pool& pool, VXdoc& vdoc, + const String& method_name, MethodParams *params, int index, + VXdoc::Output_options& oo, + const String *file_spec, + char **parser_buf, size_t *parser_size) { + prepare_output_options(pool, method_name, params, index, oo); - xmlCharEncodingHandler *encoder=0; - if(oo.encoding) { - const char *encoding_cstr=oo.encoding->cstr(); - encoder=xmlFindCharEncodingHandler(encoding_cstr); - if(!encoder) - throw Exception(0, 0, - &method_name, - "encoding '%s' not supported", encoding_cstr); - } + const char *encoding_cstr=oo.encoding->cstr(); + xmlCharEncodingHandler *encoder=xmlFindCharEncodingHandler(encoding_cstr); + if(!encoder) + throw Exception(0, 0, + &method_name, + "encoding '%s' not supported", encoding_cstr); // UTF-8 encoder contains empty input/output converters, // which is wrong for xmlOutputBufferCreateIO // while zero encoder goes perfectly if(encoder && strcmp(encoder->name, "UTF-8")==0) encoder=0; - String& result=*new(pool) String(pool); - xmlOutputBuffer_auto_ptr outputBuffer(xmlOutputBufferCreateIO( - xmlOutputWriteToParserString, - 0/*xmlOutputCloseCallback*/, - &result, - encoder)); + xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(encoder)); - xsltStylesheet *stylesheet=xsltNewStylesheet(); - if(!stylesheet) + xsltStylesheet_auto_ptr stylesheet(xsltNewStylesheet()); + if(!stylesheet.get()) throw Exception(0, 0, &method_name, "xsltNewStylesheet failed"); - #define OO2STYLE(name) \ + #define OOS2STYLE(name) \ stylesheet->name=oo.name?BAD_CAST g_strdup(pool.transcode(*oo.name)->str):0 + #define OOE2STYLE(name) \ + stylesheet->name=oo.name - OO2STYLE(method); - OO2STYLE(encoding); - OO2STYLE(mediaType); - OO2STYLE(doctypeSystem); - OO2STYLE(doctypePublic); - stylesheet->indent=oo.indent; - OO2STYLE(version); - stylesheet->standalone=oo.standalone; - stylesheet->omitXmlDeclaration=oo.omitXmlDeclaration; - - xmlDoc *document=((Gdome_xml_Document*)vdoc.get_document(&method_name))->n; - xsltSaveResultTo(outputBuffer.get(), document, stylesheet); - xsltFreeStylesheet(stylesheet); -/* - char *mem; - GdomeException exc; - if(!gdome_di_saveDocToMemory(domimpl, - vdoc.get_document(&method_name), - &mem, - vdoc.output_options.indent?GDOME_SAVE_LIBXML_INDENT:GDOME_SAVE_STANDARD, - &exc)) + OOS2STYLE(method); + OOS2STYLE(encoding); + OOS2STYLE(mediaType); + OOS2STYLE(doctypeSystem); + OOS2STYLE(doctypePublic); + OOE2STYLE(indent); + OOS2STYLE(version); + OOE2STYLE(standalone); + OOE2STYLE(omitXmlDeclaration); + + xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document(&method_name)); + if(xsltSaveResultTo(outputBuffer.get(), document, stylesheet.get())<0) { + GdomeException exc=0; throw Exception(0, 0, &method_name, exc); - - // move to pool memory - size_t buf_size=strlen(mem); - char *buf=(char *)pool.malloc(buf_size); - memcpy(buf, mem, buf_size); - g_free(mem);*/ + } // write out result - r.write_no_lang(result); + char *gnome_buf; size_t gnome_size; + if(outputBuffer->conv) { + gnome_size=outputBuffer->conv->use; + gnome_buf=(char *)outputBuffer->conv->content; + } else { + gnome_size=outputBuffer->buffer->use; + gnome_buf=(char *)outputBuffer->buffer->content; + } + if(file_spec) + file_write(pool, + *file_spec, + gnome_buf, gnome_size, + true/*as_text*/); + else if(*parser_size=gnome_size) { + *parser_buf=(char *)pool.malloc(gnome_size); + memcpy(*parser_buf, gnome_buf, gnome_size); + } else + *parser_buf=0; } -/// @test remove text/xml const. (r.self); - - char *mem; - GdomeException exc; - if(!gdome_di_saveDocToMemory(domimpl, - vdoc.get_document(&method_name), - &mem, - GDOME_SAVE_LIBXML_INDENT /*GDOME_SAVE_STANDARD */, - &exc)) - throw Exception(0, 0, - &method_name, - exc); - - // move to pool memory - size_t buf_size=strlen(mem); - char *buf=(char *)pool.malloc(buf_size); - memcpy(buf, mem, buf_size); - g_free(mem); + VXdoc::Output_options oo(vdoc.output_options); + char *buf; size_t buf_size; + xdoc2buf(pool, vdoc, method_name, params, 0, + oo, + 0/*not to file, to memory*/, + &buf, &buf_size); + // write out result + r.write_no_lang(*new(pool) String(pool, buf, buf_size)); // write out result VFile& vfile=*new(pool) VFile(pool); - const String& scontent_type=*new(pool) String(pool, "text/xml"); - Value *vcontent_type=new(pool) VString(scontent_type); + Value *vcontent_type; VHash *vhcontent_type=new(pool) VHash(pool); - vhcontent_type->hash(&method_name).put(*value_name, new(pool) VString(scontent_type)); - const String& scharset=pool.get_source_charset().name(); - vhcontent_type->hash(&method_name).put(*new(pool) String(pool, "charset"), new(pool) VString(scharset)); + vhcontent_type->hash(&method_name).put( + *value_name, + new(pool) VString(*oo.mediaType)); + vhcontent_type->hash(&method_name).put( + *new(pool) String(pool, "charset"), + new(pool) VString(*oo.encoding)); vcontent_type=vhcontent_type; vfile.set(false/*tainted*/, buf, buf_size, 0/*file_name*/, vcontent_type); r.write_no_lang(vfile); } +static void _save(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + VXdoc& vdoc=*static_cast(r.self); + + const String& file_spec=r.absolute(params->as_string(0, "file name must be string")); + + VXdoc::Output_options oo(vdoc.output_options); + xdoc2buf(pool, vdoc, method_name, params, 1, + oo, + &file_spec, + 0, 0); +} + +static void _string(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + VXdoc& vdoc=*static_cast(r.self); + VXdoc::Output_options oo(vdoc.output_options); + char *buf; size_t buf_size; + xdoc2buf(pool, vdoc, method_name, params, 0, + oo, + 0/*not to file, to memory*/, + &buf, &buf_size); + // write out result + r.write_no_lang(*new(pool) String(pool, buf, buf_size)); +} + static void add_xslt_param(const Hash::Key& aattribute, Hash::Val *ameaning, void *info) { Value *meaning=static_cast(ameaning); @@ -653,26 +704,25 @@ static void _transform(Request& r, const // stylesheet const String& stylesheet_filespec=r.absolute(params->as_string(0, "file name must be string")); - Stylesheet_connection& connection=stylesheet_manager->get_connection(stylesheet_filespec); + Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec); // transform - xsltStylesheet *stylesheet=connection.stylesheet(false/*nocache*/); - xmlDoc *document=((Gdome_xml_Document*)vdoc.get_document(&method_name))->n; - xsltTransformContext *transformContext=xsltNewTransformContext(stylesheet, document); + xsltStylesheet *stylesheet=connection->stylesheet(false/*nocache*/); + xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document(&method_name)); + xsltTransformContext_auto_ptr transformContext( + xsltNewTransformContext(stylesheet, document)); xmlDoc *transformed=xsltApplyStylesheetUser( stylesheet, document, transform_params, 0/*const char *output*/, 0/*FILE *profile*/, - transformContext); - if(!transformed) { - xsltFreeTransformContext(transformContext); - // close - connection.close(); + transformContext.get()); + if(!transformed || xmlHaveGenericErrors()) { + GdomeException exc=0; throw Exception(0, 0, - &method_name, - "transform failed. TODO: show errors"); + &stylesheet_filespec, + exc); } //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE type, fixing @@ -712,13 +762,8 @@ static void _transform(Request& r, const oo.standalone=stylesheet->standalone!=0; oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration!=0; - xsltFreeTransformContext(transformContext); - // close - connection.close(); - // write out result r.write_no_lang(result); - } // constructor