LCOV - code coverage report
Current view: top level - alma/ASDM - Misc.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 16 310 5.2 %
Date: 2024-10-04 18:58:15 Functions: 3 48 6.2 %

          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           0 :   bool directoryExists(const char* dir) {
      62           0 :     DIR* dhandle = opendir(dir);
      63             : 
      64           0 :     if (dhandle != NULL) {
      65           0 :       closedir(dhandle);
      66           0 :       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           0 :   string ByteOrder::toString() const {
     131           0 :     return name_;
     132             :   }
     133             : 
     134           0 :   const ByteOrder* ByteOrder::fromString(const string &s) {
     135           0 :     if (s == "Little_Endian") return Little_Endian;
     136           0 :     if (s == "Big_Endian") return Big_Endian;
     137           0 :     return 0;
     138             :   }
     139             : 
     140           0 :   string uniqSlashes(const string & s) {
     141           0 :     string result;
     142             :     char c;
     143           0 :     bool inslash = false;
     144           0 :     size_t indexi=0;
     145             : 
     146           0 :     while (indexi < s.size()) {
     147           0 :       if ((c = s.at(indexi)) != '/') {
     148           0 :         inslash = false;
     149           0 :         result.push_back(c);
     150             :       }
     151             :       else
     152           0 :         if (inslash == false) {
     153           0 :           result.push_back(c);
     154           0 :           inslash = true;
     155             :         }
     156           0 :       indexi++;
     157             :     }
     158           0 :     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           0 :   string ASDMUtils::version(const string& asdmPath) {
     201             : 
     202           0 :     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           0 :     string ASDMPath = trim_copy(asdmPath);
     209           0 :     if (ASDMPath.back()!='/') ASDMPath+="/";
     210             : #endif
     211           0 :     ASDMPath += "ASDM.xml";
     212             : 
     213             :     // Does ASDMPath exist ?
     214             : #ifndef WITHOUT_BOOST
     215             :     if (!boost::filesystem::exists(boost::filesystem::path(ASDMPath))) {
     216             : #else
     217           0 :     if (!file_exists(ASDMPath)) {
     218             : #endif
     219           0 :       throw ASDMUtilsException("File not found '"+ASDMPath+"'.");
     220             :     }
     221             : 
     222             :     // Read and parse ASDM.xml
     223           0 :     xmlDocPtr ASDMDoc = xmlParseFile(ASDMPath.c_str());
     224             :   
     225           0 :     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           0 :     xmlNodePtr cur = xmlDocGetRootElement(ASDMDoc);
     233           0 :     xmlChar* version_ = xmlGetProp(cur, (const xmlChar *) "schemaVersion");
     234             : 
     235             :     // Yes ? then return its value.
     236           0 :     if (version_ != NULL) {
     237           0 :       result = string((char *) version_);
     238           0 :       xmlFree(version_);
     239           0 :       xmlFreeDoc(ASDMDoc);
     240           0 :       return result;
     241             :     }
     242             : 
     243             :     // Let's do some housecleaning
     244           0 :     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           0 :     string MainPath = trim_copy(asdmPath);
     253           0 :     if (MainPath.back()!='/') MainPath+="/";
     254             : #endif
     255           0 :     MainPath += "Main.xml";    
     256             : 
     257           0 :     result = "UNKNOWN";
     258             :     // Does MainPath exist ?
     259             : #ifndef WITHOUT_BOOST
     260             :     if (boost::filesystem::exists(boost::filesystem::path(MainPath))) {
     261             : #else
     262           0 :     if (file_exists(MainPath)) {
     263             : #endif
     264           0 :       xmlDocPtr MainDoc =  xmlParseFile(MainPath.c_str());
     265             :   
     266           0 :       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           0 :       xmlNodePtr cur = xmlDocGetRootElement(MainDoc)->xmlChildrenNode;
     275           0 :       int nRow = 0;
     276           0 :       while (cur != NULL) {
     277           0 :         if (!xmlStrcmp(cur->name, (const xmlChar*) "row")) {
     278           0 :           nRow++;
     279           0 :           if (hasChild (MainDoc, cur, (const xmlChar *) "dataUID")) {
     280           0 :             result = "3";
     281           0 :             break;
     282             :           }
     283             : 
     284           0 :           if (hasChild (MainDoc, cur, (const xmlChar *) "dataOid")) {
     285           0 :             result = "2";
     286           0 :             break;
     287             :           }
     288             :         }
     289           0 :         cur = cur -> next;
     290             :       }
     291             : 
     292           0 :       xmlFreeDoc(MainDoc);      
     293           0 :       if (nRow == 0) {
     294           0 :         throw ASDMUtilsException("No 'row' elements present in '"+MainPath+"'.");
     295             :       }
     296             :     }
     297             :     
     298           0 :     return result;
     299           0 :   }
     300             : 
     301           0 :   bool ASDMUtils::hasChild(xmlDocPtr, // doc
     302             :                            xmlNodePtr node, const xmlChar* childName) {
     303           0 :     node = node->xmlChildrenNode;
     304             : 
     305           0 :     while (node != NULL) {
     306           0 :       if (!xmlStrcmp(node->name , childName))
     307           0 :         break; 
     308           0 :       node = node->next;
     309             :     }
     310           0 :     return (node != NULL);      
     311             :   }
     312             : 
     313           0 :   string ASDMUtils::parseRow(xmlDocPtr doc, xmlNodePtr node, const xmlChar* childName) {
     314             :     //
     315             :     // Consider the children of node (i.e. expectedly of "<row>...</row>")
     316           0 :     node = node->xmlChildrenNode;
     317             : 
     318             :     //
     319             :     // And look for childName.
     320           0 :     while (node != NULL) {
     321           0 :       if (!xmlStrcmp(node->name , childName)) {
     322           0 :         xmlChar* content = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
     323           0 :         string result((char *) content);
     324           0 :         xmlFree(content);
     325           0 :         return result;
     326           0 :       }
     327           0 :       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           0 :   vector<string> ASDMUtils::telescopeNames(const string& asdmPath) {
     334           0 :     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           0 :     string execBlockPath = trim_copy(asdmPath);
     341           0 :     if (execBlockPath.back()!='/') execBlockPath+="/";
     342             : #endif
     343           0 :     execBlockPath += "ExecBlock.xml";
     344             : 
     345             :     // Does execBlockPath exist ?
     346             : #ifndef WITHOUT_BOOST
     347             :     if (!boost::filesystem::exists(boost::filesystem::path(execBlockPath))) {
     348             : #else
     349           0 :     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           0 :     execBlockDoc = xmlParseFile(execBlockPath.c_str());
     359           0 :     if (execBlockDoc == NULL) {
     360           0 :       throw ASDMUtilsException("Error while parsing '"+execBlockPath+"'.");
     361             :     }
     362             : 
     363           0 :     cur = xmlDocGetRootElement(execBlockDoc)->xmlChildrenNode;
     364           0 :     while (cur != NULL) {
     365           0 :       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           0 :         result.push_back(trim_copy(parseRow(execBlockDoc, cur, (const xmlChar *) "telescopeName")));
     370             : #endif
     371             :       }
     372           0 :       cur = cur -> next;
     373             :     }
     374           0 :     xmlFreeDoc(execBlockDoc);
     375             : 
     376           0 :     return result;
     377           0 :   }
     378             : 
     379           0 :   ASDMUtils::Origin ASDMUtils::origin(const vector<string>& telescopeNames) {
     380           0 :     unsigned int numEVLA = 0;
     381           0 :     unsigned int numALMA = 0;
     382           0 :     for ( vector<string>::const_iterator iter = telescopeNames.begin(); iter != telescopeNames.end(); iter++ ) {
     383           0 :       if (evlaValidNames.find(*iter) != evlaValidNames.end())
     384           0 :         numEVLA++;
     385           0 :       else if (almaValidNames.find(*iter) != almaValidNames.end())
     386           0 :         numALMA++;
     387             :     }
     388             : 
     389           0 :     Origin autoOrigin = UNKNOWN;
     390           0 :     if (numEVLA == telescopeNames.size())
     391           0 :       autoOrigin = EVLA;
     392           0 :     else if (numALMA == telescopeNames.size())
     393           0 :       autoOrigin = ALMA;
     394             :     
     395           0 :     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           0 :   string ASDMUtils::pathToV2V3ALMAxslTransform() {return pathToxslTransform(filenameOfV2V3xslTransform[ASDMUtils::ALMA]);}
     429           0 :   string ASDMUtils::pathToV2V3EVLAxslTransform() {return pathToxslTransform(filenameOfV2V3xslTransform[ASDMUtils::EVLA]);}
     430           0 :   string ASDMUtils::nameOfV2V3xslTransform(ASDMUtils::Origin origin) {
     431           0 :     return filenameOfV2V3xslTransform[origin];
     432             :   }
     433             : 
     434           0 :   ASDMParseOptions::ASDMParseOptions() {
     435           0 :     origin_             = ASDMUtils::UNKNOWN;
     436           0 :     detectOrigin_       = true;
     437           0 :     version_            = "UNKNOWN";
     438           0 :     detectVersion_      = true;
     439           0 :     loadTablesOnDemand_ = false;
     440           0 :     checkRowUniqueness_ = true;
     441           0 :   }
     442             : 
     443           0 :   ASDMParseOptions::ASDMParseOptions(const ASDMParseOptions& x) {
     444           0 :     origin_                                    = x.origin_;
     445           0 :     detectOrigin_                              = x.detectOrigin_;
     446           0 :     version_                                   = x.version_;
     447           0 :     detectVersion_                             = x.detectVersion_;
     448           0 :     loadTablesOnDemand_                        = x.loadTablesOnDemand_;
     449           0 :     checkRowUniqueness_                        = x.checkRowUniqueness_;
     450           0 :   }
     451             : 
     452           0 :   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           0 :   ASDMParseOptions& ASDMParseOptions::loadTablesOnDemand(bool b) { loadTablesOnDemand_ = b;  return *this; }
     469           0 :   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           0 :   XSLTransformer::XSLTransformer() : cur(NULL) {
     477           0 :     xmlSubstituteEntitiesDefault(1);
     478           0 :     xmlLoadExtDtdDefaultValue = 1;
     479           0 :   }
     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           0 :   void XSLTransformer::setTransformation(const string& xsltPath) {
     496           0 :     if (getenv("ASDM_DEBUG")) 
     497           0 :       cout << "XSLTransformer::setTransformation(const string& xsltPath) called on '" << xsltPath << "'." << endl;
     498             :     
     499           0 :     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           0 :     cur = xsltParseStylesheetFile((const xmlChar*) xsltPath.c_str());
     507           0 :     if (cur == NULL)
     508           0 :       throw XSLTransformerException("Failed to parse the XSL stylecheet contained in '" + xsltPath + "'." );
     509           0 :   }
     510             : 
     511             : 
     512           0 :   XSLTransformer::~XSLTransformer() {
     513             :     // cout << "XSLTransformer::~XSLTransformer() called" << endl;
     514           0 :     if (cur) {
     515           0 :       xsltFreeStylesheet(cur);
     516           0 :       cur = NULL;
     517             :     }
     518           0 :   } 
     519             : 
     520           0 :   string XSLTransformer::operator()(const string& xmlPath){
     521           0 :     xmlDocPtr doc = NULL, res = NULL;
     522             :     //xsltStylesheetPtr cur;
     523             : 
     524           0 :     xmlChar* docTxtPtr = NULL;
     525           0 :     int docTxtLen = 0;
     526             :     
     527           0 :     if (getenv("ASDM_DEBUG")) cout << "About to read and parse " << xmlPath << endl;
     528           0 :     doc = xmlParseFile(xmlPath.c_str());
     529           0 :     if (doc == NULL) {
     530           0 :       throw XSLTransformerException("Could not parse the XML file '" + xmlPath + "'." );
     531             :     }
     532             : 
     533           0 :     if (!cur) {
     534           0 :       xmlDocDumpFormatMemory(doc, &docTxtPtr, &docTxtLen, 1);
     535             :     }
     536             :     else {
     537           0 :       res = xsltApplyStylesheet(cur, doc, NULL);
     538           0 :       if ( res == NULL ) {
     539           0 :         throw XSLTransformerException("Failed to apply the XSLT tranformation to the XML document contained in '" + xmlPath + "'.");
     540             :       }
     541           0 :       int status = xsltSaveResultToString(&docTxtPtr,
     542             :                                           &docTxtLen,
     543             :                                           res,
     544             :                                           cur);
     545           0 :       if (status == -1) 
     546           0 :         throw XSLTransformerException("Could not dump the result of the XSL transformation into memory.");
     547             :     }
     548             : 
     549           0 :     if (getenv("ASDM_DEBUG")) 
     550           0 :       cout << "Making a string from the result of the XSL transformation" << endl;
     551           0 :     string docXML((char *) docTxtPtr, docTxtLen);
     552             :     // cout << "docXML = " << docXML << endl;
     553             : 
     554           0 :     xmlFree(docTxtPtr);
     555           0 :     if (res) xmlFreeDoc(res);
     556           0 :     xmlFreeDoc(doc);
     557             :     
     558             :     // cout << "All resources unneeded freed" << endl;
     559           0 :     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