Isis 3 Programmer Reference
PvlToPvlTranslationManager.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "LabelTranslationManager.h"
8 
9 #include "IException.h"
10 #include "IString.h"
11 #include "Message.h"
12 #include "Pvl.h"
13 #include "PvlContainer.h"
14 #include "PvlGroup.h"
15 #include "PvlKeyword.h"
16 #include "PvlObject.h"
17 #include "PvlToPvlTranslationManager.h"
18 
19 using namespace std;
20 namespace Isis {
21 
31  PvlToPvlTranslationManager::PvlToPvlTranslationManager(const QString &transFile)
32  : LabelTranslationManager(transFile) {
33  }
34 
45  : LabelTranslationManager(transStrm) {
46  }
47 
48 
58  const QString &transFile)
59  : LabelTranslationManager(transFile) {
60  p_fLabel = inputLabel;
61  }
62 
63 
73  std::istream &transStrm)
74  : LabelTranslationManager(transStrm) {
75  p_fLabel = inputLabel;
76  }
77 
78 
81  }
82 
83 
84  void PvlToPvlTranslationManager::SetLabel(Pvl &inputLabel) {
85  p_fLabel = inputLabel;
86  }
87 
88 
107  QString PvlToPvlTranslationManager::Translate(QString translationGroupName, int findex) {
108  const PvlContainer *con;
109  int inst = 0;
110  PvlKeyword grp;
111 
112  while((grp = InputGroup(translationGroupName, inst++)).name() != "") {
113  if((con = GetContainer(grp)) != NULL) {
114  if(con->hasKeyword(InputKeywordName(translationGroupName))) {
115  return PvlTranslationTable::Translate(translationGroupName,
116  (*con)[InputKeywordName(translationGroupName)][findex]);
117  }
118  }
119  }
120 
121  return PvlTranslationTable::Translate(translationGroupName);
122  }
123 
124 
142  PvlKeyword PvlToPvlTranslationManager::DoTranslation(const QString translationGroupName) {
143  const PvlContainer *con = NULL;
144  PvlKeyword key;
145 
146  int inst = 0;
147  PvlGroup transGroup;
148  PvlKeyword grp;
149 
150  while((grp = InputGroup(translationGroupName, inst++)).name() != "") {
151  if((con = GetContainer(grp)) != NULL) {
152  transGroup = TranslationTable().findGroup(translationGroupName);
153  Pvl::ConstPvlKeywordIterator it = transGroup.findKeyword("InputKey",
154  transGroup.begin(),
155  transGroup.end());
156  // Loop through potential InputKeys in the translation file group currently beginning
157  // translated.
158  while(it != transGroup.end()) {
159  const PvlKeyword &result = *it;
160  if(con->hasKeyword(result[0])) {
161  key.setName(OutputName(translationGroupName));
162 
163  for(int v = 0; v < (*con)[(result[0])].size(); v++) {
164  key.addValue(PvlTranslationTable::Translate(translationGroupName,
165  (*con)[result[0]][v]),
166  (*con)[result[0]].unit(v));
167  }
168 
169  return key;
170  }
171  it = transGroup.findKeyword("InputKey", it + 1, transGroup.end());
172  }
173  }
174  }
175 
176  return PvlKeyword(OutputName(translationGroupName),
177  PvlTranslationTable::Translate(translationGroupName, ""));
178  }
179 
180 
187  void PvlToPvlTranslationManager::Auto(Pvl &inputLabel, Pvl &outputLabel) {
188  p_fLabel = inputLabel;
189  Auto(outputLabel);
190  }
191 
192 
200  // Attempt to translate every group in the translation table
201  for(int i = 0; i < TranslationTable().groups(); i++) {
202  PvlGroup &g = TranslationTable().group(i);
203  if(IsAuto(g.name())) {
204  try {
205  PvlContainer *con = CreateContainer(g.name(), outputLabel);
206  (*con) += DoTranslation(g.name());
207  }
208  catch(IException &e) {
209  if(!IsOptional(g.name())) {
210  throw;
211  }
212  }
213  }
214  }
215  }
216 
217 
231  const PvlKeyword &PvlToPvlTranslationManager::InputKeyword(const QString translationGroupName) const {
232 
233  int instanceNumber = 0;
234  PvlKeyword inputGroupKeyword = InputGroup(translationGroupName, instanceNumber);
235  bool anInputGroupFound = false;
236 
237  while(inputGroupKeyword.name() != "") {
238  const PvlContainer *containingGroup = GetContainer(inputGroupKeyword);
239  if(containingGroup != NULL) {
240  anInputGroupFound = true;
241 
242  if(containingGroup->hasKeyword(InputKeywordName(translationGroupName))) {
243  return containingGroup->findKeyword(InputKeywordName(translationGroupName));
244  }
245  }
246 
247  instanceNumber ++;
248  inputGroupKeyword = InputGroup(translationGroupName, instanceNumber);
249  }
250 
251  if(anInputGroupFound) {
252  QString msg = "Unable to find input keyword [" + InputKeywordName(translationGroupName) +
253  "] for output name [" + translationGroupName + "] in file [" + TranslationTable().fileName() + "]";
254  throw IException(IException::Programmer, msg, _FILEINFO_);
255  }
256  else {
257  QString container = "";
258 
259  for(int i = 0; i < InputGroup(translationGroupName).size(); i++) {
260  if(i > 0) container += ",";
261 
262  container += InputGroup(translationGroupName)[i];
263  }
264 
265  QString msg = "Unable to find input group [" + container +
266  "] for output name [" + translationGroupName + "] in file [" + TranslationTable().fileName() + "]";
267  throw IException(IException::Programmer, msg, _FILEINFO_);
268  }
269  }
270 
271 
282  bool PvlToPvlTranslationManager::InputHasKeyword(const QString translationGroupName) {
283 
284  // Set the current position in the input label pvl
285  // by finding the input group corresponding to the output group
286  const PvlContainer *con;
287  int inst = 0;
288  //while ((con = GetContainer(InputGroup(translationGroupName, inst++))) != NULL) {
289  //if ((con = GetContainer (InputGroup(translationGroupName))) != NULL) {
290 
291  PvlKeyword grp;
292  while((grp = InputGroup(translationGroupName, inst++)).name() != "") {
293  if((con = GetContainer(grp)) != NULL) {
294  if(con->hasKeyword(InputKeywordName(translationGroupName))) return true;
295  }
296  }
297 
298  return false;
299  }
300 
301 
304 
305 
306  // Return the root container if "ROOT" is the ONLY thing in the list
307  if(inputGroup.size() == 1 &&
308  PvlKeyword::stringEqual(inputGroup[0], "ROOT")) {
309  return &p_fLabel;
310  }
311 
312  const PvlObject *currentObject = &p_fLabel;
313 
314  // Search for object containing our solution
315  int objectIndex;
316  for(objectIndex = 0;
317  objectIndex < inputGroup.size() - 1;
318  objectIndex ++) {
319  if(currentObject->hasObject(inputGroup[objectIndex])) {
320  currentObject = &currentObject->findObject(inputGroup[objectIndex]);
321  }
322  else {
323  return NULL;
324  }
325  }
326 
327  // Our solution can be an object or a group
328  if(currentObject->hasObject(inputGroup[objectIndex])) {
329  return &currentObject->findObject(inputGroup[objectIndex]);
330  }
331  else if(currentObject->hasGroup(inputGroup[objectIndex])) {
332  return &currentObject->findGroup(inputGroup[objectIndex]);
333  }
334  else {
335  return NULL;
336  }
337  }
338 
339 
352  PvlContainer *PvlToPvlTranslationManager::CreateContainer(const QString translationGroupName, Pvl &pvl) {
353  return LabelTranslationManager::CreateContainer(translationGroupName, pvl);
354  }
355 } // end namespace isis
Isis::PvlKeyword::name
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:98
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::PvlObject::group
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:452
Isis::PvlObject
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:61
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::PvlKeyword::addValue
void addValue(QString value, QString unit="")
Adds a value with units.
Definition: PvlKeyword.cpp:252
Isis::PvlObject::groups
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:75
Isis::PvlTranslationTable::OutputName
QString OutputName(const QString translationGroupName)
Retrieves a string containing the value of the OutputName keyword for the translation group with the ...
Definition: PvlTranslationTable.cpp:511
Isis::PvlToPvlTranslationManager::Translate
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
Definition: PvlToPvlTranslationManager.cpp:107
Isis::PvlObject::hasGroup
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:210
Isis::PvlContainer::hasKeyword
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
Definition: PvlContainer.cpp:159
Isis::PvlToPvlTranslationManager::DoTranslation
virtual PvlKeyword DoTranslation(const QString translationGroupName)
Translate the requested output name to output values using the input name and values or default value...
Definition: PvlToPvlTranslationManager.cpp:142
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::PvlToPvlTranslationManager::CreateContainer
virtual PvlContainer * CreateContainer(const QString translationGroupName, Pvl &pvl)
Create the requsted container and any containers above it and return a reference to the container.
Definition: PvlToPvlTranslationManager.cpp:352
Isis::PvlKeyword::setName
void setName(QString name)
Sets the keyword name.
Definition: PvlKeyword.cpp:120
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::PvlToPvlTranslationManager::PvlToPvlTranslationManager
PvlToPvlTranslationManager(const QString &transFile)
Constructs and initializes a TranslationManager object from given the Pvl translation file.
Definition: PvlToPvlTranslationManager.cpp:31
Isis::PvlToPvlTranslationManager::InputKeyword
virtual const PvlKeyword & InputKeyword(const QString translationGroupName) const
Returns the ith input value associated with the output name argument.
Definition: PvlToPvlTranslationManager.cpp:231
Isis::LabelTranslationManager::CreateContainer
virtual PvlContainer * CreateContainer(const QString translationGroupName, Pvl &pvl)
Creates all parent PVL containers for an output keyword.
Definition: LabelTranslationManager.cpp:100
Isis::PvlToPvlTranslationManager::~PvlToPvlTranslationManager
virtual ~PvlToPvlTranslationManager()
Destroys the TranslationManager object.
Definition: PvlToPvlTranslationManager.cpp:80
Isis::LabelTranslationManager
Allows applications to translate simple text files.
Definition: LabelTranslationManager.h:43
Isis::PvlObject::findObject
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:274
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
Isis::PvlTranslationTable::Translate
QString Translate(const QString translationGroupName, const QString inputKeyValue="") const
Translates a single output value from the given translation group name and input value.
Definition: PvlTranslationTable.cpp:216
Isis::PvlToPvlTranslationManager::p_fLabel
Pvl p_fLabel
A Pvl object for the input label file.
Definition: PvlToPvlTranslationManager.h:100
Isis::PvlObject::hasObject
bool hasObject(const QString &name) const
Returns a boolean value based on whether the object exists in the current PvlObject or not.
Definition: PvlObject.h:323
Isis::PvlTranslationTable::InputKeywordName
virtual QString InputKeywordName(const QString translationGroupName) const
Returns the input keyword name from the translation table corresponding to the output name argument.
Definition: PvlTranslationTable.cpp:360
Isis::PvlTranslationTable::InputGroup
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.
Definition: PvlTranslationTable.cpp:285
Isis::PvlTranslationTable::IsAuto
bool IsAuto(const QString translationGroupName)
Determines whether the given group should be automatically translated.
Definition: PvlTranslationTable.cpp:432
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
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::PvlTranslationTable::IsOptional
bool IsOptional(const QString translationGroupName)
Determines whether the translation group is optional.
Definition: PvlTranslationTable.cpp:457
Isis::PvlToPvlTranslationManager::GetContainer
virtual const PvlContainer * GetContainer(const PvlKeyword &inputGroup) const
Return a container from the input label according tund.
Definition: PvlToPvlTranslationManager.cpp:303
Isis::PvlToPvlTranslationManager::InputHasKeyword
virtual bool InputHasKeyword(const QString translationGroupName)
Indicates if the input keyword corresponding to the output name exists in the label.
Definition: PvlToPvlTranslationManager.cpp:282
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
Contains more than one keyword-value pair.
Definition: PvlContainer.h:49
Isis::PvlKeyword::stringEqual
static bool stringEqual(const QString &string1, const QString &string2)
Checks to see if two QStrings are equal.
Definition: PvlKeyword.cpp:535
Isis::PvlToPvlTranslationManager::Auto
void Auto(Pvl &outputLabel)
Automatically translate all the output names found in the translation table If a output name does not...
Definition: PvlToPvlTranslationManager.cpp:199
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
Isis::PvlTranslationTable::TranslationTable
Pvl & TranslationTable()
Protected accessor for pvl translation table passed into class.
Definition: PvlTranslationTable.cpp:62