daeElement.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. */
  8. #ifndef __DAE_ELEMENT_H__
  9. #define __DAE_ELEMENT_H__
  10. #include <string>
  11. #include <dae/daeTypes.h>
  12. #include <dae/daeMemorySystem.h>
  13. #include <wchar.h>
  14. #include <dae/daeArray.h>
  15. #include <dae/daeRefCountedObj.h>
  16. #include <dae/daeSmartRef.h>
  17. //#ifndef NO_MALLOC_HEADER
  18. //#include <malloc.h>
  19. //#endif
  20. namespace COLLADA_TYPE
  21. {
  22. typedef int TypeEnum;
  23. }
  24. class DAE;
  25. class daeMetaElement;
  26. class daeMetaAttribute;
  27. class daeDocument;
  28. class daeURI;
  29. /**
  30. * The @c daeElement class represents an instance of a COLLADA "Element";
  31. * it is the main base class for the COLLADA Dom.
  32. * Features of this class include:
  33. * - Uses factory concepts defined via daeMetaElement
  34. * - Composed of attributes, content elements and content values
  35. * - Reference counted via daeSmartRef
  36. * - Contains information for XML base URI, and XML containing element
  37. */
  38. class DLLSPEC daeElement : public daeRefCountedObj
  39. {
  40. public:
  41. /**
  42. * Macro that defines new and delete overrides for this class
  43. */
  44. DAE_ALLOC
  45. protected:
  46. daeElement* _parent;
  47. daeDocument* _document;
  48. daeMetaElement* _meta;
  49. daeString _elementName;
  50. daeBoolArray _validAttributeArray; // This is now obsolete and can be removed
  51. void* _userData;
  52. protected:
  53. daeElement( const daeElement &cpy ) : daeRefCountedObj() { (void)cpy; };
  54. virtual daeElement &operator=( const daeElement &cpy ) { (void)cpy; return *this; }
  55. void init();
  56. // These functions are called internally.
  57. void setDocument( daeDocument* c, bool notifyDocument );
  58. daeElement* simpleAdd(daeString name, int index = -1);
  59. public:
  60. /**
  61. * Element Constructor.
  62. * @note This should not be used externally.
  63. * Use factories to create elements
  64. */
  65. daeElement();
  66. /**
  67. * Element Constructor.
  68. * @note This should not be used externally.
  69. * Use factories to create elements
  70. */
  71. daeElement(DAE& dae);
  72. /**
  73. * Element Destructor.
  74. * @note This should not be used externally,
  75. * if daeSmartRefs are being used.
  76. */
  77. virtual ~daeElement();
  78. /**
  79. * Sets up a @c daeElement. Called on all @c daeElements as part of their initialization.
  80. * @param meta Meta element to use to configure this element.
  81. * @note Should not be called externally.
  82. */
  83. void setup(daeMetaElement* meta);
  84. // These functions are for adding child elements. They return null if adding
  85. // the element failed.
  86. daeElement* add(daeString name, int index = -1);
  87. daeElement* add(daeElement* elt, int index = -1);
  88. daeElement* addBefore(daeElement* elt, daeElement* index);
  89. daeElement* addAfter(daeElement* elt, daeElement* index);
  90. // These functions are deprecated. Use 'add' instead.
  91. daeElement* createAndPlace(daeString elementName);
  92. daeElement* createAndPlaceAt(daeInt index, daeString elementName);
  93. daeBool placeElement(daeElement* element);
  94. daeBool placeElementAt(daeInt index, daeElement* element);
  95. daeBool placeElementBefore( daeElement* marker, daeElement *element );
  96. daeBool placeElementAfter( daeElement* marker, daeElement *element );
  97. /**
  98. * Finds the last index into the array of children of the name specified.
  99. * @param elementName The name to look for.
  100. * @return Returns the index into the children array of the last element with name elementName. -1 if
  101. * there are no children of name elementName.
  102. */
  103. daeInt findLastIndexOf( daeString elementName );
  104. /**
  105. * Removes the specified element from it parent, the @c this element.
  106. * This function is the opposite of @c placeElement(). It removes the specified
  107. * element from the <tt><i> _contents </i></tt> array, and from wherever else it appears
  108. * inside of the @c this element. Use this function instead of @c clear(), @c remove() or @c delete()
  109. * if you want to keep the <tt><i> _contents </i></tt> field up-to-date.
  110. *
  111. * @param element Element to be removed in the @c this container.
  112. * @return Returns true if the element was successfully removed, false otherwise.
  113. */
  114. daeBool removeChildElement(daeElement* element);
  115. /**
  116. * Removes the specified element from its parent element.
  117. * This function is the opposite of @c placeElement(). It removes the specified
  118. * element from both the <tt><i> _contents </i></tt> array and from wherever else it appears
  119. * inside of its parent. The function itself finds the parent, and is defined as a static method,
  120. * since removing the element from its parent may result in the deletion of the element.
  121. * If the element has no parent, nothing is done.
  122. *
  123. * Use this function instead of @c clear(), @c remove() or @c delete()
  124. * if you want to keep <tt><i> _contents </i></tt> up-to-date.
  125. *
  126. * @param element Element to remove from its parent container, the function finds the parent element.
  127. * @return Returns true if the element was successfully removed, false otherwise.
  128. */
  129. static daeBool removeFromParent(daeElement* element)
  130. {
  131. if(element != NULL && element->_parent != NULL)
  132. return(element->_parent->removeChildElement(element));
  133. return false;
  134. };
  135. /**
  136. * Returns the number of attributes in this element.
  137. * @return The number of attributes this element has.
  138. */
  139. size_t getAttributeCount();
  140. /**
  141. * Returns the daeMetaAttribute object corresponding to the attribute specified.
  142. * @param name The name of the attribute to find.
  143. * @return Returns the corresponding daeMetaAttribute object or NULL if this element
  144. * doesn't have the specified attribute.
  145. */
  146. daeMetaAttribute* getAttributeObject(daeString name);
  147. /**
  148. * Returns the daeMetaAttribute object corresponding to attribute i.
  149. * @param i The index of the attribute to find.
  150. * @return Returns the corresponding daeMetaAttribute object
  151. */
  152. daeMetaAttribute* getAttributeObject(size_t i);
  153. /**
  154. * Returns the name of the attribute at the specified index.
  155. * @param i The index of the attribute whose name should be retrieved.
  156. * @return Returns the name of the attribute, or "" if the index is out of range.
  157. */
  158. std::string getAttributeName(size_t i);
  159. /**
  160. * Checks if this element can have the attribute specified.
  161. * @param name The name of the attribute to look for.
  162. * @return Returns true is this element can have an attribute with the name specified. False otherwise.
  163. */
  164. daeBool hasAttribute(daeString name);
  165. /**
  166. * Checks if an attribute has been set either by being loaded from the COLLADA document or set
  167. * programmatically.
  168. * @param name The name of the attribute to check.
  169. * @return Returns true if the attribute has been set. False if the attribute hasn't been set
  170. * or doesn't exist for this element.
  171. */
  172. daeBool isAttributeSet(daeString name);
  173. /**
  174. * Gets an attribute's value as a string.
  175. * @param name The name of the attribute.
  176. * @return The value of the attribute. Returns an empty string if this element doesn't
  177. * have the specified attribute.
  178. */
  179. std::string getAttribute(daeString name);
  180. /**
  181. * Just like the previous method, this method gets an attribute's value as a string. It
  182. * takes the string as a reference parameter instead of returning it, for extra efficiency.
  183. * @param name The name of the attribute.
  184. * @param A string in which to store the value of the attribute. This will be set to an empty
  185. * string if this element doesn't have the specified attribute.
  186. */
  187. void getAttribute(daeString name, std::string& value);
  188. /**
  189. * Gets an attribute's value as a string.
  190. * @param i The index of the attribute to retrieve.
  191. * @return The value of the attribute.
  192. */
  193. std::string getAttribute(size_t i);
  194. /**
  195. * Just like the previous method, this method gets an attribute's value as a string. It
  196. * takes the string as a reference parameter instead of returning it, for extra efficiency.
  197. * @param i The index of the attribute to retrieve.
  198. * @param A string in which to store the value of the attribute.
  199. */
  200. void getAttribute(size_t i, std::string& value);
  201. struct DLLSPEC attr {
  202. attr();
  203. attr(const std::string& name, const std::string& value);
  204. std::string name;
  205. std::string value;
  206. };
  207. /**
  208. * Returns an array containing all the attributes of this element.
  209. * @return A daeArray of attr objects.
  210. */
  211. daeTArray<attr> getAttributes();
  212. /**
  213. * Just like the previous method, this method returns an array containing all the attributes
  214. * of this element. It returns the result via a reference parameter for extra efficiency.
  215. * @param attrs The array of attr objects to return.
  216. */
  217. void getAttributes(daeTArray<attr>& attrs);
  218. /**
  219. * Sets the attribute to the specified value.
  220. * @param name Attribute to set.
  221. * @param value Value to apply to the attribute.
  222. * @return Returns true if the attribute was found and the value was set, false otherwise.
  223. */
  224. virtual daeBool setAttribute(daeString name, daeString value);
  225. /**
  226. * Sets the attribute at the specified index to the given value.
  227. * @param i Index of the attribute to set.
  228. * @param value Value to apply to the attribute.
  229. * @return Returns true if the attribute was found and the value was set, false otherwise.
  230. */
  231. virtual daeBool setAttribute(size_t i, daeString value);
  232. /**
  233. * Returns the daeMetaAttribute object corresponding to the character data for this element.
  234. * @return Returns a daeMetaAttribute object or NULL if this element doesn't have
  235. * character data.
  236. */
  237. daeMetaAttribute* getCharDataObject();
  238. /**
  239. * Checks if this element can have character data.
  240. * @return Returns true if this element can have character data, false otherwise.
  241. */
  242. daeBool hasCharData();
  243. /**
  244. * Returns this element's character data as a string.
  245. * @return A string containing this element's character data, or an empty string
  246. * if this element can't have character data.
  247. */
  248. std::string getCharData();
  249. /**
  250. * Similar to the previous method, but fills a string passed in by the user for efficiency.
  251. * @param data The string to be filled with this element's character content. The
  252. * string is set to an empty string if this element can't have character data.
  253. */
  254. void getCharData(std::string& data);
  255. /**
  256. * Sets this element's character data.
  257. * @param data The new character data of this element.
  258. * @return Returns true if this element can have character data and the character data
  259. * was successfully changed, false otherwise.
  260. */
  261. daeBool setCharData(const std::string& data);
  262. // These functions are deprecated.
  263. daeMemoryRef getAttributeValue(daeString name); // Use getAttribute or getAttributeObject instead.
  264. daeBool hasValue(); // Use hasCharData instead.
  265. daeMemoryRef getValuePointer(); // Use getCharData or getCharDataObject instead.
  266. /**
  267. * Finds the database document associated with @c this element.
  268. * @return Returns the @c daeDocument representing the containing file or database
  269. * group.
  270. */
  271. daeDocument* getDocument() const { return _document; }
  272. /**
  273. * Deprecated.
  274. */
  275. daeDocument* getCollection() const { return _document; }
  276. /**
  277. * Get the associated DAE object.
  278. * @return The associated DAE object.
  279. */
  280. DAE* getDAE();
  281. /**
  282. * Sets the database document associated with this element.
  283. * @param c The daeDocument to associate with this element.
  284. */
  285. void setDocument(daeDocument* c) { setDocument( c, true ); }
  286. /**
  287. * Deprecated.
  288. */
  289. void setCollection(daeDocument* c );
  290. /**
  291. * Gets the URI of the document containing this element, note that this is NOT the URI of the element.
  292. * @return Returns a pointer to the daeURI of the document containing this element.
  293. */
  294. daeURI* getDocumentURI() const;
  295. /**
  296. * Creates an element via the element factory system. This creation
  297. * is based @em only on potential child elements of this element.
  298. * @param elementName Class name of the subelement to create.
  299. * @return Returns the created @c daeElement, if it was successfully created.
  300. */
  301. daeSmartRef<daeElement> createElement(daeString elementName);
  302. /**
  303. * Gets the container element for @c this element.
  304. * If @c createAndPlace() was used to create the element, its parent is the the caller of @c createAndPlace().
  305. * @return Returns the parent element, if @c this is not the top level element.
  306. */
  307. daeElement* getParentElement() { return _parent;}
  308. /**
  309. * Deprecated. Use getParentElement()
  310. * @deprecated
  311. */
  312. daeElement* getXMLParentElement() { return _parent;}
  313. /**
  314. * Sets the parent element for this element.
  315. * @param newParent The element which is the new parent element for this element.
  316. * @note This function is called internally and not meant to be called form the client application.
  317. */
  318. void setParentElement( daeElement *parent ) { _parent = parent; }
  319. // These are helper structures to let the xml hierarchy search functions know when we've
  320. // found a match. You can implement a custom matcher by inheriting from this structure,
  321. // just like matchName and matchType.
  322. struct DLLSPEC matchElement {
  323. virtual bool operator()(daeElement* elt) const = 0;
  324. virtual ~matchElement() { };
  325. };
  326. // Matches an element by name
  327. struct DLLSPEC matchName : public matchElement {
  328. matchName(daeString name);
  329. virtual bool operator()(daeElement* elt) const;
  330. std::string name;
  331. };
  332. // Matches an element by schema type
  333. struct DLLSPEC matchType : public matchElement {
  334. matchType(daeInt typeID);
  335. virtual bool operator()(daeElement* elt) const;
  336. daeInt typeID;
  337. };
  338. // Returns a matching child element. By "child", I mean one hierarchy level beneath the
  339. // current element. This function is basically the same as getDescendant, except that it
  340. // only goes one level deep.
  341. daeElement* getChild(const matchElement& matcher);
  342. // Performs a breadth-first search and returns a matching descendant element. A "descendant
  343. // element" is an element beneath the current element in the xml hierarchy.
  344. daeElement* getDescendant(const matchElement& matcher);
  345. // Returns the parent element.
  346. daeElement* getParent();
  347. // Searches up through the xml hiearchy and returns a matching element.
  348. daeElement* getAncestor(const matchElement& matcher);
  349. // These functions perform the same as the functions above, except that they take the element
  350. // name to match as a string. This makes these functions a little simpler to use if you're
  351. // matching based on element name, which is assumed to be the most common case. Instead of
  352. // "getChild(matchName(eltName))", you can just write "getChild(eltName)".
  353. daeElement* getChild(daeString eltName);
  354. daeElement* getDescendant(daeString eltName);
  355. daeElement* getAncestor(daeString eltName);
  356. /**
  357. * Gets the associated Meta information for this element. This
  358. * Meta also acts as a factory. See @c daeMetaElement documentation for more
  359. * information.
  360. * @return Returns the associated meta information.
  361. */
  362. inline daeMetaElement* getMeta() { return _meta; }
  363. // These functions are deprecated. Use typeID instead.
  364. virtual COLLADA_TYPE::TypeEnum getElementType() const { return (COLLADA_TYPE::TypeEnum)0; }
  365. daeString getTypeName() const;
  366. /**
  367. * Returns this element's type ID. Every element is an instance of a type specified in
  368. * the Collada schema, and every schema type has a unique ID.
  369. * @return The element's type ID.
  370. */
  371. virtual daeInt typeID() const = 0;
  372. /**
  373. * Gets this element's name.
  374. * @return Returns the string for the name.
  375. * @remarks This function returns NULL if the element's name is identical to it's type's name.
  376. */
  377. daeString getElementName() const;
  378. /**
  379. * Sets this element's name.
  380. * @param nm Specifies the string to use as the element's name.
  381. * @remarks Use caution when using this function since you can easily create invalid COLLADA documents.
  382. */
  383. void setElementName( daeString nm );
  384. /**
  385. * Gets the element ID if it exists.
  386. * @return Returns the value of the ID attribute, if there is such
  387. * an attribute on this element type.
  388. * @return the string for the element ID if it exists.
  389. */
  390. daeString getID() const;
  391. /**
  392. * Gets the children/sub-elements of this element.
  393. * This is a helper function used to easily access an element's children without the use of the
  394. * _meta objects. This function adds the convenience of the _contents array to elements that do
  395. * not contain a _contents array.
  396. * @return The return value. An elementref array to append this element's children to.
  397. */
  398. daeTArray< daeSmartRef<daeElement> > getChildren();
  399. /**
  400. * Same as the previous function, but returns the result via a parameter instead
  401. * of a return value, for extra efficiency.
  402. * @param array The return value. An elementref array to append this element's children to.
  403. */
  404. //void getChildren( daeElementRefArray &array );
  405. void getChildren( daeTArray<daeSmartRef<daeElement> > &array );
  406. /**
  407. * Gets all the children of a particular type.
  408. * @return An array containing the matching child elements.
  409. */
  410. template<typename T>
  411. daeTArray< daeSmartRef<T> > getChildrenByType() {
  412. daeTArray< daeSmartRef<T> > result;
  413. getChildrenByType(result);
  414. return result;
  415. }
  416. /**
  417. * Same as the previous function, but returns the result via a parameter instead
  418. * of a return value, for extra efficiency.
  419. * @return An array containing the matching child elements.
  420. */
  421. template<typename T>
  422. void getChildrenByType(daeTArray< daeSmartRef<T> >& matchingChildren) {
  423. matchingChildren.setCount(0);
  424. daeTArray< daeSmartRef<daeElement> > children;
  425. getChildren(children);
  426. for (size_t i = 0; i < children.getCount(); i++)
  427. if (children[i]->typeID() == T::ID())
  428. matchingChildren.append((T*)children[i].cast());
  429. }
  430. /**
  431. * Clones/deep copies this @c daeElement and all of it's subtree.
  432. * @param idSuffix A string to append to the copied element's ID, if one exists.
  433. * Default is no ID mangling.
  434. * @param nameSuffix A string to append to the copied element's name, if one exists.
  435. * Default is no name mangling.
  436. * @return Returns a @c daeElement smartref of the copy of this element.
  437. */
  438. daeSmartRef<daeElement> clone( daeString idSuffix = NULL, daeString nameSuffix = NULL );
  439. // Class for reporting info about element comparisons
  440. struct DLLSPEC compareResult {
  441. int compareValue; // > 0 if elt1 > elt2,
  442. // < 0 if elt1 < elt2,
  443. // = 0 if elt1 = elt2
  444. daeElement* elt1;
  445. daeElement* elt2;
  446. bool nameMismatch; // true if the names didn't match
  447. std::string attrMismatch; // The name of the mismatched attribute, or "" if there was no attr mismatch
  448. bool charDataMismatch; // true if the char data didn't match
  449. bool childCountMismatch; // true if the number of children didn't match
  450. compareResult();
  451. std::string format(); // Write to a string
  452. };
  453. // Function for doing a generic, recursive comparison of two xml elements. It
  454. // also provides a full element ordering, so that you could store elements in
  455. // a map or a set. Return val is > 0 if elt1 > elt2, < 0 if elt1 < elt2, and 0
  456. // if elt1 == elt2.
  457. static int compare(daeElement& elt1, daeElement& elt2);
  458. // Same as the previous function, but returns a full compareResult object.
  459. static compareResult compareWithFullResult(daeElement& elt1, daeElement& elt2);
  460. /**
  461. * Sets the user data pointer attached to this element.
  462. * @param data User's custom data to store.
  463. */
  464. void setUserData(void* data);
  465. /**
  466. * Gets the user data pointer attached to this element.
  467. * @return User data pointer previously set with setUserData.
  468. */
  469. void* getUserData();
  470. public:
  471. // This function is called internally
  472. static void deleteCMDataArray(daeTArray<daeCharArray*>& cmData);
  473. };
  474. #include <dae/daeSmartRef.h>
  475. typedef daeSmartRef<daeElement> daeElementRef;
  476. typedef daeSmartRef<const daeElement> daeElementConstRef;
  477. //#include <dae/daeArray.h>
  478. typedef daeTArray<daeElementRef> daeElementRefArray;
  479. #endif //__DAE_ELEMENT_H__