1 #ifndef SimTK_SimTKCOMMON_XML_H_
2 #define SimTK_SimTKCOMMON_XML_H_
309 NoJunkNodes = ElementNode|TextNode,
310 JunkNodes = CommentNode|UnknownNode,
311 AnyNodes = NoJunkNodes|JunkNodes
315 static String getNodeTypeAsString(NodeType type);
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();
422 Element getRootElement();
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,
449 void eraseTopLevelNode(
const node_iterator& deleteThis);
456 Node removeTopLevelNode(
const node_iterator& removeThis);
472 node_iterator node_begin(NodeType allowed=AnyNodes);
476 node_iterator node_end()
const;
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;}
636 Attribute& unconst()
const {
return *
const_cast<Attribute*
>(
this);}
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) {}
952 void reassign(TiXmlNode* tiNode)
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); }
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); }
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);
1167 void setValueAs(
const T& value)
1168 { setValue(
String(value)); }
1176 template <
class T> T getValueAs()
const
1177 { T out; convertStringTo(getValue(),out);
return out;}
1181 template <
class T>
void getValueAs(T& out)
const
1182 { convertStringTo(getValue(),out); }
1192 getRequiredElementValue(
const String& tag)
const
1193 {
return unconst().getRequiredElement(tag).getValue(); }
1200 {
const Element opt(unconst().getOptionalElement(tag));
1211 template <
class T> T
1212 getRequiredElementValueAs(
const String& tag)
const
1213 { T out; convertStringTo(unconst().getRequiredElementValue(tag), out);
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;
1245 void setAttributeValue(
const String& name,
const String& value);
1251 void eraseAttribute(
const String& name);
1256 getRequiredAttributeValue(
const String& name)
const
1257 {
return unconst().getRequiredAttribute(name).getValue(); }
1265 template <
class T> T
1266 getRequiredAttributeValueAs(
const String& name)
const
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
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; }
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)); }
1431 Element& unconst()
const {
return *
const_cast<Element*
>(
this);}
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_