2010-03-27 04:22:43 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file uavobjectparser.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief Parses XML files and extracts object information.
|
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uavobjectparser.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
UAVObjectParser::UAVObjectParser()
|
|
|
|
{
|
2011-01-13 03:26:00 +01:00
|
|
|
fieldTypeStrXML << "int8" << "int16" << "int32" << "uint8"
|
|
|
|
<< "uint16" << "uint32" <<"float" << "enum";
|
2010-05-14 06:32:30 +02:00
|
|
|
|
2011-01-13 03:26:00 +01:00
|
|
|
updateModeStrXML << "periodic" << "onchange" << "manual" << "never";
|
2010-04-07 05:11:13 +02:00
|
|
|
|
2011-01-13 03:26:00 +01:00
|
|
|
accessModeStr << "ACCESS_READWRITE" << "ACCESS_READONLY";
|
2010-04-01 04:01:37 +02:00
|
|
|
|
2010-12-23 17:01:31 +01:00
|
|
|
fieldTypeNumBytes << int(1) << int(2) << int(4) <<
|
|
|
|
int(1) << int(2) << int(4) <<
|
|
|
|
int(4) << int(1);
|
|
|
|
|
2011-01-13 03:26:00 +01:00
|
|
|
accessModeStrXML << "readwrite" << "readonly";
|
2011-07-15 18:08:47 +02:00
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get number of objects
|
|
|
|
*/
|
|
|
|
int UAVObjectParser::getNumObjects()
|
|
|
|
{
|
|
|
|
return objInfo.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the detailed object information
|
|
|
|
*/
|
2011-01-13 03:26:00 +01:00
|
|
|
QList<ObjectInfo*> UAVObjectParser::getObjectInfo()
|
2010-03-27 04:22:43 +01:00
|
|
|
{
|
|
|
|
return objInfo;
|
|
|
|
}
|
|
|
|
|
2011-01-13 03:26:00 +01:00
|
|
|
ObjectInfo* UAVObjectParser::getObjectByIndex(int objIndex)
|
|
|
|
{
|
|
|
|
return objInfo[objIndex];
|
|
|
|
}
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
/**
|
|
|
|
* Get the name of the object
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::getObjectName(int objIndex)
|
|
|
|
{
|
|
|
|
ObjectInfo* info = objInfo[objIndex];
|
|
|
|
if (info == NULL)
|
|
|
|
return QString();
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
return info->name;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of the object
|
|
|
|
*/
|
|
|
|
quint32 UAVObjectParser::getObjectID(int objIndex)
|
|
|
|
{
|
|
|
|
ObjectInfo* info = objInfo[objIndex];
|
|
|
|
if (info == NULL)
|
|
|
|
return 0;
|
2011-01-13 03:26:00 +01:00
|
|
|
return info->id;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
|
2010-12-23 17:01:31 +01:00
|
|
|
/**
|
|
|
|
* Get the number of bytes in the data fields of this object
|
|
|
|
*/
|
|
|
|
int UAVObjectParser::getNumBytes(int objIndex)
|
|
|
|
{
|
|
|
|
ObjectInfo* info = objInfo[objIndex];
|
|
|
|
if (info == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int numBytes = 0;
|
|
|
|
for (int n = 0; n < info->fields.length(); ++n)
|
|
|
|
{
|
|
|
|
numBytes += info->fields[n]->numBytes * info->fields[n]->numElements;
|
|
|
|
}
|
|
|
|
return numBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 08:39:45 +01:00
|
|
|
bool fieldTypeLessThan(const FieldInfo* f1, const FieldInfo* f2)
|
|
|
|
{
|
|
|
|
return f1->numBytes > f2->numBytes;
|
|
|
|
}
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
/**
|
|
|
|
* Parse supplied XML file
|
|
|
|
* @param xml The xml text
|
|
|
|
* @param filename The xml filename
|
|
|
|
* @returns Null QString() on success, error message on failure
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::parseXML(QString& xml, QString& filename)
|
|
|
|
{
|
|
|
|
// Create DOM document and parse it
|
|
|
|
QDomDocument doc("UAVObjects");
|
|
|
|
bool parsed = doc.setContent(xml);
|
|
|
|
if (!parsed) return QString("Improperly formated XML file");
|
|
|
|
|
|
|
|
// Read all objects contained in the XML file, creating an new ObjectInfo for each
|
|
|
|
QDomElement docElement = doc.documentElement();
|
|
|
|
QDomNode node = docElement.firstChild();
|
2011-01-13 03:26:00 +01:00
|
|
|
while ( !node.isNull() ) {
|
2010-03-27 04:22:43 +01:00
|
|
|
// Create new object entry
|
|
|
|
ObjectInfo* info = new ObjectInfo;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
info->filename=filename;
|
2010-03-27 04:22:43 +01:00
|
|
|
// Process object attributes
|
|
|
|
QString status = processObjectAttributes(node, info);
|
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
|
|
|
|
|
|
|
// Process child elements (fields and metadata)
|
|
|
|
QDomNode childNode = node.firstChild();
|
2010-05-20 03:01:01 +02:00
|
|
|
bool fieldFound = false;
|
|
|
|
bool accessFound = false;
|
|
|
|
bool telGCSFound = false;
|
|
|
|
bool telFlightFound = false;
|
|
|
|
bool logFound = false;
|
2010-08-27 02:15:42 +02:00
|
|
|
bool descriptionFound = false;
|
2011-01-13 03:26:00 +01:00
|
|
|
while ( !childNode.isNull() ) {
|
2010-03-27 04:22:43 +01:00
|
|
|
// Process element depending on its type
|
2011-01-13 03:26:00 +01:00
|
|
|
if ( childNode.nodeName().compare(QString("field")) == 0 ) {
|
2010-03-27 04:22:43 +01:00
|
|
|
QString status = processObjectFields(childNode, info);
|
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
fieldFound = true;
|
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else if ( childNode.nodeName().compare(QString("access")) == 0 ) {
|
2010-05-20 03:01:01 +02:00
|
|
|
QString status = processObjectAccess(childNode, info);
|
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
accessFound = true;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else if ( childNode.nodeName().compare(QString("telemetrygcs")) == 0 ) {
|
2010-03-27 04:22:43 +01:00
|
|
|
QString status = processObjectMetadata(childNode, &info->gcsTelemetryUpdateMode,
|
|
|
|
&info->gcsTelemetryUpdatePeriod, &info->gcsTelemetryAcked);
|
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
telGCSFound = true;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else if ( childNode.nodeName().compare(QString("telemetryflight")) == 0 ) {
|
2010-03-27 04:22:43 +01:00
|
|
|
QString status = processObjectMetadata(childNode, &info->flightTelemetryUpdateMode,
|
|
|
|
&info->flightTelemetryUpdatePeriod, &info->flightTelemetryAcked);
|
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
telFlightFound = true;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else if ( childNode.nodeName().compare(QString("logging")) == 0 ) {
|
2010-03-27 04:22:43 +01:00
|
|
|
QString status = processObjectMetadata(childNode, &info->loggingUpdateMode,
|
|
|
|
&info->loggingUpdatePeriod, NULL);
|
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
logFound = true;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else if ( childNode.nodeName().compare(QString("description")) == 0 ) {
|
2010-08-27 02:15:42 +02:00
|
|
|
QString status = processObjectDescription(childNode, &info->description);
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-08-27 02:15:42 +02:00
|
|
|
if (!status.isNull())
|
|
|
|
return status;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-08-27 02:15:42 +02:00
|
|
|
descriptionFound = true;
|
|
|
|
}
|
2011-03-02 02:25:40 +01:00
|
|
|
else if (!childNode.isComment()) {
|
2010-03-27 04:22:43 +01:00
|
|
|
return QString("Unknown object element");
|
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Get next element
|
|
|
|
childNode = childNode.nextSibling();
|
|
|
|
}
|
2011-11-14 08:39:45 +01:00
|
|
|
|
|
|
|
// Sort all fields according to size
|
|
|
|
qStableSort(info->fields.begin(), info->fields.end(), fieldTypeLessThan);
|
2010-03-27 04:22:43 +01:00
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
// Make sure that required elements were found
|
|
|
|
if ( !accessFound )
|
|
|
|
return QString("Object::access element is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if ( !telGCSFound )
|
2010-05-20 03:01:01 +02:00
|
|
|
return QString("Object::telemetrygcs element is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if ( !telFlightFound )
|
2010-05-20 03:01:01 +02:00
|
|
|
return QString("Object::telemetryflight element is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if ( !logFound )
|
2010-05-20 03:01:01 +02:00
|
|
|
return QString("Object::logging element is missing");
|
|
|
|
|
2010-08-27 02:15:42 +02:00
|
|
|
// TODO: Make into error once all objects updated
|
|
|
|
if ( !descriptionFound )
|
|
|
|
return QString("Object::description element is missing");
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Calculate ID
|
|
|
|
calculateID(info);
|
|
|
|
|
|
|
|
// Add object
|
|
|
|
objInfo.append(info);
|
|
|
|
|
|
|
|
// Get next object
|
|
|
|
node = node.nextSibling();
|
|
|
|
}
|
|
|
|
|
2011-07-15 18:08:47 +02:00
|
|
|
all_units.removeDuplicates();
|
2010-03-27 04:22:43 +01:00
|
|
|
// Done, return null string
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the unique object ID based on the object information.
|
|
|
|
* The ID will change if the object definition changes, this is intentional
|
|
|
|
* and is used to avoid connecting objects with incompatible configurations.
|
2011-06-04 16:47:09 +02:00
|
|
|
* The LSB is set to zero and is reserved for metadata
|
2010-03-27 04:22:43 +01:00
|
|
|
*/
|
|
|
|
void UAVObjectParser::calculateID(ObjectInfo* info)
|
|
|
|
{
|
|
|
|
// Hash object name
|
|
|
|
quint32 hash = updateHash(info->name, 0);
|
|
|
|
// Hash object attributes
|
2010-03-29 00:19:32 +02:00
|
|
|
hash = updateHash(info->isSettings, hash);
|
2010-03-27 04:22:43 +01:00
|
|
|
hash = updateHash(info->isSingleInst, hash);
|
|
|
|
// Hash field information
|
2011-01-13 03:26:00 +01:00
|
|
|
for (int n = 0; n < info->fields.length(); ++n) {
|
2010-03-27 04:22:43 +01:00
|
|
|
hash = updateHash(info->fields[n]->name, hash);
|
|
|
|
hash = updateHash(info->fields[n]->numElements, hash);
|
|
|
|
hash = updateHash(info->fields[n]->type, hash);
|
2011-07-15 18:08:47 +02:00
|
|
|
if(info->fields[n]->type == FIELDTYPE_ENUM) {
|
|
|
|
QStringList options = info->fields[n]->options;
|
|
|
|
for (int m = 0; m < options.length(); m++)
|
|
|
|
hash = updateHash(options[m], hash);
|
|
|
|
}
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
// Done
|
2011-06-04 16:47:09 +02:00
|
|
|
info->id = hash & 0xFFFFFFFE;
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shift-Add-XOR hash implementation. LSB is set to zero, it is reserved
|
|
|
|
* for the ID of the metaobject.
|
|
|
|
*
|
|
|
|
* http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
|
|
|
|
*/
|
|
|
|
quint32 UAVObjectParser::updateHash(quint32 value, quint32 hash)
|
|
|
|
{
|
2011-06-04 16:47:09 +02:00
|
|
|
return (hash ^ ((hash<<5) + (hash>>2) + value));
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the hash given a string
|
|
|
|
*/
|
|
|
|
quint32 UAVObjectParser::updateHash(QString& value, quint32 hash)
|
|
|
|
{
|
|
|
|
QByteArray bytes = value.toAscii();
|
|
|
|
quint32 hashout = hash;
|
|
|
|
for (int n = 0; n < bytes.length(); ++n)
|
|
|
|
hashout = updateHash(bytes[n], hashout);
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
return hashout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the metadata part of the XML
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::processObjectMetadata(QDomNode& childNode, UpdateMode* mode, int* period, bool* acked)
|
|
|
|
{
|
|
|
|
// Get updatemode attribute
|
|
|
|
QDomNamedNodeMap elemAttributes = childNode.attributes();
|
|
|
|
QDomNode elemAttr = elemAttributes.namedItem("updatemode");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:telemetrygcs:updatemode attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
int index = updateModeStrXML.indexOf( elemAttr.nodeValue() );
|
|
|
|
|
|
|
|
if (index<0)
|
|
|
|
return QString("Object:telemetrygcs:updatemode attribute value is invalid");
|
|
|
|
|
|
|
|
*mode = (UpdateMode)index;
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Get period attribute
|
|
|
|
elemAttr = elemAttributes.namedItem("period");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:telemetrygcs:period attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
*period = elemAttr.nodeValue().toInt();
|
|
|
|
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Get acked attribute (only if acked parameter is not null, not applicable for logging metadata)
|
2011-01-13 03:26:00 +01:00
|
|
|
if ( acked != NULL) {
|
2010-03-27 04:22:43 +01:00
|
|
|
elemAttr = elemAttributes.namedItem("acked");
|
|
|
|
if ( elemAttr.isNull())
|
|
|
|
return QString("Object:telemetrygcs:acked attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if ( elemAttr.nodeValue().compare(QString("true")) == 0 )
|
|
|
|
*acked = true;
|
|
|
|
else if ( elemAttr.nodeValue().compare(QString("false")) == 0 )
|
|
|
|
*acked = false;
|
2010-03-27 04:22:43 +01:00
|
|
|
else
|
2011-01-13 03:26:00 +01:00
|
|
|
return QString("Object:telemetrygcs:acked attribute value is invalid");
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
|
|
|
// Done
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
/**
|
|
|
|
* Process the object access tag of the XML
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::processObjectAccess(QDomNode& childNode, ObjectInfo* info)
|
|
|
|
{
|
|
|
|
// Get gcs attribute
|
|
|
|
QDomNamedNodeMap elemAttributes = childNode.attributes();
|
|
|
|
QDomNode elemAttr = elemAttributes.namedItem("gcs");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:access:gcs attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
int index = accessModeStrXML.indexOf( elemAttr.nodeValue() );
|
|
|
|
if (index >= 0)
|
|
|
|
info->gcsAccess = (AccessMode)index;
|
2010-05-20 03:01:01 +02:00
|
|
|
else
|
2011-01-13 03:26:00 +01:00
|
|
|
return QString("Object:access:gcs attribute value is invalid");
|
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
// Get flight attribute
|
|
|
|
elemAttr = elemAttributes.namedItem("flight");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:access:flight attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
index = accessModeStrXML.indexOf( elemAttr.nodeValue() );
|
|
|
|
if (index >= 0)
|
|
|
|
info->flightAccess = (AccessMode)index;
|
2010-05-20 03:01:01 +02:00
|
|
|
else
|
2011-01-13 03:26:00 +01:00
|
|
|
return QString("Object:access:flight attribute value is invalid");
|
|
|
|
|
2010-05-20 03:01:01 +02:00
|
|
|
// Done
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
/**
|
|
|
|
* Process the object fields of the XML
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::processObjectFields(QDomNode& childNode, ObjectInfo* info)
|
|
|
|
{
|
|
|
|
// Create field
|
|
|
|
FieldInfo* field = new FieldInfo;
|
|
|
|
// Get name attribute
|
|
|
|
QDomNamedNodeMap elemAttributes = childNode.attributes();
|
|
|
|
QDomNode elemAttr = elemAttributes.namedItem("name");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:field:name attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
field->name = elemAttr.nodeValue();
|
|
|
|
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Get units attribute
|
|
|
|
elemAttr = elemAttributes.namedItem("units");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:field:units attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
field->units = elemAttr.nodeValue();
|
2011-07-15 18:08:47 +02:00
|
|
|
all_units << field->units;
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Get type attribute
|
|
|
|
elemAttr = elemAttributes.namedItem("type");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:field:type attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
int index = fieldTypeStrXML.indexOf(elemAttr.nodeValue());
|
2011-07-15 18:08:47 +02:00
|
|
|
if (index >= 0) {
|
2011-01-13 03:26:00 +01:00
|
|
|
field->type = (FieldType)index;
|
|
|
|
field->numBytes = fieldTypeNumBytes[index];
|
2011-07-15 18:08:47 +02:00
|
|
|
}
|
|
|
|
else {
|
2011-01-13 03:26:00 +01:00
|
|
|
return QString("Object:field:type attribute value is invalid");
|
2011-07-15 18:08:47 +02:00
|
|
|
}
|
|
|
|
|
2010-04-21 04:50:51 +02:00
|
|
|
// Get numelements or elementnames attribute
|
|
|
|
elemAttr = elemAttributes.namedItem("elementnames");
|
2011-01-13 03:26:00 +01:00
|
|
|
if ( !elemAttr.isNull() ) {
|
2010-04-22 02:44:14 +02:00
|
|
|
// Get element names
|
|
|
|
QStringList names = elemAttr.nodeValue().split(",", QString::SkipEmptyParts);
|
|
|
|
for (int n = 0; n < names.length(); ++n)
|
|
|
|
names[n] = names[n].trimmed();
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-04-22 02:44:14 +02:00
|
|
|
field->elementNames = names;
|
|
|
|
field->numElements = names.length();
|
2010-04-22 02:56:11 +02:00
|
|
|
field->defaultElementNames = false;
|
2010-04-22 02:44:14 +02:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else {
|
2010-04-21 04:50:51 +02:00
|
|
|
elemAttr = elemAttributes.namedItem("elements");
|
2011-01-13 03:26:00 +01:00
|
|
|
if ( elemAttr.isNull() ) {
|
2010-04-22 02:56:11 +02:00
|
|
|
return QString("Object:field:elements and Object:field:elementnames attribute is missing");
|
2010-04-21 04:50:51 +02:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else {
|
2010-04-21 04:50:51 +02:00
|
|
|
field->numElements = elemAttr.nodeValue().toInt();
|
|
|
|
for (int n = 0; n < field->numElements; ++n)
|
2010-04-22 02:44:14 +02:00
|
|
|
field->elementNames.append(QString("%1").arg(n));
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-04-22 02:56:11 +02:00
|
|
|
field->defaultElementNames = true;
|
2010-04-21 04:50:51 +02:00
|
|
|
}
|
2010-03-27 04:22:43 +01:00
|
|
|
}
|
2010-04-01 04:01:37 +02:00
|
|
|
// Get options attribute (only if an enum type)
|
2011-01-13 03:26:00 +01:00
|
|
|
if (field->type == FIELDTYPE_ENUM) {
|
|
|
|
|
2010-04-01 04:01:37 +02:00
|
|
|
// Get options attribute
|
|
|
|
elemAttr = elemAttributes.namedItem("options");
|
|
|
|
if ( elemAttr.isNull() )
|
|
|
|
return QString("Object:field:options attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
QStringList options = elemAttr.nodeValue().split(",", QString::SkipEmptyParts);
|
|
|
|
for (int n = 0; n < options.length(); ++n)
|
|
|
|
options[n] = options[n].trimmed();
|
|
|
|
|
|
|
|
field->options = options;
|
2010-04-01 04:01:37 +02:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-04-28 03:50:53 +02:00
|
|
|
// Get the default value attribute (required for settings objects, optional for the rest)
|
|
|
|
elemAttr = elemAttributes.namedItem("defaultvalue");
|
2011-01-13 03:26:00 +01:00
|
|
|
if ( elemAttr.isNull() ) {
|
2010-04-28 03:50:53 +02:00
|
|
|
if ( info->isSettings )
|
|
|
|
return QString("Object:field:defaultvalue attribute is missing (required for settings objects)");
|
2011-01-13 03:26:00 +01:00
|
|
|
field->defaultValues = QStringList();
|
2010-04-28 03:50:53 +02:00
|
|
|
}
|
2011-01-13 03:26:00 +01:00
|
|
|
else {
|
2010-10-05 14:40:42 +02:00
|
|
|
QStringList defaults = elemAttr.nodeValue().split(",", QString::SkipEmptyParts);
|
|
|
|
for (int n = 0; n < defaults.length(); ++n)
|
|
|
|
defaults[n] = defaults[n].trimmed();
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if(defaults.length() != field->numElements) {
|
2010-10-05 14:40:42 +02:00
|
|
|
if(defaults.length() != 1)
|
|
|
|
return QString("Object:field:incorrect number of default values");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-10-05 14:40:42 +02:00
|
|
|
/*support legacy single default for multiple elements
|
|
|
|
We sould really issue a warning*/
|
|
|
|
for(int ct=1; ct< field->numElements; ct++)
|
|
|
|
defaults.append(defaults[0]);
|
|
|
|
}
|
|
|
|
field->defaultValues = defaults;
|
2010-04-28 03:50:53 +02:00
|
|
|
}
|
2012-02-03 21:29:28 +01:00
|
|
|
elemAttr = elemAttributes.namedItem("limits");
|
|
|
|
if ( elemAttr.isNull() ) {
|
|
|
|
field->limitValues=QString();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
field->limitValues=elemAttr.nodeValue();
|
|
|
|
}
|
2010-03-27 04:22:43 +01:00
|
|
|
// Add field to object
|
|
|
|
info->fields.append(field);
|
|
|
|
// Done
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the object attributes from the XML
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::processObjectAttributes(QDomNode& node, ObjectInfo* info)
|
|
|
|
{
|
|
|
|
// Get name attribute
|
|
|
|
QDomNamedNodeMap attributes = node.attributes();
|
|
|
|
QDomNode attr = attributes.namedItem("name");
|
|
|
|
if ( attr.isNull() )
|
|
|
|
return QString("Object:name attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
info->name = attr.nodeValue();
|
|
|
|
info->namelc = attr.nodeValue().toLower();
|
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Get singleinstance attribute
|
|
|
|
attr = attributes.namedItem("singleinstance");
|
|
|
|
if ( attr.isNull() )
|
|
|
|
return QString("Object:singleinstance attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if ( attr.nodeValue().compare(QString("true")) == 0 )
|
|
|
|
info->isSingleInst = true;
|
|
|
|
else if ( attr.nodeValue().compare(QString("false")) == 0 )
|
|
|
|
info->isSingleInst = false;
|
2010-03-27 04:22:43 +01:00
|
|
|
else
|
2011-01-13 03:26:00 +01:00
|
|
|
return QString("Object:singleinstance attribute value is invalid");
|
|
|
|
|
2010-03-29 00:19:32 +02:00
|
|
|
// Get settings attribute
|
|
|
|
attr = attributes.namedItem("settings");
|
2010-03-27 04:22:43 +01:00
|
|
|
if ( attr.isNull() )
|
2010-03-29 00:19:32 +02:00
|
|
|
return QString("Object:settings attribute is missing");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
if ( attr.nodeValue().compare(QString("true")) == 0 )
|
2010-03-29 00:19:32 +02:00
|
|
|
info->isSettings = true;
|
2011-01-13 03:26:00 +01:00
|
|
|
else if ( attr.nodeValue().compare(QString("false")) == 0 )
|
|
|
|
info->isSettings = false;
|
|
|
|
else
|
|
|
|
return QString("Object:settings attribute value is invalid");
|
|
|
|
|
|
|
|
|
2010-03-29 00:19:32 +02:00
|
|
|
// Settings objects can only have a single instance
|
|
|
|
if ( info->isSettings && !info->isSingleInst )
|
|
|
|
return QString("Object: Settings objects can not have multiple instances");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2010-03-27 04:22:43 +01:00
|
|
|
// Done
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-08-27 02:15:42 +02:00
|
|
|
/**
|
|
|
|
* Process the description field from the XML file
|
|
|
|
*/
|
|
|
|
QString UAVObjectParser::processObjectDescription(QDomNode& childNode, QString * description)
|
|
|
|
{
|
|
|
|
description->append(childNode.firstChild().nodeValue());
|
|
|
|
return QString();
|
|
|
|
}
|