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

Generated by: LCOV version 1.16