Isis 3.0 Programmer Reference
Back | Home
PvlTranslationManager.cpp
Go to the documentation of this file.
1 
23 #include "IString.h"
24 #include "Message.h"
25 #include "IException.h"
26 #include "PvlTranslationManager.h"
27 
28 using namespace std;
29 namespace Isis {
30 
31  PvlTranslationManager::PvlTranslationManager(const QString &transFile) {
32  AddTable(transFile);
33  }
34 
43  PvlTranslationManager::PvlTranslationManager(Isis::Pvl &inputLabel,
44  const QString &transFile) {
45  p_fLabel = inputLabel;
46 
47  // Internalize the translation table
48  AddTable(transFile);
49  }
50 
59  PvlTranslationManager::PvlTranslationManager(Isis::Pvl &inputLabel,
60  std::istream &transStrm) {
61  p_fLabel = inputLabel;
62 
63  // Internalize the translation table
64  AddTable(transStrm);
65  }
66 
82  QString PvlTranslationManager::Translate(QString nName, int findex) {
83  const Isis::PvlContainer *con;
84  int inst = 0;
85  PvlKeyword grp;
86 
87  while((grp = InputGroup(nName, inst++)).name() != "") {
88  if((con = GetContainer(grp)) != NULL) {
89  if(con->hasKeyword(InputKeywordName(nName))) {
90  return PvlTranslationTable::Translate(nName,
91  (*con)[InputKeywordName(nName)][findex]);
92  }
93  }
94  }
95 
97  }
98 
108  Isis::PvlKeyword PvlTranslationManager::DoTranslation(
109  const QString nName) {
110  const Isis::PvlContainer *con = NULL;
111  Isis::PvlKeyword key;
112 
113  int inst = 0;
114  PvlKeyword grp;
115 
116  while((grp = InputGroup(nName, inst++)).name() != "") {
117  if((con = GetContainer(grp)) != NULL) {
118  if(con->hasKeyword(InputKeywordName(nName))) {
119  key.setName(OutputName(nName));
120 
121  for(int v = 0; v < (*con)[(InputKeywordName(nName))].size(); v++) {
123  (*con)[InputKeywordName(nName)][v]),
124  (*con)[InputKeywordName(nName)].unit(v));
125  }
126 
127  return key;
128  }
129  }
130  }
131 
132  return Isis::PvlKeyword(OutputName(nName),
133  PvlTranslationTable::Translate(nName, ""));
134  }
135 
136 
137 
138  // Automatically translate all the output names found in the translation table
139  // If a output name does not translate an error will be thrown by one
140  // of the support members
141  // Store the translated key, value pairs in the argument pvl
142  void PvlTranslationManager::Auto(Isis::Pvl &outputLabel) {
143  // Attempt to translate every group in the translation table
144  for(int i = 0; i < TranslationTable().groups(); i++) {
145  Isis::PvlGroup &g = TranslationTable().group(i);
146  if(IsAuto(g.name())) {
147  try {
148  Isis::PvlContainer *con = CreateContainer(g.name(), outputLabel);
149  (*con) += PvlTranslationManager::DoTranslation(g.name());
150  }
151  catch(IException &e) {
152  if(!IsOptional(g.name())) {
153  throw;
154  }
155  }
156  }
157  }
158  }
159 
169  const PvlKeyword &PvlTranslationManager::InputKeyword(
170  const QString nName) const {
171 
172  int instanceNumber = 0;
173  PvlKeyword inputGroupKeyword = InputGroup(nName, instanceNumber);
174  bool anInputGroupFound = false;
175 
176  while(inputGroupKeyword.name() != "") {
177  const PvlContainer *containingGroup = GetContainer(inputGroupKeyword);
178  if(containingGroup != NULL) {
179  anInputGroupFound = true;
180 
181  if(containingGroup->hasKeyword(InputKeywordName(nName))) {
182  return containingGroup->findKeyword(InputKeywordName(nName));
183  }
184  }
185 
186  instanceNumber ++;
187  inputGroupKeyword = InputGroup(nName, instanceNumber);
188  }
189 
190  if(anInputGroupFound) {
191  QString msg = "Unable to find input keyword [" + InputKeywordName(nName) +
192  "] for output name [" + nName + "] in file [" + TranslationTable().fileName() + "]";
193  throw IException(IException::Programmer, msg, _FILEINFO_);
194  }
195  else {
196  QString container = "";
197 
198  for(int i = 0; i < InputGroup(nName).size(); i++) {
199  if(i > 0) container += ",";
200 
201  container += InputGroup(nName)[i];
202  }
203 
204  QString msg = "Unable to find input group [" + container +
205  "] for output name [" + nName + "] in file [" + TranslationTable().fileName() + "]";
206  throw IException(IException::Programmer, msg, _FILEINFO_);
207  }
208  }
209 
210 
217  bool PvlTranslationManager::InputHasKeyword(const QString nName) {
218 
219  // Set the current position in the input label pvl
220  // by finding the input group corresponding to the output group
221  const Isis::PvlContainer *con;
222  int inst = 0;
223  //while ((con = GetContainer(InputGroup(nName, inst++))) != NULL) {
224  //if ((con = GetContainer (InputGroup(nName))) != NULL) {
225 
226  PvlKeyword grp;
227  while((grp = InputGroup(nName, inst++)).name() != "") {
228  if((con = GetContainer(grp)) != NULL) {
229  if(con->hasKeyword(InputKeywordName(nName))) return true;
230  }
231  }
232 
233  return false;
234  }
235 
236  /*
237  * Indicates if the input group corresponding to the output name exists in
238  * the label
239  *
240  * @param nName The output name used to identify the input keyword.
241 
242  bool PvlTranslationManager::InputHasGroup (const QString nName) {
243 
244  if (GetContainer (InputGroup(nName)) != NULL) {
245  return true;
246  }
247 
248  return false;
249  }
250  */
251 
252  // Return a container from the input label according tund
253  const Isis::PvlContainer *PvlTranslationManager::GetContainer(
254  const PvlKeyword &inputGroup) const {
255 
256 
257  // Return the root container if "ROOT" is the ONLY thing in the list
258  if(inputGroup.size() == 1 &&
259  PvlKeyword::stringEqual(inputGroup[0], "ROOT")) {
260  return &p_fLabel;
261  }
262 
263  const Isis::PvlObject *currentObject = &p_fLabel;
264 
265  // Search for object containing our solution
266  int objectIndex;
267  for(objectIndex = 0;
268  objectIndex < inputGroup.size() - 1;
269  objectIndex ++) {
270  if(currentObject->hasObject(inputGroup[objectIndex])) {
271  currentObject = &currentObject->findObject(inputGroup[objectIndex]);
272  }
273  else {
274  return NULL;
275  }
276  }
277 
278  // Our solution can be an object or a group
279  if(currentObject->hasObject(inputGroup[objectIndex])) {
280  return &currentObject->findObject(inputGroup[objectIndex]);
281  }
282  else if(currentObject->hasGroup(inputGroup[objectIndex])) {
283  return &currentObject->findGroup(inputGroup[objectIndex]);
284  }
285  else {
286  return NULL;
287  }
288  }
289 
290 
291  // Create the requsted container and any containers above it and
292  // return a reference to the container
293  // list is an Isis::PvlKeyword with an array of container types an their names
294  Isis::PvlContainer *PvlTranslationManager::CreateContainer(const QString nName,
295  Isis::Pvl &pvl) {
296 
297  // Get the array of Objects/Groups from the OutputName keyword
298  Isis::PvlKeyword np = OutputPosition(nName);
299 
300  Isis::PvlObject *obj = &pvl;
301 
302  // Look at every pair in the output position
303  for(int c = 0; c < np.size(); c += 2) {
304  // If this pair is an object
305  if(np[c].toUpper() == "OBJECT") {
306  // If the object doesn't exist create it
307  if(!obj->hasObject(np[c+1])) {
308  obj->addObject(np[c+1]);
309  }
310  obj = &(obj->findObject(np[c+1]));
311  }
312  // If this pair is a group
313  else if(np[c].toUpper() == "GROUP") {
314  // If the group doesn't exist create it
315  if(!obj->hasGroup(np[c+1])) {
316  obj->addGroup(np[c+1]);
317  }
318  return (Isis::PvlContainer *) & (obj->findGroup(np[c+1]));
319 
320  }
321  }
322 
323  return (Isis::PvlContainer *) obj;
324  }
325 } // end namespace isis
int size() const
Returns the number of values stored in this keyword.
Definition: PvlKeyword.h:141
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
Contains more than one keyword-value pair.
Definition: PvlContainer.h:64
QString Translate(const QString nName, const QString fValue="") const
Translates the output name and input value.
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
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
void setName(QString name)
Sets the keyword name.
Definition: PvlKeyword.cpp:136
void addObject(const PvlObject &object)
Add a PvlObject.
Definition: PvlObject.h:319
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
Container for cube-like labels.
Definition: Pvl.h:135
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
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
Isis exception class.
Definition: IException.h:99
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
Contains Pvl Groups and Pvl Objects.
Definition: PvlObject.h:74
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
QString name() const
Returns the container name.
Definition: PvlContainer.h:78
void addValue(QString value, QString unit="")
Adds a value with units.
Definition: PvlKeyword.cpp:268

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:24