LCOV - code coverage report
Current view: top level - alma/ASDM - PointingModelTable.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 0 434 0.0 %
Date: 2024-10-28 15:53:10 Functions: 0 38 0.0 %

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

Generated by: LCOV version 1.16