|
|
| version 1.171, 2011/11/11 03:55:15 | version 1.179, 2013/10/15 22:28:36 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b xdoc parser class. | Parser: @b xdoc parser class. |
| Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2012 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 9 | Line 9 |
| #ifdef XML | #ifdef XML |
| static const char * const IDENT_XDOC_C="$Date$"; | |
| #include "libxml/tree.h" | #include "libxml/tree.h" |
| #include "libxml/HTMLtree.h" | #include "libxml/HTMLtree.h" |
| #include "libxslt/xsltInternals.h" | #include "libxslt/xsltInternals.h" |
| Line 30 static const char * const IDENT_XDOC_C=" | Line 28 static const char * const IDENT_XDOC_C=" |
| #include "xnode.h" | #include "xnode.h" |
| #include "pa_charsets.h" | #include "pa_charsets.h" |
| volatile const char * IDENT_XDOC_C="$Id$"; | |
| // defines | // defines |
| #define XDOC_CLASS_NAME "xdoc" | #define XDOC_CLASS_NAME "xdoc" |
| Line 381 static void _create(Request& r, MethodPa | Line 381 static void _create(Request& r, MethodPa |
| Temp_lang temp_lang(r, String::L_XML); | Temp_lang temp_lang(r, String::L_XML); |
| const String& xml=r.process_to_string(param); | 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()); | xmldoc=xmlParseMemory(sbody.cstr(), sbody.length()); |
| //printf("document=0x%p\n", document); | //printf("document=0x%p\n", document); |
| if(!xmldoc || xmlHaveGenericErrors()) | if(!xmldoc || xmlHaveGenericErrors()) |
| throw XmlException(0); | throw XmlException(0, r); |
| // must be last action in if, see after if} | // must be last action in if, see after if} |
| } else { // [localName] | } else { // [localName] |
| Line 410 static void _create(Request& r, MethodPa | Line 410 static void _create(Request& r, MethodPa |
| #endif | #endif |
| xmldoc=xmlNewDoc(0); | xmldoc=xmlNewDoc(0); |
| if(!xmldoc || xmlHaveGenericErrors()) | if(!xmldoc || xmlHaveGenericErrors()) |
| throw XmlException(0); | throw XmlException(0, r); |
| xmlNode* node=xmlNewChild((xmlNode*)xmldoc, NULL, localName, NULL); | xmlNode* node=xmlNewChild((xmlNode*)xmldoc, NULL, localName, NULL); |
| if(!node || xmlHaveGenericErrors()) | if(!node || xmlHaveGenericErrors()) |
| throw XmlException(0); | throw XmlException(0, r); |
| set_encoding=true; | set_encoding=true; |
| // must be last action in if, see after if} | // must be last action in if, see after if} |
| Line 422 static void _create(Request& r, MethodPa | Line 422 static void _create(Request& r, MethodPa |
| VFile* vfile=param.as_vfile(String::L_AS_IS); | VFile* vfile=param.as_vfile(String::L_AS_IS); |
| xmldoc=xmlParseMemory(vfile->value_ptr(), vfile->value_size()); | xmldoc=xmlParseMemory(vfile->value_ptr(), vfile->value_size()); |
| if(!xmldoc || xmlHaveGenericErrors()) | if(!xmldoc || xmlHaveGenericErrors()) |
| throw XmlException(0); | throw XmlException(0, r); |
| } | } |
| } | } |
| // must be first action after if} | // must be first action after if} |
| Line 459 static void _load(Request& r, MethodPara | Line 459 static void _load(Request& r, MethodPara |
| /// @todo!! add SAFE MODE!! | /// @todo!! add SAFE MODE!! |
| xmlDoc* xmldoc=xmlParseFile(uri_cstr); | xmlDoc* xmldoc=xmlParseFile(uri_cstr); |
| if(!xmldoc || xmlHaveGenericErrors()) | if(!xmldoc || xmlHaveGenericErrors()) |
| throw XmlException(uri); | throw XmlException(uri, r); |
| // must be first action after if} | // must be first action after if} |
| // replace any previous parsed source | // replace any previous parsed source |
| Line 517 String::C xdoc2buf(Request& r, VXdoc& vd | Line 517 String::C xdoc2buf(Request& r, VXdoc& vd |
| 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.get())<0 |
| || xmlHaveGenericErrors()) | || xmlHaveGenericErrors()) |
| throw XmlException(0); | throw XmlException(0, r); |
| // write out result | // write out result |
| char *gnome_str; | char *gnome_str; |
| Line 548 inline HashStringValue* get_options(Meth | Line 548 inline HashStringValue* get_options(Meth |
| static void _file(Request& r, MethodParams& params) { | static void _file(Request& r, MethodParams& params) { |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | 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*/); | String::C buf=xdoc2buf(r, vdoc, oo, 0/*file_name. not to file, to memory*/); |
| VFile& vfile=*new VFile; | VFile& vfile=*new VFile; |
| Line 560 static void _file(Request& r, MethodPara | Line 561 static void _file(Request& r, MethodPara |
| String::Body("charset"), | String::Body("charset"), |
| new VString(*oo.encoding)); | new VString(*oo.encoding)); |
| vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, | vfile.set_binary(false/*not tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, oo.filename, &vhcontent_type); |
| (oo.filename)?oo.filename->taint_cstr(String::L_FILE_SPEC):0, &vhcontent_type); | |
| vfile.set_mode(true/*text*/); | |
| // write out result | // write out result |
| r.write_no_lang(vfile); | r.write_no_lang(vfile); |
| Line 574 static void _save(Request& r, MethodPara | Line 572 static void _save(Request& r, MethodPara |
| const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING)); | 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); | xdoc2buf(r, vdoc, oo, &file_spec); |
| } | } |
| static void _string(Request& r, MethodParams& params) { | static void _string(Request& r, MethodParams& params) { |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | 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, | String::C buf=xdoc2buf(r, vdoc, oo, |
| 0/*file_name. not to file, to memory*/, | 0/*file_name. not to file, to memory*/, |
| true/*use source charset to render, client charset to put to header*/); | true/*use source charset to render, client charset to put to header*/); |
| Line 626 static VXdoc& _transform(Request& r, con | Line 626 static VXdoc& _transform(Request& r, con |
| 0/*FILE *profile*/, | 0/*FILE *profile*/, |
| transformContext.get()); | transformContext.get()); |
| if(!transformed || xmlHaveGenericErrors()) | if(!transformed || xmlHaveGenericErrors()) |
| throw XmlException(stylesheet_source); | throw XmlException(stylesheet_source, r); |
| //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE type, fixing | //gdome_xml_doc_mkref dislikes XML_HTML_DOCUMENT_NODE type, fixing |
| transformed->type=XML_DOCUMENT_NODE; | transformed->type=XML_DOCUMENT_NODE; |
| // constructing result | // constructing result |
| return *new VXdoc(r.charsets, *transformed); | VXdoc& result=*new VXdoc(r.charsets, *transformed); |
| /* grabbing options | |
| <xsl:output | |
| !method = "xml" | "html" | "text" | |
| X| qname-but-not-ncname | |
| !version = nmtoken | |
| !encoding = string | |
| !omit-xml-declaration = "yes" | "no" | |
| !standalone = "yes" | "no" | |
| !doctype-public = string | |
| !doctype-system = string | |
| Xcdata-section-elements = qnames | |
| !indent = "yes" | "no" | |
| !media-type = string /> | |
| */ | |
| 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) { | static void _transform(Request& r, MethodParams& params) { |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| Line 641 static void _transform(Request& r, Metho | Line 667 static void _transform(Request& r, Metho |
| Array<const xmlChar*> transform_strings; | Array<const xmlChar*> transform_strings; |
| const xmlChar** transform_params=0; | const xmlChar** transform_params=0; |
| if(params.count()>1) | if(params.count()>1) |
| if(HashStringValue* hash=params.as_hash(1)) { | if(HashStringValue* hash=params.as_hash(1, "transform options")) { |
| transform_params=new(UseGC) const xmlChar*[hash->count()*2+1]; | transform_params=new(UseGC) const xmlChar*[hash->count()*2+1]; |
| Add_xslt_param_info info={ | Add_xslt_param_info info={ |
| &r, | &r, |
| Line 658 static void _transform(Request& r, Metho | Line 684 static void _transform(Request& r, Metho |
| // compile xdoc stylesheet | // compile xdoc stylesheet |
| xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc)); | xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc)); |
| if(xmlHaveGenericErrors()) | if(xmlHaveGenericErrors()) |
| throw XmlException(0); | throw XmlException(0, r); |
| if(!stylesheet_ptr.get()) | if(!stylesheet_ptr.get()) |
| throw Exception("xml", | throw Exception("xml", |
| 0, | 0, |