Source

Coin / src / scxml / ScXMLDocument.cpp

Full commit
Lars J. Aas 5aee4f4 
Marius Kintel 3ba6b41 




























Lars J. Aas 5aee4f4 

Tom Fredrik Blen… f9fcec5 
Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 





Tom Fredrik Blen… a3f87c8 

Tom Fredrik Blen… f9fcec5 
Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 


Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 






























Lars J. Aas 5aee4f4 
Lars J. Aas ea47e7f 

Lars J. Aas ed67f94 
Lars J. Aas ea47e7f 
Tom Fredrik Blen… f9fcec5 






Lars J. Aas ed67f94 
Tom Fredrik Blen… f9fcec5 
































Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 

Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 





Lars J. Aas ed67f94 
Tom Fredrik Blen… f9fcec5 

Lars J. Aas ed67f94 
Tom Fredrik Blen… f9fcec5 

Lars J. Aas ed67f94 

Tom Fredrik Blen… f9fcec5 
Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 













Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 
























































Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 

Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 

Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 












Lars J. Aas ea47e7f 
Lars J. Aas 1ade4e6 

Lars J. Aas 5aee4f4 




Tom Fredrik Blen… f9fcec5 






Lars J. Aas 5aee4f4 









Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 












Jostein 62cd825 
Tom Fredrik Blen… f9fcec5 



































































Lars J. Aas 5aee4f4 

Tom Fredrik Blen… f9fcec5 
Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 





Lars J. Aas 5aee4f4 


Tom Fredrik Blen… f9fcec5 




Lars J. Aas 5aee4f4 

Tom Fredrik Blen… f9fcec5 
Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 
Lars J. Aas 5aee4f4 

Tom Fredrik Blen… f9fcec5 

Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 
Lars J. Aas 5aee4f4 

Tom Fredrik Blen… f9fcec5 

Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 




Lars J. Aas 5aee4f4 
Tom Fredrik Blen… f9fcec5 





Lars J. Aas 5aee4f4 

Tom Fredrik Blen… f9fcec5 















Lars J. Aas 1ade4e6 
  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
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

#include <Inventor/scxml/ScXMLDocument.h>

/*!
  \class ScXMLDocument ScXMLDocument.h Inventor/scxml/Document.h
  The &lt;ScXMLDocument class is a container class for a complete SCXML document.

*/

#include <cassert>
#include <cstring>
#include <map>

#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
#include <boost/intrusive_ptr.hpp>

#include <Inventor/C/XML/document.h>
#include <Inventor/C/XML/element.h>
#include <Inventor/misc/CoinResources.h>
#include <Inventor/scxml/ScXMLScxmlElt.h>
#include <Inventor/scxml/ScXMLStateElt.h>
#include <Inventor/scxml/ScXMLParallelElt.h>
#include <Inventor/scxml/ScXMLHistoryElt.h>
#include <Inventor/scxml/ScXMLAnchorElt.h>
#include <Inventor/scxml/ScXMLDataElt.h>
#include <Inventor/scxml/ScXMLDataModelElt.h>
#include <Inventor/scxml/ScXMLLogElt.h>
#include <Inventor/scxml/ScXMLTransitionElt.h>
#include <Inventor/scxml/ScXMLSendElt.h>
#include <Inventor/scxml/ScXMLInitialElt.h>
#include <Inventor/scxml/ScXMLFinalElt.h>
#include <Inventor/scxml/ScXMLFinalizeElt.h>
#include <Inventor/scxml/ScXMLInvokeElt.h>
#include <Inventor/scxml/ScXMLOnEntryElt.h>
#include <Inventor/scxml/ScXMLOnExitElt.h>
#include <Inventor/scxml/ScXMLAssignElt.h>

#include <Inventor/errors/SoDebugError.h>

#include "coindefs.h"

#ifndef COIN_WORKAROUND_NO_USING_STD_FUNCS
using std::strncmp;
using std::strcmp;
using std::strcpy;
using std::strlen;
#endif // !COIN_WORKAROUND_NO_USING_STD_FUNCS

// *************************************************************************

class ScXMLDocument::PImpl {
public:
  PImpl(void)
  : filename(NULL),
    root(NULL),
    stateidmap(NULL),
    dataidmap(NULL)
  { }
  ~PImpl(void) { }

