--- parser3/src/classes/xdoc.C 2011/11/23 12:17:23 1.172 +++ parser3/src/classes/xdoc.C 2016/04/06 16:08:19 1.185 @@ -1,7 +1,7 @@ /** @file Parser: @b xdoc parser class. - Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ @@ -9,8 +9,6 @@ #ifdef XML -static const char * const IDENT_XDOC_C="$Date: 2011/11/23 12:17:23 $"; - #include "libxml/tree.h" #include "libxml/HTMLtree.h" #include "libxslt/xsltInternals.h" @@ -30,6 +28,8 @@ static const char * const IDENT_XDOC_C=" #include "xnode.h" #include "pa_charsets.h" +volatile const char * IDENT_XDOC_C="$Id: xdoc.C,v 1.185 2016/04/06 16:08:19 moko Exp $"; + // defines #define XDOC_CLASS_NAME "xdoc" @@ -47,7 +47,7 @@ public: // global variable -DECLARE_CLASS_VAR(xdoc, new MXdoc, 0); +DECLARE_CLASS_VAR(xdoc, new MXdoc); // helper classes @@ -381,12 +381,12 @@ static void _create(Request& r, MethodPa Temp_lang temp_lang(r, String::L_XML); const String& xml=r.process_to_string(param); - String::Body sbody=xml.cstr_to_string_body_untaint(r.flang, 0, &r.charsets); + String::Body sbody=xml.cstr_to_string_body_untaint(r.flang, r.connection(false), &r.charsets); xmldoc=xmlParseMemory(sbody.cstr(), sbody.length()); //printf("document=0x%p\n", document); if(!xmldoc || xmlHaveGenericErrors()) - throw XmlException(0); + throw XmlException(0, r); // must be last action in if, see after if} } else { // [localName] @@ -410,11 +410,11 @@ static void _create(Request& r, MethodPa #endif xmldoc=xmlNewDoc(0); if(!xmldoc || xmlHaveGenericErrors()) - throw XmlException(0); + throw XmlException(0, r); xmlNode* node=xmlNewChild((xmlNode*)xmldoc, NULL, localName, NULL); if(!node || xmlHaveGenericErrors()) - throw XmlException(0); + throw XmlException(0, r); set_encoding=true; // must be last action in if, see after if} @@ -422,7 +422,7 @@ static void _create(Request& r, MethodPa VFile* vfile=param.as_vfile(String::L_AS_IS); xmldoc=xmlParseMemory(vfile->value_ptr(), vfile->value_size()); if(!xmldoc || xmlHaveGenericErrors()) - throw XmlException(0); + throw XmlException(0, r); } } // must be first action after if} @@ -459,7 +459,7 @@ static void _load(Request& r, MethodPara /// @todo!! add SAFE MODE!! xmlDoc* xmldoc=xmlParseFile(uri_cstr); if(!xmldoc || xmlHaveGenericErrors()) - throw XmlException(uri); + throw XmlException(uri, r); // must be first action after if} // replace any previous parsed source @@ -517,11 +517,20 @@ String::C xdoc2buf(Request& r, VXdoc& vd stylesheet->encoding=BAD_CAST xmlMemStrdup(header_encoding); if(xsltSaveResultTo(outputBuffer.get(), &xmldoc, stylesheet.get())<0 || xmlHaveGenericErrors()) - throw XmlException(0); + throw XmlException(0, r); // write out result char *gnome_str; size_t gnome_length; +#ifdef LIBXML2_NEW_BUFFER + if(outputBuffer->conv) { + gnome_length=xmlBufUse(outputBuffer->conv); + gnome_str=(char *)xmlBufContent(outputBuffer->conv); + } else { + gnome_length=xmlOutputBufferGetSize(&(*outputBuffer)); + gnome_str=(char *)xmlOutputBufferGetContent(&(*outputBuffer)); + } +#else if(outputBuffer->conv) { gnome_length=outputBuffer->conv->use; gnome_str=(char *)outputBuffer->conv->content; @@ -529,6 +538,7 @@ String::C xdoc2buf(Request& r, VXdoc& vd gnome_length=outputBuffer->buffer->use; gnome_str=(char *)outputBuffer->buffer->content; } +#endif if(file_spec){ file_write(r.charsets, @@ -548,21 +558,16 @@ inline HashStringValue* get_options(Meth static void _file(Request& r, MethodParams& params) { VXdoc& vdoc=GET_SELF(r, VXdoc); - XDocOutputOptions oo(r, get_options(params, 0), true/* $.name[filename] could be specified by user */); + XDocOutputOptions oo(vdoc.output_options); + oo.append(r, get_options(params, 0), true/* $.name[filename] could be specified by user */); String::C buf=xdoc2buf(r, vdoc, oo, 0/*file_name. not to file, to memory*/); VFile& vfile=*new VFile; VHash& vhcontent_type=*new VHash; - vhcontent_type.hash().put( - value_name, - new VString(*oo.mediaType)); - vhcontent_type.hash().put( - String::Body("charset"), - new VString(*oo.encoding)); + vhcontent_type.hash().put(value_name, new VString(*oo.mediaType)); + vhcontent_type.hash().put("charset", new VString(*oo.encoding)); - vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, oo.filename, &vhcontent_type); - - vfile.set_mode(true/*text*/); + vfile.set_binary(false/*not tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, oo.filename, &vhcontent_type); // write out result r.write_no_lang(vfile); @@ -573,14 +578,16 @@ static void _save(Request& r, MethodPara const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING)); - XDocOutputOptions oo(r, get_options(params, 1)); + XDocOutputOptions oo(vdoc.output_options); + oo.append(r, get_options(params, 1)); xdoc2buf(r, vdoc, oo, &file_spec); } static void _string(Request& r, MethodParams& params) { VXdoc& vdoc=GET_SELF(r, VXdoc); - XDocOutputOptions oo(r, get_options(params, 0)); + XDocOutputOptions oo(vdoc.output_options); + oo.append(r, get_options(params, 0)); String::C buf=xdoc2buf(r, vdoc, oo, 0/*file_name. not to file, to memory*/, true/*use source charset to render, client charset to put to header*/); @@ -625,13 +632,39 @@ static VXdoc& _transform(Request& r, con 0/*FILE *profile*/, transformContext.get()); if(!transformed || xmlHaveGenericErrors()) - throw XmlException(stylesheet_source); + throw XmlException(stylesheet_source, r); //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE type, fixing transformed->type=XML_DOCUMENT_NODE; - // constructing result - return *new VXdoc(r.charsets, *transformed); + VXdoc& result=*new VXdoc(r.charsets, *transformed); + /* grabbing options + + + */ + XDocOutputOptions& oo=result.output_options; + + oo.method=stylesheet->method?&r.transcode(stylesheet->method):0; + oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0; + oo.mediaType=stylesheet->mediaType?&r.transcode(stylesheet->mediaType):0; + oo.indent=stylesheet->indent; + oo.version=stylesheet->version?&r.transcode(stylesheet->version):0; + oo.standalone=stylesheet->standalone; + oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration; + + // return + return result; } static void _transform(Request& r, MethodParams& params) { VXdoc& vdoc=GET_SELF(r, VXdoc); @@ -640,8 +673,8 @@ static void _transform(Request& r, Metho Array transform_strings; const xmlChar** transform_params=0; if(params.count()>1) - if(HashStringValue* hash=params.as_hash(1)) { - transform_params=new(UseGC) const xmlChar*[hash->count()*2+1]; + if(HashStringValue* hash=params.as_hash(1, "transform options")) { + transform_params=new const xmlChar*[hash->count()*2+1]; Add_xslt_param_info info={ &r, &transform_strings, @@ -657,7 +690,7 @@ static void _transform(Request& r, Metho // compile xdoc stylesheet xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc)); if(xmlHaveGenericErrors()) - throw XmlException(0); + throw XmlException(0, r); if(!stylesheet_ptr.get()) throw Exception("xml", 0, @@ -689,7 +722,9 @@ static void _transform(Request& r, Metho // constructor /// @test how to create empty type html? -MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, xnode_class) { +MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME) { + set_base(xnode_class); + /// DOM1 // Element createElement(in DOMString tagName) raises(DOMException); @@ -759,6 +794,6 @@ MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, // global variable -DECLARE_CLASS_VAR(xdoc, 0, 0); // fictive +DECLARE_CLASS_VAR(xdoc, 0); // fictive #endif