1 Star 0 Fork 1

cpp_workspace/poco

forked from sulayman_tien/poco 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TemplateCache.cpp 2.74 KB
一键复制 编辑 原始数据 按行查看 历史
Aleksandar Fabijanic 提交于 2022-07-09 00:28 +08:00 . Release 1.12.0 (#3674)
//
// TemplateCache.cpp
//
// Library: JSON
// Package: JSON
// Module: TemplateCache
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/File.h"
#include "Poco/JSON/TemplateCache.h"
namespace Poco {
namespace JSON {
TemplateCache* TemplateCache::_pInstance = 0;
TemplateCache::TemplateCache(): _pLogger(0)
{
setup();
}
TemplateCache::~TemplateCache()
{
_pInstance = 0;
}
void TemplateCache::setup()
{
poco_assert (_pInstance == 0);
_pInstance = this;
}
Template::Ptr TemplateCache::getTemplate(const Path& path)
{
if (_pLogger)
{
poco_trace_f1(*_pLogger, "Trying to load %s", path.toString());
}
Path templatePath = resolvePath(path);
std::string templatePathname = templatePath.toString();
if (_pLogger)
{
poco_trace_f1(*_pLogger, "Path resolved to %s", templatePathname);
}
File templateFile(templatePathname);
Template::Ptr tpl;
std::map<std::string, Template::Ptr>::iterator it = _cache.find(templatePathname);
if (it == _cache.end())
{
if (templateFile.exists())
{
if (_pLogger)
{
poco_information_f1(*_pLogger, "Loading template %s", templatePath.toString());
}
tpl = new Template(templatePath);
try
{
tpl->parse();
_cache[templatePathname] = tpl;
}
catch (JSONTemplateException& jte)
{
if (_pLogger)
{
poco_error_f2(*_pLogger, "Template %s contains an error: %s", templatePath.toString(), jte.message());
}
}
}
else
{
if (_pLogger)
{
poco_error_f1(*_pLogger, "Template file %s doesn't exist", templatePath.toString());
}
throw FileNotFoundException(templatePathname);
}
}
else
{
tpl = it->second;
if (tpl->parseTime() < templateFile.getLastModified())
{
if (_pLogger)
{
poco_information_f1(*_pLogger, "Reloading template %s", templatePath.toString());
}
tpl = new Template(templatePath);
try
{
tpl->parse();
_cache[templatePathname] = tpl;
}
catch (JSONTemplateException& jte)
{
if (_pLogger)
{
poco_error_f2(*_pLogger, "Template %s contains an error: %s", templatePath.toString(), jte.message());
}
}
}
}
return tpl;
}
Path TemplateCache::resolvePath(const Path& path) const
{
if (path.isAbsolute())
return path;
for (const auto& p: _includePaths)
{
Path templatePath(p, path);
File templateFile(templatePath);
if (templateFile.exists())
{
if (_pLogger)
{
poco_trace_f2(*_pLogger, "%s template file resolved to %s", path.toString(), templatePath.toString());
}
return templatePath;
}
if (_pLogger)
{
poco_trace_f1(*_pLogger, "%s doesn't exist", templatePath.toString());
}
}
throw FileNotFoundException(path.toString());
}
} } // Poco::JSON
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cpp_workspace/poco.git
git@gitee.com:cpp_workspace/poco.git
cpp_workspace
poco
poco
master

搜索帮助