LCOV - code coverage report
Current view: top level - alma/ASDM - CorrelatorModeTable.cc (source / functions) Hit Total Coverage
Test: casa_coverage.info Lines: 141 399 35.3 %
Date: 2023-10-25 08:47:59 Functions: 20 37 54.1 %

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

Generated by: LCOV version 1.16