root/OpenSceneGraph/trunk/include/osgDB/XmlParser @ 10943

Revision 10943, 4.7 kB (checked in by robert, 4 years ago)

From Jean-Sebastien Guay, "OK, so here are new changes.

1. The node type will be set to ATOM on read of <tag prop="..." ... /> type tags.
2. GROUP and NODE are now written using the same code (and not just duplicated code). Also NODE will not be written as an ATOM if it has no children or contents, so you need to set the type to ATOM if you want the <tag ... /> style.
3. You had put the write of "/>" for ATOM after the "return true", so it had no effect... Moved to before the return.
4. ATOM did not write its properties correctly, fixed.
5. As an added bonus, I made the write() method indent the output so it's more readable. It brings a small public interface change but the indent argument has a default value so client code doesn't need to change (if there even is any).
6. Another added bonus, I've simplified the write() method a bit by factoring out the write for children and properties into protected methods."

Line 
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
19namespace osgDB {
20
21// forward declare
22class XmlNode;
23
24/** read an Xml file, find the file in Options DataFilePathList.*/
25extern OSGDB_EXPORT XmlNode* readXmlFile(const std::string& filename,const Options* options);
26
27/** read an Xml file, find the file in osgDB::Registry's eaderWriter::Options DataFilePathList.*/
28inline XmlNode* readXmlFile(const std::string& filename)
29{
30    return readXmlFile(filename, osgDB::Registry::instance()->getOptions());
31}
32
33/** read an Xml from from an istream.*/
34extern OSGDB_EXPORT XmlNode* readXmlStream(std::istream& fin);
35
36extern OSGDB_EXPORT std::string trimEnclosingSpaces(const std::string& str);
37
38/** XmlNode class for very basic reading and writing of xml files.*/
39class OSGDB_EXPORT XmlNode : public osg::Referenced
40{
41    public:
42
43        XmlNode();
44
45        enum NodeType
46        {
47            UNASSIGNED,
48            ATOM,
49            NODE,
50            GROUP,
51            ROOT,
52            COMMENT,
53            INFORMATION
54        };
55
56        typedef std::map< std::string, std::string > Properties;
57        typedef std::vector< osg::ref_ptr<XmlNode> >  Children;
58
59        NodeType        type;
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
69        class OSGDB_EXPORT Input
70        {
71            public:
72
73                Input();
74                Input(const Input&);
75
76                ~Input();
77
78                typedef std::string::size_type size_type;
79
80                void open(const std::string& filename);
81                void attach(std::istream& istream);
82
83                void readAllDataIntoBuffer();
84
85                operator bool () const { return _currentPos<_buffer.size(); }
86
87                size_type currentPosition() const { return _currentPos; }
88
89                int get() { if (_currentPos<_buffer.size()) return _buffer[_currentPos++]; else return -1; }
90
91                int operator [] (size_type i) const { if ((_currentPos+i)<_buffer.size()) return _buffer[_currentPos+i]; else return -1; }
92
93                void operator ++ () { if (_currentPos<_buffer.size()) ++_currentPos; }
94
95                void operator += (size_type n) { if ((_currentPos+n)<_buffer.size()) _currentPos+=n; else _currentPos = _buffer.size(); }
96
97                void skipWhiteSpace();
98
99                std::string substr(size_type pos, size_type n=std::string::npos) { return (_currentPos<_buffer.size()) ? _buffer.substr(_currentPos+pos,n) : std::string(); }
100
101                size_type find(const std::string& str)
102                {
103                    if (_currentPos<_buffer.size())
104                    {
105                        size_type pos = _buffer.find(str, _currentPos);
106                        if (pos==std::string::npos) return std::string::npos;
107                        else return pos-_currentPos;
108                    } else return std::string::npos;
109                }
110
111                bool match(const std::string& str) { return (_currentPos<_buffer.size()) ? _buffer.compare(_currentPos, str.size(), str)==0 : false; }
112
113
114                typedef std::map< std::string, int > ControlToCharacterMap;
115                typedef std::map< int, std::string> CharacterToControlMap;
116
117                void addControlToCharacter(const std::string& control, int c);
118
119                ControlToCharacterMap _controlToCharacterMap;
120                CharacterToControlMap _characterToControlMap;
121
122            private:
123
124                void setUpControlMappings();
125
126                size_type _currentPos;
127
128                std::ifstream _fin;
129                std::string  _buffer;
130
131        };
132
133        bool read(Input& input);
134
135        bool write(std::ostream& fout, const std::string& indent = "") const;
136        bool writeString(std::ostream& fout, const std::string& str) const;
137
138    protected:
139
140        bool writeChildren(std::ostream& fout, const std::string& indent) const;
141        bool writeProperties(std::ostream& fout) const;
142};
143
144}
145#endif
Note: See TracBrowser for help on using the browser.