1 #ifndef SimTK_SimTKCOMMON_XML_H_ 2 #define SimTK_SimTKCOMMON_XML_H_ 309 NoJunkNodes = ElementNode|TextNode,
310 JunkNodes = CommentNode|UnknownNode,
311 AnyNodes = NoJunkNodes|JunkNodes
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;
395 static void setXmlCondenseWhiteSpace(
bool shouldCondense);
398 static bool isXmlWhiteSpaceCondensed();
426 const String& getRootTag()
const;
429 void setRootTag(
const String& tag);
438 void insertTopLevelNodeAfter (
const node_iterator& afterThis,
442 void insertTopLevelNodeBefore(
const node_iterator& beforeThis,
499 String getXmlVersion()
const;
502 String getXmlEncoding()
const;
508 bool getXmlIsStandalone()
const;
512 void setXmlVersion(
const String& version);
515 void setXmlEncoding(
const String& encoding);
520 void setXmlIsStandalone(
bool isStandalone);
528 const Impl& getImpl()
const {assert(impl);
return *impl;}
529 Impl& updImpl() {assert(impl);
return *impl;}
531 Xml& unconst()
const {
return *
const_cast<Xml*
>(
this);}
573 {
if (&src!=
this) {clear(); tiAttr=src.tiAttr;}
return *
this; }
584 bool isOrphan()
const;
586 const String& getName()
const;
589 const String& getValue()
const;
611 void writeToString(
String& out)
const;
624 explicit Attribute(TiXmlAttribute* attr) {tiAttr=attr;}
625 const TiXmlAttribute& getTiAttr()
const {assert(tiAttr);
return *tiAttr;}
626 TiXmlAttribute& updTiAttr() {assert(tiAttr);
return *tiAttr;}
632 void setTiAttrPtr(TiXmlAttribute* attr) {tiAttr=attr;}
633 const TiXmlAttribute* getTiAttrPtr()
const {
return tiAttr;}
634 TiXmlAttribute* updTiAttrPtr() {
return tiAttr;}
638 TiXmlAttribute* tiAttr;
660 :
public std::iterator<std::bidirectional_iterator_tag, Xml::Attribute> {
671 : attr(src->updTiAttrPtr()) {}
677 { attr.setTiAttrPtr(src->updTiAttrPtr());
return *
this; }
710 {
return other.attr==attr; }
713 {
return other.attr!=attr; }
772 {
if (&src!=
this) {clear(); tiNode=src.tiNode;}
return *
this; }
802 String getNodeTypeAsString()
const;
811 bool isTopLevelNode()
const;
817 bool isOrphan()
const;
822 bool hasParentElement()
const;
845 const String& getNodeText()
const;
853 void writeToString(
String& out,
bool compact=
false)
const;
867 explicit Node(TiXmlNode* tiNode) : tiNode(tiNode) {}
869 const TiXmlNode& getTiNode()
const {assert(tiNode);
return *tiNode;}
870 TiXmlNode& updTiNode() {assert(tiNode);
return *tiNode;}
875 void setTiNodePtr(TiXmlNode* node) {tiNode=node;}
876 const TiXmlNode* getTiNodePtr()
const {
return tiNode;}
877 TiXmlNode* updTiNodePtr() {
return tiNode;}
883 friend class Xml::Impl;
890 Node& unconst()
const {
return *
const_cast<Node*
>(
this);}
916 :
public std::iterator<std::bidirectional_iterator_tag, Xml::Node> {
920 : allowed(allowed) {}
922 : node(node), allowed(allowed) {}
927 : node(*src), allowed(src.allowed) {}
933 { node = *src; allowed = src.allowed;
return *
this; }
951 : node(tiNode), allowed(allowed) {}
953 { node.setTiNodePtr(tiNode); }
995 { upcast()=src; tag = src.tag;
return *
this; }
1002 inline Element* operator->()
const;
1005 {
return other.upcast()==upcast();}
1007 {
return other.upcast()!=upcast();}
1015 :
node_iterator((TiXmlNode*)tiElt, ElementNode), tag(tag) {}
1016 void reassign(TiXmlElement* tiElt)
1017 { upcast().
reassign((TiXmlNode*)tiElt); }
1091 const String& getElementTag()
const;
1093 void setElementTag(
const String& tag);
1138 bool isValueElement()
const;
1147 const String& getValue()
const;
1161 void setValue(
const String& value);
1168 { setValue(
String(value)); }
1177 { T out; convertStringTo(getValue(),out);
return out;}
1182 { convertStringTo(getValue(),out); }
1193 {
return unconst().getRequiredElement(tag).getValue(); }
1200 {
const Element opt(unconst().getOptionalElement(tag));
1211 template <
class T> T
1213 { T out; convertStringTo(unconst().getRequiredElementValue(tag), out);
1227 template <
class T> T
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;
1245 void setAttributeValue(
const String& name,
const String& value);
1251 void eraseAttribute(
const String& name);
1257 {
return unconst().getRequiredAttribute(name).getValue(); }
1265 template <
class T> T
1267 { T out; convertStringTo(getRequiredAttributeValue(name),out);
return out; }
1277 {
Attribute attr = unconst().getOptionalAttribute(name);
1278 if (!attr.
isValid())
return def;
1291 template <
class T> T
1293 {
Attribute attr = unconst().getOptionalAttribute(name);
1294 if (!attr.
isValid())
return def;
1295 T out; convertStringTo(attr.
getValue(), out);
return out; }
1340 bool hasElement(
const String& tag)
const;
1343 bool hasNode(
NodeType allowed=AnyNodes)
const;
1373 {
return Array_<Node>(node_begin(allowed), node_end()); }
1399 static bool isA(
const Node&);
1413 explicit Element(TiXmlElement* tiElt)
1414 :
Node(reinterpret_cast<TiXmlNode*>(tiElt)) {}
1416 TiXmlElement& updTiElement()
1417 {
return reinterpret_cast<TiXmlElement&
>(updTiNode()); }
1418 const TiXmlElement& getTiElement()
const 1419 {
return reinterpret_cast<const TiXmlElement&
>(getTiNode()); }
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)); }
1445 {
return Element::getAs(*upcast());}
1447 {
return &Element::getAs(*upcast());}
1473 const String& getText()
const;
1483 static bool isA(
const Node&);
1486 static const Text& getAs(
const Node& node);
1496 explicit Text(TiXmlText* tiText)
1497 :
Node(reinterpret_cast<TiXmlNode*>(tiText)) {}
1528 static bool isA(
const Node&);
1541 explicit Comment(TiXmlComment* tiComment)
1542 :
Node(reinterpret_cast<TiXmlNode*>(tiComment)) {}
1568 {
new(
this)
Unknown(contents);
1578 const String& getContents()
const;
1581 void setContents(
const String& contents);
1588 static bool isA(
const Node&);
1601 explicit Unknown(TiXmlUnknown* tiUnknown)
1602 :
Node(reinterpret_cast<TiXmlNode*>(tiUnknown)) {}
1607 #endif // SimTK_SimTKCOMMON_XML_H_ 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'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
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'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
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
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'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