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

Generated by: LCOV version 1.16