LCOV - code coverage report
Current view: top level - alma/ASDM - EntityRef.cc (source / functions) Hit Total Coverage
Test: casa_coverage.info Lines: 50 90 55.6 %
Date: 2023-10-25 08:47:59 Functions: 7 13 53.8 %

          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             :                 }
      47             :         }
      48             : 
      49       25551 :         EntityRef::EntityRef() : entityId(), partId() {
      50       25551 :                 entityTypeName = "";
      51       25551 :                 instanceVersion = "";
      52       25551 :         }
      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       14478 :         EntityRef::EntityRef(string id, string pId, string sTypeName,
      63       14478 :                         string sInstanceVersion) : entityId(id), partId(pId) {
      64       14478 :                 entityTypeName = sTypeName;
      65       14478 :                 instanceVersion = sInstanceVersion;
      66       14478 :         }
      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       34048 :         string EntityRef::getXMLValue(string xml, string parm) const {
      83       34048 :                 string::size_type n = xml.find(parm,0);
      84       34048 :                 if (n == string::npos)
      85         512 :                         return "";
      86       33536 :                 string::size_type beg = xml.find("\"",n + parm.length());
      87       33536 :                 if (beg == string::npos)
      88           0 :                         return "";
      89       33536 :                 beg++;
      90       33536 :                 string::size_type end = xml.find("\"",beg);
      91       33536 :                 if (end == string::npos)
      92           0 :                         return "";
      93       33536 :                 return xml.substr(beg,(end - beg));
      94             :         }
      95             : 
      96       10743 :         string EntityRef::validXML() const {
      97             :                 // Check for any null values. PartId may be null.
      98       21486 :                 string msg = "Null values detected in EntityRef " + entityId.getId();
      99       21486 :                 if (entityId.isNull() ||
     100       21486 :                         entityTypeName.length() == 0 ||
     101       10743 :                         instanceVersion.length() == 0)
     102           0 :                         return msg;
     103             :                 // Check the entityId for the correct format.
     104       10743 :                 msg = EntityId::validate(entityId.toString());
     105       10743 :                 if (msg.length() != 0)
     106           0 :                         return msg;
     107             :                 // Check the entityId for the correct format.
     108       10743 :                 if (partId.toString().length() != 0)
     109        8000 :                         return PartId::validate(partId.toString());
     110        2743 :                 return "";
     111             :         }
     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        2231 :         string EntityRef::toXML() const {
     128        4462 :                 string msg = validXML();
     129        2231 :                 if (msg.length() != 0) 
     130           0 :                         throw InvalidDataException(msg);
     131        2231 :                 string s = "<EntityRef entityId=\"" + entityId.toString();
     132        2231 :                 if (partId.toString().length() != 0)
     133           0 :                         s += "\" partId=\"" + partId.toString();
     134        4462 :                 s += "\" entityTypeName=\"" + entityTypeName + 
     135        4462 :                          "\" documentVersion=\"" + instanceVersion + "\"/>";
     136        4462 :                 return s;
     137             :         }
     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        8512 :         void EntityRef::setFromXML(string xml)  {
     151        8512 :                 entityId.setId(getXMLValue(xml,"entityId"));
     152        8512 :                 partId.setId(getXMLValue(xml,"partId"));
     153        8512 :                 entityTypeName = getXMLValue(xml,"entityTypeName");
     154        8512 :                 instanceVersion = getXMLValue(xml,"documentVersion");
     155       17024 :                 string msg = validXML();
     156        8512 :                 if (msg.length() != 0) {
     157           0 :                   throw InvalidArgumentException(msg);
     158             :                 }
     159        8512 :         }
     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 :                 return EntityRef(eis.readString(), eis.readString(), eis.readString(), eis.readString());
     176             :         }
     177             : 
     178           0 :         vector<EntityRef> EntityRef::from1DBin(EndianIStream & eis) {
     179           0 :                 int dim1 = eis.readInt();
     180           0 :                 vector<EntityRef> result;
     181           0 :                 for (int i = 0; i < dim1; i++)
     182           0 :                         result.push_back(EntityRef(eis.readString(), eis.readString(), eis.readString(), eis.readString()));
     183           0 :                 return result;
     184             :         }
     185             : 
     186             : } // End namespace asdm

Generated by: LCOV version 1.16