|
|
| version 1.10, 2001/10/09 14:25:30 | version 1.32.2.5, 2003/03/07 16:05:10 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Stylesheet connection decl. | Parser: Stylesheet connection decl. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | |
| */ | */ |
| #ifndef PA_STYLESHEET_CONNECTION_H | #ifndef PA_STYLESHEET_CONNECTION_H |
| #define PA_STYLESHEET_CONNECTION_H | #define PA_STYLESHEET_CONNECTION_H |
| #include "pa_config_includes.h" | static const char* IDENT_STYLESHEET_CONNECTION_H="$Date$"; |
| #include "libxslt/xslt.h" | |
| #include "libxslt/xsltInternals.h" | |
| #include "pa_pool.h" | #include "pa_pool.h" |
| #include "pa_xslt_stylesheet_manager.h" | #include "pa_stylesheet_manager.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_globals.h" | |
| #include "XalanTransformer2.hpp" | |
| // defines | // defines |
| Line 25 | Line 26 |
| /** Connection with stylesheet: | /** Connection with stylesheet: |
| remembers time and can figure out that it needs recompilation | remembers time and can figure out that it needs recompilation |
| */ | */ |
| class Stylesheet_connection : public Pooled { | class Stylesheet_connection: public PA_Object { |
| private: | |
| public: | StringPtr ffile_spec; |
| xsltStylesheet *fstylesheet; | |
| time_t time_used; | |
| time_t prev_disk_time; | |
| Stylesheet_connection(Pool& pool, const String& afile_spec) : Pooled(pool), | public: |
| ftransformer(new XalanTransformer2), | |
| Stylesheet_connection(StringPtr afile_spec): | |
| ffile_spec(afile_spec), | ffile_spec(afile_spec), |
| time_used(0), | |
| prev_disk_time(0), | prev_disk_time(0), |
| fservices_pool(0), | fstylesheet(0), |
| fstylesheet(0) { | time_used(0) {} |
| } | |
| const String& file_spec() { return ffile_spec; } | StringPtr file_spec() { return ffile_spec; } |
| void set_services(Pool *aservices_pool) { | |
| time_used=time(0); // they started to use at this time | |
| fservices_pool=aservices_pool; | |
| } | |
| bool expired(time_t older_dies) { | bool expired(time_t older_dies) { |
| return time_used<older_dies; | return references()==1/*!used*/ && time_used<older_dies; |
| } | |
| void close() { | |
| XSLT_stylesheet_manager->close_connection(ffile_spec, *this); | |
| } | } |
| time_t get_time_used() { return time_used; } | |
| void disconnect() { | void disconnect() { |
| /*ignore error*/ftransformer->destroyStylesheet(fstylesheet); fstylesheet=0; | xsltFreeStylesheet(fstylesheet); fstylesheet=0; |
| // connection effectively useless now, free up some memory | |
| delete ftransformer; | |
| } | } |
| bool connected() { return fstylesheet!=0; } | bool connected() { return fstylesheet!=0; } |
| const XalanCompiledStylesheet& stylesheet(bool nocache) { | xsltStylesheet *stylesheet(bool nocache) { |
| time_t new_disk_time=get_new_disk_time(); | time_t new_disk_time=get_new_disk_time(); |
| if(nocache || new_disk_time) | if(nocache || new_disk_time) |
| load(new_disk_time); | load(new_disk_time); |
| return *fstylesheet; | return fstylesheet; |
| } | |
| private: | |
| /// return to cache | |
| void close() { | |
| stylesheet_manager.close_connection(ffile_spec, *this); | |
| } | } |
| private: | private: |
| Line 77 private: | Line 77 private: |
| } | } |
| void load(time_t new_disk_time) { | void load(time_t new_disk_time) { |
| Pool& pool=*fservices_pool; | int saved=xmlDoValidityCheckingDefaultValue;// |
| xmlDoValidityCheckingDefaultValue=0;// | |
| try{ | xsltStylesheet *nstylesheet= |
| ftransformer->compileStylesheet2(ffile_spec.cstr(String::UL_FILE_SPEC), fstylesheet); | xsltParseStylesheetFile(BAD_CAST ffile_spec->cstr(String::UL_FILE_SPEC).get()); |
| } | xmlDoValidityCheckingDefaultValue = saved;// |
| catch (XSLException& e) { | if(xmlHaveGenericErrors()) { |
| pool._throw(&ffile_spec, e); | GdomeException exc=0; |
| } | throw Exception(ffile_spec, exc); |
| catch (SAXParseException& e) { | } |
| pool._throw(&ffile_spec, e); | if(!nstylesheet) |
| } | throw Exception("file.missing", |
| catch (SAXException& e) { | ffile_spec, |
| pool._throw(&ffile_spec, e); | "stylesheet failed to load"); |
| } | |
| catch (XMLException& e) { | |
| pool._throw(&ffile_spec, e); | |
| } | |
| catch(const XalanDOMException& e) { | |
| pool._throw(&ffile_spec, e); | |
| } | |
| xsltFreeStylesheet(fstylesheet); | |
| fstylesheet=nstylesheet; | |
| prev_disk_time=new_disk_time; | prev_disk_time=new_disk_time; |
| } | } |
| time_t get_disk_time() { | time_t get_disk_time() { |
| size_t size; | size_t size; |
| time_t atime, mtime, ctime; | time_t atime, mtime, ctime; |
| String stamp_file_spec(ffile_spec); | StringPtr stamp_file_spec(new String(*ffile_spec)); |
| stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX; | *stamp_file_spec << STYLESHEET_FILENAME_STAMP_SUFFIX; |
| // {file_spec}.stamp modification time OR {file_spec} | // {file_spec}.stamp modification time OR {file_spec} |
| const String& stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec; | StringPtr stat_file_spec=file_readable(stamp_file_spec)?stamp_file_spec:ffile_spec; |
| if(!file_stat(stat_file_spec, | file_stat(stat_file_spec, |
| size, | size, |
| atime, mtime, ctime, | atime, mtime, ctime, |
| false/*no exception on global pool[stat_file_spec], please*/)) | true/*exception on error*/); |
| mtime=1; //no file=pseudo non-zero time, see get_new_disk_time | |
| return mtime; | return mtime; |
| } | } |
| private: | void touch() { |
| time_used=time(0); // they started to use at this time | |
| const String& ffile_spec; | } |
| const XalanCompiledStylesheet *fstylesheet; | |
| time_t time_used; | |
| time_t prev_disk_time; | |
| private: | |
| XalanTransformer2 *ftransformer; | |
| Pool *fservices_pool; | |
| }; | }; |
| DECLARE_OBJECT_PTR(Stylesheet_connection); | |
| #endif | #endif |