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

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.35    ! paf        11: static const char * const IDENT_STYLESHEET_CONNECTION_H="$Date: 2003/11/06 09:11:35 $";
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),
                     43:                prev_disk_time(0),
1.19      paf        44:                fstylesheet(0),
1.33      paf        45:                time_used(0), used(0)  {}
1.1       parser     46:        
                     47:        const String& file_spec() { return ffile_spec; }
                     48: 
                     49:        bool expired(time_t older_dies) {
1.19      paf        50:                return !used && time_used<older_dies;
1.1       parser     51:        }
1.15      paf        52:        time_t get_time_used() { return time_used; }
1.1       parser     53: 
                     54:        void disconnect() { 
1.18      paf        55:                xsltFreeStylesheet(fstylesheet);  fstylesheet=0; 
1.1       parser     56:        }
                     57: 
                     58:        bool connected() { return fstylesheet!=0; }
                     59: 
1.18      paf        60:        xsltStylesheet *stylesheet(bool nocache) { 
1.6       parser     61:                time_t new_disk_time=get_new_disk_time();
                     62:                if(nocache || new_disk_time)
1.1       parser     63:                        load(new_disk_time);
1.18      paf        64:                return fstylesheet; 
1.1       parser     65:        }
                     66: 
                     67: private:
                     68: 
1.19      paf        69:        /// return to cache
1.33      paf        70:        void close();
1.19      paf        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) {
1.31      paf        80:                int saved=xmlDoValidityCheckingDefaultValue;//
                     81:                xmlDoValidityCheckingDefaultValue=0;//
1.24      paf        82:                xsltStylesheet *nstylesheet=
1.33      paf        83:                        xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::L_FILE_SPEC));
1.31      paf        84:                xmlDoValidityCheckingDefaultValue = saved;//
1.28      paf        85:                if(xmlHaveGenericErrors()) {
1.20      paf        86:                        GdomeException exc=0;
1.34      paf        87:                        throw XmlException(&ffile_spec, exc);
1.28      paf        88:                }
                     89:                if(!nstylesheet)
                     90:                        throw Exception("file.missing",
1.18      paf        91:                                &ffile_spec,
1.28      paf        92:                                "stylesheet failed to load");
1.1       parser     93: 
1.18      paf        94:                xsltFreeStylesheet(fstylesheet);  
                     95:                fstylesheet=nstylesheet;
1.1       parser     96:                prev_disk_time=new_disk_time;
                     97:        }
                     98: 
                     99:        time_t get_disk_time() {
                    100:                size_t size;
                    101:                time_t atime, mtime, ctime;
1.27      paf       102:                String stamp_file_spec(ffile_spec);
1.1       parser    103:                stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX;
1.3       parser    104:                // {file_spec}.stamp modification time OR {file_spec}
                    105:                const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec;
1.14      parser    106:                file_stat(stat_file_spec, 
1.1       parser    107:                        size,
                    108:                        atime, mtime, ctime,
1.14      parser    109:                        true/*exception on error*/);
1.1       parser    110:                return mtime;
                    111:        }
                    112: 
1.19      paf       113: private: // connection usage methods
                    114: 
1.33      paf       115:        int used;
1.19      paf       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: };
                    127: 
                    128: /// Auto-object used to track Stylesheet_connection usage
                    129: class Stylesheet_connection_ptr {
                    130:        Stylesheet_connection *fconnection;
                    131: public:
                    132:        explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) : 
                    133:                fconnection(aconnection) {
                    134:                fconnection->use();
                    135:        }
                    136:        ~Stylesheet_connection_ptr() {
                    137:                fconnection->unuse();
                    138:        }
                    139:        Stylesheet_connection* operator->() {
                    140:                return fconnection;
                    141:        }
1.33      paf       142: /*     Stylesheet_connection& operator*() {
                    143:                return *fconnection;
                    144:        }*/
1.19      paf       145: 
                    146:        // copying
                    147:        Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
                    148:                fconnection->use();
                    149:        }
                    150:        Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
                    151:                // may do without this=src check
                    152:                fconnection->unuse();
                    153:                fconnection=src.fconnection;
                    154:                fconnection->use();
                    155: 
                    156:                return *this;
                    157:        }
1.1       parser    158: };
                    159: 
                    160: #endif

E-mail: