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 22878 : 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 5837 : EntityId Entity::getEntityId() const { 59 5837 : return entityId; 60 : } 61 : 62 0 : string Entity::getEntityIdEncrypted() const { 63 0 : return entityIdEncrypted; 64 : } 65 : 66 4162 : string Entity::getEntityTypeName() const { 67 4162 : 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 8131 : void Entity::setEntityId(EntityId e) { 79 8131 : entityId = e; 80 8131 : } 81 : 82 7834 : void Entity::setEntityIdEncrypted(string s) { 83 7834 : entityIdEncrypted = s; 84 7834 : } 85 : 86 7834 : void Entity::setEntityTypeName(string s) { 87 7834 : entityTypeName = s; 88 7834 : } 89 : 90 7834 : void Entity::setEntityVersion(string s) { 91 7834 : entityVersion = s; 92 7834 : } 93 : 94 7834 : void Entity::setInstanceVersion(string s) { 95 7834 : instanceVersion = s; 96 7834 : } 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 : } 116 : } 117 : 118 19696 : Entity::Entity() : entityId() { 119 19696 : entityIdEncrypted = ""; 120 19696 : entityTypeName = ""; 121 19696 : entityVersion = ""; 122 19696 : instanceVersion = ""; 123 19696 : } 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 20810 : string Entity::getXMLValue(string xml, string parm) const { 159 20810 : string::size_type n = xml.find(parm+"=",0); 160 20810 : if (n == string::npos) 161 0 : return ""; 162 20810 : string::size_type beg = xml.find("\"",n + parm.length()); 163 20810 : if (beg == string::npos) 164 0 : return ""; 165 20810 : beg++; 166 20810 : string::size_type end = xml.find("\"",beg); 167 20810 : if (end == string::npos) 168 0 : return ""; 169 20810 : return xml.substr(beg,(end - beg)); 170 : } 171 : 172 815 : string Entity::validXML() const { 173 : // Check for any null values. 174 1630 : 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 815 : return EntityId::validate(entityId.toString()); 183 : } 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 1630 : 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 : } 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 4162 : void Entity::setFromXML(string xml) { 226 4162 : entityId.setId(getXMLValue(xml,"entityId")); 227 4162 : entityIdEncrypted = getXMLValue(xml,"entityIdEncrypted"); 228 4162 : entityTypeName = getXMLValue(xml,"entityTypeName"); 229 4162 : entityVersion = getXMLValue(xml,"schemaVersion"); 230 4162 : instanceVersion = getXMLValue(xml,"documentVersion"); 231 8324 : if (entityIdEncrypted.length() == 0 || 232 8324 : entityTypeName.length() == 0 || 233 12486 : entityVersion.length() == 0 || 234 4162 : instanceVersion.length() == 0) 235 0 : throw InvalidArgumentException("Null values detected in Entity " + entityId.toString()); 236 4162 : } 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 120 : Entity Entity::fromBin(EndianIStream& eis) { 247 120 : Entity entity; 248 120 : entity.setEntityId(EntityId(eis.readString())); 249 120 : entity.setEntityIdEncrypted(eis.readString()); 250 120 : entity.setEntityTypeName(eis.readString()); 251 120 : entity.setEntityVersion(eis.readString()); 252 120 : entity.setInstanceVersion(eis.readString()); 253 120 : return entity; 254 : } 255 : } // End namespace asdm