|
|
| version 1.163, 2009/07/07 05:47:43 | version 1.197, 2020/11/10 22:42:25 |
|---|---|
| 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-2017 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 28 static const char * const IDENT_XDOC_C=" | Line 26 static const char * const IDENT_XDOC_C=" |
| #include "pa_vfile.h" | #include "pa_vfile.h" |
| #include "pa_xml_exception.h" | #include "pa_xml_exception.h" |
| #include "xnode.h" | #include "xnode.h" |
| #include "pa_charsets.h" | |
| volatile const char * IDENT_XDOC_C="$Id$"; | |
| // defines | // defines |
| #define XDOC_CLASS_NAME "xdoc" | #define XDOC_CLASS_NAME "xdoc" |
| #define XDOC_OUTPUT_METHOD_OPTION_NAME "method" | |
| #define XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "xml" | |
| #define XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "html" | |
| #define XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "text" | |
| // class | // class |
| class MXdoc: public MXnode { | class MXdoc: public MXnode { |
| public: // VStateless_class | public: // VStateless_class |
| Value* create_new_value(Pool&, HashStringValue*) { return new VXdoc(); } | Value* create_new_value(Pool&) { return new VXdoc(); } |
| public: | public: |
| MXdoc(); | MXdoc(); |
| Line 51 public: | Line 47 public: |
| // global variable | // global variable |
| DECLARE_CLASS_VAR(xdoc, new MXdoc, 0); | DECLARE_CLASS_VAR(xnode, new MXnode); // must be here as Xdoc is inherited from Xnode and should be inited before |
| DECLARE_CLASS_VAR(xdoc, new MXdoc); | |
| // helper classes | // helper classes |
| Line 163 static void writeNode(Request& r, VXdoc& | Line 160 static void writeNode(Request& r, VXdoc& |
| "error creating node"); // OOM, bad name, things like that | "error creating node"); // OOM, bad name, things like that |
| // write out result | // write out result |
| r.write_no_lang(xdoc.wrap(*node)); | r.write(xdoc.wrap(*node)); |
| } | } |
| struct IdsIteratorInfo { | struct IdsIteratorInfo { |
| Line 171 struct IdsIteratorInfo { | Line 168 struct IdsIteratorInfo { |
| xmlNode *element; | xmlNode *element; |
| }; | }; |
| /* Hash Scanner function for pa_getElementById */ | /* switching to calling convetion of libxml */ |
| extern "C" void // switching to calling convetion of libxml | extern "C" void idsHashScanner (void *payload, void *data, const xmlChar *name) { |
| idsHashScanner (void *payload, void *data, xmlChar *name) { | |
| IdsIteratorInfo *priv = (IdsIteratorInfo *)data; | IdsIteratorInfo *priv = (IdsIteratorInfo *)data; |
| if (priv->element == NULL && xmlStrEqual (name, priv->elementId)) | if (priv->element == NULL && xmlStrEqual (name, priv->elementId)) |
| Line 184 idsHashScanner (void *payload, void *dat | Line 180 idsHashScanner (void *payload, void *dat |
| } | } |
| } | } |
| static xmlNode* | static xmlNode* pa_getElementById(xmlDoc& xmldoc, xmlChar* elementId) { |
| pa_getElementById(xmlDoc& xmldoc, xmlChar* elementId) { | |
| xmlHashTable *ids = (xmlHashTable *)xmldoc.ids; | xmlHashTable *ids = (xmlHashTable *)xmldoc.ids; |
| IdsIteratorInfo iter={elementId, NULL}; | IdsIteratorInfo iter={elementId, NULL}; |
| xmlHashScan(ids, idsHashScanner, &iter); | xmlHashScan(ids, (xmlHashScanner)idsHashScanner, &iter); |
| return iter.element; | return iter.element; |
| } | } |
| Line 222 pa_importNode (xmlDoc& xmldoc, xmlNode& | Line 217 pa_importNode (xmlDoc& xmldoc, xmlNode& |
| // Element createElement(in DOMString tagName) raises(DOMException); | // Element createElement(in DOMString tagName) raises(DOMException); |
| static void _createElement(Request& r, MethodParams& params) { | static void _createElement(Request& r, MethodParams& params) { |
| xmlChar* tagName=as_xmlname(r, params, 0, "tagName must be string"); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* tagName=as_xmlchar(r, params, 0, "tagName must be string"); | |
| xmlNode *node=xmlNewDocNode(&xmldoc, NULL, tagName, NULL); | xmlNode *node=xmlNewDocNode(&xmldoc, NULL, tagName, NULL); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException); | // Element createElementNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException); |
| static void _createElementNS(Request& r, MethodParams& params) { | static void _createElementNS(Request& r, MethodParams& params) { |
| xmlChar* namespaceURI=as_xmlnsuri(r, params, 0); | |
| xmlChar* qualifiedName=as_xmlqname(r, params, 1); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| // namespaceURI;localName | |
| xmlChar* namespaceURI=as_xmlchar(r, params, 0, NAMESPACEURI_MUST_BE_STRING); | |
| xmlChar* qualifiedName=as_xmlchar(r, params, 1, "qualifiedName must be string"); | |
| xmlChar* prefix=0; | xmlChar* prefix=0; |
| xmlChar* localName=xmlSplitQName2(qualifiedName, &prefix); | xmlChar* localName=xmlSplitQName2(qualifiedName, &prefix); |
| Line 263 static void _createDocumentFragment(Requ | Line 257 static void _createDocumentFragment(Requ |
| // Text createTextNode(in DOMString data); | // Text createTextNode(in DOMString data); |
| static void _createTextNode(Request& r, MethodParams& params) { | static void _createTextNode(Request& r, MethodParams& params) { |
| xmlChar* data=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* data=as_xmlchar(r, params, 0, DATA_MUST_BE_STRING); | |
| xmlNode *node=xmlNewDocText(&xmldoc, data); | xmlNode *node=xmlNewDocText(&xmldoc, data); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| // Comment createComment(in DOMString data) | // Comment createComment(in DOMString data) |
| static void _createComment(Request& r, MethodParams& params) { | static void _createComment(Request& r, MethodParams& params) { |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | xmlChar* data=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING); |
| xmlChar* data=as_xmlchar(r, params, 0, DATA_MUST_BE_STRING); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlNode *node=xmlNewComment(data); | xmlNode *node=xmlNewComment(data); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| Line 284 static void _createComment(Request& r, M | Line 278 static void _createComment(Request& r, M |
| // CDATASection createCDATASection(in DOMString data) raises(DOMException); | // CDATASection createCDATASection(in DOMString data) raises(DOMException); |
| static void _createCDATASection(Request& r, MethodParams& params) { | static void _createCDATASection(Request& r, MethodParams& params) { |
| xmlChar* data=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* data=as_xmlchar(r, params, 0, DATA_MUST_BE_STRING); | |
| xmlNode *node=xmlNewCDataBlock(&xmldoc, data, strlen((const char*)data)); | xmlNode *node=xmlNewCDataBlock(&xmldoc, data, strlen((const char*)data)); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| // ProcessingInstruction createProcessingInstruction(in DOMString target,in DOMString data) raises(DOMException); | // ProcessingInstruction createProcessingInstruction(in DOMString target,in DOMString data) raises(DOMException); |
| static void _createProcessingInstruction(Request& r, MethodParams& params) { | static void _createProcessingInstruction(Request& r, MethodParams& params) { |
| xmlChar* target=as_xmlchar(r, params, 0, XML_DATA_MUST_BE_STRING); | |
| xmlChar* data=as_xmlchar(r, params, 1, XML_DATA_MUST_BE_STRING); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* target=as_xmlchar(r, params, 0, DATA_MUST_BE_STRING); | |
| xmlChar* data=as_xmlchar(r, params, 1, DATA_MUST_BE_STRING); | |
| xmlNode *node=xmlNewDocPI(&xmldoc, target, data); | xmlNode *node=xmlNewDocPI(&xmldoc, target, data); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| // Attr createAttribute(in DOMString name) raises(DOMException); | // Attr createAttribute(in DOMString name) raises(DOMException); |
| static void _createAttribute(Request& r, MethodParams& params) { | static void _createAttribute(Request& r, MethodParams& params) { |
| xmlChar* name=as_xmlname(r, params, 0); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* name=as_xmlchar(r, params, 0, NAME_MUST_BE_STRING); | |
| xmlNode *node=(xmlNode*)xmlNewDocProp(&xmldoc, name, 0); | xmlNode *node=(xmlNode*)xmlNewDocProp(&xmldoc, name, 0); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException); | // Attr createAttributeNS(in DOMString namespaceURI, in DOMString qualifiedName) raises(DOMException); |
| static void _createAttributeNS(Request& r, MethodParams& params) { | static void _createAttributeNS(Request& r, MethodParams& params) { |
| xmlChar* namespaceURI=as_xmlnsuri(r, params, 0); | |
| xmlChar* qualifiedName=as_xmlqname(r, params, 1); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* namespaceURI=as_xmlchar(r, params, 0, NAMESPACEURI_MUST_BE_STRING); | |
| xmlChar* qualifiedName=as_xmlchar(r, params, 1, NAME_MUST_BE_STRING); | |
| xmlChar* prefix=0; | xmlChar* prefix=0; |
| xmlChar* localName=xmlSplitQName2(qualifiedName, &prefix); | xmlChar* localName=xmlSplitQName2(qualifiedName, &prefix); |
| Line 339 static void _createAttributeNS(Request& | Line 333 static void _createAttributeNS(Request& |
| // EntityReference createEntityReference(in DOMString name) raises(DOMException); | // EntityReference createEntityReference(in DOMString name) raises(DOMException); |
| static void _createEntityReference(Request& r, MethodParams& params) { | static void _createEntityReference(Request& r, MethodParams& params) { |
| xmlChar* name=as_xmlname(r, params, 0); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlChar* name=as_xmlchar(r, params, 0, NAME_MUST_BE_STRING); | |
| xmlNode *node=xmlNewReference(&xmldoc, name); | xmlNode *node=xmlNewReference(&xmldoc, name); |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| static void _getElementById(Request& r, MethodParams& params) { | static void _getElementById(Request& r, MethodParams& params) { |
| xmlChar* elementId=as_xmlname(r, params, 0, "elementID must be string"); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| // elementId | if(xmlNode *node=pa_getElementById(xmldoc, elementId)) |
| xmlChar* elementId=as_xmlchar(r, params, 0, "elementID must be string"); | |
| if(xmlNode *node=pa_getElementById(xmldoc, elementId)) { | |
| // write out result | |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | |
| } | } |
| static void _importNode(Request& r, MethodParams& params) { | static void _importNode(Request& r, MethodParams& params) { |
| xmlNode& importedNode=as_node(params, 0, "importedNode must be node"); | |
| bool deep=params.as_bool(1, "deep must be bool", r); | |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| xmlDoc& xmldoc=vdoc.get_xmldoc(); | xmlDoc& xmldoc=vdoc.get_xmldoc(); |
| xmlNode& importedNode= | |
| as_node(params, 0, "importedNode must be node"); | |
| bool deep= | |
| params.as_bool(1, "deep must be bool", r); | |
| xmlNode *node=xmlDocCopyNode(&importedNode, &xmldoc, deep?1: 0); | xmlNode *node=xmlDocCopyNode(&importedNode, &xmldoc, deep?1: 0); |
| // write out result | |
| writeNode(r, vdoc, node); | writeNode(r, vdoc, node); |
| } | } |
| /* | |
| GdomeElement *gdome_doc_createElementNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc); | #define XML_PARSE_OPTIONS (XML_PARSE_DTDLOAD | XML_PARSE_NOENT | XML_PARSE_HUGE) |
| GdomeAttr *gdome_doc_createAttributeNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc); | |
| */ | |
| static void _create(Request& r, MethodParams& params) { | static void _create(Request& r, MethodParams& params) { |
| Charset& source_charset=r.charsets.source(); | Charset& source_charset=r.charsets.source(); |
| Line 388 static void _create(Request& r, MethodPa | Line 374 static void _create(Request& r, MethodPa |
| xmlDoc* xmldoc; | xmlDoc* xmldoc; |
| bool set_encoding=false; | bool set_encoding=false; |
| if(param.get_junction()) { // {<?xml?>...} | if(param.get_junction()) { // {<?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(String::L_XML, r.connection(false), &r.charsets); | |
| String::Body sbody=xml.cstr_to_string_body_untaint(r.flang, 0, &r.charsets); | xmldoc=xmlReadMemory(sbody.cstr(), sbody.length(), NULL, NULL, XML_PARSE_OPTIONS); |
| xmldoc=xmlParseMemory(sbody.cstr(), sbody.length()); | |
| //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] |
| if(const String* value = param.get_string()){ | if(const String* value = param.get_string()){ |
| xmlChar* localName=r.transcode(*value); | xmlChar* localName=r.transcode(*value); |
| #if 0 | if(xmlValidateNCName(localName, 0) != 0) |
| GdomeDocumentType *documentType=gdome_di_createDocumentType ( | throw XmlException(0, XML_INVALID_LOCAL_NAME, localName); |
| docimpl, | |
| r.transcode(qualifiedName), | |
| 0/*publicId*/, | |
| 0/*systemId*/, | |
| &exc); | |
| if(!documentType || exc || xmlHaveGenericErrors()) | |
| throw Exception( | |
| method_name, | |
| exc); | |
| /// +xalan createXMLDecl ? | |
| #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} |
| } else { | } else { |
| 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=xmlReadMemory(vfile->value_ptr(), vfile->value_size(), NULL, NULL, XML_PARSE_OPTIONS); |
| 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 439 static void _create(Request& r, MethodPa | Line 415 static void _create(Request& r, MethodPa |
| const char* URI_cstr; | const char* URI_cstr; |
| if(params.count()>1) { // absolute(param) | if(params.count()>1) { // absolute(param) |
| const String& URI=params.as_string(0, "URI must be string"); | const String& URI=params.as_string(0, "URI must be string"); |
| URI_cstr=r.absolute(URI).cstr(); | URI_cstr=r.full_disk_path(URI).cstr(); |
| } else // default = disk path to requested document | } else // default = disk path to requested document |
| URI_cstr=r.request_info.path_translated; | URI_cstr=r.request_info.path_translated; |
| if(URI_cstr) | if(URI_cstr) |
| Line 458 static void _load(Request& r, MethodPara | Line 434 static void _load(Request& r, MethodPara |
| const String* uri=¶ms.as_string(0, "URI must be string"); | const String* uri=¶ms.as_string(0, "URI must be string"); |
| const char* uri_cstr; | const char* uri_cstr; |
| if(uri->pos("://")==STRING_NOT_FOUND) // disk path | if(uri->pos("://")==STRING_NOT_FOUND) // disk path |
| uri_cstr=r.absolute(*uri).taint_cstr(String::L_FILE_SPEC); | uri_cstr=r.full_disk_path(*uri).taint_cstr(String::L_FILE_SPEC); |
| else // xxx:// | else // xxx:// |
| uri_cstr=uri->taint_cstr(String::L_AS_IS); // leave as-is for xmlParseFile to handle | uri_cstr=uri->taint_cstr(String::L_AS_IS); // leave as-is for xmlParseFile to handle |
| /// @todo!! add SAFE MODE!! | /// @todo!! add SAFE MODE!! |
| xmlDoc* xmldoc=xmlParseFile(uri_cstr); | xmlDoc* xmldoc=xmlReadFile(uri_cstr, NULL, XML_PARSE_OPTIONS); |
| 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 |
| vdoc.set_xmldoc(r.charsets, *xmldoc); | vdoc.set_xmldoc(r.charsets, *xmldoc); |
| } | } |
| static void param_option_over_output_option( | String::C xdoc2buf(Request& r, VXdoc& vdoc, |
| HashStringValue& param_options, const char* option_name, | XDocOutputOptions& oo, |
| const String*& output_option) { | const String* file_spec, |
| if(Value* value=param_options.get(String::Body(option_name))) | bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) { |
| output_option=&value->as_string(); | Charset* render=0; |
| } | Charset* header=0; |
| static void param_option_over_output_option( | |
| HashStringValue& param_options, const char* option_name, | |
| int& output_option) { | |
| if(Value* value=param_options.get(String::Body(option_name))) { | |
| const String& s=value->as_string(); | |
| if(s=="yes") | |
| output_option=1; | |
| else if(s=="no") | |
| output_option=0; | |
| else | |
| throw Exception(PARSER_RUNTIME, | |
| &s, | |
| "%s must be either 'yes' or 'no'", option_name); | |
| } | |
| } | |
| /// @test valid_options check | |
| static void prepare_output_options(Request& r, | |
| MethodParams& params, size_t index, | |
| VXdoc::Output_options& oo) { | |
| /* | |
| <xsl:output | |
| !method = "xml" | "html" | "text" | qname-but-not-ncname | |
| !version = nmtoken | |
| !encoding = string | |
| !omit-xml-declaration = "yes" | "no" | |
| !standalone = "yes" | "no" | |
| !doctype-public = string | |
| !doctype-system = string | |
| cdata-section-elements = qnames | |
| !indent = "yes" | "no" | |
| !media-type = string /> | |
| */ | |
| // configuring with options from parameter... | |
| if(params.count()>index) { | |
| Value& voptions=params.as_no_junction(index, "options must be string"); | |
| if(voptions.is_defined()) { | |
| if(HashStringValue *options=voptions.get_hash()) { | |
| // $.method[xml|html|text] | |
| if(Value* vmethod=options->get(String::Body(XDOC_OUTPUT_METHOD_OPTION_NAME))) | |
| oo.method=&vmethod->as_string(); | |
| // $.version[1.0] | |
| param_option_over_output_option(*options, "version", oo.version); | |
| // $.encoding[windows-1251|...] | |
| param_option_over_output_option(*options, "encoding", oo.encoding); | |
| // $.omit-xml-declaration[yes|no] | |
| param_option_over_output_option(*options, "omit-xml-declaration", oo.omitXmlDeclaration); | |
| // $.standalone[yes|no] | |
| param_option_over_output_option(*options, "standalone", oo.standalone); | |
| // $.indent[yes|no] | |
| param_option_over_output_option(*options, "indent", oo.indent); | |
| // $.media-type[text/{html|xml|plain}] | |
| param_option_over_output_option(*options, "media-type", oo.mediaType); | |
| } | |
| } | |
| } | |
| // default encoding from pool | |
| if(!oo.encoding) | |
| oo.encoding=new String(r.charsets.source().NAME(), String::L_TAINTED); | |
| // default method=xml | |
| if(!oo.method) | |
| oo.method=new String(XDOC_OUTPUT_METHOD_OPTION_VALUE_XML); | |
| // default mediaType = depending on method | |
| if(!oo.mediaType) { | |
| if(*oo.method==XDOC_OUTPUT_METHOD_OPTION_VALUE_XML) | |
| oo.mediaType=new String("text/xml"); | |
| else if(*oo.method==XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML) | |
| oo.mediaType=new String("text/html"); | |
| else // XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT & all others | |
| oo.mediaType=new String("text/plain"); | |
| } | |
| } | |
| struct Xdoc2buf_result { | |
| char* str; | |
| size_t length; | |
| }; | |
| static Xdoc2buf_result xdoc2buf(Request& r, VXdoc& vdoc, | |
| MethodParams& params, int index, | |
| VXdoc::Output_options& oo, | |
| const String* file_spec, | |
| bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) { | |
| Xdoc2buf_result result; | |
| prepare_output_options(r, params, index, | |
| oo); | |
| const char* render_encoding; | |
| const char* header_encoding; | |
| if(use_source_charset_to_render_and_client_charset_to_write_to_header) { | if(use_source_charset_to_render_and_client_charset_to_write_to_header) { |
| render_encoding=r.charsets.source().NAME_CSTR(); | render=&r.charsets.source(); |
| header_encoding=r.charsets.client().NAME_CSTR(); | header=&r.charsets.client(); |
| } else { | } else { |
| header_encoding=render_encoding=oo.encoding->cstr(); | header=render=&pa_charsets.get(*oo.encoding); |
| } | } |
| const char* render_encoding=render->NAME_CSTR(); | |
| const char* header_encoding=header->NAME_CSTR(); | |
| xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding); | xmlCharEncodingHandler *renderer=xmlFindCharEncodingHandler(render_encoding); |
| if(!renderer) | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "encoding '%s' not supported", render_encoding); | |
| // UTF-8 renderer contains empty input/output converters, | // UTF-8 renderer contains empty input/output converters, |
| // which is wrong for xmlOutputBufferCreateIO | // which is wrong for xmlOutputBufferCreateIO |
| // while zero renderer goes perfectly | // while zero renderer goes perfectly |
| if(strcmp(render_encoding, "UTF-8")==0) | if(render->isUTF8()) |
| renderer=0; | renderer=0; |
| xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer)); | xmlOutputBuffer_auto_ptr outputBuffer(xmlAllocOutputBuffer(renderer)); |
| Line 616 static Xdoc2buf_result xdoc2buf(Request& | Line 499 static Xdoc2buf_result xdoc2buf(Request& |
| 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; size_t gnome_length; | 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) { | if(outputBuffer->conv) { |
| gnome_length=outputBuffer->conv->use; | gnome_length=outputBuffer->conv->use; |
| gnome_str=(char *)outputBuffer->conv->content; | gnome_str=(char *)outputBuffer->conv->content; |
| Line 627 static Xdoc2buf_result xdoc2buf(Request& | Line 520 static Xdoc2buf_result xdoc2buf(Request& |
| gnome_length=outputBuffer->buffer->use; | gnome_length=outputBuffer->buffer->use; |
| gnome_str=(char *)outputBuffer->buffer->content; | gnome_str=(char *)outputBuffer->buffer->content; |
| } | } |
| #endif | |
| if((result.length=gnome_length)) { | if(file_spec){ |
| result.str=pa_strdup(gnome_str, gnome_length); | file_write(r.charsets, |
| } else | *file_spec, |
| result.str=0; | gnome_str, |
| gnome_length, | |
| if(file_spec) | |
| file_write(*file_spec, | |
| gnome_str, gnome_length, | |
| true/*as_text*/); | true/*as_text*/); |
| return String::C(); // actually, we don't need this output at all | |
| } else | |
| return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length); | |
| } | |
| return result; | inline HashStringValue* get_options(MethodParams& params, size_t index){ |
| return (params.count()>index) ? params.as_hash(index) : 0; | |
| } | } |
| 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); |
| VXdoc::Output_options oo(vdoc.output_options); | |
| Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0, | |
| oo, | |
| 0/*not to file, to memory*/); | |
| // write out result | |
| r.write_no_lang(String(buf.str)); | |
| // write out result | 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; | VFile& vfile=*new VFile; |
| VHash& vhcontent_type=*new VHash; | VHash& vhcontent_type=*new VHash; |
| vhcontent_type.hash().put( | vhcontent_type.hash().put(value_name, new VString(*oo.mediaType)); |
| value_name, | vhcontent_type.hash().put("charset", new VString(*oo.encoding)); |
| new VString(*oo.mediaType)); | |
| vhcontent_type.hash().put( | vfile.set_binary(false/*not tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, oo.filename, &vhcontent_type); |
| String::Body("charset"), | |
| new VString(*oo.encoding)); | // write out result |
| r.write(vfile); | |
| vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, | |
| 0/*file_name*/, &vhcontent_type); | |
| r.write_no_lang(vfile); | |
| } | } |
| static void _save(Request& r, MethodParams& params) { | static void _save(Request& r, MethodParams& params) { |
| VXdoc& vdoc=GET_SELF(r, VXdoc); | VXdoc& vdoc=GET_SELF(r, VXdoc); |
| const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING)); | const String& file_spec=r.full_disk_path(params.as_string(0, FILE_NAME_MUST_BE_STRING)); |
| VXdoc::Output_options oo(vdoc.output_options); | XDocOutputOptions oo(vdoc.output_options); |
| xdoc2buf(r, vdoc, params, 1, | oo.append(r, get_options(params, 1)); |
| oo, | xdoc2buf(r, vdoc, oo, &file_spec); |
| &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); |
| VXdoc::Output_options oo(vdoc.output_options); | |
| Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0, | XDocOutputOptions oo(vdoc.output_options); |
| oo, | oo.append(r, get_options(params, 0)); |
| 0/*not to file, to memory*/, | 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*/); | true/*use source charset to render, client charset to put to header*/); |
| // write out result | // write out result |
| r.write_no_lang(String(buf.str, String::L_AS_IS)); | r.write(String(buf, String::L_AS_IS)); |
| } | } |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| Line 723 static VXdoc& _transform(Request& r, con | Line 614 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; |
| Line 744 static VXdoc& _transform(Request& r, con | Line 635 static VXdoc& _transform(Request& r, con |
| !indent = "yes" | "no" | !indent = "yes" | "no" |
| !media-type = string /> | !media-type = string /> |
| */ | */ |
| VXdoc::Output_options& oo=result.output_options; | XDocOutputOptions& oo=result.output_options; |
| oo.method=stylesheet->method?&r.transcode(stylesheet->method):0; | oo.method=stylesheet->method?&r.transcode(stylesheet->method):0; |
| oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0; | oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0; |
| Line 763 static void _transform(Request& r, Metho | Line 654 static void _transform(Request& r, Metho |
| // params | // params |
| 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) |
| Value& vparams=params.as_no_junction(1, "transform parameters must be hash"); | if(HashStringValue* hash=params.as_hash(1, "transform options")) { |
| if(!vparams.is_string()) | transform_params=new(PointerGC) const xmlChar*[hash->count()*2+1]; |
| if(HashStringValue* hash=vparams.get_hash()) { | Add_xslt_param_info info={ |
| transform_params=new(UseGC) const xmlChar*[hash->count()*2+1]; | &r, |
| Add_xslt_param_info info={ | &transform_strings, |
| &r, | transform_params |
| &transform_strings, | }; |
| transform_params | hash->for_each<Add_xslt_param_info*>(add_xslt_param, &info); |
| }; | transform_params[hash->count()*2]=0; |
| hash->for_each<Add_xslt_param_info*>(add_xslt_param, &info); | } |
| transform_params[hash->count()*2]=0; | |
| } else | |
| throw Exception(PARSER_RUNTIME, | |
| 0, | |
| "transform parameters parameter must be hash"); | |
| } | |
| VXdoc* result; | VXdoc* result; |
| if(Value *vxdoc=params[0].as(VXDOC_TYPE, false)) { // stylesheet (xdoc) | if(Value *vxdoc=params[0].as(VXDOC_TYPE)) { // stylesheet (xdoc) |
| xmlDoc& stylesheetdoc=static_cast<VXdoc *>(vxdoc)->get_xmldoc(); | VXdoc& vstylesheet=static_cast<VXdoc &>(*vxdoc); |
| // compile xdoc stylesheet | |
| xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc)); | if(!vstylesheet.stylesheet){ |
| if(xmlHaveGenericErrors()) | xmlDoc& stylesheetdoc=vstylesheet.get_xmldoc(); |
| throw XmlException(0); | |
| if(!stylesheet_ptr.get()) | // compile xdoc stylesheet |
| throw Exception("xml", | vstylesheet.stylesheet=xsltParseStylesheetDoc(&stylesheetdoc); |
| 0, | if(xmlHaveGenericErrors()) |
| "stylesheet failed to compile"); | throw XmlException(0, r); |
| // strange thing - xsltParseStylesheetDoc records document and destroys it in stylesheet destructor | if(!vstylesheet.stylesheet) |
| // we don't need that | throw Exception("xml", 0, "stylesheet failed to compile"); |
| stylesheet_ptr->doc=0; | } |
| // transform! | // transform! |
| result=&_transform(r, 0, | result=&_transform(r, 0, vdoc, vstylesheet.stylesheet, transform_params); |
| vdoc, stylesheet_ptr.get(), | |
| transform_params); | |
| } else { // stylesheet (file name) | } else { // stylesheet (file name) |
| // extablish stylesheet connection | // extablish stylesheet connection |
| const String& stylesheet_filespec= | const String& stylesheet_filespec=r.full_disk_path(params.as_string(0, "stylesheet must be file name (string) or DOM document (xdoc)")); |
| r.absolute(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! |
| result=&_transform(r, &stylesheet_filespec, vdoc, connection->stylesheet(), | result=&_transform(r, &stylesheet_filespec, vdoc, connection->stylesheet(), transform_params); |
| transform_params); | |
| } | } |
| // write out result | // write out result |
| r.write_no_lang(*result); | r.write(*result); |
| } | } |
| // constructor | // constructor |
| /// @test how to create empty type html? | /// @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 | /// DOM1 |
| // Element createElement(in DOMString tagName) raises(DOMException); | // Element createElement(in DOMString tagName) raises(DOMException); |
| Line 889 MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, | Line 772 MXdoc::MXdoc(): MXnode(XDOC_CLASS_NAME, |
| // global variable | // global variable |
| DECLARE_CLASS_VAR(xdoc, 0, 0); // fictive | DECLARE_CLASS_VAR(xnode, 0); // fictive |
| DECLARE_CLASS_VAR(xdoc, 0); // fictive | |
| #endif | #endif |