Commit b8c851aa authored by Jesse Mapel's avatar Jesse Mapel
Browse files

Added utilities for translating xml label files. Fixes #4584.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/trunk@7385 41f8697f-d340-4b68-9986-7bafba869bb8
parent 1990073a
Loading
Loading
Loading
Loading
+163 −0
Original line number Diff line number Diff line
/**
 * @file
 * $Revision: 1.10 $
 * $Date: 2010/01/04 18:01:31 $
 *
 *   Unless noted otherwise, the portions of Isis written by the USGS are public
 *   domain. See individual third-party library and package descriptions for
 *   intellectual property information,user agreements, and related information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or implied,
 *   is made by the USGS as to the accuracy and functioning of such software
 *   and related material nor shall the fact of distribution constitute any such
 *   warranty, and no responsibility is assumed by the USGS in connection
 *   therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html in a browser or see
 *   the Privacy & Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */
#include "PvlTranslationTable.h"

#include "IException.h"
#include "IString.h"
#include "LabelTranslationManager.h"
#include "Message.h"
#include "Pvl.h"
#include "PvlContainer.h"
#include "PvlGroup.h"
#include "PvlKeyword.h"
#include "PvlObject.h"

using namespace std;
namespace Isis {

  /**
   * Constructs a default LabelTranslationManager.
   */
  LabelTranslationManager::LabelTranslationManager() 
      : PvlTranslationTable() {
  }


  /**
   * Constructs a LabelTranslationManager with a given translation table.
   *
   * @param transfile The translation table file.
   */
  LabelTranslationManager::LabelTranslationManager(const QString &transFile) 
      : PvlTranslationTable() {
    AddTable(transFile);
  }


  /**
   * Constructs and initializes a LabelTranslationManager object
   *
   * @param transStrm A stream containing the tranlation table to be used to
   *                  tranlate keywords in the input label.
   */
  LabelTranslationManager::LabelTranslationManager(std::istream &transStrm)
      : PvlTranslationTable() {
    AddTable(transStrm);
  }


  /**
   * Destroys the LabelTranslationManager object.
   */
  LabelTranslationManager::~LabelTranslationManager() {
  }


  /**
   * Automatically translate all the output names tagged as Auto in the 
   * translation table If a output name does not translate an error will be
   * thrown by one of the support members.
   *
   * The results of the translations will be stored in the outputLabel PVL
   * based on the OutputPosition keywords in the translation table.
   *
   * @param outputLabel The PVL to add the translated keywords to.
   */
  void LabelTranslationManager::Auto(Pvl &outputLabel) {
    // Attempt to translate every group in the translation table
    for(int i = 0; i < TranslationTable().groups(); i++) {
      PvlGroup &g = TranslationTable().group(i);
      if(IsAuto(g.name())) {
        try {
          PvlContainer *con = CreateContainer(g.name(), outputLabel);
          (*con) += DoTranslation(g.name());
        }
        catch(IException &e) {
          if(!IsOptional(g.name())) {
            throw;
          }
        }
      }
    }
  }


  /**
   * Creates all parent PVL containers for an output keyword. If any parent
   * containers already exist then they will not be recreated.
   *
   * @param nName The name of the output keyword. The OutputPosition keyword
   *              in the translation group for nName will be used to determine
   *              which containers are made.
   * @param pvl The PVL file to create the containers in.
   *
   * @return @b PvlContainer The immediate parent container for nName.
   */
  PvlContainer *LabelTranslationManager::CreateContainer(const QString nName,
                                                         Pvl &pvl) {

    // Get the array of Objects/Groups from the OutputName keyword
    PvlKeyword np = OutputPosition(nName);

    PvlObject *obj = &pvl;

    // Look at every pair in the output position
    for(int c = 0; c < np.size(); c += 2) {
      // If this pair is an object
      if(np[c].toUpper() == "OBJECT") {
        // If the object doesn't exist create it
        if(!obj->hasObject(np[c+1])) {
          obj->addObject(np[c+1]);
        }
        obj = &(obj->findObject(np[c+1]));
      }
      // If this pair is a group
      else if(np[c].toUpper() == "GROUP") {
        // If the group doesn't exist create it
        if(!obj->hasGroup(np[c+1])) {
          obj->addGroup(np[c+1]);
        }
        return (PvlContainer *) & (obj->findGroup(np[c+1]));

      }
    }

    return (PvlContainer *) obj;
  }


