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

1.1       parser      1: /** @file
1.7       parser      2:        Parser: Stylesheet connection decl.
1.1       parser      3: 
1.33      paf         4:        Copyright (c) 2001-2003 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.36    ! paf        11: static const char * const IDENT_STYLESHEET_CONNECTION_H="$Date: 2003/11/20 16:34:25 $";
1.1       parser     12: 
1.22      paf        13: #include "libxslt/xslt.h"
1.18      paf        14: #include "libxslt/xsltInternals.h"
                     15: 
1.33      paf        16: 
1.34      paf        17: #include "pa_xml_exception.h"
1.1       parser     18: #include "pa_common.h"
1.33      paf        19: #include "pa_globals.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: */
1.33      paf        28: class Stylesheet_connection: public PA_Object {
1.1       parser     29: 
1.19      paf        30:        friend class Stylesheet_connection_ptr;
                     31: 
1.33      paf        32: private:
                     33: 
                     34:        const String& ffile_spec;
                     35:        xsltStylesheet *fstylesheet;
                     36:        time_t time_used;
                     37:        time_t prev_disk_time;
                     38: 
1.1       parser     39: public:
                     40: 
1.33      paf        41:        Stylesheet_connection(const String& afile_spec):
1.1       parser     42:                ffile_spec(afile_spec),
1.36    ! paf        43:                fstylesheet(0),
        !            44:                time_used(0),
1.1       parser     45:                prev_disk_time(0),
1.36    ! paf        46:                used(0)  {}
1.1       parser     47:        
                     48:        const String& file_spec() { return ffile_spec; }
                     49: 
                     50:        bool expired(time_t older_dies) {
1.19      paf        51:                return !used && time_used<older_dies;
1.1       parser     52:        }
1.15      paf        53:        time_t get_time_used() { return time_used; }
1.1       parser     54: 
                     55:        void disconnect() { 
1.18      paf        56:                xsltFreeStylesheet(fstylesheet);  fstylesheet=0; 
1.1       parser     57:        }
                     58: 
                     59:        bool connected() { return fstylesheet!=0; }
                     60: 
1.18      paf        61:        xsltStylesheet *stylesheet(bool nocache) { 
1.6       parser     62:                time_t new_disk_time=get_new_disk_time();
                     63:                if(nocache || new_disk_time)
1.1       parser     64:                        load(new_disk_time);
1.18      paf        65:                return fstylesheet; 
1.1       parser     66:        }
                     67: 
                     68: private:
                     69: 
1.19      paf        70:        /// return to cache
1.33      paf        71:        void close();
1.19      paf        72: 
                     73: private:
                     74: 
1.1       parser     75:        time_t get_new_disk_time() {
                     76:                time_t now_disk_time=get_disk_time();
                     77:                return now_disk_time>prev_disk_time?now_disk_time:0;
                     78:        }
                     79: 
                     80:        void load(time_t new_disk_time) {
1.31      paf        81:                int saved=xmlDoValidityCheckingDefaultValue;//
                     82:                xmlDoValidityCheckingDefaultValue=0;//
1.24      paf        83:                xsltStylesheet *nstylesheet=
1.33      paf        84:                        xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::L_FILE_SPEC));
1.31      paf        85:                xmlDoValidityCheckingDefaultValue = saved;//
1.28      paf        86:                if(xmlHaveGenericErrors()) {
1.20      paf        87:                        GdomeException exc=0;
1.34      paf        88:                        throw XmlException(&ffile_spec, exc);
1.28      paf        89:                }
                     90:                if(!nstylesheet)
                     91:                        throw Exception("file.missing",
1.18      paf        92:                                &ffile_spec,
1.28      paf        93:                                "stylesheet failed to load");
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;
1.27      paf       103:                String stamp_file_spec(ffile_spec);
1.1       parser    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: 
1.33      paf       116:        int used;
1.19      paf       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: };
                    128: 
                    129: /// Auto-object used to track Stylesheet_connection usage
                    130: class Stylesheet_connection_ptr {
                    131:        Stylesheet_connection *fconnection;
                    132: public:
                    133:        explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : 
                    134:                fconnection(aconnection) {
                    135:                fconnection->use();
                    136:        }
                    137:        ~Stylesheet_connection_ptr() {
                    138:                fconnection->unuse();
                    139:        }
                    140:        Stylesheet_connection* operator->() {
                    141:                return fconnection;
                    142:        }
1.33      paf       143: /*     Stylesheet_connection& operator*() {
                    144:                return *fconnection;
                    145:        }*/
1.19      paf       146: 
                    147:        // copying
                    148:        Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
                    149:                fconnection->use();
                    150:        }
                    151:        Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
                    152:                // may do without this=src check
                    153:                fconnection->unuse();
                    154:                fconnection=src.fconnection;
                    155:                fconnection->use();
                    156: 
                    157:                return *this;
                    158:        }
1.1       parser    159: };
                    160: 
                    161: #endif

E-mail: