33 #include <boost/scoped_ptr.hpp>
34 #include <boost/filesystem/convenience.hpp>
44 #include "boost/version.hpp"
45 #include "boost/filesystem/config.hpp"
46 #if BOOST_VERSION <= 104600 || BOOST_FILESYSTEM_VERSION < 3
47 namespace boost {
namespace filesystem {
54 namespace fs = boost::filesystem;
65 using boost::regex_match;
66 using boost::regex_search;
67 using boost::scoped_ptr;
70 namespace pexExcept = lsst::pex::exceptions;
86 : PolicySource(fmts), Persistable(),
87 _file(PolicyParserFactory::UNRECOGNIZED), _format(), _pfact()
90 PolicyFile::PolicyFile(
const string& filepath,
91 const SupportedFormats::Ptr& fmts)
92 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
95 PolicyFile::PolicyFile(
const char *filepath,
96 const SupportedFormats::Ptr& fmts)
97 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
100 PolicyFile::PolicyFile(
const fs::path& filepath,
101 const SupportedFormats::Ptr& fmts)
102 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
105 PolicyFile::PolicyFile(
const string& filepath,
106 const PolicyParserFactory::Ptr& parserFactory)
107 : PolicySource(), Persistable(),
108 _file(filepath), _format(), _pfact(parserFactory)
110 if (! _pfact.get()) _format = _pfact->getFormatName();
113 PolicyFile::PolicyFile(
const fs::path& filepath,
114 const PolicyParserFactory::Ptr& parserFactory)
115 : PolicySource(), Persistable(),
116 _file(filepath), _format(), _pfact(parserFactory)
118 if (! _pfact.get()) _format = _pfact->getFormatName();
121 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
122 const SupportedFormats::Ptr& fmts)
123 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
125 if (! _file.has_root_path() && ! reposDir.empty())
126 _file = reposDir / _file;
129 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
130 const SupportedFormats::Ptr& fmts)
131 : PolicySource(fmts), Persistable(), _file(filepath), _format(), _pfact()
133 if (! _file.has_root_path() && ! reposDir.empty())
134 _file = reposDir / _file;
137 PolicyFile::PolicyFile(
const string& filepath,
const fs::path& reposDir,
138 const PolicyParserFactory::Ptr& parserFactory)
139 : PolicySource(), Persistable(),
140 _file(filepath), _format(), _pfact(parserFactory)
142 if (! _file.has_root_path() && ! reposDir.empty())
143 _file = reposDir / _file;
144 if (! _pfact.get()) _format = _pfact->getFormatName();
147 PolicyFile::PolicyFile(
const fs::path& filepath,
const fs::path& reposDir,
148 const PolicyParserFactory::Ptr& parserFactory)
149 : PolicySource(), Persistable(),
150 _file(filepath), _format(), _pfact(parserFactory)
152 if (! _file.has_root_path() && ! reposDir.empty())
153 _file = reposDir / _file;
154 if (! _pfact.get()) _format = _pfact->getFormatName();
163 const string& PolicyFile::getFormatName() {
164 if (_format.size() != 0)
return _format;
165 if (_file.empty())
return PolicyParserFactory::UNRECOGNIZED;
168 string ext = fs::extension(_file);
170 if (ext == EXT_PAF) {
171 if (_formats->supports(PAFParserFactory::FORMAT_NAME))
172 return cacheName(PAFParserFactory::FORMAT_NAME);
174 else if (ext == EXT_XML) {
175 return cacheName(
"XML");
181 ifstream is(_file.string().c_str());
184 "failure opening Policy file: "
191 (regex_match(line, SPACE_RE) ||
192 (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
197 "failure reading Policy file: "
200 (regex_match(line, SPACE_RE) ||
201 (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
204 return PAFParserFactory::FORMAT_NAME;
207 return cacheName(_formats->recognizeType(line));
210 return PolicyParserFactory::UNRECOGNIZED;
220 void PolicyFile::load(Policy& policy)
const {
222 PolicyParserFactory::Ptr pfactory = _pfact;
223 if (! pfactory.get()) {
224 const string& fmtname = getFormatName();
226 throw LSST_EXCEPT(ParserError,
"Unknown Policy format: " + _file.string());
228 pfactory = _formats->getFactory(fmtname);
231 scoped_ptr<PolicyParser> parser(pfactory->createParser(policy));
233 ifstream fs(_file.string().c_str());
236 "failure opening Policy file: "
definition of the PolicyParser class
definition of the PolicyFile class
static const std::string EXT_PAF
definition of Policy-specific exceptions classes
static const boost::regex CONTENTID
reg-exp for the start of a comment
definition of Policy parsing exceptions
definition of the PAFParserFactory class
PolicyFile(const std::string &filepath, const SupportedFormats::Ptr &fmts=defaultFormats)
path absolute(const path &p)
#define LSST_EXCEPT(type,...)
static const boost::regex SPACE_RE
the XML file extension, ".xml"
static const std::string EXT_XML
the PAF file extension, ".paf"
static const boost::regex COMMENT
reg-exp for an empty line