  /**
   * Translate the requested output name to output values using the input name
   * and values or default value
   *
   * @param outputName The output name used to identify the input keyword to be
   *                   translated.
   *
   * @return @b PvlKeyword A keyword containing the output name and output value. 
   * 
   * @TODO output units
   */
  PvlKeyword LabelTranslationManager::DoTranslation(const QString outputName) {
    PvlKeyword outputKeyword( outputName, Translate(outputName) );
    return outputKeyword;
  }
} // end namespace isis
+78 −0
Original line number Diff line number Diff line
#ifndef LabelTranslationManager_h
#define LabelTranslationManager_h
/**
 * @file
 * $Revision: 1.6 $
 * $Date: 2010/01/04 18:01:31 $
 *
 *   Unless noted otherwise, the portions of Isis written by the USGS are public
 *   domain. See individual third-party library and package descriptions for
 *   intellectual property information,user agreements, and related information.
 *
 *   Although Isis has been used by the USGS, no warranty, expressed or implied,
 *   is made by the USGS as to the accuracy and functioning of such software
 *   and related material nor shall the fact of distribution constitute any such
 *   warranty, and no responsibility is assumed by the USGS in connection
 *   therewith.
 *
 *   For additional information, launch
 *   $ISISROOT/doc//documents/Disclaimers/Disclaimers.html in a browser or see
 *   the Privacy &amp; Disclaimers page on the Isis website,
 *   http://isis.astrogeology.usgs.gov, and the USGS privacy and disclaimers on
 *   http://www.usgs.gov/privacy.html.
 */

#include <string>
#include <fstream>

#include "FileName.h"
#include "PvlTokenizer.h"
#include "PvlTranslationTable.h"

namespace Isis {
  class Pvl;
  class PvlContainer;
  class PvlKeyword;
  /**
   * @brief Allows applications to translate simple text files
   *
   * This class allows the translation of text files which can be parsed by the
   * Pvl class.
   *
   * @ingroup Parsing
   *
   * @author 2017-01-11 Jeannie Backer
   *
   * @internal
   *  @history 2017-01-11 Jeannie Backer - Original Version. Code moved out of
   *                          PvlTranslationManager to make a generic parent
   *                          class. Fixes #4584.
   *  @history 2017-01-20 Jesse Mapel - Updated documentation and unit test. Fixes #4584.
   */
  class LabelTranslationManager : public PvlTranslationTable {
    public:
      LabelTranslationManager();

      LabelTranslationManager(const QString &transFile);

      LabelTranslationManager(std::istream &transStrm);

      virtual ~LabelTranslationManager();

      // Attempt to translate the requested output name to output value
      // using the input name and value/default value
      virtual QString Translate(QString nName, int findex = 0) = 0;

      // Translate all translation table groups which contain "Auto"
      virtual void Auto(Pvl &outputLabel);

    protected:

      virtual PvlKeyword DoTranslation(const QString nName);
      virtual PvlContainer *CreateContainer(const QString nName, Pvl &pvl);
  };
};

#endif

+23 −0
Original line number Diff line number Diff line
Testing LabelTranslationManager object

Testing Translate method:

Translating Extra: Test Input, Index 0

Testing Auto method:

Object = IsisCube
  Object = BandBin
    BandName = "Test Input, Index 0"
  End_Object

  Group = Dimensions
    NumberOfLines = "Test Input, Index 0"
    NumberOfBands = "Test Input, Index 0"
  End_Group
End_Object

Group = Mapping
  CenterLongitude = "Test Input, Index 0"
End_Group
End
+7 −0
Original line number Diff line number Diff line
ifeq ($(ISISROOT), $(BLANK))
.SILENT:
error:
	echo "Please set ISISROOT";
else
	include $(ISISROOT)/make/isismake.objs
endif
 No newline at end of file
+124 −0
Original line number Diff line number Diff line
#include <sstream>
#include "LabelTranslationManager.h"
#include "Preference.h"
#include "IException.h"
#include "IString.h"
#include "Preference.h"

using namespace Isis;
using namespace std;
/**
 * Child class of LabelTranslationManager to implement pure virtual methods.
 *
 * @author 2017-01-11 Jeannie Backer
 *
 * @internal
 *  @history 2017-01-11 Jeannie Backer - Original Version. Fixes #4584.
 */
