--- parser3/src/classes/xdoc.C 2002/03/27 15:30:34 1.87 +++ parser3/src/classes/xdoc.C 2002/06/28 09:59:00 1.95 @@ -4,7 +4,7 @@ Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) - $Id: xdoc.C,v 1.87 2002/03/27 15:30:34 paf Exp $ + $Id: xdoc.C,v 1.95 2002/06/28 09:59:00 paf Exp $ */ #include "classes.h" #ifdef XML @@ -25,6 +25,7 @@ extern "C" { #include "libxslt/xsltInternals.h" #include "libxslt/transform.h" #include "libxslt/xsltutils.h" +#include "libxslt/variables.h" // defines @@ -45,7 +46,6 @@ public: MXdoc(Pool& pool); public: // Methoded - bool used_directly() { return true; } void configure_admin(Request& r); }; @@ -311,7 +311,7 @@ static void _importNode(Request& r, cons VXdoc& vdoc=*static_cast(r.self); GdomeNode *importedNode= - as_node(pool, method_name, params, 0, "importedNode must be node"); + as_node(method_name, params, 0, "importedNode must be node"); bool deep= params->as_bool(1, "deep must be bool", r); @@ -332,17 +332,15 @@ 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); - 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(param).as_string(); + const String& xml=r.process_to_string(param); const char *cstr=xml.cstr(String::UL_UNSPECIFIED); document=(GdomeDocument *) @@ -383,6 +381,18 @@ static void _create(Request& r, const St /// +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); } @@ -626,51 +636,31 @@ static void add_xslt_param(const Hash::K void *info) { Value *meaning=static_cast(ameaning); Pool& pool=meaning->pool(); - const char **transform_params=(const char **)info; - *transform_params++=pool.transcode(aattribute)->str; - *transform_params++=pool.transcode(meaning->as_string())->str; -} -static void _transform(Request& r, const String& method_name, MethodParams *params) { - //_asm int 3; - Pool& pool=r.pool(); - VXdoc& vdoc=*static_cast(r.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(Hash *params=vparams.get_hash(&method_name)) { - transform_params= - (const char **)pool.malloc(sizeof(const char *)*params->size()*2+1); - params->for_each(add_xslt_param, transform_params); - transform_params[params->size()*2]=0; - } else - throw Exception("parser.runtime", - &method_name, - "transform parameters parameter must be hash"); - } - - // stylesheet - const String& stylesheet_filespec=r.absolute(params->as_string(0, "file name must be string")); - Stylesheet_connection_ptr connection=stylesheet_manager->get_connection(stylesheet_filespec); - - // transform - xsltStylesheet *stylesheet=connection->stylesheet(false/*nocache*/); - xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document(&method_name)); + const char ** & current_transform_param=*(const char ***)info; + *current_transform_param++=pool.transcode(aattribute)->str; + *current_transform_param++=pool.transcode(meaning->as_string())->str; +} +static VXdoc& _transform(Pool& pool, const String *doc_source, const String *stylesheet_source, + VXdoc& vdoc, xsltStylesheetPtr stylesheet, const char **transform_params) { + xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document(doc_source)); xsltTransformContext_auto_ptr transformContext( xsltNewTransformContext(stylesheet, document)); + // make params literal + if (transformContext->globalVars == NULL) // strangly not initialized by xsltNewTransformContext + transformContext->globalVars = xmlHashCreate(20); + xsltQuoteUserParams(transformContext.get(), transform_params); + // do transform xmlDoc *transformed=xsltApplyStylesheetUser( stylesheet, document, - transform_params, + 0/*already quoted-inserted transform_params*/, 0/*const char *output*/, 0/*FILE *profile*/, transformContext.get()); if(!transformed || xmlHaveGenericErrors()) { GdomeException exc=0; throw Exception( - &stylesheet_filespec, + stylesheet_source, exc); } @@ -680,7 +670,7 @@ static void _transform(Request& r, const GdomeDocument *gdomeDocument=gdome_xml_doc_mkref(transformed); if(!gdomeDocument) throw Exception(0, - &method_name, + doc_source, "gdome_xml_doc_mkref failed"); VXdoc& result=*new(pool) VXdoc(pool, gdomeDocument); /* grabbing options @@ -711,17 +701,73 @@ static void _transform(Request& r, const oo.standalone=stylesheet->standalone!=0; oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration!=0; - // write out result - r.write_no_lang(result); + // return + return result; } +static void _transform(Request& r, const String& method_name, MethodParams *params) { + Pool& pool=r.pool(); + VXdoc& vdoc=*static_cast(r.self); -// constructor + // 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(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)); + params->for_each(add_xslt_param, ¤t_transform_param); + transform_params[params->size()*2]=0; + } else + throw Exception("parser.runtime", + &method_name, + "transform parameters parameter must be hash"); + } + + VXdoc *result; + Value& vmaybe_xdoc=params->get(0); + if(strcmp(vmaybe_xdoc.type(), VXDOC_TYPE)==0) { // stylesheet (xdoc) + xmlDoc *document=gdome_xml_doc_get_xmlDoc( + static_cast(&vmaybe_xdoc)->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 + // we don't need that + stylesheet_ptr->doc=0; + if(xmlHaveGenericErrors()) { + GdomeException exc=0; + throw Exception(&method_name, exc); + } + if(!stylesheet_ptr.get()) + throw Exception("xml", + &method_name, + "stylesheet failed to compile"); + + // transform! + result=&_transform(pool, &method_name, &method_name, + vdoc, stylesheet_ptr.get(), + transform_params); + } else { // stylesheet (file name) + // extablish stylesheet connection + const String& stylesheet_filespec= + 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); + + // load and compile file to stylesheet [or get cached if any] + // transform! + result=&_transform(pool, &method_name, &stylesheet_filespec, + vdoc, connection->stylesheet(false/*nocache*/), + transform_params); + } -MXdoc::MXdoc(Pool& apool) : MXnode(apool) { - set_name(*NEW String(pool(), XDOC_CLASS_NAME)); + // write out result + r.write_no_lang(*result); +} - /// @test how to create empty type html? +// constructor +/// @test how to create empty type html? +MXdoc::MXdoc(Pool& apool) : MXnode(apool, XDOC_CLASS_NAME, Xnode_class) { /// DOM1 // Element createElement(in DOMString tagName) raises(DOMException); @@ -752,8 +798,9 @@ MXdoc::MXdoc(Pool& apool) : MXnode(apool /// 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);