--- parser3/src/include/pa_stylesheet_connection.h 2002/01/14 17:48:56 1.18 +++ parser3/src/include/pa_stylesheet_connection.h 2003/01/21 15:51:11 1.32 @@ -1,16 +1,16 @@ /** @file Parser: Stylesheet connection decl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) - - $Id: pa_stylesheet_connection.h,v 1.18 2002/01/14 17:48:56 paf Exp $ + Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_STYLESHEET_CONNECTION_H #define PA_STYLESHEET_CONNECTION_H -#include "libxslt/libxslt.h" +static const char* IDENT_STYLESHEET_CONNECTION_H="$Date: 2003/01/21 15:51:11 $"; + +#include "libxslt/xslt.h" #include "libxslt/xsltInternals.h" #include "pa_pool.h" @@ -27,31 +27,28 @@ */ class Stylesheet_connection : public Pooled { + friend class Stylesheet_connection_ptr; + public: Stylesheet_connection(Pool& pool, const String& afile_spec) : Pooled(pool), 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); - } - void disconnect() { xsltFreeStylesheet(fstylesheet); fstylesheet=0; } @@ -67,21 +64,34 @@ public: private: + /// return to cache + void close() { + stylesheet_manager->close_connection(ffile_spec, *this); + } + +private: + time_t get_new_disk_time() { time_t now_disk_time=get_disk_time(); return now_disk_time>prev_disk_time?now_disk_time:0; } - /// @test grab errors void load(time_t new_disk_time) { Pool& pool=*fservices_pool; - xsltStylesheet *nstylesheet; - nstylesheet=xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::UL_FILE_SPEC)); + int saved=xmlDoValidityCheckingDefaultValue;// + xmlDoValidityCheckingDefaultValue=0;// + xsltStylesheet *nstylesheet= + xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::UL_FILE_SPEC)); + xmlDoValidityCheckingDefaultValue = saved;// + if(xmlHaveGenericErrors()) { + GdomeException exc=0; + throw Exception(&ffile_spec, exc); + } if(!nstylesheet) - throw Exception(0, 0, + throw Exception("file.missing", &ffile_spec, - "error compiling. TODO: grab errors"); + "stylesheet failed to load"); xsltFreeStylesheet(fstylesheet); fstylesheet=nstylesheet; @@ -91,7 +101,7 @@ private: time_t get_disk_time() { size_t size; time_t atime, mtime, ctime; - String stamp_file_spec(ffile_spec); + String stamp_file_spec(ffile_spec); stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX; // {file_spec}.stamp modification time OR {file_spec} const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec; @@ -102,6 +112,22 @@ 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; @@ -118,4 +144,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