LCOV - code coverage report
Current view: top level - alma/ASDM - AnnotationTable.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 19 419 4.5 %
Date: 2024-12-11 20:54:31 Functions: 5 37 13.5 %

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

Generated by: LCOV version 1.16