Line data Source code
1 : /* 2 : * ALMA - Atacama Large Millimeter Array 3 : * (c) European Southern Observatory, 2002 4 : * (c) Associated Universities Inc., 2002 5 : * Copyright by ESO (in the framework of the ALMA collaboration), 6 : * Copyright by AUI (in the framework of the ALMA collaboration), 7 : * All rights reserved. 8 : * 9 : * This library is free software; you can redistribute it and/or 10 : * modify it under the terms of the GNU Lesser General Public 11 : * License as published by the Free software Foundation; either 12 : * version 2.1 of the License, or (at your option) any later version. 13 : * 14 : * This library is distributed in the hope that it will be useful, 15 : * but WITHOUT ANY WARRANTY, without even the implied warranty of 16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 : * Lesser General Public License for more details. 18 : * 19 : * You should have received a copy of the GNU Lesser General Public 20 : * License along with this library; if not, write to the Free Software 21 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 22 : * MA 02111-1307 USA 23 : * 24 : * File EntityId.cpp 25 : */ 26 : //#define _POSIX_C_SOURCE 27 : 28 : #ifndef WITHOUT_BOOST 29 : #include <boost/regex.hpp> 30 : #else 31 : #include <regex> 32 : #endif 33 : 34 : #include <alma/ASDM/EntityId.h> 35 : #include <alma/ASDM/OutOfBoundsException.h> 36 : #include <alma/ASDM/InvalidArgumentException.h> 37 : using namespace std; 38 : 39 : namespace asdm { 40 : 41 : static bool EntityIdVerbose = getenv("ASDM_DEBUG") != NULL; 42 : 43 0 : EntityId EntityId::getEntityId(StringTokenizer &t) { 44 : try { 45 0 : string value = t.nextToken(); 46 0 : return EntityId (value); 47 0 : } catch (const OutOfBoundsException &err) { 48 0 : throw InvalidArgumentException("Unexpected end-of-string!"); 49 : } 50 : } 51 : 52 : /** 53 : * Returns a null string if the string x contains a valid 54 : * entity-id. Otherwise, the string contains the error message. 55 : */ 56 34167 : string EntityId::validate(string x) { 57 : 58 34167 : if (EntityIdVerbose) 59 0 : cout << "EntityId::validate. Validating '" << x << "'." << endl; 60 : 61 : // Here we use the boost machinery for the regular expressions, assuming its thread safety. 62 : // std::regex (optionally used here) is as thread-safe in this context 63 : 64 : // We will test two possible syntaxes for the EntityId, since EVLA did not want to follow 65 : // the rules established by ALMA. 66 : // 67 : #ifndef WITHOUT_BOOST 68 : boost::regex ALMAbasicUIDRegex("^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$"); 69 : boost::regex EVLAbasicUIDRegex("^[uU][iI][dD]:///?evla/[0-9a-zA-Z]+/.+$"); 70 : 71 : const char * theUID_p = x.c_str(); 72 : boost::cmatch whatALMA, whatEVLA; 73 : string result = ""; 74 : 75 : if (!(boost::regex_match(theUID_p, whatALMA, ALMAbasicUIDRegex) || 76 : boost::regex_match(theUID_p, whatEVLA, EVLAbasicUIDRegex)) 77 : ) 78 : result = "Invalid format for EntityId: " + x; 79 : #else 80 68334 : std::regex ALMAbasicUIDRegex("^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$"); 81 68334 : std::regex EVLAbasicUIDRegex("^[uU][iI][dD]:///?evla/[0-9a-zA-Z]+/.+$"); 82 : 83 34167 : const char * theUID_p = x.c_str(); 84 102501 : std::cmatch whatALMA, whatEVLA; 85 34167 : string result = ""; 86 : 87 34508 : if (!(std::regex_match(theUID_p, whatALMA, ALMAbasicUIDRegex) || 88 341 : std::regex_match(theUID_p, whatEVLA, EVLAbasicUIDRegex)) 89 : ) 90 0 : result = "Invalid format for EntityId: " + x; 91 : #endif 92 : 93 34167 : if (EntityIdVerbose) 94 0 : cout << "EntityId::validate. Validation message is '" << result << "'. (An empty message is a good sign.)" << endl; 95 : 96 68334 : return result; 97 : 98 : /* 99 : * This is the previous version of the code which was using 100 : * the regex machinery contained in libc. I was not sure 101 : * of its thread safetiness... 102 : 103 : // Check the entityId for the correct format. 104 : // the old one char * rexp = "^uid://X[a-fA-F0-9]\\+/X[a-fA-F0-9]\\+\\(/X[a-fA-F0-9]\\+\\)\\?$"; 105 : char * rexp = "^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$"; 106 : 107 : regex_t preg; 108 : regcomp(&preg, rexp, REG_NOSUB); 109 : if ( regexec(&preg, x.c_str(), 0, 0, 0) ) 110 : result = msg; 111 : 112 : regfree(&preg); 113 : return string(""); 114 : */ 115 : } 116 : 117 22609 : EntityId::EntityId(const string &id) { 118 45218 : string msg = validate(id); 119 22609 : if (msg.length() != 0) 120 0 : throw InvalidArgumentException(msg); 121 22609 : this->id = id; 122 22609 : } 123 : 124 : #ifndef WITHOUT_ACS 125 : EntityId::EntityId(asdmIDLTypes::IDLEntityId &x) { 126 : string tmp(x.value); 127 : string msg = validate(tmp); 128 : if (msg.length() != 0) 129 : throw InvalidArgumentException(msg); 130 : this->id = tmp; 131 : } 132 : #endif 133 : 134 44 : void EntityId::toBin(EndianOSStream& eoss) const { 135 44 : eoss.writeString(id); 136 44 : } 137 : 138 0 : EntityId EntityId::fromBin(EndianIStream& eis) { 139 0 : return EntityId(eis.readString()); 140 : } 141 : } // End namespace asdm