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

Generated by: LCOV version 1.16