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

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.26      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: */
                      7: 
                      8: #ifndef PA_STYLESHEET_CONNECTION_H
                      9: #define PA_STYLESHEET_CONNECTION_H
1.29    ! paf        10: 
        !            11: static const char* IDENT_STYLESHEET_CONNECTION_H="$Id: zzz $";
1.1       parser     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.28      paf        84:                if(xmlHaveGenericErrors()) {
1.20      paf        85:                        GdomeException exc=0;
1.28      paf        86:                        throw Exception(&ffile_spec, exc);
                     87:                }
                     88:                if(!nstylesheet)
                     89:                        throw Exception("file.missing",
1.18      paf        90:                                &ffile_spec,
1.28      paf        91:                                "stylesheet failed to load");
1.1       parser     92: 
1.18      paf        93:                xsltFreeStylesheet(fstylesheet);  
                     94:                fstylesheet=nstylesheet;
1.1       parser     95:                prev_disk_time=new_disk_time;
                     96:        }
                     97: 
                     98:        time_t get_disk_time() {
                     99:                size_t size;
                    100:                time_t atime, mtime, ctime;
1.27      paf       101:                String stamp_file_spec(ffile_spec);
1.1       parser    102:                stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX;
1.3       parser    103:                // {file_spec}.stamp modification time OR {file_spec}
                    104:                const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec;
1.14      parser    105:                file_stat(stat_file_spec, 
1.1       parser    106:                        size,
                    107:                        atime, mtime, ctime,
1.14      parser    108:                        true/*exception on error*/);
1.1       parser    109:                return mtime;
                    110:        }
                    111: 
1.19      paf       112: private: // connection usage methods
                    113: 
                    114:        void use() {
                    115:                time_used=time(0); // they started to use at this time
                    116:                used++;
                    117:        }
                    118:        void unuse() {
                    119:                used--;
                    120:                if(!used)
                    121:                        close();
                    122:        }
                    123: 
                    124: private: // connection usage data
                    125: 
                    126:        int used;
                    127: 
1.1       parser    128: private:
                    129: 
                    130:        const String& ffile_spec;
1.18      paf       131:        xsltStylesheet *fstylesheet;
1.1       parser    132:        time_t time_used;
                    133:        time_t prev_disk_time;
                    134: 
                    135: private:
                    136: 
                    137:        Pool *fservices_pool;
1.14      parser    138: 
                    139: private:
                    140:        void *malloc(size_t size) { return fservices_pool->malloc(size); }
                    141:        void *calloc(size_t size) { return fservices_pool->calloc(size); }
1.19      paf       142: };
                    143: 
                    144: /// Auto-object used to track Stylesheet_connection usage
                    145: class Stylesheet_connection_ptr {
                    146:        Stylesheet_connection *fconnection;
                    147: public:
                    148:        explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : 
                    149:                fconnection(aconnection) {
                    150:                fconnection->use();
                    151:        }
                    152:        ~Stylesheet_connection_ptr() {
                    153:                fconnection->unuse();
                    154:        }
                    155:        Stylesheet_connection* operator->() {
                    156:                return fconnection;
                    157:        }
                    158: 
                    159:        // copying
                    160:        Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
                    161:                fconnection->use();
                    162:        }
                    163:        Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
                    164:                // may do without this=src check
                    165:                fconnection->unuse();
                    166:                fconnection=src.fconnection;
                    167:                fconnection->use();
                    168: 
                    169:                return *this;
                    170:        }
1.1       parser    171: };
                    172: 
                    173: #endif

E-mail: