| 1 | // |
| 2 | // TemplateCache.cpp |
| 3 | // |
| 4 | // Library: JSON |
| 5 | // Package: JSON |
| 6 | // Module: TemplateCache |
| 7 | // |
| 8 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/File.h" |
| 16 | #include "Poco/JSON/TemplateCache.h" |
| 17 | |
| 18 | |
| 19 | namespace Poco { |
| 20 | namespace JSON { |
| 21 | |
| 22 | |
| 23 | TemplateCache* TemplateCache::_pInstance = 0; |
| 24 | |
| 25 | |
| 26 | TemplateCache::TemplateCache(): _pLogger(0) |
| 27 | { |
| 28 | setup(); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | TemplateCache::~TemplateCache() |
| 33 | { |
| 34 | _pInstance = 0; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | void TemplateCache::setup() |
| 39 | { |
| 40 | poco_assert (_pInstance == 0); |
| 41 | _pInstance = this; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | Template::Ptr TemplateCache::getTemplate(const Path& path) |
| 46 | { |
| 47 | if (_pLogger) |
| 48 | { |
| 49 | poco_trace_f1(*_pLogger, "Trying to load %s" , path.toString()); |
| 50 | } |
| 51 | |
| 52 | Path templatePath = resolvePath(path); |
| 53 | std::string templatePathname = templatePath.toString(); |
| 54 | |
| 55 | if (_pLogger) |
| 56 | { |
| 57 | poco_trace_f1(*_pLogger, "Path resolved to %s" , templatePathname); |
| 58 | } |
| 59 | |
| 60 | File templateFile(templatePathname); |
| 61 | |
| 62 | Template::Ptr tpl; |
| 63 | |
| 64 | std::map<std::string, Template::Ptr>::iterator it = _cache.find(templatePathname); |
| 65 | if (it == _cache.end()) |
| 66 | { |
| 67 | if (templateFile.exists()) |
| 68 | { |
| 69 | if (_pLogger) |
| 70 | { |
| 71 | poco_information_f1(*_pLogger, "Loading template %s" , templatePath.toString()); |
| 72 | } |
| 73 | |
| 74 | tpl = new Template(templatePath); |
| 75 | |
| 76 | try |
| 77 | { |
| 78 | tpl->parse(); |
| 79 | _cache[templatePathname] = tpl; |
| 80 | } |
| 81 | catch (JSONTemplateException& jte) |
| 82 | { |
| 83 | if (_pLogger) |
| 84 | { |
| 85 | poco_error_f2(*_pLogger, "Template %s contains an error: %s" , templatePath.toString(), jte.message()); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | if (_pLogger) |
| 92 | { |
| 93 | poco_error_f1(*_pLogger, "Template file %s doesn't exist" , templatePath.toString()); |
| 94 | } |
| 95 | throw FileNotFoundException(templatePathname); |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | tpl = it->second; |
| 101 | if (tpl->parseTime() < templateFile.getLastModified()) |
| 102 | { |
| 103 | if (_pLogger) |
| 104 | { |
| 105 | poco_information_f1(*_pLogger, "Reloading template %s" , templatePath.toString()); |
| 106 | } |
| 107 | |
| 108 | tpl = new Template(templatePath); |
| 109 | |
| 110 | try |
| 111 | { |
| 112 | tpl->parse(); |
| 113 | _cache[templatePathname] = tpl; |
| 114 | } |
| 115 | catch (JSONTemplateException& jte) |
| 116 | { |
| 117 | if (_pLogger) |
| 118 | { |
| 119 | poco_error_f2(*_pLogger, "Template %s contains an error: %s" , templatePath.toString(), jte.message()); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return tpl; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | Path TemplateCache::resolvePath(const Path& path) const |
| 130 | { |
| 131 | if (path.isAbsolute()) |
| 132 | return path; |
| 133 | |
| 134 | for (std::vector<Path>::const_iterator it = _includePaths.begin(); it != _includePaths.end(); ++it) |
| 135 | { |
| 136 | Path templatePath(*it, path); |
| 137 | |
| 138 | File templateFile(templatePath); |
| 139 | if (templateFile.exists()) |
| 140 | { |
| 141 | if (_pLogger) |
| 142 | { |
| 143 | poco_trace_f2(*_pLogger, "%s template file resolved to %s" , path.toString(), templatePath.toString()); |
| 144 | } |
| 145 | return templatePath; |
| 146 | } |
| 147 | if (_pLogger) |
| 148 | { |
| 149 | poco_trace_f1(*_pLogger, "%s doesn't exist" , templatePath.toString()); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | throw FileNotFoundException(path.toString()); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | } } // Poco::JSON |
| 158 | |