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 770 : Entity::~Entity() { } 43 : 44 0 : bool Entity::isNull() const { 45 0 : 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 276 : EntityId Entity::getEntityId() const { 59 276 : return entityId; 60 : } 61 : 62 0 : string Entity::getEntityIdEncrypted() const { 63 0 : return entityIdEncrypted; 64 : } 65 : 66 166 : string Entity::getEntityTypeName() const { 67 166 : return entityTypeName; 68 : } 69 : 70 0 : string Entity::getEntityVersion() const { 71 0 : return entityVersion; 72 : } 73 : 74 0 : string Entity::getInstanceVersion() const { 75 0 : return instanceVersion; 76 : } 77 : 78 280 : void Entity::setEntityId(EntityId e) { 79 280 : entityId = e; 80 280 : } 81 : 82 280 : void Entity::setEntityIdEncrypted(string s) { 83 280 : entityIdEncrypted = s; 84 280 : } 85 : 86 280 : void Entity::setEntityTypeName(string s) { 87 280 : entityTypeName = s; 88 280 : } 89 : 90 280 : void Entity::setEntityVersion(string s) { 91 280 : entityVersion = s; 92 280 : } 93 : 94 280 : void Entity::setInstanceVersion(string s) { 95 280 : instanceVersion = s; 96 280 : } 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 726 : Entity::Entity() : entityId() { 119 726 : entityIdEncrypted = ""; 120 726 : entityTypeName = ""; 121 726 : entityVersion = ""; 122 726 : instanceVersion = ""; 123 726 : } 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 830 : string Entity::getXMLValue(string xml, string parm) const { 159 830 : string::size_type n = xml.find(parm+"=",0); 160 830 : if (n == string::npos) 161 0 : return ""; 162 830 : string::size_type beg = xml.find("\"",n + parm.length()); 163 830 : if (beg == string::npos) 164 0 : return ""; 165 830 : beg++; 166 830 : string::size_type end = xml.find("\"",beg); 167 830 : if (end == string::npos) 168 0 : return ""; 169 830 : return xml.substr(beg,(end - beg)); 170 : } 171 : 172 0 : string Entity::validXML() const { 173 : // Check for any null values. 174 0 : string msg = "Null values detected in Entity " + entityId.getId(); 175 0 : if (entityId.isNull() || 176 0 : entityIdEncrypted.length() == 0 || 177 0 : entityTypeName.length() == 0 || 178 0 : entityVersion.length() == 0 || 179 0 : instanceVersion.length() == 0) 180 0 : return msg; 181 : // Check the entityId for the correct format. 182 0 : return EntityId::validate(entityId.toString()); 183 0 : } 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 0 : string Entity::toXML() const { 202 0 : string msg = validXML(); 203 0 : if (msg.length() != 0) 204 0 : throw InvalidDataException(msg); 205 0 : string s = "<Entity entityId=\"" + entityId.toString() + 206 0 : "\" entityIdEncrypted=\"" + entityIdEncrypted + 207 0 : "\" entityTypeName=\"" + entityTypeName + 208 0 : "\" schemaVersion=\"" + entityVersion + 209 0 : "\" documentVersion=\"" + instanceVersion + "\"/>"; 210 0 : return s; 211 0 : } 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 166 : void Entity::setFromXML(string xml) { 226 166 : entityId.setId(getXMLValue(xml,"entityId")); 227 166 : entityIdEncrypted = getXMLValue(xml,"entityIdEncrypted"); 228 166 : entityTypeName = getXMLValue(xml,"entityTypeName"); 229 166 : entityVersion = getXMLValue(xml,"schemaVersion"); 230 166 : instanceVersion = getXMLValue(xml,"documentVersion"); 231 332 : if (entityIdEncrypted.length() == 0 || 232 332 : entityTypeName.length() == 0 || 233 498 : entityVersion.length() == 0 || 234 166 : instanceVersion.length() == 0) 235 0 : throw InvalidArgumentException("Null values detected in Entity " + entityId.toString()); 236 166 : } 237 : 238 0 : void Entity::toBin(EndianOSStream& eoss) const { 239 0 : entityId.toBin(eoss); 240 0 : eoss.writeString(entityIdEncrypted); 241 0 : eoss.writeString(entityTypeName); 242 0 : eoss.writeString(entityVersion); 243 0 : eoss.writeString(instanceVersion); 244 0 : } 245 : 246 0 : Entity Entity::fromBin(EndianIStream& eis) { 247 0 : Entity entity; 248 0 : entity.setEntityId(EntityId(eis.readString())); 249 0 : entity.setEntityIdEncrypted(eis.readString()); 250 0 : entity.setEntityTypeName(eis.readString()); 251 0 : entity.setEntityVersion(eis.readString()); 252 0 : entity.setInstanceVersion(eis.readString()); 253 0 : return entity; 254 0 : } 255 : } // End namespace asdm