vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ReaderWriter.h
Go to the documentation of this file.
1 #ifndef READERWRITER_H
2 #define READERWRITER_H
3 
4 #include <vrecko/BaseClass.h>
5 #include <vrecko/Export>
6 #include <vrecko/Ability.h>
7 
8 
9 #include <fstream>
10 #include <vector>
11 #include <map>
12 #include <string>
13 
14 #include <osg/Vec2>
15 #include <osg/Vec4>
16 #include <osg/Matrix>
17 #include <osg/Group>
18 #include <osgDB/DynamicLibrary>
19 
20 #ifdef WIN32
21 #include <windows.h>
22 #endif
23 
24 // xercesc includes
25 #include <xercesc/parsers/AbstractDOMParser.hpp>
26 #include <xercesc/dom/DOMErrorHandler.hpp>
27 #include <xercesc/dom/DOMBuilder.hpp>
28 #include <xercesc/util/XMLString.hpp>
29 #include <helpers/XercesXMLUtils.h>
30 #if defined(XERCES_NEW_IOSTREAMS)
31 #include <iostream>
32 #else
33 #include <iostream.h>
34 #endif
35 
36 //--------------------
37 
38 #define CREATOR_FILENAME_ID_SIMPLE "_XMLCreatorFileName_"
39 #define CREATOR_FILENAME_ID L ## "_XMLCreatorFileName_"
40  // XML attribute name that hold the filename of the creator the respective element
41 
42 
43 namespace vrecko {
44 class VRECKO_EXPORT World;
45 class VRECKO_EXPORT Scene;
46 class VRECKO_EXPORT EnvironmentObject;
47 class VRECKO_EXPORT DeviceManager;
48 class VRECKO_EXPORT EventDispatcher;
49 }
50 
51 namespace vrecko {
52 
53 // Xercesc classes ---------------------------------------------------------------
54 
55 // ---------------------------------------------------------------------------
56 // Simple error handler deriviative to install on parser
57 // ---------------------------------------------------------------------------
58 class RWErrorHandler : public XERCES_CPP_NAMESPACE_QUALIFIER DOMErrorHandler
59 {
60  public:
63 
64  // Getter methods
65  bool getSawErrors() const;
66 
67  // Implementation of the DOM ErrorHandler interface
68  bool handleError(const XERCES_CPP_NAMESPACE_QUALIFIER DOMError& domError);
69  void resetErrors();
70 
71  private :
72  // Unimplemented constructors and operators
74  void operator=(const RWErrorHandler&);
75 
76 
77  // -----------------------------------------------------------------------
78  // Private data members
79  //
80  // fSawErrors
81  // This is set if we get any errors, and is queryable via a getter
82  // method. Its used by the main code to suppress output if there are
83  // errors.
84  // -----------------------------------------------------------------------
85  bool fSawErrors;
86 };
87 
88 
89 
90 inline bool RWErrorHandler::getSawErrors() const {
91  return fSawErrors;
92 }
93 // ----------------------------------------------------------------------------------
94 
95 
96 #ifdef WIN32
97 typedef HANDLE VRECKOMutex;
98 #else
99 typedef pthread_mutex* VRECKOMutex;
100 #endif
101 
102 
104 class VRECKO_EXPORT ReaderWriter {
105  protected:
107  public:
108  AbilityToInitialize(Ability* _ab, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *_params) { ability = _ab; parameters = _params; };
109  Ability *ability;
110  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parameters;
111  };
112 
115  typedef std::vector<AbilityToInitialize> AbInitVector;
116 
117  public:
120 
125  bool loadMultipleFiles(std::vector<std::string> &files, float *loadingPosition, std::string *loadingCategory, VRECKOMutex load_Mutex);
126 
128  bool loadOld(const char *configName, float *loadingPosition, std::string *loadingCategory, VRECKOMutex load_Mutex);
129 
138  int save(const char *configName, const char *originalSceneFile, float *savingPosition, std::string *savingCategory, VRECKOMutex save_Mutex);
139 
140 
145  static XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *findNodeByName(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, int level = 1);
146 
147  /* Find a node within children of the specified node. */
148  static XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *findChildByName(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const XMLCh* xChChildNodeName, int level = 1);
149 
152  static void getStringValue(std::string &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, const std::string &failReturn = "", int level = 1);
153 
156  static const XMLCh* getStringValueXCh(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parentNode, const XMLCh* childNodeName);
157 
160  static bool getBoolValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, bool failReturn = false, int level = 1);
161 
164  static int getIntValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, int failReturn = 0, int level = 1);
165 
168  static long int getLongIntValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, long int failReturn = 0, int level = 1);
169 
172  static float getFloatValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, float failReturn = 0.0, int level = 1);
173 
176  static double getDoubleValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, double failReturn = 0.0, int level = 1);
177 
180  static void getVec2Value(osg::Vec2 &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, const osg::Vec2 &failReturn = osg::Vec2(0.0, 0.0), int level = 1);
181 
184  static void getVec3Value(osg::Vec3 &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, const osg::Vec3 &failReturn = osg::Vec3(0.0, 0.0, 0.0), int level = 1);
185 
188  static void getVec4Value(osg::Vec4 &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, const osg::Vec4 &failReturn = osg::Vec4(0.0, 0.0, 0.0, 0.0), int level = 1);
189 
192  static void getMatrixValue(osg::Matrix &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, const osg::Matrix &failReturn = osg::Matrix::identity(), int level = 1);
193 
196  static void getQuatValue(osg::Quat &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &nodeName, const osg::Quat &failReturn = osg::Quat(), int level = 1);
197 
205  static bool getAttribStringValue(std::string &returnValue, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const std::string &attribName, const std::string &failReturn = "");
206 
207 
209  static XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *createElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, const std::string &elementName);
210 
212  static XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *appendElementAndStringValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, const std::string &value);
213 
215  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndBoolValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, bool value);
216 
218  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndIntValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, int value);
219 
221  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndLongIntValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, long int value);
222 
224  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndFloatValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, float value);
225 
227  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndDoubleValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, double value);
228 
230  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndVec2Value(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, osg::Vec2 value);
231 
233  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndVec3Value(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, osg::Vec3 value);
234 
236  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndVec4Value(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, osg::Vec4 value);
237 
239  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndMatrixValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, osg::Matrix value);
240 
242  static XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *appendElementAndQuatValue(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParentNode, const std::string &elementName, osg::Quat value);
243 
244  protected:
246  bool loadVirtualEnvironment(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *veNode);
248  bool loadScene(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *sceneNode, AbInitVector &abilitiesToInitialize);
250  bool loadDevice(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *deviceNode);
252  bool loadAbilities(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *eoNode, bool bLoadingWorldAbility, AbInitVector &abilitiesToInitialize);
254  vrecko::EnvironmentObject *loadEnvironmentObject(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *eoNode, osg::Group *parentSceneNode);
256  bool loadEvent(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *eventNode);
257 
259  void saveEnvironmentObject(const char *originalSceneFile, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parentNode, vrecko::EnvironmentObject *pEO);
261  void saveChildren(const char *originalSceneFile, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDocNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *eoNode, vrecko::EnvironmentObject *pEO);
262 
267  void analyzeXMLTree(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pNode);
268 
270  unsigned int getNodeCountByName(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pNode, const std::string &nodeName);
271 
276  XERCES_CPP_NAMESPACE_QUALIFIER DOMBuilder * createParser();
277 
282  void releaseParser(XERCES_CPP_NAMESPACE_QUALIFIER DOMBuilder * parser);
283 
287  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *loadXMLDocument(const char *configName, XERCES_CPP_NAMESPACE_QUALIFIER DOMBuilder *parser);
288 
292  bool findRealFile(char *outputFileWithPath, const char* inputFile, const char* currentDir, std::vector<std::string> *requiredBy = NULL, bool bOutputErrors = true);
293 
295  void removeUnnecessaryWhitespace(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pNode);
296 
298  bool saveXMLDocument(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDoc, const char *strPath);
299 
303  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *mergeFiles(std::vector<std::string> &files, XERCES_CPP_NAMESPACE_QUALIFIER DOMBuilder *parser);
304 
306  bool processNodeToMergeRecursive(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *mainNode,
307  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *secNode, const XMLCh *xChConfigFile);
308 
310  bool removeAttribRecursive(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const XMLCh *attribName);
311 
312 
313  enum FDNNodeType {
314  FDNSimpleNode = 0,
315  // for any generic node, that
316  // 1) We don't know
317  // OR
318  // 2) Either cannot have any siblings of the same name
319  // OR
320  // Can have siblings of the same name, but is not distinguishable
321  // (So if we will find existing node with the same name, we can take it as a duplicate)
323  // This is a case of Phantom (Device) filters.
330  FDNReceiver, // this is Event/Receiver
331 // FDNParameters,
332  };
333 
335  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *findDuplicateNode(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *mainParentNode, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *origNode);
336 
340  bool getNodeIdentification(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, FDNNodeType nodeType, XMLCh *strId1, XMLCh *strId2);
341 
343  bool addCreatorInfoRecursive(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, const XMLCh *xChConfigFile);
344 
347  void addSceneFileInfoIfPossible(vrecko::BaseClass *bc, XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node);
348 
349 
350 /* enum {
351  NONE = 0,
352  SCENE,
353  ENVIRONMENTOBJECT,
354  ABILITY
355  };*/
356 
357  std::string _cfg_file, _cfg_path;
358 // std::map<std::string, osgDB::DynamicLibrary *> mapDL;
359 // osgDB::DynamicLibrary *pDL;
360 // std::vector<vrecko::EnvironmentObject *> _parent_EO;
361 // std::ifstream _fr;
362 
366 
367  unsigned int nodes_count, act_node;
368 
370  bool incrementActNode();
371 
372 #ifdef WIN32
373  HANDLE loadsave_Mutex;
374 #else
375  pthread_mutex_t *loadsave_Mutex;
376 #endif
378  std::string *loadsave_category;
379 
380  private:
381  const char* xmlFile;
382  XERCES_CPP_NAMESPACE_QUALIFIER AbstractDOMParser::ValSchemes valScheme;
383  bool doNamespaces;
384  bool doSchema;
385  bool schemaFullChecking;
386  bool errorOccurred;
387  bool recognizeNEL;
388  char localeStr[64];
389 
390 
391 };
392 
393 // ---------------------------------------------------------------------------
394 // This is a simple class that lets us do easy (though not terribly efficient)
395 // trancoding of char* data to XMLCh data.
396 // ---------------------------------------------------------------------------
398 {
399  public :
400  // -----------------------------------------------------------------------
401  // Constructors and Destructor
402  // -----------------------------------------------------------------------
403  XercesStr(const char* const toTranscode)
404  {
405  // Call the private transcoding method
406  fUnicodeForm = XERCES_CPP_NAMESPACE_QUALIFIER XMLString::transcode(toTranscode);
407  }
408 
410  {
411  XERCES_CPP_NAMESPACE_QUALIFIER XMLString::release(&fUnicodeForm);
412  }
413 
414 
415  // -----------------------------------------------------------------------
416  // Getter methods
417  // -----------------------------------------------------------------------
418  const XMLCh* unicodeForm() const
419  {
420  return fUnicodeForm;
421  }
422 
423  private :
424  // -----------------------------------------------------------------------
425  // Private data members
426  //
427  // fUnicodeForm
428  // This is the Unicode XMLCh format of the string.
429  // -----------------------------------------------------------------------
430  XMLCh* fUnicodeForm;
431 };
432 
433 #define strToXerces(str) XercesStr(str).unicodeForm()
434 
435 
436 
437 }
438 
439 #endif
440