$search
00001 #include "Identity.h" 00002 00003 #include <boost/algorithm/string.hpp> 00004 #include <boost/lexical_cast.hpp> 00005 00006 #include <vector> 00007 #include <iostream> 00008 #include <sstream> 00009 00010 #include "SpicaException.h" 00011 00012 using namespace boost::algorithm; 00013 00014 namespace castor { namespace spica { 00015 00016 Identity::Identity() : 00017 id(0), concept(""), rep(""), name("") 00018 { 00019 } 00020 00021 Identity::Identity(const Identifier identifier) : 00022 id(0), concept(""), rep(""), name("") 00023 { 00024 00025 std::vector<std::string> result; 00026 00027 split(result, identifier, is_any_of(";")); 00028 00029 for (size_t i = 0; i < result.size(); i++) { 00030 00031 std::vector<std::string> keyvalue; 00032 00033 split(keyvalue, result[i], is_any_of("=")); 00034 00035 if (keyvalue.size() != 2) { 00036 std::ostringstream ss; 00037 ss << "Unable to parse component " << i << " of identifier " << identifier << std::endl; 00038 std::cout << ss.str() << std::endl; 00039 throw SpicaException(ss.str()); 00040 } 00041 00042 trim(keyvalue[0]); 00043 to_lower(keyvalue[0]); 00044 00045 if (keyvalue[0] == "id") { 00046 00047 this->id = boost::lexical_cast<SpicaId>(keyvalue[1]); 00048 00049 } else if (keyvalue[0] == "concept") { 00050 00051 this->concept = keyvalue[1]; 00052 00053 } else if (keyvalue[0] == "rep") { 00054 00055 this->rep = keyvalue[1]; 00056 00057 } else if (keyvalue[0] == "name") { 00058 00059 this->name = keyvalue[1]; 00060 } 00061 00062 } 00063 } 00064 00065 Identity::Identity(SpicaId id, const std::string concept, const std::string rep, const std::string name) : 00066 id(id), concept(concept), rep(rep), name(name) 00067 { 00068 } 00069 00070 Identity::Identity(const Identity &other) : 00071 id(other.id), concept(other.concept), rep(other.rep), name(other.name) 00072 { 00073 } 00074 00075 bool Identity::isMatch(const Identifier other) const { 00076 return isMatch(Identity(other)); 00077 } 00078 00079 bool Identity::isMatch(const Identity &other) const { 00080 return (!(*this < other) && !(other < *this)); 00081 } 00082 00083 00084 bool Identity::hasId() const { 00085 return (this->id > 0); 00086 } 00087 00088 SpicaId Identity::getId() const { 00089 return this->id; 00090 } 00091 00092 bool Identity::hasConcept() const { 00093 return (this->concept.size() > 0); 00094 } 00095 00096 const std::string Identity::getConcept() const { 00097 return this->concept; 00098 } 00099 00100 bool Identity::hasRep() const { 00101 return (this->rep.size() > 0); 00102 } 00103 00104 const std::string Identity::getRep() const { 00105 return this->rep; 00106 } 00107 00108 bool Identity::hasName() const { 00109 return (this->name.size() > 0); 00110 } 00111 00112 const std::string Identity::getName() const { 00113 return this->name; 00114 } 00115 00116 std::string Identity::str() const { 00117 00118 std::ostringstream ss; 00119 00120 int count = 0; 00121 00122 if (hasId()) { 00123 ss << "id=" << this->id; 00124 count++; 00125 } 00126 00127 if (hasName()) { 00128 ss << (count > 0 ? ";" : "") << "name=" << this->name; 00129 count++; 00130 00131 } else if (hasConcept()) { 00132 ss << (count > 0 ? ";" : "") << "concept=" << this->concept; 00133 count++; 00134 00135 } else if (hasRep()) { 00136 ss << (count > 0 ? ";" : "") << "rep=" << this->rep; 00137 count++; 00138 } 00139 00140 return ss.str(); 00141 } 00142 00143 Identity &Identity::operator=(const Identity &other) { 00144 00145 this->id = other.id; 00146 this->concept = other.concept; 00147 this->rep = other.rep; 00148 this->name = other.name; 00149 00150 return *this; 00151 } 00152 00153 Identity &Identity::operator=(const Identifier other) { 00154 00155 Identity identity(other); 00156 00157 this->id = identity.id; 00158 this->concept = identity.concept; 00159 this->rep = identity.rep; 00160 this->name = identity.name; 00161 00162 return *this; 00163 } 00164 00165 bool operator==(const Identity &one, 00166 const Identity &other) 00167 { 00168 return (!(one < other) && !(other < one)); 00169 } 00170 00171 bool operator<(const Identity &one, 00172 const Identity &other) 00173 { 00174 00175 if (one.hasId() && other.hasId()) { 00176 if (one.getId() < other.getId()) return true; 00177 if (one.getId() != other.getId()) return false; 00178 } 00179 00180 if (one.hasConcept() && other.hasConcept()) { 00181 if (one.getConcept() < other.getConcept()) return true; 00182 if (one.getConcept() != other.getConcept()) return false; 00183 } 00184 00185 if (one.hasRep() && other.hasRep()) { 00186 if (one.getRep() < other.getRep()) return true; 00187 if (one.getRep() != other.getRep()) return false; 00188 } 00189 00190 if (one.hasName() && other.hasName()) { 00191 if (one.getName() < other.getName()) return true; 00192 if (one.getName() != other.getName()) return false; 00193 } 00194 00195 return false; 00196 } 00197 00198 00199 } } 00200 00201