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

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

Generated by: LCOV version 1.16