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 EntityRef.cpp 25 : */ 26 : 27 : #include <alma/ASDM/EntityRef.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 0 : EntityRef EntityRef::getEntityRef(StringTokenizer &t) { 37 : try { 38 0 : string s = t.nextToken("<>"); 39 0 : if (s == " ") 40 0 : s = t.nextToken(); 41 0 : EntityRef e; 42 0 : e.setFromXML(s); 43 0 : return e; 44 0 : } catch (const OutOfBoundsException &err) { 45 0 : throw InvalidArgumentException("Unexpected end-of-string!"); 46 0 : } 47 : } 48 : 49 0 : EntityRef::EntityRef() : entityId(), partId() { 50 0 : entityTypeName = ""; 51 0 : instanceVersion = ""; 52 0 : } 53 : 54 : #ifndef WITHOUT_ACS 55 : EntityRef::EntityRef(asdmIDLTypes::IDLEntityRef &x) : entityId(string(x.entityId)), 56 : partId(string(x.partId)) { 57 : entityTypeName = string(x.entityTypeName); 58 : instanceVersion = string(x.instanceVersion); 59 : } 60 : #endif 61 : 62 0 : EntityRef::EntityRef(string id, string pId, string sTypeName, 63 0 : string sInstanceVersion) : entityId(id), partId(pId) { 64 0 : entityTypeName = sTypeName; 65 0 : instanceVersion = sInstanceVersion; 66 0 : } 67 : 68 0 : bool EntityRef::operator == (const EntityRef& e) const { 69 0 : return entityId.getId() == e.entityId.getId() && 70 0 : partId.toString() == e.partId.toString() && 71 0 : entityTypeName == e.entityTypeName && 72 0 : instanceVersion == e.instanceVersion; 73 : } 74 : 75 0 : bool EntityRef::operator != (const EntityRef& e) const { 76 0 : return entityId.getId() != e.entityId.getId() || 77 0 : partId.toString() != e.partId.toString()|| 78 0 : entityTypeName != e.entityTypeName || 79 0 : instanceVersion != e.instanceVersion; 80 : } 81 : 82 0 : string EntityRef::getXMLValue(string xml, string parm) const { 83 0 : string::size_type n = xml.find(parm,0); 84 0 : if (n == string::npos) 85 0 : return ""; 86 0 : string::size_type beg = xml.find("\"",n + parm.length()); 87 0 : if (beg == string::npos) 88 0 : return ""; 89 0 : beg++; 90 0 : string::size_type end = xml.find("\"",beg); 91 0 : if (end == string::npos) 92 0 : return ""; 93 0 : return xml.substr(beg,(end - beg)); 94 : } 95 : 96 0 : string EntityRef::validXML() const { 97 : // Check for any null values. PartId may be null. 98 0 : string msg = "Null values detected in EntityRef " + entityId.getId(); 99 0 : if (entityId.isNull() || 100 0 : entityTypeName.length() == 0 || 101 0 : instanceVersion.length() == 0) 102 0 : return msg; 103 : // Check the entityId for the correct format. 104 0 : msg = EntityId::validate(entityId.toString()); 105 0 : if (msg.length() != 0) 106 0 : return msg; 107 : // Check the entityId for the correct format. 108 0 : if (partId.toString().length() != 0) 109 0 : return PartId::validate(partId.toString()); 110 0 : return ""; 111 0 : } 112 : 113 : /** 114 : * Return the values of this EntityRef as an XML-formated string. 115 : * As an example, for the Main table in the ASDM, the toXML 116 : * method would give: 117 : * <ul> 118 : * <li> <EntityRef 119 : * <li> entityId="uid://X0000000000000079/X00000000" 120 : * <li> partId="X00000002" 121 : * <li> entityTypeName="Main" 122 : * <li> documentVersion="1"/> 123 : * </ul> 124 : * 125 : * @return The values of this EntityRef as an XML-formated string. 126 : */ 127 0 : string EntityRef::toXML() const { 128 0 : string msg = validXML(); 129 0 : if (msg.length() != 0) 130 0 : throw InvalidDataException(msg); 131 0 : string s = "<EntityRef entityId=\"" + entityId.toString(); 132 0 : if (partId.toString().length() != 0) 133 0 : s += "\" partId=\"" + partId.toString(); 134 0 : s += "\" entityTypeName=\"" + entityTypeName + 135 0 : "\" documentVersion=\"" + instanceVersion + "\"/>"; 136 0 : return s; 137 0 : } 138 : 139 : #ifndef WITHOUT_ACS 140 : asdmIDLTypes::IDLEntityRef EntityRef::toIDLEntityRef() const { 141 : asdmIDLTypes::IDLEntityRef e; 142 : e.entityId = CORBA::string_dup(entityId.getId().c_str()); 143 : e.partId = CORBA::string_dup(partId.toString().c_str()); 144 : e.entityTypeName = CORBA::string_dup(entityTypeName.c_str()); 145 : e.instanceVersion = CORBA::string_dup(instanceVersion.c_str()); 146 : return e; 147 : } 148 : #endif 149 : 150 0 : void EntityRef::setFromXML(string xml) { 151 0 : entityId.setId(getXMLValue(xml,"entityId")); 152 0 : partId.setId(getXMLValue(xml,"partId")); 153 0 : entityTypeName = getXMLValue(xml,"entityTypeName"); 154 0 : instanceVersion = getXMLValue(xml,"documentVersion"); 155 0 : string msg = validXML(); 156 0 : if (msg.length() != 0) { 157 0 : throw InvalidArgumentException(msg); 158 : } 159 0 : } 160 : 161 0 : void EntityRef::toBin(EndianOSStream& eoss) const { 162 0 : entityId.toBin(eoss); 163 0 : partId.toBin(eoss); 164 0 : eoss.writeString(entityTypeName); 165 0 : eoss.writeString(instanceVersion); 166 0 : } 167 : 168 0 : void EntityRef::toBin(const vector<EntityRef>& entityRef, EndianOSStream& eoss) { 169 0 : eoss.writeInt((int) entityRef.size()); 170 0 : for (unsigned int i = 0; i < entityRef.size(); i++) 171 0 : entityRef.at(i).toBin(eoss); 172 0 : } 173 : 174 0 : EntityRef EntityRef::fromBin(EndianIStream& eis) { 175 0 : std::string entityId = eis.readString(); 176 0 : std::string partId = eis.readString(); 177 0 : std::string entityTypeName = eis.readString(); 178 0 : std::string instanceVersion = eis.readString(); 179 0 : return EntityRef(entityId, partId, entityTypeName, instanceVersion); 180 0 : } 181 : 182 0 : vector<EntityRef> EntityRef::from1DBin(EndianIStream & eis) { 183 0 : int dim1 = eis.readInt(); 184 0 : vector<EntityRef> result; 185 0 : for (int i = 0; i < dim1; i++) 186 0 : result.push_back(EntityRef::fromBin(eis)); 187 0 : return result; 188 0 : } 189 : 190 : } // End namespace asdm