nomlib
Loading...
Searching...
No Matches
RapidXmlSerializer.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SERIALIZERS_RAPIDXML_SERIALIZER_HPP
30#define NOMLIB_SERIALIZERS_RAPIDXML_SERIALIZER_HPP
31
32#include <iostream>
33#include <string>
34#include <fstream>
35#include <sstream>
36#include <stdexcept>
37
38#include "nomlib/config.hpp"
39#include "nomlib/serializers/serializers_config.hpp"
40#include "nomlib/serializers/IValueSerializer.hpp"
41#include "nomlib/ptree.hpp"
42
43// Enable dumping output of each key, value pair, sizes, etc. as we
44// traverse the object.
45// #define NOM_DEBUG_RAPIDXML_SERIALIZER_VALUES
46
47// Forward declarations for third-party libraries used; these allow us to not
48// worry about including third-party header files in outside-project builds,
49// such as is the case with TTcards.
50namespace rapidxml {
51
52template<class Ch> class xml_node;
53template<class Ch> class xml_document;
54
55} // namespace rapidxml
56
57namespace nom {
58
64// const std::string RAPIDXML_INDENTION_LEVEL = " ";
65
68class RapidXmlSerializer: public IValueSerializer
69{
70 public:
71 typedef RapidXmlSerializer SelfType;
72
78
81
83 RapidXmlSerializer( enum SerializerOptions options );
84
93 std::string serialize ( const Value& source );
94
105 bool save ( const Value& source, const std::string& filename );
106
107 private:
108 enum SerializerOptions options_;
109
110 // rapidxml::xml_document<> doc;
111
112 bool write( const Value& source, rapidxml::xml_document<char>& dest ) const;
113
114 bool write_value( const Value& object, const std::string& key, rapidxml::xml_document<char>& doc, rapidxml::xml_node<char>* parent ) const;
115
117 bool write_array( const Value& object, const std::string& key, rapidxml::xml_document<char>& doc, rapidxml::xml_node<char>* parent ) const;
118
120 bool write_object( const Value& object, const std::string& key, rapidxml::xml_document<char>& doc, rapidxml::xml_node<char>* parent ) const;
121
122 const char* stralloc( const std::string& buffer, rapidxml::xml_document<char>& dest ) const;
123
135};
136
137} // namespace nom
138
139#endif // include guard defined
140
bool write_object(const Value &object, const std::string &key, rapidxml::xml_document< char > &doc, rapidxml::xml_node< char > *parent) const
Internal helper method for serialization of object nodes.
bool save(const Value &source, const std::string &filename)
Save nom::Value values to XML nodes.
void append_decl(rapidxml::xml_document< char > &dest) const
Add the top-level XML node declaring the version and encoding used (think: DOCTYPE).
RapidXmlSerializer(void)
Default constructor; initialize the writer with human friendly output.
bool write_array(const Value &object, const std::string &key, rapidxml::xml_document< char > &doc, rapidxml::xml_node< char > *parent) const
Internal helper method for serialization of array nodes.
RapidXmlSerializer(enum SerializerOptions options)
Construct a parser with a feature set enumeration.
std::string serialize(const Value &source)
Output a nom::Value object to a string.
~RapidXmlSerializer(void)
Destructor.
Generic interface for opaque data containers.
Definition Value.hpp:43