1 // Copyright (C) 2009 - 2012 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # include <simgear_config.h>
22 #include <simgear/compiler.h>
24 #include "HLAOMTXmlVisitor.hxx"
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/xml/easyxml.hxx>
32 #include "HLAArrayDataType.hxx"
33 #include "HLABasicDataType.hxx"
34 #include "HLADataTypeVisitor.hxx"
35 #include "HLAEnumeratedDataType.hxx"
36 #include "HLAFederate.hxx"
37 #include "HLAFixedRecordDataType.hxx"
38 #include "HLAVariantRecordDataType.hxx"
42 HLAOMTXmlVisitor::ObjectClass::ObjectClass(const std::string& name, const std::string& sharing) :
48 HLAOMTXmlVisitor::ObjectClass::~ObjectClass()
53 HLAOMTXmlVisitor::ObjectClass::getName() const
59 HLAOMTXmlVisitor::ObjectClass::getSharing() const
65 HLAOMTXmlVisitor::ObjectClass::getNumAttributes() const
67 return _attributes.size();
70 const HLAOMTXmlVisitor::Attribute*
71 HLAOMTXmlVisitor::ObjectClass::getAttribute(unsigned index) const
73 if (_attributes.size() <= index)
75 return _attributes[index];
78 const HLAOMTXmlVisitor::ObjectClass*
79 HLAOMTXmlVisitor::ObjectClass::getParentObjectClass() const
81 return _parentObjectClass.get();
84 HLAOMTXmlVisitor::InteractionClass::InteractionClass(const std::string& name) :
89 HLAOMTXmlVisitor::InteractionClass::~InteractionClass()
94 HLAOMTXmlVisitor::InteractionClass::getName() const
100 HLAOMTXmlVisitor::InteractionClass::getDimensions() const
106 HLAOMTXmlVisitor::InteractionClass::getSharing() const
112 HLAOMTXmlVisitor::InteractionClass::getTransportation() const
114 return _transportation;
118 HLAOMTXmlVisitor::InteractionClass::getOrder() const
124 HLAOMTXmlVisitor::InteractionClass::getNumParameters() const
126 return _parameters.size();
129 const HLAOMTXmlVisitor::Parameter*
130 HLAOMTXmlVisitor::InteractionClass::getParameter(unsigned index) const
132 if (_parameters.size() <= index)
134 return _parameters[index];
137 const HLAOMTXmlVisitor::InteractionClass*
138 HLAOMTXmlVisitor::InteractionClass::getParentInteractionClass() const
140 return _parentInteractionClass.get();
143 HLAOMTXmlVisitor::HLAOMTXmlVisitor()
147 HLAOMTXmlVisitor::~HLAOMTXmlVisitor()
152 HLAOMTXmlVisitor::setDataTypesToFederate(HLAFederate& federate)
154 // Provide all the data types
155 for (BasicDataMap::iterator i = _basicDataMap.begin(); i != _basicDataMap.end(); ++i)
156 federate.insertDataType(i->first, getDataType(i->first));
157 for (SimpleDataMap::iterator i = _simpleDataMap.begin(); i != _simpleDataMap.end(); ++i)
158 federate.insertDataType(i->first, getDataType(i->first));
159 for (EnumeratedDataMap::iterator i = _enumeratedDataMap.begin(); i != _enumeratedDataMap.end(); ++i)
160 federate.insertDataType(i->first, getDataType(i->first));
161 for (ArrayDataMap::iterator i = _arrayDataMap.begin(); i != _arrayDataMap.end(); ++i)
162 federate.insertDataType(i->first, getDataType(i->first));
163 for (FixedRecordDataMap::iterator i = _fixedRecordDataMap.begin(); i != _fixedRecordDataMap.end(); ++i)
164 federate.insertDataType(i->first, getDataType(i->first));
165 for (VariantRecordDataMap::iterator i = _variantRecordDataMap.begin(); i != _variantRecordDataMap.end(); ++i)
166 federate.insertDataType(i->first, getDataType(i->first));
168 // Finish alignment computations
169 federate.recomputeDataTypeAlignment();
173 HLAOMTXmlVisitor::setToFederate(HLAFederate& federate)
175 setDataTypesToFederate(federate);
177 // Provide all interaction classes
178 unsigned numInteractionClasses = getNumInteractionClasses();
179 for (unsigned i = 0; i < numInteractionClasses; ++i) {
180 const InteractionClass* interactionClass = getInteractionClass(i);
181 if (federate.getInteractionClass(interactionClass->getName())) {
182 SG_LOG(SG_IO, SG_ALERT, "Not creating Interaction class \"" << interactionClass->getName() << "\" twice!.");
186 SGSharedPtr<HLAInteractionClass> hlaInteractionClass;
187 hlaInteractionClass = federate.createInteractionClass(interactionClass->getName());
188 if (!hlaInteractionClass.valid()) {
189 SG_LOG(SG_IO, SG_INFO, "Ignoring Interaction class \"" << interactionClass->getName() << "\".");
193 hlaInteractionClass->setSubscriptionType(interactionClass->getSubscriptionType());
194 hlaInteractionClass->setPublicationType(interactionClass->getPublicationType());
196 // process the parameters
197 for (unsigned j = 0; j < interactionClass->getNumParameters(); ++j) {
198 const Parameter* parameter = interactionClass->getParameter(j);
199 unsigned index = hlaInteractionClass->addParameter(parameter->getName());
200 hlaInteractionClass->setParameterDataType(index, federate.getDataType(parameter->getDataType()));
204 // Provide all object classes
205 unsigned numObjectClasses = getNumObjectClasses();
206 for (unsigned i = 0; i < numObjectClasses; ++i) {
207 const ObjectClass* objectClass = getObjectClass(i);
208 if (federate.getObjectClass(objectClass->getName())) {
209 SG_LOG(SG_IO, SG_ALERT, "Not creating Object class \"" << objectClass->getName() << "\" twice!.");
213 SGSharedPtr<HLAObjectClass> hlaObjectClass;
214 hlaObjectClass = federate.createObjectClass(objectClass->getName());
215 if (!hlaObjectClass.valid()) {
216 SG_LOG(SG_IO, SG_INFO, "Ignoring Object class \"" << objectClass->getName() << "\".");
220 // process the attributes
221 for (unsigned j = 0; j < objectClass->getNumAttributes(); ++j) {
222 const Attribute* attribute = objectClass->getAttribute(j);
224 unsigned index = hlaObjectClass->addAttribute(attribute->getName());
225 hlaObjectClass->setAttributeDataType(index, federate.getDataType(attribute->getDataType()));
227 hlaObjectClass->setAttributeSubscriptionType(index, attribute->getSubscriptionType());
228 hlaObjectClass->setAttributePublicationType(index, attribute->getPublicationType());
229 hlaObjectClass->setAttributeUpdateType(index, attribute->getUpdateType());
235 HLAOMTXmlVisitor::getNumObjectClasses() const
237 return _objectClassList.size();
240 const HLAOMTXmlVisitor::ObjectClass*
241 HLAOMTXmlVisitor::getObjectClass(unsigned i) const
243 if (_objectClassList.size() <= i)
245 return _objectClassList[i];
249 HLAOMTXmlVisitor::getNumInteractionClasses() const
251 return _interactionClassList.size();
254 const HLAOMTXmlVisitor::InteractionClass*
255 HLAOMTXmlVisitor::getInteractionClass(unsigned i) const
257 if (_interactionClassList.size() <= i)
259 return _interactionClassList[i];
262 SGSharedPtr<HLADataType>
263 HLAOMTXmlVisitor::getDataType(const std::string& dataTypeName)
265 StringDataTypeMap::const_iterator i = _dataTypeMap.find(dataTypeName);
266 if (i != _dataTypeMap.end())
269 SGSharedPtr<HLADataType> dataType;
270 dataType = getBasicDataType(dataTypeName);
271 if (dataType.valid()) {
272 _dataTypeMap[dataTypeName] = dataType;
276 dataType = getSimpleDataType(dataTypeName);
277 if (dataType.valid())
280 dataType = getEnumeratedDataType(dataTypeName);
281 if (dataType.valid())
284 dataType = getArrayDataType(dataTypeName);
285 if (dataType.valid())
288 dataType = getFixedRecordDataType(dataTypeName);
289 if (dataType.valid())
292 dataType = getVariantRecordDataType(dataTypeName);
293 if (dataType.valid())
296 SG_LOG(SG_IO, SG_WARN, "Could not resolve dataType \"" << dataTypeName << "\".");
300 SGSharedPtr<HLABasicDataType>
301 HLAOMTXmlVisitor::getBasicDataType(const std::string& dataTypeName)
303 BasicDataMap::const_iterator i = _basicDataMap.find(dataTypeName);
304 if (i == _basicDataMap.end())
306 if (i->second._size == "8") {
307 return new HLAUInt8DataType(dataTypeName);
308 } else if (i->second._size == "16") {
309 if (i->first.find("Unsigned") != std::string::npos) {
310 if (i->second._endian == "Little") {
311 return new HLAUInt16LEDataType(dataTypeName);
313 return new HLAUInt16BEDataType(dataTypeName);
315 } else if (i->first.find("octetPair") != std::string::npos) {
316 if (i->second._endian == "Little") {
317 return new HLAUInt16LEDataType(dataTypeName);
319 return new HLAUInt16BEDataType(dataTypeName);
322 if (i->second._endian == "Little") {
323 return new HLAInt16LEDataType(dataTypeName);
325 return new HLAInt16BEDataType(dataTypeName);
328 } else if (i->second._size == "32") {
329 if (i->first.find("Unsigned") != std::string::npos) {
330 if (i->second._endian == "Little") {
331 return new HLAUInt32LEDataType(dataTypeName);
333 return new HLAUInt32BEDataType(dataTypeName);
335 } else if (i->first.find("float") != std::string::npos) {
336 if (i->second._endian == "Little") {
337 return new HLAFloat32LEDataType(dataTypeName);
339 return new HLAFloat32BEDataType(dataTypeName);
342 if (i->second._endian == "Little") {
343 return new HLAInt32LEDataType(dataTypeName);
345 return new HLAInt32BEDataType(dataTypeName);
348 } else if (i->second._size == "64") {
349 if (i->first.find("Unsigned") != std::string::npos) {
350 if (i->second._endian == "Little") {
351 return new HLAUInt64LEDataType(dataTypeName);
353 return new HLAUInt64BEDataType(dataTypeName);
355 } else if (i->first.find("float") != std::string::npos) {
356 if (i->second._endian == "Little") {
357 return new HLAFloat64LEDataType(dataTypeName);
359 return new HLAFloat64BEDataType(dataTypeName);
362 if (i->second._endian == "Little") {
363 return new HLAInt64LEDataType(dataTypeName);
365 return new HLAInt64BEDataType(dataTypeName);
373 SGSharedPtr<HLADataType>
374 HLAOMTXmlVisitor::getSimpleDataType(const std::string& dataTypeName)
376 SimpleDataMap::const_iterator i = _simpleDataMap.find(dataTypeName);
377 if (i == _simpleDataMap.end())
379 return getDataType(i->second._representation);
382 SGSharedPtr<HLAEnumeratedDataType>
383 HLAOMTXmlVisitor::getEnumeratedDataType(const std::string& dataTypeName)
385 EnumeratedDataMap::const_iterator i = _enumeratedDataMap.find(dataTypeName);
386 if (i == _enumeratedDataMap.end())
389 SGSharedPtr<HLAEnumeratedDataType> enumeratedDataType = new HLAEnumeratedDataType(dataTypeName);
390 _dataTypeMap[dataTypeName] = enumeratedDataType;
391 enumeratedDataType->setRepresentation(getBasicDataType(i->second._representation));
393 for (EnumeratorList::const_iterator j = i->second._enumeratorList.begin();
394 j != i->second._enumeratorList.end(); ++j) {
395 if (!enumeratedDataType->addEnumerator(j->_name, j->_values)) {
396 SG_LOG(SG_IO, SG_ALERT, "Could not add enumerator \"" << j->_name
397 << "\" to find enumerated data type \"" << dataTypeName << "\".");
402 return enumeratedDataType;
405 SGSharedPtr<HLADataType>
406 HLAOMTXmlVisitor::getArrayDataType(const std::string& dataTypeName)
408 ArrayDataMap::const_iterator i = _arrayDataMap.find(dataTypeName);
409 if (i == _arrayDataMap.end())
411 SGSharedPtr<HLAArrayDataType> arrayDataType;
412 if (i->second._encoding == "HLAvariableArray") {
413 arrayDataType = new HLAVariableArrayDataType(dataTypeName);
414 } else if (i->second._encoding == "HLAfixedArray") {
415 std::stringstream ss(i->second._cardinality);
416 unsigned cardinality;
419 SG_LOG(SG_IO, SG_ALERT, "Could not interpret cardinality \""
420 << i->second._cardinality << "\" for dataType \""
421 << dataTypeName << "\".");
424 SGSharedPtr<HLAFixedArrayDataType> dataType = new HLAFixedArrayDataType(dataTypeName);
425 dataType->setNumElements(cardinality);
426 arrayDataType = dataType;
428 SG_LOG(SG_IO, SG_ALERT, "Can not interpret encoding \""
429 << i->second._encoding << "\" for dataType \""
430 << dataTypeName << "\".");
434 _dataTypeMap[dataTypeName] = arrayDataType;
435 SGSharedPtr<HLADataType> elementDataType = getDataType(i->second._dataType);
436 if (!elementDataType.valid()) {
437 SG_LOG(SG_IO, SG_ALERT, "Could not interpret dataType \""
438 << i->second._dataType << "\" for array data type \""
439 << dataTypeName << "\".");
440 _dataTypeMap.erase(dataTypeName);
443 arrayDataType->setElementDataType(elementDataType.get());
445 // Check if this should be a string data type
446 if (elementDataType->toBasicDataType()) {
447 if (dataTypeName == "HLAopaqueData") {
448 arrayDataType->setIsOpaque(true);
449 } else if (dataTypeName.find("String") != std::string::npos || dataTypeName.find("string") != std::string::npos) {
450 arrayDataType->setIsString(true);
454 return arrayDataType;
457 SGSharedPtr<HLAFixedRecordDataType>
458 HLAOMTXmlVisitor::getFixedRecordDataType(const std::string& dataTypeName)
460 FixedRecordDataMap::const_iterator i = _fixedRecordDataMap.find(dataTypeName);
461 if (i == _fixedRecordDataMap.end())
464 SGSharedPtr<HLAFixedRecordDataType> dataType = new HLAFixedRecordDataType(dataTypeName);
465 _dataTypeMap[dataTypeName] = dataType;
466 for (FieldList::size_type j = 0; j < i->second._fieldList.size(); ++j) {
467 SGSharedPtr<HLADataType> fieldDataType = getDataType(i->second._fieldList[j]._dataType);
468 if (!fieldDataType.valid()) {
469 SG_LOG(SG_IO, SG_ALERT, "Could not get data type \"" << i->second._fieldList[j]._dataType
470 << "\" for field " << j << "of fixed record data type \"" << dataTypeName << "\".");
471 _dataTypeMap.erase(dataTypeName);
474 dataType->addField(i->second._fieldList[j]._name, fieldDataType.get());
479 SGSharedPtr<HLAVariantRecordDataType>
480 HLAOMTXmlVisitor::getVariantRecordDataType(const std::string& dataTypeName)
482 VariantRecordDataMap::const_iterator i = _variantRecordDataMap.find(dataTypeName);
483 if (i == _variantRecordDataMap.end())
485 SGSharedPtr<HLAVariantRecordDataType> dataType = new HLAVariantRecordDataType(dataTypeName);
486 _dataTypeMap[dataTypeName] = dataType;
488 SGSharedPtr<HLAEnumeratedDataType> enumeratedDataType = getEnumeratedDataType(i->second._dataType);
489 if (!enumeratedDataType.valid()) {
490 SG_LOG(SG_IO, SG_ALERT, "Could not find enumerted data type \"" << i->second._dataType
491 << "\" for variant data type \"" << dataTypeName << "\".");
494 dataType->setEnumeratedDataType(enumeratedDataType);
496 for (AlternativeList::const_iterator j = i->second._alternativeList.begin();
497 j != i->second._alternativeList.end(); ++j) {
498 SGSharedPtr<HLADataType> alternativeDataType = getDataType(j->_dataType);
499 if (!alternativeDataType.valid()) {
500 SG_LOG(SG_IO, SG_ALERT, "Could not resolve alternative dataType \"" << j->_dataType
501 << "\" for alternative \"" << j->_name << "\".");
502 _dataTypeMap.erase(dataTypeName);
505 if (!dataType->addAlternative(j->_name, j->_enumerator, alternativeDataType.get(), j->_semantics)) {
506 SG_LOG(SG_IO, SG_ALERT, "Could not add alternative \"" << j->_name << "\".");
513 HLAOMTXmlVisitor::Mode
514 HLAOMTXmlVisitor::getCurrentMode()
516 if (_modeStack.empty())
518 return _modeStack.back();
522 HLAOMTXmlVisitor::pushMode(HLAOMTXmlVisitor::Mode mode)
524 _modeStack.push_back(mode);
528 HLAOMTXmlVisitor::popMode()
530 _modeStack.pop_back();
534 HLAOMTXmlVisitor::startXML()
540 HLAOMTXmlVisitor::endXML()
542 if (!_modeStack.empty())
543 throw sg_exception("Internal parse error!");
545 // propagate parent attributes to the derived classes
546 // Note that this preserves the order of the attributes starting from the root object
547 for (ObjectClassList::const_iterator i = _objectClassList.begin(); i != _objectClassList.end(); ++i) {
548 SGSharedPtr<const ObjectClass> objectClass = (*i)->_parentObjectClass;
549 if (!objectClass.valid())
551 for (AttributeList::const_reverse_iterator j = objectClass->_attributes.rbegin();
552 j != objectClass->_attributes.rend(); ++j) {
553 (*i)->_attributes.insert((*i)->_attributes.begin(), *j);
557 // propagate parent parameter to the derived interactions
558 // Note that this preserves the order of the parameters starting from the root object
559 for (InteractionClassList::const_iterator i = _interactionClassList.begin(); i != _interactionClassList.end(); ++i) {
560 SGSharedPtr<const InteractionClass> interactionClass = (*i)->_parentInteractionClass;
561 if (!interactionClass.valid())
563 for (ParameterList::const_reverse_iterator j = interactionClass->_parameters.rbegin();
564 j != interactionClass->_parameters.rend(); ++j) {
565 (*i)->_parameters.insert((*i)->_parameters.begin(), *j);
571 HLAOMTXmlVisitor::startElement(const char* name, const XMLAttributes& atts)
573 if (strcmp(name, "attribute") == 0) {
574 if (getCurrentMode() != ObjectClassMode)
575 throw sg_exception("attribute tag outside objectClass!");
576 pushMode(AttributeMode);
578 if (_objectClassList.empty())
579 throw sg_exception("attribute tag outside of an objectClass");
581 std::string name = getAttribute("name", atts);
583 throw sg_exception("attribute tag without name attribute");
585 SGSharedPtr<Attribute> attribute = new Attribute(name);
587 attribute->_dataType = getAttribute("dataType", atts);
588 attribute->_updateType = getAttribute("updateType", atts);
589 attribute->_updateCondition = getAttribute("updateCondition", atts);
590 attribute->_ownership = getAttribute("ownership", atts);
591 attribute->_sharing = getAttribute("sharing", atts);
592 attribute->_dimensions = getAttribute("dimensions", atts);
593 attribute->_transportation = getAttribute("transportation", atts);
594 attribute->_order = getAttribute("order", atts);
596 _objectClassStack.back()->_attributes.push_back(attribute);
598 } else if (strcmp(name, "objectClass") == 0) {
599 if (getCurrentMode() != ObjectsMode && getCurrentMode() != ObjectClassMode)
600 throw sg_exception("objectClass tag outside objectClass or objects!");
601 pushMode(ObjectClassMode);
603 std::string name = getAttribute("name", atts);
605 throw sg_exception("objectClass tag without name attribute");
607 std::string sharing = getAttribute("sharing", atts);
609 // The new ObjectClass
610 ObjectClass* objectClass = new ObjectClass(name, sharing);
612 // Inherit all previous attributes
613 if (!_objectClassStack.empty())
614 objectClass->_parentObjectClass = _objectClassStack.back();
616 _objectClassStack.push_back(objectClass);
617 _objectClassList.push_back(objectClass);
619 } else if (strcmp(name, "objects") == 0) {
620 if (getCurrentMode() != ObjectModelMode)
621 throw sg_exception("objects tag outside objectModel!");
622 pushMode(ObjectsMode);
624 } else if (strcmp(name, "parameter") == 0) {
625 if (getCurrentMode() != InteractionClassMode)
626 throw sg_exception("parameter tag outside interactionClass!");
627 pushMode(ParameterMode);
629 if (_interactionClassList.empty())
630 throw sg_exception("parameter tag outside of an interactionClass");
632 std::string name = getAttribute("name", atts);
634 throw sg_exception("parameter tag without name parameter");
636 SGSharedPtr<Parameter> parameter = new Parameter(name);
637 parameter->_dataType = getAttribute("dataType", atts);
639 _interactionClassStack.back()->_parameters.push_back(parameter);
641 } else if (strcmp(name, "interactionClass") == 0) {
642 if (getCurrentMode() != InteractionsMode && getCurrentMode() != InteractionClassMode)
643 throw sg_exception("interactionClass tag outside interactions or interactionClass!");
644 pushMode(InteractionClassMode);
646 std::string name = getAttribute("name", atts);
648 throw sg_exception("interactionClass tag without name attribute");
650 // The new ObjectClass
651 InteractionClass* interactionClass = new InteractionClass(name);
652 interactionClass->_dimensions = getAttribute("dimensions", atts);
653 interactionClass->_transportation = getAttribute("transportation", atts);
654 interactionClass->_order = getAttribute("order", atts);
656 // Inherit all previous attributes
657 if (!_interactionClassStack.empty())
658 interactionClass->_parentInteractionClass = _interactionClassStack.back();
660 _interactionClassStack.push_back(interactionClass);
661 _interactionClassList.push_back(interactionClass);
663 } else if (strcmp(name, "interactions") == 0) {
664 if (getCurrentMode() != ObjectModelMode)
665 throw sg_exception("interactions tag outside objectModel!");
666 pushMode(InteractionsMode);
668 } else if (strcmp(name, "basicData") == 0) {
669 if (getCurrentMode() != BasicDataRepresentationsMode)
670 throw sg_exception("basicData tag outside basicDataRepresentations!");
671 pushMode(BasicDataMode);
673 std::string name = getAttribute("name", atts);
675 throw sg_exception("basicData tag without name attribute");
677 _basicDataMap[name]._size = getAttribute("size", atts);
678 _basicDataMap[name]._endian = getAttribute("endian", atts);
680 } else if (strcmp(name, "basicDataRepresentations") == 0) {
681 if (getCurrentMode() != DataTypesMode)
682 throw sg_exception("basicDataRepresentations tag outside dataTypes!");
683 pushMode(BasicDataRepresentationsMode);
685 } else if (strcmp(name, "simpleData") == 0) {
686 if (getCurrentMode() != SimpleDataTypesMode)
687 throw sg_exception("simpleData tag outside simpleDataTypes!");
688 pushMode(SimpleDataMode);
690 std::string name = getAttribute("name", atts);
692 throw sg_exception("simpleData tag without name attribute");
694 _simpleDataMap[name]._representation = getAttribute("representation", atts);
695 _simpleDataMap[name]._units = getAttribute("units", atts);
696 _simpleDataMap[name]._resolution = getAttribute("resolution", atts);
697 _simpleDataMap[name]._accuracy = getAttribute("accuracy", atts);
699 } else if (strcmp(name, "simpleDataTypes") == 0) {
700 if (getCurrentMode() != DataTypesMode)
701 throw sg_exception("simpleDataTypes tag outside dataTypes!");
702 pushMode(SimpleDataTypesMode);
704 } else if (strcmp(name, "enumerator") == 0) {
705 if (getCurrentMode() != EnumeratedDataMode)
706 throw sg_exception("enumerator tag outside enumeratedData!");
707 pushMode(EnumeratorMode);
709 std::string name = getAttribute("name", atts);
711 throw sg_exception("enumerator tag without name attribute");
713 Enumerator enumerator;
714 enumerator._name = name;
715 enumerator._values = getAttribute("values", atts);
716 _enumeratedDataMap[_enumeratedDataName]._enumeratorList.push_back(enumerator);
718 } else if (strcmp(name, "enumeratedData") == 0) {
719 if (getCurrentMode() != EnumeratedDataTypesMode)
720 throw sg_exception("enumeratedData tag outside enumeratedDataTypes!");
721 pushMode(EnumeratedDataMode);
723 std::string name = getAttribute("name", atts);
725 throw sg_exception("enumeratedData tag without name attribute");
727 _enumeratedDataName = name;
728 _enumeratedDataMap[_enumeratedDataName]._representation = getAttribute("representation", atts);
730 } else if (strcmp(name, "enumeratedDataTypes") == 0) {
731 if (getCurrentMode() != DataTypesMode)
732 throw sg_exception("enumeratedDataTypes tag outside dataTypes!");
733 pushMode(EnumeratedDataTypesMode);
735 } else if (strcmp(name, "arrayData") == 0) {
736 if (getCurrentMode() != ArrayDataTypesMode)
737 throw sg_exception("arrayData tag outside arrayDataTypes!");
738 pushMode(ArrayDataMode);
740 std::string name = getAttribute("name", atts);
742 throw sg_exception("arrayData tag without name attribute");
744 _arrayDataMap[name]._dataType = getAttribute("dataType", atts);
745 _arrayDataMap[name]._cardinality = getAttribute("cardinality", atts);
746 _arrayDataMap[name]._encoding = getAttribute("encoding", atts);
748 } else if (strcmp(name, "arrayDataTypes") == 0) {
749 if (getCurrentMode() != DataTypesMode)
750 throw sg_exception("arrayDataTypes tag outside dataTypes!");
751 pushMode(ArrayDataTypesMode);
753 } else if (strcmp(name, "field") == 0) {
754 if (getCurrentMode() != FixedRecordDataMode)
755 throw sg_exception("field tag outside fixedRecordData!");
758 std::string name = getAttribute("name", atts);
760 throw sg_exception("field tag without name attribute");
764 field._dataType = getAttribute("dataType", atts);
765 _fixedRecordDataMap[_fixedRecordDataName]._fieldList.push_back(field);
767 } else if (strcmp(name, "fixedRecordData") == 0) {
768 if (getCurrentMode() != FixedRecordDataTypesMode)
769 throw sg_exception("fixedRecordData tag outside fixedRecordDataTypes!");
770 pushMode(FixedRecordDataMode);
772 std::string name = getAttribute("name", atts);
774 throw sg_exception("fixedRecordData tag without name attribute");
776 _fixedRecordDataName = name;
777 _fixedRecordDataMap[name]._encoding = getAttribute("encoding", atts);
779 } else if (strcmp(name, "fixedRecordDataTypes") == 0) {
780 if (getCurrentMode() != DataTypesMode)
781 throw sg_exception("fixedRecordDataTypes tag outside dataTypes!");
782 pushMode(FixedRecordDataTypesMode);
784 } else if (strcmp(name, "alternative") == 0) {
786 if (getCurrentMode() != VariantRecordDataMode)
787 throw sg_exception("alternative tag outside variantRecordData!");
788 pushMode(AlternativeDataMode);
790 std::string name = getAttribute("name", atts);
792 throw sg_exception("alternative tag without name attribute");
794 Alternative alternative;
795 alternative._name = name;
796 alternative._dataType = getAttribute("dataType", atts);
797 alternative._semantics = getAttribute("semantics", atts);
798 alternative._enumerator = getAttribute("enumerator", atts);
799 _variantRecordDataMap[_variantRecordDataName]._alternativeList.push_back(alternative);
801 } else if (strcmp(name, "variantRecordData") == 0) {
802 if (getCurrentMode() != VariantRecordDataTypesMode)
803 throw sg_exception("variantRecordData tag outside variantRecordDataTypes!");
804 pushMode(VariantRecordDataMode);
806 std::string name = getAttribute("name", atts);
808 throw sg_exception("fixedRecordData tag without name attribute");
810 _variantRecordDataName = name;
811 _variantRecordDataMap[name]._encoding = getAttribute("encoding", atts);
812 _variantRecordDataMap[name]._dataType = getAttribute("dataType", atts);
813 _variantRecordDataMap[name]._semantics = getAttribute("semantics", atts);
814 _variantRecordDataMap[name]._discriminant = getAttribute("discriminant", atts);
816 } else if (strcmp(name, "variantRecordDataTypes") == 0) {
817 if (getCurrentMode() != DataTypesMode)
818 throw sg_exception("variantRecordDataTypes tag outside dataTypes!");
819 pushMode(VariantRecordDataTypesMode);
821 } else if (strcmp(name, "dataTypes") == 0) {
822 if (getCurrentMode() != ObjectModelMode)
823 throw sg_exception("dataTypes tag outside objectModel!");
824 pushMode(DataTypesMode);
826 } else if (strcmp(name, "objectModel") == 0) {
827 if (!_modeStack.empty())
828 throw sg_exception("objectModel tag not at top level!");
829 pushMode(ObjectModelMode);
832 _modeStack.push_back(UnknownMode);
837 HLAOMTXmlVisitor::endElement(const char* name)
839 if (strcmp(name, "objectClass") == 0) {
840 _objectClassStack.pop_back();
841 } else if (strcmp(name, "interactionClass") == 0) {
842 _interactionClassStack.pop_back();
843 } else if (strcmp(name, "enumeratedData") == 0) {
844 _enumeratedDataName.clear();
845 } else if (strcmp(name, "fixedRecordData") == 0) {
846 _fixedRecordDataName.clear();
847 } else if (strcmp(name, "variantRecordData") == 0) {
848 _variantRecordDataName.clear();
851 _modeStack.pop_back();
855 HLAOMTXmlVisitor::getAttribute(const char* name, const XMLAttributes& atts)
857 int index = atts.findAttribute(name);
858 if (index < 0 || atts.size() <= index)
859 return std::string();
860 return std::string(atts.getValue(index));
864 HLAOMTXmlVisitor::getAttribute(const std::string& name, const XMLAttributes& atts)
866 int index = atts.findAttribute(name.c_str());
867 if (index < 0 || atts.size() <= index)
868 return std::string();
869 return std::string(atts.getValue(index));
872 } // namespace simgear