LCOV - code coverage report
Current view: top level - alma/ASDM - CalReductionTable.cc (source / functions) Hit Total Coverage
Test: casa_coverage.info Lines: 19 408 4.7 %
Date: 2023-10-25 08:47:59 Functions: 5 37 13.5 %

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

Generated by: LCOV version 1.16