LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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 std::string reposFromUrn(const std::string &urn, bool strictUrn=false)
Extract the repository name from a URN, or "" if none.
static const std::string URN_PREFIX_ABBREV
The prefix that a Policy URN starts with.
table::Key< int > a
STL namespace.
static std::string filePathFromUrn(const std::string &urn, bool strictUrn=false)
Extract the local file path from a URN.
T push_back(T... args)
A base class for image defects.
static const std::string URN_PREFIX
The prefix that a Policy URN starts with.
static bool looksLikeUrn(const std::string &s, bool strict=false)
Does s look like a URN? That is, does it start with URN_PREFIX or URN_PREFIX_ABBREV?
solver_t * s
static std::string productNameFromUrn(const std::string &urn, bool strictUrn=false)
Extract the product name from a URN.
T find(T... args)
T length(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
STL class.
T back(T... args)
T substr(T... args)
table::Key< int > transform
the definition of the UrnPolicyFile class