/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Calibration/xmlBase/xmlBase-00-00-03/src/test/test_IFile.cxx

Go to the documentation of this file.
00001 
00002 
00003 #include "xmlBase/IFile.h"
00004 
00005 #include <string>
00006 #include <iostream>
00007 
00008 
00009 void lookFor(xmlBase::IFile* ifile, const char* section, const char* item);
00010 
00011 int main() {
00012     
00013   std::string filename("$(XMLBASEROOT)/xml/myIFile.xml");
00014   xmlBase::IFile* ifile = 0;
00015 
00016   ifile = new xmlBase::IFile(filename.c_str());
00017   // fetch some stuff  
00018 
00019   if (ifile) {
00020 
00021     lookFor(ifile, "section1", "section1-val1");
00022     lookFor(ifile, "section1", "section1-val2");
00023     lookFor(ifile, "subsection","subsectionItem");
00024     lookFor(ifile, "section2","subsectionItem");
00025     lookFor(ifile, "section2","bad-int");
00026   }
00027   else std::cout << "Unable to read file " << ifile << std::endl;
00028 
00029 
00030   // Now do it again for IFile referencing schema rather than dtd
00031   std::string filename2("$(XMLBASEROOT)/xml/mySchemaIFile.xml");
00032 
00033   std::cout << std::endl << std::endl;
00034   std::cout << "And for my next trick:  process an IFile using XML Schema.";
00035   std::cout << std::endl;
00036   std::cout << 
00037     "This file should be flagged bad; it doesn't follow schema content model." 
00038             << std::endl << std::endl;
00039 
00040   xmlBase::IFile* ifile2 = 0;
00041 
00042   ifile2 = new xmlBase::IFile(filename2.c_str());
00043   // fetch some stuff  
00044 
00045   if (ifile2) {
00046 
00047     lookFor(ifile2, "section1", "section1-val1");
00048     lookFor(ifile2, "section1", "section1-val2");
00049     lookFor(ifile2, "subsection","subsectionItem");
00050     lookFor(ifile2, "section2","subsectionItem");
00051     lookFor(ifile2, "section2","bad-int");
00052   }
00053   else std::cout << "Unable to read file " << ifile2 << std::endl;
00054   
00055   return(0);
00056 }
00057 
00058 void lookFor(xmlBase::IFile* ifile, const char* section, const char* item) { 
00059   bool haveItem = ifile->contains(section, item);
00060   std::cout << "Item   " << item << "   in section   " << section;
00061 
00062   if (haveItem) {
00063     std::cout << " was found." << std::endl;
00064     // try writing  in various formats
00065     const char* charRep = ifile->getString(section, item);
00066     std::cout << "..as a string = " << charRep << std::endl;
00067 
00068     try {
00069       int intRep = ifile->getInt(section, item);
00070       std::cout << "..as an int = " << intRep << std::endl;
00071     }
00072     catch (xmlBase::IFileException toCatch) {
00073       std::cout << "**ERROR** int conversion failed "
00074                 << std::endl << std::endl;
00075     }
00076 
00077     try {
00078       double doubleRep = ifile->getDouble(section, item);
00079       std::cout << "..as a double = " << doubleRep << std::endl;
00080     }
00081     catch (xmlBase::IFileException toCatch) {
00082       std::cout << " **ERROR** double conversion failed " 
00083                 << std::endl << std::endl;
00084     }
00085     
00086   }
00087   else std::cout << " was NOT found." << std::endl;
00088 }

Generated on Tue Nov 29 22:57:57 2016 for BOSS_7.0.2 by  doxygen 1.4.7