Annotation of parser3/src/include/pa_stylesheet_connection.h, revision 1.2

1.1       parser      1: /** @file
                      2:        Parser: stylesheet fstylesheet decl.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5: 
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.2     ! parser      8:        $Id: pa_stylesheet_connection.h,v 1.1 2001/09/14 15:41:59 parser Exp $
1.1       parser      9: */
                     10: 
                     11: #ifndef PA_STYLESHEET_CONNECTION_H
                     12: #define PA_STYLESHEET_CONNECTION_H
                     13: 
                     14: #include "pa_config_includes.h"
                     15: #include "pa_pool.h"
                     16: #include "pa_xslt_stylesheet_manager.h"
                     17: #include "pa_exception.h"
                     18: #include "pa_common.h"
                     19: 
                     20: #include <XalanTransformer/XalanTransformer.hpp>
                     21: 
                     22: // defines
                     23: 
                     24: #define STYLESHEET_FILENAME_STAMP_SUFFIX ".stamp"
                     25: 
                     26: /**    Connection with stylesheet: 
                     27:        remembers time and can figure out that it needs recompilation
                     28: */
                     29: class Stylesheet_connection : public Pooled {
                     30: 
                     31: public:
                     32: 
                     33:        Stylesheet_connection(Pool& pool, const String& afile_spec) : Pooled(pool),
                     34:                ftransformer(new XalanTransformer),
                     35: 
                     36:                ffile_spec(afile_spec),
                     37:                time_used(0),
                     38:                prev_disk_time(0),
                     39:                fservices_pool(0) {
                     40:        }
                     41:        
                     42:        const String& file_spec() { return ffile_spec; }
                     43: 
                     44:        void set_services(Pool *aservices_pool) {
                     45:                time_used=time(0); // they started to use at this time
                     46:                fservices_pool=aservices_pool;
                     47:        }
                     48:        bool expired(time_t older_dies) {
                     49:                return time_used<older_dies;
                     50:        }
                     51: 
                     52:        void close() {
                     53:                XSLT_stylesheet_manager->close_connection(ffile_spec, *this);
                     54:        }
                     55: 
                     56:        void disconnect() { 
                     57:                /*ignore error*/ftransformer->destroyStylesheet(fstylesheet);  fstylesheet=0; 
1.2     ! parser     58: 
        !            59:                // connection effectively useless now, free up some memory
        !            60:                delete ftransformer; 
1.1       parser     61:        }
                     62: 
                     63:        bool connected() { return fstylesheet!=0; }
                     64: 
                     65:        XalanCompiledStylesheet& stylesheet() { 
                     66:                if(time_t new_disk_time=get_new_disk_time())
                     67:                        load(new_disk_time);
                     68:                return *fstylesheet; 
                     69:        }
                     70: 
                     71: private:
                     72: 
                     73:        time_t get_new_disk_time() {
                     74:                time_t now_disk_time=get_disk_time();
                     75:                return now_disk_time>prev_disk_time?now_disk_time:0;
                     76:        }
                     77: 
                     78:        void load(time_t new_disk_time) {
                     79:                Pool& pool=*fservices_pool;
                     80: 
                     81:                int error=ftransformer->compileStylesheet(ffile_spec.cstr(String::UL_FILE_SPEC), fstylesheet);
                     82:                if(error)
                     83:                        PTHROW(0, 0,
                     84:                                &ffile_spec,
                     85:                                ftransformer->getLastError());
                     86: 
                     87:                prev_disk_time=new_disk_time;
                     88:        }
                     89: 
                     90:        time_t get_disk_time() {
                     91:                size_t size;
                     92:                time_t atime, mtime, ctime;
                     93:                String stamp_file_spec(ffile_spec); 
                     94:                stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX;
                     95:                // {file_spec}.stamp modification time OR {file_spec} mtime 
                     96:                if(!file_stat(file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec, 
                     97:                        size,
                     98:                        atime, mtime, ctime,
                     99:                        false/*no exceptions on global pool, please*/))
                    100:                        mtime=0; // no file=no time
                    101:                return mtime;
                    102:        }
                    103: 
                    104: private:
                    105: 
                    106:        const String& ffile_spec;
                    107:        XalanCompiledStylesheet *fstylesheet;
                    108:        time_t time_used;
                    109:        time_t prev_disk_time;
                    110: 
                    111: private:
                    112: 
                    113:        XalanTransformer *ftransformer;
                    114: 
                    115:        Pool *fservices_pool;
                    116: };
                    117: 
                    118: #endif

E-mail: