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
19using namespace std;
20namespace Isis {
21
34
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
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++) {
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 exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Allows applications to translate simple text files.
virtual PvlContainer * CreateContainer(const QString translationGroupName, Pvl &pvl)
Creates all parent PVL containers for an output keyword.
Contains more than one keyword-value pair.
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
A single keyword-value pair.
Definition PvlKeyword.h:87
void setName(QString name)
Sets the keyword name.
static bool stringEqual(const QString &string1, const QString &string2)
Checks to see if two QStrings are equal.
void addValue(QString value, QString unit="")
Adds a value with units.
Contains Pvl Groups and Pvl Objects.
Definition PvlObject.h:61
PvlGroup & group(const int index)
Return the group at the specified index.
int groups() const
Returns the number of groups contained.
Definition PvlObject.h:75
PvlObjectIterator findObject(const QString &name, PvlObjectIterator beg, PvlObjectIterator end)
Find the index of object with a specified name, between two indexes.
Definition PvlObject.h:276
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition PvlObject.h:129
virtual PvlContainer * CreateContainer(const QString translationGroupName, Pvl &pvl)
Create the requsted container and any containers above it and return a reference to the container.
virtual ~PvlToPvlTranslationManager()
Destroys the TranslationManager object.
virtual PvlKeyword DoTranslation(const QString translationGroupName)
Translate the requested output name to output values using the input name and values or default value...
virtual const PvlKeyword & InputKeyword(const QString translationGroupName) const
Returns the ith input value associated with the output name argument.
Pvl p_fLabel
A Pvl object for the input label file.
virtual const PvlContainer * GetContainer(const PvlKeyword &inputGroup) const
Return a container from the input label according tund.
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
virtual bool InputHasKeyword(const QString translationGroupName)
Indicates if the input keyword corresponding to the output name exists in the label.
PvlToPvlTranslationManager(const QString &transFile)
Constructs and initializes a TranslationManager object from given the Pvl translation file.
void Auto(Pvl &outputLabel)
Automatically translate all the output names found in the translation table If a output name does not...
bool IsOptional(const QString translationGroupName)
Determines whether the translation group is optional.
QString Translate(const QString translationGroupName, const QString inputKeyValue="") const
Translates a single output value from the given translation group name and input value.
QString OutputName(const QString translationGroupName)
Retrieves a string containing the value of the OutputName keyword for the translation group with the ...
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.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.