LCOV - code coverage report
Current view: top level - alma/ASDM - CalPositionTable.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 437 0.0 %
Date: 2024-10-29 13:38:20 Functions: 0 36 0.0 %

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

Generated by: LCOV version 1.16