Isis 3 Programmer Reference
PvlToPvlTranslationManager.cpp
Go to the documentation of this file.
1 
23 
24 #include "IException.h"
25 #include "IString.h"
26 #include "Message.h"
27 #include "Pvl.h"
28 #include "PvlContainer.h"
29 #include "PvlGroup.h"
30 #include "PvlKeyword.h"
31 #include "PvlObject.h"
33 
34 using namespace std;
35 namespace Isis {
36 
46  PvlToPvlTranslationManager::PvlToPvlTranslationManager(const QString &transFile)
47  : LabelTranslationManager(transFile) {
48  }
49 
60  : LabelTranslationManager(transStrm) {
61  }
62 
63 
73  const QString &transFile)
74  : LabelTranslationManager(transFile) {
75  p_fLabel = inputLabel;
76  }
77 
78 
88  std::istream &transStrm)
89  : LabelTranslationManager(transStrm) {
90  p_fLabel = inputLabel;
91  }
92 
93 
96  }
97 
98 
99  void PvlToPvlTranslationManager::SetLabel(Pvl &inputLabel) {
100  p_fLabel = inputLabel;
101  }
102 
103 
122  QString PvlToPvlTranslationManager::Translate(QString translationGroupName, int findex) {
123  const PvlContainer *con;
124  int inst = 0;
125  PvlKeyword grp;
126 
127  while((grp = InputGroup(translationGroupName, inst++)).name() != "") {
128  if((con = GetContainer(grp)) != NULL) {
129  if(con->hasKeyword(InputKeywordName(translationGroupName))) {
130  return PvlTranslationTable::Translate(translationGroupName,
131  (*con)[InputKeywordName(translationGroupName)][findex]);
132  }
133  }
134  }
135 
136  return PvlTranslationTable::Translate(translationGroupName);
137  }
138 
139 
157  PvlKeyword PvlToPvlTranslationManager::DoTranslation(const QString translationGroupName) {
158  const PvlContainer *con = NULL;
159  PvlKeyword key;
160 
161  int inst = 0;
162  PvlGroup transGroup;
163  PvlKeyword grp;
164 
165  while((grp = InputGroup(translationGroupName, inst++)).name() != "") {
166  if((con = GetContainer(grp)) != NULL) {
167  transGroup = TranslationTable().findGroup(translationGroupName);
168  Pvl::ConstPvlKeywordIterator it = transGroup.findKeyword("InputKey",
169  transGroup.begin(),
170  transGroup.end());
171  // Loop through potential InputKeys in the translation file group currently beginning
172  // translated.
173  while(it != transGroup.end()) {
174  const PvlKeyword &result = *it;
175  if(con->hasKeyword(result[0])) {
176  key.setName(OutputName(translationGroupName));
177 
178  for(int v = 0; v < (*con)[(result[0])].size(); v++) {
179  key.addValue(PvlTranslationTable::Translate(translationGroupName,
180  (*con)[result[0]][v]),
181  (*con)[result[0]].unit(v));
182  }
183 
184  return key;
185  }
186  it = transGroup.findKeyword("InputKey", it + 1, transGroup.end());
187  }
188  }
189  }
190 
191  return PvlKeyword(OutputName(translationGroupName),
192  PvlTranslationTable::Translate(translationGroupName, ""));
193  }
194 
195 
202  void PvlToPvlTranslationManager::Auto(Pvl &inputLabel, Pvl &outputLabel) {
203  p_fLabel = inputLabel;
204  Auto(outputLabel);
205  }
206 
207 
215  // Attempt to translate every group in the translation table
216  for(int i = 0; i < TranslationTable().groups(); i++) {
217  PvlGroup &g = TranslationTable().group(i);
218  if(IsAuto(g.name())) {
219  try {
220  PvlContainer *con = CreateContainer(g.name(), outputLabel);
221  (*con) += DoTranslation(g.name());
222  }
223  catch(IException &e) {
224  if(!IsOptional(g.name())) {
225  throw;
226  }
227  }
228  }
229  }
230  }
231 
232 
246  const PvlKeyword &PvlToPvlTranslationManager::InputKeyword(const QString translationGroupName) const {
247 
248  int instanceNumber = 0;
249  PvlKeyword inputGroupKeyword = InputGroup(translationGroupName, instanceNumber);
250  bool anInputGroupFound = false;
251 
252  while(inputGroupKeyword.name() != "") {
253  const PvlContainer *containingGroup = GetContainer(inputGroupKeyword);
254  if(containingGroup != NULL) {
255  anInputGroupFound = true;
256 
257  if(containingGroup->hasKeyword(InputKeywordName(translationGroupName))) {
258  return containingGroup->findKeyword(InputKeywordName(translationGroupName));
259  }
260  }
261 
262  instanceNumber ++;
263  inputGroupKeyword = InputGroup(translationGroupName, instanceNumber);
264  }
265 
266  if(anInputGroupFound) {
267  QString msg = "Unable to find input keyword [" + InputKeywordName(translationGroupName) +
268  "] for output name [" + translationGroupName + "] in file [" + TranslationTable().fileName() + "]";
270  }
271  else {
272  QString container = "";
273 
274  for(int i = 0; i < InputGroup(translationGroupName).size(); i++) {
275  if(i > 0) container += ",";
276 
277  container += InputGroup(translationGroupName)[i];
278  }
279 
280  QString msg = "Unable to find input group [" + container +
281  "] for output name [" + translationGroupName + "] in file [" + TranslationTable().fileName() + "]";
283  }
284  }
285 
286 
297  bool PvlToPvlTranslationManager::InputHasKeyword(const QString translationGroupName) {
298 
299  // Set the current position in the input label pvl
300  // by finding the input group corresponding to the output group
301  const PvlContainer *con;
302  int inst = 0;
303  //while ((con = GetContainer(InputGroup(translationGroupName, inst++))) != NULL) {
304  //if ((con = GetContainer (InputGroup(translationGroupName))) != NULL) {
305 
306  PvlKeyword grp;
307  while((grp = InputGroup(translationGroupName, inst++)).name() != "") {
308  if((con = GetContainer(grp)) != NULL) {
309  if(con->hasKeyword(InputKeywordName(translationGroupName))) return true;
310  }
311  }
312 
313  return false;
314  }
315 
316 
319 
320 
321  // Return the root container if "ROOT" is the ONLY thing in the list
322  if(inputGroup.size() == 1 &&
323  PvlKeyword::stringEqual(inputGroup[0], "ROOT")) {
324  return &p_fLabel;
325  }
326 
327  const PvlObject *currentObject = &p_fLabel;
328 
329  // Search for object containing our solution
330  int objectIndex;
331  for(objectIndex = 0;
332  objectIndex < inputGroup.size() - 1;
333  objectIndex ++) {
334  if(currentObject->hasObject(inputGroup[objectIndex])) {
335  currentObject = &currentObject->findObject(inputGroup[objectIndex]);
336  }
337  else {
338  return NULL;
339  }
340  }
341 
342  // Our solution can be an object or a group
343  if(currentObject->hasObject(inputGroup[objectIndex])) {
344  return &currentObject->findObject(inputGroup[objectIndex]);
345  }
346  else if(currentObject->hasGroup(inputGroup[objectIndex])) {
347  return &currentObject->findGroup(inputGroup[objectIndex]);
348  }
349  else {
350  return NULL;
351  }
352  }
353 
354 
367  PvlContainer *PvlToPvlTranslationManager::CreateContainer(const QString translationGroupName, Pvl &pvl) {
368  return LabelTranslationManager::CreateContainer(translationGroupName, pvl);
369  }
370 } // end namespace isis
PvlKeywordIterator end()
Return the ending iterator.
Definition: PvlContainer.h:208
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
virtual PvlContainer * CreateContainer(const QString translationGroupName, Pvl &pvl)
Create the requsted container and any containers above it and return a reference to the container...
Contains more than one keyword-value pair.
Definition: PvlContainer.h:63
virtual ~PvlToPvlTranslationManager()
Destroys the TranslationManager object.
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition: PvlObject.h:286
PvlKeywordIterator begin()
Return the beginning iterator.
Definition: PvlContainer.h:192
void setName(QString name)
Sets the keyword name.
Definition: PvlKeyword.cpp:136
Namespace for the standard library.
virtual PvlKeyword DoTranslation(const QString translationGroupName)
Translate the requested output name to output values using the input name and values or default value...
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:222
virtual PvlContainer * CreateContainer(const QString translationGroupName, Pvl &pvl)
Creates all parent PVL containers for an output keyword.
static bool stringEqual(const QString &string1, const QString &string2)
Checks to see if two QStrings are equal.
Definition: PvlKeyword.cpp:551
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
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:335
virtual bool InputHasKeyword(const QString translationGroupName)
Indicates if the input keyword corresponding to the output name exists in the label.
QString Translate(const QString translationGroupName, const QString inputKeyValue="") const
Translates a single output value from the given translation group name and input value.
QString name() const
Returns the container name.
Definition: PvlContainer.h:77
virtual QString InputKeywordName(const QString translationGroupName) const
Returns the input keyword name from the translation table corresponding to the output name argument...
QString OutputName(const QString translationGroupName)
Retrieves a string containing the value of the OutputName keyword for the translation group with the ...
int size() const
Returns the number of values stored in this keyword.
Definition: PvlKeyword.h:141
Pvl p_fLabel
A Pvl object for the input label file.
Pvl & TranslationTable()
Protected accessor for pvl translation table passed into class.
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A single keyword-value pair.
Definition: PvlKeyword.h:98
PvlToPvlTranslationManager(const QString &transFile)
Constructs and initializes a TranslationManager object from given the Pvl translation file...
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
Container for cube-like labels.
Definition: Pvl.h:135
virtual const PvlContainer * GetContainer(const PvlKeyword &inputGroup) const
Return a container from the input label according tund.
virtual const PvlKeyword & InputKeyword(const QString translationGroupName) const
Returns the ith input value associated with the output name argument.
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:423
QString fileName() const
Returns the filename used to initialise the Pvl object.
Definition: PvlContainer.h:246
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:114
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool IsOptional(const QString translationGroupName)
Determines whether the translation group is optional.
Allows applications to translate simple text files.
void Auto(Pvl &outputLabel)
Automatically translate all the output names found in the translation table If a output name does not...
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
Definition: PvlContainer.h:174
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
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.
void addValue(QString value, QString unit="")
Adds a value with units.
Definition: PvlKeyword.cpp:268