--- parser3/src/classes/Attic/dom.C 2001/09/20 07:31:51 1.26 +++ parser3/src/classes/Attic/dom.C 2001/09/26 10:32:25 1.35 @@ -2,17 +2,13 @@ Parser: @b dom parser class. Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) -*/ -static const char *RCSId="$Id: dom.C,v 1.26 2001/09/20 07:31:51 parser Exp $"; - -#if _MSC_VER -# pragma warning(disable:4291) // disable warning -// "no matching operator delete found; memory will not be freed if initialization throws an exception -#endif + $Id: dom.C,v 1.35 2001/09/26 10:32:25 parser Exp $ +*/ #include "classes.h" +#ifdef XML + #include "pa_request.h" #include "pa_vdom.h" #include "pa_xslt_stylesheet_manager.h" @@ -71,7 +67,7 @@ protected: // XalanOutputStream virtual void writeData(const char *theBuffer, unsigned long theBufferLength) { char *copy=(char *)fstring.malloc((size_t)theBufferLength); memcpy(copy, theBuffer, (size_t)theBufferLength); - fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "dom", -1); + fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "dom", 0); } virtual void doFlush() {} @@ -86,30 +82,33 @@ static void create_optioned_listener( const char *& content_type, const char *& charset, FormatterListener *& listener, Pool& pool, const String& method_name, MethodParams *params, int index, Writer& writer) { - charset=0; + // default encoding from pool + const String *scharset=&pool.get_charset(); const String *method=0; XalanDOMString xalan_encoding; - Value& voptions=params->as_no_junction(index, "options must not be code"); - if(voptions.is_defined()) { - if(Hash *options=voptions.get_hash()) { - // $.method[xml|html|text] - if(Value *vmethod=static_cast(options->get(*new(pool) - String(pool, DOM_OUTPUT_METHOD_OPTION_NAME)))) - method=&vmethod->as_string(); - - // $.encoding[windows-1251|...] - if(Value *vencoding=static_cast(options->get(*new(pool) - String(pool, DOM_OUTPUT_ENCODING_OPTION_NAME)))) { - charset=vencoding->as_string().cstr(); - xalan_encoding.append(charset, strlen(charset)); - } - } else - PTHROW(0, 0, - &method_name, - "options must be hash"); + if(params->size()>index) { + Value& voptions=params->as_no_junction(index, "options must not be code"); + if(voptions.is_defined()) { + if(Hash *options=voptions.get_hash()) { + // $.method[xml|html|text] + if(Value *vmethod=static_cast(options->get(*new(pool) + String(pool, DOM_OUTPUT_METHOD_OPTION_NAME)))) + method=&vmethod->as_string(); + + // $.encoding[windows-1251|...] + if(Value *vencoding=static_cast(options->get(*new(pool) + String(pool, DOM_OUTPUT_ENCODING_OPTION_NAME)))) { + scharset=&vencoding->as_string(); + } + } else + PTHROW(0, 0, + &method_name, + "options must be hash"); + } } + xalan_encoding.append(charset=scharset->cstr()); if(!method/*default='xml'*/ || *method == DOM_OUTPUT_METHOD_OPTION_VALUE_XML) { content_type="text/xml"; listener=new FormatterToXML(writer, @@ -149,7 +148,7 @@ static void _save(Request& r, const Stri VDnode& vnode=*static_cast(r.self); // filespec - const String& file_name=params->as_string(1, "file name must not be code"); + const String& file_name=params->as_string(0, "file name must not be code"); const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC); // node @@ -161,11 +160,11 @@ static void _save(Request& r, const Stri const char *content_type, *charset; FormatterListener *formatterListener; create_optioned_listener(content_type, charset, formatterListener, - pool, method_name, params, 0, writer); + pool, method_name, params, 1, writer); FormatterTreeWalker treeWalker(*formatterListener); treeWalker.traverse(&node); // Walk that node and produce the XML... } catch(const XSLException& e) { - _throw(pool, &method_name, e); + r._throw(&method_name, e); } } @@ -190,7 +189,7 @@ static void _string(Request& r, const St // write out result r.write_no_lang(parserString); } catch(const XSLException& e) { - _throw(pool, &method_name, e); + r._throw(&method_name, e); } } @@ -229,7 +228,7 @@ static void _file(Request& r, const Stri vfile.set(false/*tainted*/, cstr, strlen(cstr), 0/*file_name*/, vcontent_type); r.write_no_lang(vfile); } catch(const XSLException& e) { - _throw(pool, &method_name, e); + r._throw(&method_name, e); } } @@ -242,7 +241,7 @@ static void _set(Request& r, const Strin const String& xml=r.process(vxml).as_string(); std::istrstream stream(xml.cstr()); - XalanParsedSource* parsedSource; + const XalanParsedSource* parsedSource; int error=vdom.transformer().parseSource(&stream, parsedSource); if(error) @@ -262,7 +261,7 @@ static void _load(Request& r, const Stri const String& file_name=params->as_string(0, "file name must not be code"); const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC); - XalanParsedSource* parsedSource; + const XalanParsedSource* parsedSource; int error=vdom.transformer().parseSource(filespec, parsedSource); if(error) @@ -301,7 +300,7 @@ static void _xslt(Request& r, const Stri } // source - XalanParsedSource &parsed_source=vdom.get_parsed_source(pool, &method_name); + const XalanParsedSource &parsed_source=vdom.get_parsed_source(pool, &method_name); // stylesheet const String& stylesheet_file_name=params->as_string(0, "file name must not be code"); @@ -335,14 +334,17 @@ static void _xslt(Request& r, const Stri MDom::MDom(Pool& apool) : MDnode(apool) { set_name(*NEW String(pool(), DOM_CLASS_NAME)); - // ^dom.save[options hash;some.xml] - add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2); + // ^dom.save[some.xml] + // ^dom.save[some.xml;options hash] + add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2); + // ^dom.string[] // ^dom.string[options hash] - add_native_method("string", Method::CT_DYNAMIC, _string, 1, 1); + add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1); + // ^dom.file[] file with "" // ^dom.file[options hash] file with "" - add_native_method("file", Method::CT_DYNAMIC, _file, 1, 1); + add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1); // ^dom::set[xml] add_native_method("set", Method::CT_DYNAMIC, _set, 1, 1); @@ -361,6 +363,14 @@ Methoded *Dom_class; // creator +#endif + Methoded *MDom_create(Pool& pool) { - return Dom_class=new(pool) MDom(pool); + return +#ifdef XML + Dom_class=new(pool) MDom(pool); +#else + 0 +#endif + ; }