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
18using namespace std;
19namespace Isis {
20
27 AddTable(transFile.expanded());
28 }
29
30
37 istr >> p_trnsTbl;
38 }
39
40
46
47
53
54
63 return p_trnsTbl;
64 }
65
66
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
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
QString fileName() const
Returns the filename used to initialise the Pvl object.
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
void read(const QString &file)
Loads PVL information from a stream.
Definition Pvl.cpp:90
A single keyword-value pair.
Definition PvlKeyword.h:87
QString name() const
Returns the keyword name.
Definition PvlKeyword.h:103
int size() const
Returns the number of values stored in this keyword.
Definition PvlKeyword.h:133
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition PvlObject.h:212
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition PvlObject.h:129
PvlTranslationTable()
Construct an empty PvlTranslationTable.
bool IsOptional(const QString translationGroupName)
Determines whether the translation group is optional.
PvlKeyword OutputPosition(const QString translationGroupName)
Retrieves the OutputPosition PvlKeyword for the translation group with the given name.
virtual std::vector< std::pair< QString, int > > validKeywords() const
Returns a vector of valid keyword names and their sizes.
const PvlGroup & findTranslationGroup(const QString translationGroupName) const
Searches for translation group with the given name.
QString Translate(const QString translationGroupName, const QString inputKeyValue="") const
Translates a single output value from the given translation group name and input value.
bool hasInputDefault(const QString translationGroupName)
Determines whether the given group has a default input value.
virtual ~PvlTranslationTable()
Destroys the PvlTranslationTable object.
QString OutputName(const QString translationGroupName)
Retrieves a string containing the value of the OutputName keyword for the translation group with the ...
void AddTable(std::istream &transStm)
Adds the contents of a translation table to the searchable groups/keys Also performs a verification,...
virtual QString InputKeywordName(const QString translationGroupName) const
Returns the input keyword name from the translation table corresponding to the output name argument.
virtual PvlKeyword InputGroup(const QString translationGroupName, const int inst=0) const
Returns the input group name from the translation table corresponding to the output name argument.
bool IsAuto(const QString translationGroupName)
Determines whether the given group should be automatically translated.
Pvl & TranslationTable()
Protected accessor for pvl translation table passed into class.
QString InputDefault(const QString translationGroupName) const
Returns the input default value from the translation table corresponding to the output name argument.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.