Ravelin
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 _RAVELIN_XML_TREE_H
8 #define _RAVELIN_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/Origin3f.h>
18 #include <Ravelin/VectorNd.h>
19 #include <Ravelin/VectorNf.h>
20 #include <Ravelin/SVector6d.h>
21 #include <Ravelin/SVector6f.h>
22 #include <Ravelin/MatrixNd.h>
23 #include <Ravelin/MatrixNf.h>
24 #include <Ravelin/Matrix3d.h>
25 #include <Ravelin/Matrix3f.h>
26 #include <Ravelin/Pose3d.h>
27 #include <Ravelin/Pose3f.h>
28 
29 namespace Ravelin {
30 
32 class XMLAttrib
33 {
34  public:
35  XMLAttrib(const std::string& name, const std::string& string_value);
36  XMLAttrib(const std::string& name, double real_value);
37  XMLAttrib(const std::string& name, float real_value);
38  XMLAttrib(const std::string& name, int int_value);
39  XMLAttrib(const std::string& name, unsigned unsigned_value);
40  XMLAttrib(const std::string& name, const std::vector<SVelocityd>& velocity_values);
41  XMLAttrib(const std::string& name, const std::vector<SVelocityf>& velocity_values);
42  XMLAttrib(const std::string& name, const std::vector<SAcceld>& accel_values);
43  XMLAttrib(const std::string& name, const std::vector<SAccelf>& accel_values);
44  XMLAttrib(const std::string& name, double roll, double pitch, double yaw);
45  XMLAttrib(const std::string& name, float roll, float pitch, float yaw);
46  XMLAttrib(const std::string& name, const Vector2d& vector_value);
47  XMLAttrib(const std::string& name, const Vector2f& vector_value);
48  XMLAttrib(const std::string& name, const Vector3d& vector_value);
49  XMLAttrib(const std::string& name, const Vector3f& vector_value);
50  XMLAttrib(const std::string& name, const VectorNd& vector_value);
51  XMLAttrib(const std::string& name, const VectorNf& vector_value);
52  XMLAttrib(const std::string& name, const SVector6d& vector_value);
53  XMLAttrib(const std::string& name, const SVector6f& vector_value);
54  XMLAttrib(const std::string& name, const MatrixNd& matrix_value);
55  XMLAttrib(const std::string& name, const MatrixNf& matrix_value);
56  XMLAttrib(const std::string& name, const Matrix3d& matrix_value);
57  XMLAttrib(const std::string& name, const Matrix3f& matrix_value);
58  XMLAttrib(const std::string& name, const Quatd& quat_value);
59  XMLAttrib(const std::string& name, const Quatf& quat_value);
60  XMLAttrib(const std::string& name, const Origin3d& origin_value);
61  XMLAttrib(const std::string& name, const Origin3f& origin_value);
62  XMLAttrib(const std::string& name, bool bool_value);
63  XMLAttrib(const std::string& name, long long_value);
64  const std::string& get_string_value() { processed = true; return value; }
65  static std::string str(double value);
66  static std::string str(float value);
67  void get_real_value(double& value);
68  void get_real_value(float& value);
69  void get_origin_value(Origin3d& origin);
70  void get_origin_value(Origin3f& origin);
71  void get_velocity_values(std::vector<SVelocityd>& values);
72  void get_velocity_values(std::vector<SVelocityf>& values);
73  void get_accel_values(std::vector<SAcceld>& values);
74  void get_accel_values(std::vector<SAccelf>& values);
75  int get_int_value() { processed = true; return std::atoi(value.c_str()); }
76  unsigned get_unsigned_value() { processed = true; return (unsigned) std::atoi(value.c_str()); }
77  bool get_bool_value();
78  long get_long_value() { return std::atol(value.c_str()); }
79  std::list<std::string> get_strings_value();
80  void get_quat_value(Quatd& q);
81  void get_axis_angle_value(Quatd& q);
82  void get_rpy_value(Quatd& q);
83  void get_quat_value(Quatf& q);
84  void get_axis_angle_value(Quatf& q);
85  void get_rpy_value(Quatf& q);
86  void get_vector_value(VectorNd& v);
87  void get_vector_value(Vector2d& v);
88  void get_vector_value(Vector3d& v);
89  void get_vector_value(SVector6d& v);
90  void get_matrix_value(Matrix3d& m);
91  void get_matrix_value(MatrixNd& m);
92  void get_vector_value(VectorNf& v);
93  void get_vector_value(Vector2f& v);
94  void get_vector_value(Vector3f& v);
95  void get_vector_value(SVector6f& v);
96  void get_matrix_value(Matrix3f& m);
97  void get_matrix_value(MatrixNf& m);
98  bool operator==(const XMLAttrib& a) const { return name == a.name; }
99  bool operator<(const XMLAttrib& a) const { return name < a.name; }
100 
102  std::string name;
103 
105  std::string value;
106 
108  bool processed;
109 };
110 
112 class XMLTree : public boost::enable_shared_from_this<XMLTree>
113 {
114  public:
115  XMLTree(const std::string& name);
116  XMLTree(const std::string& name, const std::list<XMLAttrib>& attributes);
117  static boost::shared_ptr<const XMLTree> read_from_xml(const std::string& name);
118  XMLAttrib* get_attrib(const std::string& attrib_name) const;
119  std::list<boost::shared_ptr<const XMLTree> > find_child_nodes(const std::string& name) const;
120  std::list<boost::shared_ptr<const XMLTree> > find_child_nodes(const std::list<std::string>& name) const;
121  std::list<boost::shared_ptr<const XMLTree> > find_descendant_nodes(const std::string& name) const;
122 
124  void add_child(boost::shared_ptr<XMLTree> child) { children.push_back(child); child->set_parent(shared_from_this()); }
125 
127  void set_parent(boost::shared_ptr<XMLTree> parent) { _parent = parent; }
128 
130  boost::weak_ptr<XMLTree> get_parent() const { return _parent; }
131 
133  std::set<XMLAttrib> attribs;
134 
136  std::list<boost::shared_ptr<XMLTree> > children;
137 
139  std::string name;
140 
142  std::string id;
143 
145  std::string content;
146 
148  boost::shared_ptr<void> object;
149 
151  bool processed;
152 
153  private:
154  boost::weak_ptr<XMLTree> _parent;
155  static boost::shared_ptr<const XMLTree> construct_xml_tree(xmlNode* root);
156 }; // end class
157 
158 std::ostream& operator<<(std::ostream& out, const XMLTree& tree);
159 std::ostream& operator<<(std::ostream& out, const XMLAttrib& attr);
160 
161 } // end namespace
162 
163 #endif
164 
void get_matrix_value(Matrix3d &m)
Gets a list of space-delimited and/or comma-delimited strings from the underlying string value...
Definition: XMLTree.cpp:736
boost::shared_ptr< void > object
The object (if any) represented by this node.
Definition: XMLTree.h:148
void get_origin_value(Origin3d &origin)
Returns an Origin3d value from the attribute.
Definition: XMLTree.cpp:582
boost::weak_ptr< XMLTree > get_parent() const
Gets the parent of this tree (if any)
Definition: XMLTree.h:130
std::string name
The name of this node.
Definition: XMLTree.h:139
bool get_bool_value()
Gets a Boolean value from the underlying string representation.
Definition: XMLTree.cpp:438
std::ostream & operator<<(std::ostream &out, const AANGLE &a)
Sends the representation to the specified stream.
Definition: AAngle.cpp:197
void get_real_value(double &value)
Gets a floating point value from the underlying string representation.
Definition: XMLTree.cpp:406
static std::string str(double value)
Gets a real value as a string.
Definition: XMLTree.cpp:376
std::set< XMLAttrib > attribs
The set of attributes of this node.
Definition: XMLTree.h:133
XMLAttrib(const std::string &name, const std::string &string_value)
Constructs an XMLAttrib object from a name and a string value.
Definition: XMLTree.cpp:22
void add_child(boost::shared_ptr< XMLTree > child)
Adds a child tree to this tree; also sets the parent node.
Definition: XMLTree.h:124
void get_rpy_value(Quatd &q)
Returns a quaternion value from a roll-pitch-yaw attribute.
Definition: XMLTree.cpp:640
bool processed
Indicates whether this attribute has been processed.
Definition: XMLTree.h:108
void set_parent(boost::shared_ptr< XMLTree > parent)
Sets the parent of this tree (if any)
Definition: XMLTree.h:127
std::string name
The name of the attribute.
Definition: XMLTree.h:102
void get_quat_value(Quatd &q)
Returns a quaternion value from the attribute.
Definition: XMLTree.cpp:610
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:1028
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:520
void get_vector_value(VectorNd &v)
Gets a list of space-delimited and/or comma-delimited vectors from the underlying string value...
Definition: XMLTree.cpp:564
XMLAttrib * get_attrib(const std::string &attrib_name) const
Gets the specified attribute.
Definition: XMLTree.cpp:979
An XML tree used for serialization.
Definition: XMLTree.h:112
XMLTree(const std::string &name)
Constructs a XMLTree with no attributes.
Definition: XMLTree.cpp:960
std::list< boost::shared_ptr< XMLTree > > children
The list of children of this node.
Definition: XMLTree.h:136
std::string id
The ID of this node.
Definition: XMLTree.h:142
std::string content
Any 'content' of this node.
Definition: XMLTree.h:145
std::string value
The value in string form.
Definition: XMLTree.h:105
bool processed
Indicates whether this tag has been processed.
Definition: XMLTree.h:151
Attributes used for XML nodes.
Definition: XMLTree.h:32