Ocean
XMLParser.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SCENEDESCRIPTION_SDX_X3D_XML_PARSER_H
9 #define META_OCEAN_SCENEDESCRIPTION_SDX_X3D_XML_PARSER_H
10 
13 
14 #include "ocean/io/Scanner.h"
15 
20 
21 #include <tinyxml2.h>
22 
23 namespace Ocean
24 {
25 
26 namespace SceneDescription
27 {
28 
29 namespace SDX
30 {
31 
32 namespace X3D
33 {
34 
35 /**
36  * This class implements a XML parser for X3D.
37  * @ingroup scenedescriptionsdxx3d
38  */
39 class OCEAN_SCENEDESCRIPTION_SDX_X3D_EXPORT XMLParser
40 {
41  protected:
42 
43  /**
44  * This class implements a helper scanner for the XML X3D file format.
45  */
46  class OCEAN_SCENEDESCRIPTION_SDX_X3D_EXPORT XMLScanner : virtual public IO::Scanner
47  {
48  public:
49 
50  /**
51  * Creates a new XML scanner.
52  * @param data The data to be parsed
53  * @see BufferScanner::BufferScanner().
54  */
55  explicit XMLScanner(const std::string& data);
56  };
57 
58  public:
59 
60  /**
61  * Creates a new XML parser.
62  * @param filename Name of the file to parse
63  * @param progress Optional parser progress state recurrently receiving the parsing progress if defined
64  * @param cancel Optional cancel state to stop the progress during process
65  */
66  explicit XMLParser(const std::string& filename, float* progress = nullptr, bool* cancel = nullptr);
67 
68  /**
69  * Parses the given file and returns a node reference to the entire scene.
70  * @param library The library providing all nodes
71  * @param engine Rendering engine object to create corresponding rendering object from
72  * @param timestamp Parsing timestamp all scene objects will be initialized with this timestamp
73  * @return Parsed scene
74  */
75  SDXSceneRef parse(const Library& library, const Rendering::EngineRef& engine, const Timestamp timestamp = Timestamp());
76 
77  protected:
78 
79  /**
80  * Parses a new node.
81  * @param xmlNode XML node to be parsed
82  * @return Resulting X3D node
83  */
84  NodeRef parseNode(const tinyxml2::XMLElement* xmlNode);
85 
86  /**
87  * Parses a new node.
88  * @param parent The parent X3D node
89  * @param xmlNode XML node to be parsed
90  */
91  void parseNodeField(NodeRef& parent, const tinyxml2::XMLElement* xmlNode);
92 
93  /**
94  * Parses a route statement
95  * @param xmlNode XML node to be parsed
96  */
97  void parseRoute(const tinyxml2::XMLElement* xmlNode);
98 
99  /**
100  * Adds a dynamic field.
101  * @param parent The parent X3D node
102  * @param xmlNode XML node to be parsed
103  */
104  void addDynamicField(NodeRef& parent, const tinyxml2::XMLElement* xmlNode);
105 
106  /**
107  * Parses a field.
108  * @param field The field receiving the parsed data
109  * @param value The value to be parsed
110  * @return True, if succeeded
111  */
112  bool parseValueField(Field& field, const std::string& value);
113 
114  /**
115  * Parses a SFBool field.
116  * @param field The field receiving the parsed data
117  * @param value The value to be parsed
118  * @return True, if succeeded
119  */
120  bool parseValueField(SingleBool& field, const std::string& value);
121 
122  /**
123  * Parses a SFColor field.
124  * @param field The field receiving the parsed data
125  * @param value The value to be parsed
126  * @return True, if succeeded
127  */
128  bool parseValueField(SingleColor& field, const std::string& value);
129 
130  /**
131  * Parses a SFFloat field.
132  * @param field The field receiving the parsed data
133  * @param value The value to be parsed
134  * @return True, if succeeded
135  */
136  bool parseValueField(SingleFloat& field, const std::string& value);
137 
138  /**
139  * Parses a SFInt32 field.
140  * @param field The field receiving the parsed data
141  * @param value The value to be parsed
142  * @return True, if succeeded
143  */
144  bool parseValueField(SingleInt& field, const std::string& value);
145 
146  /**
147  * Parses a SFMatrix3 field.
148  * @param field The field receiving the parsed data
149  * @param value The value to be parsed
150  * @return True, if succeeded
151  */
152  bool parseValueField(SingleMatrix3& field, const std::string& value);
153 
154  /**
155  * Parses a SFMatrix4 field.
156  * @param field The field receiving the parsed data
157  * @param value The value to be parsed
158  * @return True, if succeeded
159  */
160  bool parseValueField(SingleMatrix4& field, const std::string& value);
161 
162  /**
163  * Parses a SFRotation field.
164  * @param field The field receiving the parsed data
165  * @param value The value to be parsed
166  * @return True, if succeeded
167  */
168  bool parseValueField(SingleRotation& field, const std::string& value);
169 
170  /**
171  * Parses a SFString field.
172  * @param field The field receiving the parsed data
173  * @param value The value to be parsed
174  * @return True, if succeeded
175  */
176  bool parseValueField(SingleString& field, const std::string& value);
177 
178  /**
179  * Parses a SFTime field.
180  * @param field The field receiving the parsed data
181  * @param value The value to be parsed
182  * @return True, if succeeded
183  */
184  bool parseValueField(SingleTime& field, const std::string& value);
185 
186  /**
187  * Parses a SFVec2f field.
188  * @param field The field receiving the parsed data
189  * @param value The value to be parsed
190  * @return True, if succeeded
191  */
192  bool parseValueField(SingleVector2& field, const std::string& value);
193 
194  /**
195  * Parses a SFVec3f field.
196  * @param field The field receiving the parsed data
197  * @param value The value to be parsed
198  * @return True, if succeeded
199  */
200  bool parseValueField(SingleVector3& field, const std::string& value);
201 
202  /**
203  * Parses a SFVec4f field.
204  * @param field The field receiving the parsed data
205  * @param value The value to be parsed
206  * @return True, if succeeded
207  */
208  bool parseValueField(SingleVector4& field, const std::string& value);
209 
210  /**
211  * Parses a MFBool field.
212  * @param field The field receiving the parsed data
213  * @param value The value to be parsed
214  * @return True, if succeeded
215  */
216  bool parseValueField(MultiBool& field, const std::string& value);
217 
218  /**
219  * Parses a MFColor field.
220  * @param field The field receiving the parsed data
221  * @param value The value to be parsed
222  * @return True, if succeeded
223  */
224  bool parseValueField(MultiColor& field, const std::string& value);
225 
226  /**
227  * Parses a MFFloat field.
228  * @param field The field receiving the parsed data
229  * @param value The value to be parsed
230  * @return True, if succeeded
231  */
232  bool parseValueField(MultiFloat& field, const std::string& value);
233 
234  /**
235  * Parses a MFInt32 field.
236  * @param field The field receiving the parsed data
237  * @param value The value to be parsed
238  * @return True, if succeeded
239  */
240  bool parseValueField(MultiInt& field, const std::string& value);
241 
242  /**
243  * Parses a MFMatrix3f field.
244  * @param field The field receiving the parsed data
245  * @param value The value to be parsed
246  * @return True, if succeeded
247  */
248  bool parseValueField(MultiMatrix3& field, const std::string& value);
249 
250  /**
251  * Parses a MFMatrix4f field.
252  * @param field The field receiving the parsed data
253  * @param value The value to be parsed
254  * @return True, if succeeded
255  */
256  bool parseValueField(MultiMatrix4& field, const std::string& value);
257 
258  /**
259  * Parses a MFRotation field.
260  * @param field The field receiving the parsed data
261  * @param value The value to be parsed
262  * @return True, if succeeded
263  */
264  bool parseValueField(MultiRotation& field, const std::string& value);
265 
266  /**
267  * Parses a MFString field.
268  * @param field The field receiving the parsed data
269  * @param value The value to be parsed
270  * @return True, if succeeded
271  */
272  bool parseValueField(MultiString& field, const std::string& value);
273 
274  /**
275  * Parses a MFTime field.
276  * @param field The field receiving the parsed data
277  * @param value The value to be parsed
278  * @return True, if succeeded
279  */
280  bool parseValueField(MultiTime& field, const std::string& value);
281 
282  /**
283  * Parses a MFVec2f field.
284  * @param field The field receiving the parsed data
285  * @param value The value to be parsed
286  * @return True, if succeeded
287  */
288  bool parseValueField(MultiVector2& field, const std::string& value);
289 
290  /**
291  * Parses a MFVec3f field.
292  * @param field The field receiving the parsed data
293  * @param value The value to be parsed
294  * @return True, if succeeded
295  */
296  bool parseValueField(MultiVector3& field, const std::string& value);
297 
298  /**
299  * Parses a MFVec4f field.
300  * @param field The field receiving the parsed data
301  * @param value The value to be parsed
302  * @return True, if succeeded
303  */
304  bool parseValueField(MultiVector4& field, const std::string& value);
305 
306  /**
307  * Parses a bool value.
308  * @param scanner The scanner to be used for parsing
309  * @return Parsed bool value
310  */
311  bool parseBool(IO::Scanner& scanner);
312 
313  /**
314  * Parses a float value.
315  * @param scanner The scanner to be used for parsing
316  * @return Parsed float value
317  */
319 
320  /**
321  * Parses an integer value.
322  * @param scanner The scanner to be used for parsing
323  * @return Parsed float value
324  */
325  int parseInt(IO::Scanner& scanner);
326 
327  /**
328  * Parses a string value.
329  * @param scanner The scanner to be used for parsing
330  * @return Parsed string value
331  */
332  std::string parseString(IO::Scanner& scanner);
333 
334  /**
335  * Parses a rotation value.
336  * @param scanner The scanner to be used for parsing
337  * @return Parsed rotation value
338  */
340 
341  /**
342  * The lookup function for node field.
343  * @param parent The parent node in that the field has to be found
344  * @param nodeType Type of the child node for that the corresponding field has to be found
345  * @return Resulting field name
346  */
347  static std::string fieldNameLookup(const NodeRef& parent, const std::string& nodeType);
348 
349  protected:
350 
351  /// XML parser.
352  tinyxml2::XMLDocument xmlDocument_;
353 
354  /// XML file opening result.
355  tinyxml2::XMLError xmlOpenResult_ = tinyxml2::XML_SUCCESS;
356 
357  /// X3D scene holding the parsed scene.
358  X3DScene* scene_ = nullptr;
359 
360  /// Scene filename.
361  std::string sceneFilename_;
362 
363  /// Scene timestamp.
365 };
366 
367 }
368 
369 }
370 
371 }
372 
373 }
374 
375 #endif // META_OCEAN_SCENEDESCRIPTION_SDX_X3D_XML_PARSER_H
This class implements a simple scanner.
Definition: Scanner.h:31
This class implements a axis-angle rotation using floating point values.
Definition: Rotation.h:79
This class implements all 1D fields (fields holding an array of single values).
Definition: Field1D.h:110
This class is the base class for all scene description fields.
Definition: Field.h:36
This class is the base class for all scene description libraries.
Definition: scenedescription/Library.h:37
This class implements a scene object holding an entire x3d scene.
Definition: X3DScene.h:39
This class implements a helper scanner for the XML X3D file format.
Definition: XMLParser.h:47
XMLScanner(const std::string &data)
Creates a new XML scanner.
This class implements a XML parser for X3D.
Definition: XMLParser.h:40
bool parseValueField(SingleInt &field, const std::string &value)
Parses a SFInt32 field.
bool parseValueField(Field &field, const std::string &value)
Parses a field.
bool parseValueField(MultiFloat &field, const std::string &value)
Parses a MFFloat field.
bool parseValueField(SingleColor &field, const std::string &value)
Parses a SFColor field.
bool parseValueField(MultiMatrix3 &field, const std::string &value)
Parses a MFMatrix3f field.
bool parseValueField(SingleMatrix4 &field, const std::string &value)
Parses a SFMatrix4 field.
void addDynamicField(NodeRef &parent, const tinyxml2::XMLElement *xmlNode)
Adds a dynamic field.
bool parseValueField(SingleString &field, const std::string &value)
Parses a SFString field.
bool parseValueField(MultiVector4 &field, const std::string &value)
Parses a MFVec4f field.
bool parseValueField(MultiInt &field, const std::string &value)
Parses a MFInt32 field.
void parseNodeField(NodeRef &parent, const tinyxml2::XMLElement *xmlNode)
Parses a new node.
tinyxml2::XMLDocument xmlDocument_
XML parser.
Definition: XMLParser.h:352
std::string sceneFilename_
Scene filename.
Definition: XMLParser.h:361
bool parseValueField(MultiColor &field, const std::string &value)
Parses a MFColor field.
bool parseBool(IO::Scanner &scanner)
Parses a bool value.
Timestamp sceneTimestamp_
Scene timestamp.
Definition: XMLParser.h:364
bool parseValueField(SingleVector4 &field, const std::string &value)
Parses a SFVec4f field.
bool parseValueField(MultiBool &field, const std::string &value)
Parses a MFBool field.
NodeRef parseNode(const tinyxml2::XMLElement *xmlNode)
Parses a new node.
bool parseValueField(SingleVector3 &field, const std::string &value)
Parses a SFVec3f field.
bool parseValueField(MultiVector2 &field, const std::string &value)
Parses a MFVec2f field.
bool parseValueField(SingleVector2 &field, const std::string &value)
Parses a SFVec2f field.
XMLParser(const std::string &filename, float *progress=nullptr, bool *cancel=nullptr)
Creates a new XML parser.
bool parseValueField(SingleMatrix3 &field, const std::string &value)
Parses a SFMatrix3 field.
bool parseValueField(MultiVector3 &field, const std::string &value)
Parses a MFVec3f field.
bool parseValueField(SingleBool &field, const std::string &value)
Parses a SFBool field.
Rotation parseRotation(IO::Scanner &scanner)
Parses a rotation value.
static std::string fieldNameLookup(const NodeRef &parent, const std::string &nodeType)
The lookup function for node field.
Scalar parseFloat(IO::Scanner &scanner)
Parses a float value.
bool parseValueField(SingleRotation &field, const std::string &value)
Parses a SFRotation field.
bool parseValueField(MultiRotation &field, const std::string &value)
Parses a MFRotation field.
SDXSceneRef parse(const Library &library, const Rendering::EngineRef &engine, const Timestamp timestamp=Timestamp())
Parses the given file and returns a node reference to the entire scene.
std::string parseString(IO::Scanner &scanner)
Parses a string value.
int parseInt(IO::Scanner &scanner)
Parses an integer value.
bool parseValueField(SingleFloat &field, const std::string &value)
Parses a SFFloat field.
bool parseValueField(SingleTime &field, const std::string &value)
Parses a SFTime field.
void parseRoute(const tinyxml2::XMLElement *xmlNode)
Parses a route statement.
bool parseValueField(MultiTime &field, const std::string &value)
Parses a MFTime field.
bool parseValueField(MultiMatrix4 &field, const std::string &value)
Parses a MFMatrix4f field.
bool parseValueField(MultiString &field, const std::string &value)
Parses a MFString field.
This class implements a timestamp.
Definition: Timestamp.h:36
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15