USGS

Isis 3.0 Object Programmers' Reference

Home

PvlTranslationManager.cpp

Go to the documentation of this file.
00001 
00023 #include "IString.h"
00024 #include "Message.h"
00025 #include "IException.h"
00026 #include "PvlTranslationManager.h"
00027 
00028 using namespace std;
00029 namespace Isis {
00030 
00031   PvlTranslationManager::PvlTranslationManager(const QString &transFile) {
00032     AddTable(transFile);
00033   }
00034 
00043   PvlTranslationManager::PvlTranslationManager(Isis::Pvl &inputLabel,
00044       const QString &transFile) {
00045     p_fLabel = inputLabel;
00046 
00047     // Internalize the translation table
00048     AddTable(transFile);
00049   }
00050 
00059   PvlTranslationManager::PvlTranslationManager(Isis::Pvl &inputLabel,
00060       std::istream &transStrm) {
00061     p_fLabel = inputLabel;
00062 
00063     // Internalize the translation table
00064     AddTable(transStrm);
00065   }
00066 
00082   QString PvlTranslationManager::Translate(QString nName, int findex) {
00083     const Isis::PvlContainer *con;
00084     int inst = 0;
00085     PvlKeyword grp;
00086 
00087     while((grp = InputGroup(nName, inst++)).Name() != "") {
00088       if((con = GetContainer(grp)) != NULL) {
00089         if(con->HasKeyword(InputKeywordName(nName))) {
00090           return PvlTranslationTable::Translate(nName,
00091                                                 (*con)[InputKeywordName(nName)][findex]);
00092         }
00093       }
00094     }
00095 
00096     return Isis::PvlTranslationTable::Translate(nName);
00097   }
00098 
00108   Isis::PvlKeyword PvlTranslationManager::DoTranslation(
00109     const QString nName) {
00110     const Isis::PvlContainer *con = NULL;
00111     Isis::PvlKeyword key;
00112 
00113     int inst = 0;
00114     PvlKeyword grp;
00115 
00116     while((grp = InputGroup(nName, inst++)).Name() != "") {
00117       if((con = GetContainer(grp)) != NULL) {
00118         if(con->HasKeyword(InputKeywordName(nName))) {
00119           key.SetName(OutputName(nName));
00120 
00121           for(int v = 0; v < (*con)[(InputKeywordName(nName))].Size(); v++) {
00122             key.AddValue(Isis::PvlTranslationTable::Translate(nName,
00123                          (*con)[InputKeywordName(nName)][v]),
00124                          (*con)[InputKeywordName(nName)].Unit(v));
00125           }
00126 
00127           return key;
00128         }
00129       }
00130     }
00131 
00132     return Isis::PvlKeyword(OutputName(nName),
00133                             PvlTranslationTable::Translate(nName, ""));
00134   }
00135 
00136 
00137 
00138   // Automatically translate all the output names found in the translation table
00139   // If a output name does not translate an error will be thrown by one
00140   // of the support members
00141   // Store the translated key, value pairs in the argument pvl
00142   void PvlTranslationManager::Auto(Isis::Pvl &outputLabel) {
00143     // Attempt to translate every group in the translation table
00144     for(int i = 0; i < TranslationTable().Groups(); i++) {
00145       Isis::PvlGroup &g = TranslationTable().Group(i);
00146       if(IsAuto(g.Name())) {
00147         try {
00148           Isis::PvlContainer *con = CreateContainer(g.Name(), outputLabel);
00149           (*con) += PvlTranslationManager::DoTranslation(g.Name());
00150         }
00151         catch(IException &e) {
00152           if(!IsOptional(g.Name())) {
00153             throw;
00154           }
00155         }
00156       }
00157     }
00158   }
00159 
00169   const PvlKeyword &PvlTranslationManager::InputKeyword(
00170     const QString nName) const {
00171 
00172     int instanceNumber = 0;
00173     PvlKeyword inputGroupKeyword = InputGroup(nName, instanceNumber);
00174     bool anInputGroupFound = false;
00175 
00176     while(inputGroupKeyword.Name() != "") {
00177       const PvlContainer *containingGroup = GetContainer(inputGroupKeyword);
00178       if(containingGroup != NULL) {
00179         anInputGroupFound = true;
00180 
00181         if(containingGroup->HasKeyword(InputKeywordName(nName))) {
00182           return containingGroup->FindKeyword(InputKeywordName(nName));
00183         }
00184       }
00185 
00186       instanceNumber ++;
00187       inputGroupKeyword = InputGroup(nName, instanceNumber);
00188     }
00189 
00190     if(anInputGroupFound) {
00191       QString msg = "Unable to find input keyword [" + InputKeywordName(nName) +
00192                    "] for output name [" + nName + "] in file [" + TranslationTable().FileName() + "]";
00193       throw IException(IException::Programmer, msg, _FILEINFO_);
00194     }
00195     else {
00196       QString container = "";
00197 
00198       for(int i = 0; i < InputGroup(nName).Size(); i++) {
00199         if(i > 0) container += ",";
00200 
00201         container += InputGroup(nName)[i];
00202       }
00203 
00204       QString msg = "Unable to find input group [" + container +
00205                    "] for output name [" + nName + "] in file [" + TranslationTable().FileName() + "]";
00206       throw IException(IException::Programmer, msg, _FILEINFO_);
00207     }
00208   }
00209 
00210 
00217   bool PvlTranslationManager::InputHasKeyword(const QString nName) {
00218 
00219     // Set the current position in the input label pvl
00220     // by finding the input group corresponding to the output group
00221     const Isis::PvlContainer *con;
00222     int inst = 0;
00223     //while ((con = GetContainer(InputGroup(nName, inst++))) != NULL) {
00224     //if ((con = GetContainer (InputGroup(nName))) != NULL) {
00225 
00226     PvlKeyword grp;
00227     while((grp = InputGroup(nName, inst++)).Name() != "") {
00228       if((con = GetContainer(grp)) != NULL) {
00229         if(con->HasKeyword(InputKeywordName(nName))) return true;
00230       }
00231     }
00232 
00233     return false;
00234   }
00235 
00236   /*
00237    * Indicates if the input group corresponding to the output name exists in
00238    * the label
00239    *
00240    * @param nName The output name used to identify the input keyword.
00241 
00242    bool PvlTranslationManager::InputHasGroup (const QString nName) {
00243 
00244      if (GetContainer (InputGroup(nName)) != NULL) {
00245        return true;
00246      }
00247 
00248      return false;
00249    }
00250   */
00251 
00252   // Return a container from the input label according tund
00253   const Isis::PvlContainer *PvlTranslationManager::GetContainer(
00254     const PvlKeyword &inputGroup) const {
00255 
00256 
00257     // Return the root container if "ROOT" is the ONLY thing in the list
00258     if(inputGroup.Size() == 1 &&
00259         PvlKeyword::StringEqual(inputGroup[0], "ROOT")) {
00260       return &p_fLabel;
00261     }
00262 
00263     const Isis::PvlObject *currentObject = &p_fLabel;
00264 
00265     // Search for object containing our solution
00266     int objectIndex;
00267     for(objectIndex = 0;
00268         objectIndex < inputGroup.Size() - 1;
00269         objectIndex ++) {
00270       if(currentObject->HasObject(inputGroup[objectIndex])) {
00271         currentObject = &currentObject->FindObject(inputGroup[objectIndex]);
00272       }
00273       else {
00274         return NULL;
00275       }
00276     }
00277 
00278     // Our solution can be an object or a group
00279     if(currentObject->HasObject(inputGroup[objectIndex])) {
00280       return &currentObject->FindObject(inputGroup[objectIndex]);
00281     }
00282     else if(currentObject->HasGroup(inputGroup[objectIndex])) {
00283       return &currentObject->FindGroup(inputGroup[objectIndex]);
00284     }
00285     else {
00286       return NULL;
00287     }
00288   }
00289 
00290 
00291   // Create the requsted container and any containers above it and
00292   // return a reference to the container
00293   // list is an Isis::PvlKeyword with an array of container types an their names
00294   Isis::PvlContainer *PvlTranslationManager::CreateContainer(const QString nName,
00295       Isis::Pvl &pvl) {
00296 
00297     // Get the array of Objects/Groups from the OutputName keyword
00298     Isis::PvlKeyword np = OutputPosition(nName);
00299 
00300     Isis::PvlObject *obj = &pvl;
00301 
00302     // Look at every pair in the output position
00303     for(int c = 0; c < np.Size(); c += 2) {
00304       // If this pair is an object
00305       if(np[c].toUpper() == "OBJECT") {
00306         // If the object doesn't exist create it
00307         if(!obj->HasObject(np[c+1])) {
00308           obj->AddObject(np[c+1]);
00309         }
00310         obj = &(obj->FindObject(np[c+1]));
00311       }
00312       // If this pair is a group
00313       else if(np[c].toUpper() == "GROUP") {
00314         // If the group doesn't exist create it
00315         if(!obj->HasGroup(np[c+1])) {
00316           obj->AddGroup(np[c+1]);
00317         }
00318         return (Isis::PvlContainer *) & (obj->FindGroup(np[c+1]));
00319 
00320       }
00321     }
00322 
00323     return (Isis::PvlContainer *) obj;
00324   }
00325 } // end namespace isis