7#include "LabelTranslationManager.h"
15#include "IException.h"
19#include "PvlContainer.h"
21#include "PvlKeyword.h"
23#include "XmlToPvlTranslationManager.h"
71 const QString &transFile)
88 std::istream &transStrm)
121 validKeywords.push_back(pair<QString, int>(
"InputKeyAttribute", -1));
122 validKeywords.push_back(pair<QString, int>(
"InputKeyDependencies", -1));
167 QString msg =
"Cannot translate value at index [" +
toString(index) +
168 "]. Xml files can only store a single value in each element.";
175 transGroup = transTable.
findGroup(translationGroupName);
178 QString msg =
"Unable to retrieve translation group from translation table.";
185 inputPosition = transGroup[
"InputPosition"];
188 QString msg =
"Unable to retrieve [InputPosition] keyword from "
189 "translation group.";
192 QString inputParentName = inputPosition[inputPosition.
size() - 1];
197 inputKey = transGroup[
"InputKey"][0];
200 QString msg =
"Unable to retrieve [InputKey] keyword from "
201 "translation group.";
204 QString attributeName;
205 if (transGroup.
hasKeyword(
"InputKeyAttribute")) {
206 attributeName = transGroup[
"InputKeyAttribute"][0];
211 if (transGroup.
hasKeyword(
"InputKeyDependencies")) {
212 keyDependencies = transGroup[
"InputKeyDependencies"];
216 bool isDebug = transGroup.
hasKeyword(
"Debug");
220 cout << endl <<
" ==================== " << endl;
221 cout << endl <<
"Translating output keyword: " << translationGroupName << endl;
222 cout << endl <<
"Translation group:" << endl;
223 cout << transGroup << endl << endl;
227 QDomElement inputParentElement =
m_xmlLabel.documentElement();
230 cout << endl <<
"Finding input element:" << endl << endl;
231 cout << inputParentElement.tagName() << endl;
238 QDomElement oldInputParentElement = inputParentElement;
240 while(it != transGroup.
end()) {
242 inputParentElement = oldInputParentElement;
244 for (
int i = 0; i < inputPosition.
size(); i++) {
245 childName = inputPosition[i];
246 inputParentElement = inputParentElement.firstChildElement(childName);
247 if(inputParentElement.isNull()) {
252 cout << indent << inputParentElement.tagName() << endl;
255 if (!inputParentElement.isNull()) {
258 it = transGroup.
findKeyword(
"InputPosition", it + 1, transGroup.
end());
261 if (inputParentElement.isNull()) {
264 cout << endl <<
"Could not traverse input position, " <<
265 "using default value: " <<
271 QString msg =
"Failed traversing input position. [" +
272 inputPosition.
name() +
"] element does not have a child element named [" +
278 QDomElement inputKeyElement = inputParentElement.firstChildElement(inputKey);
281 cout << indent << inputKeyElement.tagName() << endl;
285 while ( !inputParentElement.isNull() &&
288 cout << endl <<
"Dependencies failed, checking next candidate." << endl;
291 inputKeyElement = inputKeyElement.nextSiblingElement(inputKey);
293 while ( inputKeyElement.isNull() ) {
294 inputParentElement = inputParentElement.nextSiblingElement(inputParentName);
296 if ( inputParentElement.isNull() ) {
299 inputKeyElement = inputParentElement.firstChildElement(inputKey);
304 if ( inputParentElement.isNull() ) {
307 cout << endl <<
"No input value found, using default value: " <<
313 QString msg =
"Could not find an input or default value that fits the given input "
314 "keyword dependencies.";
320 QString inputValue = inputKeyElement.text();
322 if (attributeName.size() > 0) {
323 if ( inputKeyElement.hasAttribute(attributeName) ) {
324 inputValue = inputKeyElement.attribute(attributeName);
328 cout << endl <<
"No input value found, using default value: " <<
334 QString msg =
"Input element [" + inputKeyElement.tagName() +
335 "] does not have an attribute named [" +
336 attributeName +
"].";
341 cout << endl <<
"Translating input value: " << inputValue << endl;
346 QString msg =
"Failed to translate output value for [" + translationGroupName +
"].";
377 cout << endl <<
"Testing dependencies:" << endl;
379 for (
int i = 0; i < dependencies.
size(); i++) {
382 if (specification.size() != 3) {
386 cout << endl <<
"Testing dependency number " <<
toString(i+1) << endl;
387 cout <<
" Specification: " << dependencies[i] << endl;
389 cout <<
" Dependency type: " << specification[0] << endl;
390 cout <<
" Dependency name: " << specification[1] << endl;
391 cout <<
" Dependency value: " << specification[2] << endl;
393 if (specification[0] ==
"att") {
394 if ( element.hasAttributes() ) {
395 QDomNamedNodeMap atts = element.attributes();
396 QString attributeValue = atts.namedItem(specification[1]).nodeValue();
399 cout <<
" Attribute name: " << atts.namedItem(specification[1]).nodeName();
400 cout <<
" Attribute value: " << attributeValue << endl;
402 if ( attributeValue != specification[2] ) {
414 else if (specification[0] ==
"tag") {
415 QDomElement candidateSibling = element.parentNode().firstChildElement(specification[1]);
416 QString siblingValue = candidateSibling.text();
419 cout <<
" Tag name: " << candidateSibling.tagName() << endl;
420 cout <<
" Tag value: " << siblingValue << endl;
422 if (siblingValue != specification[2] ) {
430 QString msg =
"Parsing error, dependency type [" + specification[0] +
431 "] is not [att] or [tag].";
465 QFile xmlFile(xmlFileName.
expanded());
466 if ( !xmlFile.open(QIODevice::ReadOnly) ) {
467 QString msg =
"Could not open label file [" + xmlFileName.
expanded() +
474 if ( !
m_xmlLabel.setContent(&xmlFile,
false, &errmsg, &errline, &errcol) ) {
476 QString msg =
"XML read/parse error in file [" + xmlFileName.
expanded()
478 +
"], message: " + errmsg;
File name manipulation and expansion.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
@ Unknown
A type of error that cannot be classified as any of the other error types.
LabelTranslationManager()
Constructs a default LabelTranslationManager.
virtual QStringList parseSpecification(QString specification) const
Parses and validates a dependency specification.
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
PvlKeywordIterator begin()
Return the beginning iterator.
QList< PvlKeyword >::const_iterator ConstPvlKeywordIterator
The const keyword iterator.
PvlKeywordIterator end()
Return the ending iterator.
PvlKeyword & findKeyword(const QString &name)
Find a keyword with a specified name.
Contains multiple PvlContainers.
Container for cube-like labels.
A single keyword-value pair.
QString name() const
Returns the keyword name.
int size() const
Returns the number of values stored in this keyword.
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
virtual std::vector< std::pair< QString, int > > validKeywords() const
Returns a vector of valid keyword names and their sizes.
QString Translate(const QString translationGroupName, const QString inputKeyValue="") const
Translates a single output value from the given translation group name and input value.
bool hasInputDefault(const QString translationGroupName)
Determines whether the given group has a default input value.
void AddTable(std::istream &transStm)
Adds the contents of a translation table to the searchable groups/keys Also performs a verification,...
Pvl & TranslationTable()
Protected accessor for pvl translation table passed into class.
QString InputDefault(const QString translationGroupName) const
Returns the input default value from the translation table corresponding to the output name argument.
QDomDocument m_xmlLabel
The contents of the xml label.
virtual std::vector< std::pair< QString, int > > validKeywords() const
Returns a vector of valid keyword names and their sizes.
bool checkDependencies(QDomElement element, PvlKeyword dependencies, bool isDebug) const
Checks if a element in the xml label satisfies a list of dependencies.
virtual QString Translate(QString translationGroupName, int findex=0)
Returns a translated value.
virtual ~XmlToPvlTranslationManager()
Destroys the XmlToPvlTranslationManager object.
void Auto(FileName &inputLabel, Pvl &outputLabel)
Automatically translate all the output names flagged with the Auto keyword in the translation table a...
XmlToPvlTranslationManager(const QString &transFile)
Constructs and initializes an XmlToPvlTranslationManager object from the given Pvl translation file.
void parseFile(const FileName &xmlFileName)
Opens, parses, and internalizes an Xml label file.
void SetLabel(FileName &inputLabel)
Reads an Xml label file and internalizes it for translation.
This is free and unencumbered software released into the public domain.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Namespace for the standard library.