Isis 3 Programmer Reference
PvlTranslationTable.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include <fstream>
8 #include <sstream>
9 
10 #include <QDebug>
11 
12 #include "IException.h"
13 #include "IString.h"
14 #include "Message.h"
15 #include "Pvl.h"
16 #include "PvlTranslationTable.h"
17 
18 using namespace std;
19 namespace Isis {
20 
26  PvlTranslationTable::PvlTranslationTable(FileName transFile) {
27  AddTable(transFile.expanded());
28  }
29 
30 
36  PvlTranslationTable::PvlTranslationTable(std::istream &istr) {
37  istr >> p_trnsTbl;
38  }
39 
40 
44  PvlTranslationTable::PvlTranslationTable() {
45  }
46 
47 
51  PvlTranslationTable::~PvlTranslationTable() {
52  }
53 
54 
62  Pvl &PvlTranslationTable::TranslationTable() {
63  return p_trnsTbl;
64  }
65 
66 
74  const Pvl &PvlTranslationTable::TranslationTable() const {
75  return p_trnsTbl;
76  }
77 
78 
84  void PvlTranslationTable::AddTable(const QString &transFile) {
85  p_trnsTbl.read(FileName(transFile).expanded());
86  }
87 
88 
102  void PvlTranslationTable::AddTable(std::istream &transStm) {
103  transStm >> p_trnsTbl;
104 
105  // pair< name, size > of acceptable keywords.
106  // A size of -1 means non-zero size.
107  vector< pair<QString, int> > validKeywordSizes = validKeywords();
108 
109  for (int i = 0; i < p_trnsTbl.groups(); i++) {
110  PvlGroup currGroup = p_trnsTbl.group(i);
111 
112  if (!currGroup.hasKeyword("InputKey")) {
113  QString message = "Unable to find InputKey for group ["
114  + currGroup.name() + "] in file [" +
115  p_trnsTbl.fileName() + "]";
116  throw IException(IException::User, message, _FILEINFO_);
117  }
118 
119  for (int j = 0; j < currGroup.keywords(); j++) {
120  bool validKeyword = false;
121  bool keywordSizeMismatch = false;
122 
123  const PvlKeyword &currKey = currGroup[j];
124 
125  // Test this keyword for validity
126  for (int key = 0;
127  !validKeyword && key < (int)validKeywordSizes.size();
128  key++) {
129 
130  // If this is the right keyword (names match) then test sizes
131  if (currKey.name() == validKeywordSizes[key].first) {
132 
133  // if -1 then test that size() > 0
134  if (validKeywordSizes[key].second == -1) {
135  if (currKey.size() > 0) {
136  validKeyword = true;
137  }
138  }
139  // otherwise should exact match
140  else if (currKey.size() == validKeywordSizes[key].second) {
141  validKeyword = true;
142  }
143  else {
144  keywordSizeMismatch = true;
145  }
146  }
147 
148  }
149 
150  // if we had an error report it
151  if (!validKeyword) {
152  if (!keywordSizeMismatch) {
153  QString message = "Keyword [" + currKey.name();
154  message += "] is not a valid keyword.";
155  message += " Error in file [" + p_trnsTbl.fileName() + "]" ;
156 
157  throw IException(IException::User, message, _FILEINFO_);
158  }
159  else {
160  QString message = "Keyword [" + currKey.name();
161  message += "] does not have the correct number of elements.";
162  message += " Error in file [" + p_trnsTbl.fileName() + "]" ;
163 
164  throw IException(IException::User, message, _FILEINFO_);
165  }
166 
167  }
168  }
169  }
170  }
171 
172 
179  vector< pair<QString, int> > PvlTranslationTable::validKeywords() const {
180 
181  vector< pair<QString, int> > validKeywords;
182  validKeywords.push_back(pair<QString, int>("Translation", 2));
183  validKeywords.push_back(pair<QString, int>("OutputName", 1));
184  validKeywords.push_back(pair<QString, int>("InputGroup", -1));
185  validKeywords.push_back(pair<QString, int>("InputPosition", -1));
186  validKeywords.push_back(pair<QString, int>("OutputPosition", -1));
187  validKeywords.push_back(pair<QString, int>("Auto", 0));
188  validKeywords.push_back(pair<QString, int>("Optional", 0));
189  validKeywords.push_back(pair<QString, int>("InputKey", 1));
190  validKeywords.push_back(pair<QString, int>("InputDefault", -1));
191  validKeywords.push_back(pair<QString, int>("InputKeyDependencies", -1));
192 
193  return validKeywords;
194  }
195 
196 
216  QString PvlTranslationTable::Translate(const QString translationGroupName,
217  const QString inputKeyValue) const {
218 
219  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
220 
221  // If no input value was passed in search using the input default
222  QString tmpFValue = inputKeyValue;
223  if (tmpFValue.isEmpty()) {
224  if (translationGroup.hasKeyword("InputDefault")) {
225  tmpFValue = (QString) translationGroup["InputDefault"];
226  }
227  else {
228  QString msg = "No value or default value to translate for ";
229  msg += "translation group [";
230  msg += translationGroupName;
231  msg += "] in file [" + p_trnsTbl.fileName() + "]";
232  throw IException(IException::Programmer, msg, _FILEINFO_);
233  }
234  }
235 
236  // Search the Translation keywords for a match to the input value
237  Pvl::ConstPvlKeywordIterator it = translationGroup.findKeyword("Translation",
238  translationGroup.begin(),
239  translationGroup.end());
240 
241  while (it != translationGroup.end()) {
242  const PvlKeyword &key = *it;
243  // compare the value from the input file to the second value of each Translation in the trans file.
244  // ignore cases for input values
245  if (QString::compare((QString) key[1], tmpFValue, Qt::CaseInsensitive) == 0) {
246  return key[0];
247  }
248  else if ((QString) key[1] == "*") {
249  if ((QString) key[0] == "*") {
250  return tmpFValue;
251  }
252  else {
253  return key[0];
254  }
255  }
256 
257  it = translationGroup.findKeyword("Translation", it + 1, translationGroup.end());
258  }
259 
260  QString msg = "Unable to find a translation value for [" +
261  translationGroupName + ", " + inputKeyValue + "] in file [" +
262  p_trnsTbl.fileName() + "]";
263 
264  throw IException(IException::Programmer, msg, _FILEINFO_);
265  }
266 
267 
285  PvlKeyword PvlTranslationTable::InputGroup(const QString translationGroupName,
286  const int inst) const {
287 
288  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
289 
290  //bool foundLegalInputGroup = false;
291 
292  Pvl::ConstPvlKeywordIterator it = translationGroup.findKeyword("InputPosition",
293  translationGroup.begin(),
294  translationGroup.end());
295 
296  int currentInstance = 0;
297 
298  // If no InputPosition keyword exists, the answer is root
299  if (inst == 0 && it == translationGroup.end()) {
300  PvlKeyword root("InputPosition");
301  root += "ROOT";
302  return root;
303  }
304 
305  while (it != translationGroup.end()) {
306  const PvlKeyword &result = *it;
307 
308  // This check is to prevent backtracking to the old "value,value" way of
309  // doing translation file input groups for the new keyword. Flag it
310  // immediately to give a good error message.
311  if (result.size() == 1 && result[0].contains(",")) {
312  QString msg = "Keyword [InputPosition] cannot have a comma [,] in ";
313  msg += " the value [";
314  msg += result[0];
315  msg += "]";
316  throw IException(IException::Programmer, msg, _FILEINFO_);
317  }
318  else {
319  //foundLegalInputGroup = true;
320 
321  if (currentInstance == inst) {
322  return result;
323  }
324 
325  currentInstance ++;
326  }
327 
328  it = translationGroup.findKeyword("InputPosition", it + 1, translationGroup.end());
329  }
330 
331  /* Error if no containers were listed
332  if (!foundLegalInputGroup) {
333  QString msg = "No input position found for translation [";
334  msg += translationGroupName;
335  msg += "] in translation file [";
336  msg += p_trnsTbl.FileName();
337  msg += "]";
338  throw IException::Message(IException::Programmer, msg, _FILEINFO_);
339  }*/
340 
341  PvlKeyword empty;
342  return empty;
343  }
344 
345 
360  QString PvlTranslationTable::InputKeywordName(const QString translationGroupName) const {
361 
362  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
363 
364  if (translationGroup.hasKeyword("InputKey")) return translationGroup["InputKey"];
365 
366  return "";
367  }
368 
369 
382  QString PvlTranslationTable::InputDefault(const QString translationGroupName) const {
383 
384  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
385 
386  if (translationGroup.hasKeyword("InputDefault")) return translationGroup["InputDefault"];
387 
388  return "";
389  }
390 
391 
407  bool PvlTranslationTable::hasInputDefault(const QString translationGroupName) {
408 
409  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
410 
411  if (translationGroup.hasKeyword("InputDefault")) return true;
412 
413  return false;
414  }
415 
416 
432  bool PvlTranslationTable::IsAuto(const QString translationGroupName) {
433 
434  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
435 
436  if (translationGroup.hasKeyword("Auto")) return true;
437 
438  return false;
439  }
440 
441 
457  bool PvlTranslationTable::IsOptional(const QString translationGroupName) {
458 
459  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
460 
461  if (translationGroup.hasKeyword("Optional")) return true;
462 
463  return false;
464  }
465 
466 
483  PvlKeyword PvlTranslationTable::OutputPosition(const QString translationGroupName) {
484 
485  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
486 
487  if (!translationGroup.hasKeyword("OutputPosition")) {
488  QString msg = "Unable to find translation keyword [OutputPostion] in [" +
489  translationGroupName + "] in file [" + p_trnsTbl.fileName() + "]";
490  throw IException(IException::Programmer, msg, _FILEINFO_);
491 
492  }
493 
494  return translationGroup["OutputPosition"];
495  }
496 
497 
511  QString PvlTranslationTable::OutputName(const QString translationGroupName) {
512 
513  const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
514 
515  if (translationGroup.hasKeyword("OutputName")) {
516  return translationGroup["OutputName"];
517  }
518 
519  return "";
520  }
521 
538  const PvlGroup &PvlTranslationTable::findTranslationGroup(const QString translationGroupName) const {
539  if (!p_trnsTbl.hasGroup(translationGroupName)) {
540  QString msg = "Unable to find translation group [" + translationGroupName +
541  "] in file [" + p_trnsTbl.fileName() + "]";
542  throw IException(IException::Programmer, msg, _FILEINFO_);
543  }
544 
545  return p_trnsTbl.findGroup(translationGroupName);
546  }
547 } // end namespace isis
548 
Isis::PvlKeyword::name
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:98
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::PvlContainer::ConstPvlKeywordIterator
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
Definition: PvlContainer.h:160
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::PvlContainer::hasKeyword
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
Definition: PvlContainer.cpp:159
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::Pvl::read
void read(const QString &file)
Loads PVL information from a stream.
Definition: Pvl.cpp:90
Isis::PvlContainer::fileName
QString fileName() const
Returns the filename used to initialise the Pvl object.
Definition: PvlContainer.h:232
Isis::PvlContainer::name
QString name() const
Returns the container name.
Definition: PvlContainer.h:63
Isis::IException
Isis exception class.
Definition: IException.h:91
std
Namespace for the standard library.
Isis::PvlKeyword::size
int size() const
Returns the number of values stored in this keyword.
Definition: PvlKeyword.h:125
Isis::PvlContainer::keywords
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:86
Isis::PvlContainer::findKeyword
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
Definition: PvlContainer.cpp:62
Isis::PvlContainer::end
PvlKeywordIterator end()
Return the ending iterator.
Definition: PvlContainer.h:194
Isis::PvlContainer::begin
PvlKeywordIterator begin()
Return the beginning iterator.
Definition: PvlContainer.h:178
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16