|
|
| version 1.6, 2001/09/28 15:58:26 | version 1.41, 2012/03/16 09:24:10 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: stylesheet fstylesheet decl. | Parser: Stylesheet connection decl. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | |
| */ | */ |
| #ifndef PA_STYLESHEET_CONNECTION_H | #ifndef PA_STYLESHEET_CONNECTION_H |
| #define PA_STYLESHEET_CONNECTION_H | #define PA_STYLESHEET_CONNECTION_H |
| #include "pa_config_includes.h" | #define IDENT_PA_STYLESHEET_CONNECTION_H "$Id$" |
| #include "pa_pool.h" | |
| #include "pa_xslt_stylesheet_manager.h" | |
| #include "pa_exception.h" | |
| #include "pa_common.h" | |
| #include <XalanTransformer/XalanTransformer.hpp> | #include "libxslt/xslt.h" |
| #include "libxslt/xsltInternals.h" | |
| // defines | |
| #define STYLESHEET_FILENAME_STAMP_SUFFIX ".stamp" | #include "pa_xml_exception.h" |
| #include "pa_common.h" | |
| #include "pa_globals.h" | |
| #include "pa_xml_io.h" | |
| // defines | |
| /** Connection with stylesheet: | /** Connection with stylesheet: |
| remembers time and can figure out that it needs recompilation | remembers time and can figure out that it needs recompilation |
| */ | */ |
| class Stylesheet_connection : public Pooled { | class Stylesheet_connection: public PA_Object { |
| public: | friend class Stylesheet_connection_ptr; |
| Stylesheet_connection(Pool& pool, const String& afile_spec) : Pooled(pool), | private: |
| ftransformer(new XalanTransformer), | |
| String::Body ffile_spec; | |
| xsltStylesheet *fstylesheet; | |
| HashStringBool* dependencies; | |
| time_t time_used; | |
| time_t prev_disk_time; | |
| public: | |
| Stylesheet_connection(String::Body afile_spec): | |
| ffile_spec(afile_spec), | ffile_spec(afile_spec), |
| fstylesheet(0), | |
| dependencies(0), | |
| time_used(0), | time_used(0), |
| prev_disk_time(0), | prev_disk_time(0), |
| fservices_pool(0), | used(0) |
| fstylesheet(0) { | {} |
| } | |
| const String& file_spec() { return ffile_spec; } | String::Body file_spec() { return ffile_spec; } |
| void set_services(Pool *aservices_pool) { | bool uncachable() { |
| time_used=time(0); // they started to use at this time | return !dependencies/*means they were external*/; |
| fservices_pool=aservices_pool; | |
| } | |
| bool expired(time_t older_dies) { | |
| return time_used<older_dies; | |
| } | } |
| void close() { | bool expired(time_t older_dies) { |
| XSLT_stylesheet_manager->close_connection(ffile_spec, *this); | return uncachable() || !used && time_used<older_dies; |
| } | } |
| time_t get_time_used() { return time_used; } | |
| void disconnect() { | void disconnect() { |
| /*ignore error*/ftransformer->destroyStylesheet(fstylesheet); fstylesheet=0; | xsltFreeStylesheet(fstylesheet); fstylesheet=0; |
| // connection effectively useless now, free up some memory | |
| delete ftransformer; | |
| } | } |
| bool connected() { return fstylesheet!=0; } | bool connected() { return fstylesheet!=0; } |
| const XalanCompiledStylesheet& stylesheet(bool nocache) { | xsltStylesheet *stylesheet() { |
| time_t new_disk_time=get_new_disk_time(); | time_t new_disk_time=0; |
| if(nocache || new_disk_time) | if(uncachable() || (new_disk_time=get_new_disk_time())) |
| load(new_disk_time); | load(new_disk_time); |
| return *fstylesheet; | return fstylesheet; |
| } | } |
| private: | private: |
| /// return to cache | |
| void close(); | |
| private: | |
| time_t get_new_disk_time() { | time_t get_new_disk_time() { |
| time_t now_disk_time=get_disk_time(); | time_t now_disk_time=get_disk_time(); |
| return now_disk_time>prev_disk_time?now_disk_time:0; | return now_disk_time>prev_disk_time?now_disk_time:0; |
| } | } |
| void load(time_t new_disk_time) { | void load(time_t new_disk_time); |
| Pool& pool=*fservices_pool; | time_t get_disk_time(); |
| int error=ftransformer->compileStylesheet(ffile_spec.cstr(String::UL_FILE_SPEC), fstylesheet); | private: // connection usage methods |
| if(error) | |
| PTHROW(0, 0, | |
| &ffile_spec, | |
| ftransformer->getLastError()); | |
| prev_disk_time=new_disk_time; | |
| } | |
| time_t get_disk_time() { | |
| size_t size; | |
| time_t atime, mtime, ctime; | |
| 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; | |
| if(!file_stat(stat_file_spec, | |
| size, | |
| atime, mtime, ctime, | |
| false/*no exception on global pool[stat_file_spec], please*/)) | |
| mtime=1; //no file=pseudo non-zero time, see get_new_disk_time | |
| return mtime; | |
| } | |
| private: | |
| const String& ffile_spec; | int used; |
| const XalanCompiledStylesheet *fstylesheet; | void use() { |
| time_t time_used; | time_used=time(0); // they started to use at this time |
| time_t prev_disk_time; | used++; |
| } | |
| void unuse() { | |
| used--; | |
| if(!used) | |
| close(); | |
| } | |
| private: | }; |
| XalanTransformer *ftransformer; | /// 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; | |
| } | |
| /* 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(); | |
| Pool *fservices_pool; | return *this; |
| } | |
| }; | }; |
| #endif | #endif |