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

1.1       parser      1: /** @file
1.7       parser      2:        Parser: Stylesheet connection decl.
1.1       parser      3: 
                      4:        Copyright (c) 2001 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.23    ! paf         7:        $Id: pa_stylesheet_connection.h,v 1.22 2002/01/21 16:44:48 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.18      paf        82:                xsltStylesheet *nstylesheet;
1.23    ! paf        83:                {
        !            84:                        int savedReplaceEntities=xmlSubstituteEntitiesDefault(1);
        !            85:                        nstylesheet=xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::UL_FILE_SPEC));
        !            86:                        xmlSubstituteEntitiesDefault(savedReplaceEntities);
        !            87:                }
1.21      paf        88:                if(!nstylesheet || xmlHaveGenericErrors()) {
1.20      paf        89:                        GdomeException exc=0;
1.18      paf        90:                        throw Exception(0, 0,
                     91:                                &ffile_spec,
1.20      paf        92:                                exc);
                     93:                }
1.1       parser     94: 
1.18      paf        95:                xsltFreeStylesheet(fstylesheet);  
                     96:                fstylesheet=nstylesheet;
1.1       parser     97:                prev_disk_time=new_disk_time;
                     98:        }
                     99: 
                    100:        time_t get_disk_time() {
                    101:                size_t size;
                    102:                time_t atime, mtime, ctime;
                    103:                String stamp_file_spec(ffile_spec); 
                    104:                stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX;
1.3       parser    105:                // {file_spec}.stamp modification time OR {file_spec}
                    106:                const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec;
1.14      parser    107:                file_stat(stat_file_spec, 
1.1       parser    108:                        size,
                    109:                        atime, mtime, ctime,
1.14      parser    110:                        true/*exception on error*/);
1.1       parser    111:                return mtime;
                    112:        }
                    113: 
1.19      paf       114: private: // connection usage methods
                    115: 
                    116:        void use() {
                    117:                time_used=time(0); // they started to use at this time
                    118:                used++;
                    119:        }
                    120:        void unuse() {
                    121:                used--;
                    122:                if(!used)
                    123:                        close();
                    124:        }
                    125: 
                    126: private: // connection usage data
                    127: 
                    128:        int used;
                    129: 
1.1       parser    130: private:
                    131: 
                    132:        const String& ffile_spec;
1.18      paf       133:        xsltStylesheet *fstylesheet;
1.1       parser    134:        time_t time_used;
                    135:        time_t prev_disk_time;
                    136: 
                    137: private:
                    138: 
                    139:        Pool *fservices_pool;
1.14      parser    140: 
                    141: private:
                    142:        void *malloc(size_t size) { return fservices_pool->malloc(size); }
                    143:        void *calloc(size_t size) { return fservices_pool->calloc(size); }
1.19      paf       144: };
                    145: 
                    146: /// Auto-object used to track Stylesheet_connection usage
                    147: class Stylesheet_connection_ptr {
                    148:        Stylesheet_connection *fconnection;
                    149: public:
                    150:        explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : 
                    151:                fconnection(aconnection) {
                    152:                fconnection->use();
                    153:        }
                    154:        ~Stylesheet_connection_ptr() {
                    155:                fconnection->unuse();
                    156:        }
                    157:        Stylesheet_connection* operator->() {
                    158:                return fconnection;
                    159:        }
                    160: 
                    161:        // copying
                    162:        Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
                    163:                fconnection->use();
                    164:        }
                    165:        Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
                    166:                // may do without this=src check
                    167:                fconnection->unuse();
                    168:                fconnection=src.fconnection;
                    169:                fconnection->use();
                    170: 
                    171:                return *this;
                    172:        }
1.1       parser    173: };
                    174: 
                    175: #endif

E-mail: