Isis 3.0 Programmer Reference
Back | Home
PvlTranslationTable.cpp
Go to the documentation of this file.
1 
23 #include <fstream>
24 #include <sstream>
25 
26 #include "IException.h"
27 #include "IString.h"
28 #include "Message.h"
29 #include "Pvl.h"
30 #include "PvlTranslationTable.h"
31 
32 using namespace std;
33 namespace Isis {
34 
42  PvlTranslationTable::PvlTranslationTable(Isis::FileName transFile) {
43  AddTable(transFile.expanded());
44  }
45 
51  PvlTranslationTable::PvlTranslationTable(std::istream &istr) {
52  istr >> p_trnsTbl;
53  }
54 
56  PvlTranslationTable::PvlTranslationTable() {
57  }
58 
66  void PvlTranslationTable::AddTable(const QString &transFile) {
67  p_trnsTbl.read(FileName(transFile).expanded());
68  }
69 
77  void PvlTranslationTable::AddTable(std::istream &transStm) {
78  transStm >> p_trnsTbl;
79 
80  for(int i = 0; i < p_trnsTbl.groups(); i++) {
81  PvlGroup currGrp = p_trnsTbl.group(i);
82 
83  if(!currGrp.hasKeyword("InputKey")) {
84  QString message = "Unable to find InputKey for group ["
85  + currGrp.name() + "] in file [" +
86  p_trnsTbl.fileName() + "]";
87  throw IException(IException::User, message, _FILEINFO_);
88  }
89 
90  // pair< name, size > of acceptable keywords.
91  // A size of -1 means non-zero size.
92  vector< pair<QString, int> > validKeywords;
93 
94  validKeywords.push_back(pair<QString, int>("Translation", 2));
95  validKeywords.push_back(pair<QString, int>("OutputName", 1));
96  validKeywords.push_back(pair<QString, int>("InputGroup", -1));
97  validKeywords.push_back(pair<QString, int>("InputPosition", -1));
98  validKeywords.push_back(pair<QString, int>("OutputPosition", -1));
99  validKeywords.push_back(pair<QString, int>("Auto", 0));
100  validKeywords.push_back(pair<QString, int>("Optional", 0));
101  validKeywords.push_back(pair<QString, int>("InputKey", 1));
102  validKeywords.push_back(pair<QString, int>("InputDefault", -1));
103 
104  for(int j = 0; j < currGrp.keywords(); j++) {
105  bool validKeyword = false;
106  bool keywordSizeMismatch = false;
107 
108  const PvlKeyword &currKey = currGrp[j];
109 
110  // Test this keyword for validity
111  for(int key = 0;
112  !validKeyword && key < (int)validKeywords.size();
113  key++) {
114 
115  // If this is the right keyword (names match) then test sizes
116  if(currKey.name() == validKeywords[key].first) {
117 
118  // if -1 then test that size() > 0
119  if(validKeywords[key].second == -1) {
120  if(currKey.size() > 0) {
121  validKeyword = true;
122  }
123  }
124  // otherwise should exact match
125  else if(currKey.size() == validKeywords[key].second) {
126  validKeyword = true;
127  }
128  else {
129  keywordSizeMismatch = true;
130  }
131  }
132 
133  }
134 
135  // if we had an error report it
136  if(!validKeyword) {
137  if(!keywordSizeMismatch) {
138  QString message = "Keyword [" + currKey.name();
139  message += "] is not a valid keyword.";
140  message += " Error in file [" + p_trnsTbl.fileName() + "]" ;
141 
142  throw IException(IException::User, message, _FILEINFO_);
143  }
144  else {
145  QString message = "Keyword [" + currKey.name();
146  message += "] does not have the correct number of elements.";
147  message += " Error in file [" + p_trnsTbl.fileName() + "]" ;
148 
149  throw IException(IException::User, message, _FILEINFO_);
150  }
151 
152  }
153  }
154  }
155  }
156 
168  QString PvlTranslationTable::Translate(const QString nName,
169  const QString fValue) const {
170  if(!p_trnsTbl.hasGroup(nName)) {
171  QString msg = "Unable to find translation group [" +
172  nName + "] in file [" + p_trnsTbl.fileName() + "]";
173 
174  throw IException(IException::Programmer, msg, _FILEINFO_);
175  }
176 
177  const PvlGroup &tgrp = p_trnsTbl.findGroup(nName);
178 
179  // If no input value was passed in search using the input default
180  QString tmpFValue = fValue;
181  if(tmpFValue.isEmpty()) {
182  if(tgrp.hasKeyword("InputDefault")) {
183  tmpFValue = (QString) tgrp["InputDefault"];
184  }
185  else {
186  QString msg = "No value or default value to translate for ";
187  msg += "translation group [";
188  msg += nName;
189  msg += "] in file [" + p_trnsTbl.fileName() + "]";
190  throw IException(IException::Programmer, msg, _FILEINFO_);
191  }
192  }
193 
194  // Search the Translation keywords for a match to the input value
195  Pvl::ConstPvlKeywordIterator it = tgrp.findKeyword("Translation",
196  tgrp.begin(),
197  tgrp.end());
198 
199  while(it != tgrp.end()) {
200  const PvlKeyword &key = *it;
201  if((QString) key[1] == tmpFValue) {
202  return key[0];
203  }
204  else if((QString) key[1] == "*") {
205  if((QString) key[0] == "*") {
206  return tmpFValue;
207  }
208  else {
209  return key[0];
210  }
211  }
212 
213  it = tgrp.findKeyword("Translation", it + 1, tgrp.end());
214  }
215 
216  QString msg = "Unable to find a translation value for [" +
217  nName + ", " + fValue + "] in file [" +
218  p_trnsTbl.fileName() + "]";
219 
220  throw IException(IException::Programmer, msg, _FILEINFO_);
221  }
222 
235  PvlKeyword PvlTranslationTable::InputGroup(const QString nName,
236  const int inst) const {
237 
238  if(!p_trnsTbl.hasGroup(nName)) {
239  QString msg = "Unable to find translation group [" +
240  nName + "] in file [" + p_trnsTbl.fileName() + "]";
241  throw IException(IException::Programmer, msg, _FILEINFO_);
242  }
243 
244  const PvlGroup &transGrp = p_trnsTbl.findGroup(nName);
245 
246  //bool foundLegalInputGroup = false;
247 
248  Pvl::ConstPvlKeywordIterator it = transGrp.findKeyword("InputPosition",
249  transGrp.begin(),
250  transGrp.end());
251 
252  int currentInstance = 0;
253 
254  // If no InputGroup keywords exist, the answer is root
255  if(inst == 0 && it == transGrp.end()) {
256  PvlKeyword root("InputPosition");
257  root += "ROOT";
258  return root;
259  }
260 
261  while(it != transGrp.end()) {
262  const PvlKeyword &result = *it;
263 
264  // This check is to prevent backtracking to the old "value,value" way of
265  // doing translation file input groups for the new keyword. Flag it
266  // immediately to give a good error message.
267  if(result.size() == 1 && result[0].contains(",")) {
268  QString msg = "Keyword [InputPosition] cannot have a comma [,] in ";
269  msg += " the value [";
270  msg += result[0];
271  msg += "]";
272  throw IException(IException::Programmer, msg, _FILEINFO_);
273  }
274  else {
275  //foundLegalInputGroup = true;
276 
277  if(currentInstance == inst) {
278  return result;
279  }
280 
281  currentInstance ++;
282  }
283 
284  it = transGrp.findKeyword("InputPosition", it + 1, transGrp.end());
285  }
286 
287  /* Error if no containers were listed
288  if(!foundLegalInputGroup) {
289  QString msg = "No input position found for translation [";
290  msg += nName;
291  msg += "] in translation file [";
292  msg += p_trnsTbl.FileName();
293  msg += "]";
294  throw iException::Message(iException::Programmer, msg, _FILEINFO_);
295  }*/
296 
297  PvlKeyword empty;
298  return empty;
299  }
300 
311  QString PvlTranslationTable::InputKeywordName(const QString nName) const {
312 
313  if(!p_trnsTbl.hasGroup(nName)) {
314  QString msg = "Unable to find translation group [" +
315  nName + "] in file [" + p_trnsTbl.fileName() + "]";
316  throw IException(IException::Programmer, msg, _FILEINFO_);
317  }
318 
319  Isis::PvlGroup tgrp = p_trnsTbl.findGroup(nName);
320  if(tgrp.hasKeyword("InputKey")) return tgrp["InputKey"];
321 
322  return "";
323  }
324 
335  QString PvlTranslationTable::InputDefault(const QString nName) const {
336 
337  if(!p_trnsTbl.hasGroup(nName)) {
338  QString msg = "Unable to find translation group [" +
339  nName + "] in file [" + p_trnsTbl.fileName() + "]";
340  throw IException(IException::Programmer, msg, _FILEINFO_);
341  }
342 
343  Isis::PvlGroup tgrp = p_trnsTbl.findGroup(nName);
344  if(tgrp.hasKeyword("InputDefault")) return tgrp["InputDefault"];
345 
346  return "";
347  }
348 
349  bool PvlTranslationTable::IsAuto(const QString nName) {
350  if(!p_trnsTbl.hasGroup(nName)) {
351  QString msg = "Unable to find translation group [" + nName +
352  "] in file [" + p_trnsTbl.fileName() + "]";
353  throw IException(IException::Programmer, msg, _FILEINFO_);
354  }
355 
356  Isis::PvlGroup &tgrp = p_trnsTbl.findGroup(nName);
357  if(tgrp.hasKeyword("Auto")) return true;
358 
359  return false;
360  }
361 
362  bool PvlTranslationTable::IsOptional(const QString nName) {
363  if(!p_trnsTbl.hasGroup(nName)) {
364  QString msg = "Unable to find translation group [" + nName +
365  "] in file [" + p_trnsTbl.fileName() + "]";
366  throw IException(IException::Programmer, msg, _FILEINFO_);
367  }
368 
369  Isis::PvlGroup &tgrp = p_trnsTbl.findGroup(nName);
370  if(tgrp.hasKeyword("Optional")) return true;
371 
372  return false;
373  }
374 
375  Isis::PvlKeyword &PvlTranslationTable::OutputPosition(
376  const QString nName) {
377  if(!p_trnsTbl.hasGroup(nName)) {
378  QString msg = "Unable to find translation group [" +
379  nName + "] in file [" + p_trnsTbl.fileName() + "]";
380  throw IException(IException::Programmer, msg, _FILEINFO_);
381  }
382 
383  Isis::PvlGroup &tgrp = p_trnsTbl.findGroup(nName);
384  if(!tgrp.hasKeyword("OutputPosition")) {
385  QString msg = "Unable to find translation keyword [OutputPostion] in [" +
386  nName + "] in file [" + p_trnsTbl.fileName() + "]";
387  throw IException(IException::Programmer, msg, _FILEINFO_);
388 
389  }
390 
391  return tgrp["OutputPosition"];
392  }
393 
394 
395  QString PvlTranslationTable::OutputName(const QString nName) {
396  if(!p_trnsTbl.hasGroup(nName)) {
397  QString msg = "Unable to find translation group [" + nName +
398  "] in file [" + p_trnsTbl.fileName() + "]";
399  throw IException(IException::Programmer, msg, _FILEINFO_);
400  }
401 
402  Isis::PvlGroup tgrp = p_trnsTbl.findGroup(nName);
403  if(tgrp.hasKeyword("OutputName")) {
404  return tgrp["OutputName"];
405  }
406 
407  return "";
408  }
409 } // end namespace isis
410 
PvlKeywordIterator end()
Return the ending iterator.
Definition: PvlContainer.h:209
int size() const
Returns the number of values stored in this keyword.
Definition: PvlKeyword.h:141
File name manipulation and expansion.
Definition: FileName.h:111
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:101
PvlKeywordIterator begin()
Return the beginning iterator.
Definition: PvlContainer.h:193
QString fileName() const
Returns the filename used to initialise the Pvl object.
Definition: PvlContainer.h:247
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:114
A single keyword-value pair.
Definition: PvlKeyword.h:98
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
Isis exception class.
Definition: IException.h:99
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
Definition: PvlContainer.h:175
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
QString name() const
Returns the container name.
Definition: PvlContainer.h:78

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:27:26