class TestTranslationManager : public LabelTranslationManager {
  public:

    TestTranslationManager(const QString &transFile) : LabelTranslationManager() {
    AddTable(transFile);
    }
  
    TestTranslationManager(std::istream &transStrm) : LabelTranslationManager() {
    AddTable(transStrm);
    }
  
    ~TestTranslationManager() {
    }
  
    virtual QString Translate(QString nName, int findex = 0) {
      QString inputValue = "Test Input, Index " + toString(findex);
      return PvlTranslationTable::Translate(nName, inputValue);
    }
};

int main(void) {
  Preference::Preferences(true);

  try {

    stringstream trnsStrm;
    trnsStrm << "Group = NumberOfLines" << endl;
    trnsStrm << "  Auto" << endl;
    trnsStrm << "  OutputName = Lines" << endl;
    trnsStrm << "  OutputPosition = (\"Object\",\"IsisCube\",";
    trnsStrm <<                      "\"Group\",\"Dimensions\")" << endl;
    trnsStrm << "  InputPosition = (Image,Size)" << endl;
    trnsStrm << "  InputKey = NL" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;
    trnsStrm << "Group = NumberOfBands" << endl;
    trnsStrm << "  Auto" << endl;
    trnsStrm << "  Optional" << endl;
    trnsStrm << "  OutputName = Bands" << endl;
    trnsStrm << "  OutputPosition = (\"Object\",\"IsisCube\",";
    trnsStrm <<                      "\"Group\",\"Dimensions\")" << endl;
    trnsStrm << "  InputPosition = (Image,Size)" << endl;
    trnsStrm << "  InputKey = Nb" << endl;
    trnsStrm << "  InputDefault = 1" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;
    trnsStrm << "Group = Bonus" << endl;
    trnsStrm << "  Auto" << endl;
    trnsStrm << "  Optional" << endl;
    trnsStrm << "  InputPosition = (Image,Pixel)" << endl;
    trnsStrm << "  InputKey = Bonus" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;
    trnsStrm << "Group = Extra" << endl;
    trnsStrm << "  Optional" << endl;
    trnsStrm << "  InputPosition = (Image,Bogus)" << endl;
    trnsStrm << "  InputKey = Extra" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;
    trnsStrm << "Group = PixelResolution" << endl;
    trnsStrm << "  InputPosition = (Image,Pixel)" << endl;
    trnsStrm << "  InputKey = Resolution" << endl;
    trnsStrm << "  InputDefault = 1" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;
    trnsStrm << "Group = BandName" << endl;
    trnsStrm << "  Auto" << endl;
    trnsStrm << "  OutputName = Band" << endl;
    trnsStrm << "  OutputPosition = (\"Object\",\"IsisCube\",";
    trnsStrm <<                      "\"Object\",\"BandBin\")" << endl;
    trnsStrm << "  InputPosition = (Image,BandInfo)" << endl;
    trnsStrm << "  InputKey = Band" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;
    trnsStrm << "Group = CenterLongitude" << endl;
    trnsStrm << "  Auto" << endl;
    trnsStrm << "  OutputPosition = (\"Group\",\"Mapping\")" << endl;
    trnsStrm << "  OutputName = CenterLongitude" << endl;
    trnsStrm << "  InputPosition = IMAGE_MAP_PROJECTION" << endl;
    trnsStrm << "  InputPosition = (QUBE,IMAGE_MAP_PROJECTION)" << endl;
    trnsStrm << "  InputPosition = (SPECTRAL_QUBE,IMAGE_MAP_PROJECTION)" << endl;
    trnsStrm << "  InputKey = CENTER_LONGITUDE" << endl;
    trnsStrm << "  Translation = (*,*)" << endl;
    trnsStrm << "EndGroup" << endl;

    trnsStrm << "End" << endl;

    TestTranslationManager transMgr(trnsStrm);

    cout << "Testing LabelTranslationManager object" << endl;

    cout << endl << "Testing Translate method:" << endl;
    cout << endl << "Translating Extra: ";
    cout << transMgr.Translate("Extra") << endl;

    cout << endl << "Testing Auto method:" << endl;
    Pvl translatedLabel;
    transMgr.Auto(translatedLabel);
    cout << endl << translatedLabel << endl;

  }
  catch(IException &e) {
    e.print();
  }

  return 0;
}
Loading