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
UrnPolicyFile.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 
23 /*
24  * UrnPolicyFile
25  */
27 #include "lsst/pex/exceptions.h"
28 
29 #include <string>
30 #include <vector>
31 
32 namespace lsst {
33 namespace pex {
34 namespace policy {
35 
36 //@cond
37 
38 using namespace std;
39 
40 const string UrnPolicyFile::URN_PREFIX("urn:eupspkg:");
41 const string UrnPolicyFile::URN_PREFIX_ABBREV("@");
42 
51 string stripPrefixes(string urn, bool strict) {
52  // strip @'s
53  int numAts = 0;
54  while (urn.length() > 0 && urn.find(UrnPolicyFile::URN_PREFIX_ABBREV) == 0) {
55  urn = urn.substr(UrnPolicyFile::URN_PREFIX_ABBREV.length());
56  ++numAts;
57  }
58 
59  // strip urn:eupspkg:
60  string lowered(urn);
61  transform(lowered.begin(), lowered.end(), lowered.begin(), ::tolower);
62  bool hasPrefix = (lowered.find(UrnPolicyFile::URN_PREFIX) == 0);
63  if (hasPrefix)
64  urn = urn.substr(UrnPolicyFile::URN_PREFIX.length());
65 
66  // validate, if requested
67  if (strict && (numAts > 1 || !hasPrefix))
68  throw LSST_EXCEPT(BadNameError, ("URN must start with \"urn:eupspkg:\" or \"@urn:eupspkg:\""));
69 
70  return urn;
71 }
72 
83 void splitAndValidate(const string& urn, vector<string>& a, bool strict) {
84  // strip prefixes
85  string stripped = stripPrefixes(urn, strict);
86 
87  // split
88  while (true) {
89  size_t i = stripped.find(":");
90  if (i == string::npos) {
91  if (stripped.length() > 0) a.push_back(stripped);
92  break;
93  }
94  else {
95  a.push_back(stripped.substr(0, i));
96  stripped = stripped.substr(i + 1);
97  }
98  }
99 
100  // validate
101  // - min size is 2 -- product:file
102  // - max size is 3 -- product:repos:file
103  if (a.size() < 2 || a.size() > 3)
104  throw LSST_EXCEPT
105  (BadNameError,
106  "Wrong number of terms in policy file urn \"" + urn + "\". "
107  + "The expected form is "
108  + "@urn:eupspkg:<product>:[<repository>:]<file> or "
109  + "@@<product>:[<repository>:]<file>. "
110  + "Is there a typo in the urn?");
111 }
112 
119 string UrnPolicyFile::productNameFromUrn(const string& urn, bool strictUrn) {
120  vector<string> split;
121  splitAndValidate(urn, split, strictUrn);
122  return split[0];
123 }
124 
131 string UrnPolicyFile::filePathFromUrn(const string& urn, bool strictUrn) {
132  vector<string> split;
133  splitAndValidate(urn, split, strictUrn);
134  return split.back();
135 }
136 
143 string UrnPolicyFile::reposFromUrn(const string& urn, bool strictUrn) {
144  vector<string> split;
145  splitAndValidate(urn, split, strictUrn);
146  if (split.size() == 3) return split[1];
147  else return "";
148 }
149 
157 bool UrnPolicyFile::looksLikeUrn(const string& s, bool strict) {
158  if (strict) {
159  string lc(s);
160  transform(lc.begin(), lc.end(), lc.begin(), ::tolower);
161  while (lc[0] == '@') lc = lc.substr(1);
162  if (lc.find(UrnPolicyFile::URN_PREFIX) != 0) return false;
163  }
164  const string& stripped = stripPrefixes(s, strict);
165  return s.length() != stripped.length() && s.find(":") != s.npos;
166 }
167 
168 //@endcond
169 
170 } // namespace policy
171 } // namespace pex
172 } // namespace lsst
static const std::string URN_PREFIX_ABBREV
Include files required for standard LSST Exception handling.
static const std::string URN_PREFIX
static std::string productNameFromUrn(const std::string &urn, bool strictUrn=false)
static bool looksLikeUrn(const std::string &s, bool strict=false)
static std::string reposFromUrn(const std::string &urn, bool strictUrn=false)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
static std::string filePathFromUrn(const std::string &urn, bool strictUrn=false)
the definition of the UrnPolicyFile class