Simbody  3.5
Xml.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_XML_H_
2 #define SimTK_SimTKCOMMON_XML_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2010-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: Peter Eastman *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
30 
31 #include <iterator>
32 #include <iostream>
33 
34 namespace SimTK {
35 
36 // These are declared but never defined; all TinyXML code is hidden.
37 class TiXmlNode;
38 class TiXmlElement;
39 class TiXmlAttribute;
40 class TiXmlText;
41 class TiXmlComment;
42 class TiXmlUnknown;
43 
266 //------------------------------------------------------------------------------
267 // XML
268 //------------------------------------------------------------------------------
270 public:
271 
272 // These local classes are used to describe the contents of an XML document.
273 class Attribute;
274 class Node; // This is the abstract handle type for any node.
275 class Comment; // These are the concrete node types.
276 class Unknown; // "
277 class Text; // "
278 class Element; // "
279 
283 typedef Xml Document;
284 
285 // This provides iteration over all the attributes found in a given element.
286 class attribute_iterator;
287 
288 // This provides iteration over all the nodes, or nodes of a certain type,
289 // at either the Xml document level or over the child nodes of an element.
290 class node_iterator;
291 
292 // This provides iteration over all the element nodes that are children
293 // of a given element, or over the subset of those child elements that has
294 // a particular tag word.
295 class element_iterator;
296 
302 enum NodeType {
303  NoNode = 0x00,
304  ElementNode = 0x01,
305  TextNode = 0x02,
306  CommentNode = 0x04,
307  UnknownNode = 0x08,
308 
309  NoJunkNodes = ElementNode|TextNode,
310  JunkNodes = CommentNode|UnknownNode,
311  AnyNodes = NoJunkNodes|JunkNodes
312 };
313 
315 static String getNodeTypeAsString(NodeType type);
316 
320 
330 Xml();
331 
338 explicit Xml(const String& pathname);
339 
342 Xml(const Xml::Document& source);
343 
347 Xml::Document& operator=(const Xml::Document& souce);
348 
350 ~Xml();
351 
353 void clear();
363 void readFromFile(const String& pathname);
368 void writeToFile(const String& pathname) const;
372 void readFromString(const String& xmlDocument);
376 void readFromString(const char* xmlDocument);
382 void writeToString(String& xmlDocument, bool compact = false) const;
387 void setIndentString(const String& indent);
390 const String& getIndentString() const;
391 
395 static void setXmlCondenseWhiteSpace(bool shouldCondense);
398 static bool isXmlWhiteSpaceCondensed();
410 
422 Element getRootElement();
423 
426 const String& getRootTag() const;
429 void setRootTag(const String& tag);
430 
438 void insertTopLevelNodeAfter (const node_iterator& afterThis,
439  Node insertThis);
442 void insertTopLevelNodeBefore(const node_iterator& beforeThis,
443  Node insertThis);
449 void eraseTopLevelNode(const node_iterator& deleteThis);
456 Node removeTopLevelNode(const node_iterator& removeThis);
472 node_iterator node_begin(NodeType allowed=AnyNodes);
473 
476 node_iterator node_end() const;
499 String getXmlVersion() const;
502 String getXmlEncoding() const;
508 bool getXmlIsStandalone() const;
509 
512 void setXmlVersion(const String& version);
515 void setXmlEncoding(const String& encoding);
520 void setXmlIsStandalone(bool isStandalone);
523 //------------------------------------------------------------------------------
524  private:
525 friend class Node;
526 
527 class Impl; // a private, local class Xml::Impl
528 const Impl& getImpl() const {assert(impl); return *impl;}
529 Impl& updImpl() {assert(impl); return *impl;}
530 
531 Xml& unconst() const {return *const_cast<Xml*>(this);}
532 
533 Impl* impl; // This is the lone data member.
534 };
535 
540 // Do this inline so we don't have to pass the ostream through the API.
541 inline std::ostream& operator<<(std::ostream& o, const Xml::Document& doc) {
542  String output;
543  doc.writeToString(output);
544  return o << output;
545 }
546 
547 
548 
549 //------------------------------------------------------------------------------
550 // XML ATTRIBUTE
551 //------------------------------------------------------------------------------
558 public:
560 Attribute() : tiAttr(0) {}
563 Attribute(const String& name, const String& value);
567 Attribute(const Attribute& src) : tiAttr(src.tiAttr) {}
573 { if (&src!=this) {clear(); tiAttr=src.tiAttr;} return *this; }
577 ~Attribute() {clear();}
579 bool isValid() const {return tiAttr!=0;}
584 bool isOrphan() const;
586 const String& getName() const;
589 const String& getValue() const;
592 Attribute& setName(const String& name);
596 Attribute& setValue(const String& value);
597 
601 void clear();
605 void clearOrphan();
606 
611 void writeToString(String& out) const;
612 
616 bool operator==(const Attribute& attr) const {return tiAttr==attr.tiAttr;}
617 bool operator!=(const Attribute& attr) const {return tiAttr!=attr.tiAttr;}
618 
619 //------------------------------------------------------------------------------
620  private:
622 friend class Xml::Element;
623 
624 explicit Attribute(TiXmlAttribute* attr) {tiAttr=attr;}
625 const TiXmlAttribute& getTiAttr() const {assert(tiAttr);return *tiAttr;}
626 TiXmlAttribute& updTiAttr() {assert(tiAttr);return *tiAttr;}
627 
628 // Careful; this does not clear the handle before replacing the pointer
629 // so should not be used if this could be the owner handle of an attribute
630 // that hasn't ever been added to a document. It is intended for use by
631 // iterators, whose contained Attributes can never be owners.
632 void setTiAttrPtr(TiXmlAttribute* attr) {tiAttr=attr;}
633 const TiXmlAttribute* getTiAttrPtr() const {return tiAttr;}
634 TiXmlAttribute* updTiAttrPtr() {return tiAttr;}
635 
636 Attribute& unconst() const {return *const_cast<Attribute*>(this);}
637 
638 TiXmlAttribute* tiAttr; // this is the lone data member
639 };
640 
645 // Do this inline so we don't have to pass the ostream through the API.
646 inline std::ostream& operator<<(std::ostream& o, const Xml::Attribute& attr) {
647  String output;
648  attr.writeToString(output);
649  return o << output;
650 }
651 
652 
653 
654 //------------------------------------------------------------------------------
655 // XML ATTRIBUTE ITERATOR
656 //------------------------------------------------------------------------------
660 : public std::iterator<std::bidirectional_iterator_tag, Xml::Attribute> {
661 public:
667 explicit attribute_iterator(Attribute& attr) : attr(attr) {}
671 : attr(src->updTiAttrPtr()) {}
673 ~attribute_iterator() {attr.setTiAttrPtr(0);}
677 { attr.setTiAttrPtr(src->updTiAttrPtr()); return *this; }
681 attribute_iterator& operator++(); // prefix
685 attribute_iterator operator++(int); // postfix
689 attribute_iterator& operator--(); // prefix
693 attribute_iterator operator--(int); // postfix
694 
695 // It's the iterator that's const in these next two methods; it still points
696 // to a non-const object just like a char* const p.
697 
700 Attribute& operator*() const {return const_cast<Attribute&>(attr);}
704 Attribute* operator->() const {return const_cast<Attribute*>(&attr);}
709 bool operator==(const attribute_iterator& other) const
710 { return other.attr==attr; }
712 bool operator!=(const attribute_iterator& other) const
713 { return other.attr!=attr; }
714 
715 //------------------------------------------------------------------------------
716  private:
717 friend class Xml::Element;
718 
719 explicit attribute_iterator(TiXmlAttribute* ap) : attr(ap) {}
720 
721 Attribute attr; // the lone data member
722 };
723 
724 
725 
726 //------------------------------------------------------------------------------
727 // XML NODE
728 //------------------------------------------------------------------------------
753 public:
754 
759 
762 Node() : tiNode(0) {}
766 Node(const Node& src) : tiNode(src.tiNode) {}
771 Node& operator=(const Node& src)
772 { if (&src!=this) {clear(); tiNode=src.tiNode;} return *this; }
776 Node clone() const;
780 ~Node() {clear();}
784 void clear();
788 void clearOrphan();
796 
799 NodeType getNodeType() const;
800 
802 String getNodeTypeAsString() const;
803 
806 bool isValid() const {return tiNode != 0;}
807 
811 bool isTopLevelNode() const;
812 
817 bool isOrphan() const;
818 
822 bool hasParentElement() const;
823 
826 Element getParentElement();
834 
845 const String& getNodeText() const;
846 
853 void writeToString(String& out, bool compact=false) const;
859 bool operator==(const Node& other) const {return other.tiNode==tiNode;}
861 bool operator!=(const Node& other) const {return other.tiNode!=tiNode;}
862 
863 
864 //------------------------------------------------------------------------------
865  protected: // don't let Doxygen see these
867 explicit Node(TiXmlNode* tiNode) : tiNode(tiNode) {}
868 
869 const TiXmlNode& getTiNode() const {assert(tiNode);return *tiNode;}
870 TiXmlNode& updTiNode() {assert(tiNode);return *tiNode;}
871 
872 // Careful: these "Ptr" methods provide raw access to the contained
873 // pointer without any cleanup or error checking. In particular,
874 // setTiNodePtr() does not attempt to delete the current contents.
875 void setTiNodePtr(TiXmlNode* node) {tiNode=node;}
876 const TiXmlNode* getTiNodePtr() const {return tiNode;}
877 TiXmlNode* updTiNodePtr() {return tiNode;}
880 //------------------------------------------------------------------------------
881  private:
882 friend class Xml;
883 friend class Xml::Impl;
884 friend class Xml::node_iterator;
885 friend class Xml::Comment;
886 friend class Xml::Unknown;
887 friend class Xml::Text;
888 friend class Xml::Element;
889 
890 Node& unconst() const {return *const_cast<Node*>(this);}
891 
892 TiXmlNode* tiNode; // the lone data member
893 };
894 
900 // Do this inline so we don't have to pass the ostream through the API.
901 inline std::ostream& operator<<(std::ostream& o, const Xml::Node& xmlNode) {
902  String output;
903  xmlNode.writeToString(output);
904  return o << output;
905 }
906 
907 
908 
909 //------------------------------------------------------------------------------
910 // XML NODE ITERATOR
911 //------------------------------------------------------------------------------
916 : public std::iterator<std::bidirectional_iterator_tag, Xml::Node> {
917 public:
918 
919 explicit node_iterator(NodeType allowed=AnyNodes)
920 : allowed(allowed) {}
921 explicit node_iterator(Node& node, NodeType allowed=AnyNodes)
922 : node(node), allowed(allowed) {}
923 
927 : node(*src), allowed(src.allowed) {}
929 ~node_iterator() {node.setTiNodePtr(0);}
933 { node = *src; allowed = src.allowed; return *this; }
934 
935 node_iterator& operator++(); // prefix
936 node_iterator operator++(int); // postfix
937 node_iterator& operator--(); // prefix
938 node_iterator operator--(int); // postfix
939 Node& operator*() {return node;}
940 Node* operator->() {return &node;}
941 // It's the iterator that's const; it still points to a non-const object
942 // just like a char* const p.
943 Node& operator*() const {return const_cast<Node&>(node);}
944 Node* operator->() const {return const_cast<Node*>(&node);}
945 bool operator==(const node_iterator& other) const {return other.node==node;}
946 bool operator!=(const node_iterator& other) const {return other.node!=node;}
947 
948 //------------------------------------------------------------------------------
949  protected:
950 explicit node_iterator(TiXmlNode* tiNode, NodeType allowed=AnyNodes)
951 : node(tiNode), allowed(allowed) {}
952 void reassign(TiXmlNode* tiNode)
953 { node.setTiNodePtr(tiNode); }
954 
955 //------------------------------------------------------------------------------
956  private:
957 friend class Xml;
958 friend class Xml::Node;
959 friend class Xml::Element;
961 
962 Node node; // data members
963 NodeType allowed;
964 };
965 
966 
967 
968 //------------------------------------------------------------------------------
969 // XML ELEMENT ITERATOR
970 //------------------------------------------------------------------------------
976 : public Xml::node_iterator {
977 public:
978 
981 explicit element_iterator(const String& tag="")
982 : node_iterator(ElementNode), tag(tag) {}
985 inline explicit element_iterator(Element& elt, const String& tag=""); // below
986 
990 : node_iterator(src), tag(src.tag) {}
991 
995 { upcast()=src; tag = src.tag; return *this; }
996 
997 element_iterator& operator++(); // prefix
998 element_iterator operator++(int); // postfix
999 element_iterator& operator--(); // prefix
1000 element_iterator operator--(int); // postfix
1001 inline Element& operator*() const; // below
1002 inline Element* operator->() const; // below
1003 
1004 bool operator==(const element_iterator& other) const
1005 { return other.upcast()==upcast();}
1006 bool operator!=(const element_iterator& other) const
1007 { return other.upcast()!=upcast();}
1008 
1009 //------------------------------------------------------------------------------
1010  private:
1011 friend class Xml;
1012 friend class Xml::Element;
1013 
1014 explicit element_iterator(TiXmlElement* tiElt, const String& tag="")
1015 : node_iterator((TiXmlNode*)tiElt, ElementNode), tag(tag) {}
1016 void reassign(TiXmlElement* tiElt)
1017 { upcast().reassign((TiXmlNode*)tiElt); }
1018 
1019 const node_iterator& upcast() const
1020 { return *static_cast<const node_iterator*>(this); }
1021 node_iterator& upcast()
1022 { return *static_cast<node_iterator*>(this); }
1023 
1024 String tag; // lone data member
1025 };
1026 
1027 
1028 
1029 
1030 //------------------------------------------------------------------------------
1031 // XML ELEMENT
1032 //------------------------------------------------------------------------------
1047 public:
1048 
1056 
1059 Element() : Node() {}
1060 
1073 explicit Element(const String& tagWord, const String& value="");
1074 
1080 template <class T>
1081 Element(const String& tagWord, const T& value)
1082 { new(this) Element(tagWord, String(value)); }
1083 
1087 Element clone() const;
1088 
1091 const String& getElementTag() const;
1093 void setElementTag(const String& tag);
1094 
1100 void insertNodeBefore(const node_iterator& pos, Node node);
1106 void insertNodeAfter(const node_iterator& pos, Node node);
1112 void eraseNode(const node_iterator& deleteThis);
1118 Node removeNode(const node_iterator& removeThis);
1132 
1138 bool isValueElement() const;
1139 
1147 const String& getValue() const;
1148 
1154 String& updValue();
1155 
1161 void setValue(const String& value);
1162 
1166 template <class T>
1167 void setValueAs(const T& value)
1168 { setValue(String(value)); }
1169 
1176 template <class T> T getValueAs() const
1177 { T out; convertStringTo(getValue(),out); return out;}
1178 
1181 template <class T> void getValueAs(T& out) const
1182 { convertStringTo(getValue(),out); }
1183 
1191 const String&
1193 { return unconst().getRequiredElement(tag).getValue(); }
1194 
1198 String
1199 getOptionalElementValue(const String& tag, const String& def="") const
1200 { const Element opt(unconst().getOptionalElement(tag));
1201  return opt.isValid() ? opt.getValue() : def; }
1202 
1211 template <class T> T
1213 { T out; convertStringTo(unconst().getRequiredElementValue(tag), out);
1214  return out; }
1215 
1227 template <class T> T
1228 getOptionalElementValueAs(const String& tag, const T& def) const
1229 { const Element opt(unconst().getOptionalElement(tag));
1230  if (!opt.isValid()) return def;
1231  T out; convertStringTo(opt.getValue(), out); return out; }
1241 bool hasAttribute(const String& name) const;
1242 
1245 void setAttributeValue(const String& name, const String& value);
1246 
1251 void eraseAttribute(const String& name);
1252 
1255 const String&
1257 { return unconst().getRequiredAttribute(name).getValue(); }
1258 
1265 template <class T> T
1267 { T out; convertStringTo(getRequiredAttributeValue(name),out); return out; }
1268 
1275 String
1276 getOptionalAttributeValue(const String& name, const String& def="") const
1277 { Attribute attr = unconst().getOptionalAttribute(name);
1278  if (!attr.isValid()) return def;
1279  return attr.getValue(); }
1280 
1291 template <class T> T
1292 getOptionalAttributeValueAs(const String& name, const T& def) const
1293 { Attribute attr = unconst().getOptionalAttribute(name);
1294  if (!attr.isValid()) return def;
1295  T out; convertStringTo(attr.getValue(), out); return out; }
1296 
1299 Attribute getRequiredAttribute(const String& name);
1300 
1304 Attribute getOptionalAttribute(const String& name);
1305 
1315 { return Array_<Attribute>(attribute_begin(), attribute_end()); }
1316 
1317 
1321 attribute_iterator attribute_begin();
1324 attribute_iterator attribute_end() const;
1338 
1340 bool hasElement(const String& tag) const;
1343 bool hasNode(NodeType allowed=AnyNodes) const;
1344 
1349 Element getRequiredElement(const String& tag);
1350 
1354 Element getOptionalElement(const String& tag);
1355 
1364 { return Array_<Element>(element_begin(tag), element_end()); }
1365 
1373 { return Array_<Node>(node_begin(allowed), node_end()); }
1374 
1379 element_iterator element_begin(const String& tag="");
1382 element_iterator element_end() const;
1383 
1388 node_iterator node_begin(NodeType allowed=AnyNodes);
1391 node_iterator node_end() const;
1399 static bool isA(const Node&);
1402 static const Element& getAs(const Node& node);
1405 static Element& getAs(Node& node);
1408 //------------------------------------------------------------------------------
1409  private:
1410 friend class Xml::Node;
1412 
1413 explicit Element(TiXmlElement* tiElt)
1414 : Node(reinterpret_cast<TiXmlNode*>(tiElt)) {}
1415 
1416 TiXmlElement& updTiElement()
1417 { return reinterpret_cast<TiXmlElement&>(updTiNode()); }
1418 const TiXmlElement& getTiElement() const
1419 { return reinterpret_cast<const TiXmlElement&>(getTiNode()); }
1420 
1421 // Careful: these "Ptr" methods provide raw access to the contained
1422 // pointer without any cleanup or error checking. In particular,
1423 // setTiElementPtr() does not attempt to delete the current contents.
1424 const TiXmlElement* getTiElementPtr() const
1425 { return reinterpret_cast<const TiXmlElement*>(getTiNodePtr()); }
1426 TiXmlElement* updTiElementPtr()
1427 { return reinterpret_cast<TiXmlElement*>(updTiNodePtr()); }
1428 void setTiElementPtr(TiXmlElement* elt)
1429 { setTiNodePtr(reinterpret_cast<TiXmlNode*>(elt)); }
1430 
1431 Element& unconst() const {return *const_cast<Element*>(this);}
1432 
1433 
1434 // no data members; see Node
1435 };
1436 
1437 
1438 
1439 // A few element_iterator inline definitions had to wait for Element to be
1440 // defined.
1442  (Xml::Element& elt, const String& tag)
1443 : Xml::node_iterator(elt, Xml::ElementNode), tag(tag) {}
1445 { return Element::getAs(*upcast());}
1447 { return &Element::getAs(*upcast());}
1448 
1449 
1450 
1451 
1452 //------------------------------------------------------------------------------
1453 // XML TEXT NODE
1454 //------------------------------------------------------------------------------
1457 public:
1460 Text() : Node() {}
1461 
1464 explicit Text(const String& text);
1465 
1469 Text clone() const;
1470 
1473 const String& getText() const;
1476 String& updText();
1477 
1483 static bool isA(const Node&);
1486 static const Text& getAs(const Node& node);
1489 static Text& getAs(Node& node);
1492 //------------------------------------------------------------------------------
1493  private:
1494 // no data members; see Node
1495 
1496 explicit Text(TiXmlText* tiText)
1497 : Node(reinterpret_cast<TiXmlNode*>(tiText)) {}
1498 };
1499 
1500 
1501 
1502 //------------------------------------------------------------------------------
1503 // XML COMMENT NODE
1504 //------------------------------------------------------------------------------
1507 public:
1510 Comment() : Node() {}
1511 
1516 explicit Comment(const String& text);
1517 
1521 Comment clone() const;
1522 
1528 static bool isA(const Node&);
1531 static const Comment& getAs(const Node& node);
1534 static Comment& getAs(Node& node);
1537 //------------------------------------------------------------------------------
1538  private:
1539 // no data members; see Node
1540 
1541 explicit Comment(TiXmlComment* tiComment)
1542 : Node(reinterpret_cast<TiXmlNode*>(tiComment)) {}
1543 };
1544 
1545 
1546 
1547 //------------------------------------------------------------------------------
1548 // XML UNKNOWN NODE
1549 //------------------------------------------------------------------------------
1552 public:
1555 Unknown() : Node() {}
1556 
1562 explicit Unknown(const String& contents);
1563 
1567 Unknown(Element& element, const String& contents)
1568 { new(this) Unknown(contents);
1569  element.insertNodeBefore(element.node_end(), *this); }
1570 
1574 Unknown clone() const;
1575 
1578 const String& getContents() const;
1581 void setContents(const String& contents);
1582 
1588 static bool isA(const Node&);
1591 static const Unknown& getAs(const Node& node);
1594 static Unknown& getAs(Node& node);
1597 //------------------------------------------------------------------------------
1598  private:
1599 // no data members; see Node
1600 
1601 explicit Unknown(TiXmlUnknown* tiUnknown)
1602 : Node(reinterpret_cast<TiXmlNode*>(tiUnknown)) {}
1603 };
1604 
1605 } // namespace SimTK
1606 
1607 #endif // SimTK_SimTKCOMMON_XML_H_
1608 
1609 
Unknown()
Create an empty Unknown node handle, suitable only for holding references to other Unknown nodes...
Definition: Xml.h:1555
Attribute(const Attribute &src)
Copy constructor is shallow; that is, this handle will refer to the same attribute as the source...
Definition: Xml.h:567
node_iterator(TiXmlNode *tiNode, NodeType allowed=AnyNodes)
Definition: Xml.h:950
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:218
const String & getRequiredElementValue(const String &tag) const
Get the text value of a child value element that must be present in this element. ...
Definition: Xml.h:1192
Attribute & operator*() const
Return a writable reference to the Attribute referenced by this iterator; the handle will be invalid ...
Definition: Xml.h:700
~Attribute()
The Attribute handle destructor does not recover heap space so if you create orphan attributes and th...
Definition: Xml.h:577
const String & getValue() const
Get the text value of this value element.
Element * operator->() const
Definition: Xml.h:1446
This is something we don&#39;t understand but can carry around.
Definition: Xml.h:1551
Array_< Attribute > getAllAttributes()
Return an array containing Attribute handles referencing all the attributes of this element...
Definition: Xml.h:1314
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: LICENSE.txt:26
Text()
Create an empty Text node handle, suitable only for holding references to other Text nodes...
Definition: Xml.h:1460
node_iterator & operator=(const node_iterator &src)
Copy assignment takes an node_iterator that can be const, but that still allows writing to the Node...
Definition: Xml.h:932
const String & getRequiredAttributeValue(const String &name) const
Get the value of an attribute as a string and throw an error if that attribute is not present...
Definition: Xml.h:1256
T getOptionalAttributeValueAs(const String &name, const T &def) const
Convert the value of an optional attribute, if present, from a string to the type of the template arg...
Definition: Xml.h:1292
Element(const String &tagWord, const T &value)
Create a new value element and set its initial value to the text equivalent of any type T for which a...
Definition: Xml.h:1081
T getOptionalElementValueAs(const String &tag, const T &def) const
Convert the text value of an optional child value element, if present, to the type of the template ar...
Definition: Xml.h:1228
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
Node()
Create an empty Node handle that can be used to hold a reference to any kind of Node.
Definition: Xml.h:762
node_iterator(NodeType allowed=AnyNodes)
Definition: Xml.h:919
String getOptionalAttributeValue(const String &name, const String &def="") const
Get the value of an attribute as a string if the attribute is present in this element, otherwise return a supplied default value.
Definition: Xml.h:1276
void reassign(TiXmlNode *tiNode)
Definition: Xml.h:952
node_iterator(const node_iterator &src)
Copy constructor takes a node_iterator that can be const, but that still allows writing to the Node...
Definition: Xml.h:926
~Node()
The Node handle destructor does not recover heap space so if you create orphan nodes and then don&#39;t p...
Definition: Xml.h:780
void getValueAs(T &out) const
Alternate form of getValueAs() that avoids unnecessary copying and heap allocation for reading in lar...
Definition: Xml.h:1181
std::ostream & operator<<(std::ostream &o, const Xml::Node &xmlNode)
Output a "pretty printed" textual representation of the given XML node (and all its contents) to an s...
Definition: Xml.h:901
T getRequiredElementValueAs(const String &tag) const
Convert the text value of a required child value element to the type of the template argument T...
Definition: Xml.h:1212
Array_< Node > getAllNodes(NodeType allowed=AnyNodes)
Return an array containing Node handles referencing all the immediate child nodes contained in this e...
Definition: Xml.h:1372
Element & operator*() const
Definition: Xml.h:1444
bool isValid() const
Return true if this Node handle is referencing some node, false if the Node handle is empty...
Definition: Xml.h:806
bool operator==(const Node &other) const
Comparing Nodes for equality means asking if the two Node handles are referring to exactly the same o...
Definition: Xml.h:859
Node * operator->()
Definition: Xml.h:940
bool operator==(const Attribute &attr) const
Comparison operators return true if the same attribute is being referenced or both handles are empty...
Definition: Xml.h:616
attribute_iterator()
Default constructor creates an iterator that compares equal to attribute_end().
Definition: Xml.h:664
element_iterator & operator=(const element_iterator &src)
Copy assignment takes an element_iterator that can be const, but that still allows writing to the Ele...
Definition: Xml.h:994
bool isValid() const
Is this handle currently holding an attribute?
Definition: Xml.h:579
std::ostream & operator<<(std::ostream &o, const Xml::Document &doc)
Output a "pretty printed" textual representation of the given Xml::Document to an std::ostream...
Definition: Xml.h:541
element_iterator(const String &tag="")
This is the default constructor which leaves the element_iterator empty, and you can optionally set t...
Definition: Xml.h:981
This file defines the Array_<T,X> class and related support classes including base classes ArrayViewC...
bool operator!=(const element_iterator &other) const
Definition: Xml.h:1006
This is a bidirectional iterator suitable for moving forward or backward within a list of Attributes ...
Definition: Xml.h:659
Element()
Create an empty Element handle; this is suitable only for holding references to other Elements...
Definition: Xml.h:1059
This is the "leaf" content of an element.
Definition: Xml.h:1456
This class provides a minimalist capability for reading and writing XML documents, as files or strings.
Definition: Xml.h:269
void writeToString(String &xmlDocument, bool compact=false) const
Write the contents of this in-memory Xml::Document to the supplied string.
Node(const Node &src)
Copy constructor is shallow; that is, this handle will refer to the same node as the source...
Definition: Xml.h:766
Attribute & operator=(const Attribute &src)
Copy assignment is shallow; the handle is first cleared and then will refer to the same attribute as ...
Definition: Xml.h:572
node_iterator node_end() const
This node_end() iterator indicates the end of any sequence of nodes regardless of the NodeType restri...
An element has (1) a tagword, (2) a map of (name,value) pairs called attributes, and (3) a list of ch...
Definition: Xml.h:1046
Attribute()
Default constructor creates a null Attribute handle.
Definition: Xml.h:560
const String & getValue() const
If this is a valid attribute handle, get the value of the attribute as a String, not including the qu...
Matrix_< E > operator*(const MatrixBase< E > &l, const typename CNT< E >::StdNumber &r)
Definition: BigMatrix.h:605
The SimTK::Array_<T> container class is a plug-compatible replacement for the C++ standard template l...
Definition: Array.h:50
bool operator!=(const Attribute &attr) const
Definition: Xml.h:617
Attribute * operator->() const
Return a writable pointer to the Attribute referenced by this iterator; the pointer will never be nul...
Definition: Xml.h:704
~node_iterator()
An iterator destructor never deletes the object to which it refers.
Definition: Xml.h:929
void setValueAs(const T &value)
Set the value of this value element to the text equivalent of any type T for which a conversion const...
Definition: Xml.h:1167
NodeType
The NodeType enum serves as the actual type of a node and as a filter for allowable node types during...
Definition: Xml.h:302
Node * operator->() const
Definition: Xml.h:944
bool operator!=(const node_iterator &other) const
Definition: Xml.h:946
Node & operator*() const
Definition: Xml.h:943
bool operator!=(const attribute_iterator &other) const
Uses same criteria as operator==().
Definition: Xml.h:712
std::ostream & operator<<(std::ostream &o, const Xml::Attribute &attr)
Output a textual representation of the given Attribute to an std::ostream.
Definition: Xml.h:646
attribute_iterator & operator=(const attribute_iterator &src)
Copy assignment takes an attribute_iterator that can be const, but that still allows writing to the A...
Definition: Xml.h:676
Element node type and only-Elements filter.
Definition: Xml.h:304
Array_< Element > getAllElements(const String &tag="")
Return an array containing Element handles referencing all the immediate child elements contained in ...
Definition: Xml.h:1363
attribute_iterator(Attribute &attr)
Construct this iterator to point to the same attribute as does the supplied Attribute handle (or attr...
Definition: Xml.h:667
void writeToString(String &out, bool compact=false) const
Serialize this node (and everything it contains) to the given String.
element_iterator(const element_iterator &src)
Copy constructor takes an element_iterator that can be const, but that still allows writing to the El...
Definition: Xml.h:989
bool operator==(const node_iterator &other) const
Definition: Xml.h:945
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
This is a bidirectional iterator suitable for moving forward or backward within a list of Nodes...
Definition: Xml.h:915
node_iterator(Node &node, NodeType allowed=AnyNodes)
Definition: Xml.h:921
Mandatory first inclusion for any Simbody source or header file.
Unknown(Element &element, const String &contents)
Create a new Unknown node and append it to the list of nodes that are children of the given Element...
Definition: Xml.h:1567
Abstract handle for holding any kind of node in an XML tree.
Definition: Xml.h:752
Comment()
Create an empty Comment node handle, suitable only for holding references to other Comment nodes...
Definition: Xml.h:1510
bool operator==(const attribute_iterator &other) const
Comparison return true only if both iterators refer to the same in-memory attribute or both are at at...
Definition: Xml.h:709
Node & operator=(const Node &src)
Copy assignment is shallow; the handle is first cleared and then will refer to the same node as the s...
Definition: Xml.h:771
bool operator!=(const Node &other) const
Inequality test using same criteria as operator==().
Definition: Xml.h:861
A comment contains only uninterpreted text.
Definition: Xml.h:1506
bool operator==(const element_iterator &other) const
Definition: Xml.h:1004
String getOptionalElementValue(const String &tag, const String &def="") const
Get the text value of a child value element that may be present in this element, otherwise return a d...
Definition: Xml.h:1199
This is a bidirectional iterator suitable for moving forward or backward within a list of Element nod...
Definition: Xml.h:975
~attribute_iterator()
An iterator destructor never deletes the object to which it refers.
Definition: Xml.h:673
attribute_iterator(const attribute_iterator &src)
Copy constructor takes an attribute_iterator that can be const, but that still allows writing to the ...
Definition: Xml.h:670
Xml Document
This typedef allows Xml::Document to be used as the type of the document which is more conventional t...
Definition: Xml.h:278
Node & operator*()
Definition: Xml.h:939
Elements can have attributes, which are name="value" pairs that appear within the element start tag i...
Definition: Xml.h:557
T getRequiredAttributeValueAs(const String &name) const
Convert the text value of a required attribute to the type of the template argument T...
Definition: Xml.h:1266
void insertNodeBefore(const node_iterator &pos, Node node)
Insert a node into the list of this Element&#39;s children, just before the node pointed to by the suppli...
void writeToString(String &out) const
Serialize this attribute to the given String.
T getValueAs() const
Assuming this is a "value element", convert its text value to the type of the template argument T...
Definition: Xml.h:1176