5 #include <xercesc/parsers/XercesDOMParser.hpp> 9 #include <boost/algorithm/string.hpp> 10 #include <boost/algorithm/string/replace.hpp> 24 replaceWithRegexp(std::smatch
const&
matches,
27 std::stringstream
str;
30 for (
size_t i = 1;
i < matches.size(); ++
i) {
35 if (!matchedString.empty())
36 boost::algorithm::replace_all(result, str.str(), matchedString);
45 int FileLocator::s_numberOfInstances = 0;
48 : m_destination(
"any") {
56 catch (XMLException
const&
e) {
65 init(catUrl, fallback);
104 DOMElement* ruleElement =
static_cast<DOMElement *
>(ruleNode);
109 if (destinationMatchRegexp.empty()) {
110 destinationMatchRegexp =
".*";
114 =
toString(ruleElement->getAttribute(
uStr(
"path-match").ptr()));
116 =
toString(ruleElement->getAttribute(
uStr(
"result").ptr()));
118 =
toString(ruleElement->getAttribute(
uStr(
"chain").ptr()));
122 rule.destinationMatch.assign(destinationMatchRegexp);
125 rules[protocol].push_back(rule);
135 throw cms::Exception(
"TrivialFileCatalog",
"edm::SiteLocalConfigService is not available");
137 m_url = (fallback ? localconfservice->fallbackDataCatalog() : localconfservice->dataCatalog());
142 if (m_url.find(
"file:") == std::string::npos) {
143 throw cms::Exception(
"TrivialFileCatalog",
"TrivialFileCatalog::connect: Malformed url for file catalog configuration");
146 m_url = m_url.erase(0, m_url.find(
":") + 1);
148 std::vector<std::string> tokens;
152 if (tokens.size() == 2) {
154 std::vector<std::string> optionTokens;
160 for (
size_t oi = 0, oe = optionTokens.size(); oi != oe; ++oi) {
162 std::vector<std::string> argTokens;
165 if (argTokens.size() != 2) {
166 throw cms::Exception(
"TrivialFileCatalog",
"TrivialFileCatalog::connect: Malformed url for file catalog configuration");
169 if (argTokens[0] ==
"protocol") {
171 }
else if (argTokens[0] ==
"destination") {
178 throw cms::Exception(
"TrivialFileCatalog",
"TrivialFileCatalog::connect: protocol was not supplied in the contact string");
181 std::ifstream configFile;
187 if (!configFile.good() || !configFile.is_open()) {
188 throw cms::Exception(
"TrivialFileCatalog",
"TrivialFileCatalog::connect: Unable to open trivial file catalog " +
m_filename);
193 auto parser = std::make_unique<XercesDOMParser>();
195 parser->setValidationScheme(XercesDOMParser::Val_Auto);
196 parser->setDoNamespaces(
false);
198 DOMDocument*
doc =
parser->getDocument();
215 DOMNodeList*
rules = doc->getElementsByTagName(
uStr(
"lfn-to-pfn").ptr());
216 XMLSize_t
const ruleTagsNum = rules->getLength();
220 for (XMLSize_t
i = 0;
i < ruleTagsNum; ++
i) {
221 DOMNode* ruleNode = rules->item(
i);
227 DOMNodeList*
rules = doc->getElementsByTagName(
uStr(
"pfn-to-lfn").ptr());
228 XMLSize_t ruleTagsNum = rules->getLength();
230 for (XMLSize_t
i = 0;
i < ruleTagsNum; ++
i) {
231 DOMNode* ruleNode = rules->item(
i);
236 catch (xercesc::DOMException
const&
e) {
237 throw cms::Exception(
"TrivialFileCatalog") <<
"Xerces XML parser threw this exception: " <<
cStr(e.getMessage()).ptr() << std::endl;
250 ProtocolRules::const_iterator
const rulesIterator = protocolRules.find(protocol);
251 if (rulesIterator == protocolRules.end()) {
255 Rules const&
rules = (*(rulesIterator)).second;
257 std::smatch destinationMatches;
258 std::smatch nameMatches;
261 for (Rules::const_iterator
i = rules.begin();
i != rules.end(); ++
i) {
263 if (!std::regex_match(destination, destinationMatches,
i->destinationMatch)) {
267 if (!std::regex_match(name,
i->pathMatch)) {
274 if ((direct ==
true) && (chain !=
"")) {
275 name =
applyRules(protocolRules, chain, destination, direct, name);
281 std::regex_match(name, nameMatches,
i->pathMatch);
282 name = replaceWithRegexp(nameMatches,
i->result);
284 if ((direct ==
false) && (chain !=
"")) {
285 name =
applyRules(protocolRules, chain, destination, direct, name);
std::map< std::string, Rules > ProtocolRules
static int s_numberOfInstances
std::string lfn(std::string const &ipfn) const
std::string m_destination
static std::string const input
U second(std::pair< T, U > const &p)
ProtocolRules m_inverseRules
ZStr< char > cStr(XMLCh const *str)
std::string convert(std::string const &input, ProtocolRules const &rules, bool direct) const
ZStr< XMLCh > uStr(char const *str)
std::string pfn(std::string const &ilfn) const
ProtocolRules m_directRules
std::vector< Rule > Rules
void parseRule(xercesc::DOMNode *ruleNode, ProtocolRules &rules)
std::vector< std::string > m_protocols
std::string applyRules(ProtocolRules const &protocolRules, std::string const &protocol, std::string const &destination, bool direct, std::string name) const
void init(std::string const &catUrl, bool fallback)