LCOV - code coverage report
Current view: top level - alma/ASDMBinaries - SDMDataObjectParser.h (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 22 35 62.9 %
Date: 2025-12-19 19:56:21 Functions: 10 15 66.7 %

          Line data    Source code
       1             : #ifndef SDMDataObjectParser_CLASS
       2             : #define SDMDataObjectParser_CLASS
       3             : 
       4             : #include <iostream>
       5             : #include <string>
       6             : #include <vector>
       7             : #include <libxml/parser.h>
       8             : #include <libxml/tree.h>
       9             : 
      10             : 
      11             : 
      12             : #ifdef REG_BASIC
      13             : #undef REG_BASIC
      14             : #endif
      15             : 
      16             : #ifdef REG_EXTENDED 
      17             : #undef REG_EXTENDED
      18             : #endif 
      19             : 
      20             : #ifdef REG_ICASE
      21             : #undef REG_ICASE
      22             : #endif
      23             : 
      24             : #ifdef REG_NOSUB
      25             : #undef REG_NOSUB
      26             : #endif
      27             : 
      28             : #ifdef REG_NEWLINE
      29             : #undef REG_NEWLINE
      30             : #endif
      31             : 
      32             : #ifdef REG_NOTBOL
      33             : #undef REG_NOTBOL
      34             : #endif
      35             : 
      36             : #ifdef REG_NOTEOL
      37             : #undef REG_NOTEOL
      38             : #endif
      39             : 
      40             : #ifdef REG_STARTEND
      41             : #undef REG_STARTEND
      42             : #endif
      43             : 
      44             : #include <regex>
      45             : 
      46             : #include <alma/ASDMBinaries/SDMDataObject.h>
      47             : 
      48             : 
      49             : #ifndef WITHOUT_ACS
      50             : #include "almaEnumerations_IFC.h"
      51             : #endif
      52             : #include <alma/Enumerations/CAtmPhaseCorrection.h>
      53             : 
      54             : #include <alma/Enumerations/CPrimitiveDataType.h>
      55             : 
      56             : #include <alma/Enumerations/CCorrelatorType.h>
      57             : 
      58             : namespace asdmbinaries {
      59             : 
      60             :   /**
      61             :    * A class to represent an exception thrown during the parsing of an XML header 
      62             :    * in a MIME message containing ALMA binary data.
      63             :    */ 
      64             :   class SDMDataObjectParserException {
      65             :     
      66             :   public:
      67             :     /**
      68             :      * An empty contructor.
      69             :      */
      70             :     SDMDataObjectParserException();
      71             : 
      72             :     /**
      73             :      * A constructor with a message associated with the exception.
      74             :      * @param m a string containing the message.
      75             :      */
      76             :     SDMDataObjectParserException(std::string m);
      77             :     
      78             :     /**
      79             :      * The destructor.
      80             :      */
      81             :     virtual ~SDMDataObjectParserException();
      82             : 
      83             :     /**
      84             :      * Returns the message associated to this exception.
      85             :      * @return a string.
      86             :      */
      87             :     std::string getMessage() const;
      88             :     
      89             :   protected:
      90             :     std::string message;
      91             :     
      92             :   };
      93             :   
      94             :   inline SDMDataObjectParserException::SDMDataObjectParserException() : message ("SDMDataObjectParserException") {}
      95           0 :   inline SDMDataObjectParserException::SDMDataObjectParserException(std::string m) : message(m) {}
      96           0 :   inline SDMDataObjectParserException::~SDMDataObjectParserException() {}
      97           0 :   inline std::string SDMDataObjectParserException::getMessage() const {
      98           0 :     return "SDMDataObjectParserException : " + message;
      99             :   }
     100             : 
     101             : 
     102             :   // class HeaderParser
     103             :   class HeaderParser {
     104             :     friend class SDMDataObjectParser;
     105             :     friend class SDMDataObject;
     106             :     
     107             :   public:
     108             :     HeaderParser();
     109             :     virtual ~HeaderParser();
     110             :     
     111             :     void parseFile(const std::string& filename, SDMDataObject& sdmDataObject);
     112             :     void parseMemory(const std::string& buffer, SDMDataObject& sdmDataObject);
     113             :     
     114             :     void reset();
     115             :     
     116             :     
     117             :     
     118             :   private:
     119             :     void parseSDMDataHeader(xmlNode* a_node, SDMDataObject& sdmDataObject);
     120             :     
     121             :     void parseProjectPath(xmlNode* a_node, SDMDataObject& sdmDataObject);
     122             :     long long parseStartTime(xmlNode* a_node);
     123             :     std::string parseDataOID(xmlNode* a_node);
     124             :     int  parseDimensionality(xmlNode* a_node);
     125             :     int  parseNumTime(xmlNode* a_node);
     126             :     void parseExecBlock(xmlNode* a_node, SDMDataObject& sdmDataObject);
     127             :     int  parseExecBlockNum(xmlNode* a_node);
     128             :     int  parseScanNum(xmlNode* a_node);
     129             :     int  parseSubscanNum(xmlNode* a_node);
     130             :     
     131             :     int  parseNumAntenna(xmlNode* a_node);
     132             :     
     133             :     void parseCorrelationMode(xmlNode* a_node, SDMDataObject& sdmDataObject);
     134             :     void parseSpectralResolution(xmlNode* a_node, SDMDataObject& sdmDataObject);
     135             :     void parseProcessorType(xmlNode* a_node, SDMDataObject& sdmDataObject);
     136             :     void parseDataStruct (xmlNode* a_node, SDMDataObject& sdmDataObject);
     137             :     
     138             :     SDMDataObject::Baseband  parseBaseband(xmlNode* a_node, SDMDataObject& sdmDataObject);
     139             :     
     140             :     void  parseSpectralWindow(xmlNode* a_node, SDMDataObject& sdmDataObject, std::vector<SDMDataObject::SpectralWindow>& spectralWindow);
     141             :     
     142             :     SDMDataObject::BinaryPart parseBinaryPart(xmlNode* a_node, const std::string& attachmentName);
     143             :     SDMDataObject::AutoDataBinaryPart parseAutoDataBinaryPart(xmlNode* a_node, const std::string& attachmentName);
     144             :     SDMDataObject::ZeroLagsBinaryPart parseZeroLagsBinaryPart(xmlNode* a_node, const std::string& attachmentName);
     145             : 
     146             :     //      SDMDataObject::TypedBinaryPart  parseTypedBinaryPart(xmlNode* a_node, const std::string& attachmentName);
     147             :     
     148             :     xmlDoc *doc;
     149             : 
     150             :     static const std::regex  PROJECTPATH3;
     151             :     const static std::string SDMDATAHEADER;
     152             :     const static std::string SCHEMAVERSION;
     153             :     const static std::string BYTEORDER;
     154             :     const static std::string PROJECTPATH;
     155             :     const static std::string STARTTIME;
     156             :     const static std::string DATAOID;
     157             :     const static std::string XLINKHREF;
     158             :     const static std::string XLINKTITLE;
     159             :     
     160             :     const static std::string DIMENSIONALITY;
     161             :     const static std::string NUMTIME;
     162             :     
     163             :     const static std::string EXECBLOCK;
     164             :     const static std::string EXECBLOCKNUM;
     165             :     const static std::string SCANNUM;
     166             :     const static std::string SUBSCANNUM;
     167             :     
     168             :     const static std::string NUMANTENNA;
     169             :     
     170             :     const static std::string CORRELATIONMODE;
     171             :     const static std::string SPECTRALRESOLUTION;
     172             :     const static std::string PROCESSORTYPE;
     173             :     const static std::string DATASTRUCT;
     174             :     const static std::string APC;
     175             :     const static std::string REF;
     176             :     
     177             :     const static std::string BASEBAND;
     178             :     const static std::string NAME;
     179             :     
     180             :     const static std::string SPECTRALWINDOW;
     181             :     const static std::string SW;
     182             :     const static std::string SWBB;
     183             :     const static std::string CROSSPOLPRODUCTS;
     184             :     const static std::string SDPOLPRODUCTS;
     185             :     const static std::string SCALEFACTOR;
     186             :     const static std::string NUMSPECTRALPOINT;
     187             :     const static std::string NUMBIN;
     188             :     const static std::string SIDEBAND;
     189             :     const static std::string IMAGE;
     190             :     
     191             :     const static std::string FLAGS;
     192             :     const static std::string ACTUALTIMES;
     193             :     const static std::string ACTUALDURATIONS;
     194             :     const static std::string ZEROLAGS;
     195             :     const static std::string CORRELATORTYPE;
     196             :     const static std::string CROSSDATA;
     197             :     const static std::string AUTODATA;
     198             :     const static std::string NORMALIZED;
     199             :     
     200             :     const static std::string SIZE;
     201             :     const static std::string AXES;
     202             :     const static std::string TYPE;
     203             :   }; // class HeaderParser
     204             : 
     205             :   // class CorrSubsetHeaderParser
     206             :   class CorrSubsetHeaderParser {
     207             :     friend class SDMDataSubset;
     208             :     
     209             :   public:
     210             :     CorrSubsetHeaderParser();
     211             :     virtual ~CorrSubsetHeaderParser();
     212             :     void parseFile(const std::string& filename, SDMDataSubset& sdmCorrDataSubset);
     213             :     void parseMemory(const std::string& buffer, SDMDataSubset& sdmCorrDataSubset);
     214             :     void parseCrossDataType(xmlNode* a_node, SDMDataSubset& sdmCorrDataSubset);
     215             :     void reset();
     216             :     
     217             :   private:
     218             :     // Regular expressions used to decipher the content of projectPath attribute.
     219             :     static const std::regex PROJECTPATH4;
     220             :     static const std::regex PROJECTPATH5;
     221             :     
     222             :     void parseSDMDataSubsetHeader(xmlNode* a_node, SDMDataSubset& sdmCorrDataSubset);
     223             :     //    void parseProjectPath(xmlNode* a_node, SDMDataSubset& sdmCorrDataSubset);
     224             :     void parseSchedulePeriodTime(xmlNode* a_node, SDMDataSubset& sdmCorrDataSubset);
     225             :     long long parseTime(xmlNode* a_node);
     226             :     long long parseInterval(xmlNode* a_node);
     227             :     void parseAbortObservation(xmlNode* a_node, SDMDataSubset& sdmCorrDataSubset);
     228             :     
     229             :     xmlDoc* doc;
     230             :     
     231             :     const static std::string SDMDATASUBSETHEADER;
     232             :     const static std::string PROJECTPATH;
     233             :     const static std::string SCHEDULEPERIODTIME;
     234             :     const static std::string TIME;
     235             :     const static std::string INTERVAL;
     236             :     const static std::string DATASTRUCT;
     237             :     const static std::string REF;
     238             :     const static std::string ABORTOBSERVATION;
     239             :     const static std::string ABORTTIME;
     240             :     const static std::string ABORTREASON;
     241             :     const static std::string XLINKHREF;
     242             :     const static std::string DATAREF;
     243             :     const static std::string FLAGSREF;
     244             :     const static std::string ACTUALTIMESREF;
     245             :     const static std::string ACTUALDURATIONSREF;
     246             :     const static std::string ZEROLAGSREF;
     247             :     const static std::string CROSSDATAREF;
     248             :     const static std::string TYPE;
     249             :     const static std::string AUTODATAREF;
     250             :   };  // class CorrSubsetHeaderParser
     251             : 
     252             : 
     253             :   // class TPSubsetHeaderParser
     254             :   class TPSubsetHeaderParser {
     255             :     friend class SDMDataSubset;
     256             :   public:
     257             :     TPSubsetHeaderParser();
     258             :     virtual ~TPSubsetHeaderParser();
     259             :     void parseFile(const std::string& filename,SDMDataSubset& sdmTPDataSubset );
     260             :     void parseMemory(const std::string& buffer,SDMDataSubset& sdmTPDataSubset );
     261             :     void reset();
     262             :     
     263             :   private:
     264             :     // Regular expressions used to decipher the content of projectPath attribute.
     265             :     static const std::regex PROJECTPATH3;
     266             : 
     267             :     void parseSDMDataSubsetHeader(xmlNode* a_node,SDMDataSubset& sdmTPDataSubset);
     268             :     void parseProjectPath(xmlNode* a_node, SDMDataSubset& sdmTPDataSubset);
     269             :     void parseSchedulePeriodTime(xmlNode* a_node, SDMDataSubset& sdmCorrDataSubset);
     270             :     long long parseTime(xmlNode* a_node);
     271             :     long long parseInterval(xmlNode* a_node);
     272             :     std::string parseDataStructureDesc(xmlNode* a_node);
     273             :     void parseBinaryData(xmlNode* a_node, SDMDataSubset& sdmTPDataSubset);
     274             :     
     275             :     xmlDoc* doc;
     276             :     
     277             :     const static std::string SDMDATASUBSETHEADER;
     278             :     const static std::string PROJECTPATH;
     279             :     const static std::string SCHEDULEPERIODTIME;
     280             :     const static std::string TIME;
     281             :     const static std::string INTERVAL;
     282             :     const static std::string DATASTRUCT;
     283             :     const static std::string REF;
     284             :     const static std::string DATAREF;
     285             :     const static std::string XLINKHREF;
     286             :     const static std::string FLAGSREF;
     287             :     const static std::string ACTUALTIMESREF;
     288             :     const static std::string ACTUALDURATIONSREF;
     289             :     const static std::string AUTODATAREF;
     290             :   };  // class TPSubsetHeaderParser
     291             :   
     292             :   
     293             :   class SDMDataObjectParser {
     294             :     friend class SDMDataObject;
     295             :     friend class SDMDataSubset;
     296             :     friend class HeaderParser;
     297             : 
     298             :   public:
     299             :     SDMDataObjectParser();
     300             :     virtual ~SDMDataObjectParser();
     301             :     void parseFileHeader(const std::string& filename, SDMDataObject& sdmDataObject);
     302             :     void parseMemoryHeader(const std::string& buffer, SDMDataObject& sdmDataObject);
     303             : 
     304             :     void parseFileCorrSubsetHeader(const std::string& filename, SDMDataSubset& sdmCorrSubset);
     305             :     void parseMemoryCorrSubsetHeader(const std::string& buffer, SDMDataSubset& sdmCorrSubset);
     306             : 
     307             :     void parseFileTPSubsetHeader(const std::string& filename, SDMDataSubset& sdmCorrDataSubset);
     308             :     void parseMemoryTPSubsetHeader(const std::string& filename, SDMDataSubset& sdmCorrDataSubset);
     309             :     
     310             :     static void isElement(xmlNode* a_node, const std::string& elementName);
     311             :     static bool testElement(xmlNode* a_node, const std::string& elementName);
     312             :     static void inElements(xmlNode* a_node, const std::vector<std::string>& elementNames);
     313             : 
     314             :     static xmlAttr* hasAttr(xmlNode* a_node, const std::string& attrName);
     315             : 
     316             :     static void tokenize(const std::string& str,
     317             :                          std::vector<std::string>& tokens,
     318             :                          const std::string& delimiters = " ");
     319             : 
     320             :     static void tokenize(const std::string& str,
     321             :                          std::set<std::string>& tokens,
     322             :                          const std::string& delimiters = " ");
     323             :     static std::string substring(const std::string &s, int a, int b);
     324             :     static std::string trim(const std::string &s);
     325             : 
     326             :     static std::string parseString(xmlNode* a_node);
     327             :     static long long parseLongLong(xmlNode* a_node);
     328             :     static int   parseInt(xmlNode* a_node);
     329             :     static bool  parseBool(xmlNode* a_node);
     330             :     static float parseFloat(xmlNode* a_node);
     331             :     static int   parseIntAttr(xmlNode* a_node, const std::string& attrName);
     332             :     static bool  parseBoolAttr(xmlNode* a_node, const std::string& attrName);
     333             :     static float parseFloatAttr(xmlNode* a_node, const std::string& attrName);
     334             :     static std::string parseStringAttr(xmlNode* a_node, const std::string& attrName);
     335             :     static const ByteOrder* parseByteOrderAttr(xmlNode* a_node, const std::string& attrName);
     336             : 
     337       46094 :     template<class Enum, class EnumHelper> static Enum parseStringAttr(xmlNode* a_node, const std::string& attrName) {
     338       46094 :       xmlAttr* attr = 0;
     339             :       
     340       46094 :       if ((attr = hasAttr(a_node, attrName))) {
     341       46094 :         std::string s = std::string((const char*)attr->children->content);
     342             :         try {
     343       46094 :           Enum result = EnumHelper::literal(SDMDataObjectParser::trim(s));
     344       46094 :           return result;
     345             :         }
     346           0 :         catch(std::string m) {
     347           0 :           throw  SDMDataObjectParserException(m);
     348             :         }
     349       46094 :       }
     350             :       else 
     351           0 :         throw SDMDataObjectParserException("could not find attribute '" + attrName + "' in " + std::string((const char*)a_node->name));    
     352             :     }
     353             :     
     354        8865 :     template<class Enum, class EnumHelper> static Enum parseLiteral(xmlNode* a_node) {
     355        8865 :       if ((a_node != NULL) && (a_node->next == NULL)) {      
     356             :         try {
     357        8865 :           Enum result = EnumHelper::literal(SDMDataObjectParser::trim(std::string((const char*) a_node->content)));
     358        8865 :           return result;
     359             :         }
     360           0 :         catch (std::string m) {
     361           0 :           throw SDMDataObjectParserException(m);
     362             :         }
     363             :       }
     364             :       else 
     365           0 :         throw SDMDataObjectParserException("The content of an element could not parsed into a literal");
     366             :     }
     367             : 
     368             : 
     369             :     static std::vector<std::string> parseStringsAttr(xmlNode* a_node, const std::string& attrName);
     370             :     static std::set<std::string> parseStringSetAttr(xmlNode* a_node, const std::string& attrName);
     371             : 
     372       23802 :     template<class Enum, class EnumHelper> static std::vector<Enum> parseStringsAttr(xmlNode* a_node, const std::string& attrName) {
     373       23802 :       xmlAttr* attr = 0;
     374             :       
     375       23802 :       if ((attr = hasAttr(a_node, attrName))) {
     376       23802 :         std::vector<std::string> v_s;
     377       23802 :         tokenize((const char*)attr->children->content, v_s);
     378             : 
     379       23802 :         std::vector<Enum> result; 
     380       23802 :         unsigned int i = 0;
     381             :         try {
     382       85452 :           for (i = 0; i < v_s.size(); i++)
     383       61650 :             result.push_back(EnumHelper::literal(v_s.at(i)));
     384       47604 :           return result;
     385             :         }
     386           0 :         catch (std::string m) {
     387           0 :           throw  SDMDataObjectParserException(m);
     388             :         }
     389       23802 :       }
     390             :       else 
     391           0 :         throw SDMDataObjectParserException("could not find attribute '" + attrName + "' in " + std::string((const char*)a_node->name));
     392             :     }
     393             : 
     394             :     static std::vector<unsigned int> parseProjectPath(xmlNode* a_node, unsigned int len);
     395             : 
     396             :     static std::vector<unsigned int> parseProjectPath(xmlNode* a_node);
     397             : 
     398             :     private:    
     399             : 
     400             :     static const std::regex PROJECTPATH3;
     401             :     static const std::regex PROJECTPATH4;
     402             :     static const std::regex PROJECTPATH5;
     403             :     static const std::regex PROJECTPATH4OR5;
     404             : 
     405             :     HeaderParser headerParser;
     406             :     CorrSubsetHeaderParser corrSubsetHeaderParser;
     407             :     TPSubsetHeaderParser tpSubsetHeaderParser;
     408             :     
     409             :   }; // class SDMDataObjectParser
     410             :   
     411             :   
     412             : } // namespace asdmbinaries
     413             : #endif // HeaderParser_CLASS

Generated by: LCOV version 1.16