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

          Line data    Source code
       1             : 
       2             : /*
       3             :  * ALMA - Atacama Large Millimeter Array
       4             :  * (c) European Southern Observatory, 2002
       5             :  * (c) Associated Universities Inc., 2002
       6             :  * Copyright by ESO (in the framework of the ALMA collaboration),
       7             :  * Copyright by AUI (in the framework of the ALMA collaboration),
       8             :  * All rights reserved.
       9             :  * 
      10             :  * This library is free software; you can redistribute it and/or
      11             :  * modify it under the terms of the GNU Lesser General Public
      12             :  * License as published by the Free software Foundation; either
      13             :  * version 2.1 of the License, or (at your option) any later version.
      14             :  * 
      15             :  * This library is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY, without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :  * Lesser General Public License for more details.
      19             :  * 
      20             :  * You should have received a copy of the GNU Lesser General Public
      21             :  * License along with this library; if not, write to the Free Software
      22             :  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      23             :  * MA 02111-1307  USA
      24             :  *
      25             :  * Warning!
      26             :  *  -------------------------------------------------------------------- 
      27             :  * | This is generated code!  Do not modify this file.                  |
      28             :  * | If you do, all changes will be lost when the file is re-generated. |
      29             :  *  --------------------------------------------------------------------
      30             :  *
      31             :  * File FieldTable.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/FieldTable.h>
      43             : #include <alma/ASDM/FieldRow.h>
      44             : #include <alma/ASDM/Parser.h>
      45             : 
      46             : using asdm::ASDM;
      47             : using asdm::FieldTable;
      48             : using asdm::FieldRow;
      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 entityNameOfField = "Field";
      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 attributesNamesOfField_a[] = {
      80             :                 
      81             :                         "fieldId"
      82             :                 
      83             :                 
      84             :                         , "fieldName"
      85             :                 
      86             :                         , "numPoly"
      87             :                 
      88             :                         , "delayDir"
      89             :                 
      90             :                         , "phaseDir"
      91             :                 
      92             :                         , "referenceDir"
      93             :                                 
      94             :                 
      95             :                         , "time"
      96             :                 
      97             :                         , "code"
      98             :                 
      99             :                         , "directionCode"
     100             :                 
     101             :                         , "directionEquinox"
     102             :                 
     103             :                         , "assocNature"
     104             :                 
     105             :                         , "ephemerisId"
     106             :                 
     107             :                         , "sourceId"
     108             :                 
     109             :                         , "assocFieldId"
     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> attributesNamesOfField_v (attributesNamesOfField_a, attributesNamesOfField_a + sizeof(attributesNamesOfField_a) / sizeof(attributesNamesOfField_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 attributesNamesInBinOfField_a[] = {
     122             :     
     123             :          "fieldId" , "fieldName" , "numPoly" , "delayDir" , "phaseDir" , "referenceDir" 
     124             :         ,
     125             :          "time" , "code" , "directionCode" , "directionEquinox" , "assocNature" , "ephemerisId" , "sourceId" , "assocFieldId" 
     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> attributesNamesInBinOfField_v(attributesNamesInBinOfField_a, attributesNamesInBinOfField_a + sizeof(attributesNamesInBinOfField_a) / sizeof(attributesNamesInBinOfField_a[0]));             
     132             :         
     133             : 
     134             :         // The array of attributes (or column) names that make up key key.
     135             :         //
     136             :         string keyOfField_a[] = {
     137             :         
     138             :                 "fieldId"
     139             :                  
     140             :         };
     141             :          
     142             :         // A vector of strings which are copies of those stored in the array above.
     143             :         vector<string> keyOfField_v(keyOfField_a, keyOfField_a + sizeof(keyOfField_a) / sizeof(keyOfField_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>& FieldTable::getKeyName() {
     150           0 :                 return keyOfField_v;
     151             :         }
     152             : 
     153             : 
     154         110 :         FieldTable::FieldTable(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("FieldTable");
     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 FieldTable.
     178             :  */
     179         220 :         FieldTable::~FieldTable() {
     180         588 :                 for (unsigned int i = 0; i < privateRows.size(); i++) 
     181         478 :                         delete(privateRows.at(i));
     182         220 :         }
     183             : 
     184             :         /**
     185             :          * Container to which this table belongs.
     186             :          */
     187         225 :         ASDM &FieldTable::getContainer() const {
     188         225 :                 return container;
     189             :         }
     190             : 
     191             :         /**
     192             :          * Return the number of rows in the table.
     193             :          */
     194         380 :         unsigned int FieldTable::size() const {
     195         380 :                 if (presentInMemory) 
     196         313 :                         return privateRows.size();
     197             :                 else
     198          67 :                         return declaredSize;
     199             :         }
     200             :         
     201             :         /**
     202             :          * Return the name of this table.
     203             :          */
     204        2296 :         string FieldTable::getName() const {
     205        2296 :                 return entityNameOfField;
     206             :         }
     207             :         
     208             :         /**
     209             :          * Return the name of this table.
     210             :          */
     211           0 :         string FieldTable::name() {
     212           0 :                 return entityNameOfField;
     213             :         }
     214             :         
     215             :         /**
     216             :          * Return the the names of the attributes (or columns) of this table.
     217             :          */
     218           0 :         const vector<string>& FieldTable::getAttributesNames() { return attributesNamesOfField_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>& FieldTable::defaultAttributesNamesInBin() { return attributesNamesInBinOfField_v; }
     225             : 
     226             :         /**
     227             :          * Return this table's Entity.
     228             :          */
     229          40 :         Entity FieldTable::getEntity() const {
     230          40 :                 return entity;
     231             :         }
     232             : 
     233             :         /**
     234             :          * Set this table's Entity.
     235             :          */
     236          86 :         void FieldTable::setEntity(Entity e) {
     237          86 :                 this->entity = e; 
     238          86 :         }
     239             :         
     240             :         //
     241             :         // ====> Row creation.
     242             :         //
     243             :         
     244             :         /**
     245             :          * Create a new row.
     246             :          */
     247         223 :         FieldRow *FieldTable::newRow() {
     248         223 :                 return new FieldRow (*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 fieldName 
     257             :         
     258             :          * @param numPoly 
     259             :         
     260             :          * @param delayDir 
     261             :         
     262             :          * @param phaseDir 
     263             :         
     264             :          * @param referenceDir 
     265             :         
     266             :      */
     267         255 :         FieldRow* FieldTable::newRow(std::string fieldName, int numPoly, std::vector<std::vector<Angle > > delayDir, std::vector<std::vector<Angle > > phaseDir, std::vector<std::vector<Angle > > referenceDir){
     268         255 :                 FieldRow *row = new FieldRow(*this);
     269             :                         
     270         255 :                 row->setFieldName(fieldName);
     271             :                         
     272         255 :                 row->setNumPoly(numPoly);
     273             :                         
     274         255 :                 row->setDelayDir(delayDir);
     275             :                         
     276         255 :                 row->setPhaseDir(phaseDir);
     277             :                         
     278         255 :                 row->setReferenceDir(referenceDir);
     279             :         
     280         255 :                 return row;             
     281             :         }       
     282             :         
     283             : 
     284             : 
     285           0 : FieldRow* FieldTable::newRow(FieldRow* row) {
     286           0 :         return new FieldRow(*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.fieldId, 
     299             :          * add x to its table and returns x.
     300             :          *  
     301             :          * @returns a pointer on a FieldRow.
     302             :          * @param x. A pointer on the row to be added.
     303             :          */ 
     304             :                 
     305             :                         
     306         255 :         FieldRow* FieldTable::add(FieldRow* x) {
     307             :                          
     308         510 :                 FieldRow* aRow = lookup(
     309             :                                 
     310         510 :                 x->getFieldName()
     311             :                                 ,
     312             :                 x->getNumPoly()
     313             :                                 ,
     314         510 :                 x->getDelayDir()
     315             :                                 ,
     316         510 :                 x->getPhaseDir()
     317             :                                 ,
     318         510 :                 x->getReferenceDir()
     319             :                                 
     320             :                 );
     321         255 :                 if (aRow) return aRow;
     322             :                         
     323             : 
     324             :                         
     325             :                 // Autoincrement fieldId
     326         255 :                 x->setFieldId(Tag(size(), TagType::Field));
     327             :                                                 
     328         255 :                 row.push_back(x);
     329         255 :                 privateRows.push_back(x);
     330         255 :                 x->isAdded(true);
     331         255 :                 return x;
     332             :         }
     333             :                 
     334             :         
     335             :                 
     336         223 :         void FieldTable::addWithoutCheckingUnique(FieldRow * x) {
     337         223 :                 if (getRowByKey(
     338         446 :                                                 x->getFieldId()
     339         223 :                                                 ) != (FieldRow *) 0) 
     340           0 :                         throw DuplicateKey("Dupicate key exception in ", "FieldTable");
     341         223 :                 row.push_back(x);
     342         223 :                 privateRows.push_back(x);
     343         223 :                 x->isAdded(true);
     344         223 :         }
     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 :         FieldRow*  FieldTable::checkAndAdd(FieldRow* x, bool skipCheckUniqueness)  {
     367           0 :                 if (!skipCheckUniqueness) { 
     368             :          
     369             :                  
     370           0 :                         if (lookup(
     371             :                         
     372           0 :                                 x->getFieldName()
     373             :                 ,
     374             :                                 x->getNumPoly()
     375             :                 ,
     376           0 :                                 x->getDelayDir()
     377             :                 ,
     378           0 :                                 x->getPhaseDir()
     379             :                 ,
     380           0 :                                 x->getReferenceDir()
     381             :                 
     382           0 :                         )) throw UniquenessViolationException();
     383             :                 
     384             :                 
     385             :                 }
     386             :                 
     387           0 :                 if (getRowByKey(
     388             :         
     389           0 :                         x->getFieldId()
     390             :                         
     391           0 :                 )) throw DuplicateKey("Duplicate key exception in ", "FieldTable");
     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 FieldTable::append(FieldRow *x) {
     406           0 :                 privateRows.push_back(x);
     407           0 :                 x->isAdded(true);
     408           0 :         }
     409             : 
     410             : 
     411             : 
     412             : 
     413             : 
     414          22 :          vector<FieldRow *> FieldTable::get() {
     415          22 :                 checkPresenceInMemory();
     416          22 :             return privateRows;
     417             :          }
     418             :          
     419           0 :          const vector<FieldRow *>& FieldTable::get() const {
     420           0 :                 const_cast<FieldTable&>(*this).checkPresenceInMemory();       
     421           0 :             return privateRows;
     422             :          }       
     423             :                 
     424             : 
     425             : 
     426             : 
     427             : 
     428             :         
     429             : 
     430             :         
     431             : /*
     432             :  ** Returns a FieldRow* 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       11551 :         FieldRow* FieldTable::getRowByKey(Tag fieldId)  {
     438       11551 :         checkPresenceInMemory();
     439       11551 :         FieldRow* aRow = 0;
     440       12839 :         for (unsigned int i = 0; i < privateRows.size(); i++) {
     441       12616 :                 aRow = row.at(i);
     442             :                 
     443             :                         
     444       12616 :                                 if (aRow->fieldId != fieldId) continue;
     445             :                         
     446             :                 
     447       11328 :                 return aRow;
     448             :         }
     449         223 :         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 fieldName.
     461             :                         
     462             :  * @param numPoly.
     463             :                         
     464             :  * @param delayDir.
     465             :                         
     466             :  * @param phaseDir.
     467             :                         
     468             :  * @param referenceDir.
     469             :                          
     470             :  */
     471         255 : FieldRow* FieldTable::lookup(std::string fieldName, int numPoly, std::vector<std::vector<Angle > > delayDir, std::vector<std::vector<Angle > > phaseDir, std::vector<std::vector<Angle > > referenceDir) {
     472             :                 FieldRow* aRow;
     473       11075 :                 for (unsigned int i = 0; i < privateRows.size(); i++) {
     474       10820 :                         aRow = privateRows.at(i); 
     475       10820 :                         if (aRow->compareNoAutoInc(fieldName, numPoly, delayDir, phaseDir, referenceDir)) return aRow;
     476             :                 }                       
     477         255 :                 return 0;       
     478             : } 
     479             :         
     480             :                 
     481             : 
     482             :         
     483             : 
     484             : 
     485             : 
     486             : #ifndef WITHOUT_ACS
     487             :         using asdmIDL::FieldTableIDL;
     488             : #endif
     489             : 
     490             : #ifndef WITHOUT_ACS
     491             :         // Conversion Methods
     492             : 
     493             :         FieldTableIDL *FieldTable::toIDL() {
     494             :                 FieldTableIDL *x = new FieldTableIDL ();
     495             :                 unsigned int nrow = size();
     496             :                 x->row.length(nrow);
     497             :                 vector<FieldRow*> 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 FieldTable::toIDL(asdmIDL::FieldTableIDL& x) const {
     506             :                 unsigned int nrow = size();
     507             :                 x.row.length(nrow);
     508             :                 vector<FieldRow*> 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 FieldTable::fromIDL(FieldTableIDL x) {
     517             :                 unsigned int nrow = x.row.length();
     518             :                 for (unsigned int i = 0; i < nrow; ++i) {
     519             :                         FieldRow *tmp = newRow();
     520             :                         tmp->setFromIDL(x.row[i]);
     521             :                         // checkAndAdd(tmp);
     522             :                         add(tmp);
     523             :                 }
     524             :         }       
     525             : #endif
     526             : 
     527             :         
     528          13 :         string FieldTable::toXML()  {
     529          13 :                 string buf;
     530             : 
     531          13 :                 buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> ");
     532          13 :                 buf.append("<FieldTable xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:field=\"http://Alma/XASDM/FieldTable\" xsi:schemaLocation=\"http://Alma/XASDM/FieldTable http://almaobservatory.org/XML/XASDM/4/FieldTable.xsd\" schemaVersion=\"4\" schemaRevision=\"-1\">\n");
     533             :         
     534          13 :                 buf.append(entity.toXML());
     535          26 :                 string s = container.getEntity().toXML();
     536             :                 // Change the "Entity" tag to "ContainerEntity".
     537          13 :                 buf.append("<Container" + s.substr(1,s.length() - 1)+" ");
     538          26 :                 vector<FieldRow*> v = get();
     539         262 :                 for (unsigned int i = 0; i < v.size(); ++i) {
     540             :                         try {
     541         249 :                                 buf.append(v[i]->toXML());
     542           0 :                         } catch (const NoSuchRow &e) {
     543             :                         }
     544         249 :                         buf.append("  ");
     545             :                 }               
     546          13 :                 buf.append("</FieldTable> ");
     547          26 :                 return buf;
     548             :         }
     549             : 
     550             :         
     551           0 :         string FieldTable::getVersion() const {
     552           0 :                 return version;
     553             :         }
     554             :         
     555             : 
     556          72 :         void FieldTable::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          72 :         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          72 :                 if ( doc == NULL )
     567           0 :                         throw ConversionException("Failed to parse the xmlHeader into a DOM structure.", "Field");
     568             :                 
     569          72 :                 xmlNode* root_element = xmlDocGetRootElement(doc);
     570          72 :                 if ( root_element == NULL || root_element->type != XML_ELEMENT_NODE )
     571           0 :                 throw ConversionException("Failed to retrieve the root element in the DOM structure.", "Field");
     572             :                 
     573          72 :         xmlChar * propValue = xmlGetProp(root_element, (const xmlChar *) "schemaVersion");
     574          72 :         if ( propValue != 0 ) {
     575          47 :                 version = string( (const char*) propValue);
     576          47 :                 xmlFree(propValue);             
     577             :         }
     578             :                                                                         
     579         144 :                 Parser xml(tableInXML);
     580          72 :                 if (!xml.isStr("<FieldTable")) 
     581           0 :                         error();
     582             :                 // cout << "Parsing a FieldTable" << endl;
     583         216 :                 string s = xml.getElement("<Entity","/>");
     584          72 :                 if (s.length() == 0) 
     585           0 :                         error();
     586         144 :                 Entity e;
     587          72 :                 e.setFromXML(s);
     588          72 :                 if (e.getEntityTypeName() != "FieldTable")
     589           0 :                         error();
     590          72 :                 setEntity(e);
     591             :                 // Skip the container's entity; but, it has to be there.
     592          72 :                 s = xml.getElement("<ContainerEntity","/>");
     593          72 :                 if (s.length() == 0) 
     594           0 :                         error();
     595             : 
     596             :                 // Get each row in the table.
     597          72 :                 s = xml.getElementContent("<row>","</row>");
     598             :                 FieldRow *row;
     599          72 :                 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(),"FieldTable");
     611             :                         } 
     612           0 :                         catch (const UniquenessViolationException &e1) {
     613           0 :                                 throw ConversionException(e1.getMessage(),"FieldTable");      
     614             :                         }
     615           0 :                         catch (...) {
     616             :                                 // cout << "Unexpected error in FieldTable::checkAndAdd called from FieldTable::fromXML " << endl;
     617             :                         }
     618             :                 }
     619             :                 else {
     620             :                         try {
     621         295 :                                 while (s.length() != 0) {
     622         223 :                                         row = newRow();
     623         223 :                                         row->setFromXML(s);
     624         223 :                                         addWithoutCheckingUnique(row);
     625         223 :                                         s = xml.getElementContent("<row>","</row>");
     626             :                                 }
     627             :                         }
     628           0 :                         catch (const DuplicateKey &e1) {
     629           0 :                                 throw ConversionException(e1.getMessage(),"FieldTable");
     630             :                         } 
     631           0 :                         catch (...) {
     632             :                                 // cout << "Unexpected error in FieldTable::addWithoutCheckingUnique called from FieldTable::fromXML " << endl;
     633             :                         }
     634             :                 }                               
     635             :                                 
     636             :                                 
     637          72 :                 if (!xml.isStr("</FieldTable>")) 
     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          72 :                 if ( doc != NULL ) xmlFreeDoc(doc);
     646             :                 
     647          72 :         }
     648             : 
     649             :         
     650           0 :         void FieldTable::error()  {
     651           0 :                 throw ConversionException("Invalid xml document","Field");
     652             :         }
     653             :         
     654             :         
     655           0 :         string FieldTable::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 << "<FieldTable xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:field=\"http://Alma/XASDM/FieldTable\" xsi:schemaLocation=\"http://Alma/XASDM/FieldTable http://almaobservatory.org/XML/XASDM/4/FieldTable.xsd\" schemaVersion=\"4\" schemaRevision=\"-1\">\n";
     663           0 :                 oss<< "<Entity entityId='"<<UID<<"' entityIdEncrypted='na' entityTypeName='FieldTable' 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 << "<fieldId/>\n"; 
     669           0 :                 oss << "<fieldName/>\n"; 
     670           0 :                 oss << "<numPoly/>\n"; 
     671           0 :                 oss << "<delayDir/>\n"; 
     672           0 :                 oss << "<phaseDir/>\n"; 
     673           0 :                 oss << "<referenceDir/>\n"; 
     674             : 
     675           0 :                 oss << "<time/>\n"; 
     676           0 :                 oss << "<code/>\n"; 
     677           0 :                 oss << "<directionCode/>\n"; 
     678           0 :                 oss << "<directionEquinox/>\n"; 
     679           0 :                 oss << "<assocNature/>\n"; 
     680           0 :                 oss << "<ephemerisId/>\n"; 
     681           0 :                 oss << "<sourceId/>\n"; 
     682           0 :                 oss << "<assocFieldId/>\n"; 
     683           0 :                 oss << "</Attributes>\n";         
     684           0 :                 oss << "</FieldTable>\n";
     685             : 
     686           0 :                 return oss.str();                               
     687             :         }
     688             :         
     689           0 :         string FieldTable::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 FieldTable::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", "Field");
     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", "Field");
     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.", "Field");
     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.", "Field");
     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("fieldId") ; 
     799             :          
     800           0 :     attributesSeq.push_back("fieldName") ; 
     801             :          
     802           0 :     attributesSeq.push_back("numPoly") ; 
     803             :          
     804           0 :     attributesSeq.push_back("delayDir") ; 
     805             :          
     806           0 :     attributesSeq.push_back("phaseDir") ; 
     807             :          
     808           0 :     attributesSeq.push_back("referenceDir") ; 
     809             :         
     810             :          
     811           0 :     attributesSeq.push_back("time") ; 
     812             :          
     813           0 :     attributesSeq.push_back("code") ; 
     814             :          
     815           0 :     attributesSeq.push_back("directionCode") ; 
     816             :          
     817           0 :     attributesSeq.push_back("directionEquinox") ; 
     818             :          
     819           0 :     attributesSeq.push_back("assocNature") ; 
     820             :          
     821           0 :     attributesSeq.push_back("ephemerisId") ; 
     822             :          
     823           0 :     attributesSeq.push_back("sourceId") ; 
     824             :          
     825           0 :     attributesSeq.push_back("assocFieldId") ; 
     826             :         
     827             :      
     828             :     
     829             :     
     830             :     // And decide that it has version == "2"
     831           0 :     version = "2";         
     832             :      }
     833           0 :     else if (string("FieldTable").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 '/FieldTable/BulkStoreRef'. Invalid XML header '"+ xmlHeader + "'.", "Field");
     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 '/FieldTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader +"'.", "Field");
     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 '/FieldTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader + "'.", "Field");
     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 '/FieldTable/Attributes'. Invalid XML header '"+ xmlHeader + "'.", "Field");
     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 :                                 FieldRow* aRow = FieldRow::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(), "Field");
     913             :         }
     914           0 :         catch (const TagFormatException &e) {
     915             :                  throw ConversionException("Error while reading binary data , the message was "
     916           0 :                                 + e.getMessage(), "Field");
     917             :         }
     918             :     }
     919             :     else {
     920           0 :                 for (uint32_t i = 0; i < this->declaredSize; i++) {
     921           0 :                         FieldRow* aRow = FieldRow::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 FieldTable::setUnknownAttributeBinaryReader(const string& attributeName, BinaryAttributeReaderFunctor* barFctr) {
     933             :                 //
     934             :                 // Is this attribute really unknown ?
     935             :                 //
     936           0 :                 for (vector<string>::const_iterator iter = attributesNamesOfField_v.begin(); iter != attributesNamesOfField_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.", "Field"); 
     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* FieldTable::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          13 :         void FieldTable::toFile(string directory) {
     952          13 :                 if (!directoryExists(directory.c_str()) &&
     953           0 :                         !createPath(directory.c_str())) {
     954           0 :                         throw ConversionException("Could not create directory " , directory);
     955             :                 }
     956             : 
     957          26 :                 string fileName = directory + "/Field.xml";
     958          26 :                 ofstream tableout(fileName.c_str(),ios::out|ios::trunc);
     959          13 :                 if (tableout.rdstate() == ostream::failbit)
     960           0 :                         throw ConversionException("Could not open file " + fileName + " to write ", "Field");
     961          13 :                 if (fileAsBin) 
     962           0 :                         tableout << MIMEXMLPart();
     963             :                 else
     964          13 :                         tableout << toXML() << endl;
     965          13 :                 tableout.close();
     966          13 :                 if (tableout.rdstate() == ostream::failbit)
     967           0 :                         throw ConversionException("Could not close file " + fileName, "Field");
     968             : 
     969          13 :                 if (fileAsBin) {
     970             :                         // write the bin serialized
     971           0 :                         string fileName = directory + "/Field.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 ", "Field");
     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, "Field");
     979             :                 }
     980          13 :         }
     981             : 
     982             :         
     983          72 :         void FieldTable::setFromFile(const string& directory) {
     984             : #ifndef WITHOUT_BOOST
     985             :     if (boost::filesystem::exists(boost::filesystem::path(uniqSlashes(directory + "/Field.xml"))))
     986             :       setFromXMLFile(directory);
     987             :     else if (boost::filesystem::exists(boost::filesystem::path(uniqSlashes(directory + "/Field.bin"))))
     988             :       setFromMIMEFile(directory);
     989             : #else 
     990             :     // alternative in Misc.h
     991          72 :     if (file_exists(uniqSlashes(directory + "/Field.xml")))
     992          72 :       setFromXMLFile(directory);
     993           0 :     else if (file_exists(uniqSlashes(directory + "/Field.bin")))
     994           0 :       setFromMIMEFile(directory);
     995             : #endif
     996             :     else
     997           0 :       throw ConversionException("No file found for the Field table", "Field");
     998          72 :         }                       
     999             : 
    1000             :         
    1001           0 :   void FieldTable::setFromMIMEFile(const string& directory) {
    1002           0 :     string tablePath ;
    1003             :     
    1004           0 :     tablePath = directory + "/Field.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, "Field");
    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,"Field");
    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,"Field");
    1020             :     
    1021           0 :     setFromMIME(ss.str());
    1022           0 :   }     
    1023             : /* 
    1024             :   void FieldTable::openMIMEFile (const string& directory) {
    1025             :                 
    1026             :         // Open the file.
    1027             :         string tablePath ;
    1028             :     tablePath = directory + "/Field.bin";
    1029             :     ifstream tablefile(tablePath.c_str(), ios::in|ios::binary);
    1030             :     if (!tablefile.is_open())
    1031             :       throw ConversionException("Could not open file " + tablePath, "Field");
    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", "Field");
    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", "Field");
    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.", "Field");
    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(attributesNamesInBinOfField_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.", "Field");
    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("FieldTable").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 '/FieldTable/BulkStoreRef'. Invalid XML header '"+ xmlHeader + "'.", "Field");
    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 '/FieldTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader +"'.", "Field");
    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 '/FieldTable/BulkStoreRef/@byteOrder'. Invalid XML header '" + xmlHeader + "'.", "Field");
    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 '/FieldTable/Attributes'. Invalid XML header '"+ xmlHeader + "'.", "Field");
    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          72 : void FieldTable::setFromXMLFile(const string& directory) {
    1154         144 :     string tablePath ;
    1155             :     
    1156          72 :     tablePath = directory + "/Field.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, "Field");
    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 + "'", "Field");
    1169             :     }
    1170             :     
    1171             :     // And close
    1172             :     tablefile.close();
    1173             :     if (tablefile.rdstate() == istream::failbit)
    1174             :       throw ConversionException("Could not close file '" + tablePath + "'", "Field");
    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         144 :     string xmlDocument;
    1185             :     try {
    1186          72 :         xmlDocument = getContainer().getXSLTransformer()(tablePath);
    1187          72 :         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() + "'.", "Field");
    1191             :     }
    1192             :     
    1193          72 :     if (xmlDocument.find("<BulkStoreRef") != string::npos)
    1194           0 :       setFromMIMEFile(directory);
    1195             :     else
    1196          72 :       fromXML(xmlDocument);
    1197          72 :   }
    1198             : 
    1199             :         
    1200             : 
    1201             :         
    1202             : 
    1203             :                         
    1204             :         
    1205             :         
    1206             : 
    1207             :         
    1208           0 :         void FieldTable::autoIncrement(string key, FieldRow* 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  fieldId to Tag(0).
    1214           0 :                         x->setFieldId(Tag(0,  TagType::Field));
    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  fieldId to Tag(n).
    1225           0 :                         x->setFieldId(Tag(n, TagType::Field));
    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