OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XmlParser.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSGDB_XML_PARSER
15 #define OSGDB_XML_PARSER 1
16 
17 #include <osgDB/Registry>
18 
19 namespace osgDB {
20 
21 // forward declare
22 class XmlNode;
23 
25 extern OSGDB_EXPORT XmlNode* readXmlFile(const std::string& filename,const Options* options);
26 
28 inline XmlNode* readXmlFile(const std::string& filename)
29 {
30  return readXmlFile(filename, osgDB::Registry::instance()->getOptions());
31 }
32 
34 extern OSGDB_EXPORT XmlNode* readXmlStream(std::istream& fin);
35 
36 extern OSGDB_EXPORT std::string trimEnclosingSpaces(const std::string& str);
37 
40 {
41  public:
42 
43  XmlNode();
44 
45  enum NodeType
46  {
53  INFORMATION
54  };
55 
56  typedef std::map< std::string, std::string > Properties;
57  typedef std::vector< osg::ref_ptr<XmlNode> > Children;
58 
60  std::string name;
61  std::string contents;
62  Properties properties;
63  Children children;
64 
65  std::string getTrimmedContents() const { return trimEnclosingSpaces(contents); }
66 
67  public:
68 
70  {
71  public:
72  ControlMap();
73 
74  typedef std::map< std::string, int > ControlToCharacterMap;
75  typedef std::map< int, std::string> CharacterToControlMap;
76 
77  void addControlToCharacter(const std::string& control, int c);
78 
79  ControlToCharacterMap _controlToCharacterMap;
80  CharacterToControlMap _characterToControlMap;
81 
82  private:
83 
84  void setUpControlMappings();
85 
86  };
87 
88  class OSGDB_EXPORT Input : public ControlMap
89  {
90  public:
91 
92  Input();
93  Input(const Input&);
94 
95  ~Input();
96 
97  typedef std::string::size_type size_type;
98 
99  void open(const std::string& filename);
100  void attach(std::istream& istream);
101 
102  void readAllDataIntoBuffer();
103 
104  operator bool () const { return _currentPos<_buffer.size(); }
105 
106  size_type currentPosition() const { return _currentPos; }
107 
108  int get() { if (_currentPos<_buffer.size()) return _buffer[_currentPos++]; else return -1; }
109 
110  int operator [] (size_type i) const { if ((_currentPos+i)<_buffer.size()) return _buffer[_currentPos+i]; else return -1; }
111 
112  void operator ++ () { if (_currentPos<_buffer.size()) ++_currentPos; }
113 
114  void operator += (size_type n) { if ((_currentPos+n)<_buffer.size()) _currentPos+=n; else _currentPos = _buffer.size(); }
115 
116  void skipWhiteSpace();
117 
118  std::string substr(size_type pos, size_type n=std::string::npos) { return (_currentPos<_buffer.size()) ? _buffer.substr(_currentPos+pos,n) : std::string(); }
119 
120  size_type find(const std::string& str)
121  {
122  if (_currentPos<_buffer.size())
123  {
124  size_type pos = _buffer.find(str, _currentPos);
125  if (pos==std::string::npos) return std::string::npos;
126  else return pos-_currentPos;
127  } else return std::string::npos;
128  }
129 
130  bool match(const std::string& str) { return (_currentPos<_buffer.size()) ? _buffer.compare(_currentPos, str.size(), str)==0 : false; }
131 
132  private:
133 
134  size_type _currentPos;
135 
136  std::ifstream _fin;
137  std::string _buffer;
138 
139  };
140 
141  bool read(Input& input);
142  bool write(std::ostream& fout, const std::string& indent = "") const;
143 
144  bool write(const ControlMap& controlMap, std::ostream& fout, const std::string& indent = "") const;
145  bool writeString(const ControlMap& controlMap, std::ostream& fout, const std::string& str) const;
146 
147  protected:
148 
149  bool writeChildren(const ControlMap& controlMap, std::ostream& fout, const std::string& indent) const;
150  bool writeProperties(const ControlMap& controlMap, std::ostream& fout) const;
151 
152  bool readAndReplaceControl(std::string& contents, Input& input);
153 };
154 
155 }
156 #endif
std::map< std::string, std::string > Properties
Definition: XmlParser.h:56
size_type find(const std::string &str)
Definition: XmlParser.h:120
void OSGDB_EXPORT open(std::fstream &fs, const char *filename, std::ios_base::openmode mode)
std::string contents
Definition: XmlParser.h:61
OSGDB_EXPORT XmlNode * readXmlFile(const std::string &filename, const Options *options)
std::string::size_type size_type
Definition: XmlParser.h:97
Properties properties
Definition: XmlParser.h:62
std::string name
Definition: XmlParser.h:60
std::vector< osg::ref_ptr< XmlNode > > Children
Definition: XmlParser.h:57
std::map< int, std::string > CharacterToControlMap
Definition: XmlParser.h:75
ControlToCharacterMap _controlToCharacterMap
Definition: XmlParser.h:79
OSGDB_EXPORT std::string trimEnclosingSpaces(const std::string &str)
size_type currentPosition() const
Definition: XmlParser.h:106
std::map< std::string, int > ControlToCharacterMap
Definition: XmlParser.h:74
Definition: Archive.h:24
#define OSGDB_EXPORT
Definition: Export.h:39
NodeType type
Definition: XmlParser.h:59
static Registry * instance(bool erase=false)
Children children
Definition: XmlParser.h:63
OSGDB_EXPORT XmlNode * readXmlStream(std::istream &fin)
bool match(const std::string &str)
Definition: XmlParser.h:130
CharacterToControlMap _characterToControlMap
Definition: XmlParser.h:80
std::string getTrimmedContents() const
Definition: XmlParser.h:65
std::string substr(size_type pos, size_type n=std::string::npos)
Definition: XmlParser.h:118