LCOV - code coverage report
Current view: top level - alma/ASDM - Misc.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 180 310 58.1 %
Date: 2024-12-11 20:54:31 Functions: 23 48 47.9 %

          Line data    Source code
       1             : /*
       2             :  * ALMA - Atacama Large Millimeter Array
       3             :  * (c) European Southern Observatory, 2002
       4             :  * (c) Associated Universities Inc., 2002
       5             :  * Copyright by ESO (in the framework of the ALMA collaboration),
       6             :  * Copyright by AUI (in the framework of the ALMA collaboration),
       7             :  * All rights reserved.
       8             :  * 
       9             :  * This library is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  * 
      14             :  * This library is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY, without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  * 
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with this library; if not, write to the Free Software
      21             :  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      22             :  * MA 02111-1307  USA
      23             :  *
      24             :  *
      25             :  * File Misc.cpp
      26             :  */
      27             : 
      28             : #include <alma/ASDM/Misc.h>
      29             :  
      30             : #include <sys/stat.h>
      31             : #include <sys/types.h>
      32             : #include <dirent.h>
      33             : 
      34             : #include <algorithm> //required for std::swap
      35             : #include <iostream>
      36             : #include <sstream>
      37             : 
      38             : // string.h provides strcpy, strtok and strcat
      39             : #include <string.h>
      40             : 
      41             : #include <libxml/xmlmemory.h>
      42             : #include <libxml/debugXML.h>
      43             : #include <libxml/HTMLtree.h>
      44             : #include <libxml/xmlIO.h>
      45             : #include <libxml/parser.h>
      46             : #include <libxml/xinclude.h>
      47             : #include <libxml/catalog.h>
      48             : 
      49             : #include <libxslt/xslt.h>
      50             : #include <libxslt/xsltInternals.h>
      51             : #include <libxslt/transform.h>
      52             : #include <libxslt/xsltutils.h>
      53             : 
      54             : using namespace std;
      55             : 
      56             : extern int xmlLoadExtDtdDefaultValue;
      57             : 
      58             : #include <alma/ASDM/ASDMValuesParser.h>
      59             : 
      60             : namespace asdm {
      61         295 :   bool directoryExists(const char* dir) {
      62         295 :     DIR* dhandle = opendir(dir);
      63             : 
      64         295 :     if (dhandle != NULL) {
      65         295 :       closedir(dhandle);
      66         295 :       return true;
      67             :     }
      68             :     else {
      69           0 :       return false;
      70             :     }
      71             :   }
      72             : 
      73           0 :   bool createDirectory(const char* dir) { 
      74           0 :     return mkdir(dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0;
      75             :   }
      76             :         
      77           0 :   bool createPath(const char* path) {
      78             :     char localpath[256];
      79           0 :     strcpy(localpath, path);
      80             : 
      81             :     char directory[256];
      82           0 :     if (path[0] == '/') {
      83           0 :       strcpy(directory, "/");
      84             :     }
      85             :     else {
      86           0 :       strcpy(directory, "");
      87             :     }
      88             :         
      89           0 :     char* pch = strtok(localpath, "/");
      90           0 :     while (pch != NULL) {
      91           0 :       strcat(directory, pch);
      92           0 :       strcat(directory, "/");
      93           0 :       if (!directoryExists(directory) && !createDirectory(directory)) {
      94           0 :         return false;
      95             :       }
      96           0 :       pch = strtok(NULL, "/");
      97             :     }
      98           0 :     return true;
      99             :   }
     100             :     
     101           0 :   void ByteSwap(unsigned char * b, int n) {
     102           0 :     int i = 0;
     103           0 :     int j = n-1;
     104           0 :     while (i<j) {
     105           0 :       std::swap(b[i], b[j]);
     106           0 :       i++, j--;
     107             :     }
     108           0 :   }
     109             : 
     110             :   const ByteOrder* ByteOrder::Little_Endian = new ByteOrder("Little_Endian");
     111             :   const ByteOrder* ByteOrder::Big_Endian = new ByteOrder("Big_Endian");
     112             :   const ByteOrder* ByteOrder::Machine_Endianity = ByteOrder::machineEndianity();
     113             : 
     114           6 :   ByteOrder::ByteOrder(const string& name):
     115           6 :     name_(name) {;}
     116             : 
     117           0 :   ByteOrder::~ByteOrder() {;}
     118             : 
     119           3 :   const ByteOrder* ByteOrder::machineEndianity() {
     120             : #if defined(__APPLE__)
     121             :     if (__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
     122             : #else 
     123             :       if (__BYTE_ORDER == __LITTLE_ENDIAN)
     124             : #endif
     125           3 :         return Little_Endian;
     126             :       else
     127             :         return Big_Endian;
     128             :   }
     129             :   
     130          44 :   string ByteOrder::toString() const {
     131          44 :     return name_;
     132             :   }
     133             : 
     134          65 :   const ByteOrder* ByteOrder::fromString(const string &s) {
     135          65 :     if (s == "Little_Endian") return Little_Endian;
     136          54 :     if (s == "Big_Endian") return Big_Endian;
     137           0 :     return 0;
     138             :   }
     139             : 
     140        1723 :   string uniqSlashes(const string & s) {
     141        1723 :     string result;
     142             :     char c;
     143        1723 :     bool inslash = false;
     144        1723 :     size_t indexi=0;
     145             : 
     146       66305 :     while (indexi < s.size()) {
     147       64582 :       if ((c = s.at(indexi)) != '/') {
     148       62619 :         inslash = false;
     149       62619 :         result.push_back(c);
     150             :       }
     151             :       else
     152        1963 :         if (inslash == false) {
     153        1963 :           result.push_back(c);
     154        1963 :           inslash = true;
     155             :         }
     156       64582 :       indexi++;
     157             :     }
     158        1723 :     return result;
     159           0 :   }
     160             : 
     161           0 :   ASDMUtilsException::ASDMUtilsException():message("ASDMUtilsException:") {;}
     162             : 
     163           0 :   ASDMUtilsException::ASDMUtilsException(const string & message): message("ASDMUtilsException:" + message) {;}   
     164             : 
     165           0 :   const string& ASDMUtilsException::getMessage() const { return message; }
     166             : 
     167             : #ifndef WITHOUT_BOOST
     168             :   ASDMUtils::DotXMLFilter::DotXMLFilter(vector<string>& filenames) {this->filenames = &filenames;}
     169             : 
     170             :   void ASDMUtils::DotXMLFilter::operator()(boost::filesystem::directory_entry& p) {
     171             :     if (!extension(p).compare(".xml")) {
     172             :       filenames->push_back(p.path().string());
     173             :     }
     174             :   }
     175             : #endif
     176             :  
     177             :   set<string>                             ASDMUtils::evlaValidNames;
     178             :   set<string>                             ASDMUtils::almaValidNames;
     179             :   map<ASDMUtils::Origin, string>  ASDMUtils::filenameOfV2V3xslTransform;
     180             :   map<string, string>                     ASDMUtils::rootSubdir ;
     181             :   bool                                  ASDMUtils::initialized = ASDMUtils::initialize();
     182             : 
     183           3 :   bool ASDMUtils::initialize() {
     184           9 :     string evlaValidNames_a[] = {"EVLA"};
     185           3 :     evlaValidNames = set<string>(evlaValidNames_a, evlaValidNames_a + 1);
     186             : 
     187          15 :     string almaValidNames_a[] = {"ALMA", "AOS", "OSF", "IRAM_PDB"};
     188           3 :     almaValidNames = set<string>(almaValidNames_a, almaValidNames_a + 4);
     189             : 
     190           3 :     filenameOfV2V3xslTransform[ASDMUtils::ALMA]    = "sdm-v2v3-alma.xsl";
     191           3 :     filenameOfV2V3xslTransform[ASDMUtils::EVLA]    = "sdm-v2v3-evla.xsl";
     192           3 :     filenameOfV2V3xslTransform[ASDMUtils::UNKNOWN] = "";
     193             : 
     194           3 :     rootSubdir["INTROOT"]  = "config/";
     195           3 :     rootSubdir["ACSROOT"]  = "config/";
     196             : 
     197           6 :     return true;
     198          21 :   }
     199             : 
     200          89 :   string ASDMUtils::version(const string& asdmPath) {
     201             : 
     202          89 :     string result;
     203             : 
     204             : #ifndef WITHOUT_BOOST
     205             :     string ASDMPath = boost::algorithm::trim_copy(asdmPath);
     206             :     if (!boost::algorithm::ends_with(ASDMPath, "/")) ASDMPath+="/";
     207             : #else
     208          89 :     string ASDMPath = trim_copy(asdmPath);
     209          89 :     if (ASDMPath.back()!='/') ASDMPath+="/";
     210             : #endif
     211          89 :     ASDMPath += "ASDM.xml";
     212             : 
     213             :     // Does ASDMPath exist ?
     214             : #ifndef WITHOUT_BOOST
     215             :     if (!boost::filesystem::exists(boost::filesystem::path(ASDMPath))) {
     216             : #else
     217          89 :     if (!file_exists(ASDMPath)) {
     218             : #endif
     219           0 :       throw ASDMUtilsException("File not found '"+ASDMPath+"'.");
     220             :     }
     221             : 
     222             :     // Read and parse ASDM.xml
     223          89 :     xmlDocPtr ASDMDoc = xmlParseFile(ASDMPath.c_str());
     224             :   
     225          89 :     if (ASDMDoc == NULL ) {
     226           0 :       throw ASDMUtilsException("Error while parsing '"+ASDMPath+"'.");
     227             :     }
     228             :   
     229             :     /*
     230             :      * Can we find an attribute schemaVersion in the top level element ?
     231             :      */
     232          89 :     xmlNodePtr cur = xmlDocGetRootElement(ASDMDoc);
     233          89 :     xmlChar* version_ = xmlGetProp(cur, (const xmlChar *) "schemaVersion");
     234             : 
     235             :     // Yes ? then return its value.
     236          89 :     if (version_ != NULL) {
     237          60 :       result = string((char *) version_);
     238          60 :       xmlFree(version_);
     239          60 :       xmlFreeDoc(ASDMDoc);
     240          60 :       return result;
     241             :     }
     242             : 
     243             :     // Let's do some housecleaning
     244          29 :     xmlFreeDoc(ASDMDoc);
     245             : 
     246             :     // No ? then try another approach ... Can we find a dataUID element in the row elements of the Main table and in such a case
     247             :     // make the assumption that it's a v3 ASDM.
     248             : #ifndef WITHOUT_BOOST
     249             :     string MainPath = boost::algorithm::trim_copy(asdmPath);
     250             :     if (!boost::algorithm::ends_with(MainPath, "/")) MainPath+="/";
     251             : #else
     252          29 :     string MainPath = trim_copy(asdmPath);
     253          29 :     if (MainPath.back()!='/') MainPath+="/";
     254             : #endif
     255          29 :     MainPath += "Main.xml";    
     256             : 
     257          29 :     result = "UNKNOWN";
     258             :     // Does MainPath exist ?
     259             : #ifndef WITHOUT_BOOST
     260             :     if (boost::filesystem::exists(boost::filesystem::path(MainPath))) {
     261             : #else
     262          29 :     if (file_exists(MainPath)) {
     263             : #endif
     264          29 :       xmlDocPtr MainDoc =  xmlParseFile(MainPath.c_str());
     265             :   
     266          29 :       if (MainDoc == NULL ) {
     267           0 :         throw ASDMUtilsException("Error while parsing '"+MainPath+"'.");
     268             :       }
     269             : 
     270             :       // Here we make the reasonable assumption that there will be
     271             :       // row elements (at least one).
     272             :       //
     273             :       // Send an alarm though if no row element is found.
     274          29 :       xmlNodePtr cur = xmlDocGetRootElement(MainDoc)->xmlChildrenNode;
     275          29 :       int nRow = 0;
     276         164 :       while (cur != NULL) {
     277         164 :         if (!xmlStrcmp(cur->name, (const xmlChar*) "row")) {
     278          29 :           nRow++;
     279          29 :           if (hasChild (MainDoc, cur, (const xmlChar *) "dataUID")) {
     280          11 :             result = "3";
     281          11 :             break;
     282             :           }
     283             : 
     284          18 :           if (hasChild (MainDoc, cur, (const xmlChar *) "dataOid")) {
     285          18 :             result = "2";
     286          18 :             break;
     287             :           }
     288             :         }
     289         135 :         cur = cur -> next;
     290             :       }
     291             : 
     292          29 :       xmlFreeDoc(MainDoc);      
     293          29 :       if (nRow == 0) {
     294           0 :         throw ASDMUtilsException("No 'row' elements present in '"+MainPath+"'.");
     295             :       }
     296             :     }
     297             :     
     298          29 :     return result;
     299          89 :   }
     300             : 
     301          47 :   bool ASDMUtils::hasChild(xmlDocPtr, // doc
     302             :                            xmlNodePtr node, const xmlChar* childName) {
     303          47 :     node = node->xmlChildrenNode;
     304             : 
     305        1052 :     while (node != NULL) {
     306        1034 :       if (!xmlStrcmp(node->name , childName))
     307          29 :         break; 
     308        1005 :       node = node->next;
     309             :     }
     310          47 :     return (node != NULL);      
     311             :   }
     312             : 
     313         159 :   string ASDMUtils::parseRow(xmlDocPtr doc, xmlNodePtr node, const xmlChar* childName) {
     314             :     //
     315             :     // Consider the children of node (i.e. expectedly of "<row>...</row>")
     316         159 :     node = node->xmlChildrenNode;
     317             : 
     318             :     //
     319             :     // And look for childName.
     320        2544 :     while (node != NULL) {
     321        2544 :       if (!xmlStrcmp(node->name , childName)) {
     322         159 :         xmlChar* content = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
     323         159 :         string result((char *) content);
     324         159 :         xmlFree(content);
     325         318 :         return result;
     326         159 :       }
     327        2385 :       node = node->next;
     328             :     }
     329             :     // If we are here , it's because we have not found childName as an element in node.
     330           0 :     throw ASDMUtilsException("Element '"+string((char *)childName)+"' not found.");
     331             :   }
     332             :  
     333          89 :   vector<string> ASDMUtils::telescopeNames(const string& asdmPath) {
     334          89 :     vector<string> result;
     335             : 
     336             : #ifndef WITHOUT_BOOST
     337             :     string execBlockPath = boost::algorithm::trim_copy(asdmPath);
     338             :     if (!boost::algorithm::ends_with(execBlockPath, "/")) execBlockPath+="/";
     339             : #else
     340          89 :     string execBlockPath = trim_copy(asdmPath);
     341          89 :     if (execBlockPath.back()!='/') execBlockPath+="/";
     342             : #endif
     343          89 :     execBlockPath += "ExecBlock.xml";
     344             : 
     345             :     // Does execBlockPath exist ?
     346             : #ifndef WITHOUT_BOOST
     347             :     if (!boost::filesystem::exists(boost::filesystem::path(execBlockPath))) {
     348             : #else
     349          89 :     if (!file_exists(execBlockPath)) {
     350             : #endif
     351           0 :       throw ASDMUtilsException("File not found '"+execBlockPath+"'.");
     352             :     }
     353             : 
     354             :     // Read and parse ExecBlock.xml
     355             :     xmlDocPtr execBlockDoc;
     356             :     xmlNodePtr cur;
     357             : 
     358          89 :     execBlockDoc = xmlParseFile(execBlockPath.c_str());
     359          89 :     if (execBlockDoc == NULL) {
     360           0 :       throw ASDMUtilsException("Error while parsing '"+execBlockPath+"'.");
     361             :     }
     362             : 
     363          89 :     cur = xmlDocGetRootElement(execBlockDoc)->xmlChildrenNode;
     364         838 :     while (cur != NULL) {
     365         749 :       if (!xmlStrcmp(cur->name, (const xmlChar*) "row")) {
     366             : #ifndef WITHOUT_BOOST
     367             :         result.push_back(boost::algorithm::trim_copy(parseRow(execBlockDoc, cur, (const xmlChar *) "telescopeName")));
     368             : #else
     369         159 :         result.push_back(trim_copy(parseRow(execBlockDoc, cur, (const xmlChar *) "telescopeName")));
     370             : #endif
     371             :       }
     372         749 :       cur = cur -> next;
     373             :     }
     374          89 :     xmlFreeDoc(execBlockDoc);
     375             : 
     376         178 :     return result;
     377          89 :   }
     378             : 
     379          89 :   ASDMUtils::Origin ASDMUtils::origin(const vector<string>& telescopeNames) {
     380          89 :     unsigned int numEVLA = 0;
     381          89 :     unsigned int numALMA = 0;
     382         248 :     for ( vector<string>::const_iterator iter = telescopeNames.begin(); iter != telescopeNames.end(); iter++ ) {
     383         159 :       if (evlaValidNames.find(*iter) != evlaValidNames.end())
     384          24 :         numEVLA++;
     385         135 :       else if (almaValidNames.find(*iter) != almaValidNames.end())
     386         134 :         numALMA++;
     387             :     }
     388             : 
     389          89 :     Origin autoOrigin = UNKNOWN;
     390          89 :     if (numEVLA == telescopeNames.size())
     391          24 :       autoOrigin = EVLA;
     392          65 :     else if (numALMA == telescopeNames.size())
     393          64 :       autoOrigin = ALMA;
     394             :     
     395          89 :     return autoOrigin;
     396             :   }
     397             : 
     398           0 :   vector<string> ASDMUtils::xmlFilenames ( const string & asdmPath  ) {
     399           0 :     vector<string> result;
     400             : 
     401             : #ifndef WITHOUT_BOOST
     402             :     boost::filesystem::path p(asdmPath);
     403             :     DotXMLFilter dotXMLFilter(result);
     404             :     std::for_each(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), dotXMLFilter);
     405             : #else
     406             :     DIR *dir;
     407           0 :     if ((dir = opendir(asdmPath.c_str())) != NULL) {
     408             :       struct dirent *ent;
     409           0 :       string dirSep="/";
     410           0 :       if (asdmPath.back()=='/') dirSep="";
     411             : 
     412           0 :       while ((ent = readdir(dir)) != NULL) {
     413           0 :         string thisFile = ent->d_name;
     414           0 :         if (thisFile.size() > 4) {
     415           0 :           if (thisFile.compare((thisFile.size()-4),4,".xml")==0) {
     416           0 :             result.push_back(asdmPath+dirSep+thisFile);
     417             :           }
     418             :         }
     419           0 :       }
     420           0 :       closedir(dir);
     421           0 :     } else {
     422           0 :       throw ASDMUtilsException("Could not open ASDM directory to retrieve xml files: "+asdmPath);
     423             :     }
     424             : #endif
     425           0 :     return result;
     426           0 :   }
     427             : 
     428          10 :   string ASDMUtils::pathToV2V3ALMAxslTransform() {return pathToxslTransform(filenameOfV2V3xslTransform[ASDMUtils::ALMA]);}
     429          26 :   string ASDMUtils::pathToV2V3EVLAxslTransform() {return pathToxslTransform(filenameOfV2V3xslTransform[ASDMUtils::EVLA]);}
     430           0 :   string ASDMUtils::nameOfV2V3xslTransform(ASDMUtils::Origin origin) {
     431           0 :     return filenameOfV2V3xslTransform[origin];
     432             :   }
     433             : 
     434          89 :   ASDMParseOptions::ASDMParseOptions() {
     435          89 :     origin_             = ASDMUtils::UNKNOWN;
     436          89 :     detectOrigin_       = true;
     437          89 :     version_            = "UNKNOWN";
     438          89 :     detectVersion_      = true;
     439          89 :     loadTablesOnDemand_ = false;
     440          89 :     checkRowUniqueness_ = true;
     441          89 :   }
     442             : 
     443          78 :   ASDMParseOptions::ASDMParseOptions(const ASDMParseOptions& x) {
     444          78 :     origin_                                    = x.origin_;
     445          78 :     detectOrigin_                              = x.detectOrigin_;
     446          78 :     version_                                   = x.version_;
     447          78 :     detectVersion_                             = x.detectVersion_;
     448          78 :     loadTablesOnDemand_                        = x.loadTablesOnDemand_;
     449          78 :     checkRowUniqueness_                        = x.checkRowUniqueness_;
     450          78 :   }
     451             : 
     452         167 :   ASDMParseOptions::~ASDMParseOptions() {;}
     453             : 
     454           0 :   ASDMParseOptions& ASDMParseOptions::operator = (const ASDMParseOptions& rhs) {
     455           0 :     origin_                                    = rhs.origin_;
     456           0 :     detectOrigin_                              = rhs.detectOrigin_;
     457           0 :     version_                                   = rhs.version_;
     458           0 :     detectVersion_                             = rhs.detectVersion_;
     459           0 :     loadTablesOnDemand_                        = rhs.loadTablesOnDemand_;
     460           0 :     checkRowUniqueness_                        = rhs.checkRowUniqueness_;
     461           0 :     return *this;
     462             :   }
     463           0 :   ASDMParseOptions& ASDMParseOptions::asALMA() { origin_ = ASDMUtils::ALMA; detectOrigin_ = false; return *this; }
     464           0 :   ASDMParseOptions& ASDMParseOptions::asIRAM_PDB() { origin_ = ASDMUtils::ALMA; detectOrigin_ = false; return *this; }
     465           0 :   ASDMParseOptions& ASDMParseOptions::asEVLA() { origin_ = ASDMUtils::EVLA; detectOrigin_ = false; return *this; }
     466           0 :   ASDMParseOptions& ASDMParseOptions::asV2() { version_ = "2"; detectVersion_ = false; return *this; }
     467           0 :   ASDMParseOptions& ASDMParseOptions::asV3() { version_ = "3"; detectVersion_ = false; return *this; }
     468          89 :   ASDMParseOptions& ASDMParseOptions::loadTablesOnDemand(bool b) { loadTablesOnDemand_ = b;  return *this; }
     469          82 :   ASDMParseOptions& ASDMParseOptions::checkRowUniqueness(bool b) { checkRowUniqueness_ = b;  return *this; }
     470           0 :   string ASDMParseOptions::toString() const {
     471           0 :     ostringstream oss;
     472           0 :     oss << *this;
     473           0 :     return oss.str();
     474           0 :   }
     475             : 
     476         118 :   XSLTransformer::XSLTransformer() : cur(NULL) {
     477         118 :     xmlSubstituteEntitiesDefault(1);
     478         118 :     xmlLoadExtDtdDefaultValue = 1;
     479         118 :   }
     480             : 
     481           0 :   XSLTransformer::XSLTransformer(const string& xsltPath) {
     482           0 :     if (getenv("ASDM_DEBUG")) {
     483           0 :         cout << "XSLTransformer::XSLTransformer(const string& xsltPath) called " << endl;
     484           0 :         cout << "About parse the style sheet contained in " << xsltPath << endl;
     485             :     }
     486             : 
     487           0 :     xmlSubstituteEntitiesDefault(1);
     488           0 :     xmlLoadExtDtdDefaultValue = 1;
     489             :     
     490           0 :     cur = xsltParseStylesheetFile((const xmlChar*) xsltPath.c_str());
     491           0 :     if (cur == NULL)
     492           0 :       throw XSLTransformerException("Failed to  parse the XSL stylecheet contained in '" + xsltPath + "'.");
     493           0 :   }
     494             : 
     495          18 :   void XSLTransformer::setTransformation(const string& xsltPath) {
     496          18 :     if (getenv("ASDM_DEBUG")) 
     497           0 :       cout << "XSLTransformer::setTransformation(const string& xsltPath) called on '" << xsltPath << "'." << endl;
     498             :     
     499          18 :     if (cur) {
     500           0 :       xsltFreeStylesheet(cur);
     501           0 :       cur = NULL;
     502             :     }
     503             : 
     504             :     // cout << "About parse the style sheet contained in " << xsltPath << endl;
     505             :       
     506          18 :     cur = xsltParseStylesheetFile((const xmlChar*) xsltPath.c_str());
     507          18 :     if (cur == NULL)
     508           0 :       throw XSLTransformerException("Failed to parse the XSL stylecheet contained in '" + xsltPath + "'." );
     509          18 :   }
     510             : 
     511             : 
     512         118 :   XSLTransformer::~XSLTransformer() {
     513             :     // cout << "XSLTransformer::~XSLTransformer() called" << endl;
     514         118 :     if (cur) {
     515          18 :       xsltFreeStylesheet(cur);
     516          18 :       cur = NULL;
     517             :     }
     518         118 :   } 
     519             : 
     520        1789 :   string XSLTransformer::operator()(const string& xmlPath){
     521        1789 :     xmlDocPtr doc = NULL, res = NULL;
     522             :     //xsltStylesheetPtr cur;
     523             : 
     524        1789 :     xmlChar* docTxtPtr = NULL;
     525        1789 :     int docTxtLen = 0;
     526             :     
     527        1789 :     if (getenv("ASDM_DEBUG")) cout << "About to read and parse " << xmlPath << endl;
     528        1789 :     doc = xmlParseFile(xmlPath.c_str());
     529        1789 :     if (doc == NULL) {
     530           0 :       throw XSLTransformerException("Could not parse the XML file '" + xmlPath + "'." );
     531             :     }
     532             : 
     533        1789 :     if (!cur) {
     534        1439 :       xmlDocDumpFormatMemory(doc, &docTxtPtr, &docTxtLen, 1);
     535             :     }
     536             :     else {
     537         350 :       res = xsltApplyStylesheet(cur, doc, NULL);
     538         350 :       if ( res == NULL ) {
     539           0 :         throw XSLTransformerException("Failed to apply the XSLT tranformation to the XML document contained in '" + xmlPath + "'.");
     540             :       }
     541         350 :       int status = xsltSaveResultToString(&docTxtPtr,
     542             :                                           &docTxtLen,
     543             :                                           res,
     544             :                                           cur);
     545         350 :       if (status == -1) 
     546           0 :         throw XSLTransformerException("Could not dump the result of the XSL transformation into memory.");
     547             :     }
     548             : 
     549        1789 :     if (getenv("ASDM_DEBUG")) 
     550           0 :       cout << "Making a string from the result of the XSL transformation" << endl;
     551        1789 :     string docXML((char *) docTxtPtr, docTxtLen);
     552             :     // cout << "docXML = " << docXML << endl;
     553             : 
     554        1789 :     xmlFree(docTxtPtr);
     555        1789 :     if (res) xmlFreeDoc(res);
     556        1789 :     xmlFreeDoc(doc);
     557             :     
     558             :     // cout << "All resources unneeded freed" << endl;
     559        3578 :     return docXML;
     560           0 :   } 
     561             : 
     562           0 :   std::ostream& operator<<(std::ostream& output, const ASDMParseOptions& p) {
     563           0 :     string s;
     564           0 :     switch (p.origin_) {
     565           0 :     case ASDMUtils::UNKNOWN:
     566           0 :       s = "UNKNOWN";
     567           0 :       break;
     568           0 :     case ASDMUtils::ALMA:
     569           0 :       s = "ALMA";
     570           0 :       break;
     571           0 :     case ASDMUtils::EVLA:
     572           0 :       s = "EVLA";
     573           0 :       break;
     574             :     }
     575           0 :     output << "Origin=" << s << ",Version=" << p.version_ << ",LoadTablesOnDemand=" << p.loadTablesOnDemand_ << ",CheckRowUniqueness=" << p.checkRowUniqueness_;
     576           0 :     return output;  // for multiple << operators.
     577           0 :   }
     578             : 
     579           0 :   CharComparator::CharComparator(std::ifstream * is_p, off_t limit):is_p(is_p), limit(limit){asdmDebug_p = getenv("ASDM_DEBUG");}
     580             : 
     581           0 :   bool CharComparator::operator() (char cl, char cr) {
     582           0 :     if (asdmDebug_p) cout << "Entering CharComparator::operator()" << endl;
     583           0 :     if (is_p && is_p->tellg() > limit) 
     584           0 :       return true;
     585             :     else 
     586           0 :       return toupper(cl) == cr;
     587             :     if (asdmDebug_p) cout << "Exiting CharComparator::operator()" << endl;
     588             :   }
     589             : 
     590           0 :   CharCompAccumulator::CharCompAccumulator(std::string* accumulator_p, std::ifstream * is_p, off_t limit): accumulator_p(accumulator_p),
     591           0 :                                                                                                            is_p(is_p),
     592           0 :                                                                                                            limit(limit) {nEqualChars = 0; asdmDebug_p = getenv("ASDM_DEBUG");}
     593           0 :   bool CharCompAccumulator::operator()(char cl, char cr) {
     594           0 :     if (asdmDebug_p) cout << "Entering CharCompAccumulator::operator()" << endl;
     595           0 :     bool result = false;
     596             :     // Are we beyond the limit ?
     597           0 :     if (is_p && is_p->tellg() > limit) 
     598           0 :       result = true;      // Yes
     599             :     else {                // No
     600           0 :       if (toupper(cl) == toupper(cr)) {
     601           0 :         result = true;
     602           0 :         nEqualChars++;
     603             :       }
     604             :       else {
     605           0 :         if (nEqualChars > 0) {
     606           0 :           accumulator_p->erase(accumulator_p->end() - nEqualChars + 1, accumulator_p->end());
     607           0 :           nEqualChars = 0;
     608             :         }
     609           0 :         result = false;
     610             :       }
     611           0 :       accumulator_p->push_back(cl);
     612             :     }
     613           0 :     if (asdmDebug_p) cout << "Exiting CharCompAccumulator::operator()" << endl;
     614           0 :     return result;
     615             :   }  
     616             :   
     617             :   istringstream ASDMValuesParser::iss;
     618             :   ostringstream ASDMValuesParser::oss;
     619             : 
     620             : } // end namespace asdm
     621             :  
     622             :  

Generated by: LCOV version 1.16