--- parser3/src/classes/xdoc.C 2002/08/01 11:41:13 1.97 +++ parser3/src/classes/xdoc.C 2003/04/11 09:58:10 1.110 @@ -1,14 +1,14 @@ /** @file Parser: @b xdoc parser class. - Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #include "classes.h" #ifdef XML -static const char* IDENT_XDOC_C="$Date: 2002/08/01 11:41:13 $"; +static const char* IDENT_XDOC_C="$Date: 2003/04/11 09:58:10 $"; #include "pa_stylesheet_connection.h" #include "pa_request.h" @@ -170,14 +170,14 @@ static void writeNode(Request& r, const // Element createElement(in DOMString tagName) raises(DOMException); static void _createElement(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& tagName=params->as_string(0, "tagName must be string"); GdomeException exc; GdomeNode *node= (GdomeNode *)gdome_doc_createElement(vdoc.get_document(&method_name), - pool.transcode(tagName).get(), + pool.transcode(tagName).use(), &exc); writeNode(r, method_name, node, exc); } @@ -185,7 +185,7 @@ static void _createElement(Request& r, c // DocumentFragment createDocumentFragment() static void _createDocumentFragment(Request& r, const String& method_name, MethodParams *) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); GdomeException exc; GdomeNode *node= @@ -198,14 +198,14 @@ static void _createDocumentFragment(Requ // Text createTextNode(in DOMString data); static void _createTextNode(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& data=params->as_string(0, "data must be string"); GdomeException exc; GdomeNode *node=(GdomeNode *)gdome_doc_createTextNode( vdoc.get_document(&method_name), - pool.transcode(data).get(), + pool.transcode(data).use(), &exc); writeNode(r, method_name, node, exc); } @@ -213,14 +213,14 @@ static void _createTextNode(Request& r, // Comment createComment(in DOMString data) static void _createComment(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& data=params->as_string(0, "data must be string"); GdomeException exc; GdomeNode *node=(GdomeNode *)gdome_doc_createComment( vdoc.get_document(&method_name), - pool.transcode(data).get(), + pool.transcode(data).use(), &exc); writeNode(r, method_name, node, exc); } @@ -228,14 +228,14 @@ static void _createComment(Request& r, c // CDATASection createCDATASection(in DOMString data) raises(DOMException); static void _createCDATASection(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& data=params->as_string(0, "data must be string"); GdomeException exc; GdomeNode *node=(GdomeNode *)gdome_doc_createCDATASection( vdoc.get_document(&method_name), - pool.transcode(data).get(), + pool.transcode(data).use(), &exc); writeNode(r, method_name, node, exc); } @@ -243,7 +243,7 @@ static void _createCDATASection(Request& // ProcessingInstruction createProcessingInstruction(in DOMString target,in DOMString data) raises(DOMException); static void _createProcessingInstruction(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& target=params->as_string(0, "data must be string"); const String& data=params->as_string(1, "data must be string"); @@ -251,8 +251,8 @@ static void _createProcessingInstruction GdomeException exc; GdomeNode *node=(GdomeNode *)gdome_doc_createProcessingInstruction( vdoc.get_document(&method_name), - pool.transcode(target).get(), - pool.transcode(data).get(), + pool.transcode(target).use(), + pool.transcode(data).use(), &exc); writeNode(r, method_name, node, exc); } @@ -260,35 +260,102 @@ static void _createProcessingInstruction // Attr createAttribute(in DOMString name) raises(DOMException); static void _createAttribute(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& name=params->as_string(0, "name must be string"); GdomeException exc; GdomeNode *node=(GdomeNode *)gdome_doc_createAttribute( vdoc.get_document(&method_name), - pool.transcode(name).get(), + pool.transcode(name).use(), &exc); writeNode(r, method_name, node, exc); } // EntityReference createEntityReference(in DOMString name) raises(DOMException); static void _createEntityReference(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); const String& name=params->as_string(0, "name must be string"); GdomeException exc; GdomeNode *node=(GdomeNode *)gdome_doc_createEntityReference( vdoc.get_document(&method_name), - pool.transcode(name).get(), + pool.transcode(name).use(), &exc); writeNode(r, method_name, node, exc); } +// NodeList getElementsByTagName(in DOMString name); +static void _getElementsByTagName(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + VXdoc& vdoc=*static_cast(r.get_self()); + + const String& name=params->as_string(0, "name must be string"); + + VHash& result=*new(pool) VHash(pool); + GdomeException exc; + if(GdomeNodeList *nodes= + gdome_doc_getElementsByTagName( + vdoc.get_document(&method_name), + pool.transcode(name).use(), + &exc)) { + gulong length=gdome_nl_length(nodes, &exc); + for(gulong i=0; i(r.get_self()); + + // namespaceURI;localName + const String& namespaceURI=params->as_string(0, "namespaceURI must be string"); + const String& localName=params->as_string(1, "localName must be string"); + + GdomeException exc; + VHash& result=*new(pool) VHash(pool); + if(GdomeNodeList *nodes= + gdome_doc_getElementsByTagNameNS( + vdoc.get_document(&method_name), + pool.transcode(namespaceURI).use(), + pool.transcode(localName).use(), + &exc)) { + gulong length=gdome_nl_length(nodes, &exc); + for(gulong i=0; i(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); // elementId const String& elementId=params->as_string(0, "elementID must be string"); @@ -296,7 +363,7 @@ static void _getElementById(Request& r, GdomeException exc; if(GdomeNode *node=(GdomeNode *)gdome_doc_getElementById( vdoc.get_document(&method_name), - pool.transcode(elementId).get(), + pool.transcode(elementId).use(), &exc)) { // write out result VXnode& result=*new(pool) VXnode(pool, node); @@ -309,7 +376,7 @@ static void _getElementById(Request& r, static void _importNode(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); GdomeNode *importedNode= as_node(method_name, params, 0, "importedNode must be node"); @@ -335,7 +402,7 @@ GdomeAttr *gdome_doc_createAttributeNS ( static void _create(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); Value& param=params->get(params->size()-1); GdomeDocument *document; @@ -372,7 +439,7 @@ static void _create(Request& r, const St */ document=gdome_di_createDocument (domimpl, 0/*namespaceURI*/, - pool.transcode(qualifiedName).get(), + pool.transcode(qualifiedName).use(), 0/*doctype*/, &exc); if(!document || exc || xmlHaveGenericErrors()) @@ -380,6 +447,10 @@ static void _create(Request& r, const St &method_name, exc); + xmlDoc *doc=gdome_xml_doc_get_xmlDoc(document); + const char *source_charset_name=pool.get_source_charset().name().cstr(); + doc->encoding=pool.get_source_charset().transcode_buf2xchar(source_charset_name, strlen(source_charset_name)); + /// +xalan createXMLDecl ? } @@ -400,20 +471,29 @@ static void _create(Request& r, const St static void _load(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); // filespec const String& file_name=params->as_string(0, "uri must be string"); const String& uri=r.absolute(file_name); - + + void *data; size_t size; + file_read(pool, uri, data, size, false/*not text*/, + params->size()>1?params->as_no_junction(1, "additional params must be hash").get_hash(&method_name) + :0); + GdomeDocument *document=(GdomeDocument *) - gdome_xml_n_mkref((xmlNode *)xmlParseFile(uri.cstr())); + gdome_xml_n_mkref((xmlNode *)xmlParseMemory((const char *)data, size)); if(!document || xmlHaveGenericErrors()) { GdomeException exc=0; throw Exception( &uri, exc); } + const char *URI_cstr=uri.cstr(); + xmlDoc *doc=gdome_xml_doc_get_xmlDoc(document); + if(URI_cstr) + doc->URL=pool.transcode_buf2xchar(URI_cstr, strlen(URI_cstr)); // replace any previous parsed source vdoc.set_document(document); @@ -443,6 +523,7 @@ static void param_option_over_output_opt } } +/// @test valid_options check static void prepare_output_options( Pool& pool, const String& method_name, MethodParams *params, int index, VXdoc::Output_options& oo) { @@ -536,7 +617,7 @@ static void xdoc2buf(Pool& pool, VXdoc& "xsltNewStylesheet failed"); #define OOS2STYLE(name) \ - stylesheet->name=oo.name?BAD_CAST g_strdup(pool.transcode(*oo.name)->str):0 + stylesheet->name=oo.name?BAD_CAST xmlMemStrdup(pool.transcode(*oo.name)->str):0 #define OOE2STYLE(name) \ stylesheet->name=oo.name @@ -581,7 +662,7 @@ static void xdoc2buf(Pool& pool, VXdoc& static void _file(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); VXdoc::Output_options oo(vdoc.output_options); char *buf; size_t buf_size; xdoc2buf(pool, vdoc, method_name, params, 0, @@ -603,13 +684,14 @@ static void _file(Request& r, const Stri new(pool) VString(*oo.encoding)); vcontent_type=vhcontent_type; - vfile.set(false/*tainted*/, buf, buf_size, 0/*file_name*/, vcontent_type); + vfile.set(false/*tainted*/, buf?buf:""/*to distinguish from stat-ed file*/, 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); + VXdoc& vdoc=*static_cast(r.get_self()); const String& file_spec=r.absolute(params->as_string(0, "file name must be string")); @@ -622,7 +704,7 @@ static void _save(Request& r, const Stri static void _string(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); VXdoc::Output_options oo(vdoc.output_options); char *buf; size_t buf_size; xdoc2buf(pool, vdoc, method_name, params, 0, @@ -692,13 +774,13 @@ static VXdoc& _transform(Pool& pool, con memset(&result.output_options, 0, sizeof(result.output_options)); VXdoc::Output_options& oo=result.output_options; - oo.method=stylesheet->method?&pool.transcode(stylesheet->method):0; - oo.encoding=stylesheet->encoding?&pool.transcode(stylesheet->encoding):0; - oo.mediaType=stylesheet->mediaType?&pool.transcode(stylesheet->mediaType):0; - oo.doctypeSystem=stylesheet->doctypeSystem?&pool.transcode(stylesheet->doctypeSystem):0; - oo.doctypePublic=stylesheet->doctypePublic?&pool.transcode(stylesheet->doctypePublic):0; + oo.method=stylesheet->method?&pool.transcode(stylesheet->method, stylesheet_source):0; + oo.encoding=stylesheet->encoding?&pool.transcode(stylesheet->encoding, stylesheet_source):0; + oo.mediaType=stylesheet->mediaType?&pool.transcode(stylesheet->mediaType, stylesheet_source):0; + oo.doctypeSystem=stylesheet->doctypeSystem?&pool.transcode(stylesheet->doctypeSystem, stylesheet_source):0; + oo.doctypePublic=stylesheet->doctypePublic?&pool.transcode(stylesheet->doctypePublic, stylesheet_source):0; oo.indent=stylesheet->indent!=0; - oo.version=stylesheet->version?&pool.transcode(stylesheet->version):0; + oo.version=stylesheet->version?&pool.transcode(stylesheet->version, stylesheet_source):0; oo.standalone=stylesheet->standalone!=0; oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration!=0; @@ -707,13 +789,13 @@ static VXdoc& _transform(Pool& pool, con } static void _transform(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); // params const char **transform_params=0; if(params->size()>1) { Value& vparams=params->as_no_junction(1, "transform parameters must be hash"); - if(vparams.is_defined()) + if(!vparams.is_string()) if(Hash *params=vparams.get_hash(&method_name)) { const char **current_transform_param=transform_params= (const char **)pool.malloc(sizeof(const char *)*(params->size()*2+1)); @@ -727,9 +809,9 @@ static void _transform(Request& r, const VXdoc *result; Value& vmaybe_xdoc=params->get(0); - if(strcmp(vmaybe_xdoc.type(), VXDOC_TYPE)==0) { // stylesheet (xdoc) + if(Value *vxdoc=vmaybe_xdoc.as(VXDOC_TYPE, false)) { // stylesheet (xdoc) xmlDoc *document=gdome_xml_doc_get_xmlDoc( - static_cast(&vmaybe_xdoc)->get_document(&method_name)); + static_cast(vxdoc)->get_document(&method_name)); // compile xdoc stylesheet xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(document)); // strange thing - xsltParseStylesheetDoc records document and destroys it in stylesheet destructor @@ -787,6 +869,8 @@ MXdoc::MXdoc(Pool& apool) : MXnode(apool add_native_method("createAttribute", Method::CT_DYNAMIC, _createAttribute, 1, 1); // EntityReference createEntityReference(in DOMString name) raises(DOMException); add_native_method("createEntityReference", Method::CT_DYNAMIC, _createEntityReference, 1, 1); + // NodeList getElementsByTagName(in DOMString name); + add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1); /// DOM2 @@ -796,6 +880,9 @@ MXdoc::MXdoc(Pool& apool) : MXnode(apool // Node (in Node importedNode, in boolean deep) raises(DOMException) add_native_method("importNode", Method::CT_DYNAMIC, _importNode, 2, 2); + // NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName); + add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2); + /// parser // ^xdoc::create{qualifiedName} @@ -806,7 +893,7 @@ MXdoc::MXdoc(Pool& apool) : MXnode(apool add_native_method("set", Method::CT_DYNAMIC, _create, 1, 1); // ^xdoc::load[some.xml] - add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1); + add_native_method("load", Method::CT_DYNAMIC, _load, 1, 2); // ^xdoc.save[some.xml] // ^xdoc.save[some.xml;options hash] @@ -820,8 +907,8 @@ MXdoc::MXdoc(Pool& apool) : MXnode(apool // ^xdoc.file[options hash] file with "" add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1); - // ^xdoc.transform[stylesheet file_name] - // ^xdoc.transform[stylesheet file_name;params hash] + // ^xdoc.transform[stylesheet file_name/xdoc] + // ^xdoc.transform[stylesheet file_name/xdoc;params hash] add_native_method("transform", Method::CT_DYNAMIC, _transform, 1, 2); }