LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
PolicyString.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
27 #include <sstream>
28 // #include <iosfwd>
29 
30 #include <boost/scoped_ptr.hpp>
31 
38 
39 namespace lsst {
40 namespace pex {
41 namespace policy {
42 
43 //@cond
44 
45 using std::string;
46 using std::ifstream;
47 using boost::regex;
48 using boost::regex_match;
49 using boost::regex_search;
50 using boost::scoped_ptr;
52 
53 namespace pexExcept = lsst::pex::exceptions;
54 
55 const regex PolicyString::SPACE_RE("^\\s*$");
56 const regex PolicyString::COMMENT("^\\s*#");
57 const regex
58  PolicyString::CONTENTID("^\\s*#\\s*<\\?cfg\\s+\\w+(\\s+\\w+)*\\s*\\?>",
59  regex::icase);
60 
61 /*
62  * create a "null" Policy formed from an empty string.
63  * @param fmts a SupportedFormats object to use. An instance
64  * encapsulates a configured set of known formats.
65  */
67  : PolicySource(fmts), Persistable(), _data(), _pfact()
68 { }
69 
70 /*
71  * create a "null" Policy formed from an empty string.
72  * @param fmts a SupportedFormats object to use. An instance
73  * encapsulates a configured set of known formats.
74  */
75 PolicyString::PolicyString(const std::string& data,
76  const SupportedFormats::Ptr& fmts)
77  : PolicySource(fmts), Persistable(), _data(data), _pfact()
78 { }
79 
80 #define PolStr_ERROR_MSG(use, msg, input) \
81  std::ostringstream use; \
82  use << msg << ": '"; \
83  if (input.length() > 40) \
84  use << input; \
85  else \
86  use << input.substr(0,40) << "..."; \
87  use << "'";
88 
89 /*
90  * return the name of the format that the data is stored in. This may
91  * cause the first few records of the source to be read.
92  * @exception IOError if an error occurs while reading the first few
93  * characters of the source stream.
94  */
95 const string& PolicyString::getFormatName() {
96 
97  // try reading the initial characters
98  std::istringstream is(_data);
99  if (is.fail()) {
100  PolStr_ERROR_MSG(msg, "failure opening input Policy string", _data);
101  throw LSST_EXCEPT(pexExcept::IoError, msg.str());
102  }
103 
104  // skip over comments
105  string line;
106  getline(is, line);
107  while (is.good() &&
108  (regex_match(line, SPACE_RE) ||
109  (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
110  { }
111 
112  if (is.fail()) {
113  PolStr_ERROR_MSG(msg, "failure reading input Policy string", _data);
114  throw LSST_EXCEPT(pexExcept::IoError, msg.str());
115  }
116  if (is.eof() &&
117  (regex_match(line, SPACE_RE) ||
118  (regex_search(line, COMMENT) && !regex_search(line, COMMENT))))
119  {
120  // empty file; let's just assume PAF (but don't cache the name).
122  }
123  return cacheName(_formats->recognizeType(line));
124 }
125 
126 /*
127  * load the data from this Policy source into a Policy object
128  * @param policy the policy object to load the data into
129  * @exception ParserError if an error occurs while parsing the data
130  * @exception IOError if an I/O error occurs while reading from the
131  * source stream.
132  */
133 void PolicyString::load(Policy& policy) {
134 
135  PolicyParserFactory::Ptr pfactory = _pfact;
136  if (! pfactory.get()) {
137  const string& fmtname = getFormatName();
138  if (fmtname.empty()) {
139  PolStr_ERROR_MSG(ms,"Unknown Policy format for string data",_data);
140  throw LSST_EXCEPT(pexExcept::IoError, ms.str());
141  }
142  pfactory = _formats->getFactory(fmtname);
143  }
144 
145  scoped_ptr<PolicyParser> parser(pfactory->createParser(policy));
146 
147  std::istringstream is(_data);
148  if (is.fail()) {
149  PolStr_ERROR_MSG(msg, "failure opening Policy string", _data);
150  throw LSST_EXCEPT(pexExcept::IoError, msg.str());
151  }
152  parser->parse(is);
153 }
154 
155 //@endcond
156 
157 }}} // end lsst::pex::policy
const std::string & cacheName(const std::string &name)
Definition: PolicyString.h:121
definition of the PolicyParser class
definition of the PolicyFile class
virtual void load(Policy &policy)
definition of Policy-specific exceptions classes
PolicyParserFactory::Ptr _pfact
Definition: PolicyString.h:127
definition of Policy parsing exceptions
static const boost::regex COMMENT
reg-exp for an empty line
Definition: PolicyString.h:108
virtual const std::string & getFormatName()
SupportedFormats::Ptr _formats
Definition: PolicySource.h:142
definition of the PAFParserFactory class
PolicyString(const std::string &data, const SupportedFormats::Ptr &fmts=defaultFormats)
static const boost::regex CONTENTID
reg-exp for the start of a comment
Definition: PolicyString.h:112
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
static const boost::regex SPACE_RE
Definition: PolicyString.h:107
boost::shared_ptr< PolicyParserFactory > Ptr
boost::shared_ptr< SupportedFormats > Ptr
definition of the PolicyString class