--- parser3/src/classes/xdoc.C 2002/06/25 15:08:09 1.93 +++ parser3/src/classes/xdoc.C 2002/09/20 13:09:49 1.103 @@ -3,12 +3,13 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - - $Id: xdoc.C,v 1.93 2002/06/25 15:08:09 paf Exp $ */ + #include "classes.h" #ifdef XML +static const char* IDENT_XDOC_C="$Date: 2002/09/20 13:09:49 $"; + #include "pa_stylesheet_connection.h" #include "pa_request.h" #include "pa_vxdoc.h" @@ -169,7 +170,7 @@ 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"); @@ -184,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= @@ -197,7 +198,7 @@ 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"); @@ -212,7 +213,7 @@ 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"); @@ -227,7 +228,7 @@ 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"); @@ -242,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"); @@ -259,7 +260,7 @@ 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"); @@ -273,7 +274,7 @@ static void _createAttribute(Request& r, // 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"); @@ -285,9 +286,76 @@ static void _createEntityReference(Reque 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).get(), + &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).get(), + pool.transcode(localName).get(), + &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"); @@ -308,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"); @@ -332,15 +400,13 @@ GdomeElement *gdome_doc_createElementNS GdomeAttr *gdome_doc_createAttributeNS (GdomeDocument *self, GdomeDOMString *namespaceURI, GdomeDOMString *qualifiedName, GdomeException *exc); */ - static void _create(Request& r, const String& method_name, MethodParams *params) { - //_asm int 3; Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.self); + VXdoc& vdoc=*static_cast(r.get_self()); - Value& param=params->get(0); + Value& param=params->get(params->size()-1); GdomeDocument *document; - if(param.get_junction()) { // {} + if(param.get_junction()) { // {...} Temp_lang temp_lang(r, String::UL_XML); const String& xml=r.process_to_string(param); @@ -381,15 +447,31 @@ 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 ? } + + // URI + const char *URI_cstr; + if(params->size()>1) { // absolute(param) + const String& URI=params->as_string(0, "URI must be string"); + URI_cstr=r.absolute(URI).cstr(); + } else // default = disk path to requested document + URI_cstr=r.info.path_translated; + 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); } 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"); @@ -432,6 +514,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) { @@ -570,7 +653,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, @@ -592,13 +675,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")); @@ -611,7 +695,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, @@ -696,7 +780,7 @@ 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; @@ -716,11 +800,14 @@ 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)); + xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(document)); + // strange thing - xsltParseStylesheetDoc records document and destroys it in stylesheet destructor + // we don't need that + stylesheet_ptr->doc=0; if(xmlHaveGenericErrors()) { GdomeException exc=0; throw Exception(&method_name, exc); @@ -773,6 +860,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 @@ -782,11 +871,15 @@ 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} - // ^xdoc::_create[xml] - add_native_method("create", Method::CT_DYNAMIC, _create, 1, 1); + // ^xdoc::create[xml] + // ^xdoc::create[URI][xml] + add_native_method("create", Method::CT_DYNAMIC, _create, 1, 2); // for backward compatibility with <=v 1.82 2002/01/31 11:51:46 paf add_native_method("set", Method::CT_DYNAMIC, _create, 1, 1); @@ -805,8 +898,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); }