  boost::scoped_array<char> filename;
  boost::scoped_ptr<ScXMLScxmlElt> root;

  typedef std::map<const char *, ScXMLAbstractStateElt *> StateIdMap;
  typedef std::map<const char *, ScXMLDataElt *> DataIdMap;
  boost::scoped_ptr<StateIdMap> stateidmap;
  boost::scoped_ptr<DataIdMap> dataidmap;

  void fillIdentifierMaps(ScXMLElt * object);

  ScXMLAbstractStateElt * getStateByIdentifier(SbName id) const;
  ScXMLDataElt * getDataById(SbName name) const;

};

void
ScXMLDocument::PImpl::fillIdentifierMaps(ScXMLElt * object)
{
  assert(object);

  if (object->isOfType(ScXMLAbstractStateElt::getClassTypeId())) {
    ScXMLAbstractStateElt * state = static_cast<ScXMLStateElt *>(object);
    SbName id(state->getIdAttribute());
    this->stateidmap->insert(std::pair<const char *, ScXMLAbstractStateElt *>(id.getString(), state));
  }

  if (object->isOfType(ScXMLDataElt::getClassTypeId())) {
    ScXMLDataElt * data = static_cast<ScXMLDataElt *>(object);
    const SbName id(data->getIDAttribute());
    this->dataidmap->insert(std::pair<const char *, ScXMLDataElt *>(id.getString(), data));
  }

  if (object->isOfType(ScXMLAnchorElt::getClassTypeId())) {
    /*ScXMLAnchorElt * anchor = */static_cast<ScXMLAnchorElt *>(object);
  }
  else if (object->isOfType(ScXMLDataElt::getClassTypeId())) {
    /*ScXMLDataElt * data = */static_cast<ScXMLDataElt *>(object);
  }
  else if (object->isOfType(ScXMLScxmlElt::getClassTypeId())) {
    ScXMLScxmlElt * root = static_cast<ScXMLScxmlElt *>(object);
    int c;
    for (c = 0; c < root->getNumStates(); ++c) {
      this->fillIdentifierMaps(root->getState(c));
    }
    for (c = 0; c < root->getNumParallels(); ++c) {
      this->fillIdentifierMaps(root->getParallel(c));
    }
    for (c = 0; c < root->getNumFinals(); ++c) {
      this->fillIdentifierMaps(root->getFinal(c));
    }
  }
  else if (object->isOfType(ScXMLFinalElt::getClassTypeId())) {
    /*ScXMLFinalElt * final = */static_cast<ScXMLFinalElt *>(object);
  }
  else if (object->isOfType(ScXMLHistoryElt::getClassTypeId())) {
    const ScXMLHistoryElt * history = static_cast<ScXMLHistoryElt *>(object);
    if (history->getTransition()) {
      this->fillIdentifierMaps(history->getTransition());
    }
  }
  else if (object->isOfType(ScXMLInitialElt::getClassTypeId())) {
    ScXMLInitialElt * initial = static_cast<ScXMLInitialElt *>(object);
    if (initial->getTransition()) {
      this->fillIdentifierMaps(initial->getTransition());
    }
  }
  else if (object->isOfType(ScXMLInvokeElt::getClassTypeId())) {
    /*ScXMLInvokeElt * invoke = */static_cast<ScXMLInvokeElt *>(object);
  }
  else if (object->isOfType(ScXMLOnEntryElt::getClassTypeId())) {
    ScXMLOnEntryElt * onentry = static_cast<ScXMLOnEntryElt *>(object);
    int c;
    for (c = 0; c < onentry->getNumExecutables(); ++c) {
      this->fillIdentifierMaps(onentry->getExecutable(c));
    }
  }
  else if (object->isOfType(ScXMLOnExitElt::getClassTypeId())) {
    ScXMLOnExitElt * onexit = static_cast<ScXMLOnExitElt *>(object);
    int c;
    for (c = 0; c < onexit->getNumExecutables(); ++c) {
      this->fillIdentifierMaps(onexit->getExecutable(c));
    }
  }
  else if (object->isOfType(ScXMLStateElt::getClassTypeId())) {
    ScXMLStateElt * state = static_cast<ScXMLStateElt *>(object);
    int c;
    if (state->getOnEntry()) {
      this->fillIdentifierMaps(state->getOnEntry());
    }
    if (state->getOnExit()) {
      this->fillIdentifierMaps(state->getOnExit());
    }
    for (c = 0; c < state->getNumTransitions(); ++c) {
      this->fillIdentifierMaps(state->getTransition(c));
    }
    if (state->getInitial()) {
      this->fillIdentifierMaps(state->getInitial());
    }
    for (c = 0; c < state->getNumStates(); ++c) {
      this->fillIdentifierMaps(state->getState(c));
    }
    for (c = 0; c < state->getNumParallels(); ++c) {
      this->fillIdentifierMaps(state->getParallel(c));
    }
    for (c = 0; c < state->getNumFinals(); ++c) {
      this->fillIdentifierMaps(state->getFinal(c));
    }
    for (c = 0; c < state->getNumHistories(); ++c) {
      this->fillIdentifierMaps(state->getHistory(c));
    }
    for (c = 0; c < state->getNumAnchors(); ++c) {
      this->fillIdentifierMaps(state->getAnchor(c));
    }
    if (state->getDataModel()) {
      this->fillIdentifierMaps(state->getDataModel());
    }
  }
  else if (object->isOfType(ScXMLTransitionElt::getClassTypeId())) {
    ScXMLTransitionElt * transition = static_cast<ScXMLTransitionElt *>(object);
    int c;
    for (c = 0; c < transition->getNumExecutables(); ++c) {
      this->fillIdentifierMaps(transition->getExecutable(c));
    }
  }
  else if (object->isOfType(ScXMLLogElt::getClassTypeId())) {
    /*ScXMLLogElt * log = */static_cast<ScXMLLogElt *>(object);
  }
  else if (object->isOfType(ScXMLSendElt::getClassTypeId())) {
    /*ScXMLSendElt * send = */static_cast<ScXMLSendElt *>(object);
  }
  else if (object->isOfType(ScXMLAssignElt::getClassTypeId())) {
    /*ScXMLAssignElt * assign = */static_cast<ScXMLAssignElt *>(object);
  }
  else if (object->isOfType(ScXMLDataModelElt::getClassTypeId())) {
    ScXMLDataModelElt * datamodel = static_cast<ScXMLDataModelElt *>(object);
    for (int c = 0; c < datamodel->getNumData(); ++c) {
      this->fillIdentifierMaps(datamodel->getData(c));
    }
  }
  else {
    SoDebugError::postInfo("ScXMLDocument::fillIdentifierMap",
                           "unsupported object type %s",
                           object->getTypeId().getName().getString());
  }
}

