LCOV - code coverage report
Current view: top level - alma/ASDM - Misc.cc (source / functions) Hit Total Coverage
Test: ctest_coverage.info Lines: 176 298 59.1 %
Date: 2023-11-06 10:06:49 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           2 :   ByteOrder::ByteOrder(const string& name):
     115           2 :     name_(name) {;}
     116             : 
     117           0 :   ByteOrder::~ByteOrder() {;}
     118             : 
     119           1 :   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           1 :         return Little_Endian;
     126             :       else
     127             :         return Big_Endian;
     128             :   }
     129             :   
     130          44 :   string ByteOrder::toString() const {
     131          44 :     return name_;
     132             :   }
     133             : 
     134          56 :   const ByteOrder* ByteOrder::fromString(const string &s) {
     135          56 :     if (s == "Little_Endian") return Little_Endian;
     136          51 :     if (s == "Big_Endian") return Big_Endian;
     137           0 :     return 0;
     138             :   }
     139             : 
     140        1585 :   string uniqSlashes(const string & s) {
     141        1585 :     string result;
     142             :     char c;
     143        1585 :     bool inslash = false;
     144        1585 :     size_t indexi=0;
     145             : 
     146       61291 :     while (indexi < s.size()) {
     147       59706 :       if ((c = s.at(indexi)) != '/') {
     148       57841 :         inslash = false;
     149       57841 :         result.push_back(c);
     150             :       }
     151             :       else
     152        1865 :         if (inslash == false) {
     153        1865 :           result.push_back(c);
     154        1865 :           inslash = true;
     155             :         }
     156       59706 :       indexi++;
     157             :     }
     158        1585 :     return result;
     159             :   }
     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           1 :   bool ASDMUtils::initialize() {
     184           3 :     string evlaValidNames_a[] = {"EVLA"};
     185           1 :     evlaValidNames = set<string>(evlaValidNames_a, evlaValidNames_a + 1);
     186             : 
     187           6 :     string almaValidNames_a[] = {"ALMA", "AOS", "OSF", "IRAM_PDB"};
     188           1 :     almaValidNames = set<string>(almaValidNames_a, almaValidNames_a + 4);
     189             : 
     190           1 :     filenameOfV2V3xslTransform[ASDMUtils::ALMA]    = "sdm-v2v3-alma.xsl";
     191           1 :     filenameOfV2V3xslTransform[ASDMUtils::EVLA]    = "sdm-v2v3-evla.xsl";
     192           1 :     filenameOfV2V3xslTransform[ASDMUtils::UNKNOWN] = "";
     193             : 
     194           1 :     rootSubdir["INTROOT"]  = "config/";
     195           1 :     rootSubdir["ACSROOT"]  = "config/";
     196             : 
     197           2 :     return true;
     198             :   }
     199             : 
     200          83 :   string ASDMUtils::version(const string& asdmPath) {
     201             : 
     202          83 :     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         166 :     string ASDMPath = trim_copy(asdmPath);
     209          83 :     if (ASDMPath.back()!='/') ASDMPath+="/";
     210             : #endif
     211          83 :     ASDMPath += "ASDM.xml";
     212             : 
     213             :     // Does ASDMPath exist ?
     214             : #ifndef WITHOUT_BOOST
     215             :     if (!boost::filesystem::exists(boost::filesystem::path(ASDMPath))) {
     216             : #else
     217          83 :     if (!file_exists(ASDMPath)) {
     218             : #endif
     219           0 :       throw ASDMUtilsException("File not found '"+ASDMPath+"'.");
     220             :     }
     221             : 
     222             :     // Read and parse ASDM.xml
     223          83 :     xmlDocPtr ASDMDoc = xmlParseFile(ASDMPath.c_str());
     224             :   
     225          83 :     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          83 :     xmlNodePtr cur = xmlDocGetRootElement(ASDMDoc);
     233          83 :     xmlChar* version_ = xmlGetProp(cur, (const xmlChar *) "schemaVersion");
     234             : 
     235             :     // Yes ? then return its value.
     236          83 :     if (version_ != NULL) {
     237          56 :       result = string((char *) version_);
     238          56 :       xmlFree(version_);
     239          56 :       xmlFreeDoc(ASDMDoc);
     240          56 :       return result;
     241             :     }
     242             : 
     243             :     // Let's do some housecleaning
     244          27 :     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          54 :     string MainPath = trim_copy(asdmPath);
     253          27 :     if (MainPath.back()!='/') MainPath+="/";
     254             : #endif
     255          27 :     MainPath += "Main.xml";    
     256             : 
     257          27 :     result = "UNKNOWN";
     258             :     // Does MainPath exist ?
     259             : #ifndef WITHOUT_BOOST
     260             :     if (boost::filesystem::exists(boost::filesystem::path(MainPath))) {
     261             : #else
     262          27 :     if (file_exists(MainPath)) {
     263             : #endif
     264          27 :       xmlDocPtr MainDoc =  xmlParseFile(MainPath.c_str());
     265             :   
     266          27 :       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          27 :       xmlNodePtr cur = xmlDocGetRootElement(MainDoc)->xmlChildrenNode;
     275          27 :       int nRow = 0;
     276         152 :       while (cur != NULL) {
     277         152 :         if (!xmlStrcmp(cur->name, (const xmlChar*) "row")) {
     278          27 :           nRow++;
     279          27 :           if (hasChild (MainDoc, cur, (const xmlChar *) "dataUID")) {
     280           9 :             result = "3";
     281           9 :             break;
     282             :           }
     283             : 
     284          18 :           if (hasChild (MainDoc, cur, (const xmlChar *) "dataOid")) {
     285          18 :             result = "2";
     286          18 :             break;
     287             :           }
     288             :         }
     289         125 :         cur = cur -> next;
     290             :       }
     291             : 
     292          27 :       xmlFreeDoc(MainDoc);      
     293          27 :       if (nRow == 0) {
     294           0 :         throw ASDMUtilsException("No 'row' elements present in '"+MainPath+"'.");
     295             :       }
     296             :     }
     297             :     
     298          27 :     return result;
     299             :   }
     300             : 
     301          45 :   bool ASDMUtils::hasChild(xmlDocPtr, // doc
     302             :                            xmlNodePtr node, const xmlChar* childName) {
     303          45 :     node = node->xmlChildrenNode;
     304             : 
     305        1016 :     while (node != NULL) {
     306         998 :       if (!xmlStrcmp(node->name , childName))
     307          27 :         break; 
     308         971 :       node = node->next;
     309             :     }
     310          45 :     return (node != NULL);      
     311             :   }
     312             : 
     313         153 :   string ASDMUtils::parseRow(xmlDocPtr doc, xmlNodePtr node, const xmlChar* childName) {
     314             :     //
     315             :     // Consider the children of node (i.e. expectedly of "<row>...</row>")
     316         153 :     node = node->xmlChildrenNode;
     317             : 
     318             :     //
     319             :     // And look for childName.
     320        2448 :     while (node != NULL) {
     321        2448 :       if (!xmlStrcmp(node->name , childName)) {
     322         153 :         xmlChar* content = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
     323         306 :         string result((char *) content);
     324         153 :         xmlFree(content);
     325         306 :         return result;
     326             :       }
     327        2295 :       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          83 :   vector<string> ASDMUtils::telescopeNames(const string& asdmPath) {
     334          83 :     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         166 :     string execBlockPath = trim_copy(asdmPath);
     341          83 :     if (execBlockPath.back()!='/') execBlockPath+="/";
     342             : #endif
     343          83 :     execBlockPath += "ExecBlock.xml";
     344             : 
     345             :     // Does execBlockPath exist ?
     346             : #ifndef WITHOUT_BOOST
     347             :     if (!boost::filesystem::exists(boost::filesystem::path(execBlockPath))) {
     348             : #else
     349          83 :     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          83 :     execBlockDoc = xmlParseFile(execBlockPath.c_str());
     359          83 :     if (execBlockDoc == NULL) {
     360           0 :       throw ASDMUtilsException("Error while parsing '"+execBlockPath+"'.");
     361             :     }
     362             : 
     363          83 :     cur = xmlDocGetRootElement(execBlockDoc)->xmlChildrenNode;
     364         791 :     while (cur != NULL) {
     365         708 :       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         153 :         result.push_back(trim_copy(parseRow(execBlockDoc, cur, (const xmlChar *) "telescopeName")));
     370             : #endif
     371             :       }
     372         708 :       cur = cur -> next;
     373             :     }
     374          83 :     xmlFreeDoc(execBlockDoc);
     375             : 
     376         166 :     return result;
     377             :   }
     378             : 
     379          83 :   ASDMUtils::Origin ASDMUtils::origin(const vector<string>& telescopeNames) {
     380          83 :     unsigned int numEVLA = 0;
     381          83 :     unsigned int numALMA = 0;
     382         236 :     for ( vector<string>::const_iterator iter = telescopeNames.begin(); iter != telescopeNames.end(); iter++ ) {
     383         153 :       if (evlaValidNames.find(*iter) != evlaValidNames.end())
     384          22 :         numEVLA++;
     385         131 :       else if (almaValidNames.find(*iter) != almaValidNames.end())
     386         130 :         numALMA++;
     387             :     }
     388             : 
     389          83 :     Origin autoOrigin = UNKNOWN;
     390          83 :     if (numEVLA == telescopeNames.size())
     391          22 :       autoOrigin = EVLA;
     392          61 :     else if (numALMA == telescopeNames.size())
     393          60 :       autoOrigin = ALMA;
     394             :     
     395          83 :     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             :       }
     420           0 :       closedir(dir);
     421             :     } else {
     422           0 :       throw ASDMUtilsException("Could not open ASDM directory to retrieve xml files: "+asdmPath);
     423             :     }
     424             : #endif
     425           0 :     return result;
     426             :   }
     427             : 
     428           5 :   string ASDMUtils::pathToV2V3ALMAxslTransform() {return pathToxslTransform(filenameOfV2V3xslTransform[ASDMUtils::ALMA]);}
     429          13 :   string ASDMUtils::pathToV2V3EVLAxslTransform() {return pathToxslTransform(filenameOfV2V3xslTransform[ASDMUtils::EVLA]);}
     430           0 :   string ASDMUtils::nameOfV2V3xslTransform(ASDMUtils::Origin origin) {
     431           0 :     return filenameOfV2V3xslTransform[origin];
     432             :   }
     433             : 
     434          83 :   ASDMParseOptions::ASDMParseOptions() {
     435          83 :     origin_             = ASDMUtils::UNKNOWN;
     436          83 :     detectOrigin_       = true;
     437          83 :     version_            = "UNKNOWN";
     438          83 :     detectVersion_      = true;
     439          83 :     loadTablesOnDemand_ = false;
     440          83 :     checkRowUniqueness_ = true;
     441          83 :   }
     442             : 
     443          72 :   ASDMParseOptions::ASDMParseOptions(const ASDMParseOptions& x) {
     444          72 :     origin_                                    = x.origin_;
     445          72 :     detectOrigin_                              = x.detectOrigin_;
     446          72 :     version_                                   = x.version_;
     447          72 :     detectVersion_                             = x.detectVersion_;
     448          72 :     loadTablesOnDemand_                        = x.loadTablesOnDemand_;
     449          72 :     checkRowUniqueness_                        = x.checkRowUniqueness_;
     450          72 :   }
     451             : 
     452         155 :   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          83 :   ASDMParseOptions& ASDMParseOptions::loadTablesOnDemand(bool b) { loadTablesOnDemand_ = b;  return *this; }
     469          76 :   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             :   }
     475             : 
     476         110 :   XSLTransformer::XSLTransformer() : cur(NULL) {
     477         110 :     xmlSubstituteEntitiesDefault(1);
     478         110 :     xmlLoadExtDtdDefaultValue = 1;
     479         110 :   }
     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         220 :   XSLTransformer::~XSLTransformer() {
     513             :     // cout << "XSLTransformer::~XSLTransformer() called" << endl;
     514         110 :     if (cur) {
     515          18 :       xsltFreeStylesheet(cur);
     516          18 :       cur = NULL;
     517             :     }
     518         110 :   } 
     519             : 
     520        1647 :   string XSLTransformer::operator()(const string& xmlPath){
     521        1647 :     xmlDocPtr doc = NULL, res = NULL;
     522             :     //xsltStylesheetPtr cur;
     523             : 
     524        1647 :     xmlChar* docTxtPtr = NULL;
     525        1647 :     int docTxtLen = 0;
     526             :     
     527        1647 :     if (getenv("ASDM_DEBUG")) cout << "About to read and parse " << xmlPath << endl;
     528        1647 :     doc = xmlParseFile(xmlPath.c_str());
     529        1647 :     if (doc == NULL) {
     530           0 :       throw XSLTransformerException("Could not parse the XML file '" + xmlPath + "'." );
     531             :     }
     532             : 
     533        1647 :     if (!cur) {
     534        1297 :       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        1647 :     if (getenv("ASDM_DEBUG")) 
     550           0 :       cout << "Making a string from the result of the XSL transformation" << endl;
     551        1647 :     string docXML((char *) docTxtPtr, docTxtLen);
     552             :     // cout << "docXML = " << docXML << endl;
     553             : 
     554        1647 :     xmlFree(docTxtPtr);
     555        1647 :     if (res) xmlFreeDoc(res);
     556        1647 :     xmlFreeDoc(doc);
     557             :     
     558             :     // cout << "All resources unneeded freed" << endl;
     559        3294 :     return docXML;
     560             :   } 
     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             :   }
     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             :                                                                                                            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