1 // Copyright (C) 2009 - 2010 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.
18 #include "HLAOMTXmlVisitor.hxx"
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/xml/easyxml.hxx>
26 #include "HLAArrayDataType.hxx"
27 #include "HLABasicDataType.hxx"
28 #include "HLADataTypeVisitor.hxx"
29 #include "HLAEnumeratedDataType.hxx"
30 #include "HLAFixedRecordDataType.hxx"
31 #include "HLAVariantDataType.hxx"
35 HLAOMTXmlVisitor::ObjectClass::ObjectClass(const std::string& name, const std::string& sharing) :
41 HLAOMTXmlVisitor::ObjectClass::~ObjectClass()
46 HLAOMTXmlVisitor::ObjectClass::getName() const
52 HLAOMTXmlVisitor::ObjectClass::getSharing() const
58 HLAOMTXmlVisitor::ObjectClass::getNumAttributes() const
60 return _attributes.size();
63 const HLAOMTXmlVisitor::Attribute*
64 HLAOMTXmlVisitor::ObjectClass::getAttribute(unsigned index) const
66 if (_attributes.size() <= index)
68 return _attributes[index];
71 const HLAOMTXmlVisitor::Attribute*
72 HLAOMTXmlVisitor::ObjectClass::getAttribute(const std::string& name) const
74 for (AttributeList::const_iterator i = _attributes.begin(); i != _attributes.end(); ++i) {
75 if ((*i)->_name != name)
79 SG_LOG(SG_IO, SG_ALERT, "Could not find class attribute \"" << name << "\".");
83 const HLAOMTXmlVisitor::ObjectClass*
84 HLAOMTXmlVisitor::ObjectClass::getParentObjectClass() const
86 return _parentObjectClass.get();
89 HLAOMTXmlVisitor::InteractionClass::InteractionClass(const std::string& name) :
94 HLAOMTXmlVisitor::InteractionClass::~InteractionClass()
99 HLAOMTXmlVisitor::InteractionClass::getName() const
105 HLAOMTXmlVisitor::InteractionClass::getDimensions() const
111 HLAOMTXmlVisitor::InteractionClass::getTransportation() const
113 return _transportation;
117 HLAOMTXmlVisitor::InteractionClass::getOrder() const
123 HLAOMTXmlVisitor::InteractionClass::getNumParameters() const
125 return _parameters.size();
128 const HLAOMTXmlVisitor::Parameter*
129 HLAOMTXmlVisitor::InteractionClass::getParameter(unsigned index) const
131 if (_parameters.size() <= index)
133 return _parameters[index];
136 const HLAOMTXmlVisitor::Parameter*
137 HLAOMTXmlVisitor::InteractionClass::getParameter(const std::string& name) const
139 for (ParameterList::const_iterator i = _parameters.begin(); i != _parameters.end(); ++i) {
140 if ((*i)->_name != name)
144 SG_LOG(SG_IO, SG_ALERT, "Could not find parameter \"" << name << "\".");
148 const HLAOMTXmlVisitor::InteractionClass*
149 HLAOMTXmlVisitor::InteractionClass::getParentInteractionClass() const
151 return _parentInteractionClass.get();
154 HLAOMTXmlVisitor::HLAOMTXmlVisitor()
158 HLAOMTXmlVisitor::~HLAOMTXmlVisitor()
163 HLAOMTXmlVisitor::getNumObjectClasses() const
165 return _objectClassList.size();
168 const HLAOMTXmlVisitor::ObjectClass*
169 HLAOMTXmlVisitor::getObjectClass(unsigned i) const
171 if (_objectClassList.size() <= i)
173 return _objectClassList[i];
176 const HLAOMTXmlVisitor::ObjectClass*
177 HLAOMTXmlVisitor::getObjectClass(const std::string& name) const
179 for (ObjectClassList::const_iterator i = _objectClassList.begin(); i != _objectClassList.end(); ++i) {
180 if ((*i)->_name != name)
184 SG_LOG(SG_IO, SG_ALERT, "Could not resolve ObjectClass \"" << name << "\".");
188 const HLAOMTXmlVisitor::Attribute*
189 HLAOMTXmlVisitor::getAttribute(const std::string& objectClassName, const std::string& attributeName) const
191 const ObjectClass* objectClass = getObjectClass(objectClassName);
194 return objectClass->getAttribute(attributeName);
198 HLAOMTXmlVisitor::getAttributeDataType(const std::string& objectClassName, const std::string& attributeName) const
200 const Attribute* attribute = getAttribute(objectClassName, attributeName);
203 return getDataType(attribute->_dataType);
207 HLAOMTXmlVisitor::getNumInteractionClasses() const
209 return _interactionClassList.size();
212 const HLAOMTXmlVisitor::InteractionClass*
213 HLAOMTXmlVisitor::getInteractionClass(unsigned i) const
215 if (_interactionClassList.size() <= i)
217 return _interactionClassList[i];
220 const HLAOMTXmlVisitor::InteractionClass*
221 HLAOMTXmlVisitor::getInteractionClass(const std::string& name) const
223 for (InteractionClassList::const_iterator i = _interactionClassList.begin(); i != _interactionClassList.end(); ++i) {
224 if ((*i)->_name != name)
228 SG_LOG(SG_IO, SG_ALERT, "Could not resolve InteractionClass \"" << name << "\".");
232 const HLAOMTXmlVisitor::Parameter*
233 HLAOMTXmlVisitor::getParameter(const std::string& interactionClassName, const std::string& parameterName) const
235 const InteractionClass* interactionClass = getInteractionClass(interactionClassName);
236 if (!interactionClass)
238 return interactionClass->getParameter(parameterName);
242 HLAOMTXmlVisitor::getParameterDataType(const std::string& interactionClassName, const std::string& parameterName) const
244 const Parameter* parameter = getParameter(interactionClassName, parameterName);
247 return getDataType(parameter->_dataType);
251 HLAOMTXmlVisitor::getDataType(const std::string& dataTypeName) const
253 SGSharedPtr<HLADataType> dataType;
255 // Playing dirty things with reference counts
256 StringDataTypeMap dataTypeMap;
257 dataType = getDataType(dataTypeName, dataTypeMap);
259 return dataType.release();
262 SGSharedPtr<HLADataType>
263 HLAOMTXmlVisitor::getDataType(const std::string& dataTypeName, HLAOMTXmlVisitor::StringDataTypeMap& dataTypeMap) const
265 StringDataTypeMap::const_iterator i = dataTypeMap.find(dataTypeName);
266 if (i != dataTypeMap.end())
267 return new HLADataTypeReference(i->second);
269 SGSharedPtr<HLADataType> dataType;
270 dataType = getBasicDataType(dataTypeName);
271 if (dataType.valid())
274 dataType = getSimpleDataType(dataTypeName);
275 if (dataType.valid())
278 dataType = getEnumeratedDataType(dataTypeName);
279 if (dataType.valid())
282 dataType = getArrayDataType(dataTypeName, dataTypeMap);
283 if (dataType.valid())
286 dataType = getFixedRecordDataType(dataTypeName, dataTypeMap);
287 if (dataType.valid())
290 dataType = getVariantDataType(dataTypeName, dataTypeMap);
291 if (dataType.valid())
294 SG_LOG(SG_IO, SG_WARN, "Could not resolve dataType \"" << dataTypeName << "\".");
298 SGSharedPtr<HLABasicDataType>
299 HLAOMTXmlVisitor::getBasicDataType(const std::string& dataTypeName) const
301 BasicDataMap::const_iterator i = _basicDataMap.find(dataTypeName);
302 if (i == _basicDataMap.end())
304 if (i->second._size == "8") {
305 return new HLAUInt8DataType(dataTypeName);
306 } else if (i->second._size == "16") {
307 if (i->first.find("Unsigned") != std::string::npos) {
308 if (i->second._endian == "Little") {
309 return new HLAUInt16LEDataType(dataTypeName);
311 return new HLAUInt16BEDataType(dataTypeName);
313 } else if (i->first.find("octetPair") != std::string::npos) {
314 if (i->second._endian == "Little") {
315 return new HLAUInt16LEDataType(dataTypeName);
317 return new HLAUInt16BEDataType(dataTypeName);
320 if (i->second._endian == "Little") {
321 return new HLAInt16LEDataType(dataTypeName);
323 return new HLAInt16BEDataType(dataTypeName);
326 } else if (i->second._size == "32") {
327 if (i->first.find("Unsigned") != std::string::npos) {
328 if (i->second._endian == "Little") {
329 return new HLAUInt32LEDataType(dataTypeName);
331 return new HLAUInt32BEDataType(dataTypeName);
333 } else if (i->first.find("float") != std::string::npos) {
334 if (i->second._endian == "Little") {
335 return new HLAFloat32LEDataType(dataTypeName);
337 return new HLAFloat32BEDataType(dataTypeName);
340 if (i->second._endian == "Little") {
341 return new HLAInt32LEDataType(dataTypeName);
343 return new HLAInt32BEDataType(dataTypeName);
346 } else if (i->second._size == "64") {
347 if (i->first.find("Unsigned") != std::string::npos) {
348 if (i->second._endian == "Little") {
349 return new HLAUInt64LEDataType(dataTypeName);
351 return new HLAUInt64BEDataType(dataTypeName);
353 } else if (i->first.find("float") != std::string::npos) {
354 if (i->second._endian == "Little") {
355 return new HLAFloat64LEDataType(dataTypeName);
357 return new HLAFloat64BEDataType(dataTypeName);
360 if (i->second._endian == "Little") {
361 return new HLAInt64LEDataType(dataTypeName);
363 return new HLAInt64BEDataType(dataTypeName);
371 SGSharedPtr<HLADataType>
372 HLAOMTXmlVisitor::getSimpleDataType(const std::string& dataTypeName) const
374 SimpleDataMap::const_iterator i = _simpleDataMap.find(dataTypeName);
375 if (i == _simpleDataMap.end())
377 return getDataType(i->second._representation);
380 SGSharedPtr<HLAEnumeratedDataType>
381 HLAOMTXmlVisitor::getEnumeratedDataType(const std::string& dataTypeName) const
383 EnumeratedDataMap::const_iterator i = _enumeratedDataMap.find(dataTypeName);
384 if (i == _enumeratedDataMap.end())
387 SGSharedPtr<HLAEnumeratedDataType> enumeratedDataType = new HLAEnumeratedDataType(dataTypeName);
388 enumeratedDataType->setRepresentation(getBasicDataType(i->second._representation));
390 for (EnumeratorList::const_iterator j = i->second._enumeratorList.begin();
391 j != i->second._enumeratorList.end(); ++j) {
392 if (!enumeratedDataType->addEnumerator(j->_name, j->_values)) {
393 SG_LOG(SG_IO, SG_ALERT, "Could not add enumerator \"" << j->_name
394 << "\" to find enumerated data type \"" << dataTypeName << "\".");
399 return enumeratedDataType;
402 SGSharedPtr<HLADataType>
403 HLAOMTXmlVisitor::getArrayDataType(const std::string& dataTypeName, HLAOMTXmlVisitor::StringDataTypeMap& dataTypeMap) const
405 ArrayDataMap::const_iterator i = _arrayDataMap.find(dataTypeName);
406 if (i == _arrayDataMap.end())
408 SGSharedPtr<HLAArrayDataType> arrayDataType;
409 if (i->second._encoding == "HLAvariableArray") {
410 arrayDataType = new HLAVariableArrayDataType(dataTypeName);
411 } else if (i->second._encoding == "HLAfixedArray") {
412 std::stringstream ss(i->second._cardinality);
413 unsigned cardinality;
416 SG_LOG(SG_IO, SG_ALERT, "Could not interpret cardinality \""
417 << i->second._cardinality << "\" for dataType \""
418 << dataTypeName << "\".");
421 SGSharedPtr<HLAFixedArrayDataType> dataType = new HLAFixedArrayDataType(dataTypeName);
422 dataType->setNumElements(cardinality);
423 arrayDataType = dataType;
425 SG_LOG(SG_IO, SG_ALERT, "Can not interpret encoding \""
426 << i->second._encoding << "\" for dataType \""
427 << dataTypeName << "\".");
431 dataTypeMap[dataTypeName] = arrayDataType;
432 SGSharedPtr<HLADataType> elementDataType = getDataType(i->second._dataType, dataTypeMap);
433 if (!elementDataType.valid()) {
434 SG_LOG(SG_IO, SG_ALERT, "Could not interpret dataType \""
435 << i->second._dataType << "\" for array data type \""
436 << dataTypeName << "\".");
437 dataTypeMap.erase(dataTypeName);
440 arrayDataType->setElementDataType(elementDataType.get());
442 return arrayDataType;
445 SGSharedPtr<HLAFixedRecordDataType>
446 HLAOMTXmlVisitor::getFixedRecordDataType(const std::string& dataTypeName, HLAOMTXmlVisitor::StringDataTypeMap& dataTypeMap) const
448 FixedRecordDataMap::const_iterator i = _fixedRecordDataMap.find(dataTypeName);
449 if (i == _fixedRecordDataMap.end())
452 SGSharedPtr<HLAFixedRecordDataType> dataType = new HLAFixedRecordDataType(dataTypeName);
453 dataTypeMap[dataTypeName] = dataType;
454 for (FieldList::size_type j = 0; j < i->second._fieldList.size(); ++j) {
455 SGSharedPtr<HLADataType> fieldDataType = getDataType(i->second._fieldList[j]._dataType, dataTypeMap);
456 if (!fieldDataType.valid()) {
457 SG_LOG(SG_IO, SG_ALERT, "Could not get data type \"" << i->second._fieldList[j]._dataType
458 << "\" for field " << j << "of fixed record data type \"" << dataTypeName << "\".");
459 dataTypeMap.erase(dataTypeName);
462 dataType->addField(i->second._fieldList[j]._name, fieldDataType.get());
467 SGSharedPtr<HLAVariantDataType>
468 HLAOMTXmlVisitor::getVariantDataType(const std::string& dataTypeName, HLAOMTXmlVisitor::StringDataTypeMap& dataTypeMap) const
470 VariantRecordDataMap::const_iterator i = _variantRecordDataMap.find(dataTypeName);
471 if (i == _variantRecordDataMap.end())
473 SGSharedPtr<HLAVariantDataType> dataType = new HLAVariantDataType(dataTypeName);
474 dataTypeMap[dataTypeName] = dataType;
476 SGSharedPtr<HLAEnumeratedDataType> enumeratedDataType = getEnumeratedDataType(i->second._dataType);
477 if (!enumeratedDataType.valid()) {
478 SG_LOG(SG_IO, SG_ALERT, "Could not find enumerted data type \"" << i->second._dataType
479 << "\" for variant data type \"" << dataTypeName << "\".");
482 dataType->setEnumeratedDataType(enumeratedDataType);
484 for (AlternativeList::const_iterator j = i->second._alternativeList.begin();
485 j != i->second._alternativeList.end(); ++j) {
486 SGSharedPtr<HLADataType> alternativeDataType = getDataType(j->_dataType, dataTypeMap);
487 if (!alternativeDataType.valid()) {
488 SG_LOG(SG_IO, SG_ALERT, "Could not resolve alternative dataType \"" << j->_dataType
489 << "\" for alternative \"" << j->_name << "\".");
490 dataTypeMap.erase(dataTypeName);
493 if (!dataType->addAlternative(j->_name, j->_enumerator, alternativeDataType.get(), j->_semantics)) {
494 SG_LOG(SG_IO, SG_ALERT, "Could not add alternative \"" << j->_name << "\".");
501 HLAOMTXmlVisitor::Mode
502 HLAOMTXmlVisitor::getCurrentMode()
504 if (_modeStack.empty())
506 return _modeStack.back();
510 HLAOMTXmlVisitor::pushMode(HLAOMTXmlVisitor::Mode mode)
512 _modeStack.push_back(mode);
516 HLAOMTXmlVisitor::popMode()
518 _modeStack.pop_back();
522 HLAOMTXmlVisitor::startXML()
528 HLAOMTXmlVisitor::endXML()
530 if (!_modeStack.empty())
531 throw sg_exception("Internal parse error!");
533 // propagate parent attributes to the derived classes
534 for (ObjectClassList::const_iterator i = _objectClassList.begin(); i != _objectClassList.end(); ++i) {
535 SGSharedPtr<const ObjectClass> objectClass = (*i)->_parentObjectClass;
536 while (objectClass) {
537 for (AttributeList::const_reverse_iterator j = objectClass->_attributes.rbegin();
538 j != objectClass->_attributes.rend(); ++j) {
539 (*i)->_attributes.insert((*i)->_attributes.begin(), *j);
541 objectClass = objectClass->_parentObjectClass;
545 // propagate parent parameter to the derived interactions
546 for (InteractionClassList::const_iterator i = _interactionClassList.begin(); i != _interactionClassList.end(); ++i) {
547 SGSharedPtr<const InteractionClass> interactionClass = (*i)->_parentInteractionClass;
548 while (interactionClass) {
549 for (ParameterList::const_reverse_iterator j = interactionClass->_parameters.rbegin();
550 j != interactionClass->_parameters.rend(); ++j) {
551 (*i)->_parameters.insert((*i)->_parameters.begin(), *j);
553 interactionClass = interactionClass->_parentInteractionClass;
559 HLAOMTXmlVisitor::startElement(const char* name, const XMLAttributes& atts)
561 if (strcmp(name, "attribute") == 0) {
562 if (getCurrentMode() != ObjectClassMode)
563 throw sg_exception("attribute tag outside objectClass!");
564 pushMode(AttributeMode);
566 if (_objectClassList.empty())
567 throw sg_exception("attribute tag outside of an objectClass");
569 std::string name = getAttribute("name", atts);
571 throw sg_exception("attribute tag without name attribute");
573 SGSharedPtr<Attribute> attribute = new Attribute(name);
575 attribute->_dataType = getAttribute("dataType", atts);
576 attribute->_updateType = getAttribute("updateType", atts);
577 attribute->_updateCondition = getAttribute("updateCondition", atts);
578 attribute->_ownership = getAttribute("ownership", atts);
579 attribute->_sharing = getAttribute("sharing", atts);
580 attribute->_dimensions = getAttribute("dimensions", atts);
581 attribute->_transportation = getAttribute("transportation", atts);
582 attribute->_order = getAttribute("order", atts);
584 _objectClassStack.back()->_attributes.push_back(attribute);
586 } else if (strcmp(name, "objectClass") == 0) {
587 if (getCurrentMode() != ObjectsMode && getCurrentMode() != ObjectClassMode)
588 throw sg_exception("objectClass tag outside objectClass or objects!");
589 pushMode(ObjectClassMode);
591 std::string name = getAttribute("name", atts);
593 throw sg_exception("objectClass tag without name attribute");
595 std::string sharing = getAttribute("sharing", atts);
597 // The new ObjectClass
598 ObjectClass* objectClass = new ObjectClass(name, sharing);
600 // Inherit all previous attributes
601 if (!_objectClassStack.empty())
602 objectClass->_parentObjectClass = _objectClassStack.back();
604 _objectClassStack.push_back(objectClass);
605 _objectClassList.push_back(objectClass);
607 } else if (strcmp(name, "objects") == 0) {
608 if (getCurrentMode() != ObjectModelMode)
609 throw sg_exception("objects tag outside objectModel!");
610 pushMode(ObjectsMode);
612 } else if (strcmp(name, "parameter") == 0) {
613 if (getCurrentMode() != InteractionClassMode)
614 throw sg_exception("parameter tag outside interactionClass!");
615 pushMode(ParameterMode);
617 if (_interactionClassList.empty())
618 throw sg_exception("parameter tag outside of an interactionClass");
620 std::string name = getAttribute("name", atts);
622 throw sg_exception("parameter tag without name parameter");
624 SGSharedPtr<Parameter> parameter = new Parameter(name);
625 parameter->_dataType = getAttribute("dataType", atts);
627 _interactionClassStack.back()->_parameters.push_back(parameter);
629 } else if (strcmp(name, "interactionClass") == 0) {
630 if (getCurrentMode() != InteractionsMode && getCurrentMode() != InteractionClassMode)
631 throw sg_exception("interactionClass tag outside interactions or interactionClass!");
632 pushMode(InteractionClassMode);
634 std::string name = getAttribute("name", atts);
636 throw sg_exception("interactionClass tag without name attribute");
638 // The new ObjectClass
639 InteractionClass* interactionClass = new InteractionClass(name);
640 interactionClass->_dimensions = getAttribute("dimensions", atts);
641 interactionClass->_transportation = getAttribute("transportation", atts);
642 interactionClass->_order = getAttribute("order", atts);
644 // Inherit all previous attributes
645 if (!_interactionClassStack.empty())
646 interactionClass->_parentInteractionClass = _interactionClassStack.back();
648 _interactionClassStack.push_back(interactionClass);
649 _interactionClassList.push_back(interactionClass);
651 } else if (strcmp(name, "interactions") == 0) {
652 if (getCurrentMode() != ObjectModelMode)
653 throw sg_exception("interactions tag outside objectModel!");
654 pushMode(InteractionsMode);
656 } else if (strcmp(name, "basicData") == 0) {
657 if (getCurrentMode() != BasicDataRepresentationsMode)
658 throw sg_exception("basicData tag outside basicDataRepresentations!");
659 pushMode(BasicDataMode);
661 std::string name = getAttribute("name", atts);
663 throw sg_exception("basicData tag without name attribute");
665 _basicDataMap[name]._size = getAttribute("size", atts);
666 _basicDataMap[name]._endian = getAttribute("endian", atts);
668 } else if (strcmp(name, "basicDataRepresentations") == 0) {
669 if (getCurrentMode() != DataTypesMode)
670 throw sg_exception("basicDataRepresentations tag outside dataTypes!");
671 pushMode(BasicDataRepresentationsMode);
673 } else if (strcmp(name, "simpleData") == 0) {
674 if (getCurrentMode() != SimpleDataTypesMode)
675 throw sg_exception("simpleData tag outside simpleDataTypes!");
676 pushMode(SimpleDataMode);
678 std::string name = getAttribute("name", atts);
680 throw sg_exception("simpleData tag without name attribute");
682 _simpleDataMap[name]._representation = getAttribute("representation", atts);
683 _simpleDataMap[name]._units = getAttribute("units", atts);
684 _simpleDataMap[name]._resolution = getAttribute("resolution", atts);
685 _simpleDataMap[name]._accuracy = getAttribute("accuracy", atts);
687 } else if (strcmp(name, "simpleDataTypes") == 0) {
688 if (getCurrentMode() != DataTypesMode)
689 throw sg_exception("simpleDataTypes tag outside dataTypes!");
690 pushMode(SimpleDataTypesMode);
692 } else if (strcmp(name, "enumerator") == 0) {
693 if (getCurrentMode() != EnumeratedDataMode)
694 throw sg_exception("enumerator tag outside enumeratedData!");
695 pushMode(EnumeratorMode);
697 std::string name = getAttribute("name", atts);
699 throw sg_exception("enumerator tag without name attribute");
701 Enumerator enumerator;
702 enumerator._name = name;
703 enumerator._values = getAttribute("values", atts);
704 _enumeratedDataMap[_enumeratedDataName]._enumeratorList.push_back(enumerator);
706 } else if (strcmp(name, "enumeratedData") == 0) {
707 if (getCurrentMode() != EnumeratedDataTypesMode)
708 throw sg_exception("enumeratedData tag outside enumeratedDataTypes!");
709 pushMode(EnumeratedDataMode);
711 std::string name = getAttribute("name", atts);
713 throw sg_exception("enumeratedData tag without name attribute");
715 _enumeratedDataName = name;
716 _enumeratedDataMap[_enumeratedDataName]._representation = getAttribute("representation", atts);
718 } else if (strcmp(name, "enumeratedDataTypes") == 0) {
719 if (getCurrentMode() != DataTypesMode)
720 throw sg_exception("enumeratedDataTypes tag outside dataTypes!");
721 pushMode(EnumeratedDataTypesMode);
723 Enumerator enumerator;
724 enumerator._name = getAttribute("name", atts);
725 enumerator._values = getAttribute("values", atts);
726 _enumeratedDataMap[_enumeratedDataName]._enumeratorList.push_back(enumerator);
728 } else if (strcmp(name, "arrayData") == 0) {
729 if (getCurrentMode() != ArrayDataTypesMode)
730 throw sg_exception("arrayData tag outside arrayDataTypes!");
731 pushMode(ArrayDataMode);
733 std::string name = getAttribute("name", atts);
735 throw sg_exception("arrayData tag without name attribute");
737 _arrayDataMap[name]._dataType = getAttribute("dataType", atts);
738 _arrayDataMap[name]._cardinality = getAttribute("cardinality", atts);
739 _arrayDataMap[name]._encoding = getAttribute("encoding", atts);
741 } else if (strcmp(name, "arrayDataTypes") == 0) {
742 if (getCurrentMode() != DataTypesMode)
743 throw sg_exception("arrayDataTypes tag outside dataTypes!");
744 pushMode(ArrayDataTypesMode);
746 } else if (strcmp(name, "field") == 0) {
747 if (getCurrentMode() != FixedRecordDataMode)
748 throw sg_exception("field tag outside fixedRecordData!");
751 std::string name = getAttribute("name", atts);
753 throw sg_exception("field tag without name attribute");
757 field._dataType = getAttribute("dataType", atts);
758 _fixedRecordDataMap[_fixedRecordDataName]._fieldList.push_back(field);
760 } else if (strcmp(name, "fixedRecordData") == 0) {
761 if (getCurrentMode() != FixedRecordDataTypesMode)
762 throw sg_exception("fixedRecordData tag outside fixedRecordDataTypes!");
763 pushMode(FixedRecordDataMode);
765 std::string name = getAttribute("name", atts);
767 throw sg_exception("fixedRecordData tag without name attribute");
769 _fixedRecordDataName = name;
770 _fixedRecordDataMap[name]._encoding = getAttribute("encoding", atts);
772 } else if (strcmp(name, "fixedRecordDataTypes") == 0) {
773 if (getCurrentMode() != DataTypesMode)
774 throw sg_exception("fixedRecordDataTypes tag outside dataTypes!");
775 pushMode(FixedRecordDataTypesMode);
777 } else if (strcmp(name, "alternative") == 0) {
779 if (getCurrentMode() != VariantRecordDataMode)
780 throw sg_exception("alternative tag outside variantRecordData!");
781 pushMode(AlternativeDataMode);
783 std::string name = getAttribute("name", atts);
785 throw sg_exception("alternative tag without name attribute");
787 Alternative alternative;
788 alternative._name = name;
789 alternative._dataType = getAttribute("dataType", atts);
790 alternative._semantics = getAttribute("semantics", atts);
791 alternative._enumerator = getAttribute("enumerator", atts);
792 _variantRecordDataMap[_variantRecordDataName]._alternativeList.push_back(alternative);
794 } else if (strcmp(name, "variantRecordData") == 0) {
795 if (getCurrentMode() != VariantRecordDataTypesMode)
796 throw sg_exception("variantRecordData tag outside variantRecordDataTypes!");
797 pushMode(VariantRecordDataMode);
799 std::string name = getAttribute("name", atts);
801 throw sg_exception("fixedRecordData tag without name attribute");
803 _variantRecordDataName = name;
804 _variantRecordDataMap[name]._encoding = getAttribute("encoding", atts);
805 _variantRecordDataMap[name]._dataType = getAttribute("dataType", atts);
806 _variantRecordDataMap[name]._semantics = getAttribute("semantics", atts);
807 _variantRecordDataMap[name]._discriminant = getAttribute("discriminant", atts);
809 } else if (strcmp(name, "variantRecordDataTypes") == 0) {
810 if (getCurrentMode() != DataTypesMode)
811 throw sg_exception("variantRecordDataTypes tag outside dataTypes!");
812 pushMode(VariantRecordDataTypesMode);
814 } else if (strcmp(name, "dataTypes") == 0) {
815 if (getCurrentMode() != ObjectModelMode)
816 throw sg_exception("dataTypes tag outside objectModel!");
817 pushMode(DataTypesMode);
819 } else if (strcmp(name, "objectModel") == 0) {
820 if (!_modeStack.empty())
821 throw sg_exception("objectModel tag not at top level!");
822 pushMode(ObjectModelMode);
825 _modeStack.push_back(UnknownMode);
830 HLAOMTXmlVisitor::endElement(const char* name)
832 if (strcmp(name, "objectClass") == 0) {
833 _objectClassStack.pop_back();
834 } else if (strcmp(name, "interactionClass") == 0) {
835 _interactionClassStack.pop_back();
836 } else if (strcmp(name, "enumeratedData") == 0) {
837 _enumeratedDataName.clear();
838 } else if (strcmp(name, "fixedRecordData") == 0) {
839 _fixedRecordDataName.clear();
840 } else if (strcmp(name, "variantRecordData") == 0) {
841 _variantRecordDataName.clear();
844 _modeStack.pop_back();
848 HLAOMTXmlVisitor::getAttribute(const char* name, const XMLAttributes& atts)
850 int index = atts.findAttribute(name);
851 if (index < 0 || atts.size() <= index)
852 return std::string();
853 return std::string(atts.getValue(index));
857 HLAOMTXmlVisitor::getAttribute(const std::string& name, const XMLAttributes& atts)
859 int index = atts.findAttribute(name.c_str());
860 if (index < 0 || atts.size() <= index)
861 return std::string();
862 return std::string(atts.getValue(index));
865 } // namespace simgear