LCOV - code coverage report
Current view: top level - alma/ASDM - CalDataTable.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 155 421 36.8 %
Date: 2024-12-11 20:54:31 Functions: 17 37 45.9 %

          Line data    Source code
       1             : 
       2             : /*
       3             :  * ALMA - Atacama Large Millimeter Array
       4             :  * (c) European Southern Observatory, 2002
       5             :  * (c) Associated Universities Inc., 2002
       6             :  * Copyright by ESO (in the framework of the ALMA collaboration),
       7             :  * Copyright by AUI (in the framework of the ALMA collaboration),
       8             :  * All rights reserved.
       9             :  * 
      10             :  * This library is free software; you can redistribute it and/or
      11             :  * modify it under the terms of the GNU Lesser General Public
      12             :  * License as published by the Free software Foundation; either
      13             :  * version 2.1 of the License, or (at your option) any later version.
      14             :  * 
      15             :  * This library is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY, without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :  * Lesser General Public License for more details.
      19             :  * 
      20             :  * You should have received a copy of the GNU Lesser General Public
      21             :  * License along with this library; if not, write to the Free Software
      22             :  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      23             :  * MA 02111-1307  USA
      24             :  *
      25             :  * Warning!
      26             :  *  -------------------------------------------------------------------- 
      27             :  * | This is generated code!  Do not modify this file.                  |
      28             :  * | If you do, all changes will be lost when the file is re-generated. |
      29             :  *  --------------------------------------------------------------------
      30             :  *
      31             :  * File CalDataTable.cpp
      32             :  */
      33             : #include <alma/ASDM/ConversionException.h>
      34             : #include <alma/ASDM/DuplicateKey.h>
      35             : #include <alma/ASDM/OutOfBoundsException.h>
      36             : 
      37             : using asdm::ConversionException;
      38             : using asdm::DuplicateKey;
      39             : using asdm::OutOfBoundsException;
      40             : 
      41             : #include <alma/ASDM/ASDM.h>
      42             : #include <alma/ASDM/CalDataTable.h>
      43             : #include <alma/ASDM/CalDataRow.h>
      44             : #include <alma/ASDM/Parser.h>
      45             : 
      46             : using asdm::ASDM;
      47             : using asdm::CalDataTable;
      48             : using asdm::CalDataRow;
      49             : using asdm::Parser;
      50             : 
      51             : #include <iostream>
      52             : #include <fstream>
      53             : #include <iterator>
      54             : #include <sstream>
      55             : #include <set>
      56             : #include <algorithm>
      57             : using namespace std;
      58             : 
      59             : #include <alma/ASDM/Misc.h>
      60             : using namespace asdm;
      61             : 
      62             : #include <libxml/parser.h>
      63             : #include <libxml/tree.h>
      64             : 
      65             : #ifndef WITHOUT_BOOST
      66             : #include "boost/filesystem/operations.hpp"
      67             : #include <boost/algorithm/string.hpp>
      68             : #else
      69             : #include <sys/stat.h>
      70             : #endif
      71             : 
      72             : namespace asdm {
      73             :         // The name of the entity we will store in this table.
      74             :         static string entityNameOfCalData = "CalData";
      75             :         
      76             :         // An array of string containing the names of the columns of this table.
      77             :         // The array is filled in the order : key, required value, optional value.
      78             :         //
      79             :         static string attributesNamesOfCalData_a[] = {
      80             :                 
      81             :                         "calDataId"
      82             :                 
      83             :                 
      84             :                         , "startTimeObserved"
      85             :                 
      86             :                         , "endTimeObserved"
      87             :                 
      88             :                         , "execBlockUID"
      89             :                 
      90             :                         , "calDataType"
      91             :                 
      92             :                         , "calType"
      93             :                 
      94             :                         , "numScan"
      95             :                 
      96             :                         , "scanSet"
      97             :                                 
      98             :                 
      99             :                         , "assocCalDataId"
     100             :                 
     101             :                         , "assocCalNature"
     102             :                 
     103             :                         , "fieldName"
     104             :                 
     105             :                         , "sourceName"
     106             :                 
     107             :                         , "sourceCode"
     108             :                 
     109             :                         , "scanIntent"
     110             :                                 
     111             :         };
     112             :         
     113             :         // A vector of string whose content is a copy of the strings in the array above.
     114             :         //
     115             :         static vector<string> attributesNamesOfCalData_v (attributesNamesOfCalData_a, attributesNamesOfCalData_a + sizeof(attributesNamesOfCalData_a) / sizeof(attributesNamesOfCalData_a[0]));
     116             : 
     117             :         // An array of string containing the names of the columns of this table.
     118             :         // The array is filled in the order where the names would be read by default in the XML header of a file containing
     119             :         // the table exported in binary mode.
     120             :         //      
     121             :         static string attributesNamesInBinOfCalData_a[] = {
     122             :     
     123             :          "calDataId" , "startTimeObserved" , "endTimeObserved" , "execBlockUID" , "calDataType" , "calType" , "numScan" , "scanSet" 
     124             :         ,
     125             :          "assocCalDataId" , "assocCalNature" , "fieldName" , "sourceName" , "sourceCode" , "scanIntent" 
     126             :     
     127             :         };
     128             :                                         
     129             :         // A vector of string whose content is a copy of the strings in the array above.
     130             :         //
     131             :         static vector<string> attributesNamesInBinOfCalData_v(attributesNamesInBinOfCalData_a, attributesNamesInBinOfCalData_a + sizeof(attributesNamesInBinOfCalData_a) / sizeof(attributesNamesInBinOfCalData_a[0]));           
     132             :         
     133             : 
     134             :         // The array of attributes (or column) names that make up key key.
     135             :         //
     136             :         string keyOfCalData_a[] = {
     137             :         
     138             :                 "calDataId"
     139             :                  
     140             :         };
     141             :          
     142             :         // A vector of strings which are copies of those stored in the array above.
     143             :         vector<string> keyOfCalData_v(keyOfCalData_a, keyOfCalData_a + sizeof(keyOfCalData_a) / sizeof(keyOfCalData_a[0]));
     144             : 
     145             :         /**
     146             :          * Return the list of field names that make up key key
     147             :          * as a const reference to a vector of strings.
     148             :          */     
     149           0 :         const vector<string>& CalDataTable::getKeyName() {
     150           0 :                 return keyOfCalData_v;
     151             :         }
     152             : 
     153             : 
     154         118 :         CalDataTable::CalDataTable(ASDM &c) : container(c) {
     155             : 
     156             :                 // Define a default entity.
     157         118 :                 entity.setEntityId(EntityId("uid://X0/X0/X0"));
     158         118 :                 entity.setEntityIdEncrypted("na");
     159         118 :                 entity.setEntityTypeName("CalDataTable");
     160         118 :                 entity.setEntityVersion("1");
     161         118 :                 entity.setInstanceVersion("1");
     162             :                 
     163             :                 // Archive XML
     164         118 :                 archiveAsBin = true;
     165             :                 
     166             :                 // File XML
     167         118 :                 fileAsBin = true;
     168             :                 
     169             :                 // By default the table is considered as present in memory
     170         118 :                 presentInMemory = true;
     171             :                 
     172             :                 // By default there is no load in progress
     173         118 :                 loadInProgress = false;
     174         118 :         }
     175             :         
     176             : /**
     177             :  * A destructor for CalDataTable.
     178             :  */
     179         236 :         CalDataTable::~CalDataTable() {
     180         212 :                 for (unsigned int i = 0; i < privateRows.size(); i++) 
     181          94 :                         delete(privateRows.at(i));
     182         236 :         }
     183             : 
     184             :         /**
     185             :          * Container to which this table belongs.
     186             :          */
     187           6 :         ASDM &CalDataTable::getContainer() const {
     188           6 :                 return container;
     189             :         }
     190             : 
     191             :         /**
     192             :          * Return the number of rows in the table.
     193             :          */
     194          39 :         unsigned int CalDataTable::size() const {
     195          39 :                 if (presentInMemory) 
     196          39 :                         return privateRows.size();
     197             :                 else
     198           0 :                         return declaredSize;
     199             :         }
     200             :         
     201             :         /**
     202             :          * Return the name of this table.
     203             :          */
     204        4281 :         string CalDataTable::getName() const {
     205        4281 :                 return entityNameOfCalData;
     206             :         }
     207             :         
     208             :         /**
     209             :          * Return the name of this table.
     210             :          */
     211           0 :         string CalDataTable::name() {
     212           0 :                 return entityNameOfCalData;
     213             :         }
     214             :         
     215             :         /**
     216             :          * Return the the names of the attributes (or columns) of this table.
     217             :          */
     218           0 :         const vector<string>& CalDataTable::getAttributesNames() { return attributesNamesOfCalData_v; }
     219             :         
     220             :         /**
     221             :          * Return the the names of the attributes (or columns) of this table as they appear by default
     222             :          * in an binary export of this table.
     223             :          */
     224           0 :         const vector<string>& CalDataTable::defaultAttributesNamesInBin() { return attributesNamesInBinOfCalData_v; }
     225             : 
     226             :         /**
     227             :          * Return this table's Entity.
     228             :          */
     229           0 :         Entity CalDataTable::getEntity() const {
     230           0 :                 return entity;
     231             :         }
     232             : 
     233             :         /**
     234             :          * Set this table's Entity.
     235             :          */
     236           1 :         void CalDataTable::setEntity(Entity e) {
     237           1 :                 this->entity = e; 
     238           1 :         }
     239             :         
     240             :         //
     241             :         // ====> Row creation.
     242             :         //
     243             :         
     244             :         /**
     245             :          * Create a new row.
     246             :          */
     247          47 :         CalDataRow *CalDataTable::newRow() {
     248          47 :                 return new CalDataRow (*this);
     249             :         }
     250             :         
     251             : 
     252             :         /**
     253             :          * Create a new row initialized to the specified values.
     254             :          * @return a pointer on the created and initialized row.
     255             :         
     256             :          * @param startTimeObserved 
     257             :         
     258             :          * @param endTimeObserved 
     259             :         
     260             :          * @param execBlockUID 
     261             :         
     262             :          * @param calDataType 
     263             :         
     264             :          * @param calType 
     265             :         
     266             :          * @param numScan 
     267             :         
     268             :          * @param scanSet 
     269             :         
     270             :      */
     271           0 :         CalDataRow* CalDataTable::newRow(ArrayTime startTimeObserved, ArrayTime endTimeObserved, EntityRef execBlockUID, CalDataOriginMod::CalDataOrigin calDataType, CalTypeMod::CalType calType, int numScan, std::vector<int > scanSet){
     272           0 :                 CalDataRow *row = new CalDataRow(*this);
     273             :                         
     274           0 :                 row->setStartTimeObserved(startTimeObserved);
     275             :                         
     276           0 :                 row->setEndTimeObserved(endTimeObserved);
     277             :                         
     278           0 :                 row->setExecBlockUID(execBlockUID);
     279             :                         
     280           0 :                 row->setCalDataType(calDataType);
     281             :                         
     282           0 :                 row->setCalType(calType);
     283             :                         
     284           0 :                 row->setNumScan(numScan);
     285             :                         
     286           0 :                 row->setScanSet(scanSet);
     287             :         
     288           0 :                 return row;             
     289             :         }       
     290             :         
     291             : 
     292             : 
     293           0 : CalDataRow* CalDataTable::newRow(CalDataRow* row) {
     294           0 :         return new CalDataRow(*this, row);
     295             : }
     296             : 
     297             :         //
     298             :         // Append a row to its table.
     299             :         //
     300             : 
     301             :         
     302             :          
     303             :         
     304             :         /** 
     305             :          * Look up the table for a row whose noautoincrementable attributes are matching their
     306             :          * homologues in *x.  If a row is found  this row else autoincrement  *x.calDataId, 
     307             :          * add x to its table and returns x.
     308             :          *  
     309             :          * @returns a pointer on a CalDataRow.
     310             :          * @param x. A pointer on the row to be added.
     311             :          */ 
     312             :                 
     313             :                         
     314           0 :         CalDataRow* CalDataTable::add(CalDataRow* x) {
     315             :                          
     316           0 :                 CalDataRow* aRow = lookup(
     317             :                                 
     318           0 :                 x->getStartTimeObserved()
     319             :                                 ,
     320           0 :                 x->getEndTimeObserved()
     321             :                                 ,
     322           0 :                 x->getExecBlockUID()
     323             :                                 ,
     324             :                 x->getCalDataType()
     325             :                                 ,
     326             :                 x->getCalType()
     327             :                                 ,
     328             :                 x->getNumScan()
     329             :                                 ,
     330           0 :                 x->getScanSet()
     331             :                                 
     332             :                 );
     333           0 :                 if (aRow) return aRow;
     334             :                         
     335             : 
     336             :                         
     337             :                 // Autoincrement calDataId
     338           0 :                 x->setCalDataId(Tag(size(), TagType::CalData));
     339             :                                                 
     340           0 :                 row.push_back(x);
     341           0 :                 privateRows.push_back(x);
     342           0 :                 x->isAdded(true);
     343           0 :                 return x;
     344             :         }
     345             :                 
     346             :         
     347             :                 
     348          47 :         void CalDataTable::addWithoutCheckingUnique(CalDataRow * x) {
     349          47 :                 if (getRowByKey(
     350          94 :                                                 x->getCalDataId()
     351          47 :                                                 ) != (CalDataRow *) 0) 
     352           0 :                         throw DuplicateKey("Dupicate key exception in ", "CalDataTable");
     353          47 :                 row.push_back(x);
     354          47 :                 privateRows.push_back(x);
     355          47 :                 x->isAdded(true);
     356          47 :         }
     357             : 
     358             : 
     359             : 
     360             : 
     361             :         // 
     362             :         // A private method to append a row to its table, used by input conversion
     363             :         // methods, with row uniqueness.
     364             :         //
     365             : 
     366             :         
     367             :         /**
     368             :          * If this table has an autoincrementable attribute then check if *x verifies the rule of uniqueness and throw exception if not.
     369             :          * Check if *x verifies the key uniqueness rule and throw an exception if not.
     370             :          * Append x to its table.
     371             :          * @param x a pointer on the row to be appended.
     372             :          * @returns a pointer on x.
     373             :          * @throws DuplicateKey
     374             :          
     375             :          * @throws UniquenessViolationException
     376             :          
     377             :          */
     378           0 :         CalDataRow*  CalDataTable::checkAndAdd(CalDataRow* x, bool skipCheckUniqueness)  {
     379           0 :                 if (!skipCheckUniqueness) { 
     380             :          
     381             :                  
     382           0 :                         if (lookup(
     383             :                         
     384           0 :                                 x->getStartTimeObserved()
     385             :                 ,
     386           0 :                                 x->getEndTimeObserved()
     387             :                 ,
     388           0 :                                 x->getExecBlockUID()
     389             :                 ,
     390             :                                 x->getCalDataType()
     391             :                 ,
     392             :                                 x->getCalType()
     393             :                 ,
     394             :                                 x->getNumScan()
     395             :                 ,
     396           0 :                                 x->getScanSet()
     397             :                 
     398           0 :                         )) throw UniquenessViolationException();
     399             :                 
     400             :                 
     401             :                 }
     402             :                 
     403           0 :                 if (getRowByKey(
     404             :         
     405           0 :                         x->getCalDataId()
     406             :                         
     407           0 :                 )) throw DuplicateKey("Duplicate key exception in ", "CalDataTable");
     408             :                 
     409           0 :                 row.push_back(x);
     410           0 :                 privateRows.push_back(x);
     411           0 :                 x->isAdded(true);
     412           0 :                 return x;       
     413             :         }       
     414             : 
     415             : 
     416             : 
     417             :         //
     418             :         // A private method to brutally append a row to its table, without checking for row uniqueness.
     419             :         //
     420             : 
     421          47 :         void CalDataTable::append(CalDataRow *x) {
     422          47 :                 privateRows.push_back(x);
     423          47 :                 x->isAdded(true);
     424          47 :         }
     425             : 
     426             : 
     427             : 
     428             : 
     429             : 
     430           2 :          vector<CalDataRow *> CalDataTable::get() {
     431           2 :                 checkPresenceInMemory();
     432           2 :             return privateRows;
     433             :          }
     434             :          
     435           0 :          const vector<CalDataRow *>& CalDataTable::get() const {
     436           0 :                 const_cast<CalDataTable&>(*this).checkPresenceInMemory();     
     437           0 :             return privateRows;
     438             :          }       
     439             :                 
     440             : 
     441             : 
     442             : 
     443             : 
     444             :         
     445             : 
     446             :         
     447             : /*
     448             :  ** Returns a CalDataRow* given a key.
     449             :  ** @return a pointer to the row having the key whose values are passed as parameters, or 0 if
     450             :  ** no row exists for that key.
     451             :  **
     452             :  */
     453          47 :         CalDataRow* CalDataTable::getRowByKey(Tag calDataId)  {
     454          47 :         checkPresenceInMemory();
     455          47 :         CalDataRow* aRow = 0;
     456        1128 :         for (unsigned int i = 0; i < privateRows.size(); i++) {
     457        1081 :                 aRow = row.at(i);
     458             :                 
     459             :                         
     460        1081 :                                 if (aRow->calDataId != calDataId) continue;
     461             :                         
     462             :                 
     463           0 :                 return aRow;
     464             :         }
     465          47 :         return 0;               
     466             : }
     467             :         
     468             : 
     469             :         
     470             : /**
     471             :  * Look up the table for a row whose all attributes  except the autoincrementable one 
     472             :  * are equal to the corresponding parameters of the method.
     473             :  * @return a pointer on this row if any, 0 otherwise.
     474             :  *
     475             :                         
     476             :  * @param startTimeObserved.
     477             :                         
     478             :  * @param endTimeObserved.
     479             :                         
     480             :  * @param execBlockUID.
     481             :                         
     482             :  * @param calDataType.
     483             :                         
     484             :  * @param calType.
     485             :                         
     486             :  * @param numScan.
     487             :                         
     488             :  * @param scanSet.
     489             :                          
     490             :  */
     491           0 : CalDataRow* CalDataTable::lookup(ArrayTime startTimeObserved, ArrayTime endTimeObserved, EntityRef execBlockUID, CalDataOriginMod::CalDataOrigin calDataType, CalTypeMod::CalType calType, int numScan, std::vector<int > scanSet) {
     492             :                 CalDataRow* aRow;
     493           0 :                 for (unsigned int i = 0; i < privateRows.size(); i++) {
     494           0 :                         aRow = privateRows.at(i); 
     495           0 :                         if (aRow->compareNoAutoInc(startTimeObserved, endTimeObserved, execBlockUID, calDataType, calType, numScan, scanSet)) return aRow;
     496             :                 }                       
     497           0 :                 return 0;       
     498             : } 
     499             :         
     500             :                 
     501             : 
     502             :         
     503             : 
     504             : 
     505             : 
     506             : #ifndef WITHOUT_ACS
     507             :         using asdmIDL::CalDataTableIDL;
     508             : #endif
     509             : 
     510             : #ifndef WITHOUT_ACS
     511             :         // Conversion Methods
     512             : 
     513             :         CalDataTableIDL *CalDataTable::toIDL() {
     514             :                 CalDataTableIDL *x = new CalDataTableIDL ();
     515             :                 unsigned int nrow = size();
     516             :                 x->row.length(nrow);
     517             :                 vector<CalDataRow*> v = get();
     518             :                 for (unsigned int i = 0; i < nrow; ++i) {
     519             :                         //x->row[i] = *(v[i]->toIDL());
     520             :                         v[i]->toIDL(x->row[i]);
     521             :                 }
     522             :                 return x;
     523             :         }
     524             :         
     525             :         void CalDataTable::toIDL(asdmIDL::CalDataTableIDL& x) const {
     526             :                 unsigned int nrow = size();
     527             :                 x.row.length(nrow);
     528             :                 vector<CalDataRow*> v = get();
     529             :                 for (unsigned int i = 0; i < nrow; ++i) {
     530             :                         v[i]->toIDL(x.row[i]);
     531             :                 }
     532             :         }       
     533             : #endif
     534             :         
     535             : #ifndef WITHOUT_ACS
     536             :         void CalDataTable::fromIDL(CalDataTableIDL x) {
     537             :                 unsigned int nrow = x.row.length();
     538             :                 for (unsigned int i = 0; i < nrow; ++i) {
     539             :                         CalDataRow *tmp = newRow();
     540             :                         tmp->setFromIDL(x.row[i]);
     541             :                         // checkAndAdd(tmp);
     542             :                         add(tmp);
     543             :                 }
     544             :         }       
     545             : #endif
     546             : 
     547             :         
     548           0 :         string CalDataTable::toXML()  {
     549           0 :                 string buf;
     550             : 
     551           0 :                 buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> ");
     552           0 :                 buf.append("<CalDataTable xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:cldata=\"http://Alma/XASDM/CalDataTable\" xsi:schemaLocation=\"http://Alma/XASDM/CalDataTable http://almaobservatory.org/XML/XASDM/4/CalDataTable.xsd\" schemaVersion=\"4\" schemaRevision=\"-1\">\n");
     553             :         
     554           0 :                 buf.append(entity.toXML());
     555           0 :                 string s = container.getEntity().toXML();
     556             :                 // Change the "Entity" tag to "ContainerEntity".
     557           0 :                 buf.append("<Container" + s.substr(1,s.length() - 1)+" ");
     558           0 :                 vector<CalDataRow*> v = get();
     559           0 :                 for (unsigned int i = 0; i < v.size(); ++i) {
     560             :                         try {
     561           0 :                                 buf.append(v[i]->toXML());
     562           0 :                         } catch (const NoSuchRow &e) {
     563           0 :                         }
     564           0 :                         buf.append("  ");
     565             :                 }               
     566           0 :                 buf.append("</CalDataTable> ");
     567           0 :                 return buf;
     568           0 :         }
     569             : 
     570             :         
     571           0 :         string CalDataTable::getVersion() const {
     572           0 :                 return version;
     573             :         }
     574             :         
     575             : 
     576           1 :         void CalDataTable::fromXML(string& tableInXML)  {
     577             :                 //
     578             :                 // Look for a version information in the schemaVersion of the XML
     579             :                 //
     580             :                 xmlDoc *doc;
     581             : #if LIBXML_VERSION >= 20703
     582           1 :         doc = xmlReadMemory(tableInXML.data(), tableInXML.size(), "XMLTableHeader.xml", NULL, XML_PARSE_NOBLANKS|XML_PARSE_HUGE);
     583             : #else
     584             :                 doc = xmlReadMemory(tableInXML.data(), tableInXML.size(), "XMLTableHeader.xml", NULL, XML_PARSE_NOBLANKS);
     585             : #endif
     586           1 :                 if ( doc == NULL )
     587           0 :                         throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "CalData");
     588             :                 
     589           1 :                 xmlNode* root_element = xmlDocGetRootElement(doc);
     590           1 :                 if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
     591           0 :                 throw ConversionException("Failed to retrieve the root element in the DOM structure.", "CalData");
     592             :                 
     593           1 :         xmlChar * propValue = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
     594           1 :         if ( propValue != 0 ) {
     595           1 :                 version = string( (const char*) propValue);
     596           1 :                 xmlFree(propValue);             
     597             :         }
     598             :                                                                         
     599           1 :                 Parser xml(tableInXML);
     600           1 :                 if (!xml.isStr("<CalDataTable")) 
     601           0 :                         error();
     602             :                 // cout << "Parsing a CalDataTable" << endl;
     603           2 :                 string s = xml.getElement("<Entity","/>");
     604           1 :                 if (s.length() == 0) 
     605           0 :                         error();
     606           1 :                 Entity e;
     607           1 :                 e.setFromXML(s);
     608           1 :                 if (e.getEntityTypeName() != "CalDataTable")
     609           0 :                         error();
     610           1 :                 setEntity(e);
     611             :                 // Skip the container's entity; but, it has to be there.
     612           1 :                 s = xml.getElement("<ContainerEntity","/>");
     613           1 :                 if (s.length() == 0) 
     614           0 :                         error();
     615             : 
     616             :                 // Get each row in the table.
     617           1 :                 s = xml.getElementContent("<row>","</row>");
     618             :                 CalDataRow *row;
     619           1 :                 if (getContainer().checkRowUniqueness()) {
     620             :                         try {
     621           0 :                                 while (s.length() != 0) {
     622           0 :                                         row = newRow();
     623           0 :                                         row->setFromXML(s);
     624           0 :                                         checkAndAdd(row);
     625           0 :                                         s = xml.getElementContent("<row>","</row>");
     626             :                                 }
     627             :                                 
     628             :                         }
     629           0 :                         catch (const DuplicateKey &e1) {
     630           0 :                                 throw ConversionException(e1.getMessage(),"CalDataTable");
     631           0 :                         } 
     632           0 :                         catch (const UniquenessViolationException &e1) {
     633           0 :                                 throw ConversionException(e1.getMessage(),"CalDataTable");    
     634           0 :                         }
     635           0 :                         catch (...) {
     636             :                                 // cout << "Unexpected error in CalDataTable::checkAndAdd called from CalDataTable::fromXML " << endl;
     637           0 :                         }
     638             :                 }
     639             :                 else {
     640             :                         try {
     641          48 :                                 while (s.length() != 0) {
     642          47 :                                         row = newRow();
     643          47 :                                         row->setFromXML(s);
     644          47 :                                         addWithoutCheckingUnique(row);
     645          47 :                                         s = xml.getElementContent("<row>","</row>");
     646             :                                 }
     647             :                         }
     648           0 :                         catch (const DuplicateKey &e1) {
     649           0 :                                 throw ConversionException(e1.getMessage(),"CalDataTable");
     650           0 :                         } 
     651           0 :                         catch (...) {
     652             :                                 // cout << "Unexpected error in CalDataTable::addWithoutCheckingUnique called from CalDataTable::fromXML " << endl;
     653           0 :                         }
     654             :                 }                               
     655             :                                 
     656             :                                 
     657           1 :                 if (!xml.isStr("</CalDataTable>")) 
     658           0 :                 error();
     659             :                 
     660             :                 //Does not change the convention defined in the model.  
     661             :                 //archiveAsBin = false;
     662             :                 //fileAsBin = false;
     663             : 
     664             :                 // clean up the xmlDoc pointer
     665           1 :                 if ( doc != NULL ) xmlFreeDoc(doc);
     666             :                 
     667           1 :         }
     668             : 
     669             :         
     670           0 :         void CalDataTable::error()  {
     671           0 :                 throw ConversionException("Invalid xml document","CalData");
     672             :         }
     673             :         
     674             :         
     675           0 :         string CalDataTable::MIMEXMLPart(const asdm::ByteOrder* byteOrder) {
     676           0 :                 string UID = getEntity().getEntityId().toString();
     677           0 :                 string withoutUID = UID.substr(6);
     678           0 :                 string containerUID = getContainer().getEntity().getEntityId().toString();
     679           0 :                 ostringstream oss;
     680           0 :                 oss << "<?xml version='1.0'  encoding='ISO-8859-1'?>";
     681           0 :                 oss << "\n";
     682           0 :                 oss << "<CalDataTable xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:cldata=\"http://Alma/XASDM/CalDataTable\" xsi:schemaLocation=\"http://Alma/XASDM/CalDataTable http://almaobservatory.org/XML/XASDM/4/CalDataTable.xsd\" schemaVersion=\"4\" schemaRevision=\"-1\">\n";
     683           0 :                 oss<< "<Entity entityId='"<<UID<<"' entityIdEncrypted='na' entityTypeName='CalDataTable' schemaVersion='1' documentVersion='1'/>\n";
     684           0 :                 oss<< "<ContainerEntity entityId='"<<containerUID<<"' entityIdEncrypted='na' entityTypeName='ASDM' schemaVersion='1' documentVersion='1'/>\n";
     685           0 :                 oss << "<BulkStoreRef file_id='"<<withoutUID<<"' byteOrder='"<<byteOrder->toString()<<"' />\n";
     686           0 :                 oss << "<Attributes>\n";
     687             : 
     688           0 :                 oss << "<calDataId/>\n"; 
     689           0 :                 oss << "<startTimeObserved/>\n"; 
     690           0 :                 oss << "<endTimeObserved/>\n"; 
     691           0 :                 oss << "<execBlockUID/>\n"; 
     692           0 :                 oss << "<calDataType/>\n"; 
     693           0 :                 oss << "<calType/>\n"; 
     694           0 :                 oss << "<numScan/>\n"; 
     695           0 :                 oss << "<scanSet/>\n"; 
     696             : 
     697           0 :                 oss << "<assocCalDataId/>\n"; 
     698           0 :                 oss << "<assocCalNature/>\n"; 
     699           0 :                 oss << "<fieldName/>\n"; 
     700           0 :                 oss << "<sourceName/>\n"; 
     701           0 :                 oss << "<sourceCode/>\n"; 
     702           0 :                 oss << "<scanIntent/>\n"; 
     703           0 :                 oss << "</Attributes>\n";         
     704           0 :                 oss << "</CalDataTable>\n";
     705             : 
     706           0 :                 return oss.str();                               
     707           0 :         }
     708             :         
     709           0 :         string CalDataTable::toMIME(const asdm::ByteOrder* byteOrder) {
     710           0 :                 EndianOSStream eoss(byteOrder);
     711             :                 
     712           0 :                 string UID = getEntity().getEntityId().toString();
     713             :                 
     714             :                 // The MIME Header
     715           0 :                 eoss <<"MIME-Version: 1.0";
     716           0 :                 eoss << "\n";
     717           0 :                 eoss << "Content-Type: Multipart/Related; boundary='MIME_boundary'; type='text/xml'; start= '<header.xml>'";
     718           0 :                 eoss <<"\n";
     719           0 :                 eoss <<"Content-Description: Correlator";
     720           0 :                 eoss <<"\n";
     721           0 :                 eoss <<"alma-uid:" << UID;
     722           0 :                 eoss <<"\n";
     723           0 :                 eoss <<"\n";            
     724             :                 
     725             :                 // The MIME XML part header.
     726           0 :                 eoss <<"--MIME_boundary";
     727           0 :                 eoss <<"\n";
     728           0 :                 eoss <<"Content-Type: text/xml; charset='ISO-8859-1'";
     729           0 :                 eoss <<"\n";
     730           0 :                 eoss <<"Content-Transfer-Encoding: 8bit";
     731           0 :                 eoss <<"\n";
     732           0 :                 eoss <<"Content-ID: <header.xml>";
     733           0 :                 eoss <<"\n";
     734           0 :                 eoss <<"\n";
     735             :                 
     736             :                 // The MIME XML part content.
     737           0 :                 eoss << MIMEXMLPart(byteOrder);
     738             : 
     739             :                 // The MIME binary part header
     740           0 :                 eoss <<"--MIME_boundary";
     741           0 :                 eoss <<"\n";
     742           0 :                 eoss <<"Content-Type: binary/octet-stream";
     743           0 :                 eoss <<"\n";
     744           0 :                 eoss <<"Content-ID: <content.bin>";
     745           0 :                 eoss <<"\n";
     746           0 :                 eoss <<"\n";    
     747             :                 
     748             :                 // The MIME binary content
     749           0 :                 entity.toBin(eoss);
     750           0 :                 container.getEntity().toBin(eoss);
     751           0 :                 eoss.writeInt((int) privateRows.size());
     752           0 :                 for (unsigned int i = 0; i < privateRows.size(); i++) {
     753           0 :                         privateRows.at(i)->toBin(eoss);      
     754             :                 }
     755             :                 
     756             :                 // The closing MIME boundary
     757           0 :                 eoss << "\n--MIME_boundary--";
     758           0 :                 eoss << "\n";
     759             :                 
     760           0 :                 return eoss.str();      
     761           0 :         }
     762             : 
     763             :         
     764           1 :         void CalDataTable::setFromMIME(const string & mimeMsg) {
     765           1 :     string xmlPartMIMEHeader = "Content-ID: <header.xml>\n\n";
     766             :     
     767           1 :     string binPartMIMEHeader = "--MIME_boundary\nContent-Type: binary/octet-stream\nContent-ID: <content.bin>\n\n";
     768             :     
     769             :     // Detect the XML header.
     770           1 :     string::size_type loc0 = mimeMsg.find(xmlPartMIMEHeader, 0);
     771           1 :     if ( loc0 == string::npos) {
     772             :       // let's try with CRLFs
     773           0 :       xmlPartMIMEHeader = "Content-ID: <header.xml>\r\n\r\n";
     774           0 :       loc0 = mimeMsg.find(xmlPartMIMEHeader, 0);
     775           0 :       if  ( loc0 == string::npos ) 
     776           0 :               throw ConversionException("Failed to detect the beginning of the XML header", "CalData");
     777             :     }
     778             : 
     779           1 :     loc0 += xmlPartMIMEHeader.size();
     780             :     
     781             :     // Look for the string announcing the binary part.
     782           1 :     string::size_type loc1 = mimeMsg.find( binPartMIMEHeader, loc0 );
     783             :     
     784           1 :     if ( loc1 == string::npos ) {
     785           0 :       throw ConversionException("Failed to detect the beginning of the binary part", "CalData");
     786             :     }
     787             :     
     788             :     //
     789             :     // Extract the xmlHeader and analyze it to find out what is the byte order and the sequence
     790             :     // of attribute names.
     791             :     //
     792           1 :     string xmlHeader = mimeMsg.substr(loc0, loc1-loc0);
     793             :     xmlDoc *doc;
     794           1 :     doc = xmlReadMemory(xmlHeader.data(), xmlHeader.size(), "BinaryTableHeader.xml", NULL, XML_PARSE_NOBLANKS);
     795           1 :     if ( doc == NULL ) 
     796           0 :       throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "CalData");
     797             :     
     798             :    // This vector will be filled by the names of  all the attributes of the table
     799             :    // in the order in which they are expected to be found in the binary representation.
     800             :    //
     801           1 :     vector<string> attributesSeq;
     802             :       
     803           1 :     xmlNode* root_element = xmlDocGetRootElement(doc);
     804           1 :     if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
     805           0 :       throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "CalData");
     806             :     
     807           1 :     const ByteOrder* byteOrder=0;
     808           1 :     if ( string("ASDMBinaryTable").compare((const char*) root_element->name) == 0) {
     809             :       // Then it's an "old fashioned" MIME file for tables.
     810             :       // Just try to deserialize it with Big_Endian for the bytes ordering.
     811           0 :       byteOrder = asdm::ByteOrder::Big_Endian;
     812             :       
     813             :          //
     814             :     // Let's consider a  default order for the sequence of attributes.
     815             :     //
     816             :     
     817             :          
     818           0 :     attributesSeq.push_back("calDataId") ; 
     819             :          
     820           0 :     attributesSeq.push_back("startTimeObserved") ; 
     821             :          
     822           0 :     attributesSeq.push_back("endTimeObserved") ; 
     823             :          
     824           0 :     attributesSeq.push_back("execBlockUID") ; 
     825             :          
     826           0 :     attributesSeq.push_back("calDataType") ; 
     827             :          
     828           0 :     attributesSeq.push_back("calType") ; 
     829             :          
     830           0 :     attributesSeq.push_back("numScan") ; 
     831             :          
     832           0 :     attributesSeq.push_back("scanSet") ; 
     833             :         
     834             :          
     835           0 :     attributesSeq.push_back("assocCalDataId") ; 
     836             :          
     837           0 :     attributesSeq.push_back("assocCalNature") ; 
     838             :          
     839           0 :     attributesSeq.push_back("fieldName") ; 
     840             :          
     841           0 :     attributesSeq.push_back("sourceName") ; 
     842             :          
     843           0 :     attributesSeq.push_back("sourceCode") ; 
     844             :          
     845           0 :     attributesSeq.push_back("scanIntent") ; 
     846             :         
     847             :      
     848             :     
     849             :     
     850             :     // And decide that it has version == "2"
     851           0 :     version = "2";         
     852             :      }
     853           1 :     else if (string("CalDataTable").compare((const char*) root_element->name) == 0) {
     854             :       // It's a new (and correct) MIME file for tables.
     855             :       //
     856             :       // 1st )  Look for a BulkStoreRef element with an attribute byteOrder.
     857             :       //
     858           1 :       xmlNode* bulkStoreRef = 0;
     859           1 :       xmlNode* child = root_element->children;
     860             :       
     861           1 :       if (xmlHasProp(root_element, (const xmlChar*) "schemaVersion")) {
     862           1 :         xmlChar * value = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
     863           1 :         version = string ((const char *) value);
     864           1 :         xmlFree(value); 
     865             :       }
     866             :       
     867             :       // Skip the two first children (Entity and ContainerEntity).
     868           1 :       bulkStoreRef = (child ==  0) ? 0 : ( (child->next) == 0 ? 0 : child->next->next );
     869             :       
     870           1 :       if ( bulkStoreRef == 0 || (bulkStoreRef->type != XML_ELEMENT_NODE)  || (string("BulkStoreRef").compare((const char*) bulkStoreRef->name) != 0))
     871           0 :         throw ConversionException ("Could not find the element '/CalDataTable/BulkStoreRef'. Invalid XML header '"+ xmlHeader + "'.", "CalData");
     872             :         
     873             :       // We found BulkStoreRef, now look for its attribute byteOrder.
     874           1 :       _xmlAttr* byteOrderAttr = 0;
     875           2 :       for (struct _xmlAttr* attr = bulkStoreRef->properties; attr; attr = attr->next) 
     876           2 :           if (string("byteOrder").compare((const char*) attr->name) == 0) {
     877           1 :            byteOrderAttr = attr;
     878           1 :            break;
     879             :          }
     880             :       
     881           1 :       if (byteOrderAttr == 0) 
     882           0 :              throw ConversionException("Could not find the element '/CalDataTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader +"'.", "CalData");
     883             :       
     884           1 :       string byteOrderValue = string((const char*) byteOrderAttr->children->content);
     885           1 :       if (!(byteOrder = asdm::ByteOrder::fromString(byteOrderValue)))
     886           0 :                 throw ConversionException("No valid value retrieved for the element '/CalDataTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader + "'.", "CalData");
     887             :                 
     888             :          //
     889             :          // 2nd) Look for the Attributes element and grab the names of the elements it contains.
     890             :          //
     891           1 :          xmlNode* attributes = bulkStoreRef->next;
     892           1 :      if ( attributes == 0 || (attributes->type != XML_ELEMENT_NODE)  || (string("Attributes").compare((const char*) attributes->name) != 0))     
     893           0 :         throw ConversionException ("Could not find the element '/CalDataTable/Attributes'. Invalid XML header '"+ xmlHeader + "'.", "CalData");
     894             :  
     895           1 :         xmlNode* childOfAttributes = attributes->children;
     896             :         
     897          15 :         while ( childOfAttributes != 0 && (childOfAttributes->type == XML_ELEMENT_NODE) ) {
     898          14 :                 attributesSeq.push_back(string((const char*) childOfAttributes->name));
     899          14 :                 childOfAttributes = childOfAttributes->next;
     900             :     }
     901           1 :     }
     902             :     // Create an EndianISStream from the substring containing the binary part.
     903           1 :     EndianISStream eiss(mimeMsg.substr(loc1+binPartMIMEHeader.size()), byteOrder);
     904             :     
     905           1 :     entity = Entity::fromBin((EndianIStream&) eiss);
     906             :     
     907             :     // We do nothing with that but we have to read it.
     908           1 :     Entity containerEntity = Entity::fromBin((EndianIStream&) eiss);
     909             : 
     910             :         // Let's read numRows but ignore it and rely on the value specified in the ASDM.xml file.    
     911           1 :     int numRows = ((EndianIStream&) eiss).readInt();
     912           1 :     if ((numRows != -1)                        // Then these are *not* data produced at the EVLA.
     913           1 :         && ((unsigned int) numRows != this->declaredSize )) { // Then the declared size (in ASDM.xml) is not equal to the one 
     914             :                                                // written into the binary representation of the table.
     915           0 :                 cout << "The a number of rows ('" 
     916             :                          << numRows
     917           0 :                          << "') declared in the binary representation of the table is different from the one declared in ASDM.xml ('"
     918           0 :                          << this->declaredSize
     919           0 :                          << "'). I'll proceed with the value declared in ASDM.xml"
     920           0 :                          << endl;
     921             :     }                                           
     922             : 
     923           1 :         if (getContainer().checkRowUniqueness()) {
     924             :         try {
     925           0 :                 for (uint32_t i = 0; i < this->declaredSize; i++) {
     926           0 :                                 CalDataRow* aRow = CalDataRow::fromBin((EndianIStream&) eiss, *this, attributesSeq);
     927           0 :                                 checkAndAdd(aRow);
     928             :                 }
     929             :         }
     930           0 :         catch (const DuplicateKey &e) {
     931           0 :                 throw ConversionException("Error while writing binary data , the message was "
     932           0 :                                 + e.getMessage(), "CalData");
     933           0 :         }
     934           0 :         catch (const TagFormatException &e) {
     935           0 :                  throw ConversionException("Error while reading binary data , the message was "
     936           0 :                                 + e.getMessage(), "CalData");
     937           0 :         }
     938             :     }
     939             :     else {
     940          48 :                 for (uint32_t i = 0; i < this->declaredSize; i++) {
     941          47 :                         CalDataRow* aRow = CalDataRow::fromBin((EndianIStream&) eiss, *this, attributesSeq);
     942          47 :                         append(aRow);
     943             :         }       
     944             :     }
     945             :     //Does not change the convention defined in the model.      
     946             :     //archiveAsBin = true;
     947             :     //fileAsBin = true;
     948           1 :     if ( doc != NULL ) xmlFreeDoc(doc);
     949             : 
     950           1 :         }
     951             :         
     952           0 :         void CalDataTable::setUnknownAttributeBinaryReader(const string& attributeName, BinaryAttributeReaderFunctor* barFctr) {
     953             :                 //
     954             :                 // Is this attribute really unknown ?
     955             :                 //
     956           0 :                 for (vector<string>::const_iterator iter = attributesNamesOfCalData_v.begin(); iter != attributesNamesOfCalData_v.end(); iter++) {
     957           0 :                         if ((*iter).compare(attributeName) == 0) 
     958           0 :                                 throw ConversionException("the attribute '"+attributeName+"' is known you can't override the way it's read in the MIME binary file containing the table.", "CalData"); 
     959             :                 }
     960             :                 
     961             :                 // Ok then register the functor to activate when an unknown attribute is met during the reading of a binary table?
     962           0 :                 unknownAttributes2Functors[attributeName] = barFctr;
     963           0 :         }
     964             :         
     965           0 :         BinaryAttributeReaderFunctor* CalDataTable::getUnknownAttributeBinaryReader(const string& attributeName) const {
     966           0 :                 map<string, BinaryAttributeReaderFunctor*>::const_iterator iter = unknownAttributes2Functors.find(attributeName);
     967           0 :                 return (iter == unknownAttributes2Functors.end()) ? 0 : iter->second;
     968             :         }
     969             : 
     970             :         
     971           0 :         void CalDataTable::toFile(string directory) {
     972           0 :                 if (!directoryExists(directory.c_str()) &&
     973           0 :                         !createPath(directory.c_str())) {
     974           0 :                         throw ConversionException("Could not create directory " , directory);
     975             :                 }
     976             : 
     977           0 :                 string fileName = directory + "/CalData.xml";
     978           0 :                 ofstream tableout(fileName.c_str(),ios::out|ios::trunc);
     979           0 :                 if (tableout.rdstate() == ostream::failbit)
     980           0 :                         throw ConversionException("Could not open file " + fileName + " to write ", "CalData");
     981           0 :                 if (fileAsBin) 
     982           0 :                         tableout << MIMEXMLPart();
     983             :                 else
     984           0 :                         tableout << toXML() << endl;
     985           0 :                 tableout.close();
     986           0 :                 if (tableout.rdstate() == ostream::failbit)
     987           0 :                         throw ConversionException("Could not close file " + fileName, "CalData");
     988             : 
     989           0 :                 if (fileAsBin) {
     990             :                         // write the bin serialized
     991           0 :                         string fileName = directory + "/CalData.bin";
     992           0 :                         ofstream tableout(fileName.c_str(),ios::out|ios::trunc);
     993           0 :                         if (tableout.rdstate() == ostream::failbit)
     994           0 :                                 throw ConversionException("Could not open file " + fileName + " to write ", "CalData");
     995           0 :                         tableout << toMIME() << endl;
     996           0 :                         tableout.close();
     997           0 :                         if (tableout.rdstate() == ostream::failbit)
     998           0 :                                 throw ConversionException("Could not close file " + fileName, "CalData");
     999           0 :                 }
    1000           0 :         }
    1001             : 
    1002             :         
    1003           2 :         void CalDataTable::setFromFile(const string& directory) {
    1004             : #ifndef WITHOUT_BOOST
    1005             :     if (boost::filesystem::exists(boost::filesystem::path(uniqSlashes(directory + "/CalData.xml"))))
    1006             :       setFromXMLFile(directory);
    1007             :     else if (boost::filesystem::exists(boost::filesystem::path(uniqSlashes(directory + "/CalData.bin"))))
    1008             :       setFromMIMEFile(directory);
    1009             : #else 
    1010             :     // alternative in Misc.h
    1011           2 :     if (file_exists(uniqSlashes(directory + "/CalData.xml")))
    1012           2 :       setFromXMLFile(directory);
    1013           0 :     else if (file_exists(uniqSlashes(directory + "/CalData.bin")))
    1014           0 :       setFromMIMEFile(directory);
    1015             : #endif
    1016             :     else
    1017           0 :       throw ConversionException("No file found for the CalData table", "CalData");
    1018           2 :         }                       
    1019             : 
    1020             :         
    1021           1 :   void CalDataTable::setFromMIMEFile(const string& directory) {
    1022           1 :     string tablePath ;
    1023             :     
    1024           1 :     tablePath = directory + "/CalData.bin";
    1025           1 :     ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
    1026           1 :     if (!tablefile.is_open()) { 
    1027           0 :       throw ConversionException("Could not open file " + tablePath, "CalData");
    1028             :     }
    1029             :     // Read in a stringstream.
    1030           1 :     stringstream ss; ss << tablefile.rdbuf();
    1031             :     
    1032           1 :     if (tablefile.rdstate() == istream::failbit || tablefile.rdstate() == istream::badbit) {
    1033           0 :       throw ConversionException("Error reading file " + tablePath,"CalData");
    1034             :     }
    1035             :     
    1036             :     // And close.
    1037           1 :     tablefile.close();
    1038           1 :     if (tablefile.rdstate() == istream::failbit)
    1039           0 :       throw ConversionException("Could not close file " + tablePath,"CalData");
    1040             :     
    1041           1 :     setFromMIME(ss.str());
    1042           1 :   }     
    1043             : /* 
    1044             :   void CalDataTable::openMIMEFile (const string& directory) {
    1045             :                 
    1046             :         // Open the file.
    1047             :         string tablePath ;
    1048             :     tablePath = directory + "/CalData.bin";
    1049             :     ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
    1050             :     if (!tablefile.is_open())
    1051             :       throw ConversionException("Could not open file " + tablePath, "CalData");
    1052             :       
    1053             :         // Locate the xmlPartMIMEHeader.
    1054             :     string xmlPartMIMEHeader = "CONTENT-ID: <HEADER.XML>\n\n";
    1055             :     CharComparator comparator;
    1056             :     istreambuf_iterator<char> BEGIN(tablefile.rdbuf());
    1057             :     istreambuf_iterator<char> END;
    1058             :     istreambuf_iterator<char> it = search(BEGIN, END, xmlPartMIMEHeader.begin(), xmlPartMIMEHeader.end(), comparator);
    1059             :     if (it == END) 
    1060             :         throw ConversionException("failed to detect the beginning of the XML header", "CalData");
    1061             :     
    1062             :     // Locate the binaryPartMIMEHeader while accumulating the characters of the xml header.     
    1063             :     string binPartMIMEHeader = "--MIME_BOUNDARY\nCONTENT-TYPE: BINARY/OCTET-STREAM\nCONTENT-ID: <CONTENT.BIN>\n\n";
    1064             :     string xmlHeader;
    1065             :         CharCompAccumulator compaccumulator(&xmlHeader, 100000);
    1066             :         ++it;
    1067             :         it = search(it, END, binPartMIMEHeader.begin(), binPartMIMEHeader.end(), compaccumulator);
    1068             :         if (it == END) 
    1069             :                 throw ConversionException("failed to detect the beginning of the binary part", "CalData");
    1070             :         
    1071             :         cout << xmlHeader << endl;
    1072             :         //
    1073             :         // We have the xmlHeader , let's parse it.
    1074             :         //
    1075             :         xmlDoc *doc;
    1076             :     doc = xmlReadMemory(xmlHeader.data(), xmlHeader.size(), "BinaryTableHeader.xml", NULL, XML_PARSE_NOBLANKS);
    1077             :     if ( doc == NULL ) 
    1078             :       throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "CalData");
    1079             :     
    1080             :    // This vector will be filled by the names of  all the attributes of the table
    1081             :    // in the order in which they are expected to be found in the binary representation.
    1082             :    //
    1083             :     vector<string> attributesSeq(attributesNamesInBinOfCalData_v);
    1084             :       
    1085             :     xmlNode* root_element = xmlDocGetRootElement(doc);
    1086             :     if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
    1087             :       throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "CalData");
    1088             :     
    1089             :     const ByteOrder* byteOrder=0;
    1090             :     if ( string("ASDMBinaryTable").compare((const char*) root_element->name) == 0) {
    1091             :       // Then it's an "old fashioned" MIME file for tables.
    1092             :       // Just try to deserialize it with Big_Endian for the bytes ordering.
    1093             :       byteOrder = asdm::ByteOrder::Big_Endian;
    1094             :         
    1095             :       // And decide that it has version == "2"
    1096             :     version = "2";         
    1097             :      }
    1098             :     else if (string("CalDataTable").compare((const char*) root_element->name) == 0) {
    1099             :       // It's a new (and correct) MIME file for tables.
    1100             :       //
    1101             :       // 1st )  Look for a BulkStoreRef element with an attribute byteOrder.
    1102             :       //
    1103             :       xmlNode* bulkStoreRef = 0;
    1104             :       xmlNode* child = root_element->children;
    1105             :       
    1106             :       if (xmlHasProp(root_element, (const xmlChar*) "schemaVersion")) {
    1107             :         xmlChar * value = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
    1108             :         version = string ((const char *) value);
    1109             :         xmlFree(value); 
    1110             :       }
    1111             :       
    1112             :       // Skip the two first children (Entity and ContainerEntity).
    1113             :       bulkStoreRef = (child ==  0) ? 0 : ( (child->next) == 0 ? 0 : child->next->next );
    1114             :       
    1115             :       if ( bulkStoreRef == 0 || (bulkStoreRef->type != XML_ELEMENT_NODE)  || (string("BulkStoreRef").compare((const char*) bulkStoreRef->name) != 0))
    1116             :         throw ConversionException ("Could not find the element '/CalDataTable/BulkStoreRef'. Invalid XML header '"+ xmlHeader + "'.", "CalData");
    1117             :         
    1118             :       // We found BulkStoreRef, now look for its attribute byteOrder.
    1119             :       _xmlAttr* byteOrderAttr = 0;
    1120             :       for (struct _xmlAttr* attr = bulkStoreRef->properties; attr; attr = attr->next) 
    1121             :           if (string("byteOrder").compare((const char*) attr->name) == 0) {
    1122             :            byteOrderAttr = attr;
    1123             :            break;
    1124             :          }
    1125             :       
    1126             :       if (byteOrderAttr == 0) 
    1127             :              throw ConversionException("Could not find the element '/CalDataTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader +"'.", "CalData");
    1128             :       
    1129             :       string byteOrderValue = string((const char*) byteOrderAttr->children->content);
    1130             :       if (!(byteOrder = asdm::ByteOrder::fromString(byteOrderValue)))
    1131             :                 throw ConversionException("No valid value retrieved for the element '/CalDataTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader + "'.", "CalData");
    1132             :                 
    1133             :          //
    1134             :          // 2nd) Look for the Attributes element and grab the names of the elements it contains.
    1135             :          //
    1136             :          xmlNode* attributes = bulkStoreRef->next;
    1137             :      if ( attributes == 0 || (attributes->type != XML_ELEMENT_NODE)  || (string("Attributes").compare((const char*) attributes->name) != 0))     
    1138             :         throw ConversionException ("Could not find the element '/CalDataTable/Attributes'. Invalid XML header '"+ xmlHeader + "'.", "CalData");
    1139             :  
    1140             :         xmlNode* childOfAttributes = attributes->children;
    1141             :         
    1142             :         while ( childOfAttributes != 0 && (childOfAttributes->type == XML_ELEMENT_NODE) ) {
    1143             :                 attributesSeq.push_back(string((const char*) childOfAttributes->name));
    1144             :                 childOfAttributes = childOfAttributes->next;
    1145             :     }
    1146             :     }
    1147             :     // Create an EndianISStream from the substring containing the binary part.
    1148             :     EndianIFStream eifs(&tablefile, byteOrder);
    1149             :     
    1150             :     entity = Entity::fromBin((EndianIStream &) eifs);
    1151             :     
    1152             :     // We do nothing with that but we have to read it.
    1153             :     Entity containerEntity = Entity::fromBin((EndianIStream &) eifs);
    1154             : 
    1155             :         // Let's read numRows but ignore it and rely on the value specified in the ASDM.xml file.    
    1156             :     int numRows = eifs.readInt();
    1157             :     if ((numRows != -1)                        // Then these are *not* data produced at the EVLA.
    1158             :         && ((unsigned int) numRows != this->declaredSize )) { // Then the declared size (in ASDM.xml) is not equal to the one 
    1159             :                                                // written into the binary representation of the table.
    1160             :                 cout << "The a number of rows ('" 
    1161             :                          << numRows
    1162             :                          << "') declared in the binary representation of the table is different from the one declared in ASDM.xml ('"
    1163             :                          << this->declaredSize
    1164             :                          << "'). I'll proceed with the value declared in ASDM.xml"
    1165             :                          << endl;
    1166             :     }
    1167             :     // clean up xmlDoc pointer
    1168             :     if ( doc != NULL ) xmlFreeDoc(doc);    
    1169             :   } 
    1170             :  */
    1171             : 
    1172             :         
    1173           2 : void CalDataTable::setFromXMLFile(const string& directory) {
    1174           2 :     string tablePath ;
    1175             :     
    1176           2 :     tablePath = directory + "/CalData.xml";
    1177             :     
    1178             :     /*
    1179             :     ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
    1180             :     if (!tablefile.is_open()) { 
    1181             :       throw ConversionException("Could not open file " + tablePath, "CalData");
    1182             :     }
    1183             :       // Read in a stringstream.
    1184             :     stringstream ss;
    1185             :     ss << tablefile.rdbuf();
    1186             :     
    1187             :     if  (tablefile.rdstate() == istream::failbit || tablefile.rdstate() == istream::badbit) {
    1188             :       throw ConversionException("Error reading file '" + tablePath + "'", "CalData");
    1189             :     }
    1190             :     
    1191             :     // And close
    1192             :     tablefile.close();
    1193             :     if (tablefile.rdstate() == istream::failbit)
    1194             :       throw ConversionException("Could not close file '" + tablePath + "'", "CalData");
    1195             : 
    1196             :     // Let's make a string out of the stringstream content and empty the stringstream.
    1197             :     string xmlDocument = ss.str(); ss.str("");
    1198             :         
    1199             :     // Let's make a very primitive check to decide
    1200             :     // whether the XML content represents the table
    1201             :     // or refers to it via a <BulkStoreRef element.
    1202             :     */
    1203             :     
    1204           2 :     string xmlDocument;
    1205             :     try {
    1206           2 :         xmlDocument = getContainer().getXSLTransformer()(tablePath);
    1207           2 :         if (getenv("ASDM_DEBUG")) cout << "About to read " << tablePath << endl;
    1208             :     }
    1209           0 :     catch (const XSLTransformerException &e) {
    1210           0 :         throw ConversionException("Caugth an exception whose message is '" + e.getMessage() + "'.", "CalData");
    1211           0 :     }
    1212             :     
    1213           2 :     if (xmlDocument.find("<BulkStoreRef") != string::npos)
    1214           1 :       setFromMIMEFile(directory);
    1215             :     else
    1216           1 :       fromXML(xmlDocument);
    1217           2 :   }
    1218             : 
    1219             :         
    1220             : 
    1221             :         
    1222             : 
    1223             :                         
    1224             :         
    1225             :         
    1226             : 
    1227             :         
    1228           0 :         void CalDataTable::autoIncrement(string key, CalDataRow* x) {
    1229           0 :                 map<string, int>::iterator iter;
    1230           0 :                 if ((iter=noAutoIncIds.find(key)) == noAutoIncIds.end()) {
    1231             :                         // There is not yet a combination of the non autoinc attributes values in the hashtable
    1232             :                         
    1233             :                         // Initialize  calDataId to Tag(0).
    1234           0 :                         x->setCalDataId(Tag(0,  TagType::CalData));
    1235             :                         
    1236             :                         // Record it in the map.                
    1237           0 :                         noAutoIncIds.insert(make_pair(key, 0));                 
    1238             :                 } 
    1239             :                 else {
    1240             :                         // There is already a combination of the non autoinc attributes values in the hashtable
    1241             :                         // Increment its value.
    1242           0 :                         int n = iter->second + 1; 
    1243             :                         
    1244             :                         // Initialize  calDataId to Tag(n).
    1245           0 :                         x->setCalDataId(Tag(n, TagType::CalData));
    1246             :                         
    1247             :                         // Record it in the map.                
    1248           0 :                         noAutoIncIds.insert(make_pair(key, n));                         
    1249             :                 }               
    1250           0 :         }
    1251             :         
    1252             : } // End namespace asdm
    1253             :  

Generated by: LCOV version 1.16