1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
#ifdef _MSC_VER
#pragma warning(disable:4786) // symbol truncated
#endif // _MSC_VER
#include <Inventor/scxml/ScXML.h>
#include <cassert>
#include <cstring>
#include <Inventor/SbName.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/C/XML/document.h>
#include <Inventor/C/XML/element.h>
#include <Inventor/C/XML/attribute.h>
#include <Inventor/scxml/ScXMLObject.h>
#include <Inventor/scxml/ScXMLEvent.h>
#include <Inventor/scxml/ScXMLInvoke.h>
#include <Inventor/scxml/ScXMLStateMachine.h>
#include <Inventor/scxml/SoScXMLEvent.h>
#include <Inventor/scxml/SoScXMLStateMachine.h>
#include "scxml/ScXMLDocument.h"
#include "scxml/ScXMLState.h"
#include "scxml/ScXMLInitial.h"
#include "scxml/ScXMLFinal.h"
#include "scxml/ScXMLTransition.h"
#include "scxml/ScXMLOnEntry.h"
#include "scxml/ScXMLOnExit.h"
#include "scxml/ScXMLAnchor.h"
#include "scxml/ScXMLHistory.h"
#include "tidbitsp.h"
#include "misc/CoinResources.h"
#include "scxml/ScXMLP.h"
#include "scxml/ScXMLFallbackInvoke.h"
#include "scxml/SoScXMLDebugInfoInvoke.h"
#include "scxml/SoScXMLScheduleRedrawInvoke.h"
#include "scxml/SoScXMLScheduleTimerEventInvoke.h"
// *************************************************************************
/*!
\page scxml State Chart XML
The ScXML part of Coin is a basic, non-conformant, partial
implementation of State Chart XML, based on the W3C Working Draft 21
February 2007 of SCXML <http://www.w3.org/TR/2007/WD-scxml-20070221/>.
Read that document for a basic understanding how SCXML documents
should be constructed.
NOTE: A new version of the draft has just been released (May 2008).
The code here does not reflect any changes done to the draft SCXML
specification yet.
Coin uses it for its navigation system, to be able to remove
hardcoded logic for user navigation and externalize it into XML
files. It is hoped that this technology will prove to be a useful
addition to Coin and get broader use as this subsystem matures. The
dragger and manipulator user interaction logic is likely one area
where ScXML might be employed later. Attaching state machines to
interactive parts of the scene graph could be another interesting
idea for the future.
For now, the ScXML system is not employed in any pre-established
Coin usage patterns, and you will need to employ new features to touch
base with it. The most obvious are where ScXML will be used is if
you use Quarter 1.0 over Coin 3.0, as you manipulate the camera
movements around the 3D models through mouse and keyboard interaction.
The Coin type system makes it possible to override the default types
to have the state chart description instantiated using customized
objects. A design choice made in that regard was to both use the
type overriding system in Coin, and also to use the 'xmlns'
(XML Namespace) attribute to make ScXML types available and to
prioritize which classtypes to instantiate objects from. The xmlns
attribute is currently only checked at the document level, but the
plan is to let any state have this attribute, and to simulate
static scoping rules for namespace prioritization.
Partially Supported Items:
- At the moment, the Coin ScXML module only supports "Executable
Content" through the <invoke> element, using the ScXMLInvoke
class interface and having the Executable Content written in C++
in subclasses of the ScXMLInvoke class. ECMAScript and XPath is
not supported yet.
Unsupported Items:
- External document referencing through the <state> /
<parallel> 'src' attribute is not yet handled, but should be
easy to implement so it will likely be one of the first things
that will be fixed.
- The <parallel> element is not supported as intended with
parallel states. Coin will just treat it as an ordinary
<state> element for now. Parallel states is not high up on
the priority list, so expect this to be handled after a lot of
other functionality is in place.
- The <datamodel>-related part of the specification is not
supported.
- The condition attribute in the <transition> element is not
supported, so the only condition you can set is on the event type
in the event attribute. The ScXMLTransition class has a virtual
method
SbBool evaluateCondition()
which is used in the state machine logic, and should be all that is
needed to overload to implement this in subclasses yourself.
- The 'target' attribute in the <transition> element can only
identify a single state currently, not multiple as you would have
to when having support for <parallel> elements (which we
don't have).
- The virtual state elements like <history> and <anchor>
are just implemented as dummy states for now and do not do
anything in relation to what they should actually do.
- There are no mechanisms for inter-statemachine event passing yet.
For learning more about how ScXML is implemented and used in Coin,
take a look at $COINDIR/scxml/navigation/examiner.xml (or in the
Coin source directory, data/scxml/navigation/examiner.xml) for an
example of how an SCXML system for camera navigation looks,
and look at the ScXML* source files in src/navigation/ for the C++
counterparts to the same SCXML navigation system.
With support for <datamodel>, <transition>-conditions,
and inline executable content in the XML file in some scripting
language, the C++ parts could probably have been greatly simplified,
if not more or less eliminated. This will hopefully evolve for
future versions of Coin.
\ingroup scxml
\since Coin 3.0
*/
// *************************************************************************
// private static variables
ScXMLP::NamespaceDict *ScXMLP::namespaces;
ScXMLP::TargettypeDict *ScXMLP::targettypes;
SbBool ScXMLP::no_datamodel_warning = FALSE;
// *************************************************************************
/*!
\class ScXML ScXML.h Inventor/scxml/ScXML.h
\brief Namespace for static ScXML-related functions.
This is a static namespace class for ScXML-related functions.
\ingroup scxml
\since Coin 3.0
*/
/*!
Initializes the basic ScXML classes.
*/
void
ScXML::initClasses(void)
{
ScXMLP::init();
ScXMLObject::initClass();
ScXMLDocument::initClass();
ScXMLState::initClass();
ScXMLInitial::initClass();
ScXMLFinal::initClass();
ScXMLTransition::initClass();
ScXMLOnEntry::initClass();
ScXMLOnExit::initClass();
ScXMLEvent::initClass();
ScXMLInvoke::initClass();
ScXMLFallbackInvoke::initClass();
ScXMLStateMachine::initClass();
ScXMLAnchor::initClass();
ScXMLHistory::initClass();
SoScXMLEvent::initClass();
SoScXMLStateMachine::initClass();
SoScXMLDebugInfoInvoke::initClass();
SoScXMLScheduleRedrawInvoke::initClass();
SoScXMLScheduleTimerEventInvoke::initClass();
}
// *************************************************************************
// SCXML FILE INPUT
// *************************************************************************
/*!
This function reads in an SCXML document from a file.
\returns an ScXMLDocument-derived statemachine object hierarchy.
*/
ScXMLStateMachine *
ScXML::readFile(const char * filename)
{
if (strncmp(filename, "coin:", 5) == 0) { // is a "resource"
const char * buffer = NULL;
size_t buffersize = 0;
if (CoinResources::get(filename, buffer, buffersize)) {
return ScXML::readBuffer(buffer);
}
}
cc_xml_doc * doc = cc_xml_doc_new();
if (!cc_xml_doc_read_file_x(doc, filename)) {
cc_xml_doc_delete_x(doc);
return NULL;
}
ScXMLStateMachine * statemachine = ScXMLP::readXMLData(doc);
cc_xml_doc_delete_x(doc);
if (statemachine) {
statemachine->setName(filename);
}
return statemachine;
}
/*!
This function reads in an SCXML document residing in memory.
\returns an ScXMLDocument-derived statemachine object hierarchy.
*/
ScXMLStateMachine *
ScXML::readBuffer(const char * bufferdata)
{
cc_xml_doc * doc = cc_xml_doc_new();
if (!cc_xml_doc_read_buffer_x(doc, bufferdata, strlen(bufferdata))) {
cc_xml_doc_delete_x(doc);
return NULL;
}
ScXMLStateMachine * statemachine = ScXMLP::readXMLData(doc);
cc_xml_doc_delete_x(doc);
return statemachine;
}
// *************************************************************************
// TYPE SYSTEM REGISTERING AND LOOKUP
// *************************************************************************
/*
*/
void
ScXMLP::registerClassType(const char * xmlns, const char * classname, SoType type)
{
assert(!type.isBad());
ScXMLP::registerClassType(ScXMLP::namespaces, xmlns, classname, type);
}
/*
*/
void
ScXMLP::registerInvokeClassType(const char * xmlns, const char * targettype, const char * source, SoType type)
{
assert(!type.isBad());
assert(xmlns);
// Note that we actually split on targettype on the top of the
// hierarchy/tree here, and not on xml-namespace as one might think
// at first, but this is intentional. This is because the namespace
// logic should fall through to the default namespace if you don't
// find the type you are looking for in the specified namespace, and
// because of that, it is most convenient to split on namespace at
// the next-to-bottom level to be able to share code when looking up
// types.
ScXMLP::NamespaceDict * nsdict = ScXMLP::getNamespaceDict(ScXMLP::targettypes, targettype);
assert(nsdict);
ScXMLP::registerClassType(nsdict, xmlns, source, type);
}
/*
*/
void
ScXMLP::registerClassType(NamespaceDict * nsdict, const char * xmlns, const char * classname, SoType type)
{
assert(xmlns);
TypeDict * typedict = ScXMLP::getTypeDict(nsdict, xmlns);
assert(typedict);
SbName classnamename(classname);
const char * key = classnamename.getString();
TypeDict::iterator it = typedict->find(key);
if (it != typedict->end()) {
it->second = type;
} else {
typedict->insert(TypeEntry(key, type));
}
}
/*
*/
ScXMLP::TypeDict *
ScXMLP::getTypeDict(NamespaceDict * nsdict, const char * xmlns)
{
SbName xmlnsname(xmlns); // uniqify on string pointer
const char * key = xmlnsname.getString();
NamespaceDict::iterator it = nsdict->find(key);
if (it != nsdict->end()) {
return it->second;
} else {
TypeDict * dict = new TypeDict;
nsdict->insert(NamespaceEntry(key, dict));
return dict;
}
}
// *************************************************************************
/*
*/
SoType
ScXMLP::getClassType(NamespaceDict * nsdict, const char * xmlns, const char * classname)
{
assert(xmlns && classname);
const SbName classnamename(classname);
const char * key = classnamename.getString();
ScXMLP::TypeDict * typedict = NULL;
typedict = ScXMLP::getTypeDict(nsdict, xmlns);
if (typedict) {
TypeDict::iterator it = typedict->find(key);
if (it != typedict->end()) {
return it->second;
}
}
typedict = ScXMLP::getTypeDict(nsdict, SCXML_DEFAULT_NS);
if (typedict) {
TypeDict::iterator it = typedict->find(key);
if (it != typedict->end()) {
return it->second;
}
}
return SoType::badType();
}
/*
*/
ScXMLP::NamespaceDict *
ScXMLP::getNamespaceDict(TargettypeDict * ttdict, const char * targettype)
{
SbName targettypename(targettype); // uniqify on string pointer
const char * key = targettypename.getString();
TargettypeDict::iterator it = ttdict->find(key);
if (it != ttdict->end()) {
return it->second;
} else {
NamespaceDict * dict = new NamespaceDict;
ttdict->insert(TargettypeEntry(key, dict));
return dict;
}
}
/*
*/
SoType
ScXMLP::getClassType(const char * xmlns, const char * classname)
{
return ScXMLP::getClassType(ScXMLP::namespaces, xmlns, classname);
}
SoType
ScXMLP::getInvokeClassType(const char * xmlns, const char * targettype, const char * source)
{
NamespaceDict * nsdict = ScXMLP::getNamespaceDict(ScXMLP::targettypes, targettype);
assert(nsdict);
SoType invoketype = ScXMLP::getClassType(nsdict, xmlns, source);
if (invoketype == SoType::badType()) {
return ScXMLFallbackInvoke::getClassTypeId();
}
return invoketype;
}
// *************************************************************************
// internal implementation
ScXMLStateMachine *
ScXMLP::readXMLData(cc_xml_doc * doc)
{
ScXMLP::no_datamodel_warning = FALSE;
const char * xmlns = SCXML_DEFAULT_NS;
// get a handle on the root element
cc_xml_elt * root = cc_xml_doc_get_root(doc);
if (strcmp(cc_xml_elt_get_type(root), "scxml") != 0) {
return NULL;
}
// peek at xmlns attribute to get the correct object types instantiated
const cc_xml_attr * nsattr = cc_xml_elt_get_attribute(root, "xmlns");
if (nsattr) {
xmlns = cc_xml_attr_get_value(nsattr);
assert(xmlns);
}
SoType statemachinetype = ScXMLP::getClassType(xmlns, "statemachine");
assert(statemachinetype.canCreateInstance());
ScXMLStateMachine * statemachine =
static_cast<ScXMLStateMachine *>(statemachinetype.createInstance());
assert(statemachine && statemachine->isOfType(ScXMLStateMachine::getClassTypeId()));
ScXMLObject * documentobj = ScXMLP::readScXMLDocument(statemachine, root, xmlns);
assert(documentobj && documentobj->isOfType(ScXMLDocument::getClassTypeId()));
ScXMLDocument * document = static_cast<ScXMLDocument *>(documentobj);
statemachine->setDescription(document);
return statemachine;
}
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLDocument(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(elt && xmlns);
// instantiate root ScXMLDocument object
SoType documenttype = ScXMLP::getClassType(xmlns, "scxml");
assert(documenttype.canCreateInstance());
ScXMLDocument * document = static_cast<ScXMLDocument *>(documenttype.createInstance());
// handle XML attributes
assert(document && document->isOfType(ScXMLDocument::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
document->setXMLAttribute(attrname, attrvalue);
}
if (!document->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete document;
return NULL;
}
document->setContainer(container);
// read in all children, and recurse down
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// <state> - zero or more times
if (strcmp(elementtype, "state") == 0) {
ScXMLObject * stateobj = ScXMLP::readScXMLState(document, element, xmlns);
if (stateobj) {
assert(stateobj->isOfType(ScXMLState::getClassTypeId()));
document->addState(static_cast<ScXMLState *>(stateobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <state> element");
delete document;
return NULL;
}
}
// <parallel> - zero or more times
else if (strcmp(elementtype, "parallel") == 0) {
ScXMLObject * parallelobj = ScXMLP::readScXMLParallel(document, element, xmlns);
if (parallelobj) {
assert(parallelobj->isOfType(ScXMLState::getClassTypeId()));
document->addParallel(static_cast<ScXMLState *>(parallelobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <parallel> element");
delete document;
return NULL;
}
}
// <final> - one or more times
else if (strcmp(elementtype, "final") == 0) {
ScXMLObject * finalobj = ScXMLP::readScXMLFinal(document, element, xmlns);
if (finalobj) {
assert(finalobj->isOfType(ScXMLFinal::getClassTypeId()));
document->addFinal(static_cast<ScXMLFinal *>(finalobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <final> element");
delete document;
return NULL;
}
}
// <datamodel> - zero or one time
else if (strcmp(elementtype, "datamodel") == 0) {
ScXMLObject * datamodelobj = ScXMLP::readScXMLDatamodel(document, element, xmlns);
if (datamodelobj) {
// document->addDatamodel(datamodelobj);
} else {
SoDebugError::post("ScXML::readFile", "error reading <datamodel> element");
delete document;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile", "unexpected XML element '<%s>' found in <scxml>",
elementtype);
delete document;
return NULL;
}
}
return document;
} // readFile
// *************************************************************************
// internal - reads in the base state object rooted at 'elt' and recurses for children
ScXMLObject *
ScXMLP::readScXMLState(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns, SbBool isparallel)
{
// thisw function is multi-purpose, and reads in both <state> and <parallel> elements
assert(container && elt && xmlns);
SoType statetype = ScXMLP::getClassType(xmlns, "state");
assert(statetype.canCreateInstance());
ScXMLState * state = static_cast<ScXMLState *>(statetype.createInstance());
assert(state && state->isOfType(ScXMLState::getClassTypeId()));
state->setIsParallel(isparallel);
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
state->setXMLAttribute(attrname, attrvalue);
}
if (!state->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete state;
return NULL;
}
state->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// <onentry> - zero or one time
if (strcmp(elementtype, "onentry") == 0) {
ScXMLObject * element, xmlns);
if (onentryobj) {
assert(onentryobj->isOfType(ScXMLOnEntry::getClassTypeId()));
state->setOnEntry(static_cast<ScXMLOnEntry *>(onentryobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <onexit> - zero or one time
else if (strcmp(elementtype, "onexit") == 0) {
ScXMLObject * element, xmlns);
if (onexitobj) {
assert(onexitobj->isOfType(ScXMLOnExit::getClassTypeId()));
state->setOnExit(static_cast<ScXMLOnExit *>(onexitobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <transition> - zero or more times
else if (!isparallel && strcmp(elementtype, "transition") == 0) {
ScXMLObject * transitionobj = ScXMLP::readScXMLTransition(state, element, xmlns);
if (transitionobj) {
assert(transitionobj->isOfType(ScXMLTransition::getClassTypeId()));
state->addTransition(static_cast<ScXMLTransition *>(transitionobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <initial> - must occur iff states+parallels >= 1
else if (!isparallel && strcmp(elementtype, "initial") == 0) {
ScXMLObject * initialobj = ScXMLP::readScXMLInitial(state, element, xmlns);
if (initialobj) {
assert(initialobj->isOfType(ScXMLInitial::getClassTypeId()));
state->setInitial(static_cast<ScXMLInitial *>(initialobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <state> - zero or more times
else if (strcmp(elementtype, "state") == 0) {
ScXMLObject * stateobj = ScXMLP::readScXMLState(state, element, xmlns);
if (stateobj) {
assert(stateobj->isOfType(ScXMLState::getClassTypeId()));
state->addState(static_cast<ScXMLState *>(stateobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <parallel> - zero or more times
else if (strcmp(elementtype, "parallel") == 0) {
ScXMLObject * parallelobj = ScXMLP::readScXMLParallel(state, element, xmlns);
if (parallelobj) {
assert(parallelobj->isOfType(ScXMLState::getClassTypeId()));
state->addParallel(static_cast<ScXMLState *>(parallelobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <final> - zero or more times
else if (!isparallel && strcmp(elementtype, "final") == 0) {
ScXMLObject * finalobj = ScXMLP::readScXMLFinal(state, element, xmlns);
if (finalobj) {
assert(finalobj->isOfType(ScXMLFinal::getClassTypeId()));
state->addFinal(static_cast<ScXMLFinal *>(finalobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <history> - zero or more times
else if (strcmp(elementtype, "history") == 0) {
ScXMLObject * historyobj = ScXMLP::readScXMLHistory(state, element, xmlns);
if (historyobj) {
assert(historyobj->isOfType(ScXMLHistory::getClassTypeId()));
state->addHistory(static_cast<ScXMLHistory *>(historyobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <anchor> - zero or more times
else if (strcmp(elementtype, "anchor") == 0) {
ScXMLObject * anchorobj = ScXMLP::readScXMLAnchor(state, element, xmlns);
if (anchorobj) {
assert(anchorobj->isOfType(ScXMLAnchor::getClassTypeId()));
state->addAnchor(static_cast<ScXMLAnchor *>(anchorobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <datamodel> - zero or one time
else if (strcmp(elementtype, "datamodel") == 0) {
ScXMLObject * datamodelobj = ScXMLP::readScXMLDatamodel(state, element, xmlns);
if (datamodelobj) {
//assert(datamodelobj->isOfType(ScXMLDatamodel::getClassTypeId()));
//state->addDatamodel(static_cast<ScXMLDatamodel *>(datamodelobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
// <invoke> - one time iff states+parallel == 0
else if (!isparallel && strcmp(elementtype, "invoke") == 0) {
ScXMLObject * invokeobj = ScXMLP::readScXMLInvoke(state, element, xmlns);
if (invokeobj) {
assert(invokeobj->isOfType(ScXMLInvoke::getClassTypeId()));
state->setInvoke(static_cast<ScXMLInvoke *>(invokeobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete state;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile",
"unexpected XML element '<%s>' found in <%s>",
elementtype, isparallel ? "parallel" : "state");
delete state;
return NULL;
}
}
return state;
} // readScXMLState
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLParallel(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
return ScXMLP::readScXMLState(container, elt, xmlns, TRUE);
} // readScXMLParallel
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLTransition(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType transitiontype = ScXMLP::getClassType(xmlns, "transition");
assert(transitiontype.canCreateInstance());
ScXMLTransition * transition = static_cast<ScXMLTransition *>(transitiontype.createInstance());
assert(transition && transition->isOfType(ScXMLTransition::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
transition->setXMLAttribute(attrname, attrvalue);
}
if (!transition->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete transition;
return NULL;
}
transition->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// only executable content
if (strcmp(elementtype, "invoke") == 0) {
ScXMLObject * invokeobj = ScXMLP::readScXMLInvoke(transition, element, xmlns);
if (invokeobj) {
assert(invokeobj->isOfType(ScXMLInvoke::getClassTypeId()));
transition->addInvoke(static_cast<ScXMLInvoke *>(invokeobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element",
elementtype);
delete transition;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile",
"unexpected XML element '<%s>' found in <transition>",
elementtype);
delete transition;
return NULL;
}
}
return transition;
} // readScXMLTransition
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLInitial(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType initialtype = ScXMLP::getClassType(xmlns, "initial");
assert(initialtype.canCreateInstance());
ScXMLInitial * initial = static_cast<ScXMLInitial *>(initialtype.createInstance());
assert(initial && initial->isOfType(ScXMLInitial::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
initial->setXMLAttribute(attrname, attrvalue);
}
if (!initial->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete initial;
return NULL;
}
initial->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// <transition>
if (strcmp(elementtype, "transition") == 0) {
ScXMLObject * transitionobj = ScXMLP::readScXMLTransition(initial, element, xmlns);
if (transitionobj) {
assert(transitionobj->isOfType(ScXMLTransition::getClassTypeId()));
assert(initial->getTransition() == NULL);
initial->setTransition(static_cast<ScXMLTransition *>(transitionobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element",
elementtype);
delete initial;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile",
"unexpected XML element '<%s>' found in <initial>",
elementtype);
delete initial;
return NULL;
}
}
return initial;
} // readScXMLInitial
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLFinal(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType finaltype = ScXMLP::getClassType(xmlns, "final");
assert(finaltype.canCreateInstance());
ScXMLFinal * final = static_cast<ScXMLFinal *>(finaltype.createInstance());
assert(final && final->isOfType(ScXMLFinal::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
final->setXMLAttribute(attrname, attrvalue);
}
if (!final->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete final;
return NULL;
}
final->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
SoDebugError::post("ScXML::readFile",
"unexpected XML element '<%s>' found in <final>",
elementtype);
delete final;
return NULL;
}
return final;
} // readScXMLFinal
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLHistory(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType historytype = ScXMLP::getClassType(xmlns, "history");
assert(historytype.canCreateInstance());
ScXMLHistory * history = static_cast<ScXMLHistory *>(historytype.createInstance());
assert(history && history->isOfType(ScXMLHistory::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
history->setXMLAttribute(attrname, attrvalue);
}
if (!history->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete history;
return NULL;
}
history->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// <transition>
if (strcmp(elementtype, "transition") == 0) {
ScXMLObject * transitionobj = ScXMLP::readScXMLTransition(history, element, xmlns);
if (transitionobj) {
assert(transitionobj->isOfType(ScXMLTransition::getClassTypeId()));
assert(history->getTransition() == NULL);
history->setTransition(static_cast<ScXMLTransition *>(transitionobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element",
elementtype);
delete history;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile",
"unexpected XML element '<%s>' found in <history>",
elementtype);
delete history;
return NULL;
}
}
return history;
} // readScXMLHistory
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLOnEntry(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType "onentry");
assert(onentrytype.canCreateInstance());
ScXMLOnEntry * *>(onentrytype.createInstance());
assert(onentry && onentry->isOfType(ScXMLOnEntry::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
onentry->setXMLAttribute(attrname, attrvalue);
}
if (!onentry->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete onentry;
return NULL;
}
onentry->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// invoke
if (strcmp(elementtype, "invoke") == 0) {
ScXMLObject * invokeobj = ScXMLP::readScXMLInvoke(onentry, element, xmlns);
if (invokeobj) {
assert(invokeobj->isOfType(ScXMLInvoke::getClassTypeId()));
onentry->addInvoke(static_cast<ScXMLInvoke *>(invokeobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete onentry;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile", "unexpected XML element '<%s>' found in <onentry>",
elementtype);
delete onentry;
return NULL;
}
}
return onentry;
} // readScXMLOnEntry
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLOnExit(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType "onexit");
assert(onexittype.canCreateInstance());
ScXMLOnExit * *>(onexittype.createInstance());
assert(onexit && onexit->isOfType(ScXMLOnExit::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
onexit->setXMLAttribute(attrname, attrvalue);
}
if (!onexit->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete onexit;
return NULL;
}
onexit->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
// invoke
if (strcmp(elementtype, "invoke") == 0) {
ScXMLObject * invokeobj = ScXMLP::readScXMLInvoke(onexit, element, xmlns);
if (invokeobj) {
assert(invokeobj->isOfType(ScXMLInvoke::getClassTypeId()));
onexit->addInvoke(static_cast<ScXMLInvoke *>(invokeobj));
} else {
SoDebugError::post("ScXML::readFile", "error reading <%s> element", elementtype);
delete onexit;
return NULL;
}
}
else {
SoDebugError::post("ScXML::readFile", "unexpected XML element '<%s>' found in <onentry>",
elementtype);
delete onexit;
return NULL;
}
}
return onexit;
} // readScXMLOnExit
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLAnchor(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
assert(container && elt && xmlns);
SoType anchortype = ScXMLP::getClassType(xmlns, "anchor");
assert(anchortype.canCreateInstance());
ScXMLAnchor * anchor = static_cast<ScXMLAnchor *>(anchortype.createInstance());
assert(anchor && anchor->isOfType(ScXMLAnchor::getClassTypeId()));
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
anchor->setXMLAttribute(attrname, attrvalue);
}
if (!anchor->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "error in handleXMLAttributes()");
delete anchor;
return NULL;
}
anchor->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
SoDebugError::post("ScXML::readFile", "unexpected XML element '<%s>' found in <anchor>",
elementtype);
delete anchor;
return NULL;
}
return anchor;
} // readScXMLAnchor
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLInvoke(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
const char * targettype = "";
const char * source = "";
const int numattrs = cc_xml_elt_get_num_attributes(elt);
const cc_xml_attr ** attrs = cc_xml_elt_get_attributes(elt);
int c;
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
if (strcmp(attrname, "targettype") == 0) {
targettype = cc_xml_attr_get_value(attrs[c]);
}
else if (strcmp(attrname, "src") == 0) {
source = cc_xml_attr_get_value(attrs[c]);
}
}
SoType invoketype = ScXMLP::getInvokeClassType(xmlns, targettype, source);
assert(invoketype.canCreateInstance());
ScXMLInvoke * invoke = static_cast<ScXMLInvoke *>(invoketype.createInstance());
assert(invoke && invoke->isOfType(ScXMLInvoke::getClassTypeId()));
for (c = 0; c < numattrs; ++c) {
const char * attrname = cc_xml_attr_get_name(attrs[c]);
const char * attrvalue = cc_xml_attr_get_value(attrs[c]);
invoke->setXMLAttribute(attrname, attrvalue);
}
if (!invoke->handleXMLAttributes()) {
SoDebugError::postInfo("ScXML::readFile", "%s error in handleXMLAttributes()",
invoketype.getName().getString());
delete invoke;
return NULL;
}
invoke->setContainer(container);
const int numchildren = cc_xml_elt_get_num_children(elt);
for (c = 0; c < numchildren; ++c) {
cc_xml_elt * element = cc_xml_elt_get_child(elt, c);
const char * elementtype = cc_xml_elt_get_type(element);
if (strcmp(elementtype, COIN_XML_CDATA_TYPE) == 0) continue;
SoDebugError::post("ScXML::readFile",
"unexpected XML element '<%s>' found in <invoke>",
elementtype);
delete invoke;
return NULL;
}
return invoke;
} // readScXMLInvoke
// *************************************************************************
// internal
ScXMLObject *
ScXMLP::readScXMLDatamodel(ScXMLObject * container, cc_xml_elt * elt, const char * xmlns)
{
if (!ScXMLP::no_datamodel_warning) {
SoDebugError::postWarning("ScXML::readFile", "<datamodel> elements not supported");
ScXMLP::no_datamodel_warning = TRUE; // don't repeat warning for this read...
}
return NULL;
} // readScXMLDatamodel
void
ScXMLP::init(void)
{
coin_atexit((coin_atexit_f*) ScXMLP::cleanup, CC_ATEXIT_NORMAL);
ScXMLP::namespaces = new ScXMLP::NamespaceDict;
ScXMLP::targettypes = new ScXMLP::TargettypeDict;
}
void
ScXMLP::cleanup(void)
{
cleanup_namespacedict(ScXMLP::namespaces);
delete ScXMLP::namespaces;
ScXMLP::namespaces = NULL;
cleanup_targettypes();
}
void
ScXMLP::cleanup_namespacedict(NamespaceDict * dict)
{
NamespaceDict::iterator it = dict->begin();
while (it != dict->end()) {
delete it->second;
it++;
}
}
void
ScXMLP::cleanup_targettypes(void)
{
assert(ScXMLP::targettypes);
TargettypeDict::iterator it = ScXMLP::targettypes->begin();
while (it != ScXMLP::targettypes->end()) {
ScXMLP::NamespaceDict * dict = it->second;
cleanup_namespacedict(dict);
delete dict;
it++;
}
delete ScXMLP::targettypes;
ScXMLP::targettypes = NULL;
}
// *************************************************************************
|