12 #include "IException.h"
16 #include "PvlTranslationTable.h"
26 PvlTranslationTable::PvlTranslationTable(
FileName transFile) {
36 PvlTranslationTable::PvlTranslationTable(std::istream &istr) {
44 PvlTranslationTable::PvlTranslationTable() {
51 PvlTranslationTable::~PvlTranslationTable() {
62 Pvl &PvlTranslationTable::TranslationTable() {
74 const Pvl &PvlTranslationTable::TranslationTable()
const {
84 void PvlTranslationTable::AddTable(
const QString &transFile) {
102 void PvlTranslationTable::AddTable(std::istream &transStm) {
103 transStm >> p_trnsTbl;
107 vector< pair<QString, int> > validKeywordSizes = validKeywords();
109 for (
int i = 0; i < p_trnsTbl.groups(); i++) {
110 PvlGroup currGroup = p_trnsTbl.group(i);
113 QString message =
"Unable to find InputKey for group ["
114 + currGroup.
name() +
"] in file [" +
115 p_trnsTbl.fileName() +
"]";
116 throw IException(IException::User, message, _FILEINFO_);
119 for (
int j = 0; j < currGroup.
keywords(); j++) {
120 bool validKeyword =
false;
121 bool keywordSizeMismatch =
false;
127 !validKeyword && key < (int)validKeywordSizes.size();
131 if (currKey.
name() == validKeywordSizes[key].first) {
134 if (validKeywordSizes[key].second == -1) {
135 if (currKey.
size() > 0) {
140 else if (currKey.
size() == validKeywordSizes[key].second) {
144 keywordSizeMismatch =
true;
152 if (!keywordSizeMismatch) {
153 QString message =
"Keyword [" + currKey.
name();
154 message +=
"] is not a valid keyword.";
155 message +=
" Error in file [" + p_trnsTbl.fileName() +
"]" ;
157 throw IException(IException::User, message, _FILEINFO_);
160 QString message =
"Keyword [" + currKey.
name();
161 message +=
"] does not have the correct number of elements.";
162 message +=
" Error in file [" + p_trnsTbl.fileName() +
"]" ;
164 throw IException(IException::User, message, _FILEINFO_);
179 vector< pair<QString, int> > PvlTranslationTable::validKeywords()
const {
181 vector< pair<QString, int> > validKeywords;
182 validKeywords.push_back(pair<QString, int>(
"Translation", 2));
183 validKeywords.push_back(pair<QString, int>(
"OutputName", 1));
184 validKeywords.push_back(pair<QString, int>(
"InputGroup", -1));
185 validKeywords.push_back(pair<QString, int>(
"InputPosition", -1));
186 validKeywords.push_back(pair<QString, int>(
"OutputPosition", -1));
187 validKeywords.push_back(pair<QString, int>(
"Auto", 0));
188 validKeywords.push_back(pair<QString, int>(
"Optional", 0));
189 validKeywords.push_back(pair<QString, int>(
"InputKey", 1));
190 validKeywords.push_back(pair<QString, int>(
"InputDefault", -1));
191 validKeywords.push_back(pair<QString, int>(
"InputKeyDependencies", -1));
193 return validKeywords;
216 QString PvlTranslationTable::Translate(
const QString translationGroupName,
217 const QString inputKeyValue)
const {
219 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
222 QString tmpFValue = inputKeyValue;
223 if (tmpFValue.isEmpty()) {
224 if (translationGroup.
hasKeyword(
"InputDefault")) {
225 tmpFValue = (QString) translationGroup[
"InputDefault"];
228 QString msg =
"No value or default value to translate for ";
229 msg +=
"translation group [";
230 msg += translationGroupName;
231 msg +=
"] in file [" + p_trnsTbl.
fileName() +
"]";
232 throw IException(IException::Programmer, msg, _FILEINFO_);
238 translationGroup.
begin(),
239 translationGroup.
end());
241 while (it != translationGroup.
end()) {
245 if (QString::compare((QString) key[1], tmpFValue, Qt::CaseInsensitive) == 0) {
248 else if ((QString) key[1] ==
"*") {
249 if ((QString) key[0] ==
"*") {
257 it = translationGroup.
findKeyword(
"Translation", it + 1, translationGroup.
end());
260 QString msg =
"Unable to find a translation value for [" +
261 translationGroupName +
", " + inputKeyValue +
"] in file [" +
262 p_trnsTbl.fileName() +
"]";
264 throw IException(IException::Programmer, msg, _FILEINFO_);
285 PvlKeyword PvlTranslationTable::InputGroup(
const QString translationGroupName,
286 const int inst)
const {
288 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
293 translationGroup.
begin(),
294 translationGroup.
end());
296 int currentInstance = 0;
299 if (inst == 0 && it == translationGroup.
end()) {
305 while (it != translationGroup.
end()) {
311 if (result.
size() == 1 && result[0].contains(
",")) {
312 QString msg =
"Keyword [InputPosition] cannot have a comma [,] in ";
313 msg +=
" the value [";
316 throw IException(IException::Programmer, msg, _FILEINFO_);
321 if (currentInstance == inst) {
328 it = translationGroup.
findKeyword(
"InputPosition", it + 1, translationGroup.
end());
360 QString PvlTranslationTable::InputKeywordName(
const QString translationGroupName)
const {
362 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
364 if (translationGroup.
hasKeyword(
"InputKey"))
return translationGroup[
"InputKey"];
382 QString PvlTranslationTable::InputDefault(
const QString translationGroupName)
const {
384 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
386 if (translationGroup.
hasKeyword(
"InputDefault"))
return translationGroup[
"InputDefault"];
407 bool PvlTranslationTable::hasInputDefault(
const QString translationGroupName) {
409 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
411 if (translationGroup.
hasKeyword(
"InputDefault"))
return true;
432 bool PvlTranslationTable::IsAuto(
const QString translationGroupName) {
434 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
436 if (translationGroup.
hasKeyword(
"Auto"))
return true;
457 bool PvlTranslationTable::IsOptional(
const QString translationGroupName) {
459 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
461 if (translationGroup.
hasKeyword(
"Optional"))
return true;
483 PvlKeyword PvlTranslationTable::OutputPosition(
const QString translationGroupName) {
485 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
487 if (!translationGroup.
hasKeyword(
"OutputPosition")) {
488 QString msg =
"Unable to find translation keyword [OutputPostion] in [" +
489 translationGroupName +
"] in file [" + p_trnsTbl.fileName() +
"]";
490 throw IException(IException::Programmer, msg, _FILEINFO_);
494 return translationGroup[
"OutputPosition"];
511 QString PvlTranslationTable::OutputName(
const QString translationGroupName) {
513 const PvlGroup &translationGroup = findTranslationGroup(translationGroupName);
515 if (translationGroup.
hasKeyword(
"OutputName")) {
516 return translationGroup[
"OutputName"];
538 const PvlGroup &PvlTranslationTable::findTranslationGroup(
const QString translationGroupName)
const {
539 if (!p_trnsTbl.hasGroup(translationGroupName)) {
540 QString msg =
"Unable to find translation group [" + translationGroupName +
541 "] in file [" + p_trnsTbl.
fileName() +
"]";
542 throw IException(IException::Programmer, msg, _FILEINFO_);
545 return p_trnsTbl.findGroup(translationGroupName);