DAEChannelTarget.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "Base.h"
  2. #include "DAEChannelTarget.h"
  3. namespace gameplay
  4. {
  5. DAEChannelTarget::DAEChannelTarget(const domChannelRef channelRef) : _channel(channelRef), _targetElement(NULL)
  6. {
  7. const std::string target = channelRef->getTarget();
  8. size_t index = target.find('/');
  9. if (index == std::string::npos)
  10. {
  11. // If the string doesn't contain a '/' then the whole string is the id
  12. // and there are no sid's being targeted.
  13. _targetId = target;
  14. }
  15. else
  16. {
  17. // The targetId is the part before the first '/'
  18. _targetId = target.substr(0, index);
  19. // each '/' denotes another sid
  20. size_t start;
  21. size_t end;
  22. do
  23. {
  24. start = index + 1;
  25. end = target.find('/', start);
  26. std::string sub;
  27. if (end == std::string::npos)
  28. {
  29. sub = target.substr(start);
  30. // break;
  31. }
  32. else
  33. {
  34. sub = target.substr(start, end - start);
  35. index = end + 1;
  36. }
  37. _attributeIds.push_back(sub);
  38. } while (end != std::string::npos);
  39. }
  40. }
  41. DAEChannelTarget::~DAEChannelTarget(void)
  42. {
  43. }
  44. daeElement* DAEChannelTarget::getTargetElement()
  45. {
  46. if (!_targetElement && _targetId.length() > 0)
  47. {
  48. daeSIDResolver resolver(_channel->getDocument()->getDomRoot(), _targetId.c_str());
  49. _targetElement = resolver.getElement();
  50. }
  51. return _targetElement;
  52. }
  53. const std::string& DAEChannelTarget::getTargetId() const
  54. {
  55. return _targetId;
  56. }
  57. size_t DAEChannelTarget::getTargetAttributeCount() const
  58. {
  59. return _attributeIds.size();
  60. }
  61. daeElement* DAEChannelTarget::getTargetAttribute(size_t index)
  62. {
  63. if (index >= _attributeIds.size())
  64. {
  65. return NULL;
  66. }
  67. const std::string& att = _attributeIds[index];
  68. std::string sid = att.substr(0, att.find('.'));
  69. daeSIDResolver resolver(getTargetElement(), sid.c_str());
  70. return resolver.getElement();
  71. }
  72. void DAEChannelTarget::getPropertyName(size_t index, std::string* str)
  73. {
  74. if (index < _attributeIds.size())
  75. {
  76. // The property name is the string segment after the '.'
  77. // The propery is optional so it might not be found.
  78. const std::string& att = _attributeIds[index];
  79. size_t i = att.find('.');
  80. if (i != std::string::npos && i < att.size())
  81. {
  82. str->assign(att.substr(i+1));
  83. return;
  84. }
  85. }
  86. str->clear();
  87. }
  88. }