33 #include <boost/filesystem/convenience.hpp>
43 #include "boost/version.hpp"
44 #include "boost/filesystem/config.hpp"
45 #if BOOST_VERSION <= 104600 || BOOST_FILESYSTEM_VERSION < 3
47 namespace filesystem {
48 path
absolute(
const path& p) {
return complete(p); }
61 using boost::regex_match;
62 using boost::regex_search;
82 : PolicySource(fmts), Persistable(), _file(PolicyParserFactory::UNRECOGNIZED), _format(), _pfact() {}
84 PolicyFile::PolicyFile(
const string& filepath,
const SupportedFormats::Ptr& fmts)
85 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
87 PolicyFile::PolicyFile(
const char* filepath,
const SupportedFormats::Ptr& fmts)
88 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
90 PolicyFile::PolicyFile(
const fs::path& filepath,
const SupportedFormats::Ptr& fmts)
91 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {}
93 PolicyFile::PolicyFile(
const string& filepath,
const PolicyParserFactory::Ptr& parserFactory)
94 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
95 if (!_pfact.get()) _format = _pfact->getFormatName();
98 PolicyFile::PolicyFile(
const fs::path& filepath,
const PolicyParserFactory::Ptr& parserFactory)
99 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
100 if (!_pfact.get()) _format = _pfact->getFormatName();
103 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
const SupportedFormats::Ptr& fmts)
104 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {
105 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
108 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
const SupportedFormats::Ptr& fmts)
109 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact() {
110 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
113 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
114 const PolicyParserFactory::Ptr& parserFactory)
115 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
116 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
117 if (!_pfact.get()) _format = _pfact->getFormatName();
120 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
121 const PolicyParserFactory::Ptr& parserFactory)
122 : PolicySource(), Persistable(), _file(filepath), _format(), _pfact(parserFactory) {
123 if (!_file.has_root_path() && !reposDir.empty()) _file = reposDir / _file;
124 if (!_pfact.get()) _format = _pfact->getFormatName();
133 const string& PolicyFile::getFormatName() {
134 if (_format.size() != 0)
return _format;
135 if (_file.empty())
return PolicyParserFactory::UNRECOGNIZED;
138 string ext = fs::extension(_file);
140 if (ext == EXT_PAF) {
141 if (_formats->supports(PAFParserFactory::FORMAT_NAME))
142 return cacheName(PAFParserFactory::FORMAT_NAME);
143 }
else if (ext == EXT_XML) {
144 return cacheName(
"XML");
149 if (fs::exists(_file)) {
150 ifstream is(_file.string().c_str());
153 "failure opening Policy file: " +
fs::absolute(_file).
string());
164 "failure reading Policy file: " +
fs::absolute(_file).
string());
168 return PAFParserFactory::FORMAT_NAME;
171 return cacheName(_formats->recognizeType(
line));
174 return PolicyParserFactory::UNRECOGNIZED;
184 void PolicyFile::load(Policy& policy)
const {
185 PolicyParserFactory::Ptr pfactory = _pfact;
186 if (!pfactory.get()) {
187 const string& fmtname = getFormatName();
188 if (fmtname.empty())
throw LSST_EXCEPT(ParserError,
"Unknown Policy format: " + _file.string());
190 pfactory = _formats->getFactory(fmtname);
195 ifstream
fs(_file.string().c_str());