#define PRIVATE(obj) ((obj)->pimpl)

SCXML_OBJECT_SOURCE(ScXMLDocument);

void
ScXMLDocument::initClass(void)
{
  SCXML_OBJECT_INIT_CLASS(ScXMLDocument, ScXMLObject, "ScXMLObject");
}

void
ScXMLDocument::cleanClass(void)
{
  ScXMLDocument::classTypeId = SoType::badType();
}

ScXMLDocument::ScXMLDocument(void)
{
}

ScXMLDocument::~ScXMLDocument(void)
{
}

void intrusive_ptr_add_ref(cc_xml_doc * COIN_UNUSED_ARG(doc)) {
  // nada
}

void intrusive_ptr_release(cc_xml_doc * doc) {
  cc_xml_doc_delete_x(doc);
}

ScXMLDocument *
ScXMLDocument::readFile(const char * filename)
{
  ScXMLDocument * scxmldoc = NULL;

  if (strncmp(filename, "coin:", 5) == 0) { // is a "resource"
    //size_t buffersize = 0;
    SbByteBuffer buffer = CoinResources::get(filename);
    if (likely(buffer.isValid())) {
      scxmldoc = ScXMLDocument::readBuffer(buffer);
      if (scxmldoc) {
        scxmldoc->setFilename(filename);
      }
      return scxmldoc;
    }
  }

  boost::intrusive_ptr<cc_xml_doc> xmldoc(cc_xml_doc_new());
  if (unlikely(!cc_xml_doc_read_file_x(xmldoc.get(), filename))) {
    return NULL;
  }

  scxmldoc = ScXMLDocument::readXMLData(xmldoc.get());
  if (unlikely(!scxmldoc)) {
    return NULL;
  }

  scxmldoc->setFilename(filename);

  return scxmldoc;
}

