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

PROG: Moved dependecy parsing from child classes to LabelTranslationManager.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/trunk@8175 41f8697f-d340-4b68-9986-7bafba869bb8
parent 9436a5e7
Loading
Loading
Loading
Loading
+78 −5
Original line number Diff line number Diff line
@@ -160,4 +160,77 @@ namespace Isis {
    PvlKeyword outputKeyword( outputName, Translate(outputName) );
    return outputKeyword;
  }


  /**
 * Parses and validates a dependency specification.
 *
 * @param specification The dependency specification string.
 *
 * @return @b QStringList The dependency split into 3 components
 *                        <ol>
 *                          <li>the type (att or tag)</li>
 *                          <li>the name of what to check</li>
 *                          <li>the value to check for</li>
 *                        </ol>
 *
 * @throws IException::Unknown "Malformed dependency specification."
 * @throws IException::Unknown "Specification does not have two components
 *                              separated by [@], the type of dependency and
 *                              the name-value pair.
 * @throws IException::Unknown "Dependency type specification is invalid.
 *                              Valid types are [att] and [tag]"
 * @throws IException::Unknown "Name-value specification does not have two
 *                              components separated by [:]."
 *
 */
QStringList LabelTranslationManager::parseSpecification(QString specification) const {

  QStringList parsedSpecification;

  try {
    QStringList typeSplit = specification.split("@", QString::SkipEmptyParts);
    QStringList colonSplit = specification.split(":", QString::SkipEmptyParts);
    if (typeSplit.size() == 2) { //handle tag@elementname:value
      if (typeSplit[0].toLower() != "att" &&
          typeSplit[0].toLower() != "tag" &&
          typeSplit[0].toLower() != "new") {
        QString msg = "Dependency type specification [" + typeSplit[0] +
                      "] is invalid. Valid types are [att], [tag] and [new]";
        throw IException(IException::Unknown, msg, _FILEINFO_);
      }
      parsedSpecification.append(typeSplit[0].toLower());

      QStringList nameValueSplit = typeSplit[1].split(":", QString::SkipEmptyParts);
      if (nameValueSplit.size() == 2) {
        parsedSpecification.append(nameValueSplit);
      }
      else if (nameValueSplit.size() == 1) {
        parsedSpecification.append(nameValueSplit);
      }
      else { //nameValueSplit is an unexpected value
        QString msg = "Malformed dependency specification [" + specification + "].";
        throw IException(IException::Unknown, msg, _FILEINFO_);
      }
    }
    else if (colonSplit.size() == 2) { //handle elementname:value
      parsedSpecification = colonSplit;
    }
    else if (colonSplit.size() == 1 && typeSplit.size() == 1) { //handle value with no "@" or ":" characters
      parsedSpecification = colonSplit;
    }
    else { //nameValueSplit is an unexpected value
      QString msg = " [" + specification + "] has unexpected number of '@' or ':' delimiters";
      throw IException(IException::Unknown,msg, _FILEINFO_);
    }
  }

  catch (IException &e) {
    QString msg = "Malformed dependency specification [" + specification + "].";
    throw IException(e, IException::Unknown, msg, _FILEINFO_);
  }

  return parsedSpecification;
}

} // end namespace isis
+6 −4
Original line number Diff line number Diff line
@@ -45,9 +45,12 @@ namespace Isis {
   *
   * @internal
   *  @history 2017-01-11 Jeannie Backer - Original Version. Code moved out of
   *                          PvlToPvlTranslationManager to make a generic parent
   *                          PvlTranslationManager to make a generic parent
   *                          class. Fixes #4584.
   *  @history 2017-01-20 Jesse Mapel - Updated documentation and unit test. Fixes #4584.
   *  @history 2017-01-20 Jesse Mapel - Updated documentation and unit test.
   *                          Fixes #4584.
   *  @history 2017-05-26 Cole Neubauer - Moved parseDependancy from children
   *                          class. Fixes #5167.
   */
  class LabelTranslationManager : public PvlTranslationTable {
    public:
@@ -66,6 +69,7 @@ namespace Isis {
      // Translate all translation table groups which contain "Auto"
      virtual void Auto(Pvl &outputLabel);

      virtual QStringList parseSpecification(QString specification) const;
    protected:

      virtual PvlKeyword DoTranslation(const QString nName);
@@ -74,5 +78,3 @@ namespace Isis {
};

#endif

+28 −0
Original line number Diff line number Diff line
@@ -21,3 +21,31 @@ Group = Mapping
  CenterLongitude = "Test Input, Index 0"
End_Group
End

Testing parseSpecification method: att@name:value

Testing parseSpecification method: tag@name:value

Testing parseSpecification method: att@name

Testing parseSpecification method: new@name

Testing parseSpecification method: name:value

Testing parseSpecification method: value

Testing parseSpecification method: att:name:value
**ERROR** Malformed dependency specification [att:name:value].
**ERROR** [att:name:value] has unexpected number of '@' or ':' delimiters.

Testing parseSpecification method: att@name@value
**ERROR** Malformed dependency specification [att@name@value].
**ERROR** [att@name@value] has unexpected number of '@' or ':' delimiters.

Testing parseSpecification method: not@name:value
**ERROR** Malformed dependency specification [not@name:value].
**ERROR** Dependency type specification [not] is invalid. Valid types are [att], [tag] and [new].

Testing parseSpecification method: att@name:value1:value2
**ERROR** Malformed dependency specification [att@name:value1:value2].
**ERROR** Malformed dependency specification [att@name:value1:value2].
+113 −67
Original line number Diff line number Diff line
@@ -36,10 +36,8 @@ class TestTranslationManager : public LabelTranslationManager {
};

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

  try {

  Preference::Preferences(true);
  stringstream trnsStrm;
  trnsStrm << "Group = NumberOfLines" << endl;
  trnsStrm << "  Auto" << endl;
@@ -104,6 +102,7 @@ int main(void) {

  TestTranslationManager transMgr(trnsStrm);

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

    cout << endl << "Testing Translate method:" << endl;
@@ -114,11 +113,58 @@ int main(void) {
    Pvl translatedLabel;
    transMgr.Auto(translatedLabel);
    cout << endl << translatedLabel << endl;
  }
  catch(IException &e) {
    e.print();
  }
  try {
    cout << endl << "Testing parseSpecification method: att@name:value" << endl;
    transMgr.parseSpecification((QString)"att@name:value");

    cout << endl << "Testing parseSpecification method: tag@name:value" << endl;
    transMgr.parseSpecification((QString)"tag@name:value");

    cout << endl << "Testing parseSpecification method: att@name" << endl;
    transMgr.parseSpecification((QString)"att@name");

    cout << endl << "Testing parseSpecification method: new@name" << endl;
    transMgr.parseSpecification((QString)"new@name");

    cout << endl << "Testing parseSpecification method: name:value" << endl;
    transMgr.parseSpecification((QString)"name:value");

    cout << endl << "Testing parseSpecification method: value" << endl;
    transMgr.parseSpecification((QString)"value");

    cout << endl << "Testing parseSpecification method: att:name:value" << endl;
    transMgr.parseSpecification((QString)"att:name:value");
  }
  catch(IException &e) {
    e.print();
  }

  try {
    cout << endl << "Testing parseSpecification method: att@name@value" << endl;
    transMgr.parseSpecification((QString)"att@name@value");
  }
  catch(IException &e) {
    e.print();
  }

  try {
    cout << endl << "Testing parseSpecification method: not@name:value" << endl;
    transMgr.parseSpecification((QString)"not@name:value");
  }
  catch(IException &e) {
    e.print();
  }

  try {
    cout << endl << "Testing parseSpecification method: att@name:value1:value2" << endl;
    transMgr.parseSpecification((QString)"att@name:value1:value2");
  }
  catch(IException &e) {
    e.print();
  }
  return 0;
}