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 : #include <regex> 29 : 30 : #include <alma/ASDM/EntityId.h> 31 : #include <alma/ASDM/OutOfBoundsException.h> 32 : #include <alma/ASDM/InvalidArgumentException.h> 33 : using namespace std; 34 : 35 : namespace asdm { 36 : 37 : static bool EntityIdVerbose = getenv("ASDM_DEBUG") != NULL; 38 : 39 0 : EntityId EntityId::getEntityId(StringTokenizer &t) { 40 : try { 41 0 : string value = t.nextToken(); 42 0 : return EntityId (value); 43 0 : } catch (const OutOfBoundsException &err) { 44 0 : throw InvalidArgumentException("Unexpected end-of-string!"); 45 0 : } 46 : } 47 : 48 : /** 49 : * Returns a null string if the string x contains a valid 50 : * entity-id. Otherwise, the string contains the error message. 51 : */ 52 0 : string EntityId::validate(string x) { 53 : 54 0 : if (EntityIdVerbose) 55 0 : cout << "EntityId::validate. Validating '" << x << "'." << endl; 56 : 57 : // std::regex is thread-safe in this context 58 : 59 : // We will test two possible syntaxes for the EntityId, since EVLA did not want to follow 60 : // the rules established by ALMA. 61 : // 62 0 : std::regex ALMAbasicUIDRegex("^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$"); 63 0 : std::regex EVLAbasicUIDRegex("^[uU][iI][dD]:///?evla/[0-9a-zA-Z]+/.+$"); 64 : 65 0 : const char * theUID_p = x.c_str(); 66 0 : std::cmatch whatALMA, whatEVLA; 67 0 : string result = ""; 68 : 69 0 : if (!(std::regex_match(theUID_p, whatALMA, ALMAbasicUIDRegex) || 70 0 : std::regex_match(theUID_p, whatEVLA, EVLAbasicUIDRegex)) 71 : ) 72 0 : result = "Invalid format for EntityId: " + x; 73 : 74 0 : if (EntityIdVerbose) 75 0 : cout << "EntityId::validate. Validation message is '" << result << "'. (An empty message is a good sign.)" << endl; 76 : 77 0 : return result; 78 : 79 : /* 80 : * This is the previous version of the code which was using 81 : * the regex machinery contained in libc. I was not sure 82 : * of its thread safetiness... 83 : 84 : // Check the entityId for the correct format. 85 : // the old one char * rexp = "^uid://X[a-fA-F0-9]\\+/X[a-fA-F0-9]\\+\\(/X[a-fA-F0-9]\\+\\)\\?$"; 86 : char * rexp = "^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$"; 87 : 88 : regex_t preg; 89 : regcomp(&preg, rexp, REG_NOSUB); 90 : if ( regexec(&preg, x.c_str(), 0, 0, 0) ) 91 : result = msg; 92 : 93 : regfree(&preg); 94 : return string(""); 95 : */ 96 0 : } 97 : 98 0 : EntityId::EntityId(const string &id) { 99 0 : string msg = validate(id); 100 0 : if (msg.length() != 0) 101 0 : throw InvalidArgumentException(msg); 102 0 : this->id = id; 103 0 : } 104 : 105 : #ifndef WITHOUT_ACS 106 : EntityId::EntityId(asdmIDLTypes::IDLEntityId &x) { 107 : string tmp(x.value); 108 : string msg = validate(tmp); 109 : if (msg.length() != 0) 110 : throw InvalidArgumentException(msg); 111 : this->id = tmp; 112 : } 113 : #endif 114 : 115 0 : void EntityId::toBin(EndianOSStream& eoss) const { 116 0 : eoss.writeString(id); 117 0 : } 118 : 119 0 : EntityId EntityId::fromBin(EndianIStream& eis) { 120 0 : return EntityId(eis.readString()); 121 : } 122 : } // End namespace asdm