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 Entity.cpp 25 : */ 26 : 27 : #include <alma/ASDM/Entity.h> 28 : #include <alma/ASDM/OutOfBoundsException.h> 29 : #include <alma/ASDM/InvalidArgumentException.h> 30 : #include <alma/ASDM/InvalidDataException.h> 31 : 32 : using namespace std; 33 : 34 : namespace asdm { 35 : 36 : // Entity constructors 37 0 : Entity::Entity(const string &s) { 38 0 : setFromXML(s); 39 0 : } 40 : 41 : // Entity destructor 42 24492 : Entity::~Entity() { } 43 : 44 295 : bool Entity::isNull() const { 45 295 : return entityId.isNull(); 46 : } 47 : 48 0 : string Entity::toString() const { 49 0 : return toXML(); 50 : } 51 : 52 0 : bool Entity::equals(const Entity &x) const { 53 0 : return *this == x; 54 : } 55 : 56 : // Getters and Setters 57 : 58 6251 : EntityId Entity::getEntityId() const { 59 6251 : return entityId; 60 : } 61 : 62 0 : string Entity::getEntityIdEncrypted() const { 63 0 : return entityIdEncrypted; 64 : } 65 : 66 4503 : string Entity::getEntityTypeName() const { 67 4503 : return entityTypeName; 68 : } 69 : 70 14 : string Entity::getEntityVersion() const { 71 14 : return entityVersion; 72 : } 73 : 74 0 : string Entity::getInstanceVersion() const { 75 0 : return instanceVersion; 76 : } 77 : 78 8709 : void Entity::setEntityId(EntityId e) { 79 8709 : entityId = e; 80 8709 : } 81 : 82 8412 : void Entity::setEntityIdEncrypted(string s) { 83 8412 : entityIdEncrypted = s; 84 8412 : } 85 : 86 8412 : void Entity::setEntityTypeName(string s) { 87 8412 : entityTypeName = s; 88 8412 : } 89 : 90 8412 : void Entity::setEntityVersion(string s) { 91 8412 : entityVersion = s; 92 8412 : } 93 : 94 8412 : void Entity::setInstanceVersion(string s) { 95 8412 : instanceVersion = s; 96 8412 : } 97 : 98 : // Friend functions 99 : 100 0 : ostream & operator << ( ostream &o, const Entity &x ) { 101 0 : o << x.toXML(); 102 0 : return o; 103 : } 104 : 105 0 : Entity Entity::getEntity(StringTokenizer &t) { 106 : try { 107 0 : string s = t.nextToken("<>"); 108 0 : if (s == " ") 109 0 : s = t.nextToken(); 110 0 : Entity e; 111 0 : e.setFromXML(s); 112 0 : return e; 113 0 : } catch (const OutOfBoundsException &err) { 114 0 : throw InvalidArgumentException("Unexpected end-of-string!"); 115 0 : } 116 : } 117 : 118 21175 : Entity::Entity() : entityId() { 119 21175 : entityIdEncrypted = ""; 120 21175 : entityTypeName = ""; 121 21175 : entityVersion = ""; 122 21175 : instanceVersion = ""; 123 21175 : } 124 : 125 : #ifndef WITHOUT_ACS 126 : Entity::Entity(asdmIDLTypes::IDLEntity &x) : entityId(string(x.entityId)) { 127 : entityIdEncrypted = string(x.entityIdEncrypted); 128 : entityTypeName = string(x.entityTypeName); 129 : entityVersion = string(x.entityVersion); 130 : instanceVersion = string(x.instanceVersion); 131 : } 132 : #endif 133 : 134 0 : Entity::Entity(string id, string sEncrypted, string sTypeName, 135 0 : string sVersion, string sInstanceVersion) : entityId(id){ 136 0 : entityIdEncrypted = sEncrypted; 137 0 : entityTypeName = sTypeName; 138 0 : entityVersion = sVersion; 139 0 : instanceVersion = sInstanceVersion; 140 0 : } 141 : 142 0 : bool Entity::operator == (const Entity& e) const { 143 0 : return entityId.getId() == e.entityId.getId() && 144 0 : entityIdEncrypted == e.entityIdEncrypted && 145 0 : entityTypeName == e.entityTypeName && 146 0 : entityVersion == e.entityVersion && 147 0 : instanceVersion == e.instanceVersion; 148 : } 149 : 150 0 : bool Entity::operator != (const Entity& e) const { 151 0 : return entityId.getId() != e.entityId.getId() || 152 0 : entityIdEncrypted != e.entityIdEncrypted || 153 0 : entityTypeName != e.entityTypeName || 154 0 : entityVersion != e.entityVersion || 155 0 : instanceVersion != e.instanceVersion; 156 : } 157 : 158 22515 : string Entity::getXMLValue(string xml, string parm) const { 159 22515 : string::size_type n = xml.find(parm+"=",0); 160 22515 : if (n == string::npos) 161 0 : return ""; 162 22515 : string::size_type beg = xml.find("\"",n + parm.length()); 163 22515 : if (beg == string::npos) 164 0 : return ""; 165 22515 : beg++; 166 22515 : string::size_type end = xml.find("\"",beg); 167 22515 : if (end == string::npos) 168 0 : return ""; 169 22515 : return xml.substr(beg,(end - beg)); 170 : } 171 : 172 815 : string Entity::validXML() const { 173 : // Check for any null values. 174 815 : string msg = "Null values detected in Entity " + entityId.getId(); 175 1630 : if (entityId.isNull() || 176 1630 : entityIdEncrypted.length() == 0 || 177 1630 : entityTypeName.length() == 0 || 178 2445 : entityVersion.length() == 0 || 179 815 : instanceVersion.length() == 0) 180 0 : return msg; 181 : // Check the entityId for the correct format. 182 1630 : return EntityId::validate(entityId.toString()); 183 815 : } 184 : 185 : /** 186 : * Return the values of this Entity as an XML-formated string. 187 : * As an example, for the Main table in the ASDM, the toXML 188 : * method would give: 189 : * <ul> 190 : * <li> <Entity 191 : * <li> entityId="uid://X0000000000000079/X00000000" 192 : * <li> entityIdEncrypted="none" 193 : * <li> entityTypeName="Main" 194 : * <li> schemaVersion="1" 195 : * <li> documentVersion="1"/> 196 : * </ul> 197 : * 198 : * @return The values of this Entity as an XML-formated string. 199 : * @throws IllegalStateException if the values of this Entity do not conform to the proper XML format. 200 : */ 201 815 : string Entity::toXML() const { 202 815 : string msg = validXML(); 203 815 : if (msg.length() != 0) 204 0 : throw InvalidDataException(msg); 205 1630 : string s = "<Entity entityId=\"" + entityId.toString() + 206 1630 : "\" entityIdEncrypted=\"" + entityIdEncrypted + 207 1630 : "\" entityTypeName=\"" + entityTypeName + 208 1630 : "\" schemaVersion=\"" + entityVersion + 209 2445 : "\" documentVersion=\"" + instanceVersion + "\"/>"; 210 1630 : return s; 211 815 : } 212 : 213 : #ifndef WITHOUT_ACS 214 : asdmIDLTypes::IDLEntity Entity::toIDLEntity() const { 215 : asdmIDLTypes::IDLEntity e; 216 : e.entityId = CORBA::string_dup(entityId.getId().c_str()); 217 : e.entityIdEncrypted = CORBA::string_dup(entityIdEncrypted.c_str()); 218 : e.entityTypeName = CORBA::string_dup(entityTypeName.c_str()); 219 : e.entityVersion = CORBA::string_dup(entityVersion.c_str()); 220 : e.instanceVersion = CORBA::string_dup(instanceVersion.c_str()); 221 : return e; 222 : } 223 : #endif 224 : 225 4503 : void Entity::setFromXML(string xml) { 226 4503 : entityId.setId(getXMLValue(xml,"entityId")); 227 4503 : entityIdEncrypted = getXMLValue(xml,"entityIdEncrypted"); 228 4503 : entityTypeName = getXMLValue(xml,"entityTypeName"); 229 4503 : entityVersion = getXMLValue(xml,"schemaVersion"); 230 4503 : instanceVersion = getXMLValue(xml,"documentVersion"); 231 9006 : if (entityIdEncrypted.length() == 0 || 232 9006 : entityTypeName.length() == 0 || 233 13509 : entityVersion.length() == 0 || 234 4503 : instanceVersion.length() == 0) 235 0 : throw InvalidArgumentException("Null values detected in Entity " + entityId.toString()); 236 4503 : } 237 : 238 44 : void Entity::toBin(EndianOSStream& eoss) const { 239 44 : entityId.toBin(eoss); 240 44 : eoss.writeString(entityIdEncrypted); 241 44 : eoss.writeString(entityTypeName); 242 44 : eoss.writeString(entityVersion); 243 44 : eoss.writeString(instanceVersion); 244 44 : } 245 : 246 138 : Entity Entity::fromBin(EndianIStream& eis) { 247 138 : Entity entity; 248 138 : entity.setEntityId(EntityId(eis.readString())); 249 138 : entity.setEntityIdEncrypted(eis.readString()); 250 138 : entity.setEntityTypeName(eis.readString()); 251 138 : entity.setEntityVersion(eis.readString()); 252 138 : entity.setInstanceVersion(eis.readString()); 253 138 : return entity; 254 0 : } 255 : } // End namespace asdm