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

1.1       parser      1: /** @file
1.7       parser      2:        Parser: Stylesheet connection decl.
1.1       parser      3: 
1.25    ! paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.16      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: 
1.25    ! paf         7:        $Id: pa_stylesheet_connection.h,v 1.24 2002/01/23 12:19:56 paf Exp $
1.1       parser      8: */
                      9: 
                     10: #ifndef PA_STYLESHEET_CONNECTION_H
                     11: #define PA_STYLESHEET_CONNECTION_H
                     12: 
1.22      paf        13: #include "libxslt/xslt.h"
1.18      paf        14: #include "libxslt/xsltInternals.h"
                     15: 
1.1       parser     16: #include "pa_pool.h"
1.14      parser     17: #include "pa_stylesheet_manager.h"
1.1       parser     18: #include "pa_exception.h"
                     19: #include "pa_common.h"
1.9       parser     20: 
1.1       parser     21: // defines
                     22: 
                     23: #define STYLESHEET_FILENAME_STAMP_SUFFIX ".stamp"
                     24: 
                     25: /**    Connection with stylesheet: 
                     26:        remembers time and can figure out that it needs recompilation
                     27: */
                     28: class Stylesheet_connection : public Pooled {
                     29: 
1.19      paf        30:        friend class Stylesheet_connection_ptr;
                     31: 
1.1       parser     32: public:
                     33: 
                     34:        Stylesheet_connection(Pool& pool, const String& afile_spec) : Pooled(pool),
                     35:                ffile_spec(afile_spec),
                     36:                prev_disk_time(0),
1.3       parser     37:                fservices_pool(0),
1.19      paf        38:                fstylesheet(0),
                     39:                time_used(0), used(0) {
1.1       parser     40:        }
                     41:        
                     42:        const String& file_spec() { return ffile_spec; }
                     43: 
                     44:        void set_services(Pool *aservices_pool) {
                     45:                fservices_pool=aservices_pool;
                     46:        }
                     47:        bool expired(time_t older_dies) {
1.19      paf        48:                return !used && time_used<older_dies;
1.1       parser     49:        }
1.15      paf        50:        time_t get_time_used() { return time_used; }
1.1       parser     51: 
                     52:        void disconnect() { 
1.18      paf        53:                xsltFreeStylesheet(fstylesheet);  fstylesheet=0; 
1.1       parser     54:        }
                     55: 
                     56:        bool connected() { return fstylesheet!=0; }
                     57: 
1.18      paf        58:        xsltStylesheet *stylesheet(bool nocache) { 
1.6       parser     59:                time_t new_disk_time=get_new_disk_time();
                     60:                if(nocache || new_disk_time)
1.1       parser     61:                        load(new_disk_time);
1.18      paf        62:                return fstylesheet; 
1.1       parser     63:        }
                     64: 
                     65: private:
                     66: 
1.19      paf        67:        /// return to cache
                     68:        void close() {
                     69:                stylesheet_manager->close_connection(ffile_spec, *this);
                     70:        }
                     71: 
                     72: private:
                     73: 
1.1       parser     74:        time_t get_new_disk_time() {
                     75:                time_t now_disk_time=get_disk_time();
                     76:                return now_disk_time>prev_disk_time?now_disk_time:0;
                     77:        }
                     78: 
                     79:        void load(time_t new_disk_time) {
                     80:                Pool& pool=*fservices_pool;
                     81: 
1.24      paf        82:                xsltStylesheet *nstylesheet=
                     83:                        xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::UL_FILE_SPEC));
1.21      paf        84:                if(!nstylesheet || xmlHaveGenericErrors()) {
1.20      paf        85:                        GdomeException exc=0;
1.18      paf        86:                        throw Exception(0, 0,
                     87:                                &ffile_spec,
1.20      paf        88:                                exc);
                     89:                }
1.1       parser     90: 
1.18      paf        91:                xsltFreeStylesheet(fstylesheet);  
                     92:                fstylesheet=nstylesheet;
1.1       parser     93:                prev_disk_time=new_disk_time;
                     94:        }
                     95: 
                     96:        time_t get_disk_time() {
                     97:                size_t size;
                     98:                time_t atime, mtime, ctime;
                     99:                String stamp_file_spec(ffile_spec); 
                    100:                stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX;
1.3       parser    101:                // {file_spec}.stamp modification time OR {file_spec}
                    102:                const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec;
1.14      parser    103:                file_stat(stat_file_spec, 
1.1       parser    104:                        size,
                    105:                        atime, mtime, ctime,
1.14      parser    106:                        true/*exception on error*/);
1.1       parser    107:                return mtime;
                    108:        }
                    109: 
1.19      paf       110: private: // connection usage methods
                    111: 
                    112:        void use() {
                    113:                time_used=time(0); // they started to use at this time
                    114:                used++;
                    115:        }
                    116:        void unuse() {
                    117:                used--;
                    118:                if(!used)
                    119:                        close();
                    120:        }
                    121: 
                    122: private: // connection usage data
                    123: 
                    124:        int used;
                    125: 
1.1       parser    126: private:
                    127: 
                    128:        const String& ffile_spec;
1.18      paf       129:        xsltStylesheet *fstylesheet;
1.1       parser    130:        time_t time_used;
                    131:        time_t prev_disk_time;
                    132: 
                    133: private:
                    134: 
                    135:        Pool *fservices_pool;
1.14      parser    136: 
                    137: private:
                    138:        void *malloc(size_t size) { return fservices_pool->malloc(size); }
                    139:        void *calloc(size_t size) { return fservices_pool->calloc(size); }
1.19      paf       140: };
                    141: 
                    142: /// Auto-object used to track Stylesheet_connection usage
                    143: class Stylesheet_connection_ptr {
                    144:        Stylesheet_connection *fconnection;
                    145: public:
                    146:        explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : 
                    147:                fconnection(aconnection) {
                    148:                fconnection->use();
                    149:        }
                    150:        ~Stylesheet_connection_ptr() {
                    151:                fconnection->unuse();
                    152:        }
                    153:        Stylesheet_connection* operator->() {
                    154:                return fconnection;
                    155:        }
                    156: 
                    157:        // copying
                    158:        Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
                    159:                fconnection->use();
                    160:        }
                    161:        Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
                    162:                // may do without this=src check
                    163:                fconnection->unuse();
                    164:                fconnection=src.fconnection;
                    165:                fconnection->use();
                    166: 
                    167:                return *this;
                    168:        }
1.1       parser    169: };
                    170: 
                    171: #endif

E-mail: