--- parser3/src/classes/xdoc.C 2002/04/18 15:54:39 1.92 +++ 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.92 2002/04/18 15:54:39 paf Exp $ + $Id: xdoc.C,v 1.95 2002/06/28 09:59:00 paf Exp $ */ #include "classes.h" #ifdef XML @@ -332,15 +332,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); - 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); @@ -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); } @@ -630,34 +640,9 @@ static void add_xslt_param(const Hash::K *current_transform_param++=pool.transcode(aattribute)->str; *current_transform_param++=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)) { - 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"); - } - - // 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); - - // prepare to transform - xsltStylesheet *stylesheet=connection->stylesheet(false/*nocache*/); - xmlDoc *document=gdome_xml_doc_get_xmlDoc(vdoc.get_document(&method_name)); +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 @@ -675,7 +660,7 @@ static void _transform(Request& r, const if(!transformed || xmlHaveGenericErrors()) { GdomeException exc=0; throw Exception( - &stylesheet_filespec, + stylesheet_source, exc); } @@ -685,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 @@ -716,8 +701,67 @@ static void _transform(Request& r, const oo.standalone=stylesheet->standalone!=0; oo.omitXmlDeclaration=stylesheet->omitXmlDeclaration!=0; + // return + return result; +} +static void _transform(Request& r, const String& method_name, MethodParams *params) { + 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)) { + 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); + } + // write out result - r.write_no_lang(result); + r.write_no_lang(*result); } // constructor @@ -754,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);