11 #include "PvlContainer.h"
14 #include "IException.h"
16 #include "PvlFormat.h"
26 PvlContainer::PvlContainer(
const QString &type) {
37 PvlContainer::PvlContainer(
const QString &type,
const QString &name) {
51 void PvlContainer::init() {
53 m_formatTemplate = NULL;
65 QString msg =
"PVL Keyword [" + name +
"] does not exist in [" +
66 type() +
" = " + this->name() +
"]";
67 if(m_filename.size() > 0) msg +=
" in file [" + m_filename +
"]";
68 throw IException(IException::Unknown, msg, _FILEINFO_);
83 QString msg =
"PVL Keyword [" + name +
"] does not exist in [" +
84 type() +
" = " + this->name() +
"]";
85 if(m_filename.size() > 0) msg +=
" in file [" + m_filename +
"]";
86 throw IException(IException::Unknown, msg, _FILEINFO_);
97 void PvlContainer::deleteKeyword(
const QString &name) {
100 QString msg =
"PVL Keyword [" + name +
"] does not exist in [" +
101 type() +
" = " + this->name() +
"]";
102 if(m_filename.size() > 0) msg +=
" in file [" + m_filename +
"]";
103 throw IException(IException::Unknown, msg, _FILEINFO_);
106 m_keywords.erase(key);
115 void PvlContainer::deleteKeyword(
const int index) {
116 if(index >= (
int)m_keywords.size() || index < 0) {
117 QString msg =
"The specified index is out of bounds in PVL [" +
118 type() +
" = " + name() +
"]";
119 if(m_filename.size() > 0) msg +=
" in file [" + m_filename +
"]";
120 throw IException(IException::Unknown, msg, _FILEINFO_);
124 for(
int i = 0; i < index; i++) key++;
126 m_keywords.erase(key);
136 bool PvlContainer::cleanDuplicateKeywords() {
137 bool keywordDeleted =
false;
139 for(
int index = 0; index < m_keywords.size(); index ++) {
143 if(current == *key) {
144 key = m_keywords.erase(key);
145 keywordDeleted =
true;
150 return keywordDeleted;
159 bool PvlContainer::hasKeyword(
const QString &name)
const {
161 if(key == end())
return false;
173 if(index < 0 || index >= (
int)m_keywords.size()) {
174 QString msg = Message::ArraySubscriptNotInRange(index);
175 throw IException(IException::Programmer, msg, _FILEINFO_);
177 return *(m_keywords.begin() + index);
188 if(index < 0 || index >= (
int)m_keywords.size()) {
189 QString msg = Message::ArraySubscriptNotInRange(index);
190 throw IException(IException::Programmer, msg, _FILEINFO_);
192 return *(m_keywords.begin() + index);
205 m_keywords.push_back(key);
207 else if(hasKeyword(key.
name())) {
212 m_keywords.push_back(key);
235 return (m_keywords.insert(pos, key));
248 bool removeFormatter =
false;
249 if(container.format() == NULL) {
251 removeFormatter =
true;
255 if(container.hasFormatTemplate()) outTemplate = *(container.formatTemplate());
261 for(
int i = 0; i < outTemplate.
keywords(); i++) {
262 if(outTemplate[i].isNamed(
"Isis:PvlTemplate:File")) {
263 QString filename = outTemplate[i];
266 QString message =
"Could not open the template file [" + filename +
"]";
267 throw IException(IException::Io, message, _FILEINFO_);
271 for(
int j = 0; j < include.
keywords(); j++) {
282 outTemplate = newTemp;
286 for(
int i = 0; i < container.
keywords(); i++) {
287 if(container[i].name().length() > width) width = container[i].
name().length();
294 for(
int i = 0; i < outTemplate.
keywords(); i++) {
295 for(
int j = 0; j < container.
keywords(); j++) {
296 if(outTemplate[i].name() != container[j].name())
continue;
297 container[j].setIndent(container.indent());
298 container[j].setWidth(width);
299 container[j].setFormat(container.format());
301 if(outTemplate[i].comments() + container[j].comments() > 0) os << container.format()->formatEOL();
302 if(outTemplate[i].comments() > 0) {
303 for(
int k = 0; k < outTemplate[i].comments(); k++) {
304 for(
int l = 0; l < outTemplate[i].indent() + container[j].indent(); l++) os <<
" ";
305 os << outTemplate[i].comment(k) << container.format()->formatEOL();
309 container[j].setFormat(NULL);
310 container[j].setIndent(0);
311 container[j].setWidth(0);
312 if(++numKeywords < container.keywords()) {
314 os << container.format()->formatEOL();
320 for(
int i = 0; i < container.
keywords(); i++) {
322 container[i].setIndent(container.indent());
323 container[i].setWidth(width);
324 container[i].setFormat(container.format());
326 container[i].setFormat(NULL);
327 container[i].setIndent(0);
328 container[i].setWidth(0);
329 if(++numKeywords < container.keywords()) {
330 if(i + 1 < container.keywords() && container[i+1].comments() > 0) os << container.format()->formatEOL();
331 os << container.format()->formatEOL();
335 if(removeFormatter) {
336 delete container.format();
337 container.setFormat(NULL);
355 return find(beg, end, temp);
370 return find(beg, end, temp);
379 m_formatTemplate = other.m_formatTemplate;
397 int iTmplKeySize = keywords();
398 for(
int i=0; i<iTmplKeySize; i++) {
400 QString sKeyName = pvlTmplKwrd.
name();
401 bool bKwrdFound =
false;
405 if(sKeyName.contains(
"__Required") || sKeyName.contains(
"__Repeated") ||
406 sKeyName.contains(
"__Range") || sKeyName.contains(
"__Value") ||
407 sKeyName.contains(
"__Type")) {
413 QString sTmplKwrdRange = sKeyName +
"__Range";
414 QString sTmplKwrdValue = sKeyName +
"__Value";
415 QString sTmplKwrdType = sKeyName +
"__Type";
417 PvlKeyword pvlTmplKwrdRange, pvlTmplKwrdValue;
420 if(hasKeyword(sTmplKwrdType)) {
421 sType = findKeyword(sTmplKwrdType)[0];
424 if(hasKeyword(sTmplKwrdRange)) {
425 pvlTmplKwrdRange = findKeyword(sTmplKwrdRange);
429 else if(hasKeyword(sTmplKwrdValue)) {
430 pvlTmplKwrdValue = findKeyword(sTmplKwrdValue);
441 QString sOption = sKeyName +
"__Required";
442 if(hasKeyword(sOption)) {
443 PvlKeyword pvlKeyOption = findKeyword(sOption);
444 if(pvlKeyOption[0] ==
"true") {
449 if (bKwrdFound ==
false) {
450 QString sErrMsg =
"Keyword \"" + sKeyName +
"\" Not Found in the Template File\n";
451 throw IException(IException::User, sErrMsg, _FILEINFO_);
455 validateRepeatOption(pvlTmplKwrd, pPvlCont);
472 QString sTmplKeyName = pPvlTmplKwrd.
name();
475 QString sType = sTmplKeyName +
"__Type";
476 QString sValueType =
"";
477 if(hasKeyword(sType)) {
478 sValueType = findKeyword(sType)[0];
480 QString sRepeatOption = sTmplKeyName +
"__Repeated";
482 if(hasKeyword(sRepeatOption)) {
483 PvlKeyword pvlKeyOption = findKeyword(sRepeatOption);
484 if(pvlKeyOption[0] ==
"true") {
490 for(
int j=(iKeySize-1); j>=0; j--) {
492 QString sKeyName = pvlKwrd.
name();
493 if(sTmplKeyName == sKeyName) {