--- parser3/src/include/pa_stylesheet_connection.h 2001/10/22 16:44:42 1.14 +++ parser3/src/include/pa_stylesheet_connection.h 2002/02/08 07:27:45 1.25 @@ -1,23 +1,23 @@ /** @file Parser: Stylesheet connection decl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://paf.design.ru) - $Id: pa_stylesheet_connection.h,v 1.14 2001/10/22 16:44:42 parser Exp $ + $Id: pa_stylesheet_connection.h,v 1.25 2002/02/08 07:27:45 paf Exp $ */ #ifndef PA_STYLESHEET_CONNECTION_H #define PA_STYLESHEET_CONNECTION_H -#include "pa_config_includes.h" +#include "libxslt/xslt.h" +#include "libxslt/xsltInternals.h" + #include "pa_pool.h" #include "pa_stylesheet_manager.h" #include "pa_exception.h" #include "pa_common.h" -#include "XalanTransformer2.hpp" - // defines #define STYLESHEET_FILENAME_STAMP_SUFFIX ".stamp" @@ -27,46 +27,46 @@ */ class Stylesheet_connection : public Pooled { + friend class Stylesheet_connection_ptr; + public: Stylesheet_connection(Pool& pool, const String& afile_spec) : Pooled(pool), - ftransformer(new XalanTransformer2), - ffile_spec(afile_spec), - time_used(0), prev_disk_time(0), fservices_pool(0), - fstylesheet(0) { + fstylesheet(0), + time_used(0), used(0) { } const String& file_spec() { return ffile_spec; } void set_services(Pool *aservices_pool) { - time_used=time(0); // they started to use at this time fservices_pool=aservices_pool; } bool expired(time_t older_dies) { - return time_usedclose_connection(ffile_spec, *this); + return !used && time_useddestroyStylesheet(fstylesheet); fstylesheet=0; - - // connection effectively useless now, free up some memory - delete ftransformer; + xsltFreeStylesheet(fstylesheet); fstylesheet=0; } bool connected() { return fstylesheet!=0; } - const XalanCompiledStylesheet& stylesheet(bool nocache) { + xsltStylesheet *stylesheet(bool nocache) { time_t new_disk_time=get_new_disk_time(); if(nocache || new_disk_time) load(new_disk_time); - return *fstylesheet; + return fstylesheet; + } + +private: + + /// return to cache + void close() { + stylesheet_manager->close_connection(ffile_spec, *this); } private: @@ -79,28 +79,17 @@ private: void load(time_t new_disk_time) { Pool& pool=*fservices_pool; - try{ - const XalanCompiledStylesheet *nstylesheet; - ftransformer->compileStylesheet2(ffile_spec.cstr(String::UL_FILE_SPEC), nstylesheet); - ftransformer->destroyStylesheet(fstylesheet); - fstylesheet=nstylesheet; - } - catch (XSLException& e) { - Exception::provide_source(pool, &ffile_spec, e); - } - catch (SAXParseException& e) { - Exception::provide_source(pool, &ffile_spec, e); - } - catch (SAXException& e) { - Exception::provide_source(pool, &ffile_spec, e); - } - catch (XMLException& e) { - Exception::provide_source(pool, &ffile_spec, e); - } - catch(const XalanDOMException& e) { - Exception::provide_source(pool, &ffile_spec, e); + xsltStylesheet *nstylesheet= + xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::UL_FILE_SPEC)); + if(!nstylesheet || xmlHaveGenericErrors()) { + GdomeException exc=0; + throw Exception(0, 0, + &ffile_spec, + exc); } + xsltFreeStylesheet(fstylesheet); + fstylesheet=nstylesheet; prev_disk_time=new_disk_time; } @@ -118,17 +107,31 @@ private: return mtime; } +private: // connection usage methods + + void use() { + time_used=time(0); // they started to use at this time + used++; + } + void unuse() { + used--; + if(!used) + close(); + } + +private: // connection usage data + + int used; + private: const String& ffile_spec; - const XalanCompiledStylesheet *fstylesheet; + xsltStylesheet *fstylesheet; time_t time_used; time_t prev_disk_time; private: - XalanTransformer2 *ftransformer; - Pool *fservices_pool; private: @@ -136,4 +139,33 @@ private: void *calloc(size_t size) { return fservices_pool->calloc(size); } }; +/// Auto-object used to track Stylesheet_connection usage +class Stylesheet_connection_ptr { + Stylesheet_connection *fconnection; +public: + explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : + fconnection(aconnection) { + fconnection->use(); + } + ~Stylesheet_connection_ptr() { + fconnection->unuse(); + } + Stylesheet_connection* operator->() { + return fconnection; + } + + // copying + Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) { + fconnection->use(); + } + Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) { + // may do without this=src check + fconnection->unuse(); + fconnection=src.fconnection; + fconnection->use(); + + return *this; + } +}; + #endif