ScXMLDocument *
ScXMLDocument::readBuffer(const SbByteBuffer & buffer)
{
  if (buffer.size()==0) return NULL;

  boost::intrusive_ptr<cc_xml_doc> xmldoc(cc_xml_doc_new());
  if (unlikely(!cc_xml_doc_read_buffer_x(xmldoc.get(), buffer.constData(), buffer.size()))) {
    return NULL;
  }

  ScXMLDocument * scxmldoc = ScXMLDocument::readXMLData(xmldoc.get());
  if (unlikely(!scxmldoc)) {
    return NULL;
  }

  scxmldoc->setFilename("<memory buffer>");

  return scxmldoc;
}

ScXMLDocument *
ScXMLDocument::readXMLData(cc_xml_doc * xmldoc)
{
  // get a handle on the root element
  cc_xml_elt * root = cc_xml_doc_get_root(xmldoc);
  if (unlikely(strcmp(cc_xml_elt_get_type(root), "scxml") != 0)) {
    SoDebugError::post("ScXMLDocument::readXMLData",
                       "expected root to be an <scxml> element, not '%s'",
                       cc_xml_elt_get_type(root));
    return NULL;
  }

  ScXMLEltReader * reader = ScXMLScxmlElt::getElementReader();
  ScXMLDocument * scxmldoc = new ScXMLDocument;
  ScXMLElt * elt = reader->read(NULL, root, scxmldoc, NULL);
  if (unlikely(!elt)) {
    delete scxmldoc;
    return NULL;
  }
  assert(elt->isOfType(ScXMLScxmlElt::getClassTypeId()));
  scxmldoc->setRoot(static_cast<ScXMLScxmlElt *>(elt));
  return scxmldoc;
}

void
ScXMLDocument::setFilename(const char * filename)
{
  if (filename) {
    const size_t len = strlen(filename);
    PRIVATE(this)->filename.reset(new char [len + 1]);
    strcpy(PRIVATE(this)->filename.get(), filename);
  } else {
    PRIVATE(this)->filename.reset();
  }
}

const char *
ScXMLDocument::getFilename(void) const
{
  return PRIVATE(this)->filename.get();
}

void
ScXMLDocument::setRoot(ScXMLScxmlElt * root)
{
  PRIVATE(this)->root.reset(root);
}

ScXMLScxmlElt *
ScXMLDocument::getRoot(void) const
{
  return PRIVATE(this)->root.get();
}

ScXMLAbstractStateElt *
ScXMLDocument::getStateById(SbName id) const
{
  if (PRIVATE(this)->stateidmap.get() == NULL) {
    ScXMLDocument * thisp = const_cast<ScXMLDocument *>(this);
    PRIVATE(thisp)->stateidmap.reset(new PImpl::StateIdMap);
    PRIVATE(thisp)->dataidmap.reset(new PImpl::DataIdMap);
    PRIVATE(thisp)->fillIdentifierMaps(PRIVATE(this)->root.get());
  }
  std::map<const char *, ScXMLAbstractStateElt *>::const_iterator it =
    PRIVATE(this)->stateidmap->find(id.getString());
  if (it != PRIVATE(this)->stateidmap->end()) {
    return it->second;
  }
  return NULL;
}

ScXMLDataElt *
ScXMLDocument::getDataById(SbName id) const
{
  if (PRIVATE(this)->dataidmap.get() == NULL) {
    ScXMLDocument * thisp = const_cast<ScXMLDocument *>(this);
    PRIVATE(thisp)->stateidmap.reset(new PImpl::StateIdMap);
    PRIVATE(thisp)->dataidmap.reset(new PImpl::DataIdMap);
    PRIVATE(thisp)->fillIdentifierMaps(PRIVATE(this)->root.get());
  }
  std::map<const char *, ScXMLDataElt *>::const_iterator it =
    PRIVATE(this)->dataidmap->find(id.getString());
  if (it != PRIVATE(this)->dataidmap->end()) {
    return it->second;
  }
  return NULL;
}

#undef PRIVATE