Moby
XMLTree.h
1 /****************************************************************************
2  * Copyright 2007 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef _MOBY_XML_TREE_H
8 #define _MOBY_XML_TREE_H
9 
10 #include <boost/enable_shared_from_this.hpp>
11 #include <list>
12 #include <string>
13 #include <set>
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #include <Ravelin/Origin3d.h>
17 #include <Ravelin/VectorNd.h>
18 #include <Ravelin/SVector6d.h>
19 #include <Ravelin/MatrixNd.h>
20 #include <Ravelin/Matrix3d.h>
21 #include <Ravelin/Pose3d.h>
22 #include <Moby/Types.h>
23 
24 namespace Moby {
25 
27 class XMLAttrib
28 {
29  public:
30  XMLAttrib(const std::string& name, const std::string& string_value);
31  XMLAttrib(const std::string& name, double real_value);
32  XMLAttrib(const std::string& name, int int_value);
33  XMLAttrib(const std::string& name, unsigned unsigned_value);
34  XMLAttrib(const std::string& name, const std::vector<Ravelin::SVelocityd>& velocity_values);
35  XMLAttrib(const std::string& name, const std::vector<Ravelin::SAcceld>& accel_values);
36  XMLAttrib(const std::string& name, double roll, double pitch, double yaw);
37  XMLAttrib(const std::string& name, const Ravelin::Vector2d& vector_value);
38  XMLAttrib(const std::string& name, const Ravelin::Vector3d& vector_value);
39  XMLAttrib(const std::string& name, const Ravelin::VectorNd& vector_value);
40  XMLAttrib(const std::string& name, const Ravelin::SVector6d& vector_value);
41  XMLAttrib(const std::string& name, const Ravelin::MatrixNd& matrix_value);
42  XMLAttrib(const std::string& name, const Ravelin::Matrix3d& matrix_value);
43  XMLAttrib(const std::string& name, const Ravelin::Quatd& quat_value);
44  XMLAttrib(const std::string& name, const Ravelin::Origin3d& origin_value);
45  XMLAttrib(const std::string& name, bool bool_value);
46  XMLAttrib(const std::string& name, long long_value);
47  const std::string& get_string_value() { processed = true; return value; }
48  static std::string str(double value);
49  double get_real_value();
50  Ravelin::Origin3d get_origin_value();
51  std::vector<Ravelin::SVelocityd> get_velocity_values();
52  std::vector<Ravelin::SAcceld> get_accel_values();
53  int get_int_value() { processed = true; return std::atoi(value.c_str()); }
54  unsigned get_unsigned_value() { processed = true; return (unsigned) std::atoi(value.c_str()); }
55  bool get_bool_value();
56  long get_long_value() { return std::atol(value.c_str()); }
57  std::list<std::string> get_strings_value();
58  Ravelin::Quatd get_quat_value();
59  Ravelin::Quatd get_axis_angle_value();
60  Ravelin::Quatd get_rpy_value();
61  void get_vector_value(Ravelin::VectorNd& v);
62  void get_vector_value(Ravelin::Vector2d& v);
63  void get_vector_value(Ravelin::Vector3d& v);
64  void get_vector_value(Ravelin::SVector6d& v);
65  void get_matrix_value(Ravelin::Matrix3d& m);
66  void get_matrix_value(Ravelin::MatrixNd& m);
67  bool operator==(const XMLAttrib& a) const { return name == a.name; }
68  bool operator<(const XMLAttrib& a) const { return name < a.name; }
69 
71  std::string name;
72 
74  std::string value;
75 
77  bool processed;
78 };
79 
81 class XMLTree : public boost::enable_shared_from_this<XMLTree>
82 {
83  public:
84  XMLTree(const std::string& name);
85  XMLTree(const std::string& name, const std::list<XMLAttrib>& attributes);
86  static boost::shared_ptr<const XMLTree> read_from_xml(const std::string& name);
87  XMLAttrib* get_attrib(const std::string& attrib_name) const;
88  std::list<boost::shared_ptr<const XMLTree> > find_child_nodes(const std::string& name) const;
89  std::list<boost::shared_ptr<const XMLTree> > find_child_nodes(const std::list<std::string>& name) const;
90  std::list<boost::shared_ptr<const XMLTree> > find_descendant_nodes(const std::string& name) const;
91 
93  void add_child(XMLTreePtr child) { children.push_back(child); child->set_parent(shared_from_this()); }
94 
96  void set_parent(XMLTreePtr parent) { _parent = parent; }
97 
99  boost::weak_ptr<XMLTree> get_parent() const { return _parent; }
100 
102  std::set<XMLAttrib> attribs;
103 
105  std::list<XMLTreePtr> children;
106 
108  std::string name;
109 
111  std::string id;
112 
114  std::string content;
115 
117  boost::shared_ptr<void> object;
118 
120  bool processed;
121 
122  private:
123  boost::weak_ptr<XMLTree> _parent;
124  static boost::shared_ptr<const XMLTree> construct_xml_tree(xmlNode* root);
125 }; // end class
126 
127 std::ostream& operator<<(std::ostream& out, const XMLTree& tree);
128 std::ostream& operator<<(std::ostream& out, const XMLAttrib& attr);
129 
130 } // end namespace
131 
132 #endif
133 
boost::weak_ptr< XMLTree > get_parent() const
Gets the parent of this tree (if any)
Definition: XMLTree.h:99
bool processed
Indicates whether this tag has been processed.
Definition: XMLTree.h:120
boost::shared_ptr< void > object
The object (if any) represented by this node.
Definition: XMLTree.h:117
XMLAttrib * get_attrib(const std::string &attrib_name) const
Gets the specified attribute.
Definition: XMLTree.cpp:607
std::list< std::string > get_strings_value()
Gets a list of space-delimited and/or comma-delimited strings from the underlying string value...
Definition: XMLTree.cpp:338
Ravelin::Origin3d get_origin_value()
Returns an Origin3d value from the attribute.
Definition: XMLTree.cpp:391
static std::string str(double value)
Gets a real value as a string.
Definition: XMLTree.cpp:223
double get_real_value()
Gets a floating point value from the underlying string representation.
Definition: XMLTree.cpp:238
XMLAttrib(const std::string &name, const std::string &string_value)
Constructs an XMLAttrib object from a name and a string value.
Definition: XMLTree.cpp:23
Attributes used for XML nodes.
Definition: XMLTree.h:27
std::string name
The name of the attribute.
Definition: XMLTree.h:71
An XML tree used for serialization.
Definition: XMLTree.h:81
Ravelin::Quatd get_rpy_value()
Returns a quaternion value from a roll-pitch-yaw attribute.
Definition: XMLTree.cpp:424
boost::shared_ptr< XMLTree > XMLTreePtr
XML tree smart pointer.
Definition: Types.h:104
std::list< boost::shared_ptr< const XMLTree > > find_child_nodes(const std::string &name) const
Returns a list of all child nodes (not including further descendants) matching the given name (case i...
Definition: XMLTree.cpp:656
std::string id
The ID of this node.
Definition: XMLTree.h:111
void set_parent(XMLTreePtr parent)
Sets the parent of this tree (if any)
Definition: XMLTree.h:96
std::list< XMLTreePtr > children
The list of children of this node.
Definition: XMLTree.h:105
std::string name
The name of this node.
Definition: XMLTree.h:108
void add_child(XMLTreePtr child)
Adds a child tree to this tree; also sets the parent node.
Definition: XMLTree.h:93
Ravelin::Quatd get_quat_value()
Returns a quaternion value from the attribute.
Definition: XMLTree.cpp:407
bool processed
Indicates whether this attribute has been processed.
Definition: XMLTree.h:77
std::string value
The value in string form.
Definition: XMLTree.h:74
std::string content
Any 'content' of this node.
Definition: XMLTree.h:114
std::set< XMLAttrib > attribs
The set of attributes of this node.
Definition: XMLTree.h:102
XMLTree(const std::string &name)
Constructs a XMLTree with no attributes.
Definition: XMLTree.cpp:588
bool get_bool_value()
Gets a Boolean value from the underlying string representation.
Definition: XMLTree.cpp:256