Annotation of parser3/src/include/pa_stylesheet_connection.h, revision 1.19
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.19 ! paf 7: $Id: pa_stylesheet_connection.h,v 1.18 2002/01/14 17:48:56 paf Exp $
1.1 parser 8: */
9:
10: #ifndef PA_STYLESHEET_CONNECTION_H
11: #define PA_STYLESHEET_CONNECTION_H
12:
1.18 paf 13: #include "libxslt/libxslt.h"
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:
1.18 paf 79: /// @test grab errors
1.1 parser 80: void load(time_t new_disk_time) {
81: Pool& pool=*fservices_pool;
82:
1.18 paf 83: xsltStylesheet *nstylesheet;
84: nstylesheet=xsltParseStylesheetFile(BAD_CAST ffile_spec.cstr(String::UL_FILE_SPEC));
85: if(!nstylesheet)
86: throw Exception(0, 0,
87: &ffile_spec,
88: "error compiling. TODO: grab errors");
1.1 parser 89:
1.18 paf 90: xsltFreeStylesheet(fstylesheet);
91: fstylesheet=nstylesheet;
1.1 parser 92: prev_disk_time=new_disk_time;
93: }
94:
95: time_t get_disk_time() {
96: size_t size;
97: time_t atime, mtime, ctime;
98: String stamp_file_spec(ffile_spec);
99: stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX;
1.3 parser 100: // {file_spec}.stamp modification time OR {file_spec}
101: const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec;
1.14 parser 102: file_stat(stat_file_spec,
1.1 parser 103: size,
104: atime, mtime, ctime,
1.14 parser 105: true/*exception on error*/);
1.1 parser 106: return mtime;
107: }
108:
1.19 ! paf 109: private: // connection usage methods
! 110:
! 111: void use() {
! 112: time_used=time(0); // they started to use at this time
! 113: used++;
! 114: }
! 115: void unuse() {
! 116: used--;
! 117: if(!used)
! 118: close();
! 119: }
! 120:
! 121: private: // connection usage data
! 122:
! 123: int used;
! 124:
1.1 parser 125: private:
126:
127: const String& ffile_spec;
1.18 paf 128: xsltStylesheet *fstylesheet;
1.1 parser 129: time_t time_used;
130: time_t prev_disk_time;
131:
132: private:
133:
134: Pool *fservices_pool;
1.14 parser 135:
136: private:
137: void *malloc(size_t size) { return fservices_pool->malloc(size); }
138: void *calloc(size_t size) { return fservices_pool->calloc(size); }
1.19 ! paf 139: };
! 140:
! 141: /// Auto-object used to track Stylesheet_connection usage
! 142: class Stylesheet_connection_ptr {
! 143: Stylesheet_connection *fconnection;
! 144: public:
! 145: explicit Stylesheet_connection_ptr(Stylesheet_connection *aconnection) :
! 146: fconnection(aconnection) {
! 147: fconnection->use();
! 148: }
! 149: ~Stylesheet_connection_ptr() {
! 150: fconnection->unuse();
! 151: }
! 152: Stylesheet_connection* operator->() {
! 153: return fconnection;
! 154: }
! 155:
! 156: // copying
! 157: Stylesheet_connection_ptr(const Stylesheet_connection_ptr& src) : fconnection(src.fconnection) {
! 158: fconnection->use();
! 159: }
! 160: Stylesheet_connection_ptr& operator =(const Stylesheet_connection_ptr& src) {
! 161: // may do without this=src check
! 162: fconnection->unuse();
! 163: fconnection=src.fconnection;
! 164: fconnection->use();
! 165:
! 166: return *this;
! 167: }
1.1 parser 168: };
169:
170: #endif
E-mail: