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

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

E-mail: