LCOV - code coverage report
Current view: top level - alma/ASDM - EntityRef.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 60 99 60.6 %
Date: 2024-11-06 17:42:47 Functions: 8 13 61.5 %

          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       25797 :         EntityRef::EntityRef() : entityId(), partId() {
      50       25797 :                 entityTypeName = "";
      51       25797 :                 instanceVersion = "";
      52       25797 :         }
      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       14525 :         EntityRef::EntityRef(string id, string pId, string sTypeName,
      63       14525 :                         string sInstanceVersion) : entityId(id), partId(pId) {
      64       14525 :                 entityTypeName = sTypeName;
      65       14525 :                 instanceVersion = sInstanceVersion;
      66       14525 :         }
      67             : 
      68       23232 :         bool EntityRef::operator == (const EntityRef& e) const {
      69       36144 :                 return  entityId.getId() == e.entityId.getId() &&
      70       49056 :                                 partId.toString() == e.partId.toString() &&
      71       49056 :                                 entityTypeName == e.entityTypeName &&
      72       59376 :                                 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       34820 :         string EntityRef::getXMLValue(string xml, string parm) const {
      83       34820 :                 string::size_type n = xml.find(parm,0);
      84       34820 :                 if (n == string::npos)
      85         512 :                         return "";
      86       34308 :                 string::size_type beg = xml.find("\"",n + parm.length());
      87       34308 :                 if (beg == string::npos)
      88           0 :                         return "";
      89       34308 :                 beg++;
      90       34308 :                 string::size_type end = xml.find("\"",beg);
      91       34308 :                 if (end == string::npos)
      92           0 :                         return "";
      93       34308 :                 return xml.substr(beg,(end - beg));
      94             :         }
      95             : 
      96       11030 :         string EntityRef::validXML() const {
      97             :                 // Check for any null values. PartId may be null.
      98       11030 :                 string msg = "Null values detected in EntityRef " + entityId.getId();
      99       22060 :                 if (entityId.isNull() ||
     100       22060 :                         entityTypeName.length() == 0 ||
     101       11030 :                         instanceVersion.length() == 0)
     102           0 :                         return msg;
     103             :                 // Check the entityId for the correct format.
     104       11030 :                 msg = EntityId::validate(entityId.toString());
     105       11030 :                 if (msg.length() != 0)
     106           0 :                         return msg;
     107             :                 // Check the entityId for the correct format.
     108       11030 :                 if (partId.toString().length() != 0)
     109       16574 :                         return PartId::validate(partId.toString());
     110        2743 :                 return "";
     111       11030 :         }
     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>   &lt;EntityRef 
     119             :          * <li>           entityId="uid://X0000000000000079/X00000000" 
     120             :          * <li>           partId="X00000002" 
     121             :          * <li>           entityTypeName="Main" 
     122             :          * <li>           documentVersion="1"/&gt;
     123             :          * </ul>
     124             :          * 
     125             :          * @return The values of this EntityRef as an XML-formated string.
     126             :          */
     127        2325 :         string EntityRef::toXML() const {
     128        2325 :                 string msg = validXML();
     129        2325 :                 if (msg.length() != 0) 
     130           0 :                         throw InvalidDataException(msg);
     131        2325 :                 string s = "<EntityRef entityId=\"" + entityId.toString();
     132        2325 :                 if (partId.toString().length() != 0)
     133          94 :                         s += "\" partId=\"" + partId.toString();
     134        4650 :                 s += "\" entityTypeName=\"" + entityTypeName + 
     135        4650 :                          "\" documentVersion=\"" + instanceVersion + "\"/>";
     136        4650 :                 return s;
     137        2325 :         }
     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        8705 :         void EntityRef::setFromXML(string xml)  {
     151        8705 :                 entityId.setId(getXMLValue(xml,"entityId"));
     152        8705 :                 partId.setId(getXMLValue(xml,"partId"));
     153        8705 :                 entityTypeName = getXMLValue(xml,"entityTypeName");
     154        8705 :                 instanceVersion = getXMLValue(xml,"documentVersion");
     155        8705 :                 string msg = validXML();
     156        8705 :                 if (msg.length() != 0) {
     157           0 :                   throw InvalidArgumentException(msg);
     158             :                 }
     159        8705 :         }
     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          47 :         EntityRef EntityRef::fromBin(EndianIStream& eis) {
     175          47 :             std::string entityId = eis.readString();
     176          47 :             std::string partId = eis.readString();
     177          47 :             std::string entityTypeName = eis.readString();
     178          47 :             std::string instanceVersion = eis.readString();
     179          94 :             return EntityRef(entityId, partId, entityTypeName, instanceVersion);
     180          47 :         }
     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

Generated by: LCOV version 1.16